def update_library(filename): """ Update units in current library from `filename`. Parameters ---------- filename : string or file Source of units configuration data. """ if isinstance(filename, basestring): inp = open(filename, 'rU') else: inp = filename try: cfg = ConfigParser() cfg.optionxform = _do_nothing # New in Python 3.2: read_file() replaces readfp(). if sys.version_info >= (3, 2): cfg.read_file(inp) else: cfg.readfp(inp) _update_library(cfg) finally: inp.close()
def submodule_list(self): modules = "{}:{}".format(self.ref, GIT_MODULES) exit_code, output = self.source.check_output( [self.source.host_git, "show", modules], cwd=self.mirror) # If git show reports error code 128 here, we take it to mean there is # no .gitmodules file to display for the given revision. if exit_code == 128: return elif exit_code != 0: raise SourceError( "{plugin}: Failed to show gitmodules at ref {ref}".format( plugin=self, ref=self.ref)) content = "\n".join([l.strip() for l in output.splitlines()]) io = StringIO(content) parser = RawConfigParser() parser.read_file(io) for section in parser.sections(): # validate section name against the 'submodule "foo"' pattern if re.match(r'submodule "(.*)"', section): path = parser.get(section, "path") url = parser.get(section, "url") yield (path, url)
def read_config( file: Union[Iterable[str], Path] ) -> "PsConfiguration": # Read the settings file """ Read the settings file, either from a path or from an iterable over strings (such as a file-like object or a list of strings) """ config = RawConfigParser() if isinstance(file, Path): with file.open() as fh: config.read_file(fh) else: config.read_file(file) return PsConfiguration( max_threads=config.getint("DEFAULT", "threads"), p3_path=config.get("Primer3", "path"), p3_config_path=config.get("Primer3", "configPath"), p3_thermo_path=config.get("Primer3", "thermoParamPath"), blast_path=config.get("BLAST", "path"), blast_dbpath=config.get("BLAST", "db_path"), blast_dbname=config.get("BLAST", "db_name"), blast_max_hits=config.getint("BLAST", "maxHits"), rnacf_path=config.get("RNAcofold", "path"), opt_steps=config.getint("Optimization", "steps"), opt_max_temp=config.getint("Optimization", "maxTemp"), )
def settingsFromFile(infile, defaults): """Given a path string :attr:`infile`, load settings and return them as dictionary. Args: infile (str): a path to a file defaults (dict): a dictionary containing fallback values """ config = RawConfigParser() config.optionxform = lambda option: option try: with open(infile) as f: try: config.read_file(f) except MissingSectionHeaderError: config['General'] = defaults except OSError: config['General'] = defaults for key in defaults: if key not in config['General']: config['General'][key] = defaults[key] try: if int(config['General']['UpdateEvery']) <= 0: config['General']['UpdateEvery'] = defaults['UpdateEvery'] except ValueError: # can't convert to integer config['General']['UpdateEvery'] = defaults['UpdateEvery'] for value in ('SyncNewCourses', ): try: booleanvalue = config.getboolean('General', value) config['General'][value] = str(booleanvalue) except ValueError: # can't convert to boolean config['General'][value] = defaults[value] return dict(config['General'])
def convert_ini(ini_file): cfg_ini = RawConfigParser(strict=False) try: if isinstance(ini_file, str): cfg_ini.read(ini_file) else: cfg_ini.read_file(ini_file) except ParsingError as e: raise ConversionError(e.message) ready_sections = enable_sections(combine_sections(parse_sections(cfg_ini)), core_options(cfg_ini)) plugins_cfg_dict = { section.new_name: section.get_cfg_dict() for section in ready_sections } plugins_cfg_dict.update({ 'core': dict([ Option('core', key, value, CORE_SCHEMA).as_tuple for key, value in without_defaults(cfg_ini, CORE_SECTION_OLD) if not key.startswith(PLUGIN_PREFIX) ]) }) logger.info('Converted config:\n{}'.format(yaml.dump(plugins_cfg_dict))) return plugins_cfg_dict
def Apply(self): try: config = RawConfigParser() with open(self.inifile, 'r') as openini: config.read_file(openini) except MissingSectionHeaderError as e: print("Error: {0}".format(e)) raise SystemExit except IOError: print("Error: No such file or directory") raise SystemExit try: # Update default configurations, based on .ini file values for key, value in config['default'].items(): defaultconf[key] = value.split('#')[0].strip() del config['default'] except KeyError: # In case there is no Default section pass try: for each in config.sections(): self.objects[config[each]['type']].append(config[each]) except KeyError as e: print("Error: Missing {0} in '{1}'".format(e, each)) # "No such type {0}" raise SystemExit
def parse_abbs_spec(spec_file_loc, pkg_name): try: fp = open(spec_file_loc + '/spec', 'rt') spec_cont = fp.read() fp.close() except: print('[E] Failed to load spec file! Do you have read permission?') return False # Stupid but necessary laundry list of possible varibles script = spec_cont + gen_laundry_list(['VER', 'REL', 'SUBDIR', 'SRCTBL', 'GITSRC', 'GITCO', 'GITBRCH', 'SVNSRC', 'SVNCO', 'HGSRC', 'BZRSRC', 'BZRCO', 'DUMMYSRC']) try: # Better to be replaced by subprocess.Popen spec_out = subprocess.check_output(script, shell=True) except: print('[E] Malformed spec file found! Couldn\'t continue!') return False # Assume it's UTF-8 since we have no clue of the real world on how it # works ... spec_fp = io.StringIO('[wrap]\n' + spec_out.decode('utf-8')) config = RawConfigParser() config.read_file(spec_fp) config_dict = {} for i in config['wrap']: config_dict[i.upper()] = config['wrap'][i] config_dict['NAME'] = pkg_name res, err_msg = parser_validate(config_dict) if res is not True: print('[E] {}'.format(err_msg)) return False return config_dict
def _update_release_version_for_sanic(current_version, new_version, config_file): config_parser = RawConfigParser() with open(config_file) as cfg: config_parser.read_file(cfg) config_parser.set("version", "current_version", new_version) version_file = config_parser.get("version", "file") current_version_line = config_parser.get( "version", "current_version_pattern").format(current_version=current_version) new_version_line = config_parser.get( "version", "new_version_pattern").format(new_version=new_version) with open(version_file) as init_file: data = init_file.read() new_data = data.replace(current_version_line, new_version_line) with open(version_file, "w") as init_file: init_file.write(new_data) with open(config_file, "w") as config: config_parser.write(config) command = GIT_COMMANDS.get("commit_version_change") command[0] = command[0].format(new_version=new_version, current_version=current_version) _, err, ret = _run_shell_command(command) if int(ret) != 0: print("Failed to Commit Version upgrade changes to Sanic: {}".format( err.decode("utf-8"))) exit(1)
def load_fp(fp): cfg = RawConfigParser() cfg.read_file(fp) for section in cfg.sections(): for k, v in cfg.items(section): rkey = '.'.join((section, k)) apply_kv(rkey, v)
def __init__(self): """클래스의 생성자 입니다. webdriver와 로그인에 사용될 email, password, 어떤 그룹의 신규가입자에게 메세지를 보낼지 결정하는 group_id, 메세지 내용을 담고있는 msg(message) 를 settings.ini에서 읽어와 변수에 저장합니다. """ self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = "https://www.facebook.com/" self.verificationErrors = [] self.accept_next_alert = True from configparser import RawConfigParser from os.path import join, abspath, dirname import codecs settings_path = join(abspath(dirname(__file__)), 'settings.ini') cfg = RawConfigParser() cfg.read_file(codecs.open(settings_path, "r", "utf8")) self.email = cfg.get('setting', "email") self.pw = cfg.get('setting', 'pw') self.group_id = cfg.get('setting', 'group_id') self.group_name = cfg.get('setting', 'group_name')
def load(self, csv): conf = RawConfigParser() # utf-8-sig per https://bugs.python.org/issue7185#msg94346 with open(csv, encoding='utf-8-sig') as f: conf.read_file(f) for sec in conf.sections(): if not self.conf.has_section(sec): self.conf.add_section(sec) self.crap.add_section(sec) for k, v in conf.items(sec): is_crap = False if '__' in v: is_crap = True if not is_crap: if self.conf.has_option(sec, k): if self.conf.get(sec, k).lower() != v.lower(): print('Overwriting locale %s (%r -> %r)' % (k, self.conf.get(sec, k), v)) self.conf.set(sec, k, v) else: if self.crap.has_option(sec, k): print('Overwriting crap locale %s (%r -> %r)' % (k, self.crap.get(sec, k), v)) self.crap.set(sec, k, v)
def import_library(libfilepointer): """ Import a units library, replacing any existing definitions. Parameters ---------- libfilepointer : file new library file to work with Returns ------- ConfigParser newly updated units library for the module """ global _UNIT_LIB global _UNIT_CACHE _UNIT_CACHE = {} _UNIT_LIB = ConfigParser() _UNIT_LIB.optionxform = _do_nothing # New in Python 3.2: read_file() replaces readfp(). if sys.version_info >= (3, 2): _UNIT_LIB.read_file(libfilepointer) else: _UNIT_LIB.readfp(libfilepointer) required_base_types = ['length', 'mass', 'time', 'temperature', 'angle'] _UNIT_LIB.base_names = list() # used to is_angle() and other base type checking _UNIT_LIB.base_types = dict() _UNIT_LIB.unit_table = dict() _UNIT_LIB.prefixes = dict() _UNIT_LIB.help = list() for prefix, factor in _UNIT_LIB.items('prefixes'): factor, comma, comment = factor.partition(',') _UNIT_LIB.prefixes[prefix] = float(factor) base_list = [0] * len(_UNIT_LIB.items('base_units')) for i, (unit_type, name) in enumerate(_UNIT_LIB.items('base_units')): _UNIT_LIB.base_types[unit_type] = i powers = list(base_list) powers[i] = 1 # print '%20s'%unit_type, powers # cant use add_unit because no base units exist yet _new_unit(name, 1, powers) _UNIT_LIB.base_names.append(name) # test for required base types missing = [utype for utype in required_base_types if utype not in _UNIT_LIB.base_types] if missing: raise ValueError('Not all required base type were present in the' ' config file. missing: %s, at least %s required' % (missing, required_base_types)) _update_library(_UNIT_LIB) return _UNIT_LIB
def get_config(config_path): with open(config_path) as f: str_config = StringIO('[root]\n' + f.read()) config = RawConfigParser() config.read_file(str_config) return config
def read_current_version(): """Read the current_version string in .bumpversion.cfg""" config = RawConfigParser() config.add_section('bumpversion') config.read_file(io.open('.bumpversion.cfg', 'rt', encoding='utf-8')) items = dict(config.items('bumpversion')) current_version = items.get('current_version') return current_version
def readConfig(fileName): cp = RawConfigParser() fp = open(fileName, 'r') if sys.version_info < (3, 2): cp.readfp(fp) # pylint: disable=W1505 else: cp.read_file(fp, fileName) return cp
def main(args=None): # load in the configuration file config = ConfigParser({ 'certfile': None, 'keyfile': None, 'ca_certs': None, 'ssl': False, 'port': '8000', 'host': '0.0.0.0', 'icons': 'link', 'corpus_link': None, 'doc_title_format': '{0}', 'doc_url_format': '', 'raw_corpus': None, 'label_module': None, 'fulltext': 'false', 'topics': None, 'cluster': None, 'corpus_desc': None, 'home_link': '/', 'lang': None }) #open config for reading with open(args.config, encoding='utf8') as configfile: config.read_file(configfile) # clean up output file path if args.output is None: args.output = args.config.replace('.ini', '.tez') if not args.output.endswith('.tez'): args.output += '.tez' # path variables context_type = config.get('main', 'context_type') corpus_file = config.get('main', 'corpus_file') model_pattern = config.get('main', 'model_pattern') cluster_path = config.get('main', 'cluster') corpus_desc = config.get('main', 'corpus_desc') # topic variables if config.get('main', 'topics'): topic_range = eval(config.get('main', 'topics')) if args.include_corpus: raw_corpus = config.get('main', 'raw_corpus') else: raw_corpus = None # get manifest for zip file filenames = build_manifest(args.config, corpus_file, model_pattern, topic_range, cluster_path, raw_corpus=raw_corpus, corpus_desc=corpus_desc) zip_files(args.output, filenames, args.include_corpus)
def _fetch_current_version(config_file: str) -> str: if path.isfile(config_file): config_parser = RawConfigParser() with open(config_file) as cfg: config_parser.read_file(cfg) return (config_parser.get("version", "current_version") or _fetch_default_calendar_release_version()) else: return _fetch_default_calendar_release_version()
def _load_ini(path): """ Load an INI file from *path*. """ cfg = RawConfigParser() with codecs.open(path, mode="r", encoding="utf-8") as f: cfg.read_file(f) return cfg
def read_config(filename): """ Read configuration from file :param filename: name of the configuration file :return: the config object """ config = RawConfigParser() config.read_file(open(filename)) return config
def parse_modelsimini(file_name): """ Parse a modelsim.ini file :returns: A RawConfigParser object """ cfg = RawConfigParser() with io.open(file_name, "r", encoding="utf-8") as fptr: cfg.read_file(fptr) return cfg
def read_rose_cp(fn): cp = RawConfigParser() with open(fn, 'r') as f: metaline = f.readline() metaval = metaline.split('=')[1].strip() while f.readline() != '\n': pass cp.read_file(f) return metaval, cp
def iter_config(path: Path): if path.exists(): config = RawConfigParser() with path.open('r') as f: config.read_file(f) for section in config.sections(): for key, val in config[section].items(): yield key, val
def read_config(config_path: str) -> RawConfigParser: """ Reads the AMT configuration from the given path """ config = RawConfigParser() config.optionxform = str config.read_file(open(GLOBAL_CONFIG)) if config_path: config.read(config_path) return config
def _fetch_current_version(config_file: str) -> str: if path.isfile(config_file): config_parser = RawConfigParser() with open(config_file) as cfg: config_parser.read_file(cfg) return ( config_parser.get("version", "current_version") or _fetch_default_calendar_release_version() ) else: return _fetch_default_calendar_release_version()
def create_relative_config_file(config_file, manifest, include_corpus=False): if sys.version_info[0] == 3: root = os.path.commonpath(map(os.path.abspath, manifest)) + '/' else: root = os.path.commonprefix(map(os.path.abspath, manifest)) config = ConfigParser({ 'cluster': None, 'corpus_desc': None, 'raw_corpus': None, 'cluster_path': None, 'path': None, 'htrc_metadata': None }) with open(config_file, encoding='utf8') as configfile: config.read_file(configfile) # path variables corpus_file = config.get('main', 'corpus_file') model_pattern = config.get('main', 'model_pattern') cluster_path = config.get('main', 'cluster') path = config.get('main', 'path') raw_corpus = config.get('main', 'raw_corpus') corpus_desc = config.get('main', 'corpus_desc') config.set('main', 'corpus_file', corpus_file.replace(root, '')) config.set('main', 'model_pattern', model_pattern.replace(root, '')) if cluster_path is not None: config.set('main', 'cluster', cluster_path.replace(root, '')) if path is not None: config.set('main', 'path', path.replace(root, '')) if raw_corpus is not None and include_corpus: config.set('main', 'raw_corpus', raw_corpus.replace(root, '')) else: config.set('main', 'raw_corpus', None) if corpus_desc is not None: config.set('main', 'corpus_desc', corpus_desc.replace(root, '')) try: if config.getboolean('main', 'htrc'): htrc_metapath = config.get('www', 'htrc_metadata') if htrc_metapath is not None: config.set('www', 'htrc_metadata', htrc_metapath.replace(root, '')) except: pass tempfh = NamedTemporaryFile(prefix='tez.' + config_file, delete=False) temp_config_file = tempfh.name tempfh.close() with open(temp_config_file, 'w', encoding='utf-8') as tempfile: config.write(tempfile) return temp_config_file
def compare_section_and_name(fn): """ Helper function to test if section captions for conf files ever differ from the name option Parameters ---------- fn: Filename """ from configparser import RawConfigParser config = RawConfigParser() config.read_file(open(fn)) return [(fn, sect, config.get(sect, 'name')) for sect in config.sections() if config.has_option(sect, 'name')]
def init_paths(cls): if cls.TEMP_PATH is None: config = RawConfigParser() path = os.path.join(os.path.expanduser('~'), '.rgfrc') try: with codecs.open(path, 'r', 'utf-8') as cfg: with StringIO(cfg.read()) as strIO: config.read_file(strIO) except MissingSectionHeaderError: with codecs.open(path, 'r', 'utf-8') as cfg: with StringIO('[glob]\n' + cfg.read()) as strIO: config.read_file(strIO) except Exception: pass if SYSTEM in ('Windows', 'Microsoft'): try: cls.RGF_PATH = os.path.abspath( config.get(config.sections()[0], 'rgf_location')) except Exception: cls.RGF_PATH = os.path.join(os.path.expanduser('~'), 'rgf.exe') cls.DEFAULT_RGF_PATH = 'rgf.exe' else: # Linux, Darwin (macOS), etc. try: cls.RGF_PATH = os.path.abspath( config.get(config.sections()[0], 'rgf_location')) except Exception: cls.RGF_PATH = os.path.join(os.path.expanduser('~'), 'rgf') cls.DEFAULT_RGF_PATH = 'rgf' try: cls.FASTRGF_PATH = os.path.abspath( config.get(config.sections()[0], 'fastrgf_location')) except Exception: cls.FASTRGF_PATH = os.path.expanduser('~') cls.DEFAULT_FASTRGF_PATH = '' try: cls.TEMP_PATH = os.path.abspath( config.get(config.sections()[0], 'temp_location')) except Exception: cls.TEMP_PATH = os.path.join(gettempdir(), 'rgf') if not os.path.isdir(cls.TEMP_PATH): os.makedirs(cls.TEMP_PATH) if not os.access(cls.TEMP_PATH, os.W_OK): raise Exception( "{0} is not writable directory. Please set " "config flag 'temp_location' to writable directory".format( cls.TEMP_PATH)) return cls.DEFAULT_RGF_PATH, cls.RGF_PATH, cls.DEFAULT_FASTRGF_PATH, cls.FASTRGF_PATH, cls.TEMP_PATH
def _load_properties_file(self, properties_file): """ Reads a properties file using ConfigParser. :param propertiesFile/configFile: """ config_file = open(properties_file, 'r') config_content = StringIO('[root]\n' + config_file.read()) config = RawConfigParser() config.read_file(config_content) return config._sections['root']
def main(args=None): # load in the configuration file config = ConfigParser({ 'certfile': None, 'keyfile': None, 'ca_certs': None, 'ssl': False, 'port': '8000', 'host': '0.0.0.0', 'icons': 'link', 'corpus_link': None, 'doc_title_format': '{0}', 'doc_url_format': '', 'raw_corpus': None, 'label_module': None, 'fulltext': 'false', 'topics': None, 'cluster': None, 'corpus_desc' : None, 'home_link' : '/', 'lang': None}) #open config for reading with open(args.config, encoding='utf8') as configfile: config.read_file(configfile) # clean up output file path if args.output is None: args.output = args.config.replace('.ini', '.tez') if not args.output.endswith('.tez'): args.output += '.tez' # path variables context_type = config.get('main', 'context_type') corpus_file = config.get('main', 'corpus_file') model_pattern = config.get('main', 'model_pattern') cluster_path = config.get('main', 'cluster') corpus_desc = config.get('main', 'corpus_desc') # topic variables if config.get('main', 'topics'): topic_range = eval(config.get('main', 'topics')) if args.include_corpus: raw_corpus = config.get('main', 'raw_corpus') else: raw_corpus = None # get manifest for zip file filenames = build_manifest( args.config, corpus_file, model_pattern, topic_range, cluster_path, raw_corpus=raw_corpus, corpus_desc=corpus_desc) zip_files(args.output, filenames, args.include_corpus)
def file_exist(): commands_file = open("commands.txt", 'r', encoding="utf-8") commands = commands_file.read().splitlines() commands_file.close() # Chargement de la configuration config = RawConfigParser() load_ini = open('times.ini', 'r', encoding='utf-8') config.read_file(load_ini) config = dict(config['TIME']) load_ini.close() time_wait = config['time_items'] time_wait_items = config['time_wait_items'] allcommand(time_wait, time_wait_items, commands)
def get_config(conf): config = RawConfigParser() with open(conf, 'r') as f: config_string = '[asection]\n' + f.read() try: config.read_string(config_string) except Exception: config_fp = StringIO.StringIO(config_string) config.read_file(config_fp) return config
def absolutize_config_file(config_file, output_dir): config_file = os.path.join(output_dir, config_file) config = ConfigParser({'cluster': None, 'htrc_metadata': None}) with open(config_file, encoding='utf8') as configfile: config.read_file(configfile) # path variables corpus_file = config.get('main', 'corpus_file') corpus_file = os.path.join(output_dir, corpus_file) corpus_file = os.path.abspath(corpus_file) config.set('main', 'corpus_file', corpus_file) model_pattern = config.get('main', 'model_pattern') model_pattern = os.path.join(output_dir, model_pattern) model_pattern = os.path.abspath(model_pattern) config.set('main', 'model_pattern', model_pattern) cluster_path = config.get('main', 'cluster') if cluster_path is not None and cluster_path != 'None': cluster_path = os.path.join(output_dir, cluster_path) cluster_path = os.path.abspath(cluster_path) config.set('main', 'cluster', cluster_path) path = config.get('main', 'path') if path is not None and path != 'None': path = os.path.join(output_dir, path) path = os.path.abspath(path) config.set('main', 'path', path) raw_corpus = config.get('main', 'raw_corpus') if raw_corpus is not None and raw_corpus != 'None': raw_corpus = os.path.join(output_dir, raw_corpus) raw_corpus = os.path.abspath(raw_corpus) config.set('main', 'raw_corpus', raw_corpus) corpus_desc = config.get('main', 'corpus_desc') if corpus_desc is not None and corpus_desc != 'None': corpus_desc = os.path.join(output_dir, corpus_desc) corpus_desc = os.path.abspath(corpus_desc) config.set('main', 'corpus_desc', corpus_desc) htrc_metadata = config.get('www', 'htrc_metadata') if htrc_metadata is not None and htrc_metadata != 'None': htrc_metadata = os.path.join(output_dir, htrc_metadata) htrc_metadata = os.path.abspath(htrc_metadata) config.set('www', 'htrc_metadata', htrc_metadata) with open(config_file, 'w', encoding='utf8') as configfile: config.write(configfile)
def parse_modelsimini(file_name): """ Parse a modelsim.ini file :returns: A RawConfigParser object """ cfg = RawConfigParser() if sys.version_info.major == 2: # For Python 2 read modelsim.ini as binary file since ConfigParser # does not support unicode with io.open(file_name, "rb") as fptr: cfg.readfp(fptr) # pylint: disable=deprecated-method else: with io.open(file_name, "r", encoding="utf-8") as fptr: cfg.read_file(fptr) return cfg
def verify_pki_config_override(cls, filename): """Verify pki config override file * filename must be an absolute path to an existing file * file must be a valid ini file * ini file must not override immutable settings TODO: The checker does not verify config interpolation values, yet. The validator does not have access to all settings. :param filename: path to pki.ini """ if not os.path.isfile(filename): raise ValueError( "Config file '{}' does not exist.".format(filename) ) if not os.path.isabs(filename): raise ValueError( "Config file '{}' is not an absolute path.".format(filename) ) try: cfg = RawConfigParser() with open(filename) as f: cfg.read_file(f) except Exception as e: raise ValueError( "Invalid config '{}': {}".format(filename, e) ) immutable_keys = cls.get_immutable_keys() invalid_keys = set() sections = [cfg.default_section] sections.extend(cls.subsystems) for section in sections: if not cfg.has_section(section): continue for k, _v in cfg.items(section, raw=True): if k in immutable_keys: invalid_keys.add(k) if invalid_keys: raise ValueError( "'{}' overrides immutable options: {}".format( filename, ', '.join(sorted(invalid_keys)) ) )
def get_immutable_keys(cls): """Get set of immutable keys Immutable keys are calculated from 'ipaca_default' config file and known keys that are defined in code. """ if cls._immutable_keys is None: immutable = set() immutable.update(cls._immutable_code_keys) cfg = RawConfigParser() with open(cls.ipaca_default) as f: cfg.read_file(f) for section in cls.subsystems: for k, _v in cfg.items(section, raw=True): if k.startswith('pki_'): immutable.add(k) cls._immutable_keys = frozenset(immutable) return cls._immutable_keys
class TestApplicationConfiguration(unittest.TestCase): configfile = '../config.ini' def setUp(self): # first test if config file is present self.test_config_file_is_present() # load configuration for tests self.config = RawConfigParser() self.config.read_file(open(self.configfile)) def test_config_file_is_present(self): self.assertEqual(os.path.isfile(self.configfile), True, "Config file not present! you should create one " "according to the github repository") def test_socket_configuration(self): port = self.config.get(MusicServer._config_section, 'port') self.assertTrue(port.isdigit(), "Port should be a number, port = " + str(port)) self.assertTrue(len(str(port)) == 4, "Port should be 4 numbers long")
def is_valid_configfile(parser, arg): if not arg.endswith('.ini'): if os.path.isdir(arg): print("{0} is a directory, using the config file {0}.ini".format(arg)) else: print("{0} is missing the '.ini' extension, using the config file {0}.ini".format(arg)) arg = arg + '.ini' if os.path.exists(arg): from configparser import RawConfigParser as ConfigParser config = ConfigParser() try: with open(arg, encoding='utf-8') as configfile: config.read_file(configfile) return arg except: parser.error("Invalid config file {0}".format(arg)) else: parser.error("The file %s does not exist!" % arg)
def parse_acbs_conf(tree_name): import configparser acbs_config = RawConfigParser() acbs_config._interpolation = configparser.ExtendedInterpolation() fp = open('/etc/acbs_forest.conf', 'rt') try: acbs_config.read_file(fp) except: return None try: tree_loc_dict = acbs_config[tree_name] except: print('[E] 404 - Tree not found: {}, defined trees: {}'.format(tree_name, arr2str(acbs_config.sections()))) return None try: tree_loc = tree_loc_dict['location'] except KeyError: print('[E] Malformed configuration file: missing `location` keyword') return None return tree_loc
def create_relative_config_file(config_file, manifest, include_corpus=False): if sys.version_info[0] == 3: root = os.path.commonpath(map(os.path.abspath, manifest)) + '/' else: root = os.path.commonprefix(map(os.path.abspath, manifest)) config = ConfigParser({'cluster': None }) with open(config_file, encoding='utf8') as configfile: config.read_file(configfile) # path variables corpus_file = config.get('main', 'corpus_file') model_pattern = config.get('main', 'model_pattern') cluster_path = config.get('main', 'cluster') path = config.get('main', 'path') raw_corpus = config.get('main', 'raw_corpus') corpus_desc = config.get('main', 'corpus_desc') config.set('main', 'corpus_file', corpus_file.replace(root, '')) config.set('main', 'model_pattern', model_pattern.replace(root, '')) if cluster_path is not None: config.set('main', 'cluster', cluster_path.replace(root, '')) if path is not None: config.set('main', 'path', path.replace(root, '')) if raw_corpus is not None and include_corpus: config.set('main', 'raw_corpus', raw_corpus.replace(root, '')) else: config.set('main', 'raw_corpus', None) if corpus_desc is not None: config.set('main', 'corpus_desc', corpus_desc.replace(root, '')) tempfh = NamedTemporaryFile(prefix='tez.'+config_file, delete=False) temp_config_file = tempfh.name tempfh.close() with open(temp_config_file, 'w') as tempfile: config.write(tempfile) return temp_config_file
def _update_release_version_for_sanic( current_version, new_version, config_file ): config_parser = RawConfigParser() with open(config_file) as cfg: config_parser.read_file(cfg) config_parser.set("version", "current_version", new_version) version_file = config_parser.get("version", "file") current_version_line = config_parser.get( "version", "current_version_pattern" ).format(current_version=current_version) new_version_line = config_parser.get( "version", "new_version_pattern" ).format(new_version=new_version) with open(version_file) as init_file: data = init_file.read() new_data = data.replace(current_version_line, new_version_line) with open(version_file, "w") as init_file: init_file.write(new_data) with open(config_file, "w") as config: config_parser.write(config) command = GIT_COMMANDS.get("commit_version_change") command[0] = command[0].format( new_version=new_version, current_version=current_version ) _, err, ret = _run_shell_command(command) if int(ret) != 0: print( "Failed to Commit Version upgrade changes to Sanic: {}".format( err.decode("utf-8") ) ) exit(1)
def config(): """Create a RawConfigParser and if exists read it before return :returns: the current config or a new one :rtype: configparser.RawConfigParser """ parser = RawConfigParser() if path.exists(CONFIG_PATH): with open(CONFIG_PATH, 'r') as f: parser.read_file(f) if CONFIG_SECTION not in parser: parser.read_dict(DEFAULT_CONFIG) # ensure that directory app_dir exists or creates otherwise os.makedirs(APP_DIR, exist_ok=True) with open(CONFIG_PATH, 'w') as f: parser.write(f) elif 'animation' not in parser[CONFIG_SECTION]: parser.set(CONFIG_SECTION, 'animation', True) with open(CONFIG_PATH, 'w') as f: parser.write(f) return parser
def parse_ab3_defines(defines_file): # , pkg_name): try: fp = open(defines_file, 'rt') abd_cont = fp.read() fp.close() except: print('[E] Failed to load autobuild defines file! Do you have read permission?') return False script = "ARCH={}\n".format( get_arch_name()) + abd_cont + gen_laundry_list(['PKGNAME', 'PKGDEP', 'BUILDDEP']) try: # Better to be replaced by subprocess.Popen abd_out = subprocess.check_output(script, shell=True) except: print('[E] Malformed Autobuild defines file found! Couldn\'t continue!') return False abd_fp = io.StringIO('[wrap]\n' + abd_out.decode('utf-8')) abd_config = RawConfigParser() abd_config.read_file(abd_fp) abd_config_dict = {} for i in abd_config['wrap']: abd_config_dict[i.upper()] = abd_config['wrap'][i] return abd_config_dict
def setUp(self): config = RawConfigParser() config.read_file(open('../config.ini')) self.musicplayer = MusicPlayer(config, False)
def read_yarrharr_conf(files, namespace): """ Read the given configuration files, mutating the given dictionary to contain Django settings. :raises UnreadableConfError: if any of the given files are not read """ conf = RawConfigParser() conf.read_file(StringIO(DEFAULT_CONF), '<defaults>') files_read = conf.read(files) files_unread = set(files) - set(files_read) if files_unread: raise UnreadableConfError(files_unread) namespace['DEBUG'] = conf.getboolean('yarrharr', 'debug') namespace['HOT'] = conf.getboolean('yarrharr', 'hot') namespace['DATABASES'] = { 'default': { 'ENGINE': conf.get('db', 'engine'), 'NAME': conf.get('db', 'name'), 'USER': conf.get('db', 'user'), 'PASSWORD': conf.get('db', 'password'), 'HOST': conf.get('db', 'host'), 'PORT': conf.get('db', 'port'), }, } namespace['ATOMIC_REQUESTS'] = True external_url = urlparse(conf.get('yarrharr', 'external_url')) if external_url.path != '': # Ensure that the URL doesn't contain a path, as some day we will # probably want to add the ability to add a prefix to the path. msg = "external_url must not include path: remove {!r}".format(external_url.path) raise ValueError(msg) namespace['ALLOWED_HOSTS'] = [external_url.hostname] # The proxied config is an enumeration to ensure it can be extended to # support the Forwarded header (RFC 7239) in the future. We require expicit # configuration rather than auto-detecting these headers because the # frontend proxy *must* be configured to strip whatever header is in use, # lest clients be able to forge it. proxied = conf.get('yarrharr', 'proxied') if proxied not in {'no', 'x-forwarded'}: msg = "proxied must be 'no' or 'x-forwarded', not {!r}".format(proxied) raise ValueError(msg) namespace['USE_X_FORWARDED_HOST'] = proxied == 'x-forwarded' # Config for the Twisted production server. namespace['SERVER_ENDPOINT'] = conf.get('yarrharr', 'server_endpoint') namespace['ROOT_URLCONF'] = 'yarrharr.urls' namespace['LOGIN_URL'] = 'login' namespace['LOGIN_REDIRECT_URL'] = 'home' namespace['LOGOUT_URL'] = 'logout' namespace['LANGUAGE_CODE'] = 'en-us' namespace['USE_I18N'] = True namespace['USE_L10N'] = True namespace['USE_TZ'] = True namespace['TIME_ZONE'] = 'UTC' namespace['STATIC_ROOT'] = conf.get('yarrharr', 'static_root') namespace['STATIC_URL'] = conf.get('yarrharr', 'static_url') namespace['STATICFILES_FINDERS'] = ( 'django.contrib.staticfiles.finders.AppDirectoriesFinder',) # Template context processors. This list is missing most of the processors # in the default list as Yarrharr's templates don't use them. context_processors = [ 'django.contrib.auth.context_processors.auth', 'yarrharr.context_processors.hot', ] if namespace['DEBUG']: # When in debug mode, display SQL queries for requests coming from the # loopback interface. context_processors.append('django.template.context_processors.debug') namespace['INTERNAL_IPS'] = ['127.0.0.1'] namespace['TEMPLATES'] = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': {'context_processors': context_processors}, }] namespace['SECRET_KEY'] = conf.get('secrets', 'secret_key') namespace['X_FRAME_OPTIONS'] = 'DENY' namespace['MIDDLEWARE'] = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) namespace['SESSION_ENGINE'] = 'django.contrib.sessions.backends.signed_cookies' namespace['SESSION_COOKIE_HTTPONLY'] = True namespace['SESSION_COOKIE_SECURE'] = external_url.scheme == 'https' namespace['CSRF_TOKEN_SECURE'] = external_url.scheme == 'https' namespace['WSGI_APPLICATION'] = 'yarrharr.wsgi.application' namespace['INSTALLED_APPS'] = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.staticfiles', 'yarrharr', ) # Disable Django's logging configuration stuff (except when running under # the dev server). if 'runserver' not in sys.argv: namespace['LOGGING_CONFIG'] = None return conf
def get_host_port(args): """ Returns the hostname and port number """ config = ConfigParser({'port': '3001', 'host': '0.0.0.0'}) with open(args.config, encoding='utf8') as configfile: config.read_file(configfile) # automatic port assignment def test_port(port): try: host = args.host or config.get("www", "host") if host == '0.0.0.0': host = 'localhost' try: s = socket.create_connection((host, port), 2) s.close() raise IOError("Socket connectable on port {0}".format(port)) except socket.error: pass return port except IOError: if not args.quiet: port = int_prompt( "Conflict on port {0}. Enter new port:".format(port)) return test_port(port) else: raise IOError( "Conflict on port {0}. Try running with -p to manually set new port.".format(port)) port = args.port or int(config.get('www', 'port').format(0)) port = test_port(port) # prompt to save if (int(config.get("www", "port").format(0))) != port: if not args.quiet and bool_prompt( "Change default baseport to {0}?".format(port), default=True): config.set("www", "port", str(port)) # create deep copy of configuration # see http://stackoverflow.com/a/24343297 config_string = StringIO() config.write(config_string) # skip DEFAULT section config_string.seek(0) idx = config_string.getvalue().index("[main]") config_string.seek(idx) # read deep copy new_config = ConfigParser() config.read_file(config_string) # write deep copy without DEFAULT section # this preserves DEFAULT for rest of program with open(args.config, 'w') as configfh: new_config.write(configfh) # hostname assignment host = args.host or config.get('www', 'host') return host, port
APPLICATION_ROOT = BASE_DIR # Config file should be installed in parent directory # format is: # [global] # domain = dev.bbonfhir.com # debug = True # template_debug = True # debug_settings = True # email_host = box905.bluehost.com # CONFIG_FILE = 'local.ini' # Read the config file PARSE_INI.read_file(open(os.path.join(APPLICATION_ROOT, CONFIG_FILE))) # Then use PARSE_INI.get(SECTION, VARIABLE) to read in value # Value is in string format # Use util functions to convert strings to boolean or Integer # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! # The real value is set in the local_settings.py # local_settings.py is excluded from the git repository # Place other important keys, passwords etc. in local_settings.py # which is called at the end of settings.py
def create_app(args): # load in the configuration file config = ConfigParser({ 'certfile': None, 'keyfile': None, 'ca_certs': None, 'ssl': False, 'port': '3001', 'host': '0.0.0.0', 'icons': 'link', 'corpus_link': None, 'doc_title_format': '{0}', 'doc_url_format': '', 'raw_corpus': None, 'label_module': None, 'fulltext': 'false', 'topics': None, 'cluster': None, 'corpus_desc' : None, 'home_link' : '/', 'lang': None}) with open(args.config, encoding='utf8') as configfile: config.read_file(configfile) # path variables context_type = config.get('main', 'context_type') corpus_file = config.get('main', 'corpus_file') model_pattern = config.get('main', 'model_pattern') cluster_path = config.get('main', 'cluster') # language customization lang = config.get('main', 'lang') # set topic_range if config.get('main', 'topics'): topic_range = eval(config.get('main', 'topics')) # get icons_list config_icons = config.get('www', 'icons').split(",") if args.fulltext or config.getboolean('www', 'fulltext'): if not any('fulltext' in icon for icon in config_icons): config_icons.insert(0, 'fulltext-inline') # Create application object corpus_name = config.get('www', 'corpus_name') corpus_link = config.get('www', 'corpus_link') doc_title_format = config.get('www', 'doc_title_format') doc_url_format = config.get('www', 'doc_url_format') home_link = config.get('www', 'home_link') label_module = config.get('main', 'label_module') corpus_path = config.get('main', 'raw_corpus') corpus_desc = config.get('main', 'corpus_desc') fulltext = args.fulltext or config.getboolean('www', 'fulltext') app = Application(corpus_file=corpus_file, model_pattern=model_pattern, topic_range=topic_range, context_type=context_type, label_module=label_module, config_file=args.config, corpus_path=corpus_path, fulltext=fulltext, lang=lang, icons=config_icons, corpus_name=corpus_name, corpus_link=corpus_link, doc_title_format=doc_title_format, doc_url_format=doc_url_format, cluster_path=cluster_path, corpus_desc=corpus_desc, home_link=home_link) """ host, port = get_host_port(args) """ # app.run(host='0.0.0.0', port=8081) return app
# TODO: let user select config file from cmd argument inifile = 'config.ini' musicserver = None # type: musicserver.MusicServer # Start main program banner = c("\n" "\tMelonMusicPlayer made by Melle Dijkstra © " + str(datetime.now().year) + "\n" "\tVersion: " + __version__ + "\n", Colors.BLUE) if __name__ == '__main__': # Check if program is run with root privileges, which is needed for socket communication try: print(banner) # Get configuration for the application config = RawConfigParser(defaults={}) if os.path.exists(inifile): config.read_file(open(inifile)) else: print(c('configuration file ('+inifile+') does not exist', Colors.WARNING)) musicserver = MusicServer(config) # This method will start the server and wait for anyone to connect musicserver.serve() except KeyboardInterrupt as e: print(c("Aborting MelonMusicPlayer...", Colors.BOLD)) musicserver.shutdown() musicserver.shutdown()
def load_entries(fp, filename, prefix): entries = [] parser = RawConfigParser() parser.optionxform = str try: if hasattr(parser, "read_file"): parser.read_file(fp, filename) else: parser.readfp(fp, filename) except IOError: raise except Exception as e: raise IOError("Error reading persistent file: %s" % e) try: items = parser.items(PERSISTENT_SECTION) except NoSectionError: raise IOError("Persistent section not found") value = None m = None for k, v in items: # Reduces code duplication if value: key = _unescape_string(m.group(1)) if key.startswith(prefix): entries.append((key, value)) value = None m = _key_bool.match(k) if m: if v == "true": value = Value.makeBoolean(True) elif v == "false": value = Value.makeBoolean(False) else: logger.warning("Unrecognized boolean value %r for %s", v, m.group(1)) continue m = _key_double.match(k) if m: try: value = Value.makeDouble(float(v)) except ValueError as e: logger.warning("Unrecognized double value %r for %s", v, m.group(1)) continue m = _key_string.match(k) if m: mm = _value_string.match(v) if mm: value = Value.makeString(_unescape_string(mm.group(1))) else: logger.warning("Unrecognized string value %r for %s", v, m.group(1)) continue m = _key_raw.match(k) if m: try: v = base64.b64decode(v, validate=True) value = Value.makeRaw(v) except binascii.Error: logger.warning("Unrecognized raw value %r for %s", v, m.group(1)) continue m = _key_bool_array.match(k) if m: bools = [] arr = v.strip().split(",") if arr != [""]: for vv in arr: vv = vv.strip() if vv == "true": bools.append(True) elif vv == "false": bools.append(False) else: logger.warning( "Unrecognized bool '%s' in bool array %s'", vv, m.group(1) ) bools = None break if bools is not None: value = Value.makeBooleanArray(bools) continue m = _key_double_array.match(k) if m: doubles = [] arr = v.strip().split(",") if arr != [""]: for vv in arr: try: doubles.append(float(vv)) except ValueError: logger.warning( "Unrecognized double '%s' in double array %s", vv, m.group(1), ) doubles = None break value = Value.makeDoubleArray(doubles) continue m = _key_string_array.match(k) if m: # Technically, this will let invalid inputs in... but, # I don't really care. Feel free to fix it if you do. strings = [_unescape_string(vv) for vv in _value_string.findall(v)] value = Value.makeStringArray(strings) continue logger.warning("Unrecognized type '%s'", k) if value: key = _unescape_string(m.group(1)) if key.startswith(prefix): entries.append((key, value)) return entries
""" Cleans the count.ini to remove users that have only used x amount of commands. Keeping them in there and letting the count.ini line count add up is not good. """ from configparser import RawConfigParser import time import sys import os count_file = r"../count.ini" busy_file = r"../is_busy.txt" needed_count = 6 to_delete = [] config = RawConfigParser(allow_no_value=True) config.read_file(open(count_file)) for each_section in config.sections(): if each_section == "global" or each_section == "failed": continue if each_section == "waifu" or each_section == "husbando": continue count = 0 for (each_key, each_val) in config.items(each_section): if each_val is None: if each_section not in to_delete: to_delete.append(each_section) else: count += int(each_val) if count <= needed_count: to_delete.append(each_section) for sec in to_delete: config.remove_section(sec)
APPLICATION_ROOT = BASE_DIR # Config file should be installed in parent directory # format is: # [global] # domain = dev.bbonfhir.com # debug = True # template_debug = True # debug_settings = True # email_host = box905.bluehost.com # CONFIG_FILE = 'local.ini' # Read the config file parser.read_file(open(os.path.join(APPLICATION_ROOT, CONFIG_FILE))) # Then use parser.get(SECTION, VARIABLE) to read in value # Value is in string format # Use util functions to convert strings to boolean or Integer # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! # The real value is set in the local_settings.py # local_settings.py is excluded from the git repository # Place other important keys, passwords etc. in local_settings.py # which is called at the end of settings.py
from docopt import docopt _file_path = path.dirname(path.realpath(__file__)) # argument parser args = docopt(__doc__) config_file = args['-c'] ## configuration config = RawConfigParser() config.read(path.join(_file_path, 'config.defaults.ini')) try: with open(config_file) as conf: try: config.read_file(conf) # py3 except AttributeError: config.readfp(conf) # py2 except IOError as e: log.error('cannot open config file {}'.format(config_file)) sys.exit(1) nps_port = config.getint("General", "Port") nps_bindip = config.get("General", "IP") min_cache_imgs = config.getint("Cache", "Images") min_cache_imgs_before_refill = config.getint("Cache", "Images_min_limit") user_agent = config.get("General", "Useragent") imageSum = config.get("General","imageSum",fallback=None) logverbosity = config.get("Logging", "Verbosity") log = logging.getLogger(config.get("Logging", "Log_name")) #hdlr = logging.FileHandler(config.get("Logging", "File"))
""" from configparser import RawConfigParser from venusianconfiguration import configure from io import StringIO import os import pkg_resources # Register pipelines for resource in pkg_resources.resource_listdir(__package__, ''): name, ext = os.path.splitext(resource) if ext == '.cfg': # Parse to read title and description data = pkg_resources.resource_string(__package__, resource) config = RawConfigParser() try: config.read_file(StringIO(data.decode('utf-8'))) except AttributeError: # noinspection PyDeprecation config.readfp(StringIO(data.decode('utf-8'))) # Register configure.transmogrifier.pipeline( name=name, title=config.get('transmogrifier', 'title'), description=config.get('transmogrifier', 'description'), configuration=resource )
# argument parser arg_parser = argparse.ArgumentParser() arg_parser.add_argument('-c', '--config-file', metavar='<file>', type=argparse.FileType('r'), default=open(path.join(_file_path, 'config.ini'), 'r'), help='a file path to the config ini', dest="config_file") args = arg_parser.parse_args() ## configuration config = RawConfigParser() config.read(path.join(_file_path, 'config.defaults.ini')) try: config.read_file(args.config_file) # py3 except AttributeError: config.readfp(args.config_file) # py2 args.config_file.close() nps_port = config.getint("General", "Port") nps_bindip = config.get("General", "IP") min_cache_imgs = config.getint("Cache", "Images") min_cache_imgs_before_refill = config.getint("Cache", "Images_min_limit") user_agent = config.get("General", "Useragent") logverbosity = config.get("Logging", "Verbosity") logger = logging.getLogger(config.get("Logging", "Log_name")) hdlr = logging.FileHandler(config.get("Logging", "File")) hdlr.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s')) logger.addHandler(hdlr) logger.setLevel(logverbosity.upper())