Skip to content

Commit

Permalink
extract base structure and spatial structures to commons
Browse files Browse the repository at this point in the history
  • Loading branch information
o3bvv committed Jan 2, 2016
1 parent 8a70760 commit 59adfcb
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pip-log.txt
.coverage
.coveralls.yml
.tox
.cache
nosetests.xml
*.lprof
htmlcov
Expand Down
3 changes: 2 additions & 1 deletion il2fb/parsers/events/grammar/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import six

from il2fb.commons.organization import Belligerents
from il2fb.commons.spatial import Point2D

from ..constants import LOG_TIME_FORMAT, LOG_DATE_FORMAT, TOGGLE_VALUES
from ..structures import (
Point2D, HumanAircraft, HumanAircraftCrewMember, AIAircraftCrewMember,
HumanAircraft, HumanAircraftCrewMember, AIAircraftCrewMember,
MovingUnitMember,
)

Expand Down
57 changes: 4 additions & 53 deletions il2fb/parsers/events/structures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,9 @@
# -*- coding: utf-8 -*-

from il2fb.commons.structures import BaseStructure

class Base(object):
__slots__ = []

def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented

return all([
getattr(self, x) == getattr(other, x)
for x in self.__slots__
])

def __ne__(self, other):
return not (self == other)

def __hash__(self):
return hash(tuple(
getattr(self, x) for x in self.__slots__
))

def to_primitive(self, context=None):
fields = ((key, getattr(self, key)) for key in self.__slots__)
return {
key: self._to_primitive(value, context)
for key, value in fields
}

@staticmethod
def _to_primitive(instance, context):
from candv import SimpleConstant
from il2fb.commons.organization import Regiment

if isinstance(instance, (Base, SimpleConstant, Regiment)):
return instance.to_primitive(context)
elif hasattr(instance, 'isoformat'):
return instance.isoformat()
else:
return instance


class Point2D(Base):
__slots__ = ['x', 'y', ]

def __init__(self, x, y):
self.x = x
self.y = y

def __repr__(self):
return "<Point2D '{0};{1}'>".format(self.x, self.y)


class HumanAircraft(Base):
class HumanAircraft(BaseStructure):
__slots__ = ['callsign', 'aircraft', ]

def __init__(self, callsign, aircraft):
Expand All @@ -75,7 +26,7 @@ def __repr__(self):
.format(self.seat_number, self.callsign, self.aircraft))


class AIAircraftCrewMember(Base):
class AIAircraftCrewMember(BaseStructure):
__slots__ = ['aircraft', 'seat_number', ]

def __init__(self, aircraft, seat_number):
Expand All @@ -87,7 +38,7 @@ def __repr__(self):
self.seat_number)


class MovingUnitMember(Base):
class MovingUnitMember(BaseStructure):
__slots__ = ['moving_unit', 'index', ]

def __init__(self, moving_unit, index):
Expand Down
6 changes: 3 additions & 3 deletions il2fb/parsers/events/structures/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from il2fb.parsers.events.utils import translations

from . import Base
from il2fb.commons.structures import BaseStructure


__all__ = (
Expand Down Expand Up @@ -64,9 +64,9 @@
_ = translations.ugettext_lazy


class Event(Base):
class Event(BaseStructure):
"""
Base event structure.
BaseStructure event structure.
"""

def __init__(self, **kwargs):
Expand Down
6 changes: 4 additions & 2 deletions tests/grammar/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import datetime

from il2fb.commons.organization import Belligerents
from pyparsing import ParseException

from il2fb.commons.organization import Belligerents
from il2fb.commons.spatial import Point2D

from il2fb.parsers.events.constants import TARGET_END_STATES
from il2fb.parsers.events.grammar.helpers import (
aircraft, ai_aircraft_crew_member, belligerent, bridge, building, callsign,
Expand All @@ -15,7 +17,7 @@
)
from il2fb.parsers.events.grammar.primitives import space
from il2fb.parsers.events.structures import (
Point2D, HumanAircraft, HumanAircraftCrewMember, AIAircraftCrewMember,
HumanAircraft, HumanAircraftCrewMember, AIAircraftCrewMember,
MovingUnitMember,
)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import inspect

from il2fb.commons.organization import Belligerents
from il2fb.commons.spatial import Point2D

from il2fb.parsers.events.constants import TARGET_END_STATES
from il2fb.parsers.events.grammar import events as grammar
from il2fb.parsers.events.structures import (
Point2D, HumanAircraft, HumanAircraftCrewMember, AIAircraftCrewMember,
HumanAircraft, HumanAircraftCrewMember, AIAircraftCrewMember,
MovingUnitMember, events as structures,
)

Expand Down

0 comments on commit 59adfcb

Please sign in to comment.