devtools/devctl.py
changeset 3293 69c0ba095536
parent 3230 1d25e928c299
parent 3287 19c1011736a6
child 3369 7b88d12b4ee2
equal deleted inserted replaced
3230:1d25e928c299 3293:69c0ba095536
    10 
    10 
    11 import sys
    11 import sys
    12 from datetime import datetime
    12 from datetime import datetime
    13 from os import mkdir, chdir, getcwd
    13 from os import mkdir, chdir, getcwd
    14 from os.path import join, exists, abspath, basename, normpath, split, isdir
    14 from os.path import join, exists, abspath, basename, normpath, split, isdir
       
    15 from copy import deepcopy
    15 from warnings import warn
    16 from warnings import warn
    16 
    17 
    17 from logilab.common import STD_BLACKLIST
    18 from logilab.common import STD_BLACKLIST
    18 from logilab.common.modutils import get_module_files
    19 from logilab.common.modutils import get_module_files
    19 from logilab.common.textutils import splitstrip
    20 from logilab.common.textutils import splitstrip
   110     _generate_schema_pot(w, vreg, schema, libconfig=libconfig, cube=cube)
   111     _generate_schema_pot(w, vreg, schema, libconfig=libconfig, cube=cube)
   111 
   112 
   112 
   113 
   113 def _generate_schema_pot(w, vreg, schema, libconfig=None, cube=None):
   114 def _generate_schema_pot(w, vreg, schema, libconfig=None, cube=None):
   114     from cubicweb.common.i18n import add_msg
   115     from cubicweb.common.i18n import add_msg
       
   116     from cubicweb.web import uicfg
       
   117     from cubicweb.schema import META_RTYPES, SYSTEM_RTYPES
       
   118     no_context_rtypes = META_RTYPES | SYSTEM_RTYPES
   115     w('# schema pot file, generated on %s\n' % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
   119     w('# schema pot file, generated on %s\n' % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
   116     w('# \n')
   120     w('# \n')
   117     w('# singular and plural forms for each entity type\n')
   121     w('# singular and plural forms for each entity type\n')
   118     w('\n')
   122     w('\n')
       
   123     vregdone = set()
   119     if libconfig is not None:
   124     if libconfig is not None:
       
   125         from cubicweb.cwvreg import CubicWebVRegistry, clear_rtag_objects
   120         libschema = libconfig.load_schema(remove_unused_rtypes=False)
   126         libschema = libconfig.load_schema(remove_unused_rtypes=False)
   121         entities = [e for e in schema.entities() if not e in libschema]
   127         rinlined = deepcopy(uicfg.autoform_is_inlined)
       
   128         appearsin_addmenu = deepcopy(uicfg.actionbox_appearsin_addmenu)
       
   129         clear_rtag_objects()
       
   130         cleanup_sys_modules(libconfig)
       
   131         libvreg = CubicWebVRegistry(libconfig)
       
   132         libvreg.set_schema(libschema) # trigger objects registration
       
   133         librinlined = uicfg.autoform_is_inlined
       
   134         libappearsin_addmenu = uicfg.actionbox_appearsin_addmenu
       
   135         # prefill vregdone set
       
   136         list(_iter_vreg_objids(libvreg, vregdone))
   122     else:
   137     else:
   123         libschema = None
   138         libschema = {}
   124         entities = schema.entities()
   139         rinlined = uicfg.autoform_is_inlined
       
   140         appearsin_addmenu = uicfg.actionbox_appearsin_addmenu
   125     done = set()
   141     done = set()
   126     for eschema in sorted(entities):
   142     for eschema in sorted(schema.entities()):
   127         etype = eschema.type
   143         etype = eschema.type
   128         add_msg(w, etype)
   144         if etype not in libschema:
   129         add_msg(w, '%s_plural' % etype)
   145             add_msg(w, etype)
   130         if not eschema.is_final():
   146             add_msg(w, '%s_plural' % etype)
   131             add_msg(w, 'This %s' % etype)
   147             if not eschema.is_final():
   132             add_msg(w, 'New %s' % etype)
   148                 add_msg(w, 'This %s' % etype)
   133             add_msg(w, 'add a %s' % etype)
   149                 add_msg(w, 'New %s' % etype)
   134             add_msg(w, 'remove this %s' % etype)
   150             if eschema.description and not eschema.description in done:
   135         if eschema.description and not eschema.description in done:
   151                 done.add(eschema.description)
   136             done.add(eschema.description)
   152                 add_msg(w, eschema.description)
   137             add_msg(w, eschema.description)
       
   138     w('# subject and object forms for each relation type\n')
       
   139     w('# (no object form for final relation types)\n')
       
   140     w('\n')
       
   141     if libconfig is not None:
       
   142         relations = [r for r in schema.relations() if not r in libschema]
       
   143     else:
       
   144         relations = schema.relations()
       
   145     for rschema in sorted(set(relations)):
       
   146         rtype = rschema.type
       
   147         add_msg(w, rtype)
       
   148         done.add(rtype)
       
   149         if not (schema.rschema(rtype).is_final() or rschema.symetric):
       
   150             add_msg(w, '%s_object' % rtype)
       
   151         if rschema.description and rschema.description not in done:
       
   152             done.add(rschema.description)
       
   153             add_msg(w, rschema.description)
       
   154     w('# add related box generated message\n')
       
   155     w('\n')
       
   156     from cubicweb.web import uicfg
       
   157     appearsin_addmenu = uicfg.actionbox_appearsin_addmenu
       
   158     for eschema in schema.entities():
       
   159         if eschema.is_final():
   153         if eschema.is_final():
   160             continue
   154             continue
   161         for role, rschemas in (('subject', eschema.subject_relations()),
   155         for rschema, targetschemas, role in eschema.relation_definitions(True):
   162                             ('object', eschema.object_relations())):
   156             for tschema in targetschemas:
   163             for rschema in rschemas:
   157                 if rinlined.etype_get(eschema, rschema, role, tschema) and \
   164                 if rschema.is_final():
   158                        (libconfig is None or not
   165                     continue
   159                         librinlined.etype_get(eschema, rschema, role, tschema)):
   166                 if libconfig is not None:
   160                     add_msg(w, 'add a %s' % tschema,
   167                     librschema = libschema.get(rschema)
   161                             'inlined:%s.%s.%s' % (etype, rschema, role))
   168                 for teschema in rschema.targets(eschema, role):
   162                     add_msg(w, 'remove this %s' % tschema,
   169                     if libconfig is not None and librschema is not None:
   163                             'inlined:%s:%s:%s' % (etype, rschema, role))
   170                         if role == 'subject':
   164                 if appearsin_addmenu.etype_get(eschema, rschema, role, tschema) and \
   171                             subjtype, objtype = eschema, teschema
   165                        (libconfig is None or not
   172                         else:
   166                         libappearsin_addmenu.etype_get(eschema, rschema, role, tschema)):
   173                             subjtype, objtype = teschema, eschema
   167                     if role == 'subject':
   174                         if librschema.has_rdef(subjtype, objtype):
   168                         label = 'add %s %s %s %s' % (eschema, rschema,
   175                             continue
   169                                                      tschema, role)
   176                     if appearsin_addmenu.etype_get(eschema, rschema, role,
   170                         label2 = "creating %s (%s %%(linkto)s %s %s)" % (
   177                                                    teschema):
   171                             tschema, eschema, rschema, tschema)
   178                         if role == 'subject':
   172                     else:
   179                             label = 'add %s %s %s %s' % (eschema, rschema,
   173                         label = 'add %s %s %s %s' % (tschema, rschema,
   180                                                          teschema, role)
   174                                                      eschema, role)
   181                             label2 = "creating %s (%s %%(linkto)s %s %s)" % (
   175                         label2 = "creating %s (%s %s %s %%(linkto)s)" % (
   182                                 teschema, eschema, rschema, teschema)
   176                             tschema, tschema, rschema, eschema)
   183                         else:
   177                     add_msg(w, label)
   184                             label = 'add %s %s %s %s' % (teschema, rschema,
   178                     add_msg(w, label2)
   185                                                          eschema, role)
   179     w('# subject and object forms for each relation type\n')
   186                             label2 = "creating %s (%s %s %s %%(linkto)s)" % (
   180     w('# (no object form for final or symetric relation types)\n')
   187                                 teschema, teschema, rschema, eschema)
   181     w('\n')
   188                         add_msg(w, label)
   182     for rschema in sorted(schema.relations()):
   189                         add_msg(w, label2)
   183         rtype = rschema.type
   190     #cube = (cube and 'cubes.%s.' % cube or 'cubicweb.')
   184         if rtype not in libschema:
   191     done = set()
   185             # bw compat, necessary until all translation of relation are done properly...
   192     if libconfig is not None:
   186             add_msg(w, rtype)
   193         from cubicweb.cwvreg import CubicWebVRegistry
   187             if rschema.description and rschema.description not in done:
   194         libvreg = CubicWebVRegistry(libconfig)
   188                 done.add(rschema.description)
   195         libvreg.set_schema(libschema) # trigger objects registration
   189                 add_msg(w, rschema.description)
   196         # prefill done set
   190             done.add(rtype)
   197         list(_iter_vreg_objids(libvreg, done))
   191             librschema = None
   198     for objid in _iter_vreg_objids(vreg, done):
   192         else:
       
   193             librschema = libschema.rschema(rtype)
       
   194         # add context information only for non-metadata rtypes
       
   195         if rschema not in no_context_rtypes:
       
   196             libsubjects = librschema and librschema.subjects() or ()
       
   197             for subjschema in rschema.subjects():
       
   198                 if not subjschema in libsubjects:
       
   199                     add_msg(w, rtype, subjschema.type)
       
   200         if not (schema.rschema(rtype).is_final() or rschema.symetric):
       
   201             if rschema not in no_context_rtypes:
       
   202                 libobjects = librschema and librschema.objects() or ()
       
   203                 for objschema in rschema.objects():
       
   204                     if not objschema in libobjects:
       
   205                         add_msg(w, '%s_object' % rtype, objschema.type)
       
   206             if rtype not in libschema:
       
   207                 # bw compat, necessary until all translation of relation are done properly...
       
   208                 add_msg(w, '%s_object' % rtype)
       
   209     for objid in _iter_vreg_objids(vreg, vregdone):
   199         add_msg(w, '%s_description' % objid)
   210         add_msg(w, '%s_description' % objid)
   200         add_msg(w, objid)
   211         add_msg(w, objid)
       
   212 
   201 
   213 
   202 def _iter_vreg_objids(vreg, done, prefix=None):
   214 def _iter_vreg_objids(vreg, done, prefix=None):
   203     for reg, objdict in vreg.items():
   215     for reg, objdict in vreg.items():
   204         for objects in objdict.values():
   216         for objects in objdict.values():
   205             for obj in objects:
   217             for obj in objects: