def generate_schema_pot(w, cubedir=None): """generate a pot file with schema specific i18n messages notice that relation definitions description and static vocabulary should be marked using '_' and extracted using xgettext """ from cubicweb.cwvreg import CWRegistryStore if cubedir: cube = osp.split(cubedir)[-1] if cube.startswith('cubicweb_'): cube = cube[len('cubicweb_'):] config = DevConfiguration(cube) depcubes = list(config._cubes) depcubes.remove(cube) libconfig = DevConfiguration(*depcubes) else: config = DevConfiguration() cube = libconfig = None clean_sys_modules(config.appobjects_modnames()) schema = config.load_schema(remove_unused_rtypes=False) vreg = CWRegistryStore(config) # set_schema triggers objects registrations vreg.set_schema(schema) w(DEFAULT_POT_HEAD) _generate_schema_pot(w, vreg, schema, libconfig=libconfig)
def init(self, cube): """Initialize analyze for the given cube, returning the (already loaded) vregistry and a set of entities which we're interested in. """ config = DevConfiguration(cube) schema = config.load_schema() vreg = CWRegistryStore(config) vreg.set_schema(schema) # set_schema triggers objects registrations eschemas = set(eschema for eschema in schema.entities() if self.should_include(eschema)) return vreg, eschemas
def _generate_schema_pot(w, vreg, schema, libconfig=None): from cubicweb.i18n import add_msg from cubicweb.schema import NO_I18NCONTEXT, CONSTRAINTS w('# schema pot file, generated on %s\n' % datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')) w('# \n') w('# singular and plural forms for each entity type\n') w('\n') vregdone = set() afss = vreg['uicfg']['autoform_section'] aiams = vreg['uicfg']['actionbox_appearsin_addmenu'] if libconfig is not None: # processing a cube, libconfig being a config with all its dependencies # (cubicweb incl.) from cubicweb.cwvreg import CWRegistryStore libschema = libconfig.load_schema(remove_unused_rtypes=False) clean_sys_modules(libconfig.appobjects_modnames()) libvreg = CWRegistryStore(libconfig) libvreg.set_schema(libschema) # trigger objects registration libafss = libvreg['uicfg']['autoform_section'] libaiams = libvreg['uicfg']['actionbox_appearsin_addmenu'] # prefill vregdone set list(_iter_vreg_objids(libvreg, vregdone)) def is_in_lib(rtags, eschema, rschema, role, tschema, predicate=bool): return any( predicate(rtag.etype_get(eschema, rschema, role, tschema)) for rtag in rtags) else: # processing cubicweb itself libschema = {} for cstrtype in CONSTRAINTS: add_msg(w, cstrtype) libafss = libaiams = None is_in_lib = lambda *args: False done = set() for eschema in sorted(schema.entities()): if eschema.type in libschema: done.add(eschema.description) for eschema in sorted(schema.entities()): etype = eschema.type if etype not in libschema: add_msg(w, etype) add_msg(w, '%s_plural' % etype) if not eschema.final: add_msg(w, 'This %s:' % etype) add_msg(w, 'New %s' % etype) add_msg(w, 'add a %s' % etype) # AddNewAction if libconfig is not None: # processing a cube # As of 3.20.3 we no longer use it, but keeping this string # allows developers to run i18ncube with new cubicweb and still # have the right translations at runtime for older versions add_msg(w, 'This %s' % etype) if eschema.description and not eschema.description in done: done.add(eschema.description) add_msg(w, eschema.description) if eschema.final: continue for rschema, targetschemas, role in eschema.relation_definitions(True): if rschema.final: continue for tschema in targetschemas: for afs in afss: fsections = afs.etype_get(eschema, rschema, role, tschema) if 'main_inlined' in fsections and not \ is_in_lib(libafss, eschema, rschema, role, tschema, lambda x: 'main_inlined' in x): add_msg(w, 'add a %s' % tschema, 'inlined:%s.%s.%s' % (etype, rschema, role)) add_msg(w, str(tschema), 'inlined:%s.%s.%s' % (etype, rschema, role)) break for aiam in aiams: if aiam.etype_get(eschema, rschema, role, tschema) and not \ is_in_lib(libaiams, eschema, rschema, role, tschema): if role == 'subject': label = 'add %s %s %s %s' % (eschema, rschema, tschema, role) label2 = "creating %s (%s %%(linkto)s %s %s)" % ( tschema, eschema, rschema, tschema) else: label = 'add %s %s %s %s' % (tschema, rschema, eschema, role) label2 = "creating %s (%s %s %s %%(linkto)s)" % ( tschema, tschema, rschema, eschema) add_msg(w, label) add_msg(w, label2) break # XXX also generate "creating ...' messages for actions in the # addrelated submenu w('# subject and object forms for each relation type\n') w('# (no object form for final or symmetric relation types)\n') w('\n') for rschema in sorted(schema.relations()): if rschema.type in libschema: done.add(rschema.type) done.add(rschema.description) for rschema in sorted(schema.relations()): rtype = rschema.type if rtype not in libschema: # bw compat, necessary until all translation of relation are done # properly... add_msg(w, rtype) done.add(rtype) if rschema.description and rschema.description not in done: add_msg(w, rschema.description) done.add(rschema.description) librschema = None else: librschema = libschema.rschema(rtype) # add context information only for non-metadata rtypes if rschema not in NO_I18NCONTEXT: libsubjects = librschema and librschema.subjects() or () for subjschema in rschema.subjects(): if not subjschema in libsubjects: add_msg(w, rtype, subjschema.type) if not (rschema.final or rschema.symmetric): if rschema not in NO_I18NCONTEXT: libobjects = librschema and librschema.objects() or () for objschema in rschema.objects(): if not objschema in libobjects: add_msg(w, '%s_object' % rtype, objschema.type) if rtype not in libschema: # bw compat, necessary until all translation of relation are # done properly... add_msg(w, '%s_object' % rtype) for rdef in rschema.rdefs.values(): if not rdef.description or rdef.description in done: continue if (librschema is None or (rdef.subject, rdef.object) not in librschema.rdefs or librschema.rdefs[(rdef.subject, rdef.object)].description != rdef.description): add_msg(w, rdef.description) done.add(rdef.description) for objid in _iter_vreg_objids(vreg, vregdone): add_msg(w, '%s_description' % objid) add_msg(w, objid)
def setUp(self): config = devtools.TestServerConfiguration('data', __file__) self.vreg = CWRegistryStore(config) config.bootstrap_cubes() self.vreg.schema = config.load_schema()
class VRegistryTC(TestCase): def setUp(self): config = devtools.TestServerConfiguration('data', __file__) self.vreg = CWRegistryStore(config) config.bootstrap_cubes() self.vreg.schema = config.load_schema() def test_load_interface_based_vojects(self): self.vreg.init_registration([WEBVIEWSDIR]) self.vreg.load_file(join(BASE, 'entities', '__init__.py'), 'cubicweb.entities.__init__') self.vreg.load_file(join(WEBVIEWSDIR, 'idownloadable.py'), 'cubicweb.web.views.idownloadable') self.vreg.load_file(join(WEBVIEWSDIR, 'primary.py'), 'cubicweb.web.views.primary') self.assertEqual(len(self.vreg['views']['primary']), 2) self.vreg.initialization_completed() self.assertEqual(len(self.vreg['views']['primary']), 1) def test_load_subinterface_based_appobjects(self): self.vreg.register_objects( [join(BASE, 'web', 'views', 'idownloadable.py')]) # check downloadlink was kicked self.assertFalse(self.vreg['views'].get('downloadlink')) # we've to emulate register_objects to add custom MyCard objects path = [ join(BASE, 'entities', '__init__.py'), join(BASE, 'entities', 'adapters.py'), join(BASE, 'web', 'views', 'idownloadable.py') ] filemods = self.vreg.init_registration(path, None) for filepath, modname in filemods: self.vreg.load_file(filepath, modname) class CardIDownloadableAdapter(EntityAdapter): __regid__ = 'IDownloadable' self.vreg._loadedmods[__name__] = {} self.vreg.register(CardIDownloadableAdapter) self.vreg.initialization_completed() # check progressbar isn't kicked self.assertEqual(len(self.vreg['views']['downloadlink']), 1) def test_properties(self): self.vreg.reset() self.assertNotIn('system.version.cubicweb', self.vreg['propertydefs']) self.assertTrue(self.vreg.property_info('system.version.cubicweb')) self.assertRaises(UnknownProperty, self.vreg.property_info, 'a.non.existent.key')