1616from .user_tables import Part , Computed , Imported , Manual , Lookup
1717from .table import lookup_class_name , Log , FreeTable
1818import types
19- from .plugin import override
2019
2120logger = logging .getLogger (__name__ )
2221
@@ -281,8 +280,8 @@ def repl(s):
281280 body = '\n \n \n ' .join (make_class_definition (table ) for table in diagram .topological_sort ())
282281 python_code = '\n \n \n ' .join ((
283282 '"""This module was auto-generated by datajoint from an existing schema"""' ,
284- "import datajoint as dj\n \n schema = dj.schema ('{db}')" .format (db = db ),
285- '\n ' .join ("{module} = dj.create_virtual_module ('{module}', '{schema_name}')" .format (module = v , schema_name = k )
283+ "import datajoint as dj\n \n schema = dj.Schema ('{db}')" .format (db = db ),
284+ '\n ' .join ("{module} = dj.VirtualModule ('{module}', '{schema_name}')" .format (module = v , schema_name = k )
286285 for k , v in module_lookup .items ()), body ))
287286 if python_filename is None :
288287 return python_code
@@ -291,29 +290,30 @@ def repl(s):
291290 f .write (python_code )
292291
293292
294- def create_virtual_module (module_name , schema_name , * ,
295- create_schema = False , create_tables = False , connection = None , add_objects = None ):
293+ class VirtualModule (types .ModuleType ):
296294 """
297- Creates a python module with the given name from the name of a schema on the server and
298- automatically adds classes to it corresponding to the tables in the schema.
299- :param module_name: displayed module name
300- :param schema_name: name of the database in mysql
301- :param create_schema: if True, create the schema on the database server
302- :param create_tables: if True, module.schema can be used as the decorator for declaring new
303- :param connection: a dj.Connection object to pass into the schema
304- :param add_objects: additional objects to add to the module
305- :return: the python module containing classes from the schema object and the table classes
295+ A virtual module which will contain context for schema.
306296 """
307- module = types .ModuleType (module_name )
308- _schema = Schema (schema_name , create_schema = create_schema , create_tables = create_tables , connection = connection )
309- if add_objects :
310- module .__dict__ .update (add_objects )
311- module .__dict__ ['schema' ] = _schema
312- _schema .spawn_missing_classes (context = module .__dict__ )
313- return module
314-
315-
316- override ('schema' , globals ())
297+ def __init__ (self , module_name , schema_name , * , create_schema = False ,
298+ create_tables = False , connection = None , add_objects = None ):
299+ """
300+ Creates a python module with the given name from the name of a schema on the server and
301+ automatically adds classes to it corresponding to the tables in the schema.
302+ :param module_name: displayed module name
303+ :param schema_name: name of the database in mysql
304+ :param create_schema: if True, create the schema on the database server
305+ :param create_tables: if True, module.schema can be used as the decorator for declaring new
306+ :param connection: a dj.Connection object to pass into the schema
307+ :param add_objects: additional objects to add to the module
308+ :return: the python module containing classes from the schema object and the table classes
309+ """
310+ super (VirtualModule , self ).__init__ (name = module_name )
311+ _schema = Schema (schema_name , create_schema = create_schema , create_tables = create_tables ,
312+ connection = connection )
313+ if add_objects :
314+ self .__dict__ .update (add_objects )
315+ self .__dict__ ['schema' ] = _schema
316+ _schema .spawn_missing_classes (context = self .__dict__ )
317317
318318
319319def list_schemas (connection = None ):
0 commit comments