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"
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(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
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 __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 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
raise e if __name__ == "__main__": logger = logging.getLogger(__name__) handler = logging.StreamHandler(sys.stdout) handler.setFormatter( logging.Formatter( "%(asctime)s %(name)-12s %(levelname)-8s %(message)s")) logger.addHandler(handler) logger.setLevel(logging.DEBUG) config_file = os.environ.get( "CONFIG", os.path.join(os.path.dirname(__file__), "config.toml")) toml = TOMLFile(config_file) config = toml.read() if len(sys.argv) == 1 or sys.argv[1] == "help": print( dedent(""" Usage: {} COMMAND Where COMMAND is one of: help - Show this init - Create database gentokens TYPE X - Generate X more tokens of TYPE import TYPE FILENAME - Import CSV of TYPE tokens (imported as EXPORTED and USED!) export TYPE [FILENAME] - Generate CSV of all un-exported TYPE tokens issue TYPE EMAIL [COUNT] - Take tokens, print to console, mark used find EMAIL|TOKEN [TYPE] - Find previously issued tokens. % wildcards accepted. send TYPE EMAIL X - Send X unused tokens of TYPE to EMAIL sendcsv TYPE FILE - Send unused tokens of TYPE to each EMAIL,COUNT from FILE