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 #39 from gisce/new_medidas_format
Soporte para cargar ficheros MEDIDAS
- Loading branch information
Showing
7 changed files
with
141 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
|
||
from cchloader.adapters import CchAdapter | ||
from cchloader.models.medidas import MedidasSchema | ||
from marshmallow import Schema, fields, pre_load | ||
|
||
|
||
class MedidasBaseAdapter(Schema): | ||
""" MEDIDAS 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 | ||
|
||
@pre_load | ||
def fix_ae(self, data): | ||
data['ae'] = data.get('ae', 0) | ||
|
||
@pre_load | ||
def fix_r2(self, data): | ||
data['r2'] = data.get('r2', 0) | ||
|
||
@pre_load | ||
def fix_r3(self, data): | ||
data['r3'] = data.get('r3', 0) | ||
|
||
@pre_load | ||
def fix_factor_potencia(self, data): | ||
factor_potencia = data.get('factor_potencia', 0.0) | ||
if ',' in factor_potencia: | ||
factor_potencia = factor_potencia.replace(',', '.') | ||
data['factor_potencia'] = factor_potencia | ||
|
||
@pre_load | ||
def fix_tipo_factor_potencia(self, data): | ||
data['tipo_factor_potencia'] = data.get('tipo_factor_potencia', 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 MedidasAdapter(MedidasBaseAdapter, CchAdapter, MedidasSchema): | ||
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,21 @@ | ||
# -*- coding: utf-8 -*- | ||
# -*- encoding: utf-8 -*- | ||
from __future__ import absolute_import | ||
|
||
from marshmallow import Schema, fields | ||
from marshmallow.validate import OneOf | ||
|
||
|
||
class MedidasSchema(Schema): | ||
cil = fields.String(position=0, required=True) | ||
datetime = fields.DateTime(position=1, format='%Y/%m/%d %H:%M:%S') | ||
season = fields.Integer(position=2, validate=OneOf([0, 1])) | ||
ae = fields.Integer(position=3, allow_none=True) | ||
r2 = fields.Integer(position=4, allow_none=True) | ||
r3 = fields.Integer(position=5, allow_none=True) | ||
factor_potencia = fields.Float(position=6, allow_none=True) | ||
tipo_factor_potencia = fields.Integer(position=7, allow_none=True) | ||
tipo_lectura = fields.String(position=8, validate=OneOf(['R', 'E'])) | ||
|
||
|
||
MedidasSchema() |
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,40 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
|
||
from cchloader import logger | ||
from cchloader.utils import build_dict | ||
from cchloader.adapters.medidas import MedidasAdapter | ||
from cchloader.models.medidas import MedidasSchema | ||
from cchloader.parsers.parser import Parser, register | ||
|
||
|
||
class Medidas(Parser): | ||
|
||
patterns = ['^medidas_(\d{4})_(\d{4})(\d{2})_(\d{1})_(\d{4})(\d{2})(\d{2})', | ||
'^medidas_(\d{4})_(\d{4})_(\d{4})(\d{2})_(\d{1})_(\d{4})(\d{2})(\d{2})'] | ||
encoding = "iso-8859-15" | ||
delimiter = ';' | ||
|
||
def __init__(self, strict=False): | ||
self.adapter = MedidasAdapter(strict=strict) | ||
self.schema = MedidasSchema(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 = {'medidas': {}, 'orig': line} | ||
data = build_dict(self.headers, slinia) | ||
result, errors = self.adapter.load(data) | ||
if errors: | ||
logger.error(errors) | ||
parsed['medidas'] = result | ||
return parsed, errors | ||
|
||
|
||
register(Medidas) |
Binary file not shown.
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