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.
 
 
 
 

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