Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

dumper.py 3.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 .data import Object, AsterEphemerides, MOON_PHASES
  24. class Dumper(ABC):
  25. def __init__(self, ephemeris: dict, date: datetime.date = datetime.date.today()):
  26. self.ephemeris = ephemeris
  27. self.date = date
  28. @abstractmethod
  29. def to_string(self):
  30. pass
  31. class JsonDumper(Dumper):
  32. def to_string(self):
  33. return json.dumps(self.ephemeris,
  34. default=self._json_default,
  35. indent=4)
  36. @staticmethod
  37. def _json_default(obj):
  38. # Fixes the "TypeError: Object of type int64 is not JSON serializable"
  39. # See https://stackoverflow.com/a/50577730
  40. if isinstance(obj, int64):
  41. return int(obj)
  42. if isinstance(obj, Time):
  43. return obj.utc_iso()
  44. if isinstance(obj, Object):
  45. obj = obj.__dict__
  46. obj.pop('skyfield_name')
  47. return obj
  48. if isinstance(obj, AsterEphemerides):
  49. return obj.__dict__
  50. raise TypeError('Object of type "%s" could not be integrated in the JSON' % str(type(obj)))
  51. class TextDumper(Dumper):
  52. def to_string(self):
  53. return '\n\n'.join(['Ephemerides of %s' % self.date.strftime('%A %B %d, %Y'),
  54. self.get_asters(self.ephemeris['details']),
  55. self.get_moon(self.ephemeris['moon_phase']),
  56. 'Note: All the hours are given in UTC.'])
  57. @staticmethod
  58. def get_asters(asters: [Object]) -> str:
  59. data = []
  60. for aster in asters:
  61. name = aster.name
  62. if aster.ephemerides.rise_time is not None:
  63. planet_rise = aster.ephemerides.rise_time.utc_strftime('%H:%M')
  64. else:
  65. planet_rise = '-'
  66. if aster.ephemerides.culmination_time is not None:
  67. planet_culmination = aster.ephemerides.culmination_time.utc_strftime('%H:%M')
  68. else:
  69. planet_culmination = '-'
  70. if aster.ephemerides.set_time is not None:
  71. planet_set = aster.ephemerides.set_time.utc_strftime('%H:%M')
  72. else:
  73. planet_set = '-'
  74. data.append([name, planet_rise, planet_culmination, planet_set])
  75. return tabulate(data, headers=['Planet', 'Rise time', 'Culmination time', 'Set time'], tablefmt='simple',
  76. stralign='center', colalign=('left',))
  77. @staticmethod
  78. def get_moon(moon_phase: str) -> str:
  79. return 'Moon phase: %s' % MOON_PHASES[moon_phase]