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.
 
 
 
 

177 lines
7.6 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 kosmorrolib.version import VERSION
  24. from kosmorrolib import dumper
  25. from kosmorrolib import core
  26. from kosmorrolib import events
  27. from kosmorrolib.i18n import _
  28. from .ephemerides import EphemeridesComputer, Position
  29. from .exceptions import UnavailableFeatureError
  30. def main():
  31. environment = core.get_env()
  32. output_formats = get_dumpers()
  33. args = get_args(list(output_formats.keys()))
  34. if args.special_action is not None:
  35. return 0 if args.special_action() else 1
  36. try:
  37. compute_date = get_date(args.date)
  38. except ValueError as error:
  39. print(colored(error.args[0], color='red', attrs=['bold']))
  40. return -1
  41. position = None
  42. if args.latitude is not None or args.longitude is not None:
  43. position = Position(args.latitude, args.longitude)
  44. elif environment.latitude is not None and environment.longitude is not None:
  45. position = Position(float(environment.latitude), float(environment.longitude))
  46. if args.format == 'pdf':
  47. print(_('Save the planet and paper!\n'
  48. 'Consider printing you PDF document only if really necessary, and use the other side of the sheet.'))
  49. if position is None:
  50. print()
  51. print(colored(_("PDF output will not contain the ephemerides, because you didn't provide the observation "
  52. "coordinate."), 'yellow'))
  53. try:
  54. ephemeris = EphemeridesComputer(position)
  55. ephemerides = ephemeris.compute_ephemerides(compute_date)
  56. events_list = events.search_events(compute_date)
  57. timezone = args.timezone
  58. if timezone is None and environment.timezone is not None:
  59. timezone = int(environment.timezone)
  60. elif timezone is None:
  61. timezone = 0
  62. selected_dumper = output_formats[args.format](ephemerides, events_list,
  63. date=compute_date, timezone=timezone,
  64. with_colors=args.colors)
  65. output = selected_dumper.to_string()
  66. except UnavailableFeatureError as error:
  67. print(colored(error.msg, 'red'))
  68. return 2
  69. if args.output is not None:
  70. try:
  71. with open(args.output, 'wb') as output_file:
  72. output_file.write(output)
  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 selected_dumper.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_date(yyyymmdd: str) -> date:
  83. if not re.match(r'^\d{4}-\d{2}-\d{2}$', yyyymmdd):
  84. raise ValueError(_('The date {date} does not match the required YYYY-MM-DD format.').format(date=yyyymmdd))
  85. try:
  86. return date.fromisoformat(yyyymmdd)
  87. except ValueError as error:
  88. raise ValueError(_('The date {date} is not valid: {error}').format(date=yyyymmdd, error=error.args[0]))
  89. def get_dumpers() -> {str: dumper.Dumper}:
  90. return {
  91. 'text': dumper.TextDumper,
  92. 'json': dumper.JsonDumper,
  93. 'pdf': dumper.PdfDumper
  94. }
  95. def output_version() -> bool:
  96. python_version = '%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2])
  97. print('Kosmorro %s' % VERSION)
  98. print(_('Running on Python {python_version}').format(python_version=python_version))
  99. return True
  100. def clear_cache() -> bool:
  101. confirm = input(_("Do you really want to clear Kosmorro's cache? [yN] ")).upper()
  102. if re.match(locale.nl_langinfo(locale.YESEXPR), confirm) is not None:
  103. try:
  104. core.clear_cache()
  105. except FileNotFoundError:
  106. pass
  107. elif confirm != '' and re.match(locale.nl_langinfo(locale.NOEXPR), confirm) is None:
  108. print(_('Answer did not match expected options, cache not cleared.'))
  109. return False
  110. return True
  111. def get_args(output_formats: [str]):
  112. today = date.today()
  113. parser = argparse.ArgumentParser(description=_('Compute the ephemerides and the events for a given date,'
  114. ' at a given position on Earth.'),
  115. epilog=_('By default, only the events will be computed for today ({date}).\n'
  116. 'To compute also the ephemerides, latitude and longitude arguments'
  117. ' are needed.').format(date=today.strftime(dumper.FULL_DATE_FORMAT)))
  118. parser.add_argument('--version', '-v', dest='special_action', action='store_const', const=output_version,
  119. default=None, help=_('Show the program version'))
  120. parser.add_argument('--clear-cache', dest='special_action', action='store_const', const=clear_cache, default=None,
  121. help=_('Delete all the files Kosmorro stored in the cache.'))
  122. parser.add_argument('--format', '-f', type=str, default=output_formats[0], choices=output_formats,
  123. help=_('The format under which the information have to be output'))
  124. parser.add_argument('--latitude', '-lat', type=float, default=None,
  125. help=_("The observer's latitude on Earth. Can also be set in the KOSMORRO_LATITUDE environment "
  126. "variable."))
  127. parser.add_argument('--longitude', '-lon', type=float, default=None,
  128. help=_("The observer's longitude on Earth. Can also be set in the KOSMORRO_LONGITUDE "
  129. "environment variable."))
  130. parser.add_argument('--date', '-d', type=str, default=today.strftime('%Y-%m-%d'),
  131. help=_('The date for which the ephemerides must be computed (in the YYYY-MM-DD format). '
  132. 'Defaults to the current date ({default_date})').format(
  133. default_date=today.strftime('%Y-%m-%d')))
  134. parser.add_argument('--timezone', '-t', type=int, default=None,
  135. help=_('The timezone to display the hours in (e.g. 2 for UTC+2 or -3 for UTC-3). '
  136. 'Can also be set in the KOSMORRO_TIMEZONE environment variable.'))
  137. parser.add_argument('--no-colors', dest='colors', action='store_false',
  138. help=_('Disable the colors in the console.'))
  139. parser.add_argument('--output', '-o', type=str, default=None,
  140. help=_('A file to export the output to. If not given, the standard output is used. '
  141. 'This argument is needed for PDF format.'))
  142. return parser.parse_args()