forked from Som-Energia/cchloader
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from gisce/new_infpa_format
Soporte para cargar ficheros INFPA
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ Eina d'importació de CCH | |
- `CORBAGEN` | ||
- `F1` | ||
- `F5D` | ||
- `INFPA` | ||
- `MEDIDAS` | ||
- `MHCIL` | ||
- `P1` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters