Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

4 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 OSError as error:
  74. print(_('Could not save the output in "{path}": {error}').format(path=args.output,
  75. error=error.strerror))
  76. elif not output.is_file_output_needed():
  77. print(output)
  78. else:
  79. print(colored(_('Selected output format needs an output file (--output).'), color='red'))
  80. return 1
  81. return 0
  82. def get_information(compute_date: date, position: Position, timezone: int,
  83. output_format: str, colors: bool, show_graph: bool) -> dumper.Dumper:
  84. if position is not None:
  85. eph = ephemerides.get_ephemerides(date=compute_date, position=position, timezone=timezone)
  86. else:
  87. eph = None
  88. try:
  89. moon_phase = ephemerides.get_moon_phase(compute_date)
  90. except OutOfRangeDateError as error:
  91. moon_phase = None
  92. print(colored(_('Moon phase can only be displayed'
  93. ' between {min_date} and {max_date}').format(min_date=error.min_date,
  94. max_date=error.max_date), 'yellow'))
  95. events_list = events.search_events(compute_date, timezone)
  96. return get_dumpers()[output_format](ephemerides=eph, moon_phase=moon_phase, events=events_list,
  97. date=compute_date, timezone=timezone, with_colors=colors,
  98. show_graph=show_graph)
  99. def get_dumpers() -> {str: dumper.Dumper}:
  100. return {
  101. 'text': dumper.TextDumper,
  102. 'json': dumper.JsonDumper,
  103. 'pdf': dumper.PdfDumper,
  104. }
  105. def output_version() -> bool:
  106. python_version = '%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2])
  107. print('Kosmorro %s' % VERSION)
  108. print(_('Running on Python {python_version}').format(python_version=python_version))
  109. return True
  110. def clear_cache() -> bool:
  111. confirm = input(_("Do you really want to clear Kosmorro's cache? [yN] ")).upper()
  112. if re.match(locale.nl_langinfo(locale.YESEXPR), confirm) is not None:
  113. try:
  114. core.clear_cache()
  115. except FileNotFoundError:
  116. pass
  117. elif confirm != '' and re.match(locale.nl_langinfo(locale.NOEXPR), confirm) is None:
  118. print(_('Answer did not match expected options, cache not cleared.'))
  119. return False
  120. return True
  121. def get_args(output_formats: [str]):
  122. today = date.today()
  123. parser = argparse.ArgumentParser(description=_('Compute the ephemerides and the events for a given date,'
  124. ' at a given position on Earth.'),
  125. epilog=_('By default, only the events will be computed for today ({date}).\n'
  126. 'To compute also the ephemerides, latitude and longitude arguments'
  127. ' are needed.').format(date=today.strftime(dumper.FULL_DATE_FORMAT)))
  128. parser.add_argument('--version', '-v', dest='special_action', action='store_const', const=output_version,
  129. default=None, help=_('Show the program version'))
  130. parser.add_argument('--clear-cache', dest='special_action', action='store_const', const=clear_cache, default=None,
  131. help=_('Delete all the files Kosmorro stored in the cache.'))
  132. parser.add_argument('--format', '-f', type=str, default=output_formats[0], choices=output_formats,
  133. help=_('The format under which the information have to be output'))
  134. parser.add_argument('--latitude', '-lat', type=float, default=None,
  135. help=_("The observer's latitude on Earth. Can also be set in the KOSMORRO_LATITUDE environment "
  136. "variable."))
  137. parser.add_argument('--longitude', '-lon', type=float, default=None,
  138. help=_("The observer's longitude on Earth. Can also be set in the KOSMORRO_LONGITUDE "
  139. "environment variable."))
  140. parser.add_argument('--date', '-d', type=str, default=today.strftime('%Y-%m-%d'),
  141. help=_('The date for which the ephemerides must be computed (in the YYYY-MM-DD format), '
  142. 'or as an interval in the "[+-]YyMmDd" format (with Y, M, and D numbers). '
  143. 'Defaults to the current date ({default_date})').format(
  144. default_date=today.strftime('%Y-%m-%d')))
  145. parser.add_argument('--timezone', '-t', type=int, default=None,
  146. help=_('The timezone to display the hours in (e.g. 2 for UTC+2 or -3 for UTC-3). '
  147. 'Can also be set in the KOSMORRO_TIMEZONE environment variable.'))
  148. parser.add_argument('--no-colors', dest='colors', action='store_false',
  149. help=_('Disable the colors in the console.'))
  150. parser.add_argument('--output', '-o', type=str, default=None,
  151. help=_('A file to export the output to. If not given, the standard output is used. '
  152. 'This argument is needed for PDF format.'))
  153. parser.add_argument('--no-graph', dest='show_graph', action='store_false',
  154. help=_('Do not generate a graph to represent the rise and set times in the PDF format.'))
  155. return parser.parse_args()