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 #50 from gisce/new_mcilqh_support
Soporte para cargar ficheros MCILQH
- Loading branch information
Showing
6 changed files
with
123 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 |
---|---|---|
|
@@ -18,6 +18,7 @@ Eina d'importació de CCH | |
- `F1` | ||
- `F5D` | ||
- `INFPA` | ||
- `MCILQH` | ||
- `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,38 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
|
||
from cchloader.adapters import CchAdapter | ||
from cchloader.models.mcilqh import McilQhSchema | ||
from marshmallow import Schema, fields, pre_load | ||
|
||
|
||
class McilQhBaseAdapter(Schema): | ||
""" MCILQH 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 | ||
|
||
@pre_load | ||
def fix_ae(self, data): | ||
ae = int(data.get('ae', 0)) | ||
if ae < 0: | ||
data['ae'] = 0 | ||
|
||
@pre_load | ||
def fix_season(self, data): | ||
valid_values = [0, 1] | ||
season = data.get('season') | ||
if season and season.isdigit() and season in map(str, valid_values): | ||
data['season'] = int(season) | ||
else: | ||
data['season'] = None | ||
|
||
|
||
class McilQhAdapter(McilQhBaseAdapter, CchAdapter, McilQhSchema): | ||
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,26 @@ | ||
# -*- coding: utf-8 -*- | ||
# -*- encoding: utf-8 -*- | ||
from __future__ import absolute_import | ||
|
||
from marshmallow import Schema, fields | ||
from marshmallow.validate import OneOf | ||
|
||
|
||
class McilQhSchema(Schema): | ||
cil = fields.String(position=0, required=True) | ||
year = fields.Integer(position=1, required=True) | ||
month = fields.Integer(position=2, required=True) | ||
day = fields.Integer(position=3, required=True) | ||
hour = fields.Integer(position=4, required=True) | ||
minute = fields.Integer(position=5, required=True) | ||
season = fields.Integer(position=6, validate=OneOf([0, 1])) | ||
ae = fields.Float(position=7, allow_none=True) | ||
ai = fields.Float(position=8, allow_none=True) | ||
r1 = fields.Float(position=9, allow_none=True) | ||
r2 = fields.Float(position=10, allow_none=True) | ||
r3 = fields.Float(position=11, allow_none=True) | ||
r4 = fields.Float(position=12, allow_none=True) | ||
type_measure = fields.String(position=13, validate=OneOf(['R', 'E', 'L', 'M'])) | ||
|
||
|
||
McilQhSchema() |
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
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,42 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
|
||
from cchloader import logger | ||
from cchloader.utils import build_dict | ||
from cchloader.adapters.mcilqh import McilQhAdapter | ||
from cchloader.models.mcilqh import McilQhSchema | ||
from cchloader.parsers.parser import Parser, register | ||
import six | ||
if six.PY3: | ||
unicode = str | ||
|
||
|
||
class McilQh(Parser): | ||
|
||
patterns = ['^MCILQH_'] | ||
encoding = "iso-8859-15" | ||
delimiter = ';' | ||
|
||
def __init__(self, strict=False): | ||
self.adapter = McilQhAdapter(strict=strict) | ||
self.schema = McilQhSchema(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 = {'mcilqh': {}, 'orig': line} | ||
data = build_dict(self.headers, slinia) | ||
result, errors = self.adapter.load(data) | ||
if errors: | ||
logger.error(errors) | ||
parsed['mcilqh'] = result | ||
return parsed, errors | ||
|
||
|
||
register(McilQh) |
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