def process_config(self): config = super(Application, self).process_config() if 'ETHEREUM_NODE_URL' in os.environ: config['ethereum'] = {'url': os.environ['ETHEREUM_NODE_URL']} if 'PUSH_URL' in os.environ: config.setdefault('pushserver', SectionProxy( config, 'pushserver'))['url'] = os.environ['PUSH_URL'] if 'PUSH_PASSWORD' in os.environ: config.setdefault('pushserver', SectionProxy( config, 'pushserver'))['password'] = os.environ['PUSH_PASSWORD'] if 'PUSH_USERNAME' in os.environ: config.setdefault('pushserver', SectionProxy( config, 'pushserver'))['username'] = os.environ['PUSH_USERNAME'] if 'GCM_SERVER_KEY' in os.environ: config.setdefault('gcm', SectionProxy( config, 'gcm'))['server_key'] = os.environ['GCM_SERVER_KEY'] configure_logger(services_log) configure_logger(monitor_log) return config
def __init__(self) -> None: parser = ConfigParser() path = Path.home() / Path(".plot-cli/config") if path.exists(): parser.read(path) self.settings = SectionProxy(parser, "settings")
def __init__(self, defaults=None, dict_type=_default_dict, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=DEFAULTSECT, interpolation=_UNSET, converters=_UNSET): # replacement of ConfigParser.__init__, do not call super-class constructor self._dict = dict_type self._defaults = dict_type() self._sections = dict_type() self._proxies = dict_type() self._cache = dict() self._comment_prefixes = tuple(comment_prefixes or ()) self._inline_comment_prefixes = tuple(inline_comment_prefixes or ()) self._strict = strict self._allow_no_value = allow_no_value self._empty_lines_in_values = empty_lines_in_values self.default_section = default_section self._converters = ConverterMapping(self) if (converters is not _UNSET): self._converters.update(converters) self._proxies[default_section] = SectionProxy(self, default_section) if defaults: for key, value in defaults.items(): self._defaults[self.optionxform(key)] = value self._delimiters = tuple(delimiters) if delimiters == ('=', ':'): self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE else: d = "|".join(re_escape(d) for d in delimiters) if allow_no_value: self._optcre = re_compile(self._OPT_NV_TMPL.format(delim=d), RE_VERBOSE) else: self._optcre = re_compile(self._OPT_TMPL.format(delim=d), RE_VERBOSE) if (interpolation is None): self._interpolation = Interpolation() elif (interpolation is _UNSET): self._interpolation = ExtendedInterpolation() else: self._interpolation = interpolation
def process_config(self): config = super().process_config() if 'PUSH_URL' in os.environ: config.setdefault('pushserver', SectionProxy( config, 'pushserver'))['url'] = os.environ['PUSH_URL'] if 'PUSH_PASSWORD' in os.environ: config.setdefault('pushserver', SectionProxy( config, 'pushserver'))['password'] = os.environ['PUSH_PASSWORD'] if 'PUSH_USERNAME' in os.environ: config.setdefault('pushserver', SectionProxy( config, 'pushserver'))['username'] = os.environ['PUSH_USERNAME'] if 'GCM_SERVER_KEY' in os.environ: config.setdefault('gcm', SectionProxy( config, 'gcm'))['server_key'] = os.environ['GCM_SERVER_KEY'] configure_logger(log) return config
def process_config(self): tornado.options.parse_command_line() config = configparser.ConfigParser() if os.path.exists(tornado.options.options.config): config.read(tornado.options.options.config) self.asyncio_loop = asyncio.get_event_loop() # verify config and set default values if 'general' not in config: config['general'] = {'debug': 'false'} elif 'debug' not in config['general']: config['debug'] = 'false' if 'DATABASE_URL' in os.environ: if 'PGSQL_STUNNEL_ENABLED' in os.environ and os.environ[ 'PGSQL_STUNNEL_ENABLED'] == '1': p = urllib.parse.urlparse(os.environ['DATABASE_URL']) config['database'] = { 'host': '/tmp/.s.PGSQL.6101', 'database': p.path[1:] } if p.username: config['database']['user'] = p.username if p.password: config['database']['password'] = p.password else: config['database'] = {'dsn': os.environ['DATABASE_URL']} if 'MAX_DATABASE_CONNECTIONS' in os.environ: config['database']['max_size'] = os.environ[ 'MAX_DATABASE_CONNECTIONS'] if 'MIN_DATABASE_CONNECTIONS' in os.environ: config['database']['min_size'] = os.environ[ 'MIN_DATABASE_CONNECTIONS'] if 'REDIS_URL' in os.environ: config['redis'] = {'url': os.environ['REDIS_URL']} if 'EXECUTOR_MAX_WORKERS' in os.environ: config['executor'] = { 'max_workers': os.environ['EXECUTOR_MAX_WORKERS'] } if 'COOKIE_SECRET' in os.environ: config['general']['cookie_secret'] = os.environ['COOKIE_SECRET'] if 'ENFORCE_HTTPS' in os.environ: mode = os.environ['ENFORCE_HTTPS'] if mode not in ['reject', 'redirect']: mode = 'redirect' config['general']['enforce_https'] = mode if 'MIXPANEL_TOKEN' in os.environ: config.setdefault('mixpanel', SectionProxy( config, 'mixpanel'))['token'] = os.environ['MIXPANEL_TOKEN'] if 'SLACK_LOG_URL' in os.environ: config.setdefault('logging', SectionProxy( config, 'logging'))['slack_webhook_url'] = os.environ['SLACK_LOG_URL'] if 'logging' in config and 'slack_webhook_url' in config['logging']: if 'SLACK_LOG_USERNAME' in os.environ: config['logging']['slack_log_username'] = os.environ[ 'SLACK_LOG_USERNAME'] if 'SLACK_LOG_LEVEL' in os.environ: config['logging']['slack_log_level'] = os.environ[ 'SLACK_LOG_LEVEL'] handler = SlackLogHandler( config['logging'].get('slack_log_username', None), {'default': config['logging']['slack_webhook_url']}, level=config['logging'].get('slack_log_level', None)) log.addHandler(handler) if 'LOG_LEVEL' in os.environ: config.setdefault('logging', SectionProxy( config, 'logging'))['level'] = os.environ['LOG_LEVEL'] if 'logging' in config and 'level' in config['logging']: level = getattr(logging, config['logging']['level'].upper(), None) if level: log.setLevel(level) else: log.warning( "log level is set in config but does not match one of `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`" ) # configure default torando loggers configure_logger(app_log) configure_logger(gen_log) configure_logger(access_log) return config
def _read(self, fp, fpname): """Parse a sectioned configuration file. Each section in a configuration file contains a header, indicated by a name in square brackets (`[]'), plus key/value options, indicated by `name' and `value' delimited with a specific substring (`=' or `:' by default). Values can span multiple lines, as long as they are indented deeper than the first line of the value. Depending on the parser's mode, blank lines may be treated as parts of multiline values or ignored. Configuration files may include comments, prefixed by specific characters (`#' and `;' by default). Comments may appear on their own in an otherwise empty line or may be entered in lines holding values or section names. This implementation is extended from the original to also accept .. code:: ini #include <file or pisa_resource> or .. code:: ini #include <file or pisa_resource> as <section_name> syntax anywhere in the file, which switches (via :class:`MutableMultiFileIterator`) to the new file as if it were in-lined within the original file. The latter syntax also prepends a section header .. code:: ini [section_name] before the text of the specified file or pisa_resource. """ elements_added = set() cursect = None # None, or a dictionary sectname = None optname = None lineno = 0 indent_level = 0 e = None # None, or an exception file_iter = MutableMultiFileIterator(fp=fp, fpname=fpname) self.file_iterators.append(file_iter) for record in file_iter: fpname = record['fpname'] lineno = record['lineno'] line = record['line'] comment_start = sys.maxsize # strip inline comments inline_prefixes = dict( (p, -1) for p in self._inline_comment_prefixes) while comment_start == sys.maxsize and inline_prefixes: next_prefixes = {} for prefix, index in inline_prefixes.items(): index = line.find(prefix, index + 1) if index == -1: continue next_prefixes[prefix] = index if index == 0 or (index > 0 and line[index - 1].isspace()): comment_start = min(comment_start, index) inline_prefixes = next_prefixes # parse #include statement include_info = self._get_include_info(line) if include_info: file_iter.switch_to_file(fpname=include_info['file']) if include_info['as']: as_header = '[%s]\n' % include_info['as'] file_iter.switch_to_file( # Aaron Fienberg # commented out as part of python3 update # fp=StringIO(as_header.decode('utf-8')) fp=StringIO(as_header)) continue # strip full line comments for prefix in self._comment_prefixes: if line.strip().startswith(prefix): comment_start = 0 break if comment_start == sys.maxsize: comment_start = None value = line[:comment_start].strip() if not value: if self._empty_lines_in_values: # add empty line to the value, but only if there was no # comment on the line if (comment_start is None and cursect is not None and optname and cursect[optname] is not None): cursect[optname].append('') # newlines added at join else: # empty line marks end of value indent_level = sys.maxsize continue # continuation line? first_nonspace = self.NONSPACECRE.search(line) cur_indent_level = first_nonspace.start() if first_nonspace else 0 if (cursect is not None and optname and cur_indent_level > indent_level): cursect[optname].append(value) # a section header or option header? else: indent_level = cur_indent_level # is it a section header? mo = self.SECTCRE.match(value) if mo: sectname = mo.group('header') if sectname in self._sections: if self._strict and sectname in elements_added: raise DuplicateSectionError( sectname, fpname, lineno) cursect = self._sections[sectname] elements_added.add(sectname) elif sectname == self.default_section: cursect = self._defaults else: cursect = self._dict() self._sections[sectname] = cursect self._proxies[sectname] = SectionProxy(self, sectname) elements_added.add(sectname) # So sections can't start with a continuation line optname = None # no section header in the file? elif cursect is None: raise MissingSectionHeaderError(fpname, lineno, line) # an option line? else: mo = self._optcre.match(value) if mo: optname, vi, optval = mo.group('option', 'vi', 'value') # pylint: disable=unused-variable if not optname: e = self._handle_error(e, fpname, lineno, line) optname = self.optionxform(optname.rstrip()) if (self._strict and (sectname, optname) in elements_added): raise DuplicateOptionError(sectname, optname, fpname, lineno) elements_added.add((sectname, optname)) # This check is fine because the OPTCRE cannot # match if it would set optval to None if optval is not None: optval = optval.strip() cursect[optname] = [optval] else: # valueless option handling cursect[optname] = None else: # a non-fatal parsing error occurred. set up the # exception but keep going. the exception will be # raised at the end of the file and will contain a # list of all bogus lines e = self._handle_error(e, fpname, lineno, line) # if any parsing errors occurred, raise an exception if e: raise e self._join_multiline_values()
def __init__(self, config=None): if config is None: config = SectionProxy(ConfigParser(), self.section) self.config = config
def _read(self, fp, fpname): """ MUGEN flavored configuration parsing Parse a sectioned configuration file. Each section in a configuration file contains a header, indicated by a name in square brackets (`[]'), plus key/value options, indicated by `name' and `value' delimited with a specific substring (`=' or `:' by default). Values can span multiple lines, as long as they are indented deeper than the first line of the value. Depending on the parser's mode, blank lines may be treated as parts of multiline values or ignored. Configuration files may include comments, prefixed by specific characters (`#' and `;' by default). Comments may appear on their own in an otherwise empty line or may be entered in lines holding values or section names. """ elements_added = set() cursect = None # None, or a dictionary sectname = None optname = None indent_level = 0 # some people like to include credits before the first section header # this will accommodate this, even if it is not ini file spec. skip_header = True e = None # None, or an exception for lineno, line in enumerate(fp, start=1): comment_start = sys.maxsize # strip inline comments inline_prefixes = {p: -1 for p in self._inline_comment_prefixes} while comment_start == sys.maxsize and inline_prefixes: next_prefixes = {} for prefix, index in inline_prefixes.items(): index = line.find(prefix, index + 1) if index == -1: continue next_prefixes[prefix] = index if index == 0 or (index > 0 and line[index - 1].isspace()): comment_start = min(comment_start, index) inline_prefixes = next_prefixes # strip full line comments for prefix in self._comment_prefixes: if line.strip().startswith(prefix): comment_start = 0 break if comment_start == sys.maxsize: comment_start = None value = line[:comment_start].strip() if not value: if self._empty_lines_in_values: # add empty line to the value, but only if there was no # comment on the line if (comment_start is None and cursect is not None and optname and cursect[optname] is not None): cursect[optname].append('') # newlines added at join else: # empty line marks end of value indent_level = sys.maxsize continue # continuation line? first_nonspace = self.NONSPACECRE.search(line) cur_indent_level = first_nonspace.start() if first_nonspace else 0 if cursect is not None and optname and cur_indent_level > indent_level: cursect[optname].append(value) # a section header or option header? else: indent_level = cur_indent_level # is it a section header? mo = self.SECTCRE.match(value) if mo: skip_header = False # LT: lower case section names sectname = mo.group('header').lower() if sectname in self._sections: if self._strict and sectname in elements_added: raise DuplicateSectionError( sectname, fpname, lineno) cursect = self._sections[sectname] elements_added.add(sectname) elif sectname == self.default_section: cursect = self._defaults else: cursect = self._dict() self._sections[sectname] = cursect self._proxies[sectname] = SectionProxy(self, sectname) elements_added.add(sectname) # So sections can't start with a continuation line optname = None # no section header in the file? # just skip it elif cursect is None: indent_level = sys.maxsize if not skip_header: sectname = None optname = None cursect = None # raise MissingSectionHeaderError(fpname, lineno, line) # an option line? else: mo = self._optcre.match(value) if mo: optname, vi, optval = mo.group('option', 'vi', 'value') if not optname: e = self._handle_error(e, fpname, lineno, line) optname = self.optionxform(optname.rstrip()) if (self._strict and (sectname, optname) in elements_added): raise DuplicateOptionError(sectname, optname, fpname, lineno) elements_added.add((sectname, optname)) # This check is fine because the OPTCRE cannot # match if it would set optval to None if optval is not None: optval = optval.strip() cursect[optname] = [optval] else: # valueless option handling cursect[optname] = None else: # a non-fatal parsing error occurred. set up the # exception but keep going. the exception will be # raised at the end of the file and will contain a # list of all bogus lines # LT: allow bogus lines # e = self._handle_error(e, fpname, lineno, line) pass # if any parsing errors occurred, raise an exception if e: raise e self._join_multiline_values()
def __getitem__(self, key): if key != self.default_section and not self.has_section(key): return SectionProxy(self, key) return self._proxies[key]