OLD | NEW |
| (Empty) |
1 """Config file for coverage.py""" | |
2 | |
3 import os, re, sys | |
4 from coverage.backward import string_class, iitems | |
5 | |
6 # In py3, # ConfigParser was renamed to the more-standard configparser | |
7 try: | |
8 import configparser # pylint: disable=F0401 | |
9 except ImportError: | |
10 import ConfigParser as configparser | |
11 | |
12 | |
13 class HandyConfigParser(configparser.RawConfigParser): | |
14 """Our specialization of ConfigParser.""" | |
15 | |
16 def read(self, filename): | |
17 """Read a filename as UTF-8 configuration data.""" | |
18 kwargs = {} | |
19 if sys.version_info >= (3, 2): | |
20 kwargs['encoding'] = "utf-8" | |
21 return configparser.RawConfigParser.read(self, filename, **kwargs) | |
22 | |
23 def get(self, *args, **kwargs): | |
24 v = configparser.RawConfigParser.get(self, *args, **kwargs) | |
25 def dollar_replace(m): | |
26 """Called for each $replacement.""" | |
27 # Only one of the groups will have matched, just get its text. | |
28 word = [w for w in m.groups() if w is not None][0] | |
29 if word == "$": | |
30 return "$" | |
31 else: | |
32 return os.environ.get(word, '') | |
33 | |
34 dollar_pattern = r"""(?x) # Use extended regex syntax | |
35 \$(?: # A dollar sign, then | |
36 (?P<v1>\w+) | # a plain word, | |
37 {(?P<v2>\w+)} | # or a {-wrapped word, | |
38 (?P<char>[$]) # or a dollar sign. | |
39 ) | |
40 """ | |
41 v = re.sub(dollar_pattern, dollar_replace, v) | |
42 return v | |
43 | |
44 def getlist(self, section, option): | |
45 """Read a list of strings. | |
46 | |
47 The value of `section` and `option` is treated as a comma- and newline- | |
48 separated list of strings. Each value is stripped of whitespace. | |
49 | |
50 Returns the list of strings. | |
51 | |
52 """ | |
53 value_list = self.get(section, option) | |
54 values = [] | |
55 for value_line in value_list.split('\n'): | |
56 for value in value_line.split(','): | |
57 value = value.strip() | |
58 if value: | |
59 values.append(value) | |
60 return values | |
61 | |
62 def getlinelist(self, section, option): | |
63 """Read a list of full-line strings. | |
64 | |
65 The value of `section` and `option` is treated as a newline-separated | |
66 list of strings. Each value is stripped of whitespace. | |
67 | |
68 Returns the list of strings. | |
69 | |
70 """ | |
71 value_list = self.get(section, option) | |
72 return list(filter(None, value_list.split('\n'))) | |
73 | |
74 | |
75 # The default line exclusion regexes | |
76 DEFAULT_EXCLUDE = [ | |
77 '(?i)# *pragma[: ]*no *cover', | |
78 ] | |
79 | |
80 # The default partial branch regexes, to be modified by the user. | |
81 DEFAULT_PARTIAL = [ | |
82 '(?i)# *pragma[: ]*no *branch', | |
83 ] | |
84 | |
85 # The default partial branch regexes, based on Python semantics. | |
86 # These are any Python branching constructs that can't actually execute all | |
87 # their branches. | |
88 DEFAULT_PARTIAL_ALWAYS = [ | |
89 'while (True|1|False|0):', | |
90 'if (True|1|False|0):', | |
91 ] | |
92 | |
93 | |
94 class CoverageConfig(object): | |
95 """Coverage.py configuration. | |
96 | |
97 The attributes of this class are the various settings that control the | |
98 operation of coverage.py. | |
99 | |
100 """ | |
101 def __init__(self): | |
102 """Initialize the configuration attributes to their defaults.""" | |
103 # Metadata about the config. | |
104 self.attempted_config_files = [] | |
105 self.config_files = [] | |
106 | |
107 # Defaults for [run] | |
108 self.branch = False | |
109 self.cover_pylib = False | |
110 self.data_file = ".coverage" | |
111 self.parallel = False | |
112 self.timid = False | |
113 self.source = None | |
114 | |
115 # Defaults for [report] | |
116 self.exclude_list = DEFAULT_EXCLUDE[:] | |
117 self.ignore_errors = False | |
118 self.include = None | |
119 self.omit = None | |
120 self.partial_list = DEFAULT_PARTIAL[:] | |
121 self.partial_always_list = DEFAULT_PARTIAL_ALWAYS[:] | |
122 self.precision = 0 | |
123 self.show_missing = False | |
124 | |
125 # Defaults for [html] | |
126 self.html_dir = "htmlcov" | |
127 self.extra_css = None | |
128 self.html_title = "Coverage report" | |
129 | |
130 # Defaults for [xml] | |
131 self.xml_output = "coverage.xml" | |
132 | |
133 # Defaults for [paths] | |
134 self.paths = {} | |
135 | |
136 def from_environment(self, env_var): | |
137 """Read configuration from the `env_var` environment variable.""" | |
138 # Timidity: for nose users, read an environment variable. This is a | |
139 # cheap hack, since the rest of the command line arguments aren't | |
140 # recognized, but it solves some users' problems. | |
141 env = os.environ.get(env_var, '') | |
142 if env: | |
143 self.timid = ('--timid' in env) | |
144 | |
145 MUST_BE_LIST = ["omit", "include"] | |
146 | |
147 def from_args(self, **kwargs): | |
148 """Read config values from `kwargs`.""" | |
149 for k, v in iitems(kwargs): | |
150 if v is not None: | |
151 if k in self.MUST_BE_LIST and isinstance(v, string_class): | |
152 v = [v] | |
153 setattr(self, k, v) | |
154 | |
155 def from_file(self, filename): | |
156 """Read configuration from a .rc file. | |
157 | |
158 `filename` is a file name to read. | |
159 | |
160 """ | |
161 self.attempted_config_files.append(filename) | |
162 | |
163 cp = HandyConfigParser() | |
164 files_read = cp.read(filename) | |
165 if files_read is not None: # return value changed in 2.4 | |
166 self.config_files.extend(files_read) | |
167 | |
168 for option_spec in self.CONFIG_FILE_OPTIONS: | |
169 self.set_attr_from_config_option(cp, *option_spec) | |
170 | |
171 # [paths] is special | |
172 if cp.has_section('paths'): | |
173 for option in cp.options('paths'): | |
174 self.paths[option] = cp.getlist('paths', option) | |
175 | |
176 CONFIG_FILE_OPTIONS = [ | |
177 # [run] | |
178 ('branch', 'run:branch', 'boolean'), | |
179 ('cover_pylib', 'run:cover_pylib', 'boolean'), | |
180 ('data_file', 'run:data_file'), | |
181 ('include', 'run:include', 'list'), | |
182 ('omit', 'run:omit', 'list'), | |
183 ('parallel', 'run:parallel', 'boolean'), | |
184 ('source', 'run:source', 'list'), | |
185 ('timid', 'run:timid', 'boolean'), | |
186 | |
187 # [report] | |
188 ('exclude_list', 'report:exclude_lines', 'linelist'), | |
189 ('ignore_errors', 'report:ignore_errors', 'boolean'), | |
190 ('include', 'report:include', 'list'), | |
191 ('omit', 'report:omit', 'list'), | |
192 ('partial_list', 'report:partial_branches', 'linelist'), | |
193 ('partial_always_list', 'report:partial_branches_always', 'linelist'), | |
194 ('precision', 'report:precision', 'int'), | |
195 ('show_missing', 'report:show_missing', 'boolean'), | |
196 | |
197 # [html] | |
198 ('html_dir', 'html:directory'), | |
199 ('extra_css', 'html:extra_css'), | |
200 ('html_title', 'html:title'), | |
201 | |
202 # [xml] | |
203 ('xml_output', 'xml:output'), | |
204 ] | |
205 | |
206 def set_attr_from_config_option(self, cp, attr, where, type_=''): | |
207 """Set an attribute on self if it exists in the ConfigParser.""" | |
208 section, option = where.split(":") | |
209 if cp.has_option(section, option): | |
210 method = getattr(cp, 'get'+type_) | |
211 setattr(self, attr, method(section, option)) | |
OLD | NEW |