Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soporte para cargar ficheros INFPA #40

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Eina d'importació de CCH
- `CORBAGEN`
- `F1`
- `F5D`
- `INFPA`
- `MEDIDAS`
- `MHCIL`
- `P1`
Expand Down
23 changes: 23 additions & 0 deletions cchloader/adapters/infpa.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.infpa import InfpaSchema
from marshmallow import Schema, fields, pre_load


class InfpaBaseAdapter(Schema):
""" INFPA Adapter
"""

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


class InfpaAdapter(InfpaBaseAdapter, CchAdapter, InfpaSchema):
pass
13 changes: 13 additions & 0 deletions cchloader/models/infpa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
# -*- encoding: utf-8 -*-
from __future__ import absolute_import

from marshmallow import Schema, fields


class InfpaSchema(Schema):
cil = fields.String(position=0, required=True)
valor = fields.Integer(position=1, required=True)
horas = fields.Integer(position=2, required=True)

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

from cchloader import logger
from cchloader.utils import build_dict
from cchloader.adapters.infpa import InfpaAdapter
from cchloader.models.infpa import InfpaSchema
from cchloader.parsers.parser import Parser, register


class Infpa(Parser):

patterns = ['^INFPA_([H][23CPD])_(\d{4})_([PA][12])_(\d{4})(\d{2}).(\d)',
'^INFPA_([H][23CPD])_(\d{4})_([PA][12])_(\d{4})(\d{2})']
encoding = "iso-8859-15"
delimiter = ';'

def __init__(self, strict=False):
self.adapter = InfpaAdapter(strict=strict)
self.schema = InfpaSchema(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 = map(lambda s: s.strip(), slinia)
parsed = {'infpa': {}, 'orig': line}
data = build_dict(self.headers, slinia)
result, errors = self.adapter.load(data)
if errors:
logger.error(errors)
parsed['infpa'] = result
return parsed, errors


register(Infpa)
Binary file added spec/curve_files/INFPA_H3_1234_P2_202401.0.bz2
Binary file not shown.
16 changes: 16 additions & 0 deletions spec/test_parsers_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cchloader.parsers.mhcil import Mhcil
from cchloader.parsers.medidas import Medidas
from cchloader.parsers.corbagen import CorbaGen
from cchloader.parsers.infpa import Infpa
from cchloader.exceptions import ParserNotFoundException
from cchloader.file import PackedCchFile, CchFile

Expand Down Expand Up @@ -56,6 +57,9 @@
'CORBAGEN_202403.0' # Documented
]
self.wrong_filename = 'P1_20170507_20170706.6'
self.infpa_filenames = [
'INFPA_H3_1234_P2_202401.0.bz2' # Documented
]

with it('test to get F1 parser'):
for filename in self.f1_filenames:
Expand Down Expand Up @@ -174,6 +178,18 @@
assert result_corbagen == expected_corbagen
break

with it('test to get INFPA parser'):
for filename in self.infpa_filenames:
expect(get_parser(filename)).to(equal(Infpa))
with it('INFPA parser fits file format'):
with PackedCchFile('spec/curve_files/INFPA_H3_1234_P2_202401.0.bz2') as packed:
for cch_file in packed:
for line in cch_file:
expected_infpa = 'ES1234000000002267QR1F001;0;20;\n'
result_infpa = line['orig']
assert result_infpa == expected_infpa
break

with it('test error to get exception'):
def test_raise_error():
get_parser(self.wrong_filename)
Expand Down
Loading