def update(self): data = [] fields = ptah.get_cfg_storage(FIELD_ID) previews = ptah.get_cfg_storage(PREVIEW_ID) for name, cls in fields.items(): data.append({'name': name, 'doc': cls.__doc__, 'preview': previews.get(cls)}) self.fields = sorted(data, key = lambda item: item['name'])
def __getitem__(self, key): if self.cfg['enable_modules']: if key in self.cfg['enable_modules']: mod = ptah.get_cfg_storage(MANAGE_ID).get(key) if mod is not None: return mod(self, self.request) elif key not in self.cfg['disable_modules']: mod = ptah.get_cfg_storage(MANAGE_ID).get(key) if mod is not None: return mod(self, self.request) raise KeyError(key)
def update(self): data = [] fields = ptah.get_cfg_storage(FIELD_ID) previews = ptah.get_cfg_storage(PREVIEW_ID) for name, cls in fields.items(): data.append({ 'name': name, 'doc': cls.__doc__, 'preview': previews.get(cls) }) self.fields = sorted(data, key=lambda item: item['name'])
def check_version(ev): """ ApplicationCreated event handler """ if not Version.__table__.exists(): return versions = dict((v.package, v.version_num) for v in ptah.get_session().query(Version).all()) packages = ptah.get_cfg_storage(MIGRATION_ID).keys() has_steps = False log = logging.getLogger('ptah.alembic') for pkg in packages: version = versions.get(pkg) script = ScriptDirectory(pkg) for sc in script.walk_revisions(): if sc.is_head: if sc.revision != version: has_steps = True log.error( "Package '%s' current revision: '%s', head: '%s'", pkg, version, sc.revision) break if has_steps: config.shutdown() log.error("Please run `ptah-migrate` script. Stopping...") raise SystemExit(1)
def check_version(ev): """ ApplicationCreated event handler """ if not Version.__table__.exists(): return versions = dict((v.package, v.version_num) for v in ptah.get_session().query(Version).all()) packages = ptah.get_cfg_storage(MIGRATION_ID).keys() has_steps = False log = logging.getLogger('ptah.alembic') for pkg in packages: version = versions.get(pkg) script = ScriptDirectory(pkg) for sc in script.walk_revisions(): if sc.is_head: if sc.revision != version: has_steps = True log.error("Package '%s' current revision: '%s', head: '%s'", pkg, version, sc.revision) break if has_steps: config.shutdown() log.error("Please run `ptah-migrate` script. Stopping...") raise SystemExit(1)
def update(self): groups = ptah.get_cfg_storage(ID_SETTINGS_GROUP).items() data = [] for name, group in sorted(groups): title = group.__title__ or name description = group.__description__ schema = [] for field in group.__fields__.values(): schema.append( ({'name': '{0}.{1}'.format(name, field.name), 'type': field.__class__.__name__, 'value': field.dumps(group[field.name]), 'title': field.title, 'description': field.description, 'default': field.dumps(field.default)})) data.append( ({'title': title, 'description': description, 'schema': schema, 'name': group.__name__, 'ttw': group.__ttw__})) return {'data': sorted(data, key=lambda item: item['title'])}
def update(self): groups = ptah.get_cfg_storage(ID_SETTINGS_GROUP).items() data = [] for name, group in sorted(groups): if self.grps is not None and name not in self.grps: continue title = group.__title__ or name description = group.__description__ schema = [] for field in group.__fields__.values(): if getattr(field, 'tint', False): value = '* * * * * * *' else: value = ptah.json.dumps(group[field.name]) schema.append( ({'name': '{0}.{1}'.format(name, field.name), 'type': field.__class__.__name__, 'value': value, 'title': field.title, 'description': field.description, 'default': ptah.json.dumps(field.default)})) data.append( ({'title': title, 'description': description, 'schema': schema, 'name': group.__name__, 'ttw': group.__ttw__})) return {'data': sorted(data, key=lambda item: item['title'])}
def get_info(self, name): group = ptah.get_cfg_storage(ID_SETTINGS_GROUP, self.registry)[name] schema = [] for field in group.__fields__.values(): if getattr(field, 'tint', False): value = '* * * * * * *' else: value = field.dumps(group[field.name]) schema.append(({ 'name': '{0}.{1}'.format(name, field.name), 'type': field.__class__.__name__, 'value': value, 'title': field.title, 'description': field.description, 'default': field.dumps(field.default) })) return { 'title': group.__title__ or name, 'description': group.__description__, 'schema': schema, 'name': group.__name__, 'ttw': group.__ttw__ }
def update(self): groups = ptah.get_cfg_storage(ID_SETTINGS_GROUP).items() data = [] for name, group in sorted(groups): if self.grps is not None and name not in self.grps: continue title = group.__title__ or name description = group.__description__ schema = [] for field in group.__fields__.values(): if getattr(field, 'tint', False): value = '* * * * * * *' else: value = ptah.json.dumps(group[field.name]) schema.append(({ 'name': '{0}.{1}'.format(name, field.name), 'type': field.__class__.__name__, 'value': value, 'title': field.title, 'description': field.description, 'default': ptah.json.dumps(field.default) })) data.append(({ 'title': title, 'description': description, 'schema': schema, 'name': group.__name__, 'ttw': group.__ttw__ })) return {'data': sorted(data, key=lambda item: item['title'])}
def update(self): groups = ptah.get_cfg_storage(ID_SETTINGS_GROUP).items() data = [] for name, group in sorted(groups): title = group.__title__ or name description = group.__description__ schema = [] for field in group.__fields__.values(): schema.append(({ 'name': '{0}.{1}'.format(name, field.name), 'type': field.__class__.__name__, 'value': field.dumps(group[field.name]), 'title': field.title, 'description': field.description, 'default': field.dumps(field.default) })) data.append(({ 'title': title, 'description': description, 'schema': schema, 'name': group.__name__, 'ttw': group.__ttw__ })) return {'data': sorted(data, key=lambda item: item['title'])}
def test_library_simple_js(self): ptah.library('test-lib', path='http://ptah.org/test.js', type='js') self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib'] self.assertEqual(lib.render(self.request), '<script src="http://ptah.org/test.js"> </script>')
def test_library_render_absurls(self): ptah.library('test-lib', path='http://ptah.org/style.css', type='css') self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib'] self.assertEqual( lib.render(self.request), '<link type="text/css" rel="stylesheet" href="http://ptah.org/style.css" />')
def test_library_render_absurls(self): ptah.library('test-lib', path='http://ptah.org/style.css', type='css') self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib'] self.assertEqual( lib.render(self.request), '<link type="text/css" rel="stylesheet" href="http://ptah.org/style.css" />' )
def test_library_simple_js(self): ptah.library( 'test-lib', path='http://ptah.org/test.js', type='js') self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib'] self.assertEqual( lib.render(self.request), '<script src="http://ptah.org/test.js"> </script>')
def test_library_render_with_extra(self): ptah.library( 'test-lib', path='http://ptah.org/test.js', type='js', extra={'test': "extra"}) self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib'] self.assertEqual( lib.render(self.request), '<script test="extra" src="http://ptah.org/test.js"> </script>')
def __init__(self, pkg): path = ptah.get_cfg_storage(MIGRATION_ID).get(pkg) if path is None: raise ValueError("Can't find package.") res = AssetResolver(pkg) self.dir = res.resolve('ptah:scripts').abspath() self.versions = res.resolve(path).abspath() if not os.access(self.versions, os.F_OK): raise alembic.util.CommandError("Path doesn't exist: %r." % path)
def test_register(self): from ptah.migrate import MIGRATION_ID ptah.register_migration('test', 'ptah.tests:migrations', 'Ptah database migration') self.init_ptah() st = ptah.get_cfg_storage(MIGRATION_ID) self.assertIn('test', st) self.assertEqual(st['test'], 'ptah.tests:migrations')
def test_register(self): from ptah.migrate import MIGRATION_ID ptah.register_migration( 'test', 'ptah.tests:migrations', 'Ptah database migration') self.init_ptah() st = ptah.get_cfg_storage(MIGRATION_ID) self.assertIn('test', st) self.assertEqual(st['test'], 'ptah.tests:migrations')
def test_library_render_with_prefix_postfix(self): ptah.library( 'test-lib', path='http://ptah.org/style.css', type='css', prefix='<!--[if lt IE 7 ]>', postfix='<![endif]-->') self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib'] self.assertEqual( lib.render(self.request), '<!--[if lt IE 7 ]><link type="text/css" rel="stylesheet" href="http://ptah.org/style.css" /><![endif]-->')
def test_library_render_with_extra(self): ptah.library('test-lib', path='http://ptah.org/test.js', type='js', extra={'test': "extra"}) self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib'] self.assertEqual( lib.render(self.request), '<script test="extra" src="http://ptah.org/test.js"> </script>')
def test_library_render_with_prefix_postfix(self): ptah.library('test-lib', path='http://ptah.org/style.css', type='css', prefix='<!--[if lt IE 7 ]>', postfix='<![endif]-->') self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib'] self.assertEqual( lib.render(self.request), '<!--[if lt IE 7 ]><link type="text/css" rel="stylesheet" href="http://ptah.org/style.css" /><![endif]-->' )
def test_library_simple_css(self): self.config.add_static_view('tests30', 'ptah.tests:static/dir1') ptah.library('test-lib30', path='style.css', type='css') self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib30'] self.assertEqual(lib.name, 'test-lib30') self.assertEqual(len(lib.entries), 1) self.assertEqual(len(lib.entries), 1) self.assertTrue('style.css' in lib.entries[0].paths) self.assertEqual(repr(lib), '<ptah.Library "test-lib30">')
def update(self): context = self.context request = self.request self.cfg = ptah.get_settings(ptah.CFG_ID_PTAH, request.registry) mods = [] for name, mod in ptah.get_cfg_storage(MANAGE_ID).items(): if name in self.cfg['disable_modules']: continue mod = mod(context, request) if not mod.available(): continue mods.append((mod.title, mod)) self.modules = [mod for _t, mod in sorted(mods)]
def test_library_simple_css(self): self.config.add_static_view('tests30', 'ptah.tests:static/dir1') ptah.library('test-lib30',path='style.css',type='css') self.init_ptah() from ptah.library import LIBRARY_ID lib = ptah.get_cfg_storage(LIBRARY_ID)['test-lib30'] self.assertEqual(lib.name, 'test-lib30') self.assertEqual(len(lib.entries), 1) self.assertEqual(len(lib.entries), 1) self.assertTrue('style.css' in lib.entries[0].paths) self.assertEqual( repr(lib), '<ptah.Library "test-lib30">')
def test_step_registration(self): import ptah @ptah.populate('step', title='Step', requires=['test-dep']) def step(registry): """ """ self.init_ptah() data = ptah.get_cfg_storage(POPULATE_ID) self.assertIn('step', data) self.assertIs(data['step']['factory'], step) self.assertEqual(data['step']['title'], 'Step') self.assertTrue(data['step']['active']) self.assertEqual(data['step']['requires'], ['test-dep'])
def list_modules(self): cfg = ptah.get_settings(ptah.CFG_ID_PTAH) disabled = cfg['disable_modules'] mods = [] for name, mod in ptah.get_cfg_storage(MANAGE_ID).items(): mods.append( {'id': name, 'title': mod.title, 'description': mod.__doc__, 'disabled': name in disabled}) for mod in sorted(mods, key=lambda item:item['id']): print (grpTitleWrap.fill( '{id}: {title} (disabled: {disabled})'.format(**mod))) print (grpDescriptionWrap.fill(mod['description'])) print ('\n')
def __call__(self, request): srv = ptah.get_cfg_storage(ID_REST)[self.srvname] url = request.application_url info = OrderedDict((('name', srv.name), ('__link__', '%s/' % url), ('title', srv.title), ('description', srv.description), ('actions', []))) actions = [(a.title, a.name, a.description) for a in srv.actions.values()] for title, name, description in sorted(actions): info['actions'].append( OrderedDict( (('name', name), ('__link__', '%s/%s' % (url, name)), ('title', title), ('description', description)))) return info
def get_info(self, name): group = ptah.get_cfg_storage(ID_SETTINGS_GROUP, self.registry)[name] schema = [] for field in group.__fields__.values(): if getattr(field, 'tint', False): value = '* * * * * * *' else: value = field.dumps(group[field.name]) schema.append( ({'name': '{0}.{1}'.format(name, field.name), 'type': field.__class__.__name__, 'value': value, 'title': field.title, 'description': field.description, 'default': field.dumps(field.default)})) return {'title': group.__title__ or name, 'description': group.__description__, 'schema': schema, 'name': group.__name__, 'ttw': group.__ttw__}
def __call__(self, request): srv = ptah.get_cfg_storage(ID_REST)[self.srvname] url = request.application_url info = OrderedDict( (('name', srv.name), ('__link__', '%s/' % url), ('title', srv.title), ('description', srv.description), ('actions', []))) actions = [(a.title, a.name, a.description) for a in srv.actions.values()] for title, name, description in sorted(actions): info['actions'].append( OrderedDict( (('name', name), ('__link__', '%s/%s' % (url, name)), ('title', title), ('description', description)))) return info
def list_steps(self, p_steps=None, all=False): seen = set() steps = dict( (name, intr) for name, intr in ptah.get_cfg_storage(POPULATE_ID, self.registry).items()) sorted_steps = [] def _step(name, step): if name in seen: return seen.add(name) for dep in step['requires']: if dep not in steps: raise RuntimeError( "Can't find populate step '{0}'.".format(dep)) _step(dep, steps[dep]) sorted_steps.append(step) if p_steps is not None: for name in p_steps: if name not in steps: raise RuntimeError( "Can't find populate step '{0}'.".format(name)) _step(name, steps[name]) else: for name, step in steps.items(): if all: _step(name, step) elif step['active']: _step(name, step) return sorted_steps
def list_steps(self, p_steps=None, all=False): seen = set() steps = dict((name, intr) for name, intr in ptah.get_cfg_storage( POPULATE_ID, self.registry).items()) sorted_steps = [] def _step(name, step): if name in seen: return seen.add(name) for dep in step['requires']: if dep not in steps: raise RuntimeError( "Can't find populate step '{0}'.".format(dep)) _step(dep, steps[dep]) sorted_steps.append(step) if p_steps is not None: for name in p_steps: if name not in steps: raise RuntimeError( "Can't find populate step '{0}'.".format(name)) _step(name, steps[name]) else: for name, step in steps.items(): if all: _step(name, step) elif step['active']: _step(name, step) return sorted_steps
def main(): parser = argparse.ArgumentParser(description="ptah migrate") parser.add_argument("config", metavar="config", help="ini config file") subparsers = parser.add_subparsers() # revision subparser = subparsers.add_parser(revision.__name__, help=revision.__doc__) subparser.add_argument("package", metavar="package", help="package name") subparser.add_argument("-r", "--revision", type=str, dest="revid", help="Unique revision id") subparser.add_argument("-m", "--message", type=str, dest="message", help="Message string to use with 'revision'") subparser.set_defaults(cmd="revision") # current subparser = subparsers.add_parser(current.__name__, help=current.__doc__) subparser.add_argument("package", metavar="package", nargs="*", help="package name") subparser.set_defaults(cmd="current") # upgrade subparser = subparsers.add_parser(upgrade.__name__, help=upgrade.__doc__) subparser.add_argument("package", metavar="package", nargs="*", help="package name") subparser.set_defaults(cmd="upgrade") # history subparser = subparsers.add_parser(history.__name__, help=history.__doc__) subparser.add_argument("package", metavar="package", nargs="*", help="package name") subparser.set_defaults(cmd="history") # list subparser = subparsers.add_parser("list", help="List registered migrations.") subparser.set_defaults(cmd="list") # parse args = parser.parse_args() # bootstrap pyramid env = scripts.bootstrap(args.config) if args.cmd == "current": print("") if not args.package: args.package = ptah.get_cfg_storage(MIGRATION_ID).keys() for pkg in args.package: current(pkg) if args.cmd == "revision": if args.revid: for ch in ",.;-": if ch in args.revid: print("Revision id contains forbidden characters") ptah.shutdown() return revision(args.package, args.revid, args.message) if args.cmd == "upgrade": # create db schemas create_db_schema(env["registry"], False) for pkg in args.package: upgrade(pkg) if args.cmd == "history": if not args.package: args.package = ptah.get_cfg_storage(MIGRATION_ID).keys() for pkg in args.package: history(pkg) if args.cmd == "list": list_migrations(env["registry"]) ptah.shutdown()
def main(): parser = argparse.ArgumentParser(description="ptah migrate") parser.add_argument('config', metavar='config', help='ini config file') subparsers = parser.add_subparsers() # revision subparser = subparsers.add_parser(revision.__name__, help=revision.__doc__) subparser.add_argument('package', metavar='package', help='package name') subparser.add_argument("-r", "--revision", type=str, dest='revid', help="Unique revision id") subparser.add_argument("-m", "--message", type=str, dest='message', help="Message string to use with 'revision'") subparser.set_defaults(cmd='revision') # current subparser = subparsers.add_parser(current.__name__, help=current.__doc__) subparser.add_argument('package', metavar='package', nargs='*', help='package name') subparser.set_defaults(cmd='current') # upgrade subparser = subparsers.add_parser(upgrade.__name__, help=upgrade.__doc__) subparser.add_argument('package', metavar='package', nargs='*', help='package name') subparser.set_defaults(cmd='upgrade') # history subparser = subparsers.add_parser(history.__name__, help=history.__doc__) subparser.add_argument('package', metavar='package', nargs='*', help='package name') subparser.set_defaults(cmd='history') # list subparser = subparsers.add_parser('list', help='List registered migrations.') subparser.set_defaults(cmd='list') # parse args = parser.parse_args() # bootstrap pyramid env = scripts.bootstrap(args.config) if args.cmd == 'current': print('') if not args.package: args.package = ptah.get_cfg_storage(MIGRATION_ID).keys() for pkg in args.package: current(pkg) if args.cmd == 'revision': if args.revid: for ch in ',.;-': if ch in args.revid: print('Revision id contains forbidden characters') ptah.shutdown() return revision(args.package, args.revid, args.message) if args.cmd == 'upgrade': # create db schemas create_db_schema(env['registry'], False) for pkg in args.package: upgrade(pkg) if args.cmd == 'history': if not args.package: args.package = ptah.get_cfg_storage(MIGRATION_ID).keys() for pkg in args.package: history(pkg) if args.cmd == 'list': list_migrations(env['registry']) ptah.shutdown()