def init_config(self, config: dict) -> None: _config = deepcopy(config) if _config and 'sshtunnel' in _config: _ssh_config = _config.pop('sshtunnel') if isinstance(_ssh_config, str): _config_path, section = _ssh_config.split(':') config_path = Config.get_config_path(path=_config_path) self.ssh_config = Config.parse_config(section=section, config_path=config_path) elif isinstance(_ssh_config, dict): self.ssh_config = _ssh_config else: raise TypeError(type(_ssh_config)) self.config = _config
def get_config_instance(cls, config_path: Optional[str] = None) -> Config: if config_path: _config = Config(config_path=config_path) elif not cls.config: cls.init_config() _config = cls.config else: _config = cls.config return _config
def get_default_alias(cls) -> Optional[dict]: default_config_dir = os.path.dirname(os.path.abspath(inspect.getfile(cls))) default_alias_path = os.path.join(default_config_dir, 'default', 'alias.ini') if os.path.isfile(default_alias_path): try: return Config.parse_config(section='alias', config_path=default_alias_path) except Exception: return None else: return None
def __init__(self, data: Optional[Union[str, dict]] = None) -> None: self.aliases = None if isinstance(data, str): aliases = Config.parse_config(section='alias', config_path=data) self.aliases = aliases elif isinstance(data, dict): self.aliases = data elif data is None: pass else: raise TypeError(type(data)) # pragma: no cover
def __getitem__(self, item: str) -> type: if self.aliases: path = self.aliases[item] elif self.get_env(item) in os.environ: path = os.environ.get(self.get_env(item)) elif '%s_ALIAS_PATH' % Config.PREFIX in os.environ: aliases = Config.parse_config(section='alias', config_path=os.environ['%s_ALIAS_PATH' % Config.PREFIX]) path = aliases[item] else: aliases = self.get_default_alias() if aliases is not None: path = aliases[item] else: raise AttributeError("%s not found in os.environ['%s'], os.environ['%s'], and os.environ['%s']" % (item, self.get_env(item), '%s_ALIAS_PATH' % Config.PREFIX, Config.LABEL_CONFIG_PATH)) tokens = path.split('.') module = '.'.join(tokens[:-1]) name = tokens[-1] return self.get_class(module, name)
def run(self, image: str, command: Optional[Union[str, list]] = None, path: Optional[str] = None, volumes: Optional[list] = None, environment: Optional[list] = None, **kwargs: Any) -> Union[Model, None, bytes]: if volumes is None: volumes = list() if environment is None: environment = list() _path = path if path else self.path if 'auto_remove' not in self.run_opt and 'auto_remove' not in kwargs: kwargs['auto_remove'] = True return self.docker.containers\ .run(image=image, command=command, volumes=volumes + ['%s:%s' % (os.path.abspath(_path), self.CONTAINER_WORK_DIR), '%s:%s' % (os.path.abspath(Config.get_log_dir()), self.CONTAINER_LOG_DIR)], environment=environment + ['%s=%s' % (Config.LABEL_LOGGER_LOG_DIR, self.CONTAINER_LOG_DIR)], working_dir=self.CONTAINER_WORK_DIR, name=id(self), **self.run_opt, **kwargs)
def init_config(cls, config_path: Optional[str] = None) -> None: cls.config = Config(config_path=config_path)