Browse Source

Add Concat datahandler

prieto 8 years ago
parent
commit
6db8ef8404
1 changed files with 28 additions and 4 deletions
  1. 28
    4
      lodel/leapi/datahandlers/datas.py

+ 28
- 4
lodel/leapi/datahandlers/datas.py View File

1
 #-*- coding: utf-8 -*-
1
 #-*- coding: utf-8 -*-
2
-
2
+import warnings
3
 from lodel.leapi.datahandlers.datas_base import *
3
 from lodel.leapi.datahandlers.datas_base import *
4
 
4
 
5
 ##@brief Data field designed to handle formated strings
5
 ##@brief Data field designed to handle formated strings
18
         self._format_string = format_string
18
         self._format_string = format_string
19
         super().__init__(internal='automatic', max_length=max_length)
19
         super().__init__(internal='automatic', max_length=max_length)
20
 
20
 
21
-
22
     def can_override(self, data_handler):
21
     def can_override(self, data_handler):
23
         if not super().can_override(data_handler):
22
         if not super().can_override(data_handler):
24
             return False
23
             return False
25
         if data_handler.max_length != self.max_length:
24
         if data_handler.max_length != self.max_length:
26
             return False
25
             return False
27
         return True
26
         return True
28
-
27
+    
28
+    def construct_data(self, emcomponent, fname, datas, cur_value):
29
+        ret = self._format_string % tuple([ datas[fname] for fname in self._field_list ])
30
+        if len(ret) > self.max_length:
31
+            warnings.warn("Format field overflow. Truncating value")
32
+            ret = ret[:self.max_length-1]
33
+        return ret
34
+    
29
 ##@brief Varchar validated by a regex
35
 ##@brief Varchar validated by a regex
30
 class Regex(Varchar):
36
 class Regex(Varchar):
31
 
37
 
39
     def __init__(self, regex='', max_length=10, **kwargs):
45
     def __init__(self, regex='', max_length=10, **kwargs):
40
         self.regex = regex
46
         self.regex = regex
41
         self.compiled_re = re.compile(regex)  # trigger an error if invalid regex
47
         self.compiled_re = re.compile(regex)  # trigger an error if invalid regex
42
-
43
         super(self.__class__, self).__init__(max_length=max_length, **kwargs)
48
         super(self.__class__, self).__init__(max_length=max_length, **kwargs)
44
 
49
 
45
     def _check_data_value(self, value):
50
     def _check_data_value(self, value):
86
 
91
 
87
     def _check_data_consistency(self, emcomponent, fname, datas):
92
     def _check_data_consistency(self, emcomponent, fname, datas):
88
         datas[fname] = emcomponent.__class__.__name__
93
         datas[fname] = emcomponent.__class__.__name__
94
+        
95
+##@brief Data field designed to handle concatenated fields
96
+class Concat(FormatString):
97
+    help = 'Automatic strings concatenation'
98
+    base_type = 'char'
99
+    
100
+    ##@brief Build its content with a field list and a separator
101
+    # @param field_list list : List of field to use
102
+    # @param max_length int : the maximum length of the handled value
103
+    # @param separator str
104
+    # @param **kwargs    
105
+    def _init__(self, field_list, max_length, separator = ' ', **kwargs):
106
+        if not field_list is None:
107
+            format_string = '%s'
108
+            for i in range(1,len(field_list)):
109
+                format_string = format_string + separator + '%s'
110
+                
111
+        super().__init__(format_string, field_list, max_length, **kwargs)
112
+

Loading…
Cancel
Save