13 def readAll(convert_camel_case
=True):
14 """Reads all config files and returns the result
16 When a value is read in multiple times the one read in
17 the last is used. The following files are tried in the
27 path
= os
.path
.expanduser(path
)
31 path
= os
.path
.join(repo
.wd
, '.git', 'config')
34 paths
.append("config")
38 # Check all the configuration files and parse them
40 if not os
.path
.isfile(path
):
43 config
= read(path
, convert_camel_case
)
45 # Add the found keys to our result
46 for key
, value
in config
.iteritems():
51 def _readVerse(verse
, config
, convert_camel_case
):
52 """Reads in a verse from a config file and stores the result in config
54 verse: The lines for the current verse, not including the header.
55 config: A dictionary to store the found configurations in.
56 convert_camel_case: Whether to convert camelCase words to dashed_form.
60 # Remove spaces and newlines
61 stripline
= line
.lstrip().rstrip()
63 if stripline
== line
.rstrip() or not stripline
:
66 # Extract the key:value pair
67 splitline
= stripline
.split(' = ')
68 key
= splitline
[0].rstrip()
69 value
= splitline
[1].lstrip()
71 # Try to convert to a number
78 # Try to convert boolean values
88 # If desired, convert camelCase to dashed_form
89 if convert_camel_case
and key
.lower() != key
:
96 dashed_form
+= c
.lower()
100 # Store the parsed and converted result
103 def read(path
, convert_camel_case
=True):
104 """Reads in the specified file
106 Understands 'True', 'False', 'None' and integer values.
107 All camelCase keys are converted to dashed_form.
108 The file is expected to have the following format:
112 <any unindented or empty line>
116 path: The location of the file to read in
118 Returns: A dictionary with the key:value pairs specified in the file.
124 lines
= file.readlines()
129 pos
= lines
.index("[GitStats]\n")
133 # Kick off the header
137 # Read in this verse, updates config with the found configurations
138 _readVerse(lines
, config
, convert_camel_case
)
142 def extractOptions(options
, options_list
):
143 """Extracts options and returns the result
145 The options are extracted from the specified options and
146 from the 'config' file.
153 for opt
in options_list
:
154 if getattr(options
, opt
, None) == None:
155 val
= opts
.get(opt
, None)
157 val
= getattr(options
, opt
)
159 if val
== None and getattr(result
, opt
, None) != None:
162 setattr(result
, opt
, val
)
167 """Parses all config files and prints the result
169 Runs once with convert_camel_case on, and once with it off.
177 print("convert_camel_case is ON:")
178 for key
, value
in result
.iteritems():
179 print(str(key
) + ": " + str(value
))
181 result
= readAll(False)
184 print("convertCamelCase is OFF:")
185 for key
, value
in result
.iteritems():
186 print(str(key
) + ": " + str(value
))
188 if __name__
== '__main__':