def reset_default_values(self): """Resets default values for paths or other elements which are not empty strings""" OUTPUT.info( 'Resetting path elements in the loaded configuration file.') for key, val in self.configdefaults.items(): if val: self.set_value(key, val)
def merge_default_values(self): """Merge in default values if they don't already exist or are set to none""" OUTPUT.info( 'Updating existing configuration item that are not yet set to the default values.', suppress=True) for key, val in self.configdefaults.items(): if self.get_value(key) is None: self.set_value(key, val)
def _add_log(self, mylog, logtype='info'): """Add a log generated from this module""" if logtype == 'error': OUTPUT.error('{0}: {1}'.format(str(self.__class__.__name__), str(mylog))) elif logtype == 'warning': OUTPUT.warning('{0}: {1}'.format(str(self.__class__.__name__), str(mylog))) else: OUTPUT.info('{0}: {1}'.format(str(self.__class__.__name__), str(mylog)))
def connect_session(self): """Connect with current profile or id/secret""" try: if self.profileid: OUTPUT.info('Using passed in profile - {0}'.format( self.profileid)) self.session = boto3.Session(profile_name=self.profileid, region_name=self.region) else: self._add_log('Connecting to AWS with key id - {0}'.format( self.awsid)) self.session = boto3.Session(aws_access_key_id=self.awsid, aws_secret_access_key=self.secret, region_name=self.region) except ClientError as e: self._add_log(e, 'error') raise e
def __init__(self, *args, **kwargs): # Default global configuration settings for a project self.configdefaults = DEFAULTAPPCONFIG.copy() self.configfile = kwargs.pop('config_file', '') try: self.scriptpath = os.path.abspath(os.path.split(__file__)[0]) except: self.scriptpath = '' if not self.configfile: self.configfile = os.path.join(self.scriptpath, ('config' + os.sep + 'config.yml')) # Initialize the data dictionary for the default settings self._defaults = {} for key, value in self.configdefaults.items(): self._defaults[key] = value # If there is no default config file then lets try to create one if not os.path.isfile(self.configfile): OUTPUT.info( 'Unable to find config file - {0}, initializing a new default config file instead.' .format(self.configfile), suppress=True) with open(self.configfile, "wb") as cfgfile: cfgfile.write( yaml.safe_dump(self._defaults, encoding='utf-8', allow_unicode=True, default_flow_style=False)) OUTPUT.info('File saved!') # pass through all args to the base class init along with our config file path kwargs['config_file'] = self.configfile super(Settings, self).__init__(*args, **kwargs) # Merge any default values that may be missing self.merge_default_values() self.emailhandler = self.get_emailhandler()
def render_jinja_template(self, data, template): template = template + '.{0}'.format(self._template_extension) OUTPUT.info('Rendering template: {0}'.format(template)) text = self._env.get_template(template) msg = text.render(data) return msg