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.

core.py 3.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 os
  18. import re
  19. from shutil import rmtree
  20. from pathlib import Path
  21. from datetime import date
  22. from dateutil.relativedelta import relativedelta
  23. from skyfield.api import Loader
  24. from skyfield.timelib import Time
  25. from skyfield.nutationlib import iau2000b
  26. from kosmorrolib.i18n import _
  27. CACHE_FOLDER = str(Path.home()) + '/.kosmorro-cache'
  28. class Environment:
  29. def __init__(self):
  30. self._vars = {}
  31. def __set__(self, key, value):
  32. self._vars[key] = value
  33. def __getattr__(self, key):
  34. return self._vars[key] if key in self._vars else None
  35. def __str__(self):
  36. return self._vars.__str__()
  37. def __len__(self):
  38. return len(self._vars)
  39. def get_env() -> Environment:
  40. environment = Environment()
  41. for var in os.environ:
  42. if not re.search('^KOSMORRO_', var):
  43. continue
  44. [_, env] = var.split('_', 1)
  45. environment.__set__(env.lower(), os.getenv(var))
  46. return environment
  47. def get_loader():
  48. return Loader(CACHE_FOLDER)
  49. def get_timescale():
  50. return get_loader().timescale()
  51. def get_skf_objects():
  52. return get_loader()('de421.bsp')
  53. def get_iau2000b(time: Time):
  54. return iau2000b(time.tt)
  55. def clear_cache():
  56. rmtree(CACHE_FOLDER)
  57. def flatten_list(the_list: list):
  58. new_list = []
  59. for item in the_list:
  60. if isinstance(item, list):
  61. for item2 in flatten_list(item):
  62. new_list.append(item2)
  63. continue
  64. new_list.append(item)
  65. return new_list
  66. def get_date(date_arg: str) -> date:
  67. if re.match(r'^\d{4}-\d{2}-\d{2}$', date_arg):
  68. try:
  69. return date.fromisoformat(date_arg)
  70. except ValueError as error:
  71. raise ValueError(_('The date {date} is not valid: {error}').format(date=date_arg,
  72. error=error.args[0])) from error
  73. elif re.match(r'^([+-])(([0-9]+)y)?[ ]?(([0-9]+)m)?[ ]?(([0-9]+)d)?$', date_arg):
  74. def get_offset(date_arg: str, signifier: str):
  75. if re.search(r'([0-9]+)' + signifier, date_arg):
  76. return abs(int(re.search(r'[+-]?([0-9]+)' + signifier, date_arg).group(0)[:-1]))
  77. return 0
  78. days = get_offset(date_arg, 'd')
  79. months = get_offset(date_arg, 'm')
  80. years = get_offset(date_arg, 'y')
  81. if date_arg[0] == '+':
  82. return date.today() + relativedelta(days=days, months=months, years=years)
  83. return date.today() - relativedelta(days=days, months=months, years=years)
  84. else:
  85. error_msg = _('The date {date} does not match the required YYYY-MM-DD format or the offset format.')
  86. raise ValueError(error_msg.format(date=date_arg))