Esempio n. 1
0
class CnmcCons(Parser):

    # En el cas de les mesures, usem Schema per mantenir el format i
    # perque no hi trobarem mes comes que les delimiters
    pattern = '[0-9]{4}-[0-9]{2}-[0-9]{2}_electricidad_consumos.csv'
    encoding = "UTF-8"
    delimiter = ','

    def __init__(self, strict=False):
        self.schema = CnmcMeasuresSchema(strict=strict)
        self.adapter = CnmcMeasuresAdapter(strict=strict)
        self.measures_adapter = self.adapter
        self.fields = []
        self.headers = []
        for f in sorted(self.schema.fields,
                key=lambda f: self.schema.fields[f].metadata['position']):
            field = self.schema.fields[f]
            self.fields.append((f, field.metadata))
            self.headers.append(f)

    def parse_line(self, line):
        slinia = tuple(line.split(self.delimiter))
        slinia = map(lambda s: s.strip(), slinia)
        parsed = {'ps': {}, 'measure_cnmc': [], 'orig': line}
        all_errors = {}
        consums = build_dict(self.headers, slinia)
        result, errors = self.adapter.load(consums)
        if errors:
            logger.error(errors)
            all_errors.update(errors)
        parsed['measure_cnmc'] = result

        return parsed, errors
Esempio n. 2
0
 def __init__(self, strict=False):
     self.schema = CnmcMeasuresSchema(strict=strict)
     self.adapter = CnmcMeasuresAdapter(strict=strict)
     self.measures_adapter = self.adapter
     self.fields = []
     self.headers = []
     for f in sorted(self.schema.fields,
             key=lambda f: self.schema.fields[f].metadata['position']):
         field = self.schema.fields[f]
         self.fields.append((f, field.metadata))
         self.headers.append(f)