Browse Source

Complete the LeFactory generated code tests, add a __hash__ method for fieldtypes

Yann Weber 9 years ago
parent
commit
0be8db4d00
2 changed files with 13 additions and 2 deletions
  1. 9
    0
      EditorialModel/fieldtypes/generic.py
  2. 4
    2
      leobject/lefactory.py

+ 9
- 0
EditorialModel/fieldtypes/generic.py View File

82
     @staticmethod
82
     @staticmethod
83
     def module_name(fieldtype_name):
83
     def module_name(fieldtype_name):
84
         return 'EditorialModel.fieldtypes.%s'%(fieldtype_name)
84
         return 'EditorialModel.fieldtypes.%s'%(fieldtype_name)
85
+
86
+    ## @brief __hash__ implementation for fieldtypes
87
+    def __hash__(self):
88
+        hash_dats = [ self.__class__.__module__ ]
89
+        for kdic in sorted([k for k in self.__dict__.keys() if not k.startswith('_')]):
90
+            hash_dats.append((kdic, getattr(self, kdic)))
91
+        return hash(tuple(hash_dats))
92
+
93
+
85
     
94
     
86
     ## @brief Transform a value into a valid python representation according to the fieldtype
95
     ## @brief Transform a value into a valid python representation according to the fieldtype
87
     # @param value ? : The value to cast
96
     # @param value ? : The value to cast

+ 4
- 2
leobject/lefactory.py View File

31
     # @return name.title()
31
     # @return name.title()
32
     @staticmethod
32
     @staticmethod
33
     def name2classname(name):
33
     def name2classname(name):
34
+        if not isinstance(name, str):
35
+            raise AttributeError("Argument name should be a str and not a %s"%type(name))
34
         return name.title()
36
         return name.title()
35
 
37
 
36
     ## @brief Return a call to a FieldType constructor given an EmField
38
     ## @brief Return a call to a FieldType constructor given an EmField
64
                 cls_fieldgroup[fieldgroup.name].append(field.name)
66
                 cls_fieldgroup[fieldgroup.name].append(field.name)
65
         
67
         
66
         return """
68
         return """
67
-{name}._fildtypes = {ftypes}
69
+{name}._fieldtypes = {ftypes}
68
 {name}._linked_types = {ltypes}
70
 {name}._linked_types = {ltypes}
69
 {name}._fieldgroups = {fgroups}
71
 {name}._fieldgroups = {fgroups}
70
 """.format(
72
 """.format(
71
     name = LeFactory.name2classname(emclass.name),
73
     name = LeFactory.name2classname(emclass.name),
72
-    ftypes = repr(cls_fields),
74
+    ftypes = "{"+(','.join([ '\n%s:%s'%(repr(f),v) for f,v in cls_fields.items()]))+"}",
73
     ltypes = "{"+(','.join(cls_linked_types))+'}',
75
     ltypes = "{"+(','.join(cls_linked_types))+'}',
74
     fgroups = repr(cls_fieldgroup)
76
     fgroups = repr(cls_fieldgroup)
75
 )
77
 )

Loading…
Cancel
Save