source: src/mlx/rpccommon.py@ 742:464227881300

Last change on this file since 742:464227881300 was 742:464227881300, checked in by István Váradi <ivaradi@…>, 8 years ago

Extracted some common classes (re #283).

File size: 3.4 KB
RevLine 
[742]1# Some common objects for RPC communication
2
3#------------------------------------------------------------------------------
4
5import const
6
7#------------------------------------------------------------------------------
8
9class Plane(object):
10 """Information about an airplane in the fleet."""
11 @staticmethod
12 def str2status(letter):
13 """Convert the given letter into a plane status."""
14 return const.PLANE_HOME if letter=="H" else \
15 const.PLANE_AWAY if letter=="A" else \
16 const.PLANE_PARKING if letter=="P" else \
17 const.PLANE_UNKNOWN
18
19 @staticmethod
20 def status2str(status):
21 """Convert the given status into the corresponding letter code."""
22 return "H" if status==const.PLANE_HOME else \
23 "A" if status==const.PLANE_AWAY else \
24 "P" if status==const.PLANE_PARKING else ""
25
26 def _setStatus(self, letter):
27 """Set the status from the given letter."""
28 self.status = Plane.str2status(letter)
29
30 def __repr__(self):
31 """Get the representation of the plane object."""
32 s = "<Plane: %s %s" % (self.tailNumber,
33 "home" if self.status==const.PLANE_HOME else \
34 "away" if self.status==const.PLANE_AWAY else \
35 "parking" if self.status==const.PLANE_PARKING \
36 else "unknown")
37 if self.gateNumber is not None:
38 s += " (gate " + self.gateNumber + ")"
39 s += ">"
40 return s
41
42#------------------------------------------------------------------------------
43
44class Fleet(object):
45 """Information about the whole fleet."""
46 def __init__(self):
47 """Construct the fleet information by reading the given file object."""
48 self._planes = {}
49
50 def isGateConflicting(self, plane):
51 """Check if the gate of the given plane conflicts with another plane's
52 position."""
53 for p in self._planes.itervalues():
54 if p.tailNumber!=plane.tailNumber and \
55 p.status==const.PLANE_HOME and \
56 p.gateNumber==plane.gateNumber:
57 return True
58
59 return False
60
61 def getOccupiedGateNumbers(self):
62 """Get a set containing the numbers of the gates occupied by planes."""
63 gateNumbers = set()
64 for p in self._planes.itervalues():
65 if p.status==const.PLANE_HOME and p.gateNumber:
66 gateNumbers.add(p.gateNumber)
67 return gateNumbers
68
69 def updatePlane(self, tailNumber, status, gateNumber = None):
70 """Update the status of the given plane."""
71 if tailNumber in self._planes:
72 plane = self._planes[tailNumber]
73 plane.status = status
74 plane.gateNumber = gateNumber
75
76 def _addPlane(self, plane):
77 """Add the given plane to the fleet."""
78 self._planes[plane.tailNumber] = plane
79
80 def __iter__(self):
81 """Get an iterator over the planes."""
82 for plane in self._planes.itervalues():
83 yield plane
84
85 def __getitem__(self, tailNumber):
86 """Get the plane with the given tail number.
87
88 If the plane is not in the fleet, None is returned."""
89 return self._planes[tailNumber] if tailNumber in self._planes else None
90
91 def __repr__(self):
92 """Get the representation of the fleet object."""
93 return self._planes.__repr__()
Note: See TracBrowser for help on using the repository browser.