Não pode escolher mais do que 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.

main.py 5.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 kosmorrolib.version import VERSION
  23. from kosmorrolib import dumper
  24. from kosmorrolib import core
  25. from kosmorrolib import events
  26. from kosmorrolib.i18n import _
  27. from .ephemerides import EphemeridesComputer, Position
  28. def main():
  29. output_formats = get_dumpers()
  30. args = get_args(list(output_formats.keys()))
  31. if args.special_action is not None:
  32. return 0 if args.special_action() else 1
  33. year = args.year
  34. month = args.month
  35. day = args.day
  36. compute_date = date(year, month, day)
  37. if day is not None and month is None:
  38. month = date.today().month
  39. if args.latitude is None or args.longitude is None:
  40. position = None
  41. else:
  42. position = Position(args.latitude, args.longitude)
  43. ephemeris = EphemeridesComputer(position)
  44. ephemerides = ephemeris.compute_ephemerides(year, month, day)
  45. events_list = events.search_events(compute_date)
  46. dump = output_formats[args.format](ephemerides, events_list, compute_date, args.colors)
  47. print(dump.to_string())
  48. return 0
  49. def get_dumpers() -> {str: dumper.Dumper}:
  50. return {
  51. 'text': dumper.TextDumper,
  52. 'json': dumper.JsonDumper
  53. }
  54. def output_version() -> bool:
  55. python_version = '%d.%d.%d' % (sys.version_info[0], sys.version_info[1], sys.version_info[2])
  56. print('Kosmorro %s' % VERSION)
  57. print(_('Running on Python {python_version}').format(python_version=python_version))
  58. return True
  59. def clear_cache() -> bool:
  60. confirm = input(_("Do you really want to clear Kosmorro's cache? [yN] ")).upper()
  61. if re.match(locale.nl_langinfo(locale.YESEXPR), confirm) is not None:
  62. try:
  63. core.clear_cache()
  64. except FileNotFoundError:
  65. pass
  66. elif confirm != '' and re.match(locale.nl_langinfo(locale.NOEXPR), confirm) is None:
  67. print(_('Answer did not match expected options, cache not cleared.'))
  68. return False
  69. return True
  70. def get_args(output_formats: [str]):
  71. today = date.today()
  72. parser = argparse.ArgumentParser(description=_('Compute the ephemerides and the events for a given date,'
  73. ' at a given position on Earth.'),
  74. epilog=_('By default, only the events will be computed for today ({date}).\n'
  75. 'To compute also the ephemerides, latitude and longitude arguments'
  76. ' are needed.').format(date=today.strftime(dumper.FULL_DATE_FORMAT)))
  77. parser.add_argument('--version', '-v', dest='special_action', action='store_const', const=output_version,
  78. default=None, help=_('Show the program version'))
  79. parser.add_argument('--clear-cache', dest='special_action', action='store_const', const=clear_cache, default=None,
  80. help=_('Delete all the files Kosmorro stored in the cache.'))
  81. parser.add_argument('--format', '-f', type=str, default=output_formats[0], choices=output_formats,
  82. help=_('The format under which the information have to be output'))
  83. parser.add_argument('--latitude', '-lat', type=float, default=None,
  84. help=_("The observer's latitude on Earth"))
  85. parser.add_argument('--longitude', '-lon', type=float, default=None,
  86. help=_("The observer's longitude on Earth"))
  87. parser.add_argument('--day', '-d', type=int, default=today.day,
  88. help=_('A number between 1 and 28, 29, 30 or 31 (depending on the month). The day you want to '
  89. ' compute the ephemerides for. Defaults to {default_day} (the current day).').format(
  90. default_day=today.day))
  91. parser.add_argument('--month', '-m', type=int, default=today.month,
  92. help=_('A number between 1 and 12. The month you want to compute the ephemerides for.'
  93. ' Defaults to {default_month} (the current month).').format(default_month=today.month))
  94. parser.add_argument('--year', '-y', type=int, default=today.year,
  95. help=_('The year you want to compute the ephemerides for.'
  96. ' Defaults to {default_year} (the current year).').format(default_year=today.year))
  97. parser.add_argument('--no-colors', dest='colors', action='store_false',
  98. help=_('Disable the colors in the console.'))
  99. return parser.parse_args()