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.
 
 
 
 

196 rivejä
8.5 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. import argparse
  18. import locale
  19. import re
  20. import sys
  21. from datetime import date
  22. from termcolor import colored
  23. from . import dumper
  24. from . import core
  25. from . import events
  26. from .data import Position, EARTH
  27. from .exceptions import UnavailableFeatureError, OutOfRangeDateError
  28. from .i18n import _
  29. from . import ephemerides
  30. from .version import VERSION
  31. def main():
  32. environment = core.get_env()
  33. output_formats = get_dumpers()
  34. args = get_args(list(output_formats.keys()))
  35. output_format = args.format
  36. if args.special_action is not None:
  37. return 0 if args.special_action() else 1
  38. try:
  39. compute_date = core.get_date(args.date)
  40. except ValueError as error:
  41. print(colored(error.args[0], color='red', attrs=['bold']))
  42. return -1
  43. position = None
  44. if args.latitude is not None or args.longitude is not None:
  45. position = Position(args.latitude, args.longitude, EARTH)
  46. elif environment.latitude is not None and environment.longitude is not None:
  47. position = Position(float(environment.latitude), float(environment.longitude), EARTH)
  48. if output_format == 'pdf':
  49. print(_('Save the planet and paper!\n'
  50. 'Consider printing you PDF document only if really necessary, and use the other side of the sheet.'))
  51. if position is None:
  52. print()
  53. print(colored(_("PDF output will not contain the ephemerides, because you didn't provide the observation "
  54. "coordinate."), 'yellow'))
  55. timezone = args.timezone
  56. if timezone is None and environment.timezone is not None:
  57. timezone = int(environment.timezone)
  58. elif timezone is None:
  59. timezone = 0
  60. try:
  61. output = get_information(compute_date, position, timezone, output_format,
  62. args.colors, args.show_graph)
  63. except UnavailableFeatureError as error:
  64. print(colored(error.msg, 'red'))
  65. return 2
  66. except OutOfRangeDateError as error:
  67. print(colored(error.msg, 'red'))
  68. return 1
  69. if args.output is not None:
  70. try:
  71. with open(args.output, 'wb') as output_file:
  72. output_file.write(output.to_string())
  73. except UnavailableFeatureError as error:
  74. print(colored(error.msg, 'red'))
  75. return 2
  76. except OSError as error:
  77. print(colored(_('Could not save the output in "{path}": {error}').format(path=args.output,
  78. error=error.strerror),
  79. 'red'))
  80. return 3
  81. elif not output.is_file_output_needed():
  82. print(output)
  83. else:
  84. print(colored(_('Selected output format needs an output file (--output).'), color='red'))
  85. return 1
  86. return 0
  87. def get_information(compute_date: date, position: Position, timezone: int,
  88. output_format: str, colors: bool, show_graph: bool) -> dumper.Dumper:
  89. if position is not None:
  90. eph = ephemerides.get_ephemerides(date=compute_date, position=position, timezone=timezone)
  91. else:
  92. eph = None
  93. try:
  94. moon_phase = ephemerides.get_moon_phase(compute_date)
  95. except OutOfRangeDateError as error:
  96. moon_phase = None
  97. print(colored(_('Moon phase can only be displayed'
  98. ' between {min_date} and {max_date}').format(min_date=error.min_date,
  99. max_date=error.max_date), 'yellow'))
  100. events_list = events.search_events(compute_date, timezone)
  101. return get_dumpers()[output_format](ephemerides=eph, moon_phase=moon_phase, events=events_list,
  102. date=compute_date, timezone=timezone, with_colors=colors,
  103. show_graph=show_graph)
  104. def get_dumpers() -> {str: dumper.Dumper}:
  105. return {
  106. 'text': dumper.TextDumper,
  107. 'json': dumper.JsonDumper,
  108. 'pdf': dumper.PdfDumper,
  109. }
  110. def output_version() -> bool:
  111. python_version = '%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2])
  112. print('Kosmorro %s' % VERSION)
  113. print(_('Running on Python {python_version}').format(python_version=python_version))
  114. return True
  115. def clear_cache() -> bool:
  116. confirm = input(_("Do you really want to clear Kosmorro's cache? [yN] ")).upper()
  117. if re.match(locale.nl_langinfo(locale.YESEXPR), confirm) is not None:
  118. try:
  119. core.clear_cache()
  120. except FileNotFoundError:
  121. pass
  122. elif confirm != '' and re.match(locale.nl_langinfo(locale.NOEXPR), confirm) is None:
  123. print(_('Answer did not match expected options, cache not cleared.'))
  124. return False
  125. return True
  126. def get_args(output_formats: [str]):
  127. today = date.today()
  128. parser = argparse.ArgumentParser(description=_('Compute the ephemerides and the events for a given date,'
  129. ' at a given position on Earth.'),
  130. epilog=_('By default, only the events will be computed for today ({date}).\n'
  131. 'To compute also the ephemerides, latitude and longitude arguments'
  132. ' are needed.').format(date=today.strftime(dumper.FULL_DATE_FORMAT)))
  133. parser.add_argument('--version', '-v', dest='special_action', action='store_const', const=output_version,
  134. default=None, help=_('Show the program version'))
  135. parser.add_argument('--clear-cache', dest='special_action', action='store_const', const=clear_cache, default=None,
  136. help=_('Delete all the files Kosmorro stored in the cache.'))
  137. parser.add_argument('--format', '-f', type=str, default=output_formats[0], choices=output_formats,
  138. help=_('The format under which the information have to be output'))
  139. parser.add_argument('--latitude', '-lat', type=float, default=None,
  140. help=_("The observer's latitude on Earth. Can also be set in the KOSMORRO_LATITUDE environment "
  141. "variable."))
  142. parser.add_argument('--longitude', '-lon', type=float, default=None,
  143. help=_("The observer's longitude on Earth. Can also be set in the KOSMORRO_LONGITUDE "
  144. "environment variable."))
  145. parser.add_argument('--date', '-d', type=str, default=today.strftime('%Y-%m-%d'),
  146. help=_('The date for which the ephemerides must be computed (in the YYYY-MM-DD format), '
  147. 'or as an interval in the "[+-]YyMmDd" format (with Y, M, and D numbers). '
  148. 'Defaults to the current date ({default_date})').format(
  149. default_date=today.strftime('%Y-%m-%d')))
  150. parser.add_argument('--timezone', '-t', type=int, default=None,
  151. help=_('The timezone to display the hours in (e.g. 2 for UTC+2 or -3 for UTC-3). '
  152. 'Can also be set in the KOSMORRO_TIMEZONE environment variable.'))
  153. parser.add_argument('--no-colors', dest='colors', action='store_false',
  154. help=_('Disable the colors in the console.'))
  155. parser.add_argument('--output', '-o', type=str, default=None,
  156. help=_('A file to export the output to. If not given, the standard output is used. '
  157. 'This argument is needed for PDF format.'))
  158. parser.add_argument('--no-graph', dest='show_graph', action='store_false',
  159. help=_('Do not generate a graph to represent the rise and set times in the PDF format.'))
  160. return parser.parse_args()