Skip to content

Commit

Permalink
New reganecuqh adapter, model and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmunoznovoa committed Oct 18, 2024
1 parent 6140cd0 commit 0a4bcbc
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ Eina d'importació de CCH
- `P2`
- `P2D`
- `REGANECU`
- `REGANECUQH`
- `RF5D`
23 changes: 23 additions & 0 deletions cchloader/adapters/reganecuqh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from cchloader.adapters import CchAdapter
from cchloader.models.reganecuqh import ReganecuQhSchema
from marshmallow import Schema, fields, pre_load


class ReganecuQhBaseAdapter(Schema):
""" REGANECUQh Adapter
"""

@pre_load
def fix_numbers(self, data):
for attr, field in self.fields.items():
if isinstance(field, (fields.Integer, fields.Float)):
if not data.get(attr):
data[attr] = None
return data


class ReganecuQhAdapter(ReganecuQhBaseAdapter, CchAdapter, ReganecuQhSchema):
pass
35 changes: 35 additions & 0 deletions cchloader/models/reganecuqh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# -*- encoding: utf-8 -*-
from __future__ import absolute_import

from marshmallow import Schema, fields


class ReganecuQhSchema(Schema):
date = fields.String(position=0, required=True)
reserved0 = fields.Integer(position=1, required=True)
upr = fields.String(position=2, required=True)
energia = fields.Float(position=3, required=True)
reserved1 = fields.Integer(position=4)
precio = fields.Float(position=5, required=True)
reserved2 = fields.Integer(position=6)
importe = fields.Float(position=7, required=True)
reserved3 = fields.Integer(position=8)
vendedor = fields.String(position=9, required=True)
segmento = fields.String(position=10, required=True)
facturacion = fields.Integer(position=11, required=True)
eiec_upr = fields.String(position=12, required=True)
cuenta = fields.String(position=13, required=True)
signo_importe = fields.Integer(position=14, required=True)
signo_magnitud = fields.Integer(position=15, required=True)
eic_titular = fields.String(position=16, required=True)
codigo_magnitud = fields.String(position=17, required=True)
codigo_precio = fields.String(position=18, required=True)
codigo_apunte = fields.String(position=19, required=True)
tipo_oferta = fields.String(position=20, required=True)
tipo_upr = fields.Integer(position=21, required=True)
energia_bilateral = fields.Integer(position=22, required=True)
hora = fields.Integer(position=23, required=True)


ReganecuQhSchema()
42 changes: 42 additions & 0 deletions cchloader/parsers/reganecuqh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from cchloader import logger
from cchloader.utils import build_dict
from cchloader.adapters.reganecuqh import ReganecuQhAdapter
from cchloader.models.reganecuqh import ReganecuQhSchema
from cchloader.parsers.parser import Parser, register
import six
if six.PY3:
unicode = str


class ReganecuQh(Parser):

patterns = ['^([ABC])(\d{1})_reganecuqh_(\d{4})(\d{2})(\d{2})_']
encoding = "iso-8859-15"
delimiter = ';'

def __init__(self, strict=False):
self.adapter = ReganecuQhAdapter(strict=strict)
self.schema = ReganecuQhSchema(strict=strict)
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(unicode(line.decode(self.encoding)).split(self.delimiter))
slinia = list(map(lambda s: s.strip(), slinia))
parsed = {'reganecu': {}, 'orig': line}
data = build_dict(self.headers, slinia)
result, errors = self.adapter.load(data)
if errors:
logger.error(errors)
parsed['reganecu'] = result
return parsed, errors


register(ReganecuQh)

0 comments on commit 0a4bcbc

Please sign in to comment.