From ed2fa610429fc0fb6b97ec2987c02854c723fa71 Mon Sep 17 00:00:00 2001 From: Wilfred Tyler Gee Date: Fri, 19 Aug 2022 15:55:36 -1000 Subject: [PATCH] Create scheduler updates (#1174) * Create scheduler updates A few updates to the helper script that creates a scheduler. * Better checking of `iers_auto` conf for downloading IERS file when problems. * Add some typing to the file. Note that this requires python 3.10. * Use `Path`s. * Don't require python 3.10 syntax. --- src/panoptes/pocs/scheduler/__init__.py | 41 +++++++++++++------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/panoptes/pocs/scheduler/__init__.py b/src/panoptes/pocs/scheduler/__init__.py index 365c91597..9fb0de35f 100644 --- a/src/panoptes/pocs/scheduler/__init__.py +++ b/src/panoptes/pocs/scheduler/__init__.py @@ -1,23 +1,23 @@ -import os +from pathlib import Path +from typing import List, Optional from astropy.utils.iers import Conf as iers_conf +from panoptes.utils import error +from panoptes.utils.config.client import get_config +from panoptes.utils.library import load_module -from panoptes.pocs.scheduler.constraint import Altitude +from panoptes.pocs.scheduler.constraint import Altitude, BaseConstraint from panoptes.pocs.scheduler.constraint import Duration from panoptes.pocs.scheduler.constraint import MoonAvoidance - from panoptes.pocs.scheduler.scheduler import BaseScheduler # noqa; needed for import -from panoptes.utils import error -from panoptes.utils.library import load_module -from panoptes.pocs.utils.logger import get_logger -from panoptes.utils.config.client import get_config - from panoptes.pocs.utils.location import create_location_from_config +from panoptes.pocs.utils.logger import get_logger logger = get_logger() -def create_scheduler_from_config(config=None, observer=None, iers_url=None, *args, **kwargs): +def create_scheduler_from_config(config=None, observer=None, iers_url=None, *args, + **kwargs) -> Optional[BaseScheduler]: """ Sets up the scheduler that will be used by the observatory """ scheduler_config = config or get_config('scheduler', default=None) @@ -29,8 +29,9 @@ def create_scheduler_from_config(config=None, observer=None, iers_url=None, *arg iers_url = iers_url or scheduler_config.get('iers_url') if iers_url is not None: - logger.debug(f'Getting IERS data at {iers_url=}') + logger.info(f'Getting IERS data at {iers_url=}') iers_conf.iers_auto_url.set(iers_url) + iers_conf.auto_download.set(scheduler_config.get('iers_auto', False)) if not observer: logger.debug(f'No Observer provided, creating location from config.') @@ -38,12 +39,12 @@ def create_scheduler_from_config(config=None, observer=None, iers_url=None, *arg observer = site_details.observer # Read the targets from the file - fields_file = scheduler_config.get('fields_file', 'simple.yaml') - fields_dir = get_config('directories.fields', './conf_files/fields') - fields_path = os.path.join(fields_dir, fields_file) + fields_file = Path(scheduler_config.get('fields_file', 'simple.yaml')) + fields_dir = Path(get_config('directories.fields', default='./conf_files/fields')) + fields_path = fields_dir / fields_file logger.debug(f'Creating scheduler: {fields_path}') - if os.path.exists(fields_path): + if fields_path.exists(): scheduler_type = scheduler_config.get('type', 'panoptes.pocs.scheduler.dispatch') try: @@ -53,20 +54,20 @@ def create_scheduler_from_config(config=None, observer=None, iers_url=None, *arg constraints = create_constraints_from_config(config=scheduler_config) # Create the Scheduler instance - scheduler = module.Scheduler(observer, - fields_file=fields_path, - constraints=constraints, - *args, **kwargs) + pocs_scheduler = module.Scheduler(observer, + fields_file=str(fields_path), + constraints=constraints, + *args, **kwargs) logger.debug("Scheduler created") except error.NotFound as e: raise error.NotFound(msg=e) else: raise error.NotFound(msg=f"Fields file does not exist: {fields_path=!r}") - return scheduler + return pocs_scheduler -def create_constraints_from_config(config=None): +def create_constraints_from_config(config=None) -> List[BaseConstraint]: scheduler_config = config or get_config('scheduler', default=dict()) constraints = list() for constraint_config in scheduler_config.get('constraints', list()):