1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-10-16 11:24:00 +02:00

leapi: put name2classname() from LeFactory in _LeCrud to be available everywhere

This commit is contained in:
ArnAud 2016-01-07 17:08:07 +01:00
commit 8eea3495e8
2 changed files with 33 additions and 24 deletions

View file

@ -96,6 +96,23 @@ class _LeCrud(object):
## @brief A flag to indicate if the object was fully intanciated or not ## @brief A flag to indicate if the object was fully intanciated or not
self._instanciation_complete = len(kwargs) + 1 == len(self.fieldlist()) self._instanciation_complete = len(kwargs) + 1 == len(self.fieldlist())
## @brief Convert an EmType or EmClass name in a python class name
# @param name str : The name
# @return name.title()
@staticmethod
def name2classname(name):
if not isinstance(name, str):
raise AttributeError("Argument name should be a str and not a %s" % type(name))
return name.title()
## @brief Convert an EmCalss and EmType name in a rel2type class name
# @param name str : The name
# @return name.title()
@staticmethod
def name2rel2type(class_name, type_name):
cls_name = "Rel_%s2%s"%(_LeCrud.name2classname(class_name), _LeCrud.name2classname(type_name))
return cls_name
## @brief Given a dynamically generated class name return the corresponding python Class ## @brief Given a dynamically generated class name return the corresponding python Class
# @param name str : a concrete class name # @param name str : a concrete class name
# @return False if no such component # @return False if no such component

View file

