def config_profile(): """Create config file for jsoncolor and set default color scheme.""" with jsonconfig.Config('jsoncolor') as cfg: cfg.data = CONFIG cfg.kwargs['dump']['indent'] = 4 print('JSON Color configuration file created: ' + cfg.filename, file=sys.stderr)
def integration_test_configured(msgtype): """ Does the user have an 'integration_tester' config profile set up, and do they have 'msgtype' set up in that profile? """ with jsonconfig.Config('messages') as cfg: data = cfg.data return ('integration_tester' in cfg.data.keys() and msgtype in data['integration_tester'])
def set_default_profile(profile): """ Sets the default profile to use in the messages config.json file. Args: :profile: (str) name of existing profile to use """ with jsonconfig.Config('messages') as cfg: if profile not in cfg.data: raise UnknownProfileError(profile) cfg.data['default'] = profile cfg.kwargs['dump']['indent'] = 4
def configure_profile(msg_type, profile_name, data, auth): """ Create the profile entry. Args: :msg_type: (str) message type to create config entry. :profile_name: (str) name of the profile entry :data: (dict) dict values for the 'settings' :auth: (dict) auth parameters """ with jsonconfig.Config("messages", indent=4) as cfg: write_data(msg_type, profile_name, data, cfg) write_auth(msg_type, profile_name, auth, cfg) print("[+] Configuration entry for <" + profile_name + "> created.") print("[+] Configuration file location: " + cfg.filename)
def configure(msg, params, to_save, credentials): """ Get default values and credentials from a configuration profile. If not created, and save=True in the msg class parameter, then configuration file is created. Args: :msg: (Message class) an instance of a message class :params: (dict) dict of params passed to message classes' __init__ method :to_save: (set or list) list of default values to save (from address, server name, etc.) :credentials: (set or list) list of credentials (password, auth_tokens, etc.) """ msg_type = msg.__class__.__name__.lower() defaults = set(to_save) creds = set(credentials) with jsonconfig.Config('messages') as cfg: if params['profile'] is None: msg.profile = 'default_user' else: msg.profile = params['profile'] if msg.profile not in cfg.data: cfg.data[msg.profile] = {} if msg_type not in cfg.data[msg.profile]: cfg.data[msg.profile][msg_type] = {} for d in defaults: setattr(msg, d, cfg.data[msg.profile][msg_type].get(d, params[d])) for c in creds: setattr( msg, c, (params[c] or cfg.pwd.get(msg.profile + '_' + msg_type, None))) if getattr(msg, c) is None: setattr(msg, c, getpass('\n' + c + ': ')) if params['save']: for d in defaults: cfg.data[msg.profile][msg_type][d] = getattr(msg, d) for c in creds: cfg.pwd[(msg.profile + '_' + msg_type)] = getattr(msg, c) cfg.kwargs['dump']['indent'] = 4
def get_color_style(all_colors=False): """ Gets color style from jsoncolor config file. Args: all_colors (boolean): return singleton value of all values Returns: default color scheme if all_colors=False dict of all preset colors if all_colors=True """ with jsonconfig.Config('jsoncolor', 'r') as cfg: colors = cfg.data if not all_colors: return colors['styles'][colors['default']] else: return colors['styles']
def check_config_file(msg): """ Checks the config.json file for default settings and auth values. Args: :msg: (Message class) an instance of a message class. """ with jsonconfig.Config("messages", indent=4) as cfg: verify_profile_name(msg, cfg) retrieve_data_from_config(msg, cfg) if msg._auth is None: retrieve_pwd_from_config(msg, cfg) if msg.save: update_config_data(msg, cfg) update_config_pwd(msg, cfg)
def create_config(msg_type, profile, params): """ Creates an entry in the config.json file for a specified message type. To be used via the cli module. Args: :msg_type: (str) the type of message, i.e. email, twilio, etc. :profile: (str) profile name to save params under. :params: (dict) the params to save in the config file. """ with jsonconfig.Config('messages') as cfg: if profile not in cfg.data: cfg.data[profile] = {} if msg_type not in cfg.data[profile]: cfg.data[profile][msg_type] = {} for d in params['defaults']: cfg.data[profile][msg_type][d] = input('Enter ' + d + ': ') for c in params['credentials']: cfg.pwd[(profile + '_' + msg_type)] = getpass('\n' + c + ': ') cfg.kwargs['dump']['indent'] = 4
def integration_test_configured(msgtype): with jsonconfig.Config('messages') as cfg: data = cfg.data return ('integration_tester' in data.keys() and msgtype in data['integration_tester'])