From 5c8d885707348d320a95f886f5c8beb5aac9e80d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Deuchnord?= Date: Fri, 15 Nov 2019 08:30:50 +0100 Subject: [PATCH] Add --clear-cache option --- kosmorro | 27 ++++++++++++++++++++++++++- kosmorrolib/core.py | 9 ++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/kosmorro b/kosmorro index 224b8cb..1b68cb4 100644 --- a/kosmorro +++ b/kosmorro @@ -17,14 +17,21 @@ # along with this program. If not, see . import argparse +import sys from datetime import date + from kosmorrolib import dumper +from kosmorrolib import core from kosmorrolib.ephemerides import EphemeridesComputer, Position def main(): output_formats = get_dumpers() args = get_args(list(output_formats.keys())) + + if args.special_action is not None: + return 0 if args.special_action() else 1 + year = args.year month = args.month day = args.day @@ -38,6 +45,8 @@ def main(): dump = output_formats[args.format](ephemerides) print(dump.to_string()) + return 0 + def get_dumpers() -> {str: dumper.Dumper}: return { @@ -46,6 +55,20 @@ def get_dumpers() -> {str: dumper.Dumper}: } +def clear_cache() -> bool: + confirm = input("Do you really want to clear Kosmorro's cache? [yN] ").upper() + if confirm == 'Y': + try: + core.clear_cache() + except FileNotFoundError: + pass + elif confirm not in ('N', ''): + print('Answer did not match expected options, cache not cleared.') + return False + + return True + + def get_args(output_formats: [str]): today = date.today() @@ -55,6 +78,8 @@ def get_args(output_formats: [str]): ' observer positioned at coordinates (0,0), with an altitude of 0.' % today.strftime('%a %b %d, %Y')) + parser.add_argument('--clear-cache', dest='special_action', action='store_const', const=clear_cache, default=None, + help='Delete all the files Kosmorro stored in the cache.') parser.add_argument('--format', '-f', type=str, default=output_formats[0], choices=output_formats, help='The format under which the information have to be output') parser.add_argument('--latitude', '-lat', type=float, default=0., @@ -77,4 +102,4 @@ def get_args(output_formats: [str]): if __name__ == '__main__': - main() + sys.exit(main()) diff --git a/kosmorrolib/core.py b/kosmorrolib/core.py index cc86ce6..b7476c4 100644 --- a/kosmorrolib/core.py +++ b/kosmorrolib/core.py @@ -16,11 +16,14 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from shutil import rmtree +from pathlib import Path from skyfield.api import Loader from .data import Star, Planet, Satellite VERSION = '0.1.1' +CACHE_FOLDER = str(Path.home()) + '/.kosmorro-cache' MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'] @@ -37,7 +40,7 @@ ASTERS = [Star('Sun', 'SUN'), def get_loader(): - return Loader('~/.kosmorro-cache') + return Loader(CACHE_FOLDER) def get_timescale(): @@ -46,3 +49,7 @@ def get_timescale(): def get_skf_objects(): return get_loader()('de421.bsp') + + +def clear_cache(): + rmtree(CACHE_FOLDER)