def finalize_system(session: HISSession, system: int, *, serial_number: str, operating_system: str, model: str, pubkey: str) -> bool: """Finalizes the system.""" json = {'system': system} if serial_number is not None: json['sn'] = serial_number if operating_system is not None: json['os'] = operating_system if model is not None: json['model'] = model if pubkey is not None: json['wg_pubkey'] = pubkey response = session.post(FINALIZE_URL, json=json) if response.status_code != 200: LOGGER.error('Could not finalize system.') LOGGER.debug(response.content) return False return True
def query_systems(account: str, passwd: str) -> list: """Query systems.""" LOGGER.debug('Querying systems.') with HISSession(account, passwd) as session: return session.get_json(SYSTEMS_URL)
def wrapper(*args, **kwargs) -> int: try: return_code = function(*args, **kwargs) except KeyboardInterrupt: LOGGER.error('Aborted by user.') return 1 return 0 if return_code is None else return_code
def main() -> None: """Runs the script.""" args = get_args() loglevel = DEBUG if args.debug else INFO if args.verbose else WARNING basicConfig(format=LOG_FORMAT, level=loglevel) LOGGER.info('Retrieving systems.') with ErrorHandler('Error during JSON data retrieval.'): systems = get_systems(args) for system in filter_systems(systems, args): print(system.get('id'))
def __exit__(self, _, value, __): """Handles login and download errors.""" if isinstance(value, LoginError): LOGGER.error('Error during login.') LOGGER.debug(value) exit(2) if isinstance(value, DownloadError): LOGGER.error(self.error_text) LOGGER.debug(value) exit(3)
def update_credentials(account: str | None, passwd: str | None = None) -> tuple[str, str]: """Reads the credentials for a HIS account.""" if not account: try: account = input('User name: ') except (EOFError, KeyboardInterrupt): print() LOGGER.error('Aborted by user.') exit(1) if not passwd: try: passwd = getpass('Password: '******'Aborted by user.') exit(1) return account, passwd
def systems_from_cache(args: Namespace) -> list: """Returns cached systems.""" if not args.cache_file.exists(): LOGGER.info('Initializing cache.') systems = query_systems(*update_credentials(args.user)) return cache_systems(systems, args) LOGGER.debug('Loading cache.') with args.cache_file.open('r') as file: cache = load(file) if timestamp := cache.get('timestamp'): timestamp = datetime.fromisoformat(timestamp) if timestamp + timedelta(hours=args.cache_time) > datetime.now(): return cache['systems'] LOGGER.info('Cache has expired.')
return cache_systems(systems, args) LOGGER.debug('Loading cache.') with args.cache_file.open('r') as file: cache = load(file) if timestamp := cache.get('timestamp'): timestamp = datetime.fromisoformat(timestamp) if timestamp + timedelta(hours=args.cache_time) > datetime.now(): return cache['systems'] LOGGER.info('Cache has expired.') else: LOGGER.warning('Corrupted cache.') return cache_systems(query_systems(*update_credentials(args.user)), args) def get_systems(args: Namespace) -> list: """Returns systems.""" if args.refresh: systems = query_systems(*update_credentials(args.user)) return cache_systems(systems, args) return systems_from_cache(args) def substr_ic_in(string: str, haystack: Iterable[str]) -> bool: