#!/usr/bin/env python3 import json import os import logging from types import SimpleNamespace from huami_token import HuamiAmazfit class ConfigHandler: def __init__(self, args, config_path='./config.json'): self.config_path = config_path new_config = self.load_config() config_change = False if args.logfile: self.config.logfile = args.logfile config_change = True if hasattr(self.config, 'logfile'): logging.basicConfig(filename=self.config.logfile, level=logging.DEBUG if args.verbose else logging.INFO) else: logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) if not hasattr(self.config, 'credentials'): self.config.credentials = SimpleNamespace() config_change = True if not hasattr(self.config.credentials, 'email'): self.config.credentials.email = input('Username/Email: ') config_change = True if not hasattr(self.config.credentials, 'password'): self.config.credentials.password = getpass('Password: ') config_change = True if args.targets: self.config.targets = args.targets config_change = True elif not args.use_config: self.config.targets = [] if args.target_dir: self.config.target_dir = os.path.abspath(args.target_dir) config_change = True elif not self.config.target_dir: self.config.target_dir = '' if config_change and (new_config or args.save): save_config() @property def target_dir(self): return self.config.target_dir @target_dir.setter def target_dir(self, value): self.config.target_dir = value def load_config(self): try: with open(self.config_path) as config_file: self.config = json.load(config_file, object_hook=lambda d: SimpleNamespace(**d)) return False except IOError: self.config = SimpleNamespace() return True def save_config(self): log.debug('Saving config') with open(self.config_path, 'w') as config_file: json.dump(self.config, config_file, default=lambda x: vars(x)) def configureHuamiDevice(self): huamidevice = HuamiAmazfit(email=self.config.credentials.email, password=self.config.credentials.password) huamidevice.method = 'amazfit' if hasattr(self.config, 'targets') and self.config.targets: huamidevice.agps_packs = dict(filter(lambda entry: entry[0] in self.config.targets, huamidevice.AGPS_PACKS.items())) return huamidevice