blob: 6b441ddc45fcf76b70128cbcd9d422e913165ad5 [file] [log] [blame]
Tor Norbye3a2425a2013-11-04 10:16:08 -08001"""Config file for coverage.py"""
2
3import os
4from coverage.backward import configparser # pylint: disable=W0622
5
6# The default line exclusion regexes
7DEFAULT_EXCLUDE = [
8 '(?i)# *pragma[: ]*no *cover',
9 ]
10
11# The default partial branch regexes, to be modified by the user.
12DEFAULT_PARTIAL = [
13 '(?i)# *pragma[: ]*no *branch',
14 ]
15
16# The default partial branch regexes, based on Python semantics.
17# These are any Python branching constructs that can't actually execute all
18# their branches.
19DEFAULT_PARTIAL_ALWAYS = [
20 'while (True|1|False|0):',
21 'if (True|1|False|0):',
22 ]
23
24
25class CoverageConfig(object):
26 """Coverage.py configuration.
27
28 The attributes of this class are the various settings that control the
29 operation of coverage.py.
30
31 """
32
33 def __init__(self):
34 """Initialize the configuration attributes to their defaults."""
35 # Defaults for [run]
36 self.branch = False
37 self.cover_pylib = False
38 self.data_file = ".coverage"
39 self.parallel = False
40 self.timid = False
41 self.source = None
42
43 # Defaults for [report]
44 self.exclude_list = DEFAULT_EXCLUDE[:]
45 self.ignore_errors = False
46 self.include = None
47 self.omit = None
48 self.partial_list = DEFAULT_PARTIAL[:]
49 self.partial_always_list = DEFAULT_PARTIAL_ALWAYS[:]
50 self.precision = 0
51
52 # Defaults for [html]
53 self.html_dir = "htmlcov"
54
55 # Defaults for [xml]
56 self.xml_output = "coverage.xml"
57
58 def from_environment(self, env_var):
59 """Read configuration from the `env_var` environment variable."""
60 # Timidity: for nose users, read an environment variable. This is a
61 # cheap hack, since the rest of the command line arguments aren't
62 # recognized, but it solves some users' problems.
63 env = os.environ.get(env_var, '')
64 if env:
65 self.timid = ('--timid' in env)
66
67 def from_args(self, **kwargs):
68 """Read config values from `kwargs`."""
69 for k, v in kwargs.items():
70 if v is not None:
71 setattr(self, k, v)
72
73 def from_file(self, *files):
74 """Read configuration from .rc files.
75
76 Each argument in `files` is a file name to read.
77
78 """
79 cp = configparser.RawConfigParser()
80 cp.read(files)
81
82 # [run]
83 if cp.has_option('run', 'branch'):
84 self.branch = cp.getboolean('run', 'branch')
85 if cp.has_option('run', 'cover_pylib'):
86 self.cover_pylib = cp.getboolean('run', 'cover_pylib')
87 if cp.has_option('run', 'data_file'):
88 self.data_file = cp.get('run', 'data_file')
89 if cp.has_option('run', 'include'):
90 self.include = self.get_list(cp, 'run', 'include')
91 if cp.has_option('run', 'omit'):
92 self.omit = self.get_list(cp, 'run', 'omit')
93 if cp.has_option('run', 'parallel'):
94 self.parallel = cp.getboolean('run', 'parallel')
95 if cp.has_option('run', 'source'):
96 self.source = self.get_list(cp, 'run', 'source')
97 if cp.has_option('run', 'timid'):
98 self.timid = cp.getboolean('run', 'timid')
99
100 # [report]
101 if cp.has_option('report', 'exclude_lines'):
102 self.exclude_list = \
103 self.get_line_list(cp, 'report', 'exclude_lines')
104 if cp.has_option('report', 'ignore_errors'):
105 self.ignore_errors = cp.getboolean('report', 'ignore_errors')
106 if cp.has_option('report', 'include'):
107 self.include = self.get_list(cp, 'report', 'include')
108 if cp.has_option('report', 'omit'):
109 self.omit = self.get_list(cp, 'report', 'omit')
110 if cp.has_option('report', 'partial_branches'):
111 self.partial_list = \
112 self.get_line_list(cp, 'report', 'partial_branches')
113 if cp.has_option('report', 'partial_branches_always'):
114 self.partial_always_list = \
115 self.get_line_list(cp, 'report', 'partial_branches_always')
116 if cp.has_option('report', 'precision'):
117 self.precision = cp.getint('report', 'precision')
118
119 # [html]
120 if cp.has_option('html', 'directory'):
121 self.html_dir = cp.get('html', 'directory')
122
123 # [xml]
124 if cp.has_option('xml', 'output'):
125 self.xml_output = cp.get('xml', 'output')
126
127 def get_list(self, cp, section, option):
128 """Read a list of strings from the ConfigParser `cp`.
129
130 The value of `section` and `option` is treated as a comma- and newline-
131 separated list of strings. Each value is stripped of whitespace.
132
133 Returns the list of strings.
134
135 """
136 value_list = cp.get(section, option)
137 values = []
138 for value_line in value_list.split('\n'):
139 for value in value_line.split(','):
140 value = value.strip()
141 if value:
142 values.append(value)
143 return values
144
145 def get_line_list(self, cp, section, option):
146 """Read a list of full-line strings from the ConfigParser `cp`.
147
148 The value of `section` and `option` is treated as a newline-separated
149 list of strings. Each value is stripped of whitespace.
150
151 Returns the list of strings.
152
153 """
154 value_list = cp.get(section, option)
155 return list(filter(None, value_list.split('\n')))
156