Kaydet (Commit) 26d513cf authored tarafından Łukasz Langa's avatar Łukasz Langa

Issue #5412: extend configparser to support mapping access

üst 47f637ce
This diff is collapsed.
This diff is collapsed.
...@@ -103,7 +103,7 @@ def _encoded(s): ...@@ -103,7 +103,7 @@ def _encoded(s):
def _create_formatters(cp): def _create_formatters(cp):
"""Create and return formatters""" """Create and return formatters"""
flist = cp.get("formatters", "keys") flist = cp["formatters"]["keys"]
if not len(flist): if not len(flist):
return {} return {}
flist = flist.split(",") flist = flist.split(",")
...@@ -111,20 +111,12 @@ def _create_formatters(cp): ...@@ -111,20 +111,12 @@ def _create_formatters(cp):
formatters = {} formatters = {}
for form in flist: for form in flist:
sectname = "formatter_%s" % form sectname = "formatter_%s" % form
opts = cp.options(sectname) fs = cp.get(sectname, "format", raw=True, fallback=None)
if "format" in opts: dfs = cp.get(sectname, "datefmt", raw=True, fallback=None)
fs = cp.get(sectname, "format", 1)
else:
fs = None
if "datefmt" in opts:
dfs = cp.get(sectname, "datefmt", 1)
else:
dfs = None
c = logging.Formatter c = logging.Formatter
if "class" in opts: class_name = cp[sectname].get("class")
class_name = cp.get(sectname, "class") if class_name:
if class_name: c = _resolve(class_name)
c = _resolve(class_name)
f = c(fs, dfs) f = c(fs, dfs)
formatters[form] = f formatters[form] = f
return formatters return formatters
...@@ -132,7 +124,7 @@ def _create_formatters(cp): ...@@ -132,7 +124,7 @@ def _create_formatters(cp):
def _install_handlers(cp, formatters): def _install_handlers(cp, formatters):
"""Install and return handlers""" """Install and return handlers"""
hlist = cp.get("handlers", "keys") hlist = cp["handlers"]["keys"]
if not len(hlist): if not len(hlist):
return {} return {}
hlist = hlist.split(",") hlist = hlist.split(",")
...@@ -140,30 +132,23 @@ def _install_handlers(cp, formatters): ...@@ -140,30 +132,23 @@ def _install_handlers(cp, formatters):
handlers = {} handlers = {}
fixups = [] #for inter-handler references fixups = [] #for inter-handler references
for hand in hlist: for hand in hlist:
sectname = "handler_%s" % hand section = cp["handler_%s" % hand]
klass = cp.get(sectname, "class") klass = section["class"]
opts = cp.options(sectname) fmt = section.get("formatter", "")
if "formatter" in opts:
fmt = cp.get(sectname, "formatter")
else:
fmt = ""
try: try:
klass = eval(klass, vars(logging)) klass = eval(klass, vars(logging))
except (AttributeError, NameError): except (AttributeError, NameError):
klass = _resolve(klass) klass = _resolve(klass)
args = cp.get(sectname, "args") args = section["args"]
args = eval(args, vars(logging)) args = eval(args, vars(logging))
h = klass(*args) h = klass(*args)
if "level" in opts: if "level" in section:
level = cp.get(sectname, "level") level = section["level"]
h.setLevel(logging._levelNames[level]) h.setLevel(logging._levelNames[level])
if len(fmt): if len(fmt):
h.setFormatter(formatters[fmt]) h.setFormatter(formatters[fmt])
if issubclass(klass, logging.handlers.MemoryHandler): if issubclass(klass, logging.handlers.MemoryHandler):
if "target" in opts: target = section.get("target", "")
target = cp.get(sectname,"target")
else:
target = ""
if len(target): #the target handler may not be loaded yet, so keep for later... if len(target): #the target handler may not be loaded yet, so keep for later...
fixups.append((h, target)) fixups.append((h, target))
handlers[hand] = h handlers[hand] = h
...@@ -197,20 +182,19 @@ def _install_loggers(cp, handlers, disable_existing): ...@@ -197,20 +182,19 @@ def _install_loggers(cp, handlers, disable_existing):
"""Create and install loggers""" """Create and install loggers"""
# configure the root first # configure the root first
llist = cp.get("loggers", "keys") llist = cp["loggers"]["keys"]
llist = llist.split(",") llist = llist.split(",")
llist = list(map(lambda x: x.strip(), llist)) llist = list(map(lambda x: x.strip(), llist))
llist.remove("root") llist.remove("root")
sectname = "logger_root" section = cp["logger_root"]
root = logging.root root = logging.root
log = root log = root
opts = cp.options(sectname) if "level" in section:
if "level" in opts: level = section["level"]
level = cp.get(sectname, "level")
log.setLevel(logging._levelNames[level]) log.setLevel(logging._levelNames[level])
for h in root.handlers[:]: for h in root.handlers[:]:
root.removeHandler(h) root.removeHandler(h)
hlist = cp.get(sectname, "handlers") hlist = section["handlers"]
if len(hlist): if len(hlist):
hlist = hlist.split(",") hlist = hlist.split(",")
hlist = _strip_spaces(hlist) hlist = _strip_spaces(hlist)
...@@ -237,13 +221,9 @@ def _install_loggers(cp, handlers, disable_existing): ...@@ -237,13 +221,9 @@ def _install_loggers(cp, handlers, disable_existing):
child_loggers = [] child_loggers = []
#now set up the new ones... #now set up the new ones...
for log in llist: for log in llist:
sectname = "logger_%s" % log section = cp["logger_%s" % log]
qn = cp.get(sectname, "qualname") qn = section["qualname"]
opts = cp.options(sectname) propagate = section.getint("propagate", fallback=1)
if "propagate" in opts:
propagate = cp.getint(sectname, "propagate")
else:
propagate = 1
logger = logging.getLogger(qn) logger = logging.getLogger(qn)
if qn in existing: if qn in existing:
i = existing.index(qn) i = existing.index(qn)
...@@ -255,14 +235,14 @@ def _install_loggers(cp, handlers, disable_existing): ...@@ -255,14 +235,14 @@ def _install_loggers(cp, handlers, disable_existing):
child_loggers.append(existing[i]) child_loggers.append(existing[i])
i = i + 1 i = i + 1
existing.remove(qn) existing.remove(qn)
if "level" in opts: if "level" in section:
level = cp.get(sectname, "level") level = section["level"]
logger.setLevel(logging._levelNames[level]) logger.setLevel(logging._levelNames[level])
for h in logger.handlers[:]: for h in logger.handlers[:]:
logger.removeHandler(h) logger.removeHandler(h)
logger.propagate = propagate logger.propagate = propagate
logger.disabled = 0 logger.disabled = 0
hlist = cp.get(sectname, "handlers") hlist = section["handlers"]
if len(hlist): if len(hlist):
hlist = hlist.split(",") hlist = hlist.split(",")
hlist = _strip_spaces(hlist) hlist = _strip_spaces(hlist)
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment