def main(**kwargs): # fmt: on if kwargs["generate_config"]: print(Config.default) return if kwargs["config_path"] is None: raise UsageError('Error: Missing option "-c" / "--config-path".') config_path = kwargs["config_path"] if not config_path.exists(): sys.exit(f"Configuration file does not exist! {config_path!r}") config = Config.from_file(config_path) # Overload the configs values with any suitable cli args. overloads = toml_loads(Config.default)["ep"] for key, value in [ (value, kwargs[value]) for value in overloads if kwargs.get(value, None) is not None ]: config["ep"][key] = value get_logger( "discord", "WARN", fmt="[[ discord ]] [%(asctime)s] %(levelname)s - %(message)s" ) config["disabled"] = disable = kwargs["disable"] with Client(config=config, disable=disable) as client: client.run()
def _read_config( self, config_contents: Optional[str] = None) -> BubblejailInstanceConfig: if config_contents is None: config_contents = self._read_config_file() conf_dict = cast(ServicesConfDictType, toml_loads(config_contents)) return BubblejailInstanceConfig(conf_dict)
async def latoml(filepath: str) -> JASM: """ :param filepath: :return: """ try: _toml_str = await lastring(filepath) return toml_loads(_toml_str) except NameError: raise EnvironmentError("'pip install toml' if you wanna use this!")
def ltoml(filepath: str) -> JASM: """ :param filepath: :return: """ try: with open(filepath) as f: return toml_load(f) except NameError: raise EnvironmentError("'pip install toml' if you wanna use this!") except UnicodeDecodeError as e: return toml_loads(lstring(filepath))
async def edit_config_in_editor(self) -> None: # Create temporary directory with TemporaryDirectory() as tempdir: # Create path to temporary file and write exists config temp_file_path = Path(tempdir + 'temp.toml') with open(temp_file_path, mode='w') as tempfile: tempfile.write(self._read_config_file()) # Launch EDITOR on the temporary file run_args = [environ['EDITOR'], str(temp_file_path)] p = await create_subprocess_exec(*run_args) await p.wait() # Verify that the new config is valid and save to variable with open(temp_file_path) as tempfile: new_config_toml = tempfile.read() BubblejailInstanceConfig( cast(ServicesConfDictType, toml_loads(new_config_toml))) # Write to instance config file with open(self.path_config_file, mode='w') as conf_file: conf_file.write(new_config_toml)
def from_file(cls, file: Union[Path, str]) -> "Config": """Read the configuration at a filepath and return a dict. >>> config = Config.from_file("./ep.toml") Parameters ---------- file : Union[:class:`pathlib.Path`, :class:`str`] The file to read. """ if isinstance(file, str): path = Path(file) elif isinstance(file, Path): path = file else: raise TypeError( "`file` argument must be a string or pathlib.Path instance." ) path = path.resolve().absolute() return cls(toml_loads(path.read_text()), fp=path)
def _get_metadata_dict(self) -> MutableMapping[Any, Any]: try: with open(self.path_metadata_file) as metadata_file: return toml_loads(metadata_file.read()) except FileNotFoundError: return {}