def test_load_data_files(self): completion_dir = find.find_data_directory('completion') self.assertIsNotNone(completion_dir, 'failed to find the \'completion\' directory') # validate that completion definitions claiming to be json are loadable as json for json_file in glob.glob(os.path.join(completion_dir, '*.json')): json_file = os.path.abspath(json_file) with open(json_file, 'r') as file_h: try: json_ex.load(file_h, strict=True) except Exception: self.fail("failed to load file '{0}' as json data".format(json_file))
def merge_config(self, config_file, strict=True): """ Merge the configuration information from the specified configuration file. Only keys which exist in the currently loaded configuration are copied over while non-existent keys are skipped. The contents of the new configuration overwrites the existing. :param bool strict: Do not try remove trailing commas from the JSON data. :param str config_file: The path to the configuration file to merge. """ with open(config_file, 'r') as tmp_file: config = json_ex.load(tmp_file, strict=strict) if not isinstance(config, dict): self.logger.error( "can not merge configuration file: {0} (invalid format)". format(config_file)) return self.logger.debug( 'merging configuration information from source file: ' + config_file) for key, value in config.items(): if not key in self.config: self.logger.warning( "skipped merging non-existent configuration key {0}". format(key)) continue self.config[key] = value return
def main(): parser = argparse.ArgumentParser(description='Stat File Verification', conflict_handler='resolve') parser.add_argument('-v', '--version', action='version', version='%(prog)s Version: ' + __version__) parser.add_argument('data_file', type=argparse.FileType('r'), help='the stats file to verify') arguments = parser.parse_args() try: data = json_ex.load(arguments.data_file) except Exception: color.print_error('failed to load the data') return 1 else: color.print_status('loaded the statistics data') finally: arguments.data_file.close() if not verify_signature(data): color.print_error('the signature is invalid') return 1 color.print_good('the signature is valid') return 0
def do_config_load(self, load_defaults): """ Load the client configuration from disk and set the :py:attr:`~.KingPhisherClientApplication.config` attribute. :param bool load_defaults: Load missing options from the template configuration file. """ self.logger.info('loading the config from disk') client_template = find.find_data_file('client_config.json') config_file = os.path.expanduser(self.config_file) with open(config_file, 'r') as tmp_file: self.config = json_ex.load(tmp_file) if load_defaults: with open(client_template, 'r') as tmp_file: client_template = json_ex.load(tmp_file) for key, value in client_template.items(): if not key in self.config: self.config[key] = value
def __init__(self): super(CustomCompletionProviderBase, self).__init__() self.logger = logging.getLogger('KingPhisher.Client.' + self.__class__.__name__) if self.data_file is not None: completion_data = find.find_data_file(os.path.join('completion', self.data_file)) if completion_data is None: raise RuntimeError("failed to find completion data file '{0}'".format(self.data_file)) self.logger.debug("loading {0} completion data from: {1}".format(self.name, completion_data)) with open(completion_data, 'r') as file_h: completion_data = json_ex.load(file_h) self.load_data(completion_data)
def __init__(self, file_name, mode): """ :param str file_name: The path to the file to open as an archive. :param str mode: The mode to open the file such as 'r' or 'w'. """ self._mode = mode + ':bz2' self.file_name = file_name epoch = datetime.datetime.utcfromtimestamp(0) self.mtime = (datetime.datetime.utcnow() - epoch).total_seconds() self._tar_h = tarfile.open(file_name, self._mode) if 'r' in mode and self.has_file(self.metadata_file_name): self.metadata = json_ex.load(self.get_file(self.metadata_file_name)) else: self.metadata = {} if 'w' in mode: self.metadata['timestamp'] = datetime.datetime.utcnow().isoformat() self.metadata['version'] = version.version
def __init__(self, file_name, mode): """ :param str file_name: The path to the file to open as an archive. :param str mode: The mode to open the file such as 'r' or 'w'. """ self._mode = mode + ':bz2' self.file_name = file_name epoch = datetime.datetime.utcfromtimestamp(0) self.mtime = (datetime.datetime.utcnow() - epoch).total_seconds() self._tar_h = tarfile.open(file_name, self._mode) if 'r' in mode and self.has_file(self.metadata_file_name): self.metadata = json_ex.load(self.get_file( self.metadata_file_name)) else: self.metadata = {} if 'w' in mode: self.metadata['timestamp'] = datetime.datetime.utcnow().isoformat() self.metadata['version'] = version.version
def merge_config(self, config_file, strict=True): """ Merge the configuration information from the specified configuration file. Only keys which exist in the currently loaded configuration are copied over while non-existent keys are skipped. The contents of the new configuration overwrites the existing. :param bool strict: Do not try remove trailing commas from the JSON data. :param str config_file: The path to the configuration file to merge. """ with open(config_file, 'r') as tmp_file: config = json_ex.load(tmp_file, strict=strict) if not isinstance(config, dict): self.logger.error("can not merge configuration file: {0} (invalid format)".format(config_file)) return self.logger.debug('merging configuration information from source file: ' + config_file) for key, value in config.items(): if not key in self.config: self.logger.warning("skipped merging non-existent configuration key {0}".format(key)) continue self.config[key] = value return