Guest User

Untitled

a guest
Aug 24th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.35 KB | None | 0 0
  1. class Import:
  2.    
  3.    DECG = '.'
  4.    DECD = ','
  5.    
  6.    def __init__(self, names):
  7.        self.names = names
  8.        self.data = {}
  9.  
  10.    def _load(self, name):
  11.        
  12.        def numeric(s):
  13.            return Decimal(s.replace(self.DECG, '').replace(self.DECD, '.'))
  14.        
  15.        class Processor:
  16.            
  17.            NEXTI = 0
  18.            REI   = 1
  19.            PROCI = 2
  20.            
  21.            def __init__(self):
  22.                rec = re.compile
  23.                self.dptable = {
  24.                    'mark'  :[['namef'], rec(ur'\(Вид соединения: исходящий / Тип соединения: все\)\s*'), self.pmark],
  25.                    'namef' :[['as'], rec(ur'(.+):\s*(.+)\s*'), self.pnamef],
  26.                    'name'  :[['as'], None, self.pname],
  27.                    'as'    :[['sum'], rec(ur'Абонентская установка:\s*(.+)\s*'), self.pas],
  28.                    'sum'   :[['suma'], rec(ur'Сумма для абон. уст.\s*(\d+):\s*;(\d+:\d+:\d+);\s*([0-9.,]+)\s*руб;\s*'), self.psum],
  29.                    'suma'  :[['as', 'sumd'], rec(ur'Сумма для абон. уст.\s*(\d+):\s*\(всего\);\s*;\s*([0-9.,]+)\s*руб;\s*'), self.psuma],
  30.                    'sumd'  :[['sumda'], None, self.psumd],
  31.                    'sumda' :[['name', 'sumo'], None, self.psumda],
  32.                    'sumo'  :[['sumoa'], rec(ur'Всего:\s*;(\d+:\d+:\d+);\s*([0-9.,]+)\s*руб;\s*'), self.psumo],
  33.                    'sumoa' :[[None], rec(ur'Всего:\s*\(всего\);\s*;\s*([0-9.,]+)\s*руб;\s*'), self.psumoa]}
  34.                        
  35.            def pmark(self, grp):
  36.                pass
  37.            
  38.            def pnamef(self, grp):
  39.                self.dname = grp[1].strip()
  40.                div = re.escape(grp[0]) # dont strip!
  41.                self.dptable['name'][self.REI] = re.compile(ur'%s:\s*(.+)\s*' % div)
  42.                self.dptable['sumd'][self.REI] = re.compile(ur'Сумма %s:\s*;(\d+:\d+:\d+);\s*([0-9.,]+)\s*руб;\s*' % div)
  43.                self.dptable['sumda'][self.REI] = re.compile(ur'Сумма %s:\s*\(всего\);\s*;\s*([0-9.,]+)\s*руб;\s*' % div)
  44.                self.dsumd = Decimal()
  45.                
  46.            def pname(self, grp):
  47.                self.dname = grp[0].strip()
  48.                self.dsumd = Decimal()
  49.                
  50.            def pas(self, grp):
  51.                self.das = grp[0].strip()
  52.                self.dsum = Decimal()
  53.                
  54.            def psum(self, grp):
  55.                #id = m.groups()[0]
  56.                self.dsum += numeric(grp[2])
  57.                
  58.            def psuma(self, grp):
  59.                #ida = m.groups()[0]
  60.                dsuma = numeric(grp[1])
  61.                if dsuma <> self.dsum:
  62.                    log.close()
  63.                    raise Exception(('Wrong summa.', self.das, dsuma - self.dsum))
  64.                
  65.            def psumd(self, grp):
  66.                self.dsumd += numeric(grp[1])
  67.                
  68.            def psumda(self, grp):
  69.                dsumda = numeric(grp[0])
  70.                if dsumda <> self.dsumd:
  71.                    log.close()
  72.                    raise Exception(('Wrong summa.', None, dsumda - self.dsumd))
  73.                self.data.append((self.dname, dsumda))
  74.                
  75.            def psumo(self, grp):
  76.                self.sumo = numeric(grp[1])
  77.                
  78.            def psumoa(self, grp):
  79.                self.sumoa = numeric(grp[0])
  80.                
  81.            def __call__(self, lines):
  82.                self.data = []
  83.                expected = ['mark']
  84.                for s in lines:
  85.                    for e in expected:
  86.                        m = self.dptable[e][self.REI].match(s)
  87.                        if m:
  88.                            self.dptable[e][self.PROCI](m.groups())
  89.                            expected = self.dptable[e][self.NEXTI]
  90.                if self.sumo <> self.sumoa:
  91.                    raise Exception(('Wrong overall summa.', None, self.sumoa - self.sumo))
  92.                return self.data
  93.            
  94.        processor = Processor()
  95.        log = codecs.open(name, 'r', encoding='cp1251')
  96.        data = processor(log.readlines())
  97.        log.close()
  98.        return data
  99.        
  100.    def loadall(self):
  101.        for n in self.names:
  102.            self.data[n.lower()] = self._load(n)
Advertisement
Add Comment
Please, Sign In to add comment