def __init__(self, config_module, config_path): assert isinstance(config_path, str) self.root = None self.config_path = c_path.normalize(config_path) logger.debug2('Checking config file existence at ' + self.config_path) if not c_path.validate_file(self.config_path): raise RuntimeError('Cannot read config file at ' + self.config_path) logger.debug2( 'Using generateDs APIs to parse config and validate syntax') try: doc = config_module.parsexml_(self.config_path) rootNode = doc.getroot() rootClass = config_module.get_root_tag(rootNode)[1] self.root = rootClass.factory() self.root.build(rootNode) except Exception as e: raise type(e), type(e)('Error parsing config file: ' + str(e) + '\n' ' ' + 'config_path: ' + self.config_path), sys.exc_info()[2] doc = None logger.debug2('Parsed config using generateDs')
def enable_file_logging(self, toolname, num_logs=50, log_dir=None): """ Find a tmp location for file logging """ from c_misc import backup_file import c_path if log_dir is None: log_dir = '' for tmp_path in [os.getenv('TEMP'), os.getenv('TMP')]: if c_path.validate_dir(tmp_path): log_dir = c_path.join(tmp_path, '{0}_logs'.format(toolname)) break else: self.warning( 'CANNOT FIND A LOCAL TEMP DIRECTORY TO CREATE LOGS') # Set Log Folder flids = [] if log_dir: log_dir = c_path.normalize(log_dir) log_file = c_path.join(log_dir, '{0}_log.txt'.format(toolname)) log_file_debug = c_path.join(log_dir, '{0}_log_debug.txt'.format(toolname)) # Create the logs directory retval, reterr = True, '' try: c_path.create_dir(log_dir) except Exception as e: retval, reterr = False, str(e) if not retval: self.warning( 'CANNOT CREATE DIRECTORY FOR LOGGING FILE: {0}\nPath: {1}'. format(reterr, log_dir)) elif num_logs > 1 and c_path.validate_file( log_file) and not backup_file(log_file, num_logs): self.warning( 'FAILED TO BACKUP LOGGING FILE: \nPath: {0}'.format( log_file)) elif num_logs > 1 and c_path.validate_file( log_file_debug) and not backup_file( log_file_debug, num_logs): self.warning( 'FAILED TO BACKUP DEBUG LOGGING FILE: \nPath: {0}'.format( log_file_debug)) else: flids.append(self.add_file_logger(log_file, self.INFO, 'w')) self.info('Logging to {0}'.format(log_file)) # Only create debug file if debug is enabled if self.verbosity < self.INFO: flids.append( self.add_file_logger(log_file_debug, self.verbosity, 'w')) self.debug('Debug logging to {0}'.format(log_file_debug)) return flids
def __init__(self, config_path): assert isinstance(config_path, str) self.root = None self.config_path = c_path.normalize(config_path) logger.debug2('Checking config file existence at ' + self.config_path) if not c_path.validate_file(self.config_path): raise RuntimeError('Cannot read config file at ' + self.config_path) logger.debug2('Using JSON module to parse the config file.') try: with open(config_path, 'r') as fp: self.root = self.SelfGeneratingClass('Root', json.load(fp, object_hook=_json_str_hook)) except Exception as e: raise type(e), type(e)('Error parsing config file: ' + str(e) + '\n' ' ' + 'config_path: ' + self.config_path), sys.exc_info()[2]