def load_state_table(cls, state_table_name='panoptes'): """ Loads the state table Args: state_table_name(str): Name of state table. Corresponds to filename in `$POCS/conf_files/state_table/` directory or to absolute path if starts with "/". Default 'panoptes.yaml'. Returns: dict: Dictionary with `states` and `transitions` keys. """ if not state_table_name.startswith('/'): state_table_file = os.path.join( os.getenv('POCS', default='/var/panoptes/POCS'), 'conf_files', 'state_table', f'{state_table_name}.yaml' ) else: state_table_file = state_table_name try: with open(state_table_file, 'r') as f: state_table = from_yaml(f.read()) except Exception as err: raise error.InvalidConfig( f'Problem loading state table yaml file: {err!r} {state_table_file}') return state_table
def current_reading(): """Gets the current weather reading.""" reading = dict() with weather_file.open('r') as f: reading = from_yaml(f.read()) return reading
def read_field_list(self): """Reads the field file and creates valid `Observations` """ self.logger.debug(f'Reading fields from file: {self.fields_file}') if self._fields_file is not None: if not os.path.exists(self.fields_file): raise FileNotFoundError with open(self.fields_file, 'r') as f: self._fields_list = from_yaml(f.read()) if self._fields_list is not None: for field_config in self._fields_list: try: self.add_observation(field_config) except AssertionError: self.logger.debug("Skipping duplicate field.") except Exception as e: self.logger.warning(f"Error adding field: {e!r}")
def _setup_commands(self, commands): """ Does any setup for the commands needed for this mount. Mostly responsible for setting the pre- and post-commands. We could also do some basic checking here to make sure required commands are in fact available. """ self.logger.debug('Setting up commands for mount') if len(commands) == 0: model = self.get_config('mount.brand') if model is not None: mount_dir = self.get_config('directories.mounts') conf_file = "{}/{}.yaml".format(mount_dir, model) if os.path.isfile(conf_file): self.logger.info( "Loading mount commands file: {}".format(conf_file)) try: with open(conf_file, 'r') as f: commands.update(from_yaml(f.read())) self.logger.debug( "Mount commands updated from {}".format( conf_file)) except OSError as err: self.logger.warning( 'Cannot load commands config file: {} \n {}'. format(conf_file, err)) except Exception: self.logger.warning( "Problem loading mount command file") else: self.logger.warning( "No such config file for mount commands: {}".format( conf_file)) # Get the pre- and post- commands self._pre_cmd = commands.setdefault('cmd_pre', ':') self._post_cmd = commands.setdefault('cmd_post', '#') self.logger.debug('Mount commands set up') return commands
def field_list(): return from_yaml(""" - name: HD 189733 position: 20h00m43.7135s +22d42m39.0645s priority: 100 - name: HD 209458 position: 22h03m10.7721s +18d53m03.543s priority: 100 - name: Tres 3 position: 17h52m07.02s +37d32m46.2012s priority: 100 exp_set_size: 15 min_nexp: 240 - name: M5 position: 15h18m33.2201s +02d04m51.7008s priority: 50 - name: KIC 8462852 position: 20h06m15.4536s +44d27m24.75s priority: 50 exptime: 60 exp_set_size: 15 min_nexp: 45 - name: Wasp 33 position: 02h26m51.0582s +37d33m01.733s priority: 100 - name: M42 position: 05h35m17.2992s -05d23m27.996s priority: 25 exptime: 240 - name: M44 position: 08h40m24s +19d40m00.12s priority: 50 """)
def parse_config(lines): # pragma: no cover yaml_string = '' for line in lines: IsID = len(line.split('/')) > 1 IsLabel = re.match(r'^Label:\s*(.*)', line) IsType = re.match(r'^Type:\s*(.*)', line) IsCurrent = re.match(r'^Current:\s*(.*)', line) IsChoice = re.match(r'^Choice:\s*(\d+)\s*(.*)', line) IsPrintable = re.match(r'^Printable:\s*(.*)', line) IsHelp = re.match(r'^Help:\s*(.*)', line) if IsLabel or IsType or IsCurrent: line = f' {line}' elif IsChoice: if int(IsChoice.group(1)) == 0: line = ' Choices:\n {}: {:d}'.format( IsChoice.group(2), int(IsChoice.group(1))) else: line = ' {}: {:d}'.format(IsChoice.group(2), int(IsChoice.group(1))) elif IsPrintable: line = ' {}'.format(line) elif IsHelp: line = ' {}'.format(line) elif IsID: line = '- ID: {}'.format(line) elif line == '': continue else: print(f'Line not parsed: {line}') yaml_string += f'{line}\n' properties_list = from_yaml(yaml_string) if isinstance(properties_list, list): properties = {} for property in properties_list: if property['Label']: properties[property['Label']] = property else: properties = properties_list return properties
def test_roundtrip_yaml(obj): config_str = serializers.to_yaml(obj) config = serializers.from_yaml(config_str) assert config['name'] == obj['name'] assert config['location']['latitude'] == obj['location']['latitude']
def load_default(): return parse_config(from_yaml(DEFAULT_CONFIG))
def _add_to_conf(config, conf_fn, parse=False): with suppress(IOError, TypeError): with open(conf_fn, 'r') as fn: config.update(from_yaml(fn.read(), parse=parse))
def _add_to_conf(config, fn, parse=False): with suppress(IOError): with open(fn, 'r') as f: c = serializers.from_yaml(f, parse=parse) if c is not None and isinstance(c, dict): config.update(c)