def write_config_file(config, username=None): config_path = CONFIG_BASE_PATH / (username or '') / 'google-music-scripts.toml' config_path.parent.mkdir(parents=True, exist_ok=True) config_path.touch() config_file = TOMLFile(config_path) config_file.write(config)
def parse_config_file(self): try: config = TOMLFile(Cesi.__config_file_path).read() except Exception as e: sys.exit("Failed to open/find {0} file. {1}".format( Cesi.__config_file_path, e)) self._check_config_file(config) self.__cesi = config.get("cesi", None) if self.__cesi is None: sys.exit( "Failed to read {0} configuration file. You must write cesi section." .format(Cesi.__config_file_path)) self.nodes = [] for node in config.get("nodes", []): _environment = node["environment"] or "default" _node = Node( name=node["name"], environment=_environment, host=node["host"], port=node["port"], username=node["username"], password=node["password"], ) self.nodes.append(_node)
def cargo_workspace_release(): workspace = TOMLFile("Cargo.toml").read()['workspace'] paths = reduce(op.concat, [glob(p) for p in workspace['members']], []) project_names = [ TOMLFile(f"{path}/Cargo.toml").read()['package']['name'] for path in paths ] for project in paths: cargo_release(project, project_names)
def sam_cli_configuration(): if skip_discovery(): return None config_file_path = os.environ.get("SAM_CONFIG_FILE", "samconfig.toml") assert os.path.exists(config_file_path) config = TOMLFile(config_file_path).read() config_env = os.environ.get("SAM_LAMBDA_CONFIG_ENV", "default") assert config.get(config_env) return config[config_env]
def read_config_file(username=None): config_path = CONFIG_BASE_PATH / (username or '') / 'google-music-scripts.toml' config_file = TOMLFile(config_path) try: config = config_file.read() except FileNotFoundError: config = TOMLDocument() write_config_file(config, username=username) return config
class PyProject: def __init__(self): self._file = TOMLFile('pyproject.toml') def get_version(self): pyproject_data = self._file.read() version = pyproject_data['tool']['poetry']['version'] return version def set_version(self, version): pyproject_data = self._file.read() pyproject_data['tool']['poetry']['version'] = version self._file.write(pyproject_data)
def read_config_file(): config_file = TOMLFile(CONFIG_PATH) try: config = config_file.read() except FileNotFoundError: config = TOMLDocument() if 'trackers' not in config: config['trackers'] = SortedDict() write_config_file(config) return config
def create_context(platform, work_dir, config_file, env): if not os.path.isfile(config_file): logging.error("config file %s is not exist", config_file) return None if not os.path.isabs(config_file): config_file = os.path.abspath(config_file) toml = TOMLFile(config_file) config = toml.read() logging.debug('config file content:') logging.debug(config) return Context(platform, work_dir, config, env)
def __init__(self, filename=None): env = os.environ filename = filename or 'tusker.toml' toml = TOMLFile(filename) try: data = toml.read() except FileNotFoundError: data = {} # time to validate some configuration variables data.setdefault('database', {'dbname': 'tusker'}) data.setdefault('schema', {'filename': 'schema.sql'}) data.setdefault('migrations', {'directory': 'migrations'}) self.schema = SchemaConfig(data['schema']) self.migrations = MigrationsConfig(data['migrations']) self.database = DatabaseConfig(data['database'])
def test_toml_file(example): original_content = example("example") toml_file = os.path.join(os.path.dirname(__file__), "examples", "example.toml") toml = TOMLFile(toml_file) content = toml.read() assert isinstance(content, TOMLDocument) assert content["owner"]["organization"] == "GitHub" toml.write(content) with io.open(toml_file, encoding="utf-8") as f: assert original_content == f.read()
def cargo_release(project, internal_dependencies=[None]): project_path = path.join(project, "Cargo.toml") file = TOMLFile(project_path) content = file.read() dependencies = content.get('dependencies') or {} build_dependencies = content.get('build-dependencies') or {} new_version = change_version(content['package']['version']) content['package']['version'] = new_version for local in internal_dependencies: if dependencies.get(local) is not None: dependencies[local]['version'] = new_version if build_dependencies.get(local) is not None: build_dependencies[local]['version'] = new_version file.write(content)
def read_config_file(config_file_path): try: config = TOMLFile(config_file_path).read() except Exception as e: sys.exit("Failed to open/find {0} file. {1}".format(config_file_path, e)) return config
def test_pyproject_toml_poetry_config( pyproject_toml: Path, poetry_section: str ) -> None: pyproject = PyProjectTOML(pyproject_toml) doc: dict[str, Any] = TOMLFile(pyproject_toml.as_posix()).read() config = doc["tool"]["poetry"] assert pyproject.is_poetry_project() assert pyproject.poetry_config == config
def from_file(cls, path: os.PathLike): """Parse toml file. Parameters ---------- path Path to the parameter file. Notes ----- All parameters declared in the toml file will be parsed. Table names are ignored. """ _params = TOMLFile(path).read() params = {} for subdict in _params.values(): params.update({k: v for k, v in subdict.items()}) return cls(**params)
def get_config(self) -> PackageConfig: persistence_provider = self._persistence_provider persisted_package_config = persistence_provider.get_package_config() current_package_config = self._package_config if persisted_package_config is None: logger = self._logger config_file_path = constants.PACKAGE_CONFIG_FILE if not config_file_path.exists(): raise PackageConfigError( f"The '{config_file_path}' path does not exist!") if not config_file_path.is_file(): raise PackageConfigError( f"The '{config_file_path}' does not represent a file!") logger.info(f"Analyzing the '{config_file_path.name}' file.") try: toml_file = TOMLFile(str(config_file_path)) toml_document = toml_file.read() except TOMLKitError as e: logger.exception( f"Error while parsing the '{config_file_path.name}' file") raise PackageConfigError( "Could not load the package configuration from file'!" ) from e else: current_package_config = FilePackageConfigProvider._read_config_from( toml_document).with_initialization_type( ConfigInitializationType.PARSED, PackageConfig) persistence_provider.save_package_config( current_package_config) elif current_package_config is None: current_package_config = persisted_package_config.with_initialization_type( ConfigInitializationType.PERSISTED, PackageConfig) self._package_config = current_package_config return current_package_config
def __init__(self): config_file = os.environ.get( "CONFIG", os.path.join(os.path.dirname(__file__), "config.toml")) toml = TOMLFile(config_file) self._config = toml.read() for key in [ "db_host", "db_name", "db_user", "db_pass", "discord_token", "discord_guild_id", ]: if key not in self._config: logging.error("Required attribute %r not in config", key) raise Exception("MissingConfigEntry", key) if "db_table_prefix" not in self._config: self._config["db_table_prefix"] = "" self._db_cfg = { "host": self._config["db_host"], "user": self._config["db_user"], "passwd": self._config["db_pass"], "db": self._config["db_name"], "cursorclass": MySQLdb.cursors.DictCursor, "charset": "utf8mb4", } logger.setLevel( getattr(logging, self._config.get("log_level", logging.INFO))) handler = logging.StreamHandler(stdout) handler.setFormatter( logging.Formatter( "%(asctime)s %(name)-12s %(levelname)-8s %(message)s")) logger.addHandler(handler) logging.getLogger("websockets").setLevel("INFO") logging.getLogger("discord").setLevel("INFO") self._bot = commands.Bot(command_prefix=self._command_prefix) for cog in self._config.get("cogs", []): self._bot.load_extension("cogs." + cog)
def generateDefaultConfigurationTomlFile(configurationFilename): """Generate default configuration file. Print once a warning to inform the user that the default needs to be adopted. """ print( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) print("!! Default configuration was written to file '", configurationFilename, "' !!", sep="") print( "!! It is assumed, that you need to adopt the defaults to your needs. !!" ) print( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) tomlfile = TOMLFile(configurationFilename) config = tomlkit.parse(defaultConfiguration) tomlfile.write(config)
def from_file(cls, path: os.PathLike, key: str = "pointing_params"): """Parse toml file. Parameters ---------- path Path to pointing error parameter file. key Table name in the TOML file. """ params = TOMLFile(path).read() return cls(**params[key])
def push(hub, args): # TODO: local imports to avoid circular dependencies, can clean-up once Poetry's plugin system arrives from poetry.packages.locker import Locker from tomlkit.toml_file import TOMLFile current_index = hub.current.index root_url = hub.current.root_url.url current_user, current_index = current_index[len(root_url):].split('/') available_indices = hub.http_api("get", root_url, quiet=True, check_version=False).result local_config = TOMLFile('pyproject.toml').read() locker = Locker('poetry.lock', local_config) locked_repository = locker.locked_repository(with_dev_reqs=not args.no_dev) for pkg in locked_repository.packages: name, version = pkg.name, pkg.version.text if pkg.source_url and pkg.source_url != current_index: hub.warn( "'{}=={}' is locked from {} which is not the current index, skipping." .format(name, version, pkg.source_url)) continue # try to guess the index from the download link project_url = hub.current.get_project_url(name) reply = hub.http_api("get", project_url, type="projectconfig") link = reply.result.get(version, {}).get('+links', [{}])[0].get('href') if not link.startswith(root_url): hub.warn( "'{}=={}' is mirrored from an external url, skipping.".format( name, version)) continue user, index, _ = link[len(root_url):].split('/', 2) if ((user, index) != (current_user, current_index) and not args.include_local_bases and available_indices.get(user, {}).get('indexes', {}).get( index, {}).get('type') != 'mirror'): hub.info( "Skipping '{}=={}' available from local base '{}/{}'".format( name, version, user, index)) continue pkg_args = Namespace(pkgspec='{}=={}'.format(name, version), index='{}/{}'.format(user, index), **vars(args)) devpi_push(hub, pkg_args)
def test_mixed_eol(tmpdir): toml_path = str(tmpdir / "pyproject.toml") with open(toml_path, "wb+") as f: f.write(b"a = 1\r\nrb = 2\n") f = TOMLFile(toml_path) f.write(f.read()) with open(toml_path, "rb") as f: assert f.read() == b"a = 1\r\nrb = 2\n"
def test_keep_old_eol_2(tmpdir): toml_path = str(tmpdir / "pyproject.toml") with open(toml_path, "wb+") as f: f.write(b"a = 1\nb = 2\n") f = TOMLFile(toml_path) content = f.read() content["b"] = 3 f.write(content) with open(toml_path, "rb") as f: assert f.read() == b"a = 1\nb = 3\n"
def read_config(path): toml = TOMLFile(path).read() config = { 'database': { 'engine': 'sqlite3', 'connection': '/var/lib/acme-dns/acme-dns.db', }, 'sidecar': { 'secrets': { 'field_selector': None, 'label_selector': None, }, }, } config = validate_config(config, toml) if config['database']['engine'] != 'sqlite3': raise InvalidConfiguration('only sqlite3 database engine is supported') return config
def read_config_file(config_file=None): """If CONFIG_FILE exists, and readable, and in TOML format... Read in the options set there and return 'cfg_opts' dict. """ if not config_file: config_file = DEFAULT_CONFIG_FILE try: log.info(f'Reading config file: {config_file}') cfg_opts = {} if os.path.exists(config_file): cfg_opts = TOMLFile(config_file).read() else: log.notice(f'Config file "{config_file}" does not exist!?') return None except Exception as e: log.exception(f'Error: {e}') raise e else: return cfg_opts
def parse(config_file: Path) -> TOMLDocument: """Parse configuration file Args: config_file (Path): Path of the configuration file. Returns: TOMLDocument with the content of the file parsed. Raises: RuntimeError if the file is not found. ConfigError if tomlkit fails to parse the file.""" path = Path(config_file) if not path.exists(): raise RuntimeError(f'Configuration file {config_file} not found.') try: return TOMLFile(path).read() except Exception as exc: raise ConfigError(f'Failed to parse {config_file}') from exc
def __init__(self): self._file = TOMLFile('pyproject.toml')
def write_config_file(config): CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True) CONFIG_PATH.touch() config_file = TOMLFile(CONFIG_PATH) config_file.write(config)
def test_pyproject_toml_poetry_config(pyproject_toml, poetry_section): pyproject = PyProjectTOML(pyproject_toml) config = TOMLFile(pyproject_toml.as_posix()).read()["tool"]["poetry"] assert pyproject.is_poetry_project() assert pyproject.poetry_config == config
def test_pyproject_toml_simple(pyproject_toml, build_system_section, poetry_section): data = TOMLFile(pyproject_toml.as_posix()).read() assert PyProjectTOML(pyproject_toml).data == data
def test_pyproject_toml_simple( pyproject_toml: Path, build_system_section: str, poetry_section: str ) -> None: data = TOMLFile(pyproject_toml.as_posix()).read() assert PyProjectTOML(pyproject_toml).data == data
src_dir = os.path.join(os.path.dirname(__file__), "..", "srcdata", "stm32_db", "mcu") pathlist = Path(src_dir).glob("STM32*.xml") for path in pathlist: xml_tree = etree.parse(str(path)) xml_root = xml_tree.getroot() toml_file = os.path.join( os.path.dirname(__file__), "..", "data", "mcu", "stm", path.stem.lower() + ".toml", ) toml = TOMLFile(toml_file) doc = document() toml_file_header(doc) doc.add(nl()) doc.add("name", xml_root.get("RefName")) doc.add("object_type", "mcu") if xml_root.get("Package"): doc.add("package", xml_root.get("Package")) if xml_root.get("Family"): doc.add("family", xml_root.get("Family")) if xml_root.get("Line"): doc.add("line", xml_root.get("Line"))