# encoding: utf-8
from __future__ import print_function, division, absolute_import
from os.path import join, exists, dirname, abspath, basename
from .domain_objects import Site, Picture, Parameter, Parameters, SourceType
from .errors import ConsistencyError
from .logger import logger
from .utils import iter_to_list
from .yaml_stuff import parse_date, check_yaml_fields_exist, load_yaml_and_bunchify
def _load_yaml(*pathes):
path = join(*pathes)
logger().info("load {}".format(path))
data = load_yaml_and_bunchify(path)
if data is None:
msg = "file {} is empty".format(path)
logger().warn(msg)
raise ConsistencyError(msg)
return data
def log(exceptions, rel_path):
for e in exceptions:
logger().warn("{}: {}".format(rel_path, e))
yield e
def parse_site(landing_zone_folder, rel_path):
""" Maksite sure that 'pictures' is always present
"""
path = join(landing_zone_folder, rel_path)
try:
site = _load_yaml(path)
except ConsistencyError as e:
yield e
return
if "pictures" not in site:
site.pictures = []
site_folder = dirname(path)
exceptions = check_site_yaml(site, rel_path)
exceptions += check_pictures(site, site_folder)
if exceptions:
yield from log(exceptions, rel_path)
return
# we transform our site Bunch so that it corresponds to the data resp. dbo objects
# attributes:
folder = dirname(abspath(path))
for picture in site.pictures:
path = join(folder, picture.path)
with open(path, "rb") as fh:
picture.data = fh.read()
if "date" in picture:
picture.date = parse_date(picture.date)
else:
picture.date = None
picture.filename = basename(picture.path)
del picture.path
site.pictures = [Picture(p) for p in site.pictures]
site.coord_x = site.coordinates.x
site.coord_y = site.coordinates.y
site.coord_z = site.coordinates.z
del site.coordinates
# now we can initialize our data object:
yield Site(site, rel_path)
def parse_parameters(landing_zone_folder, rel_path):
try:
parameters = _load_yaml(landing_zone_folder, rel_path)
except ConsistencyError as e:
yield e
return
logger().info("number of parameters found: {}".format(len(parameters)))
result = []
for parameter in parameters:
exceptions = check_parameters_yaml(parameter, rel_path)
if exceptions:
yield from log(exceptions, rel_path)
continue
# now we can initialize our data object:
result.append(Parameter(parameter))
yield Parameters(result, rel_path)
def parse_source_type(landing_zone_folder, rel_path):
try:
source_type = _load_yaml(landing_zone_folder, rel_path)
except ConsistencyError as e:
yield e
return
excs = check_source_type_yaml(source_type, rel_path)
if excs:
yield from log(excs, rel_path)
return
yield SourceType(source_type, rel_path)
@iter_to_list
def check_site_yaml(site, rel_path):
check_attributes = ["name", "coordinates", "coordinates.x", "coordinates.y", "coordinates.z",
"street", "city", "pictures"]
yield from check_yaml_fields_exist(site, rel_path, check_attributes)
@iter_to_list
def check_pictures(site, site_folder):
exceptions = []
for i, picture in enumerate(site.pictures):
if "path" not in picture:
yield ConsistencyError("picture no {} has no field 'path'".format(i + 1))
elif not picture.path:
yield ConsistencyError("entry 'path' of picture no {} is empty".format(i + 1))
else:
path = join(site_folder, picture.path)
if not exists(path):
yield ConsistencyError("picture {} does not exist".format(picture.path))
if "description" not in picture:
yield ConsistencyError("picture no {} has no field 'description'".format(i + 1))
elif not picture.description:
yield ConsistencyError("entry 'description' of picture no {} is empty".format(i + 1))
@iter_to_list
def check_parameters_yaml(parameters, rel_path):
check_attributes = ["name", "unit", "description"]
yield from check_yaml_fields_exist(parameters, rel_path, check_attributes)
if "name" in parameters and parameters.name is None:
yield ConsistencyError("field 'name' has no value")
if "unit" in parameters and parameters.unit is None:
yield ConsistencyError("field 'unit' has no value")
@iter_to_list
def check_source_type_yaml(source_type, rel_path):
for name in ("name", "description"):
if name not in source_type:
yield ConsistencyError("field {} is not set in {}".format(name, rel_path))
continue
if not source_type[name]:
yield ConsistencyError("field {} is empty in {}".format(name))
|