def save_config_file(): f = open('/app/config/app_config.ini', 'w') try: config.write(f) logging.debug("Configuration file successfully saved") finally: f.close()
def launch(hardware): from hedgehog.server.hardware.simulated import SimulatedHardwareAdapter simulator = hardware == SimulatedHardwareAdapter args = parse_args(simulator) if args.logging_conf: logging.config.fileConfig(args.logging_conf) if simulator and args.simulate_sensors: _hardware = hardware def hardware(*args, **kwargs): return _hardware(*args, simulate_sensors=True, **kwargs) config = configparser.ConfigParser() config.read(args.config_file) if args.scan_config and os.path.isfile(args.scan_config_file): scan_config = configparser.ConfigParser() scan_config.read(args.scan_config_file) apply_scan_config(config, scan_config) with open(args.config_file, mode='w') as f: config.write(f) port = args.port or config.getint('default', 'port', fallback=0) with suppress(KeyboardInterrupt): start(hardware, port)
def __parseCfgOptions(self): # Is there a configuration available? configDir = self.get('core.config') configFiles = self.__getCfgFiles(os.path.join(configDir, "config.d")) configFiles.insert(0, os.path.join(configDir, "config")) config = configparser.RawConfigParser() filesRead = config.read(configFiles) # Bail out if there's no configuration file if not filesRead: raise ConfigNoFile( "No usable configuration file (%s/config) found!" % configDir) # Walk thru core configuration values and push them into the registry for section in config.sections(): if not section in self.__registry: self.__registry[section] = {} self.__registry[section].update(config.items(section)) # Initialize the logging module on the fly try: tmp = StringIO() config.write(tmp) tmp2 = StringIO(tmp.getvalue()) logging.config.fileConfig(tmp2) except configparser.NoSectionError: logging.basicConfig( level=logging.ERROR, format='%(asctime)s (%(levelname)s): %(message)s')
def check_profile_config(config): """ Validation checks on the profile config. At least one profile must exist (otherwise exit) and the paths for each profile should exist, otherwise the profile entry is removed. Args: config (ConfigObj): The ConfigObj instance. """ # Check that at least one profile exists if 'profiles' not in config: print('There are currently no profiles. Use "sm_profile ' '-c <profile>" to create one.') sys.exit(1) # Check that the paths for each profile exist for profile in config['profiles'].keys(): data_exists = os.path.isdir(config['profiles'][profile]['data_path']) delete_profile = False if not data_exists: print('Data path for profile %s does not exist.' % profile) delete_profile = True install_exists = os.path.isdir( config['profiles'][profile]['install_path']) if not install_exists: print('Install path for profile %s does not exist.' % profile) delete_profile = True if delete_profile: print(' Deleting profile %s.' % profile) del config['profiles'][profile] config.write() return config
def __parseCfgOptions(self): # Is there a configuration available? configDir = self.get('core.config') configFiles = self.__getCfgFiles(os.path.join(configDir, "config.d")) configFiles.insert(0, os.path.join(configDir, "config")) config = ConfigParser.RawConfigParser() filesRead = config.read(configFiles) # Bail out if there's no configuration file if not filesRead: raise ConfigNoFile("No usable configuration file (%s/config) found!" % configDir) # Walk thru core configuration values and push them into the registry for section in config.sections(): if not section in self.__registry: self.__registry[section] = {} self.__registry[section].update(config.items(section)) # Initialize the logging module on the fly try: tmp = StringIO() config.write(tmp) tmp2 = StringIO(tmp.getvalue()) logging.config.fileConfig(tmp2) except ConfigParser.NoSectionError: logging.basicConfig(level=logging.ERROR, format='%(asctime)s (%(levelname)s): %(message)s')
def check_config_file(filename='config.ini'): """ checks if config.ini file exists and creates a template config file if not :param filename: filename of config file that is returned :return: """ if not os.path.exists(filename): config = configparser.ConfigParser() config['TWITTER'] = { 'consumer_key': 'your consumer key here', 'consumer_secret': 'your consumer secret here', 'access_token': 'your access token here', 'access_token_secret': 'your access token secret here' } config['REDDIT'] = { 'client_id': 'your client_id here', 'client_secret': 'your client_secret here', 'user_agent': 'your user_agent here' } with open(filename, 'w') as configfile: config.write(configfile) logger.info( 'I have created a config.ini file for u. ' 'Please fill in ur twitter and reddit credensh to proceed.') sys.exit()
def configureAPRS(args, aprsConfigPath): ''' Configure aprs configuration file from command line :param args: argparse arguments :param aprsConfigPath: Path to aprs.ini file :return: None ''' config = ConfigParser.RawConfigParser() config.read(os.path.join(path, "aprs.ini")) if args.callsign is not None: config.set('APRSIS', 'CALLSIGN', args.callsign) if args.server is not None: config.set('APRSIS', 'SERVER', args.server) if args.port is not None: config.set('APRSIS', 'PORT', args.port) if args.rate is not None: config.set('APRSIS', 'RATE', args.rate) if args.stationsage is not None: config.set('APRSIS', 'STATIONSAGE', args.stationsage) if args.comment is not None: config.set('APRS', 'COMMENT', args.comment[:43]) if args.altcomment is not None: config.set('APRS', 'ALTCOMMENT', args.altcomment[:43]) with open(aprsConfigPath, 'wb') as configfile: config.write(configfile)
def __init__(self, args, config): self.args = args self.config = config self.model_dir = utils.get_model_dir(config) self.cache_dir = utils.get_cache_dir(config) self.category = utils.get_category(config, self.cache_dir) self.anchors = torch.from_numpy(utils.get_anchors(config)).contiguous() logging.info('use cache directory ' + self.cache_dir) logging.info('tensorboard --logdir ' + self.model_dir) if args.delete: logging.warning('delete model directory: ' + self.model_dir) shutil.rmtree(self.model_dir, ignore_errors=True) os.makedirs(self.model_dir, exist_ok=True) with open(self.model_dir + '.ini', 'w') as f: config.write(f) self.saver = utils.train.Saver(self.model_dir, config.getint('save', 'keep')) self.timer_save = utils.train.Timer(config.getfloat('save', 'secs'), False) try: self.timer_eval = utils.train.Timer( eval(config.get('eval', 'secs')), config.getboolean('eval', 'first')) except configparser.NoOptionError: self.timer_eval = lambda: False self.summary_worker = SummaryWorker(self) self.summary_worker.start()
def write(): if not os.path.isdir(os.path.dirname(configs_found[-1])): os.mkdir(os.path.dirname(configs_found[-1])) f = open(configs_found[-1], 'w') config.write(f) f.close()
def try_enroll_in_operation_mode(device_id, dev): enroll_token = get_enroll_token() if enroll_token is None: return logger.info("Enroll token found. Trying to automatically enroll the node.") setup_endpoints(dev) response = mtls_request('get', 'claimed', dev=dev, requester_name="Get Node Claim Info") if response is None or not response.ok: logger.error('Did not manage to get claim info from the server.') return logger.debug("[RECEIVED] Get Node Claim Info: {}".format(response)) claim_info = response.json() if claim_info['claimed']: logger.info('The node is already claimed. No enrolling required.') else: claim_token = claim_info['claim_token'] if not enroll_device(enroll_token, claim_token, device_id): logger.error('Node enrolling failed. Will try next time.') return logger.info("Update config...") config = configparser.ConfigParser() config.read(INI_PATH) config.remove_option('DEFAULT', 'enroll_token') with open(INI_PATH, 'w') as configfile: config.write(configfile) os.chmod(INI_PATH, 0o600)
def index(): username = request.cookies.get('username') if username: logging.info("{} accessing the page".format(username)) else: logging.info("Unknown user accessing the page") config.read(status_file_path) error = False version = subprocess.check_output(["git", "describe", "--always"]).strip().decode("utf-8") if request.method == "POST": if request.form["action"] == "login": username = request.form.get('username') logging.info("Logged in with {}".format(username)) resp = make_response(redirect('/')) resp.set_cookie('username', username) return resp if request.form["action"] == "logout": logging.info("{} Logging out".format(username)) resp = make_response(redirect('/')) resp.delete_cookie('username') return resp if request.form["action"] == "release": logging.info("Attempting to set cow free status") logging.info("Setting cow free status: True") config["Cow"]["Free"] = "True" config["Cow"]["Owner"] = "" with open(status_file_path, "w") as configfile: config.write(configfile) if request.form["action"] == "acquire": logging.info("Attempting to set cow free status") if config["Cow"]["Free"] == "False": logging.info("Cow already acquired by {}".format( config["Cow"]["Owner"])) error = True else: logging.info("Setting cow free status: False") config["Cow"]["Free"] = "False" config["Cow"]["Owner"] = username with open(status_file_path, "w") as configfile: config.write(configfile) if username: return render_template('index.html', version=version, username=username, cow_free=config["Cow"]["Free"] == "True", cow_owner=config["Cow"]["Owner"], error=error) return render_template('index.html', version=version, cow_free=config["Cow"]["Free"] == "True", cow_owner=config["Cow"]["Owner"])
def create_default_config(config): config['DEFAULT'] = { 'crontab_path': 'crontab', 'logger_config': 'logger_config.conf', 'logger_name': 'logger' } with open(config_file, 'w') as configfile: config.write(configfile)
def gtk_main_quit(self, window): """ Quit the application """ Gtk.main_quit() # Save current configuration with open(CONFIG_FILE, 'wb') as configfile: config.write(configfile) log.debug("Config written to: %s" %CONFIG_FILE)
def gtk_main_quit(self, window): """ Quit the application """ Gtk.main_quit() # Save current configuration with open(CONFIG_FILE, 'wb') as configfile: config.write(configfile) log.debug("Config written to: %s" % CONFIG_FILE)
def run_tribler_core_session(api_port, api_key, state_dir, gui_test_mode=False): """ This method will start a new Tribler session. Note that there is no direct communication between the GUI process and the core: all communication is performed through the HTTP API. """ logger.info(f'Start tribler core. API port: "{api_port}". ' f'API key: "{api_key}". State dir: "{state_dir}". ' f'Core test mode: "{gui_test_mode}"') config = TriblerConfig.load(file=state_dir / CONFIG_FILE_NAME, state_dir=state_dir, reset_config_on_error=True) config.gui_test_mode = gui_test_mode if SentryReporter.is_in_test_mode(): default_core_exception_handler.sentry_reporter.global_strategy = SentryStrategy.SEND_ALLOWED config.api.http_port = int(api_port) # If the API key is set to an empty string, it will remain disabled if config.api.key not in ('', api_key): config.api.key = api_key config.write( ) # Immediately write the API key so other applications can use it config.api.http_enabled = True priority_order = config.resource_monitor.cpu_priority set_process_priority(pid=os.getpid(), priority_order=priority_order) # Enable tracer if --trace-debug or --trace-exceptions flag is present in sys.argv log_dir = config.general.get_path_as_absolute('log_dir', config.state_dir) trace_logger = check_and_enable_code_tracing('core', log_dir) logging.getLogger('asyncio').setLevel(logging.WARNING) if sys.platform.startswith('win'): # TODO for the moment being, we use the SelectorEventLoop on Windows, since with the ProactorEventLoop, ipv8 # peer discovery becomes unstable. Also see issue #5485. asyncio.set_event_loop(asyncio.SelectorEventLoop()) loop = asyncio.get_event_loop() exception_handler = default_core_exception_handler loop.set_exception_handler(exception_handler.unhandled_error_observer) try: loop.run_until_complete( core_session(config, components=list(components_gen(config)))) finally: if trace_logger: trace_logger.close() # Flush the logs to the file before exiting for handler in logging.getLogger().handlers: handler.flush()
def set_server(self, ip, port): config = self._config if 'SERVER' not in config.keys(): config['SERVER'] = {} config['SERVER']['ip'] = ip config['SERVER']['port'] = str(port) with open(self.filename, 'w') as f: config.write(f)
def create_default_configuration(cls, default_profile_url): """ensure a default profile is configured.""" config_directory = cls._ensure_configuration_directory() config = ConfigParser() config.add_section('default') config.set('default', 'repository', default_profile_url) with open(os.path.join(config_directory, 'profiles'), 'w') as profiles_file: config.write(profiles_file) return cls()
def log_location_update(): config = configparser.ConfigParser() config.read(f"{Common.config_path}/logger.ini") config.set( "handler_fileHandler", "args", f"('{Common.report_path}/Automation.log', 'a')", ) with open(f"{Common.config_path}/logger.ini", "w") as f: config.write(f)
def set_mrouter(self, id, ip, port): config = self._config if 'MROUTER' not in config.keys(): config['MROUTER'] = {} config['MROUTER']['id'] = id config['MROUTER']['ip'] = ip config['MROUTER']['port'] = str(port) with open(self.filename, 'w') as f: config.write(f)
def config_write(config): os.system("cp %s %s" % (sololink_conf, sololink_back)) os.system("md5sum %s > %s.md5" % (sololink_back, sololink_back)) os.system("sync") f = open(sololink_conf, 'wb') config.write(f) f.close() os.system("md5sum %s > %s.md5" % (sololink_conf, sololink_conf)) os.system("sync") os.system("rm %s %s.md5" % (sololink_back, sololink_back))
def _create_log_config(self, config_filename, log_filename, level): template_fname = self.get_template_fname() template = open(template_fname) config = open(config_filename, 'w') config.truncate() for line in template: config.write( line.replace('@@LEVEL@@', level).replace('@@FILENAME@@', log_filename)) config.close() template.close()
def set_config(cls, key, value, config_file=None): if config_file is None: config_file = cls.def_config_file if key not in cls.allowed_configuration_keys: raise PowergloveError('%r not an allowed configuration key. Possible values are %s' % (key, ', '.join(cls.allowed_configuration_keys))) config = configobj.ConfigObj(config_file) config[key] = value config.write()
def _setup_system(self): config_dir = utils.Utils().get_app_dir() config_path = os.path.join(config_dir, '.guardiancl.ini') if not os.path.exists(config_path): config = configparser2.ConfigParser() config.add_section('ROUTES') config.set('ROUTES', 'auth', 'http://guardiaocloud.com.br/service/v1/authenticate') config.set('ROUTES', 'devices', 'http://guardiaocloud.com.br/service/v1/devices') config.set('ROUTES', 'collect', 'http://guardiaocloud.com.br/collect') with open(config_path, 'w') as configfile: config.write(configfile)
def writeConfig(self): ''' This function creates / saves log settings to a file. ''' config = ConfigParser.ConfigParser() config.add_section('loggers') config.set('loggers', 'keys', 'root') config.add_section('formatters') config.set('formatters', 'keys', 'basic,nix') config.add_section('handlers') config.set('handlers', 'keys', 'consoleHandler,syslogHandler') config.add_section('logger_root') config.set('logger_root', 'level', 'DEBUG') config.set('logger_root', 'handlers', 'consoleHandler') config.add_section('handler_consoleHandler') config.set('handler_consoleHandler', 'class', 'StreamHandler') config.set('handler_consoleHandler', 'level', 'NOTSET') config.set('handler_consoleHandler', 'formatter', 'basic') config.set('handler_consoleHandler', 'args', '(sys.stdout,)') config.add_section('handler_syslogHandler') config.set('handler_syslogHandler', 'level', 'NOTSET') if sys.platform.startswith('linux'): config.set('handler_syslogHandler', 'class', 'handlers.SysLogHandler') config.set('handler_syslogHandler', 'formatter', 'nix') config.set('handler_syslogHandler', 'args', "("+self.getLogPath()+", handlers.SysLogHandler.LOG_USER)") elif sys.platform == 'win32' or sys.platform == 'cygwin': config.set('handler_syslogHandler', 'class', 'handlers.NTEventLogHandler') config.set('handler_syslogHandler', 'formatter', 'nix') config.set('handler_syslogHandler', 'args', "('Freeseer', '', 'Application')") elif sys.platform =='darwin': config.set('handler_syslogHandler', 'class', 'handlers.SysLogHandler') config.set('handler_syslogHandler', 'formatter', 'nix') config.set('handler_syslogHandler', 'args', "("+self.getLogPath()+", handlers.SysLogHandler.LOG_USER)") #config.set('handler_syslogHandler', 'args', "(('/dev/log'), handlers.SysLogHandler.LOG_USER)") else: pass # Unsupported config.add_section('formatter_basic') config.set('formatter_basic', 'format', '%(asctime)s freeseer: <%(levelname)s> %(message)s') config.set('formatter_basic', 'datefmt', '%Y-%m-%d %H:%M:%S') config.add_section('formatter_nix') config.set('formatter_nix', 'format', 'freeseer: <%(levelname)s> %(message)s') # Save default settings to new config file with open(self.logconf, 'w') as configfile: config.write(configfile)
def write_config(albumNum, trackNum): # Add content to the file config = ConfigParser.ConfigParser() config.add_section('cdc') config.set('cdc', 'album', albumNum) config.set('cdc', 'track', trackNum) # Create the configuration file as it doesn't exist yet with open(CONFIG, 'w') as file: config.write(file) logger.info('write config album: {}, track: {}'.format(albumNum, trackNum)) return
def write_config_to_filename(config_dictionary, config_filename): config = SafeConfigParser() for section in config_dictionary: config.add_section(section) variables = config_dictionary[section] for v in variables: config.set(section, v, variables[v]) mode = stat.S_IRUSR | stat.S_IWUSR flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL fd = os.open(config_filename, flags, mode) f = os.fdopen(fd, 'wb') config.write(f) f.close()
def write_config(cls, config: configparser.ConfigParser, identifier: str, prefix="configs_byserial"): """ writes a configuration file to an correct config file path. :param config: configuration file (configparser object) :type identifier: str :param identifier: identifier to user as the raget file name. :return: configparser object """ path = SysUtil.identifier_to_ini(identifier, prefix=prefix) with open(path, 'w+') as configfile: config.write(configfile) return config
def write_light_config(cls, config: configparser.ConfigParser, identifier: str): """ writes a configuration file to an correct config file path. :param config: configuration file (configparser object) :param identifier: identifier of the light. :type identifier: str :return: configparser object """ path = SysUtil.light_identifier_to_ini(identifier) with open(path, 'w+') as configfile: config.write(configfile) return config
def writeDefaultConfig(): logger.debug('No config found. Writing default config to server.cfg. Please adapt accordingly and restart the server.') config = configparser.ConfigParser() config['Server'] = { 'host': 'localhost', 'port': '8080' } config['HueBridge'] = { 'ip': '192.168.0.x', 'user': '******' } with open('server.cfg', 'w') as configfile: config.write(configfile)
def set_config(cls, key, value, config_file=None): if config_file is None: config_file = cls.def_config_file if key not in cls.allowed_configuration_keys: raise PowergloveError( '%r not an allowed configuration key. Possible values are %s' % (key, ', '.join(cls.allowed_configuration_keys))) config = configobj.ConfigObj(config_file) config[key] = value config.write()
def read_or_create_config_file( path_to_configfile: Path) -> configparser.ConfigParser: """ Creates config file with default values or reads the current config file "config.ini". """ config = configparser.ConfigParser(allow_no_value=True) config.optionxform = str if not path_to_configfile.is_file(): config.add_section("Basics") config.set("Basics", "# Settings related to the playfield window") config.set("Basics", "columns", "10") config.set("Basics", "rows", "24") config.set("Basics", "fraction_of_vres", "30") config.set("Basics", "x_position", "26") config.set("Basics", "y_position", "3") config.set("Basics", "spawn_row", "3") config.set("Basics", "spawn_column", "4") config.add_section("Fonts") config.set("Fonts", "# Path to the fontfile") config.set("Fonts", "file", "fonts/codeman38_deluxefont/dlxfont.ttf") config.set("Fonts", "# Default font size") config.set("Fonts", "size_fraction_of_vres", "67.5") config.add_section("Game_window") config.set("Game_window", "# Game Window Title") config.set("Game_window", "title", "Pygtris") config.set("Game_window", "# Initial Game Window Position") config.set("Game_window", "horizontal", "0") config.set("Game_window", "vertical", "0") config.set("Game_window", "# Game Window Size") config.set("Game_window", "width", "1920") config.set("Game_window", "height", "1080") config.add_section("Technical") config.set("Technical", "# Framerate Limit") config.set("Technical", "framerate", "100") config.add_section("Graphics") config.set("Graphics", "folder", "img") config.set("Graphics", "empty_playfield_tile", "opaque_playfield_tile.png") config.add_section("Colors") config.set( "Colors", "# Color values need to be separated by comma and space \", \"") config.set("Colors", "game_window_background_color", "18, 18, 18, 255") config.set("Colors", "game_window_foreground_color", "245, 245, 245, 255") config.set("Colors", "font_color", "70, 70, 70, 255") with open(path_to_configfile, mode="w", encoding="utf-8") as configfh: config.write(configfh) config.read(path_to_configfile) return config
def writeDefaultConfig(config): config.add_section('STORAGE') storageConfig = config['STORAGE'] storageConfig['data_path'] = 'data/storage/' config.add_section('TASK_MANAGER') config.add_section('SERVER') config.add_section('SCHEDULER') config.add_section('LOGGING') loggingConfig = config['LOGGING'] loggingConfig['file'] = CONFIG_PATH + '/logging.conf' if not os.path.exists(CONFIG_PATH): os.makedirs(CONFIG_PATH) with open(CONFIG_FILE,'w') as configFile: config.write(configFile)
def updateLogConfig(fileName, loggerName): try: config = openLogConfig(fileName) config.set('loggers', 'keys', config.get('loggers', 'keys')+','+loggerName) print 'new logger names:',config.get('loggers', 'keys') config.add_section('logger_'+loggerName) config.set('logger_'+loggerName, 'handlers', 'consoleHandler') config.set('logger_'+loggerName, 'propagate', '0') config.set('logger_'+loggerName, 'level', 'ERROR') config.set('logger_'+loggerName, 'qualname', loggerName) configfile = open(fileName, 'wb') config.write(configfile) except: pass
def __parseCfgOptions(self): # Is there a configuration available? configDir = self.get('core.config') configFiles = self.__getCfgFiles(os.path.join(configDir, "config.d")) configFiles.insert(0, os.path.join(configDir, "config")) config = configparser.RawConfigParser() filesRead = config.read(configFiles) # Bail out if there's no configuration file if not filesRead: raise ConfigNoFile( "No usable configuration file (%s/config) found!" % configDir) # Walk thru core configuration values and push them into the registry for section in config.sections(): if not section in self.__registry: self.__registry[section] = {} self.__registry[section].update(config.items(section)) if not hasattr(sys, "_called_from_test") or getattr( sys, "_called_from_test") is False: # do not use this in tests self.__user_config = configparser.RawConfigParser() # read the settings changed via gui client if os.path.exists( os.path.join(self.get('core.user-config'), "user-config")): filesRead = self.__user_config.read( os.path.join(self.get('core.user-config'), "user-config")) if not filesRead: raise ConfigNoFile( "No usable GUI configuration file (%s/user-config) found!" % configDir) # Initialize the logging module on the fly try: if self.__user_config is not None: for section in self.__user_config.sections(): for option, value in self.__user_config.items(section): config.set(section, option, value) tmp = StringIO() config.write(tmp) tmp2 = StringIO(tmp.getvalue()) logging.config.fileConfig(tmp2) except configparser.NoSectionError: logging.basicConfig( level=logging.ERROR, format='%(asctime)s (%(levelname)s): %(message)s')
def create_shanghai_config() -> None: """Create an empty shanghai config with necessary sections and keys.""" config = configparser.ConfigParser() config["DEFAULT"] = { "owner": "", "nick": "", "realname": "", "password": "", "prefix": ",", "server": "", "port": "6697", "ssl": "yes", } with open("config/shanghai.ini", "w+") as conffile: config.write(conffile)
def change_server_name(self, new_name): with invalidates(Rcon.get_name): remote_path = f"{self.base_path}/ServerConfig/Server.ini" logger.info("Updating name in %s", remote_path) f = BytesIO() self.adapter.get_file(remote_path, f) print(f.getvalue()) config = ConfigParser() config.read_string(f.getvalue().decode()) config.set("Server", "Name", f'"{new_name}"') temp_f = StringIO() config.write(temp_f) f = BytesIO(temp_f.getvalue().encode()) self.adapter.put_file(f, remote_path) logger.info("Updated name to %s", new_name)
def writeConfig(self): config = ConfigParser() for keysym, action in self.__macros.items(): config[keysym] = action try: with open(self.__configFile, 'w') as fic: config.write(fic) except: self.log('Write error on config file'.format(self.__configFile), cat='error') self.notify.send('Write error on config file <b>{}</b>'.format( self.__configFile)) exit(2)
def main(): args = make_args() config = configparser.ConfigParser() utils.load_config(config, args.config) for cmd in args.modify: utils.modify_config(config, cmd) with open(os.path.expanduser(os.path.expandvars(args.logging)), 'r') as f: logging.config.dictConfig(yaml.load(f)) if args.run is None: buffer = io.StringIO() config.write(buffer) args.run = hashlib.md5(buffer.getvalue().encode()).hexdigest() logging.info('cd ' + os.getcwd() + ' && ' + subprocess.list2cmdline([sys.executable] + sys.argv)) train = Train(args, config) train() logging.info(pybenchmark.stats)
class AppLogger: """ Application log """ working_dir = os.path.dirname(__file__).replace('libcommon','') __log_single = None __log_ini_file_temp = os.path.join(working_dir,'conf/log/log_config_temp.conf') __log_ini_file = os.path.join(working_dir,'conf/log/log_config.conf') filecontent = open(__log_ini_file_temp,'rb') config = ConfigParser.ConfigParser() config.readfp(filecontent) filecontent.close() config.set("handler_file","args",(os.path.join(working_dir,'log/app.log'),'d',1,'%Y-%m-%d')) configContent = open(__log_ini_file,'wb') config.write(configContent) configContent.close() def __init__(self): if not AppLogger.__log_single: try: logging.config.fileConfig(AppLogger.__log_ini_file) except: pass AppLogger.__log_single = logging.getLogger(None) def getHandler(self): return AppLogger.__log_single
async def core_session(config: TriblerConfig, components: List[Component]): session = Session(config, components, failfast=False) signal.signal(signal.SIGTERM, lambda signum, stack: session.shutdown_event.set) async with session.start() as session: # If there is a config error, report to the user via GUI notifier if config.error: session.notifier[notifications.report_config_error](config.error) # SHUTDOWN await session.shutdown_event.wait() if not config.gui_test_mode: session.notifier[notifications.tribler_shutdown_state]( "Saving configuration...") config.write()
def __parseCfgOptions(self): # Is there a configuration available? configDir = self.get('core.config') configFiles = self.__getCfgFiles(os.path.join(configDir, "config.d")) configFiles.insert(0, os.path.join(configDir, "config")) config = configparser.RawConfigParser() filesRead = config.read(configFiles) # Bail out if there's no configuration file if not filesRead: raise ConfigNoFile("No usable configuration file (%s/config) found!" % configDir) # Walk thru core configuration values and push them into the registry for section in config.sections(): if not section in self.__registry: self.__registry[section] = {} self.__registry[section].update(config.items(section)) if not hasattr(sys, "_called_from_test") or getattr(sys, "_called_from_test") is False: # do not use this in tests self.__user_config = configparser.RawConfigParser() # read the settings changed via gui client if os.path.exists(os.path.join(self.get('core.user-config'), "user-config")): filesRead = self.__user_config.read(os.path.join(self.get('core.user-config'), "user-config")) if not filesRead: raise ConfigNoFile("No usable GUI configuration file (%s/user-config) found!" % configDir) # Initialize the logging module on the fly try: if self.__user_config is not None: for section in self.__user_config.sections(): for option, value in self.__user_config.items(section): config.set(section, option, value) tmp = StringIO() config.write(tmp) tmp2 = StringIO(tmp.getvalue()) logging.config.fileConfig(tmp2) except configparser.NoSectionError: logging.basicConfig(level=logging.ERROR, format='%(asctime)s (%(levelname)s): %(message)s')
def set_syslog_logger(self, enabled): config = ConfigParser.ConfigParser() config.readfp(open(self.logconf)) handlers = config.get("logger_root", "handlers") handler_list = handlers.split(',') if enabled: new_list = "syslogHandler," else: new_list = "" for handler in handler_list: if handler == "syslogHandler": continue new_list += handler + "," new_list = new_list.rstrip(',') config.set("logger_root", "handlers", new_list) with open(self.logconf, 'w') as configfile: config.write(configfile)
def write_report_details(self): phy_topology = self._get_phy_topology_detail() details_h = open(self.report_details_file, 'w') config = ConfigParser.ConfigParser() config.add_section('Test') config.set('Test', 'Build', self.build_id) config.set('Test', 'timestamp', self.ts) config.set('Test', 'Report', self.html_log_link) config.set('Test', 'LogsLocation', self.log_link) config.set('Test', 'Topology', phy_topology) config.write(details_h) log_location = '' if self.jenkins_trigger: log_location = "nodeb10.englab.juniper.net:/cs-shared/test_runs" \ "/%s/%s" % (self.host_data[self.cfgm_ips[0]]['name'], self.ts) config.set('Test', 'CoreLocation', log_location) details_h.close()
def writeDefaultConfig(config): config.add_section('STORAGE') storageConfig = config['STORAGE'] storageConfig['data_path'] = 'data/storage/' config.add_section('SCHEDULER') config.add_section('TASK_MANAGER') config.add_section('INTERFACE') config.add_section('CONTRACTOR') config.add_section('ENTITY_MANAGER') interfaceConfig = config['INTERFACE'] interfaceConfig['xmlrpc_server_addr'] = 'localhost' interfaceConfig['xmlrpc_server_port'] = '8000' config.add_section('LOGGING') loggingConfig = config['LOGGING'] loggingConfig['file'] = CONFIG_PATH + '/logging.conf' if not os.path.exists(CONFIG_PATH): os.makedirs(CONFIG_PATH) with open(CONFIG_FILE,'w') as configFile: config.write(configFile)
def writeConfig(self): ''' This function creates / saves log settings to a file. ''' config = ConfigParser.ConfigParser() config.add_section('loggers') config.set('loggers', 'keys', 'root') config.add_section('formatters') config.set('formatters', 'keys', 'basic,nix') config.add_section('handlers') config.set('handlers', 'keys', 'consoleHandler,syslogHandler') config.add_section('logger_root') config.set('logger_root', 'level', 'DEBUG') config.set('logger_root', 'handlers', 'consoleHandler,syslogHandler') config.add_section('handler_consoleHandler') config.set('handler_consoleHandler', 'class', 'StreamHandler') config.set('handler_consoleHandler', 'level', 'NOTSET') config.set('handler_consoleHandler', 'formatter', 'basic') config.set('handler_consoleHandler', 'args', '(sys.stdout,)') config.add_section('handler_syslogHandler') config.set('handler_syslogHandler', 'class', 'handlers.SysLogHandler') config.set('handler_syslogHandler', 'level', 'NOTSET') config.set('handler_syslogHandler', 'formatter', 'nix') config.set('handler_syslogHandler', 'args', "(('/dev/log'), handlers.SysLogHandler.LOG_USER)") config.add_section('formatter_basic') config.set('formatter_basic', 'format', '%(asctime)s freeseer: <%(levelname)s> %(message)s') config.set('formatter_basic', 'datefmt', '%Y-%m-%d %H:%M:%S') config.add_section('formatter_nix') config.set('formatter_nix', 'format', 'freeseer: <%(levelname)s> %(message)s') # Save default settings to new config file with open(self.logconf, 'w') as configfile: config.write(configfile)
def __init__(self, args, config): self.args = args self.config = config self.model_dir = utils.get_model_dir(config) self.cache_dir = utils.get_cache_dir(config) self.category = utils.get_category(config, self.cache_dir) self.anchors = torch.from_numpy(utils.get_anchors(config)).contiguous() logging.info('use cache directory ' + self.cache_dir) logging.info('tensorboard --logdir ' + self.model_dir) if args.delete: logging.warning('delete model directory: ' + self.model_dir) shutil.rmtree(self.model_dir, ignore_errors=True) os.makedirs(self.model_dir, exist_ok=True) with open(self.model_dir + '.ini', 'w') as f: config.write(f) self.saver = utils.train.Saver(self.model_dir, config.getint('save', 'keep')) self.timer_save = utils.train.Timer(config.getfloat('save', 'secs'), False) try: self.timer_eval = utils.train.Timer(eval(config.get('eval', 'secs')), config.getboolean('eval', 'first')) except configparser.NoOptionError: self.timer_eval = lambda: False self.summary_worker = SummaryWorker(self) self.summary_worker.start()
def __parseCfgOptions(self): # Is there a configuration available? configFile = self.get('core.config') configFiles = self.__getCfgFiles(configFile + ".d") configFiles.insert(0, configFile) config = ConfigParser.RawConfigParser() filesRead = config.read(configFiles) # Bail out if there's no configuration file if not filesRead: raise ConfigNoFile("No usable configuration file (%s) found!" % configFile) # Walk thru core configuration values and push them into the registry for section in config.sections(): if not section in self.__registry: self.__registry[section] = {} self.__registry[section].update(config.items(section)) # Initialize the logging module on the fly tmp = StringIO.StringIO() config.write(tmp) tmp2 = StringIO.StringIO(tmp.getvalue()) logging.config.fileConfig(tmp2)
def ConfigParserConfig(config, disable_existing_loggers=False): """ config from a ConfigParser object :param config: :param disable_existing_loggers: """ # logging config uses 0 and 1 instead of True and False disable_existing_loggers = disable_existing_loggers and 1 or 0 memfile = StringIO.StringIO() config.write(memfile) memfile.seek(0) try: if sys.version_info >= (2,6): logging.config.fileConfig(memfile, disable_existing_loggers=disable_existing_loggers) else: disableds = {} #noinspection PyUnresolvedReferences for key, value in logging.root.manager.loggerDict.items(): try: disableds[key] = value.disabled or disable_existing_loggers except AttributeError: # This is OK, we're dealing with a LoggerPlaceholder which #doesn't have 'disabled' defined. logging.config.fileConfig #defines them as 1 anyways, so by default, we want to enable #them disableds[key] = disable_existing_loggers logging.config.fileConfig(memfile) for key, disabled in disableds.items(): #noinspection PyUnresolvedReferences logging.root.manager.loggerDict[key].disabled = disabled except socket.error, e: sys.stderr.write("Error occurs when initialize logging: ") sys.stderr.write(str(e)) sys.stderr.write(os.linesep)
def write(path, section, key, value): config = ConfigParser() config.read(path) config[section][key] = value with open(path, 'w') as configfile: config.write(configfile)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) if not access_key or not access_secret: # OAuth dance try: redirect_url = auth.get_authorization_url() print 'Go here to get the OAuth PIN! %s'%redirect_url except tweepy.TweepError: twlogger.error('Error! Failed to get request token.') sys.exit(1) verifier = raw_input('Verifier: ') auth.get_access_token(verifier) config.set('Twitter','access_key',auth.access_token.key) config.set('Twitter','access_secret',auth.access_token.secret) config.write(open('conf/twitterbot.ini','w')) sys.exit(0) else: auth.set_access_token(access_key,access_secret) api = tweepy.API(auth) ivy_name = config.get('Twitter','ivy_name') IvyInit(ivy_name,'[%s is ready]'%ivy_name,0,oncxproc,ondieproc) IvyStart(config.get('General','ivy_bus')) IvyBindMsg(status_change,'^status=(-?[0-1])') IvyBindMsg(picture_change,'^newpic') IvyBindMsg(heartbeat,'^hb_syn') IvySendMsg('status?') IvyMainLoop()
def save_configuration(): print "Saving configuration to " + configfilename + "..." configfile = open(configfilename, "w") config.write(configfile) configfile.close()
def main(): formatterStr = '%(asctime)s %(module)s:%(lineno)d [%(levelname)s]:%(message)s' logging.basicConfig(level=logging.DEBUG, format=formatterStr) logger = logging.getLogger() config = configparser.RawConfigParser() config.add_section('Section1') config.set('Section1', 'an_int', '15') config.set('Section1', 'a_bool', 'true') config.set('Section1', 'a_float', '3.1415') config.set('Section1', 'baz', 'fun') config.set('Section1', 'bar', 'Python') config.set('Section1', 'foo', '%(bar)s is %(baz)s!') with io.open('example.cfg', 'w') as configfile: config.write(configfile) config1 = configparser.RawConfigParser() config1.read('example.cfg') logger.info("config1{") a_float = config1.getfloat('Section1', 'a_float') an_int = config1.getint('Section1', 'an_int') logger.info(a_float + an_int) if config1.getboolean('Section1', 'a_bool'): logger.info(config1.get('Section1', 'foo')) logger.info("}") logger.info("config2{") config2 = configparser.ConfigParser() config2.read('example.cfg') logger.info(config2.get('Section1', 'foo', 0)) logger.info(config2.get('Section1', 'foo', 1)) logger.info(config2.get('Section1', 'foo', 0, {'bar': 'Documentation', 'baz': 'evil'})) logger.info("}") logger.info("config3{") config3 = configparser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'}) config3.read('example.cfg') logger.info(config3.get('Section1', 'foo')) config3.remove_option('Section1', 'bar') config3.remove_option('Section1', 'baz') logger.info(config3.get('Section1', 'foo')) logger.info("}") logger.info("config4{") sample_config = """ [mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid skip-external-locking old_passwords = 1 skip-bdb skip-innodb """ config4 = configparser.RawConfigParser(allow_no_value=True) config4.readfp(io.BytesIO(sample_config)) # Settings with values are treated as before: config4.get("mysqld", "user") # Settings without values provide None: config4.get("mysqld", "skip-bdb") # Settings which aren't specified still raise an error: config4.get("mysqld", "does-not-exist") logger.info("}")
def write(): f = open(configs_found[-1], "w") config.write(f) f.close()
def initLogging(cls, loggingLevel=None, console="stderr", logToFile=False): """ A lower-level function to initialize python logging for the calling process. Supports logging output to a console (stderr or stdout) and/or log file. See also higher-level functions initTool() and initService(). NOTE: by convention, logging should be initialized only by the main process. modules that provide APIs should not initialize logging as it would clobber the logging configuration desired by the application. :param loggingLevel: logging level string for filtering in root logger and output handlers; one of: "DEBUG", "INFO", "WARNING", "WARN", "ERROR", "CRITICAL" or "FATAL" that correspond to logging.DEBUG, logging.INFO, etc. Defaults to "INFO". :param console: Console logging destination; either "stderr" or "stdout"; None to suppress output to console. :param logToFile: True to output logs to a file. If enalbed, a log file specific to the calling app instance will be created at file path generated by our getApplicationLogFilePath method. """ validLoggingLevels = ["DEBUG", "INFO", "WARNING", "WARN", "ERROR", "CRITICAL", "FATAL"] if loggingLevel is not None and loggingLevel not in validLoggingLevels: raise ValueError("loggingLevel %r not one of %s" % (loggingLevel, validLoggingLevels)) consoleHandlerArgsMap = dict( stderr="(sys.stderr, )", stdout="(sys.stdout, )" ) if console is not None and console not in consoleHandlerArgsMap: raise ValueError("console %r not one of %s" % (console, consoleHandlerArgsMap.keys())) # Configure logging timestamp for UTC logging.Formatter.converter = time.gmtime # Load the config tempalte config = ConfigParser() with open(cls.getLoggingConfTemplatePath(), 'r') as fileObj: config.readfp(fileObj) # Customize the config template handlers = [] if console is not None: handlers.append("console") if loggingLevel is not None: config.set("handler_console", "level", loggingLevel) config.set("handler_console", "args", consoleHandlerArgsMap[console]) if logToFile: handlers.append("file") # Get a log file path specific to the calling app logFilePath = cls.getApplicationLogFilePath() # Create the directory that will contain the log file makeDirectoryFromAbsolutePath(os.path.dirname(logFilePath)) if loggingLevel is not None: config.set("handler_file", "level", loggingLevel) config.set("handler_file", "filename", logFilePath) if not handlers: print >> sys.stderr, ( "WARNING: logging_support is disabling logging output because all " "output handlers are disabled") handlers.append("null") # Convert list of logging output handler names into comma-separated string handlers = ",".join(handlers) # Initialize the root logger if loggingLevel is not None: config.set("logger_root", "level", loggingLevel) config.set("logger_root", "handlers", handlers) # Initialize the list of all logging output handlers config.set("handlers", "keys", handlers) # Dump the customized config into a StringIO object for logging setup customConfigFile = StringIO() config.write(customConfigFile) customConfigFile.seek(0) # Initialize logging from StringIO file object logging.config.fileConfig(customConfigFile, disable_existing_loggers=False)
def save_configuration(): logger.info("Saving configuration to " + configfilename + "...") configfile = open(configfilename, "w") config.write(configfile) configfile.close()
config = Config() config.read(CONFIG_FILE) DEBUG = config.get_boolean(APPLICATION_NAME, 'debug', False) LOGIN_URL = config.get_option(APPLICATION_NAME, 'login_url', '/login/') PORT = config.get_int(APPLICATION_NAME, 'port', 8000) if config.has_option(APPLICATION_NAME, 'cookie_secret'): COOKIE_SECRET = config.get(APPLICATION_NAME, 'cookie_secret') else: COOKIE_SECRET = base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes) if not config.has_section(APPLICATION_NAME): config.add_section(APPLICATION_NAME) config.set(APPLICATION_NAME, 'cookie_secret', COOKIE_SECRET) with open(CONFIG_FILE, 'wt') as cf: config.write(cf) LOGGING_CONFIG = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'verbose': { 'format': '%(asctime)s - %(name)s - %(levelname)s - %(module)s - %(process)d - %(thread)d - %(message)s', 'datefmt': '%d/%m/%Y %Hh%Mm%Ss' }, 'simple': { 'format': '%(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s', 'datefmt': '%d/%m/%Y %Hh%Mm%Ss' } }, 'handlers': {