diff Generic/fiebdc.py @ 21:f7e0cc58737f

Default interface in readFile method
author Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
date Sun, 14 Sep 2014 17:23:02 +0200
parents f5ec50b625d1
children 65e7ae0d0e63
line wrap: on
line diff
--- a/Generic/fiebdc.py	Thu Sep 11 19:03:07 2014 +0200
+++ b/Generic/fiebdc.py	Sun Sep 14 17:23:02 2014 +0200
@@ -53,6 +53,7 @@
         "__cancel": Boolean value, True mean that the read process must stop
         "__filename": The filename of the fiebdc file that is readed
         "__pattern": re compiled pattern dict
+        "__statistics": Statistics object, records number
     Methods:
         __init__(self, filename=None, budget=None)
         cancel(self)
@@ -117,6 +118,7 @@
             "after_first_tilde" : re.compile(u"^[^~]*~"),
             "end_control" : re.compile(u"((\r\n)| |\t)+$"),
             }
+        self.__statistics = Statistics()
 
     def cancel(self):
         """def cancel(self)
@@ -322,69 +324,69 @@
         # TODO:  ~O. Registro tipo Relación Comercial.
         # TODO: test records
         _field_list = record.split(u"|")
-        self._record_number = self._record_number +1
+        self.__statistics.records = self.__statistics.records +1
         _budget = self.__budget
         if _field_list[0] == u"V":
-            self._record_V_number += 1
+            self.__statistics.V += 1
             self._parseV(_field_list)
         elif _field_list[0] == u"C":
-            self._record_C_number += 1
+            self.__statistics.C += 1
             self._parseC(_field_list)
         elif _field_list[0] == u"D":
-            self._record_D_number += 1
+            self.__statistics.D += 1
             self._parseDY(_field_list)
         elif _field_list[0] == u"Y":
-            self._record_Y_number += 1
+            self.__statistics.Y += 1
             self._parseDY(_field_list)
         elif _field_list[0] == u"M":
-            self._record_M_number += 1
+            self.__statistics.M += 1
             self._parseMN(_field_list)
         elif _field_list[0] == u"N":
-            self._record_N_number += 1
+            self.__statistics.N += 1
             self._parseMN(_field_list)
         elif _field_list[0] == u"T":
-            self._record_T_number += 1
+            self.__statistics.T += 1
             self._parseT(_field_list)
         elif _field_list[0] == u"K":
-            self._record_K_number += 1
+            self.__statistics.K += 1
             self._parseK(_field_list)
         elif _field_list[0] == u"W":
-            self._record_W_number += 1
+            self.__statistics.W += 1
             self._parseW(_field_list)
         elif _field_list[0] == u"L":
-            self._record_L_number += 1
+            self.__statistics.L += 1
             self._parseL(_field_list)
         elif _field_list[0] == u"Q":
-            self._record_Q_number += 1
+            self.__statistics.Q += 1
             self._parseQ(_field_list)
         elif _field_list[0] == u"J":
-            self._record_J_number += 1
+            self.__statistics.J += 1
             self._parseJ(_field_list)
         elif _field_list[0] == u"G":
-            self._record_G_number += 1
+            self.__statistics.G += 1
             self._parseG(_field_list)
         elif _field_list[0] == u"E":
-            self._record_E_number += 1
+            self.__statistics.E += 1
             self._parseE(_field_list)
         elif _field_list[0] == "O":
-            self._record_O_number += 1
+            self.__statistics.O += 1
         elif _field_list[0] == u"P":
-            self._record_P_number += 1
+            self.__statistics.P += 1
             self._parseP(_field_list)
         elif _field_list[0] == u"X":
-            self._record_X_number += 1
+            self.__statistics.X += 1
             self._parseX(_field_list)
         elif _field_list[0] == u"B":
-            self._record_B_number += 1
+            self.__statistics.B += 1
             self._parseB(_field_list)
         elif _field_list[0] == u"F":
-            self._record_F_number += 1
+            self.__statistics.F += 1
             self._parseF(_field_list)
         elif _field_list[0] == u"A":
-            self._record_A_number += 1
+            self.__statistics.A += 1
             self._parseA(_field_list)
         else:
-            self._record_Unknow_number += 1
+            self.__statistics.unknow += 1
 
     def _parseV(self, field_list):
         """_parseV(self, field_list)
