A corporation manager and dashboard for EVE Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

176 lines
4.5 KiB

  1. # -*- coding: utf-8 -*-
  2. import gzip
  3. from threading import Lock
  4. import yaml
  5. __all__ = ["Universe"]
  6. class _SolarSystem:
  7. """Represents a solar system."""
  8. def __init__(self, universe, sid, data):
  9. self._universe = universe
  10. self._id = sid
  11. self._data = data
  12. @property
  13. def id(self):
  14. """The solar system's ID, as an integer."""
  15. return self._id
  16. @property
  17. def name(self):
  18. """The solar system's name, as a string."""
  19. return self._data["name"]
  20. @property
  21. def constellation(self):
  22. """The solar system's constellation, as a _Constellation object."""
  23. return self._universe.constellation(self._data["constellation"])
  24. @property
  25. def region(self):
  26. """The solar system's region, as a _Region object."""
  27. return self._universe.region(self._data["region"])
  28. @property
  29. def security(self):
  30. """The solar system's security status, as a float."""
  31. return self._data["security"]
  32. class _Constellation:
  33. """Represents a constellation."""
  34. def __init__(self, universe, cid, data):
  35. self._universe = universe
  36. self._id = cid
  37. self._data = data
  38. @property
  39. def id(self):
  40. """The constellation's ID, as an integer."""
  41. return self._id
  42. @property
  43. def name(self):
  44. """The constellation's name, as a string."""
  45. return self._data["name"]
  46. @property
  47. def region(self):
  48. """The constellation's region, as a _Region object."""
  49. return self._universe.region(self._data["region"])
  50. class _Region:
  51. """Represents a region."""
  52. def __init__(self, universe, rid, data):
  53. self._universe = universe
  54. self._id = rid
  55. self._data = data
  56. @property
  57. def id(self):
  58. """The region's ID, as an integer."""
  59. return self._id
  60. @property
  61. def name(self):
  62. """The region's name, as a string."""
  63. return self._data["name"]
  64. class _DummySolarSystem(_SolarSystem):
  65. """Represents an unknown or invalid solar system."""
  66. def __init__(self, universe):
  67. super().__init__(universe, -1, {
  68. "name": "Unknown",
  69. "constellation": -1,
  70. "region": -1,
  71. "security": 0.0
  72. })
  73. class _DummyConstellation(_Constellation):
  74. """Represents an unknown or invalid constellation."""
  75. def __init__(self, universe):
  76. super().__init__(universe, -1, {
  77. "name": "Unknown",
  78. "region": -1
  79. })
  80. class _DummyRegion(_Region):
  81. """Represents an unknown or invalid region."""
  82. def __init__(self, universe):
  83. super().__init__(universe, -1, {
  84. "name": "Unknown"
  85. })
  86. class Universe:
  87. """EVE API module for static universe data."""
  88. def __init__(self, datadir):
  89. self._dir = datadir
  90. self._lock = Lock()
  91. self._loaded = False
  92. self._systems = {}
  93. self._constellations = {}
  94. self._regions = {}
  95. def _load(self):
  96. """Load in universe data. This can be called multiple times safely."""
  97. if self._loaded:
  98. return
  99. with self._lock:
  100. if self._loaded:
  101. return
  102. filename = str(self._dir / "galaxy.yml.gz")
  103. with gzip.open(filename, "rb") as fp:
  104. data = yaml.load(fp, Loader=yaml.CLoader)
  105. self._systems = data["systems"]
  106. self._constellations = data["constellations"]
  107. self._regions = data["regions"]
  108. self._loaded = True
  109. def system(self, sid):
  110. """Return a _SolarSystem with the given ID.
  111. If the ID is invalid, return a dummy unknown object with ID -1.
  112. """
  113. self._load()
  114. if sid not in self._systems:
  115. return _DummySolarSystem(self)
  116. return _SolarSystem(self, sid, self._systems[sid])
  117. def constellation(self, cid):
  118. """Return a _Constellation with the given ID.
  119. If the ID is invalid, return a dummy unknown object with ID -1.
  120. """
  121. self._load()
  122. if cid not in self._constellations:
  123. return _DummyConstellation(self)
  124. return _Constellation(self, cid, self._constellations[cid])
  125. def region(self, rid):
  126. """Return a _Region with the given ID.
  127. If the ID is invalid, return a dummy unknown object with ID -1.
  128. """
  129. self._load()
  130. if rid not in self._regions:
  131. return _DummyRegion(self)
  132. return _Region(self, rid, self._regions[rid])