def _ensure_set_up(cfg: config.Config, db: database.Database): if db.needs_pulling(): raise CliException("No data found. Please run 'pull' first.") if len(cfg.update_command) == 0: raise CliException( "No update command found. Please set an update command in the config." )
def read_config(wd: WorkDir) -> config.Config: # load config file try: with open(wd.config_file) as config_file: config_dict = yaml.safe_load(config_file) except IOError as e: raise CliException(f"Failed to read config file: {e}") except yaml.YAMLError as e: raise CliException(f"Failed to parse config: {e}") # parse config data try: return config.CONFIG_SCHEMA.load(config_dict) except ValidationError as err: raise CliException(f"Failed to deserialize config: {err.messages}")
def _apply_filters( all_repositories: List[Tuple[FilterInfo, database.Repository]], filters: List[config.Filter], ) -> List[database.Repository]: # need to create a dict with (owner, name) as the key, as the type is not hashable selected_repositories: Dict[Tuple[str, str], Tuple[FilterInfo, database.Repository]] = {} if len(filters) == 0: return [repository for _filter_info, repository in all_repositories] for filter in filters: if filter.mode == config.FILTER_MODE_ADD: added_repositories = { (repository.owner, repository.name): (filter_info, repository) for filter_info, repository in all_repositories if _filter_matches(filter, filter_info) } selected_repositories.update(added_repositories) elif filter.mode == config.FILTER_MODE_REMOVE: selected_repositories = { (repository.owner, repository.name): (filter_info, repository) for filter_info, repository in selected_repositories.values() if not _filter_matches(filter, filter_info) } else: raise CliException( f"Unsupported filter.mode passed: {filter.mode}") return [ repository for _filter_info, repository in selected_repositories.values() ]
def write_database(wd: WorkDir, db: database.Database): # load database file try: data = database.DATABASE_SCHEMA.dump(db) with open(wd.database_file, "w") as database_file: json.dump(data, database_file, indent=4, sort_keys=True) except IOError as e: raise CliException(f"Failed to write database file: {e}")
def write_config(wd: WorkDir, cfg: config.Config): # load config file try: data = config.CONFIG_SCHEMA.dump(cfg) with open(wd.config_file, "w") as config_file: yaml.dump(data, config_file, default_flow_style=False) except IOError as e: raise CliException(f"Failed to write config file: {e}")
def read_database(wd: WorkDir) -> database.Database: if not wd.database_file.exists(): db = database.Database() return db # load database file try: with open(wd.database_file) as database_file: database_dict = json.load(database_file) except IOError as e: raise CliException(f"Failed to read database file: {e}") except json.JSONDecodeError as e: raise CliException(f"Failed to parse database: {e}") # parse database data try: return database.DATABASE_SCHEMA.load(database_dict) except ValidationError as err: raise CliException(f"Failed to deserialize database: {err.messages}")
def get_user(gh: Github) -> database.GitUser: gh_user = gh.get_user() login = gh_user.login # need to do this first to trigger lazy loading name = gh_user.name emails = gh_user.get_emails() primary_email = None for email in emails: if email.primary: primary_email = email.email break user_name = name or login if user_name is None or primary_email is None: raise CliException( "Please provide an API key with access to the user name and email address." ) user = database.GitUser(name=name or login, email=primary_email) return user