'Constructor
@param tablename: tablename, Table, or an S3Resource instance
@param prefix: prefix to use for the tablename
@param id: record ID (or list of record IDs)
@param uid: record UID (or list of record UIDs)
@param filter: filter query
@param vars: dictionary of URL query variables
@param components: list of component aliases
to load for this resource
@param filter_component: alias of the component the URL filters
apply for (filters for this component
must be handled separately)
@param alias: the alias for this resource (internal use only)
@param parent: the parent resource (internal use only)
@param linked: the linked resource (internal use only)
@param linktable: the link table (internal use only)
@param include_deleted: include deleted records (used for
synchronization)
@param approved: include approved records
@param unapproved: include unapproved records
@param context: apply context filters
@param extra_filters: extra filters (to be applied on
pre-filtered subsets), as list of
tuples (method, expression)'

def __init__(self, tablename, id=None, prefix=None, uid=None, filter=None, vars=None, parent=None, linked=None, linktable=None, alias=None, components=None, filter_component=None, include_deleted=False, approved=True, unapproved=False, context=False, extra_filters=None):

    s3db = current.s3db
    auth = current.auth
    table = None
    table_alias = None
    if (prefix is None):
        if (not isinstance(tablename, basestring)):
            if isinstance(tablename, Table):
                table = tablename
                table_alias = table._tablename
                tablename = table_alias
            elif isinstance(tablename, S3Resource):
                table = tablename.table
                table_alias = table._tablename
                tablename = tablename.tablename
            else:
                error = ('%s    is    not    a    valid    type    for    a    tablename' % tablename)
                raise SyntaxError(error)
        if ('_' in tablename):
            (prefix, name) = tablename.split('_', 1)
        else:
            raise SyntaxError(('invalid    tablename:    %s' % tablename))
    else:
        name = tablename
        tablename = ('%s_%s' % (prefix, name))
    self.tablename = tablename
    self.prefix = prefix
    self.name = name
    if (not alias):
        alias = name
    self.alias = alias
    if (table is None):
        table = s3db[tablename]
    auth.permission.set_default_approver(table)
    if (parent is not None):
        if (parent.tablename == self.tablename):
            table_alias = ('%s_%s_%s' % (prefix, alias, name))
            pkey = table._id.name
            table = table.with_alias(table_alias)
            table._id = table[pkey]
    self.table = table
    self._alias = (table_alias or tablename)
    self.fields = table.fields
    self._id = table._id
    self.defaults = None
    self.accessible_query = auth.s3_accessible_query
    self.include_deleted = include_deleted
    self._approved = approved
    self._unapproved = unapproved
    self.filter = None
    self.rfilter = None
    self._rows = None
    self._rowindex = None
    self.rfields = None
    self.dfields = None
    self._ids = []
    self._uids = []
    self._length = None
    self.vars = None
    self.lastid = None
    self.files = Storage()
    self.link = None
    self.linktable = None
    self.actuate = None
    self.lkey = None
    self.rkey = None
    self.pkey = None
    self.fkey = None
    self.multiple = True
    self.parent = parent
    self.linked = linked
    self.components = Storage()
    self.links = Storage()
    if (parent is None):
        attach = self._attach
        hooks = s3db.get_components(table, names=components)
        for component_alias in hooks:
            attach(component_alias, hooks[component_alias])
        self.build_query(id=id, uid=uid, filter=filter, vars=vars, extra_filters=extra_filters, filter_component=filter_component)
        if context:
            self.add_filter(s3db.context)
    elif (linktable is not None):
        link_alias = ('%s__link' % self.alias)
        self.link = S3Resource(linktable, alias=link_alias, parent=self.parent, linked=self, include_deleted=self.include_deleted, approved=self._approved, unapproved=self._unapproved)
    self.skip_import = False
    self.job = None
    self.mtime = None
    self.error = None
    self.error_tree = None
    self.import_count = 0
    self.import_created = []
    self.import_updated = []
    self.import_deleted = []
    self.muntil = None
    self.results = None
    from s3crud import S3CRUD
    self.crud = S3CRUD()
    self.crud.resource = self
