def write_file_config(options, conf_dir=None): """Write a configuration file from the given options. Parameters ---------- options : argparse.Namespace The options from ArgumentParser conf_dir : str, optional A directory for saving the configuration file in. Default is $HOME/.config/opsim4. """ parser = configparser.SafeConfigParser() parser.add_section("Database") parser.set("Database", "type", options.type) parser.add_section(options.type) if options.type == "sqlite": parser.set(options.type, "save_directory", options.save_dir) if options.session_id_start is not None: parser.set(options.type, "session_id_start", options.session_id_start) if options.type == "mysql" and options.config_path is not None: parser.set(options.type, "config_path", options.config_path) if conf_dir is None: conf_dir = expand_path(os.path.join("$HOME", ".config")) with open(os.path.join(conf_dir, "opsim4"), 'w') as cfile: parser.write(cfile)
def write_file_config(options, conf_dir=None): """Write a configuration file from the given options. Parameters ---------- options : argparse.Namespace The options from ArgumentParser conf_dir : str, optional A directory for saving the configuration file in. Default is $HOME/.config/opsim4. """ parser = configparser.SafeConfigParser() parser.add_section("Database") parser.set("Database", "type", options.type) parser.add_section(options.type) parser.set(options.type, "save_directory", options.save_dir) if options.session_save_dir is not None: parser.set(options.type, "session_save_directory", options.session_save_dir) if options.session_id_start is not None: parser.set(options.type, "session_id_start", options.session_id_start) if conf_dir is None: conf_dir = expand_path(os.path.join("$HOME", ".config")) with open(os.path.join(conf_dir, "opsim4"), 'w') as cfile: parser.write(cfile)
def _make_engine(self, sqlite_db=None, alternate_save_path=None): """Create the engine for database interactions. Parameters ---------- sqlite_db : str The name of the database file for SQLite. alternate_save_path : str, optional Specify an alternate path to save the database. """ save_path = None if self.sqlite_save_path is not None: save_path = expand_path(self.sqlite_save_path) if alternate_save_path is not None: save_path = expand_path(alternate_save_path) if save_path is not None: sqlite_db = os.path.join(save_path, sqlite_db) return create_engine("sqlite:///{}".format(sqlite_db))
def _connect(self): """Create the database connection for MySQL. Returns ------- function The connection function for MySQL. """ if self.mysql_config_path is not None: conf_path = expand_path(self.mysql_config_path) else: conf_path = os.getenv("HOME") conf_file = os.path.join(conf_path, ".my.cnf") return mysql.connect(read_default_file=conf_file, db=self.db_name)
def _make_engine(self, sqlite_db=None): """Create the engine for database interactions. Parameters ---------- sqlite_db : str The name of the database file for SQLite. """ if self.db_dialect == "mysql": return create_engine("mysql://", creator=self._connect) if self.db_dialect == "sqlite": if self.sqlite_save_path is not None: self.sqlite_save_path = expand_path(self.sqlite_save_path) sqlite_db = os.path.join(self.sqlite_save_path, sqlite_db) return create_engine("sqlite:///{}".format(sqlite_db))
def read_file_config(conf_file=None, conf_dir=None): """Read in a configuration file. Parameters ---------- conf_file : str, optional The name of the configuration file. Default is opsim4. conf_dir : str, optional The directory location of the configuration file. Default is $HOME/.config """ if conf_file is None: conf_file = "opsim4" if conf_dir is None: conf_dir = expand_path(os.path.join("$HOME", ".config")) full_conf_file = os.path.join(conf_dir, conf_file) if not os.path.exists(full_conf_file): return None parser = configparser.SafeConfigParser() parser.read(full_conf_file) return parser
def load(self, ifiles): """Load and apply configuration override files. This function loads the specified configuration files and applies them to the configuration objects. If input is a directory, it is assumed that all files in that directory are override files. Parameters ---------- ifiles : list[str] A list of files or directories containing configuration overrides. """ if ifiles is None: return config_files = [] for ifile in ifiles: ifile = expand_path(ifile) if os.path.isdir(ifile): dfiles = os.listdir(ifile) for dfile in dfiles: full_dfile = os.path.join(ifile, dfile) if os.path.isfile(full_dfile): config_files.append(full_dfile) if os.path.isdir(full_dfile): self.survey.alt_proposal_dir = full_dfile else: config_files.append(ifile) if self.survey.alt_proposal_dir is not None: sys.path.insert(0, self.survey.alt_proposal_dir) if len(config_files): load_config(self.survey, config_files) self.science.load(config_files) load_config(self.observing_site, config_files) self.observatory.load(config_files) load_config(self.downtime, config_files) load_config(self.sched_driver, config_files) load_config(self.environment, config_files)
def load(self, ifiles): """Load and apply configuration override files. This function loads the specified configuration files and applies them to the configuration objects. If input is a directory, it is assumed that all files in that directory are override files. Parameters ---------- ifiles : list[str] A list of files or directories containing configuration overrides. """ if ifiles is None: return config_files = [] for ifile in ifiles: ifile = expand_path(ifile) if os.path.isdir(ifile): dfiles = os.listdir(ifile) for dfile in dfiles: full_dfile = os.path.join(ifile, dfile) if os.path.isfile(full_dfile): config_files.append(full_dfile) if os.path.isdir(full_dfile): self.survey.alt_proposal_dir = full_dfile else: config_files.append(ifile) if len(config_files): load_config(self.survey, config_files) self.science.load(config_files) load_config(self.observing_site, config_files) self.observatory.load(config_files) load_config(self.downtime, config_files) load_config(self.sched_driver, config_files) load_config(self.environment, config_files)