123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- # -*- coding: utf-8 -*-
- import re
- import pytz
- import yaml
- import yaml.representer
- import datetime
- yaml.Dumper.add_representer(unicode, yaml.representer.SafeRepresenter.represent_unicode)
- yaml.Loader.add_constructor(
- u'tag:yaml.org,2002:str',
- lambda loader, node: unicode(loader.construct_scalar(node)),
- )
- def yaml_construct_timestamp(loader, node):
- loaded_dt = loader.construct_yaml_timestamp(node)
- if type(loaded_dt) is datetime.datetime and loaded_dt.tzinfo is None:
- timezone_match = re.search(
- ur'\+(?P<h>\d{2}):(?P<m>\d{2})$',
- loader.construct_python_unicode(node),
- )
- if timezone_match:
- loaded_dt = loaded_dt.replace(tzinfo = pytz.utc)
- return loaded_dt
- yaml.Loader.add_constructor(u'tag:yaml.org,2002:timestamp', yaml_construct_timestamp)
- class _YamlInitConstructor(yaml.YAMLObject):
- @classmethod
- def from_yaml(cls, loader, node):
- return cls(**loader.construct_mapping(node, deep = True))
- # return cls(**{
- # k: unicode(v) if isinstance(v, str) else v
- # for (k, v) in loader.construct_mapping(node, deep = True).items()
- # })
- class Figure(_YamlInitConstructor):
- yaml_tag = u"!figure"
- def __init__(self, value, unit):
- self.value = value
- self.unit = unit
- def get_value(self):
- return self._value
- def set_value(self, value):
- self._value = value
- """ use property() instead of decorator to enable overriding """
- value = property(get_value, set_value)
- def get_unit(self):
- return self._unit
- def set_unit(self, unit):
- assert type(unit) is unicode
- self._unit = unit
- """ use property() instead of decorator to enable overriding """
- unit = property(get_unit, set_unit)
- def __eq__(self, other):
- return type(self) == type(other) and self.value == other.value and self.unit == other.unit
- def __ne__(self, other):
- return not (self == other)
- @classmethod
- def to_yaml(cls, dumper, figure):
- return dumper.represent_mapping(
- cls.yaml_tag,
- {'unit': figure.get_unit(), 'value': figure.get_value()},
- )
- class ScalarFigure(Figure):
- yaml_tag = u'!scalar'
- def get_value(self):
- return super(ScalarFigure, self).get_value()
- def set_value(self, value):
- assert type(value) is float
- super(ScalarFigure, self).set_value(value)
- """ use property() instead of decorator to enable overriding """
- value = property(get_value, set_value)
- @classmethod
- def from_yaml(cls, loader, node):
- attr = loader.construct_scalar(node).split(' ')
- return cls(
- value = float(attr[0]),
- unit = attr[1],
- )
- @classmethod
- def to_yaml(cls, dumper, s):
- return dumper.represent_scalar(
- cls.yaml_tag,
- '%s %s' % (repr(s.value), s.unit),
- )
- class Distance(ScalarFigure):
- yaml_tag = u'!distance'
- @property
- def metres(self):
- if self.unit == 'km':
- return self.value * 1000
- else:
- raise Exception()
- class Sum(ScalarFigure):
- yaml_tag = u'!sum'
- def __init__(self, value, currency):
- super(Sum, self).__init__(value, currency)
- @property
- def currency(self):
- return self.unit
- def get_unit(self):
- return super(Sum, self).get_unit()
- def set_unit(self, currency):
- if currency == u'€':
- currency = u'EUR'
- if currency == u'US$':
- currency = u'USD'
- assert type(currency) is unicode
- assert currency in [u'EUR', u'USD']
- super(Sum, self).set_unit(currency)
- """ use property() instead of decorator to enable overriding """
- unit = property(get_unit, set_unit)
- @classmethod
- def from_yaml(cls, loader, node):
- attr = loader.construct_scalar(node).split(' ')
- return cls(
- value = float(attr[0]),
- currency = attr[1],
- )
- class Discount(yaml.YAMLObject):
- yaml_tag = u'!discount'
- def __init__(
- self,
- name = None,
- amount = None,
- ):
- assert type(name) is unicode
- self.name = name
- assert type(amount) is Sum
- assert amount.value >= 0
- self.amount = amount
- def dict_repr(self):
- return {
- 'name': self.name,
- 'value': self.amount.value,
- 'value_currency': self.amount.currency,
- }
- @staticmethod
- def from_dict(attr):
- return Discount(
- name = attr['name'],
- amount = Sum(attr['value'], attr['value_currency']),
- )
- def __eq__(self, other):
- return (type(self) == type(other)
- and vars(self) == vars(other))
- def __ne__(self, other):
- return not (self == other)
- class Order(_YamlInitConstructor):
- yaml_tag = u'!order'
- def __init__(self, platform, order_id, order_date,
- customer_id = None,
- items = None,
- discounts = None,
- ):
- assert type(platform) is unicode
- self.platform = platform
- if type(order_id) in [int]:
- order_id = unicode(order_id)
- assert type(order_id) is unicode
- self.order_id = order_id
- if type(order_date) is datetime.datetime:
- order_date = order_date.date()
- assert type(order_date) is datetime.date
- self.order_date = order_date
- assert customer_id is None or type(customer_id) is unicode
- self.customer_id = customer_id
- if items is None:
- items = []
- assert type(items) is list
- self.items = items
- if discounts is None:
- discounts = []
- assert type(discounts) is list
- self.discounts = discounts
- def dict_repr(self):
- return {k: v for (k, v) in {
- 'articles': self.items,
- 'customer_id': self.customer_id,
- 'discounts': self.discounts,
- 'order_date': self.order_date.strftime('%Y-%m-%d'),
- 'order_id': self.order_id,
- 'platform': self.platform,
- }.items() if v is not None}
- @staticmethod
- def from_dict(attr):
- order = Order(
- platform = attr['platform'],
- order_id = attr['order_id'],
- order_date = datetime.datetime.strptime(attr['order_date'], '%Y-%m-%d'),
- )
- if 'customer_id' in attr:
- order.customer_id = attr['customer_id']
- for item in attr['articles']:
- if type(item) is dict:
- item = Item.from_dict(item)
- assert isinstance(item, Item)
- order.items.append(item)
- for discount in attr['discounts']:
- if type(discount) is dict:
- discount = Discount.from_dict(discount)
- assert isinstance(discount, Discount)
- order.discounts.append(discount)
- return order
- def __eq__(self, other):
- return (type(self) == type(other)
- and vars(self) == vars(other))
- def __ne__(self, other):
- return not (self == other)
- class Item(_YamlInitConstructor):
- yaml_tag = u'!item'
- def __init__(
- self,
- name = None,
- price_brutto = None,
- ):
- assert type(name) is unicode
- self.name = name
- assert type(price_brutto) is Sum
- self.price_brutto = price_brutto
- def dict_repr(self):
- return {
- 'name': self.name,
- 'price_brutto': self.price_brutto.value,
- 'price_brutto_currency': self.price_brutto.currency,
- }
- @staticmethod
- def from_dict(attr):
- return Item(
- name = attr['name'],
- price_brutto = Sum(attr['price_brutto'], attr['price_brutto_currency']),
- )
- def __eq__(self, other):
- return (type(self) == type(other)
- and vars(self) == vars(other))
- def __ne__(self, other):
- return not (self == other)
- class Article(Item):
- yaml_tag = u'!article'
- def __init__(
- self,
- authors = None,
- color = None,
- delivery_date = None,
- depth = None,
- features = None,
- height = None,
- maximum_load = None,
- option = None,
- product_id = None,
- quantity = None,
- reseller = None,
- shipper = None,
- size = None,
- state = None,
- width = None,
- **kwargs
- ):
- super(Article, self).__init__(**kwargs)
- assert type(quantity) is int
- self.quantity = quantity
- if authors is not None:
- assert type(authors) is list
- self.authors = authors
- if state is not None:
- assert type(state) is unicode
- self.state = state
- if reseller is not None:
- assert type(reseller) is unicode
- self.reseller = reseller
- if shipper is not None:
- assert type(shipper) is unicode
- self.shipper = shipper
- if product_id is not None:
- if type(product_id) in [int]:
- product_id = unicode(product_id)
- assert type(product_id) is unicode
- self.product_id = product_id
- if option is not None:
- assert type(option) is unicode
- self.option = option
- if color is not None:
- assert type(color) is unicode
- self.color = color
- if size is not None:
- assert type(size) is unicode
- self.size = size
- if width is not None:
- assert type(width) is ScalarFigure
- self.width = width
- if depth is not None:
- assert type(depth) is ScalarFigure
- self.depth = depth
- if height is not None:
- assert type(height) is ScalarFigure
- self.height = height
- if maximum_load is not None:
- assert type(maximum_load) is ScalarFigure
- self.maximum_load = maximum_load
- if features is not None:
- assert type(features) is unicode
- self.features = features
- assert delivery_date is None or type(delivery_date) is datetime.date
- self.delivery_date = delivery_date
- class Transportation(Item):
- yaml_tag = u'!transportation'
- def __init__(
- self,
- departure_point = None,
- destination_point = None,
- distance = None,
- passenger = None,
- route_map = None,
- valid_from = None,
- **kwargs
- ):
- super(Transportation, self).__init__(**kwargs)
- if departure_point is not None:
- assert type(departure_point) is unicode
- self.departure_point = departure_point
- if destination_point is not None:
- assert type(destination_point) is unicode
- self.destination_point = destination_point
- if distance is not None:
- assert type(distance) is Distance
- self.distance = distance
- if route_map is not None:
- assert type(route_map) is str
- self.route_map = route_map
- if passenger is not None:
- assert type(passenger) is Person
- self.passenger = passenger
- if valid_from is not None:
- assert type(valid_from) is datetime.datetime
- assert not valid_from.tzinfo is None
- self.valid_from = valid_from
- def dict_repr(self):
- attr = super(Transportation, self).dict_repr()
- attr.update({
- 'departure_point': self.departure_point,
- 'destination_point': self.destination_point,
- 'distance_metres': self.distance.metres if self.distance else None,
- 'route_map': self.route_map,
- })
- return attr
- class TaxiRide(Transportation):
- yaml_tag = u'!taxi-ride'
- def __init__(
- self,
- arrival_time = None,
- departure_time = None,
- driver = None,
- name = None,
- **kwargs
- ):
- if name is None:
- name = u'Taxi Ride'
- super(TaxiRide, self).__init__(name = name, **kwargs)
- assert type(driver) is unicode
- self.driver = driver
- assert arrival_time is None or type(arrival_time) is datetime.datetime
- self.arrival_time = arrival_time
- assert departure_time is None or type(departure_time) is datetime.datetime
- self.departure_time = departure_time
- def dict_repr(self):
- attr = super(TaxiRide, self).dict_repr()
- attr.update({
- 'arrival_time': self.arrival_time.strftime('%Y-%m-%d %H:%M') if self.arrival_time else None,
- 'departure_time': self.departure_time.strftime('%Y-%m-%d %H:%M') if self.departure_time else None,
- 'driver': self.driver,
- })
- return attr
- class OrderRegistry(yaml.YAMLObject):
- yaml_tag = u'!order-registry'
- def __init__(self):
- self.registry = {}
- def register(self, order):
- assert isinstance(order, Order)
- if not order.platform in self.registry:
- self.registry[order.platform] = {}
- self.registry[order.platform][order.order_id] = order
- @classmethod
- def to_yaml(cls, dumper, self):
- return dumper.represent_mapping(cls.yaml_tag, self.registry)
- @classmethod
- def from_yaml(cls, loader, node):
- self = cls()
- self.registry = loader.construct_mapping(node)
- return self
- def __eq__(self, other):
- return type(self) == type(other) and vars(self) == vars(other)
- def __ne__(self, other):
- return not self == other
- class Person(_YamlInitConstructor):
- yaml_tag = u'!person'
- def __init__(self, first_name = None, last_name = None):
- self.first_name = first_name
- self.last_name = last_name
- @property
- def first_name(self):
- return self._first_name
- @first_name.setter
- def first_name(self, first_name):
- assert first_name is None or type(first_name) is unicode
- self._first_name = first_name
- @property
- def last_name(self):
- return self._last_name
- @last_name.setter
- def last_name(self, last_name):
- assert last_name is None or type(last_name) is unicode
- self._last_name = last_name
- @classmethod
- def to_yaml(cls, dumper, person):
- return dumper.represent_mapping(cls.yaml_tag, {
- 'first_name': person.first_name,
- 'last_name': person.last_name,
- })
- def __eq__(self, other):
- return type(self) == type(other) and vars(self) == vars(other)
- def __ne__(self, other):
- return not self == other
- def __repr__(self):
- return self.__class__.__name__ + '(%s)' % ', '.join([
- '%s=%r' % (k, v) for k, v in vars(self).items()
- ])
|