13 def _readVerse(verse
, config
, convert_camel_case
):
14 """Reads in a verse from a config file and stores the result in config
16 verse: The lines for the current verse, not including the header.
17 config: A dictionary to store the found configurations in.
18 convert_camel_case: Whether to convert camelCase words to dashed_form.
22 # Remove spaces and newlines
23 stripline
= line
.lstrip().rstrip()
25 # Stop if the line is not left indented, or empty
26 if stripline
== line
.rstrip() or not stripline
:
29 # Extract the key:value pair
30 splitline
= stripline
.split(' = ')
31 key
= splitline
[0].rstrip()
32 value
= splitline
[1].lstrip()
34 # Try to convert to a number
41 # Try to convert boolean values
51 # If desired, convert camelCase to dashed_form
52 if convert_camel_case
and key
.lower() != key
:
59 dashed_form
+= c
.lower()
63 # Store the parsed and converted result
66 def read(lines
, convert_camel_case
=True):
67 """Reads in the specified file
69 Understands 'True', 'False', 'None' and integer values.
70 All camelCase keys are converted to dashed_form.
71 The file is expected to have the following format:
75 <any unindented or empty line>
79 path: The location of the file to read in
81 Returns: A dictionary with the key:value pairs specified in the file.
89 pos
= lines
.index("[GitStats]\n")
92 pos
= lines
.index("[GitStats]")
100 # Read in this verse, updates config with the found configurations
101 _readVerse(lines
, config
, convert_camel_case
)
105 def _getDefaultConfigPaths():
106 """Returns a list of config paths to use by default
111 path
= "~/.gitconfig"
112 path
= os
.path
.expanduser(path
)
116 path
= os
.path
.join(repo
.wd
, '.git', 'config')
119 paths
.append("config")
123 def _readConfigPaths(paths
, convert_camel_case
):
124 """Reads the config files at the specified paths
126 When multiple values for a key are read in, the one that
127 is read in the last is used. (E.g., when reading in the
128 config files, no effort is made to prevent existing
129 values being overwritten).
132 paths: The paths to the config files.
133 convert_camel_case: Whether to convert camelCase to dashed_form.
138 # Check all the configuration files and parse them
140 if not os
.path
.isfile(path
):
144 lines
= file.readlines()
146 config
= read(lines
, convert_camel_case
)
148 # Add the found keys to our result
149 for key
, value
in config
.iteritems():
154 def readDefaultConfigs(convert_camel_case
=True):
155 """Reads all default config files and returns the result
157 When a value is read in multiple times the one read in
158 the last is used. The following files are tried in the
165 paths
= _getDefaultConfigPaths()
166 result
= _readConfigPaths(paths
, convert_camel_case
)
170 def extractOptions(options
, options_list
):
171 """Extracts options and returns the result
173 The options are extracted from the specified options and
174 from the 'config' file.
177 opts
= readDefaultConfigs()
179 result
= OptionList()
181 for opt
in options_list
:
182 if getattr(options
, opt
, None) == None:
183 val
= opts
.get(opt
, None)
185 val
= getattr(options
, opt
)
187 if val
== None and getattr(result
, opt
, None) != None:
190 setattr(result
, opt
, val
)
195 """Parses all config files and prints the result
197 Runs once with convert_camel_case on, and once with it off.
203 result
= readDefaultConfigs()
205 print("convert_camel_case is ON:")
206 for key
, value
in result
.iteritems():
207 print("%s: %s" % (key
, str(value
)))
209 result
= readDefaultConfigs(False)
212 print("convertCamelCase is OFF:")
213 for key
, value
in result
.iteritems():
214 print("%s: %s" % (key
, str(value
)))
216 if __name__
== '__main__':