class TemplateGenerator(object): def __init__(self, path: str = None): self.config_options = Config(path) self.parser = XReader(path) self.logger = Logger( name='colstract-generator', log_file=self.config_options.log_file).get_logger() self.templates = self._templates() @staticmethod def _templates() -> list: path = pathlib.Path(__file__).parent / 'templates' result = [thing for thing in path.iterdir() if thing.is_file()] return result def generate(self): for item in self.templates: item: pathlib.Path self.logger.info(f"creating {item.name}") with open(item, 'r') as file: template_blank: str = file.read() result = template_blank.format( **self.parser.parsed_data, **self.parser.parsed_data['special'], **self.parser.parsed_data['colors']) output_dir = pathlib.Path( self.config_options.config.get('output_dir')) if not output_dir.exists(): self.logger.warning( f"{output_dir} not present, attempting to create it") pathlib.Path.mkdir(output_dir, parents=True) self.logger.info(f"Created {output_dir}") output_file = output_dir / item.name with open(str(output_file), 'w') as out_: out_.write(result) self.logger.info(f"Generated {output_file}") print(f"Generated {output_file}")
class WallSetter(object): def __init__(self, path: str = None): self.config_options = Config(path) self.walconfig = pathlib.Path(__file__).parent / 'walconfig' self.log_file = self.config_options.log_file self.logger = Logger(name='colstract-wallpaper_setter', log_file=self.log_file).get_logger() def wallpaper_apply(self, backend: str) -> int: """ Apply wallpaper using provided backend Currently only these backends are supported: (feh, nitrogen) :param backend: :return: """ if backend not in ('feh', 'nitrogen'): self.logger.error(f"{backend} is not supported as of this version") print(f"{backend} is not supported as of this version") return 1 self.logger.info(f"Using {backend} as backend for setting wallpaper") default_option = '--bg-fill' if backend == 'feh' else '--set-scaled' with open(self.walconfig / f'{backend}.json', 'r') as file: options: dict = json.loads(file.read()) self.logger.info( f" {backend} configuration file {backend}.json loaded") set_options = self.config_options.config.get('wallpaper_options').get( 'setter_option') if set_options is None: self.logger.warning( f"No setter options provided, using {default_option}") set_options = default_option elif set_options in options.get('options'): self.logger.info(f"Using {set_options} option to set wallpaper") pass else: self.logger.warning( f"Invalid parameter {set_options}. Using default option: {default_option}" ) set_options = default_option function_call = subprocess.run(['which', backend], capture_output=True) if function_call.returncode == 0: program_path = function_call.stdout.decode('utf-8').strip('\n') self.logger.info(f"{backend} located at {program_path}") else: self.logger.error(f"failed to locate program {backend}") print(f"failed to locate {backend}") return 1 self.logger.info( f"Calling: {program_path} {set_options} {self.config_options.config.get('wallpaper_options').get('path')}" ) output = subprocess.run([ program_path, set_options, self.config_options.config.get('wallpaper_options').get('path') ]).returncode if output == 0: self.logger.info("Wallpaper applied successfully") print("Wallpaper applied successfully") else: self.logger.error( f"Failed to apply wallpaper. Return Code: {output}") print(f"Failed to apply wallpaper with return code {output}") return 0
class Config(object): def __init__(self, path: str = None): self.default_path = pathlib.Path( os.environ['HOME']) / '.config' / 'colstract' / 'config.json' self.default_output = pathlib.Path( os.environ['HOME']) / '.cache' / 'colstract' self.default_xresources = pathlib.Path( os.environ['HOME']) / '.Xresources' self.log_file = pathlib.Path( os.environ['HOME'] ) / '.cache' / 'colstract' / 'log' / 'colstract.log' self.template = { 'output_dir': None, 'xresources_path': None, 'wallpaper_options': { "apply_wallpaper": False, "path": None, "setter": None, "setter_option": None }, 'reload_env': False } # logger self.logger = Logger(name='colstract-config', log_file=str(self.log_file)).get_logger() # config path if provided self.__path: str = path self.path = self._path() self.config = self._config() def _path(self) -> pathlib.Path: path_ = None if self.__path is None: self.logger.info( f'Using default path: {self.default_path} - no custom path provided' ) path_ = self.default_path elif self.__path is not None: path_ = pathlib.Path(self.__path) if not path_.exists(): self.logger.warning( f'{path_} does not exist. Using default path: {self.default_path}' ) path_ = self.default_path else: self.logger.info(f'Using custom config path {path_}') return path_ def _config(self) -> dict: if not self.path.exists(): self.logger.error(f'{self.path} does not exist') raise FileNotFoundError(f"{self.path} does not exist") with open(self.path, 'r') as file: data: dict = json.loads(file.read()) self.logger.info(f'config read from {self.path}') config_ = copy.deepcopy(self.template) config_['output_dir'] = data.get('output_dir') if config_['output_dir'] is None: self.logger.info( f'output directory not specified. Using default {self.default_output}' ) config_['output_dir'] = self.default_output config_['xresources_path'] = data.get('xresources_path') if config_['xresources_path'] is None: self.logger.info( f'xresources file not specified. Using default {self.default_xresources}' ) config_['xresources_path'] = self.default_xresources # wallpaper options logging config_['wallpaper_options'].update( data.get('wallpaper_options', self.template.get('wallpaper_options'))) # if it is to be applied if config_['wallpaper_options']['apply_wallpaper']: self.logger.info( 'apply_Wallpaper is True. Wallpaper will be applied if path is provided' ) # if path actually exists if pathlib.Path(str( config_['wallpaper_options']['path'])).exists(): self.logger.info(f'Wallpaper file exists') else: self.logger.error( f'Wallpaper file does not exist at {config_.get("wallpaper_options").get("path")}' ) else: self.logger.info( 'apply_wallpaper is False. Wallpaper will not be processed') config_['reload_env'] = data.get('reload_env', False) return config_