@@ -401,10 +403,10 @@
             8- [Number budget certificate]
             9- [Date budget certificate]
         """
-        if self._record_number != 1:
+        if self.__statistics.records != 1:
             print utils.mapping(_("The 'V' record (Property and Version) "\
                     "must be the first record in the file but it is the "\
-                    "number: $1"), (self._record_number,))
+                    "number: $1"), (self.__statistics.records,))
             print _("The default values were taken and this V record is "\
                   "ignored")
             return
@@ -494,7 +496,7 @@
             self.__budget.setCertificateDate(_parsed_date_cerfificate)
         elif _data_type != "":
             self.__budget.setBudgeType(_data_type)
-        self.num_valid_record = self.num_valid_record + 1
+        self.__statistics.valid = self.__statistics.valid + 1
 
     def _parseK(self, field_list):
         """_parseK(self, field_list)
@@ -736,7 +738,7 @@
                 _decimal_index = _decimal_index + 13
                 self.__budget.setDecimals(_percentage_dict,
                                            (_decimal_index//13))
-        self.num_valid_record = self.num_valid_record +1
+        self.__statistics.valid = self.__statistics.valid +1
 
     def _parseC(self, field_list):
         """_parseC(self, field_list)
@@ -913,7 +915,7 @@
         self.__budget.setRecord(_code.encode("utf8"), _synonyms, _hierarchy,
             _unit.encode("utf8"), _summary.encode("utf8"),
             _prices, _dates, _type, _subtype.encode("utf8"))
-        self.num_valid_record = self.num_valid_record + 1
+        self.__statistics.valid = self.__statistics.valid + 1
     
     def _parseDY(self, field_list):
         """_parseDY(self, field_list)
@@ -984,7 +986,7 @@
             self.__budget.setTree(_code.encode("utf8"), _child_code.encode("utf8"), _position, _factor, 
                 _yield, "", "", "", "")
             _child_index = _child_index + 3
-        self.num_valid_record = self.num_valid_record +1
+        self.__statistics.valid = self.__statistics.valid +1
 
     def _parseT(self, field_list):
         """_parseT(self, field_list)
@@ -1012,7 +1014,7 @@
         _code = self.validateCode(_code) 
         # _____Text_____
         self.__budget.setText(_code.encode("utf8"), _text.encode("utf8"))
-        self.num_valid_record = self.num_valid_record + 1
+        self.__statistics.valid = self.__statistics.valid + 1
 
     def _parseMN(self, field_list):
         """_parseMN(self, field_list)
@@ -1151,7 +1153,7 @@
             _line_index = _line_index + 6
         self.__budget.setTree(_parent_code, _child_code.encode("utf8"), _path, "", "",
                            _total, _line_list, _label.encode("utf8"), _record_type.encode("utf8"))
-        self.num_valid_record = self.num_valid_record + 1
+        self.__statistics.valid = self.__statistics.valid + 1
 
     def _parseW(self, field_list):
         """_parseW(self, field_list)
@@ -1190,7 +1192,7 @@
                 _field_dict[_field_code.encode("utf8")] = _field_title.encode("utf8")
             _field_index = _field_index + 2
         self.__budget.setSheetFields(_field_dict)
-        self.num_valid_record = self.num_valid_record +1
+        self.__statistics.valid = self.__statistics.valid +1
     
     def _parseL(self, field_list):
         """_parseL(self, field_list)
@@ -1240,7 +1242,7 @@
                     _section_dict[_section_code.encode("utf8")] = _section_title.encode("utf8")
                 _section_index = _section_index + 2
             self.__budget.setSheetSections(_section_dict)
-            self.num_valid_record = self.num_valid_record +1
+            self.__statistics.valid = self.__statistics.valid +1
             
         else:
             # Any INFORMATION after last field separator is ignored
@@ -1283,7 +1285,7 @@
                     _section_index = _section_index + 2
                 self.__budget.setSheetParagraphs(_paragraph_dict)
                 self.__budget.setSheetRecord(_record_code.encode("utf8"), "*", _section_dict)
-                self.num_valid_record = self.num_valid_record +1
+                self.__statistics.valid = self.__statistics.valid +1
     
     def _parseQ(self, field_list):
         """_parseQ(self, field_list)
@@ -1344,7 +1346,7 @@
             _section_index = _section_index + 3
         for _field, _section_dict in _field_dict.iteritems():
             self.__budget.setSheetRecord(_record_code.encode("utf8"), _field.encode("utf8"), _section_dict)
-        self.num_valid_record = self.num_valid_record +1
+        self.__statistics.valid = self.__statistics.valid +1
     
     def _parseJ(self, field_list):
         """_parseJ(self, field_list)
@@ -1375,7 +1377,7 @@
             print "Html and rtf files not yet implemented in ~J record"
         else:
             self.__budget.setSheetParagraph(paragraph_code.encode("utf8"), paragraph_text.encode("utf8"))
-            self.num_valid_record = self.num_valid_record +1
+            self.__statistics.valid = self.__statistics.valid +1
     
     def _parseG(self, field_list):
         """_parseG(self, field_list)
@@ -1444,7 +1446,7 @@
         if len(_grafic_file_list) > 0:
             for _grafic_file in _tested_grafic_file_list:
                 self.__budget.addFile(_record_code.encode("utf8"), _grafic_file, "img", "")
-            self.num_valid_record = self.num_valid_record +1
+            self.__statistics.valid = self.__statistics.valid +1
     
     def _parseE(self, field_list):
         """_parseE(self, field_list)
@@ -1543,7 +1545,7 @@
                     _sumamary.encode("utf8"), _name.encode("utf8"), 
                     _local_offices, _cif.encode("utf8"),
                     _web.encode("utf8"), _email.encode("utf8"))
-        self.num_valid_record = self.num_valid_record +1
+        self.__statistics.valid = self.__statistics.valid +1
     
     def _parseX(self, field_list):
         """_parseX(self, field_list)
@@ -1598,7 +1600,7 @@
                     _ti_dict[_ti_code.encode("utf8")] = _ti_value.encode("utf8")
                 _ti_index = _ti_index + 2
             self.__budget.setTecnicalInformation(_record_code.encode("utf8"), _ti_dict)
-        self.num_valid_record = self.num_valid_record +1
+        self.__statistics.valid = self.__statistics.valid +1
 
     def _parseF(self, field_list):
         """_parseF(self, field_list)
@@ -1703,7 +1705,7 @@
         if len(_tested_files_list) > 0:
             for _file in _tested_file_list:
                 self.__budget.addFile(_record_code.encode("utf8"), _file[0], file[1], file[2])
-        self.num_valid_record = self.num_valid_record +1
+        self.__statistics.valid = self.__statistics.valid +1
 
     def _parseB(self, field_list):
         """_parseB(self, field_list)
@@ -1732,7 +1734,7 @@
         _new_code = self.validateCode(_new_code)
         # change code
         self.__budget.changeCode(_code, _new_code)
-        self.num_valid_record = self.num_valid_record + 1
+        self.__statistics.valid = self.__statistics.valid + 1
 
     def _parseA(self, field_list):
         """_parseA(self, field_list)
@@ -1769,7 +1771,7 @@
         _label_list = _labels.split(u"\\")
         for _label in _label_list:
             self.__budget.addLabel(_code.encode("utf8"), _label.encode("utf8"))
-        self.num_valid_record = self.num_valid_record + 1
+        self.__statistics.valid = self.__statistics.valid + 1
 
     def _parseP(self, field_list):
         """_parseP(self, field_list)
@@ -1970,7 +1972,7 @@
         for _line in _lines:
             pass
             #print _line
-        self.num_valid_record = self.num_valid_record + 1
+        self.__statistics.valid = self.__statistics.valid + 1
 
     def readFile(self, budget=None, filename=None, interface=None):
         """readFile(self, budget=None, filename=None)
@@ -1978,8 +1980,11 @@
         filename: the filename of the fiebdc file
         budget: base.obra object
         interface: a object to send messages
-            must have printf(message) progress(percent)
-                      recordStatistics(...)
+            must have readFile_send_message(message)
+                      readFile_set_statistics(statistics)
+                      readFile_progress(percent)
+                      readFile_end()
+                      readFile_cancel()
         Return the budget objetc or None if the file can be readed
         """
         if not filename is None and not budget is None:
@@ -1990,6 +1995,9 @@
             return None
         if not os.path.exists(self.__filename):
             return None
+        if interface is None:
+            interface = Interface()
+        interface.readFile_set_statistics(self.__statistics)
         _time = time.time()
         try:
             _file =  open(self.__filename, 'r')
@@ -1997,32 +2005,10 @@
             print utils.mapping("IOError: $1", (self.__filename,))
             return None
         self.__budget.filename = self.__filename
-        self._record_number = 0
-        self.num_valid_record = 0
-        self._record_V_number = 0
-        self._record_C_number = 0
-        self._record_D_number = 0
-        self._record_Y_number = 0
-        self._record_M_number = 0
-        self._record_N_number = 0
-        self._record_T_number = 0
-        self._record_K_number = 0
-        self._record_W_number = 0
-        self._record_L_number = 0
-        self._record_Q_number = 0
-        self._record_J_number = 0
-        self._record_G_number = 0
-        self._record_E_number = 0
-        self._record_O_number = 0
-        self._record_P_number = 0
-        self._record_X_number = 0
-        self._record_B_number = 0
-        self._record_F_number = 0
-        self._record_A_number = 0
-        self._record_Unknow_number = 0
-        print utils.mapping(_("Loading file $1"), (self.__filename,))
+        interface.readFile_send_message(utils.mapping(_("Loading file $1"),
+                         (self.__filename,)))
         _filesize = float(os.path.getsize(self.__filename))
-        interface.progress(_file.tell() / _filesize)
+        interface.readFile_progress(_file.tell() / _filesize)
         _buffer = _file.read(1000)
         # set codepage from V record
         _record_list = _buffer.split("~")
@@ -2039,18 +2025,23 @@
                 # remove leading spaces
                 if _version in self.__character_sets_dict:
                     self.__character_set = self.__character_sets_dict[_version]
-                    print utils.mapping(_("FIEBDC character encoding: $1"),(self.__character_set,))
+                    interface.readFile_send_message(utils.mapping(
+                        _("FIEBDC character encoding: $1"),
+                          (self.__character_set,)))
                 else:
-                    print utils.mapping(_("This Character encoding do not exist in "\
-                         "FIEBDC3! Default Character encoding: $1"),
-                         (self.__character_set,))
+                    interface.readFile_send_message(utils.mapping(
+                        _("This Character encoding do not exist in "\
+                          "FIEBDC3! Default Character encoding: $1"),
+                          (self.__character_set,)))
             else:
-                print utils.mapping(_("This V record dot have a character encoding! "\
+                interface.readFile_send_message(utils.mapping(_(
+                         "This V record dot have a character encoding! "\
                          "Default character encoding: $1"),
-                         (self.__character_set,))
+                         (self.__character_set,)))
         else:
-            print utils.mapping(_("Not 'V' record in File! Default character encoding: "\
-                  "$1"), (self.__character_set,))
+            interface.readFile_send_message(utils.mapping(_(
+                  "Not 'V' record in File! Default character encoding: "\
+                  "$1"), (self.__character_set,)))
         _buffer = unicode(_buffer, self.__character_set)
         # Any INFORMATION between the beginning of the file and the
         # beginning of the first registry “~” is ignored
@@ -2080,50 +2071,24 @@
                 if self.__cancel:
                     break
                 self.parseRecord(record)
-            interface.progress(_file.tell() / _filesize)
+            interface.readFile_progress(_file.tell() / _filesize)
             _buffer2 = _file.read(100000)
             _buffer2 = unicode(_buffer2, self.__character_set)
             _buffer = _last_record + _buffer2
         _file.close()
         if self.__cancel:
-            print _("Process terminated")
+            interface.readFile_cancel()
             return None
         else:
-            print utils.mapping(_("Time to load: $1 seconds"),
-                 (("%.2f" %(time.time()-_time)),))
-            print utils.mapping(_("Records/Valid Records: $1/$2"),
-                  (self._record_number, self.num_valid_record))
-            if self._record_O_number > 0:
-                print utils.mapping(_("$1 unsuported record type O: "\
-                  "Comercial Relationship"), (self._record_O_number,))
-            if self.num_valid_record == 0:
-                print  _("This file is not a valid FIBDC3 file")
+            self.__statistics.time = time.time()-_time
+            if self.__statistics.O > 0:
+                interface.readFile_send_message(
+                    utils.mapping(_("$1 unsuported record type O: "\
+                    "Comercial Relationship"), (self.__statistics.O,)))
+            if self.__statistics.valid == 0:
+                interface.readFile_send_message(_("This file is not a valid FIBDC3 file"))
                 return None
-            _str = ""
-            for type in \
-                     [("V", self._record_V_number),
-                     ("C", self._record_C_number),
-                     ("D", self._record_D_number),
-                     ("Y", self._record_Y_number),
-                     ("M", self._record_M_number),
-                     ("N", self._record_N_number),
-                     ("T", self._record_T_number),
-                     ("K", self._record_K_number),
-                     ("W", self._record_W_number),
-                     ("L", self._record_L_number),
-                     ("Q", self._record_Q_number),
-                     ("J", self._record_J_number),
-                     ("G", self._record_G_number),
-                     ("E", self._record_E_number),
-                     ("O", self._record_O_number),
-                     ("P", self._record_P_number),
-                     ("X", self._record_X_number),
-                     ("B", self._record_B_number),
-                     ("F", self._record_F_number),
-                     ("A", self._record_A_number),
-                     ("?", self._record_Unknow_number)]:
-                _str = _str + "%s: %s\n" %(type[0], type[1])
-            print  _str
+            interface.readFile_end()
             self._testBudget(self.__budget)
             return self.__budget
 
@@ -2164,3 +2129,164 @@
         text = text.replace(u"\r", u"")
         text = text.replace(u"\n", u"")
         return text
+    
+class Interface(object):
+    """fiebdc.Interface
+    
+    Description:
+        An example interface
+    Constructor:
+        fiebdc.Interface()
+    Ancestry:
+    +-- object
+      +-- Interface
+    Atributes:
+        "__progress": The progress percentage
+        "__statistics": The record statistics 
+    Methods:
+        __init__(self)
+        readFile_send_message(message)
+        readFile_progress(percent)
+        readFile_set_statistics(statistics)
+        readFile_end()
+        readFile_cancel()
+        
+    """
+    def __init__(self):
+        self.__progress = 0.0
+        self.__statistics = Statistics()
+
+    def readFile_set_statistics(self, statistics):
+        """readFile_set_statistics(statistics)
+        
+        statistics: record statistics from readFile method
+        
+        sets record statistics
+        """
+        self.__statistics = statistics
+
+    def readFile_send_message(self, message):
+        """readFile_send_message(message)
+        
+        message: mesage from readFile method
+        
+        print message
+        """
+        print message
+
+    def readFile_progress(self, percent):
+        """progress(percent)
+        
+        percent: Percentage executed.
+        
+        Sets progress
+        """
+        self.__progress = percent
+
+    def readFile_end(self):
+        """readFile_end()
+        
+        The readFile method end successfully
+        """
+        print self.__statistics
+
+    def readFile_cancel(self):
+        """readFile_cancel()
+        
+        The readFile method is canceled
+        """
+        print _("Process terminated")
+
+class Statistics(object):
+    """fiebdc.Statistics
+    
+    Description:
+        BC3 Statistics. Records types.
+    Constructor:
+        fiebdc.Statistics()
+    Ancestry:
+    +-- object
+      +-- Statistics
+    Atributes:
+        "records": number of records
+        "valid": number of valid records
+        "V": number of  V records
+        "C": number of C records
+        "D":number of D records
+        "Y":number of Y records
+        "M":number of M records
+        "N":number of N records
+        "T":number of T records
+        "K":number of K records
+        "W":number of W records
+        "L":number of L records
+        "Q":number of Q records
+        "J": number of J records
+        "G":number of G records
+        "E":number of E records
+        "O":number of O records
+        "P":number of P records
+        "X":number of X records
+        "B":number of B records
+        "F":number of F records
+        "A":number of A records
+        "unknow": number of Unknow records
+        "time": Time to load
+
+    Methods:
+        __init__(self)
+
+    """
+    def __init__(self):
+            self.records = 0
+            self.valid = 0
+            self.V = 0
+            self.C = 0
+            self.D = 0
+            self.Y = 0
+            self.M = 0
+            self.N = 0
+            self.T = 0
+            self.K = 0
+            self.W = 0
+            self.L = 0
+            self.Q = 0
+            self.J = 0
+            self.G = 0
+            self.E = 0
+            self.O = 0
+            self.P = 0
+            self.X = 0
+            self.B = 0
+            self.F = 0
+            self.A = 0
+            self.unknow = 0
+            self.time = 0.0
+
+    def __str__(self):
+        return utils.mapping(_("Time to load: $1 seconds"),
+                (("%.2f" %(self.time)),)) + "\n" + \
+               utils.mapping(_("Records/Valid Records: $1/$2"), 
+               (self.records, self.valid)) + "\n" +\
+               "V: %s\n" %(self.V,) + \
+               "C: %s\n" %(self.C,) + \
+               "D: %s\n" %(self.D,) + \
+               "Y: %s\n" %(self.Y,) + \
+               "M: %s\n" %(self.M,) + \
+               "N: %s\n" %(self.N,) + \
+               "T: %s\n" %(self.T,) + \
+               "K: %s\n" %(self.K,) + \
+               "W: %s\n" %(self.W,) + \
+               "L: %s\n" %(self.L,) + \
+               "Q: %s\n" %(self.Q,) + \
+               "J: %s\n" %(self.J,) + \
+               "G: %s\n" %(self.G,) + \
+               "E: %s\n" %(self.E,) + \
+               "O: %s\n" %(self.O,) + \
+               "P: %s\n" %(self.P,) + \
+               "X: %s\n" %(self.X,) + \
+               "B: %s\n" %(self.B,) + \
+               "F: %s\n" %(self.F,) + \
+               "A: %s\n" %(self.A,) + \
+               "?: %s\n" %(self.unknow,)
+