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.
 
 
 
 

338 lines
9.9 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 kosmorrolib import Position, get_ephemerides, get_events, get_moon_phase
  22. from kosmorrolib.__version__ import __version__ as kosmorrolib_version
  23. from kosmorrolib.exceptions import OutOfRangeDateError
  24. from datetime import date
  25. from termcolor import colored
  26. from . import dumper, environment, debug
  27. from .date import parse_date
  28. from .__version__ import __version__ as kosmorro_version
  29. from .exceptions import UnavailableFeatureError, OutOfRangeDateError as DateRangeError
  30. from _kosmorro.i18n.utils import _, SHORT_DATE_FORMAT
  31. def main():
  32. env_vars = environment.get_env_vars()
  33. output_formats = get_dumpers()
  34. args = get_args(list(output_formats.keys()))
  35. debug.show_debug_messages = args.show_debug_messages
  36. output_format = args.format
  37. if args.special_action is not None:
  38. return 0 if args.special_action() else 1
  39. try:
  40. compute_date = parse_date(args.date)
  41. except ValueError as error:
  42. print(colored(error.args[0], color="red", attrs=["bold"]))
  43. return -1
  44. position = None
  45. if args.latitude is not None or args.longitude is not None:
  46. position = Position(args.latitude, args.longitude)
  47. elif env_vars.latitude is not None and env_vars.longitude is not None:
  48. position = Position(float(env_vars.latitude), float(env_vars.longitude))
  49. if output_format == "pdf":
  50. print(
  51. _(
  52. "Save the planet and paper!\n"
  53. "Consider printing your PDF document only if really necessary, and use the other side of the sheet."
  54. )
  55. )
  56. if position is None:
  57. print()
  58. print(
  59. colored(
  60. _(
  61. "PDF output will not contain the ephemerides, because you didn't provide the observation "
  62. "coordinates."
  63. ),
  64. "yellow",
  65. )
  66. )
  67. timezone = args.timezone
  68. if timezone is None and env_vars.timezone is not None:
  69. timezone = int(env_vars.timezone)
  70. elif timezone is None:
  71. timezone = 0
  72. try:
  73. output = get_information(
  74. compute_date,
  75. position,
  76. timezone,
  77. output_format,
  78. args.colors,
  79. args.show_graph,
  80. )
  81. except UnavailableFeatureError as error:
  82. print(colored(error.msg, "red"))
  83. debug.debug_print(error)
  84. return 2
  85. except DateRangeError as error:
  86. print(colored(error.msg, "red"))
  87. debug.debug_print(error)
  88. return 1
  89. if args.output is not None:
  90. try:
  91. pdf_content = output.to_string()
  92. with open(args.output, "wb") as output_file:
  93. output_file.write(pdf_content)
  94. except UnavailableFeatureError as error:
  95. print(colored(error.msg, "red"))
  96. debug.debug_print(error)
  97. return 2
  98. except OSError as error:
  99. print(
  100. colored(
  101. _('The file could not be saved in "{path}": {error}').format(
  102. path=args.output, error=error.strerror
  103. ),
  104. "red",
  105. )
  106. )
  107. debug.debug_print(error)
  108. return 3
  109. elif not output.is_file_output_needed():
  110. print(output)
  111. else:
  112. print(
  113. colored(
  114. _("Please provide a file path to export in this format (--output)."),
  115. color="red",
  116. )
  117. )
  118. return 1
  119. return 0
  120. def get_information(
  121. compute_date: date,
  122. position: Position,
  123. timezone: int,
  124. output_format: str,
  125. colors: bool,
  126. show_graph: bool,
  127. ) -> dumper.Dumper:
  128. if position is not None:
  129. try:
  130. eph = get_ephemerides(
  131. date=compute_date, position=position, timezone=timezone
  132. )
  133. except OutOfRangeDateError as error:
  134. raise DateRangeError(error.min_date, error.max_date)
  135. else:
  136. eph = []
  137. try:
  138. moon_phase = get_moon_phase(compute_date)
  139. except OutOfRangeDateError as error:
  140. moon_phase = None
  141. print(
  142. colored(
  143. _(
  144. "Moon phase can only be displayed between {min_date} and {max_date}"
  145. ).format(
  146. min_date=error.min_date.strftime(SHORT_DATE_FORMAT),
  147. max_date=error.max_date.strftime(SHORT_DATE_FORMAT),
  148. ),
  149. "yellow",
  150. )
  151. )
  152. events_list = get_events(compute_date, timezone)
  153. return get_dumpers()[output_format](
  154. ephemerides=eph,
  155. moon_phase=moon_phase,
  156. events=events_list,
  157. date=compute_date,
  158. timezone=timezone,
  159. with_colors=colors,
  160. show_graph=show_graph,
  161. )
  162. def get_dumpers() -> {str: dumper.Dumper}:
  163. return {
  164. "text": dumper.TextDumper,
  165. "json": dumper.JsonDumper,
  166. "pdf": dumper.PdfDumper,
  167. }
  168. def output_version() -> bool:
  169. python_version = "%d.%d.%d" % (
  170. sys.version_info[0],
  171. sys.version_info[1],
  172. sys.version_info[2],
  173. )
  174. print("Kosmorro %s" % kosmorro_version)
  175. print(
  176. _(
  177. "Running on Python {python_version} "
  178. "with Kosmorrolib v{kosmorrolib_version}"
  179. ).format(python_version=python_version, kosmorrolib_version=kosmorrolib_version)
  180. )
  181. return True
  182. def clear_cache() -> bool:
  183. confirm = input(_("Do you really want to clear Kosmorro's cache? [yN] ")).upper()
  184. if re.match(locale.nl_langinfo(locale.YESEXPR), confirm) is not None:
  185. try:
  186. environment.clear_cache()
  187. except FileNotFoundError:
  188. debug.debug_print("No cache found, nothing done.")
  189. pass
  190. elif confirm != "" and re.match(locale.nl_langinfo(locale.NOEXPR), confirm) is None:
  191. print(_("Incorrect answer, cache not cleared."))
  192. return False
  193. return True
  194. def get_args(output_formats: [str]):
  195. today = date.today()
  196. parser = argparse.ArgumentParser(
  197. description=_(
  198. "Compute the ephemerides and the events for a given date and a given position on Earth."
  199. ),
  200. epilog=_(
  201. "By default, only the events will be computed for today ({date}).\n"
  202. "To compute also the ephemerides, latitude and longitude arguments"
  203. " are needed."
  204. ).format(date=today.strftime(dumper.FULL_DATE_FORMAT)),
  205. )
  206. parser.add_argument(
  207. "--version",
  208. "-v",
  209. dest="special_action",
  210. action="store_const",
  211. const=output_version,
  212. default=None,
  213. help=_("Show the program version"),
  214. )
  215. parser.add_argument(
  216. "--clear-cache",
  217. dest="special_action",
  218. action="store_const",
  219. const=clear_cache,
  220. default=None,
  221. help=_("Delete all the files from Kosmorro's cache."),
  222. )
  223. parser.add_argument(
  224. "--format",
  225. "-f",
  226. type=str,
  227. default=output_formats[0],
  228. choices=output_formats,
  229. help=_("The format to output the information to"),
  230. )
  231. parser.add_argument(
  232. "--latitude",
  233. "-lat",
  234. type=float,
  235. default=None,
  236. help=_(
  237. "The observer's latitude on Earth. Can also be set in the KOSMORRO_LATITUDE environment "
  238. "variable."
  239. ),
  240. )
  241. parser.add_argument(
  242. "--longitude",
  243. "-lon",
  244. type=float,
  245. default=None,
  246. help=_(
  247. "The observer's longitude on Earth. Can also be set in the KOSMORRO_LONGITUDE "
  248. "environment variable."
  249. ),
  250. )
  251. parser.add_argument(
  252. "--date",
  253. "-d",
  254. type=str,
  255. default=today.strftime("%Y-%m-%d"),
  256. help=_(
  257. "The date for which the ephemerides must be calculated. Can be in the YYYY-MM-DD format "
  258. 'or an interval in the "[+-]YyMmDd" format (with Y, M, and D numbers). '
  259. "Defaults to today ({default_date})."
  260. ).format(default_date=today.strftime("%Y-%m-%d")),
  261. )
  262. parser.add_argument(
  263. "--timezone",
  264. "-t",
  265. type=int,
  266. default=None,
  267. help=_(
  268. "The timezone to display the hours in (e.g. 2 for UTC+2 or -3 for UTC-3). "
  269. "Can also be set in the KOSMORRO_TIMEZONE environment variable."
  270. ),
  271. )
  272. parser.add_argument(
  273. "--no-colors",
  274. dest="colors",
  275. action="store_false",
  276. help=_("Disable the colors in the console."),
  277. )
  278. parser.add_argument(
  279. "--output",
  280. "-o",
  281. type=str,
  282. default=None,
  283. help=_(
  284. "A file to export the output to. If not given, the standard output is used. "
  285. "This argument is needed for PDF format."
  286. ),
  287. )
  288. parser.add_argument(
  289. "--no-graph",
  290. dest="show_graph",
  291. action="store_false",
  292. help=_(
  293. "Do not generate a graph to represent the rise and set times in the PDF format."
  294. ),
  295. )
  296. parser.add_argument(
  297. "--debug",
  298. dest="show_debug_messages",
  299. action="store_true",
  300. help=_("Show debugging messages"),
  301. )
  302. return parser.parse_args()