@ -7,6 +7,7 @@ import os.path
import EditorialModel import EditorialModel
from EditorialModel.model import Model from EditorialModel.model import Model
from EditorialModel.fieldtypes.generic import GenericFieldType from EditorialModel.fieldtypes.generic import GenericFieldType
from leapi.lecrud import _LeCrud
## @brief This class is designed to generated the leobject API given an EditorialModel.model ## @brief This class is designed to generated the leobject API given an EditorialModel.model
@ -24,15 +25,6 @@ class LeFactory(object):
self._dyn_file = os.path.basename(code_filename) self._dyn_file = os.path.basename(code_filename)
self._modname = os.path.dirname(code_filename).strip('/').replace('/', '.') #Warning Windaube compatibility self._modname = os.path.dirname(code_filename).strip('/').replace('/', '.') #Warning Windaube compatibility
## @brief Convert an EmType or EmClass name in a python class name
# @param name str : The name
# @return name.title()
@staticmethod
def name2classname(name):
if not isinstance(name, str):
raise AttributeError("Argument name should be a str and not a %s" % type(name))
return name.title()
## @brief Return a call to a FieldType constructor given an EmField ## @brief Return a call to a FieldType constructor given an EmField
# @param emfield EmField : An EmField # @param emfield EmField : An EmField
# @return a string representing the python code to instanciate a EmFieldType # @return a string representing the python code to instanciate a EmFieldType
@ -83,7 +75,7 @@ class LeFactory(object):
for field in [ f for f in model.components('EmField') if f.fieldtype == 'rel2type']: for field in [ f for f in model.components('EmField') if f.fieldtype == 'rel2type']:
related = model.component(field.rel_to_type_id) related = model.component(field.rel_to_type_id)
src = field.em_class src = field.em_class
cls_name = "Rel_%s2%s"%(self.name2classname(src.name), self.name2classname(related.name)) cls_name = _LeCrud.name2rel2type(src.name, related.name)
attr_l = dict() attr_l = dict()
for attr in [ f for f in model.components('EmField') if f.rel_field_id == field.uid]: for attr in [ f for f in model.components('EmField') if f.rel_field_id == field.uid]:
@ -98,8 +90,8 @@ class {classname}(LeRel2Type):
""".format( """.format(
classname = cls_name, classname = cls_name,
attr_dict = "{" + (','.join(['\n %s: %s' % (repr(f), v) for f,v in attr_l.items()])) + "\n}", attr_dict = "{" + (','.join(['\n %s: %s' % (repr(f), v) for f,v in attr_l.items()])) + "\n}",
supcls = self.name2classname(src.name), supcls = _LeCrud.name2classname(src.name),
subcls = self.name2classname(related.name), subcls = _LeCrud.name2classname(related.name),
) )
res_code += rel_code res_code += rel_code
return res_code return res_code
@ -115,7 +107,7 @@ class {classname}(LeRel2Type):
#Populating linked_type attr #Populating linked_type attr
for rfield in [ f for f in emclass.fields() if f.fieldtype == 'rel2type']: for rfield in [ f for f in emclass.fields() if f.fieldtype == 'rel2type']:
fti = rfield.fieldtype_instance() fti = rfield.fieldtype_instance()
cls_linked_types.append(LeFactory.name2classname(model.component(fti.rel_to_type_id).name)) cls_linked_types.append(_LeCrud.name2classname(model.component(fti.rel_to_type_id).name))
# Populating fieldtype attr # Populating fieldtype attr
for field in emclass.fields(relational = False): for field in emclass.fields(relational = False):
self.needed_fieldtypes |= set([field.fieldtype]) self.needed_fieldtypes |= set([field.fieldtype])
@ -128,7 +120,7 @@ class {classname}(LeRel2Type):
{name}._linked_types = {ltypes} {name}._linked_types = {ltypes}
{name}._classtype = {classtype} {name}._classtype = {classtype}
""".format( """.format(
name = LeFactory.name2classname(emclass.name), name = _LeCrud.name2classname(emclass.name),
ftypes = "{" + (','.join(['\n %s: %s' % (repr(f), v) for f, v in cls_fields.items()])) + "\n}", ftypes = "{" + (','.join(['\n %s: %s' % (repr(f), v) for f, v in cls_fields.items()])) + "\n}",
ltypes = "[" + (','.join(cls_linked_types))+"]", ltypes = "[" + (','.join(cls_linked_types))+"]",
@ -148,7 +140,7 @@ class {classname}(LeRel2Type):
for nat, sup_l in emtype.superiors().items(): for nat, sup_l in emtype.superiors().items():
type_superiors.append('%s: [%s]' % ( type_superiors.append('%s: [%s]' % (
repr(nat), repr(nat),
', '.join([LeFactory.name2classname(sup.name) for sup in sup_l]) ', '.join([_LeCrud.name2classname(sup.name) for sup in sup_l])
)) ))
return """ return """
@ -157,10 +149,10 @@ class {classname}(LeRel2Type):
{name}._superiors = {dsups} {name}._superiors = {dsups}
{name}._leclass = {leclass} {name}._leclass = {leclass}
""".format( """.format(
name=LeFactory.name2classname(emtype.name), name=_LeCrud.name2classname(emtype.name),
fields=repr(type_fields), fields=repr(type_fields),
dsups='{' + (', '.join(type_superiors)) + '}', dsups='{' + (', '.join(type_superiors)) + '}',
leclass=LeFactory.name2classname(emtype.em_class.name) leclass=_LeCrud.name2classname(emtype.em_class.name)
) )
## @brief Generate python code containing the LeObject API ## @brief Generate python code containing the LeObject API
@ -198,7 +190,7 @@ import %s
#Generating the code for LeObject class #Generating the code for LeObject class
leobj_me_uid = dict() leobj_me_uid = dict()
for comp in model.components('EmType') + model.components('EmClass'): for comp in model.components('EmType') + model.components('EmClass'):
leobj_me_uid[comp.uid] = LeFactory.name2classname(comp.name) leobj_me_uid[comp.uid] = _LeCrud.name2classname(comp.name)
#Building the fieldtypes dict of LeObject #Building the fieldtypes dict of LeObject
(leobj_uid_fieldtype, leobj_fieldtypes) = self.concret_fieldtypes(EditorialModel.classtypes.common_fields) (leobj_uid_fieldtype, leobj_fieldtypes) = self.concret_fieldtypes(EditorialModel.classtypes.common_fields)
@ -284,7 +276,7 @@ class {name}(LeClass, LeObject):
_class_id = {uid} _class_id = {uid}
""".format( """.format(
name=LeFactory.name2classname(emclass.name), name=_LeCrud.name2classname(emclass.name),
uid=emclass.uid uid=emclass.uid
) )
#LeType child classes definition #LeType child classes definition
@ -296,8 +288,8 @@ class {name}(LeType, {leclass}):
_type_id = {uid} _type_id = {uid}
""".format( """.format(
name=LeFactory.name2classname(emtype.name), name=_LeCrud.name2classname(emtype.name),
leclass=LeFactory.name2classname(emtype.em_class.name), leclass=_LeCrud.name2classname(emtype.em_class.name),
uid=emtype.uid uid=emtype.uid
) )
@ -312,7 +304,7 @@ class {name}(LeType, {leclass}):
#Populating LeObject._me_uid dict for a rapid fetch of LeType and LeClass given an EM uid #Populating LeObject._me_uid dict for a rapid fetch of LeType and LeClass given an EM uid
me_uid = {comp.uid: LeFactory.name2classname(comp.name) for comp in emclass_l + emtype_l} me_uid = {comp.uid: _LeCrud.name2classname(comp.name) for comp in emclass_l + emtype_l}
result += """ result += """
## @brief Dict for getting LeClass and LeType child classes given an EM uid ## @brief Dict for getting LeClass and LeType child classes given an EM uid
LeObject._me_uid = %s""" % "{" + (', '.join(['%s: %s' % (k, v) for k, v in me_uid.items()])) + "}" LeObject._me_uid = %s""" % "{" + (', '.join(['%s: %s' % (k, v) for k, v in me_uid.items()])) + "}"