Ejemplo n.º 1
0
    def __init__(self,filename,options,sitemodel=None,sqlopts=None):
        self.fn = filename
        self.options = options
        self.raw = []
        self.date = None
        self.ttp = TankToPump(sitemodel=sitemodel, options=options)
        self.sitemodel = sitemodel
        self.site = self.sitemodel.gsap
        self.sqlopts_tank = sqlopts[0]
        self.sqlopts_pump = sqlopts[1]
        self.ttp.sql_table  = sqlopts[2]

        if options.verbose: print("Opening " + self.fn + "...")
        with open(self.fn) as f:
            csvread = csv.reader(f,delimiter=',')
            for row in csvread:
                self.raw.append(row)

        #last line is crap
        trim = self.raw[:-1]

        for row in trim:
            try:
                # Side effect: updates self.ttp
                self.parse_row(row)
            except LogException as e:
                if options.errors: print(e)
                #ipdb.set_trace()

        #Finally analyze the pumps and remove dupes
        self.ttp.analyze_pumps()

        if self.options.verbose:
            print(self.ttp.output())
Ejemplo n.º 2
0
class LogEntry:
    def __init__(self,filename,options,sitemodel=None,sqlopts=None):
        self.fn = filename
        self.options = options
        self.raw = []
        self.date = None
        self.ttp = TankToPump(sitemodel=sitemodel, options=options)
        self.sitemodel = sitemodel
        self.site = self.sitemodel.gsap
        self.sqlopts_tank = sqlopts[0]
        self.sqlopts_pump = sqlopts[1]
        self.ttp.sql_table  = sqlopts[2]

        if options.verbose: print("Opening " + self.fn + "...")
        with open(self.fn) as f:
            csvread = csv.reader(f,delimiter=',')
            for row in csvread:
                self.raw.append(row)

        #last line is crap
        trim = self.raw[:-1]

        for row in trim:
            try:
                # Side effect: updates self.ttp
                self.parse_row(row)
            except LogException as e:
                if options.errors: print(e)
                #ipdb.set_trace()

        #Finally analyze the pumps and remove dupes
        self.ttp.analyze_pumps()

        if self.options.verbose:
            print(self.ttp.output())

    def parse_row(self,row):
        tmp_tank = tmp_pump = tmp_capacity = tmp_grade = None
            
        for item in row:
            # All these checks are agressive. They set the class the first time
            # They are found and ignore future matches.
            self.get_date(item) 

            valid_tank_num = self.get_tank_num(item)
            if valid_tank_num and tmp_tank is None:
                tmp_tank = valid_tank_num
                #tank = self.ttp.get_tank(valid_tank_num)

            valid_pump_num = self.get_pump_num(item)
            if valid_pump_num and tmp_pump is None:
                tmp_pump = valid_pump_num
                #pump = self.ttp.get_pump(valid_pump_num)

            valid_capacity = self.get_capacity(item)
            if valid_capacity and tmp_capacity is None:
                tmp_capacity = valid_capacity
                #capacity = valid_capacity

            valid_grade = self.get_grade(item)
            if valid_grade and tmp_grade is None:
                tmp_grade = valid_grade
                #grade = valid_grade

        # By this point we should have all four, if not, raise..
        if None in (tmp_tank, tmp_pump, tmp_capacity, tmp_grade):
            raise LogException("Did not find all of the required data... :: "
                               + "Tank=" + str(tmp_tank) + " :: "
                               + "Pump=" + str(tmp_pump) + " :: "
                               + "Cap=" + str(tmp_capacity) + " :: "
                            + "Grade=" + str(tmp_grade) + "\n"
                           "ROW= "+ str(row) + "\n")

        # We have all the tmp versions, now assign them for reals
        tank = self.ttp.get_tank(tmp_tank)
        pump = self.ttp.get_pump(tmp_pump)
        capacity = tmp_capacity
        grade = tmp_grade

        # NEVER use 'valid_' vars as these get dirty
        tank.sql_table = self.sqlopts_tank
        tank.grade = grade
        tank.sitemodel = self.sitemodel
        tank.site = self.site
        tank.capacity = capacity
        tank.add_feed(pump)
        tank.date = self.date
        pump.sql_table = self.sqlopts_pump
        pump.sitemodel = self.sitemodel
        pump.site = self.site
        pump.add_grade(grade)
        pump.add_tank(tank)
        pump.date = self.date

    def get_grade(self,st):
        st = re.sub(r'^\s+',"",st)
        st = re.sub(r'\s+$',"",st)
        if st in ["UNL PREM","UNL","UNLEADED","PU 50"]: return Grade.Unleaded
        if st in ["DIESEL","DIESELEXT","DIESEL EXTRA","DIESEL EXT"]: return Grade.Diesel
        if st in ["V POWER","V-POWER UNL","V-POWER GAS",
                  "VPOWER UNL","V-POWER","VPOWER", "V - POWER",
                  "V POWER GASOLIN"]: return Grade.VP
        if st in ["V POWER DIESEL","V-POWER DIESEL","V-POWR DIESEL",
                 "VP DIESEL"]: return Grade.VPD
        if st in ["LPG"]: return Grade.LPG
        return None

    def get_tank_num(self,st):
        test = re.match(r'\s*TANK\s(?P<num>\d+)',st)
        if not test: return None
        else: return int(test.group('num'))
   
    def get_pump_num(self,st):
        test = re.match(r'\s*PUMP\s(?P<num>\d+)',st)
        if not test: return None
        else: return int(test.group('num'))

    def get_capacity(self,st):
        # we dont want it if it has leading zeros...
        if re.match(r'^0{2,}',st) is not None: return None
        # here is the capacity match...
        test = re.match(r'\s*(?P<num>\d+)',st)
        if not test: return None
        else: return int(test.group('num'))

    def get_date(self, st):
        """Finds the date of the log and side_effect sets it """
        test = re.match(r'(?P<date>((19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])))',st)
        if test is not None:
            self.date = str(test.group('date'))