Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

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