def add_model_file(package, modul, id, clazz, mixins): target_file = os.path.join(get_app_location(package), package, 'model', '%s.py' % modul) print 'Adding new model file "%s"... ' % target_file, # Build mixins mixinclasses = [] for mixin in mixins: mixinclasses.append(mixinmap[mixin]) try: tablename = modul + 's' label = modul.capitalize() label_plural = label + "s" clazzpath = ".".join([package, 'model', modul, label]) values = { 'modul': modul, 'clazzpath': clazzpath, 'label': label, 'label_plural': label_plural, 'modul': modul, 'table': tablename, 'id': id, 'clazz': clazz, 'mixins': mixinclasses } template = template_lookup.get_template("model.mako") generated = template.render(**values) outfile = open(target_file, 'w+') outfile.write(generated) outfile.close() # Import the new model file. This is needed to be able to # generate the migration script properly. dynamic_import(clazzpath) print 'Ok.' except Exception: print 'Failed.'
def get_importer(session, modulname, fmt): modul_clazzpath = session.query(ModulItem).filter(ModulItem.name == modulname).all()[0].clazzpath modul = dynamic_import(modul_clazzpath) if fmt == "json": return JSONImporter(modul, session) else: return CSVImporter(modul, session)
def setup_modul(config, modul): """Setup routes and views for the activated actions of the given model of the modul. :config: Pylons config instance :modul: The module for which the new routes will be set up. """ clazz = helpers.dynamic_import(modul.clazzpath) log.info("Setup modul '%s'" % modul.name) # Reload modul old_actions = list(a.url for a in modul.actions) for bclazz in clazz.__bases__: if hasattr(bclazz, "get_mixin_actions"): for action in bclazz.get_mixin_actions(): if not action.url in old_actions: action.mid = clazz._modul_id modul.actions.append(action) NTDBSession.add(action) NTDBSession.commit() for action in modul.actions: # Link is a special actions which does not need a view. So # ignore it (e.g Link action). if not action.url: continue _setup_web_action(config, action, clazz, web_action_view_mapping) _setup_rest_action(config, action, clazz, rest_action_view_mapping)
def setup_modul(config, modul): """Setup routes and views for the activated actions of the given model of the modul. :config: Pylons config instance :modul: The module for which the new routes will be set up. """ clazz = helpers.dynamic_import(modul.clazzpath) log.info("Setup modul '%s'" % modul.name) # Reload modul old_actions = list(a.url for a in modul.actions) for bclazz in clazz.__bases__: if issubclass(bclazz, Mixin): for action in bclazz.get_mixin_actions(): if not action.url in old_actions: action.mid = clazz._modul_id modul.actions.append(action) NTDBSession.add(action) NTDBSession.commit() for action in modul.actions: _setup_web_action(config, action, clazz, web_action_view_mapping) _setup_rest_action(config, action, clazz, rest_action_view_mapping)
def _load_user(userid, request): try: modul = get_item_modul(request, User) UserClazz = dynamic_import(modul.clazzpath) factory = UserClazz.get_item_factory() return factory.load(userid) except NoResultFound: return None
def get_modul(modulname, session): modul_item = get_modul_by_name(modulname, session) if not modul_item: modules = [m.name for m in session.query(ModulItem).all()] msg = "Can not load modul '{}'. Please choose one from [{}].".format( modulname, ", ".join(modules)) print >> sys.stderr, msg sys.exit(1) return dynamic_import(modul_item.clazzpath)
def get_importer(session, modulname, fmt): try: modul_clazzpath = session.query(ModulItem).filter( ModulItem.name == modulname).all()[0].clazzpath modul = dynamic_import(modul_clazzpath) if fmt == "json": return JSONImporter(modul, session) else: return CSVImporter(modul, session) except: modules = [m.name for m in session.query(ModulItem).all()] print "Can not load modul '{}'. Please choose one from [{}].".format( modulname, ", ".join(modules)) sys.exit(1)
def handle_db_savedata_command(args): path = [] path.append(args.config) session = get_session(os.path.join(*path)) modul_clazzpath = session.query(ModulItem).filter(ModulItem.name == args.modul).all()[0].clazzpath modul = dynamic_import(modul_clazzpath) if args.format == "json": exporter = JSONExporter(modul, serialized=False, relations=args.include_relations) else: exporter = CSVExporter(modul, serialized=False, relations=args.include_relations) print exporter.perform(session.query(modul) .order_by(modul.id) .all())
def handle_ext_init_command(args): path = [] path.append(get_app_location(args.app)) path.append(args.config) session = get_session(os.path.join(*path)) for ext in extensions: if ext == args.name: if _load_modul_by_name(session, ext.replace("ringo_", "")): print "Extension %s already added!" % args.name else: extension = dynamic_import("%s." % args.name) _add_modul(extension.modul_config, None, session) print "Extension %s added!" % args.name break else: print "Extension %s not found!" % args.name
def handle_ext_init_command(args): path = [] path.append(get_app_location(args.app)) path.append(args.config) session = get_session(os.path.join(*path)) for ext in extensions: if ext == args.name: if get_modul_by_name(ext.replace("ringo_", ""), session): print "Extension %s already added!" % args.name else: extension = dynamic_import("%s." % args.name) _add_modul(extension.modul_config, None, session) print "Extension %s added!" % args.name break else: print "Extension %s not found!" % args.name
def handle_db_savedata_command(args): path = [] path.append(args.config) session = get_session(os.path.join(*path)) modul_clazzpath = session.query(ModulItem).filter( ModulItem.name == args.modul).all()[0].clazzpath modul = dynamic_import(modul_clazzpath) data = session.query(modul).order_by(modul.id).all() if args.filter: # Build Baselist which is used for filtering. filter_stack = [] listing = BaseList(modul, db=None, items=data) for f in args.filter.split(";"): filter_item = f.split(",") filter_item[2] = bool(filter_item[2]) filter_stack.append(tuple(filter_item)) try: listing.filter(filter_stack) except KeyError: print "Error while filtering." sys.exit(1) data = listing.items if args.export_config: with open(args.export_config, "r") as export_configfile: export_config = ExportConfiguration(json.load(export_configfile)) else: export_config = ExportConfiguration(json.loads("[]")) if args.format == "json": exporter = JSONExporter(modul, serialized=False, relations=args.include_relations, config=export_config) data = prepare_data(data) else: exporter = CSVExporter(modul, serialized=False, relations=args.include_relations, config=export_config) print exporter.perform(data)
def handle_db_uuid_command(args): path = [] path.append(args.config) session = get_session(os.path.join(*path)) modul_clazzpath = session.query(ModulItem).filter(ModulItem.name == args.modul).all()[0].clazzpath modul = dynamic_import(modul_clazzpath) updated = 0 created = 0 for item in session.query(modul).all(): if item.uuid: if args.missing_only: continue else: item.reset_uuid() updated += 1 else: item.reset_uuid() created += 1 try: transaction.commit() print "Updated %s items, Created %s items" % (updated, created) except: print "Loading data failed!"
def handle_db_uuid_command(args): path = [] path.append(args.config) session = get_session(os.path.join(*path)) modul_clazzpath = session.query(ModulItem).filter( ModulItem.name == args.modul).all()[0].clazzpath modul = dynamic_import(modul_clazzpath) updated = 0 created = 0 for item in session.query(modul).all(): if item.uuid: if args.missing_only: continue else: item.reset_uuid() updated += 1 else: item.reset_uuid() created += 1 try: transaction.commit() print "Updated %s items, Created %s items" % (updated, created) except: print "Loading data failed!"
def test_import_ok(): from ringo.lib.helpers import dynamic_import result = dynamic_import('ringo.model.base.BaseItem') assert repr(result) == "<class 'ringo.model.base.BaseItem'>"
import os from ringo.model import Base, extensions from ringo.lib import helpers # Define enabled ringo extenstion here. Extensions will be initialised # on application startup. extensions.append("ringo_comment") extensions.append("ringo_tag") # Dynamically import all model files here to have the model available in # the sqlalchemy metadata. This is needed for the schema migration with # alembic. Otherwise the migration will produce many table drops as the # tables of the models are not present in the metadata model_dir = os.path.dirname(os.path.realpath(__file__)) modul_files = [ f for f in os.listdir(model_dir) if f.split(".")[-1] == "py" ] for model in modul_files: helpers.dynamic_import(__name__ + "." + model.split(".")[0]) for extension in extensions: helpers.dynamic_import(extension + ".model")
def test_import_fails(): from ringo.lib.helpers import dynamic_import with pytest.raises(AttributeError): dynamic_import('ringo.model.base.IamNotHere')
def get_clazz(self): """Returns the class defined in the clazzpath attribute. :returns: :class:`.BaseItem` """ return dynamic_import(self.clazzpath)
import os from sqlalchemy.ext.declarative import declarative_base from ringo.lib import helpers # Define enabled ringo extenstion here. Extensions will be initialised # on application startup. extensions = [] # Global declarative base class. Base = declarative_base() # Dynamically import all model files and extensions here to have the # model available in the sqlalchemy metadata. This is needed for the # schema migration with alembic. Otherwise the migration will produce # many table drops as the tables of the models are not present in the # metadata model_dir = os.path.dirname(os.path.realpath(__file__)) modul_files = [f for f in os.listdir(model_dir) if f.split(".")[-1] == "py"] for model in modul_files: helpers.dynamic_import(__name__ + "." + model.split(".")[0]) for extension in extensions: helpers.dynamic_import(extension + ".model")
def get_renderer(self, col): if "renderer" in col: return dynamic_import(col["renderer"]) else: return None