Browse Source

Merge branch 'newlodel' of git.labocleo.org:lodel2 into newlodel

Yann Weber 8 years ago
parent
commit
d115ebda1c

+ 1
- 1
lodel/leapi/datahandlers/datas.py View File

@@ -26,7 +26,7 @@ build its content'
26 26
             datas[fname] for fname in self._field_list)
27 27
         if len(ret) > self.max_length:
28 28
             warnings.warn("Format field overflow. Truncating value")
29
-            ret = ret[:self.max_length-1]
29
+            ret = ret[:self.max_length]
30 30
         return ret
31 31
     
32 32
 ##@brief Varchar validated by a regex

+ 35
- 1
lodel/plugin/plugins.py View File

@@ -148,6 +148,35 @@ to generic PluginVersion comparison function : '%s'" % cmp_fun_name)
148 148
         return {'major': self.major, 'minor': self.minor,
149 149
             'revision': self.revision}
150 150
 
151
+##@brief Stores plugin class registered
152
+__all_ptypes = list()
153
+
154
+##@brief Plugin metaclass that allows to "catch" child class
155
+#declaration
156
+#
157
+#Automatic script registration on child class declaration
158
+class MetaPlugType(type):
159
+    
160
+    def __init__(self, name, bases, attrs):
161
+        #Here we can store all child classes of Plugin
162
+        super().__init__(name, bases, attrs)
163
+        if len(bases) == 1 and bases[0] == object:
164
+            print("Dropped : ", name, bases)
165
+            return
166
+        self.__register_types()
167
+        #list_name= [cls.__name__ for cls in __all_ptypes] 
168
+        #if self.name in list_name:
169
+        #    return
170
+        #else:
171
+        #    plug_type_register(self)
172
+
173
+    def __register_types(self):
174
+        plug_type_register(self)
175
+
176
+def plug_type_register(cls):
177
+    __all_ptypes.append(cls)
178
+    logger.info("New child class registered : %s" % cls.__name__)
179
+
151 180
 
152 181
 ##@brief Handle plugins
153 182
 #
@@ -158,7 +187,7 @@ to generic PluginVersion comparison function : '%s'" % cmp_fun_name)
158 187
 # 1. Settings call start method to instanciate all plugins found in confs
159 188
 # 2. Settings fetch all confspecs
160 189
 # 3. the loader call load_all to register hooks etc
161
-class Plugin(object):
190
+class Plugin(object, metaclass=MetaPlugType):
162 191
     
163 192
     ##@brief Stores plugin directories paths
164 193
     _plugin_directories = None
@@ -625,6 +654,11 @@ file : '%s'. Running discover again..." % DISCOVER_CACHE_FILENAME)
625 654
         cls._plugin_list = infos['plugins']
626 655
         return cls._plugin_list
627 656
 
657
+    ##@brief Return a list of child Class Plugin
658
+    @classmethod
659
+    def plugin_types(cls):
660
+        return cls.__all_ptypes
661
+
628 662
     ##@brief Attempt to open and load plugin discover cache
629 663
     #@return discover cache
630 664
     #@throw PluginError when open or load fails

+ 22
- 0
tests/datahandlers/test_concat.py View File

@@ -0,0 +1,22 @@
1
+import unittest
2
+
3
+from lodel.leapi.datahandlers.datas import Concat
4
+from lodel.editorial_model.components import EmClass
5
+
6
+class ConcatTestCase(unittest.TestCase):
7
+
8
+    def test_construct_data(self):
9
+        test_class = EmClass('testing', display_name='testing class')
10
+        test_class.new_field('field1', 'varchar')
11
+        test_class.new_field('field2', 'varchar')
12
+
13
+        test_concat = Concat(['field1', 'field2'], '*')
14
+        concat_string_value = test_concat.construct_data(test_class, 'field', {'field1': 'o'*5, 'field2': 'k'*4}, '')
15
+        self.assertEqual('%s*%s' % ('o'*5, 'k'*4), concat_string_value)
16
+
17
+        test_concat.max_length=10
18
+        concat_string_value = test_concat.construct_data(test_class, 'field', {'field1': 'o'*5, 'field2': 'k'*10}, '')
19
+        test_value = '%s*%s' % ('o'*5, 'k'*10)
20
+        self.assertNotEqual(test_value, concat_string_value)
21
+        self.assertEqual(len(concat_string_value), test_concat.max_length)
22
+        self.assertTrue(concat_string_value in test_value)

+ 28
- 0
tests/datahandlers/test_formatstring.py View File

@@ -0,0 +1,28 @@
1
+import unittest
2
+
3
+from lodel.leapi.datahandlers.datas import FormatString
4
+from lodel.editorial_model.components import EmClass
5
+
6
+
7
+class FormatStringTestCase(unittest.TestCase):
8
+
9
+    def test_construct_data(self):
10
+        test_class = EmClass('testing',display_name='testing class')
11
+        test_class.new_field('field1', 'varchar')
12
+        test_class.new_field('field2', 'varchar')
13
+
14
+        test_formatstring = FormatString('%s_%s',['field1', 'field2'], max_length=10)
15
+        formatted_string_value = test_formatstring.construct_data(test_class, 'field', {'field1': 'o'*5, 'field2': 'k'*4}, '')
16
+        self.assertEqual('%s_%s' % ('o'*5, 'k'*4), formatted_string_value)
17
+
18
+    def test_construct_too_long_data(self):
19
+        test_class = EmClass('testing', display_name='testing class')
20
+        test_class.new_field('field1', 'varchar')
21
+        test_class.new_field('field2', 'varchar')
22
+        test_formatstring = FormatString('%s-%s', ['field2', 'field1'], max_length=10)
23
+        formatted_string_value = test_formatstring.construct_data(test_class, 'field', {'field1': 'o'*300, 'field2': 'k'*500},'')
24
+        test_value = '%s-%s' % ('k'*500, 'o'*300)
25
+        self.assertNotEqual(test_value, formatted_string_value)
26
+        self.assertTrue(formatted_string_value in test_value)
27
+        self.assertTrue(len(formatted_string_value) == test_formatstring.max_length)
28
+        self.assertEqual(formatted_string_value, test_value[:test_formatstring.max_length])

Loading…
Cancel
Save