-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigparser.py
More file actions
97 lines (85 loc) · 3.59 KB
/
Copy pathconfigparser.py
File metadata and controls
97 lines (85 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
@namespace("configparser")
from Promethium import List
from collections import OrderedDict
# A small, opt-in subset of Python's configparser module: `parse(lines)`
# reads INI-style text (already split into lines — this project excludes
# filesystem APIs, so there's no file handle to read from directly) into
# an `OrderedDict[str, OrderedDict[str, str]]` (section name -> ordered
# key/value map), reusing `collections.OrderedDict` rather than inventing
# another ordered-mapping type. Supports `[section]` headers, `key = value`
# and `key: value` (both separators, like CPython), `#`/`;` comment lines,
# and blank-line skipping. Not attempted: interpolation (`%(x)s`),
# `DEFAULT` section fallback, multi-line values, or writing config back
# out.
#
# Built on the same manual-character-scan primitives `string.py`'s
# `capwords` established, plus a hand-rolled `_indexOfChar` rather than a
# native `.IndexOf` — deliberately, since Toffee's equivalent
# (`rangeOfString(...).location`) returns Cocoa's `NSNotFound` sentinel
# instead of `-1` when the character is missing (the same class of problem
# `operator.py`'s `indexOf` already avoids for `List`), and a manual scan
# sidesteps the question entirely rather than branching around it per
# target.
def _length(value: str) -> int:
if defined("COOPER") or defined("TOFFEE"):
return value.length()
else:
return value.Length
def _substring(value: str, start: int, count: int) -> str:
if defined("ECHOES") or defined("ISLAND"):
return value.Substring(start, count)
elif defined("COOPER"):
return value.substring(start, start + count)
else:
return value.substringWithRange(NSMakeRange(start, count))
def _trim(value: str) -> str:
if defined("ECHOES") or defined("ISLAND"):
return value.Trim()
elif defined("COOPER"):
return value.trim()
else:
return value.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet)
def _indexOfChar(value: str, ch: str) -> int:
length: int = _length(value)
index: int = 0
while index < length:
if _substring(value, index, 1) == ch:
return index
index += 1
return -1
def parse(lines: List[str]) -> OrderedDict[str, OrderedDict[str, str]]:
result: OrderedDict[str, OrderedDict[str, str]] = OrderedDict[str, OrderedDict[str, str]]()
currentSection: str = ""
currentMap: OrderedDict[str, str] = OrderedDict[str, str]()
hasSection: bool = False
lineIndex: int = 0
while lineIndex < len(lines):
line: str = _trim(lines.__getitem__(lineIndex))
lineIndex += 1
if _length(line) == 0:
continue
first: str = _substring(line, 0, 1)
if first == "#" or first == ";":
continue
if first == "[":
if hasSection:
result[currentSection] = currentMap
closeIndex: int = _indexOfChar(line, "]")
if closeIndex > 0:
currentSection = _substring(line, 1, closeIndex - 1)
else:
currentSection = _substring(line, 1, _length(line) - 1)
currentMap = OrderedDict[str, str]()
hasSection = True
continue
sepIndex: int = _indexOfChar(line, "=")
if sepIndex < 0:
sepIndex = _indexOfChar(line, ":")
if sepIndex < 0:
continue
key: str = _trim(_substring(line, 0, sepIndex))
value: str = _trim(_substring(line, sepIndex + 1, _length(line) - sepIndex - 1))
currentMap[key] = value
if hasSection:
result[currentSection] = currentMap
return result