No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

167 líneas
6.3 KiB

  1. #!/usr/bin/env python3
  2. # Kosmorro - Compute The Next Ephemerides
  3. # Copyright (C) 2019 Jérôme Deuchnord <jerome@deuchnord.fr>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. from abc import ABC, abstractmethod
  18. import datetime
  19. import json
  20. from tabulate import tabulate
  21. from skyfield.timelib import Time
  22. from numpy import int64
  23. from termcolor import colored
  24. from .data import Object, AsterEphemerides, MoonPhase, Event
  25. from .i18n import _
  26. FULL_DATE_FORMAT = _('{day_of_week} {month} {day_number}, {year}').format(day_of_week='%A', month='%B',
  27. day_number='%d', year='%Y')
  28. TIME_FORMAT = _('{hours}:{minutes}').format(hours='%H', minutes='%M')
  29. class Dumper(ABC):
  30. def __init__(self, ephemeris: dict, events: [Event], date: datetime.date = datetime.date.today(),
  31. with_colors: bool = True):
  32. self.ephemeris = ephemeris
  33. self.events = events
  34. self.date = date
  35. self.with_colors = with_colors
  36. @abstractmethod
  37. def to_string(self):
  38. pass
  39. class JsonDumper(Dumper):
  40. def to_string(self):
  41. self.ephemeris['events'] = self.events
  42. self.ephemeris['ephemerides'] = self.ephemeris.pop('details')
  43. return json.dumps(self.ephemeris,
  44. default=self._json_default,
  45. indent=4)
  46. @staticmethod
  47. def _json_default(obj):
  48. # Fixes the "TypeError: Object of type int64 is not JSON serializable"
  49. # See https://stackoverflow.com/a/50577730
  50. if isinstance(obj, int64):
  51. return int(obj)
  52. if isinstance(obj, Time):
  53. return obj.utc_iso()
  54. if isinstance(obj, Object):
  55. obj = obj.__dict__
  56. obj.pop('skyfield_name')
  57. obj['object'] = obj.pop('name')
  58. obj['details'] = obj.pop('ephemerides')
  59. return obj
  60. if isinstance(obj, AsterEphemerides):
  61. return obj.__dict__
  62. if isinstance(obj, MoonPhase):
  63. moon_phase = obj.__dict__
  64. moon_phase['phase'] = moon_phase.pop('identifier')
  65. moon_phase['date'] = moon_phase.pop('time')
  66. return moon_phase
  67. if isinstance(obj, Event):
  68. event = obj.__dict__
  69. event['objects'] = [object.name for object in event['objects']]
  70. return event
  71. raise TypeError('Object of type "%s" could not be integrated in the JSON' % str(type(obj)))
  72. class TextDumper(Dumper):
  73. def to_string(self):
  74. text = [self.style(self.get_date(), 'h1')]
  75. if len(self.ephemeris['details']) > 0:
  76. text.append(self.get_asters(self.ephemeris['details']))
  77. text.append(self.get_moon(self.ephemeris['moon_phase']))
  78. if len(self.events) > 0:
  79. text.append('\n'.join([self.style(_('Expected events:'), 'h2'),
  80. self.get_events(self.events)]))
  81. text.append(self.style(_('Note: All the hours are given in UTC.'), 'em'))
  82. return '\n\n'.join(text)
  83. def style(self, text: str, tag: str) -> str:
  84. if not self.with_colors:
  85. return text
  86. styles = {
  87. 'h1': lambda t: colored(t, 'yellow', attrs=['bold']),
  88. 'h2': lambda t: colored(t, 'magenta', attrs=['bold']),
  89. 'th': lambda t: colored(t, 'white', attrs=['bold']),
  90. 'strong': lambda t: colored(t, attrs=['bold']),
  91. 'em': lambda t: colored(t, attrs=['dark'])
  92. }
  93. return styles[tag](text)
  94. def get_date(self) -> str:
  95. date = self.date.strftime(FULL_DATE_FORMAT)
  96. return ''.join([date[0].upper(), date[1:]])
  97. def get_asters(self, asters: [Object]) -> str:
  98. data = []
  99. for aster in asters:
  100. name = self.style(aster.name, 'th')
  101. if aster.ephemerides.rise_time is not None:
  102. planet_rise = aster.ephemerides.rise_time.utc_strftime(TIME_FORMAT)
  103. else:
  104. planet_rise = '-'
  105. if aster.ephemerides.culmination_time is not None:
  106. planet_culmination = aster.ephemerides.culmination_time.utc_strftime(TIME_FORMAT)
  107. else:
  108. planet_culmination = '-'
  109. if aster.ephemerides.set_time is not None:
  110. planet_set = aster.ephemerides.set_time.utc_strftime(TIME_FORMAT)
  111. else:
  112. planet_set = '-'
  113. data.append([name, planet_rise, planet_culmination, planet_set])
  114. return tabulate(data, headers=[self.style(_('Object'), 'th'),
  115. self.style(_('Rise time'), 'th'),
  116. self.style(_('Culmination time'), 'th'),
  117. self.style(_('Set time'), 'th')],
  118. tablefmt='simple', stralign='center', colalign=('left',))
  119. def get_events(self, events: [Event]) -> str:
  120. data = []
  121. for event in events:
  122. data.append([self.style(event.start_time.utc_strftime(TIME_FORMAT), 'th'),
  123. event.get_description()])
  124. return tabulate(data, tablefmt='plain', stralign='left')
  125. def get_moon(self, moon_phase: MoonPhase) -> str:
  126. current_moon_phase = ' '.join([self.style(_('Moon phase:'), 'strong'), moon_phase.get_phase()])
  127. new_moon_phase = _('{next_moon_phase} on {next_moon_phase_date} at {next_moon_phase_time}').format(
  128. next_moon_phase=moon_phase.get_next_phase(),
  129. next_moon_phase_date=moon_phase.next_phase_date.utc_strftime(FULL_DATE_FORMAT),
  130. next_moon_phase_time=moon_phase.next_phase_date.utc_strftime(TIME_FORMAT)
  131. )
  132. return '\n'.join([current_moon_phase, new_moon_phase])