| Conditions | 9 |
| Total Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # |
||
| 60 | def docmanagerconfig(cfgfiles=None, include_etc=True): |
||
| 61 | """Read DocManager configuration files. The following files are |
||
| 62 | searched for and its configuration is merged together |
||
| 63 | (in this order, from lowest to highest): |
||
| 64 | |||
| 65 | * /etc/docmanager/config |
||
| 66 | * $XDG_CONFIG_HOME/docmanager/docmanager.config if not found, falls back |
||
| 67 | to ~/.config/docmanager/docmanager.config |
||
| 68 | * GIT_REPO_DIR/.git/docmanager.conf |
||
| 69 | (GIT_REPO_DIR is retrieved by the command |
||
| 70 | `git rev-parse --show-toplevel`) |
||
| 71 | * DOCMANAGER_GIT_REPO/etc/config |
||
| 72 | |||
| 73 | See the XDG Base Directory Specification: |
||
| 74 | http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
||
| 75 | |||
| 76 | :param list cfgfiles: your own list of configfiles |
||
| 77 | :param bool include_etc: Should the develop(!) 'etc/' directory included? |
||
| 78 | Only useful for development |
||
| 79 | :return: merged configuration object |
||
| 80 | :rtype: configparser.ConfigParser |
||
| 81 | |||
| 82 | """ |
||
| 83 | # Start with the global ones |
||
| 84 | configfiles = GLOBAL_CONFIG[:] |
||
| 85 | |||
| 86 | if cfgfiles is None: |
||
| 87 | # We need to assemble our configuration file list |
||
| 88 | # |
||
| 89 | # Append user config; env variable XDG_CONFIG_HOME is used if set |
||
| 90 | configfiles.append(USER_CONFIG) |
||
| 91 | |||
| 92 | # Append config when a .git repo is found |
||
| 93 | gitcfg = get_git_repo_config() |
||
| 94 | if gitcfg: |
||
| 95 | configfiles.append(gitcfg) |
||
| 96 | else: |
||
| 97 | log.debug("Using own config file %s", cfgfiles) |
||
| 98 | # In case the user passes its own config file list, use it but |
||
| 99 | # take care, it's a list: |
||
| 100 | if isinstance(cfgfiles, str): |
||
| 101 | configfiles = [cfgfiles] |
||
| 102 | else: |
||
| 103 | configfiles = cfgfiles |
||
| 104 | |||
| 105 | # Support pyvenv virtual environments; add it as a last item |
||
| 106 | # |
||
| 107 | # See http://stackoverflow.com/a/1883251 |
||
| 108 | if (cfgfiles is None) and include_etc and hasattr(sys, 'base_prefix'): |
||
| 109 | #dd = os.path.dirname(__file__) |
||
| 110 | #cc = os.path.join(dd, BASECONFIG_NAME) |
||
| 111 | #configfiles.append(cc) |
||
| 112 | #log.debug("Running inside a virtual env, using %s", cc) |
||
| 113 | # |
||
| 114 | # When code with __file__ is packed inside a zipfile, it can no longer |
||
| 115 | # assume that __file__ or __path__ contain filenames or directory |
||
| 116 | # names, and so it will fail (see also PEP 302) |
||
| 117 | # |
||
| 118 | # As such: |
||
| 119 | # 1. First try to use pkg_resources from setuptools (which is installed |
||
| 120 | # anyway in a virtual Python environment) |
||
| 121 | # 2. If that doesn't work, fallback to the __file__ method |
||
| 122 | # |
||
| 123 | # Source: |
||
| 124 | # http://peak.telecommunity.com/DevCenter/PythonEggs#accessing-package-resources |
||
| 125 | # |
||
| 126 | try: |
||
| 127 | from pkg_resources import resource_filename |
||
| 128 | cc = resource_filename(__name__, BASECONFIG_NAME) |
||
| 129 | configfiles.append(cc) |
||
| 130 | except ImportError: |
||
| 131 | # Use fallback method |
||
| 132 | dd = os.path.dirname(__file__) |
||
| 133 | cc = os.path.join(dd, BASECONFIG_NAME) |
||
| 134 | configfiles.append(cc) |
||
| 135 | log.info("Running inside a virtual env, using %r", configfiles[-1]) |
||
| 136 | |||
| 137 | config = ConfigParser() |
||
| 138 | x = config.read(configfiles) |
||
| 139 | |||
| 140 | if not x: |
||
| 141 | |||
| 142 | raise DMConfigFileNotFound(configfiles) |
||
| 143 | |||
| 144 | # Save state of configuration files |
||
| 145 | config.configfiles = configfiles |
||
| 146 | config.usedconfigfile = x |
||
| 147 | log.debug("All configfiles %s", configfiles) |
||
| 148 | log.debug("Used config file: %s", x) |
||
| 149 | |||
| 150 | return config |
||
| 151 | |||
| 178 |