comparison configmix/__init__.py @ 274:90bbade12d8e

Docu: use local lookup firstly
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 03 Oct 2020 15:50:41 +0200
parents 9733aaa261ac
children e2fd8fea1a4c
comparison
equal deleted inserted replaced
273:9733aaa261ac 274:90bbade12d8e
54 def load(*files, **kwargs): 54 def load(*files, **kwargs):
55 """Load the given configuration files, merge them in the given order 55 """Load the given configuration files, merge them in the given order
56 and return the resulting configuration dictionary. 56 and return the resulting configuration dictionary.
57 57
58 :param files: the filenames of the configuration files to read and merge; 58 :param files: the filenames of the configuration files to read and merge;
59 if a filename starts with ``<dir>`` then the name is 59 if a filename starts with ``<dir>`` then the name is
60 interpreted as directory and all files are loaded in 60 interpreted as directory and all files are loaded in
61 sorted order (non-resursively, ignoring unknown filetypes) 61 sorted order (non-resursively, ignoring unknown filetypes)
62 :keyword defaults: optional configuration dictionary with some default 62 :keyword defaults: optional configuration dictionary with some default
63 settings where the settings from `files` are merged 63 settings where the settings from `files` are merged
64 into 64 into
238 238
239 239
240 USE_DEFAULT_ASSOC = object() 240 USE_DEFAULT_ASSOC = object()
241 """Marker for the default association for an extension. 241 """Marker for the default association for an extension.
242 242
243 To be used in :func:`set_assoc`. 243 To be used in :func:`.set_assoc`.
244 """ 244 """
245 245
246 246
247 def get_default_assoc(pattern): 247 def get_default_assoc(pattern):
248 """Return the default file-mode association for the :mod:`fnmatch` 248 """Return the default file-mode association for the :mod:`fnmatch`
259 259
260 260
261 mode_loaders = {} 261 mode_loaders = {}
262 """All configured associations between file modes and loader functions. 262 """All configured associations between file modes and loader functions.
263 263
264 See :data:`DEFAULT_MODE_LOADERS`. 264 See :data:`.DEFAULT_MODE_LOADERS`.
265 265
266 """ 266 """
267 267
268 _extensions = [] 268 _extensions = []
269 """All configured assiciations of filename extensions with file modes. 269 """All configured assiciations of filename extensions with file modes.
406 the new configuration that will be logically merged 406 the new configuration that will be logically merged
407 into `default` 407 into `default`
408 :param ~configmix.config.Configuration default: 408 :param ~configmix.config.Configuration default:
409 the base configuration where `user` is logically merged into 409 the base configuration where `user` is logically merged into
410 :param bool filter_comments: flag whether to filter comment keys that 410 :param bool filter_comments: flag whether to filter comment keys that
411 start with any of the items in :data:`COMMENTS` 411 start with any of the items in :data:`.COMMENTS`
412 :returns: `user` with the necessary amendments from `default`. 412 :returns: `user` with the necessary amendments from `default`.
413 If `user` is ``None`` then `default` is returned. 413 If `user` is ``None`` then `default` is returned.
414 414
415 .. note:: The configuration in `user` is augmented/changed 415 .. note:: The configuration in `user` is augmented/changed
416 **inplace**. 416 **inplace**.
437 user[k] = _merge(user[k], v, filter_comments) 437 user[k] = _merge(user[k], v, filter_comments)
438 return user 438 return user
439 439
440 440
441 def _merge(user, default, filter_comments): 441 def _merge(user, default, filter_comments):
442 """Recursion helper for :meth:`merge` 442 """Recursion helper for :func:`merge`
443 443
444 """ 444 """
445 if isinstance(user, dict) and isinstance(default, dict): 445 if isinstance(user, dict) and isinstance(default, dict):
446 for k, v in default.items(): 446 for k, v in default.items():
447 if filter_comments and _is_comment(k): 447 if filter_comments and _is_comment(k):
480 user[k] = _safe_merge(user[k], v, filter_comments) 480 user[k] = _safe_merge(user[k], v, filter_comments)
481 return user 481 return user
482 482
483 483
484 def _safe_merge(user, default, filter_comments): 484 def _safe_merge(user, default, filter_comments):
485 """Recursion helper for :meth:`safe_merge` 485 """Recursion helper for :func:`safe_merge`
486 486
487 """ 487 """
488 if isinstance(user, dict) and isinstance(default, dict): 488 if isinstance(user, dict) and isinstance(default, dict):
489 for k, v in default.items(): 489 for k, v in default.items():
490 if filter_comments and _is_comment(k): 490 if filter_comments and _is_comment(k):
498 498
499 def _filter_comments(d): 499 def _filter_comments(d):
500 """Recursively filter comments keys in the dict `d`. 500 """Recursively filter comments keys in the dict `d`.
501 501
502 Comment keys are keys that start with any of the items in 502 Comment keys are keys that start with any of the items in
503 :data:`COMMENTS`. 503 :data:`.COMMENTS`.
504 504
505 """ 505 """
506 if not isinstance(d, dict): 506 if not isinstance(d, dict):
507 return 507 return
508 # use a copy of the keys because we change `d` while iterating 508 # use a copy of the keys because we change `d` while iterating