FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/ConfigFile.py at master · bmajoros/python · GitHub
bmajoros
python
Repository navigation
Code
Issues
Pull requests
2
(2)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
ConfigFile.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
69 lines (61 loc) · 2.44 KB
Breadcrumbs
python
/
ConfigFile.py
Copy path
File metadata and controls
executable file
·
69 lines (61 loc) · 2.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#====================================================================
# Copyright (C)2016 William H. Majoros (martiandna@gmail.com).
# This is OPEN SOURxCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
#====================================================================
from
__future__
import
(
absolute_import
,
division
,
print_function
,
unicode_literals
,
generators
,
nested_scopes
,
with_statement
)
from
builtins
import
(
bytes
,
dict
,
int
,
list
,
object
,
range
,
str
,
ascii
,
chr
,
hex
,
input
,
next
,
oct
,
open
,
pow
,
round
,
super
,
filter
,
map
,
zip
)
import
re
from
Rex
import
Rex
######################################################################
# Attributes:
# hash
# Methods:
# configFile=ConfigFile(filename)
# value=configFile.lookup(key)
# value=configFile.lookupOrDie(key)
# list=configFile.lookupList(key,sep=", ")
# list=configFile.lookupListOrDie(key,sep=", ")
# Private methods:
# self.load(filename)
######################################################################
class
ConfigFile
:
"""ConfigFile stores variable assignments as key-value pairs
in a hash
"""
def
__init__
(
self
,
filename
):
self
.
hash
=
{}
self
.
load
(
filename
)
self
.
rex
=
Rex
()
def
lookupListOrDie
(
self
,
key
,
sep
=
"[,\s]+"
):
value
=
self
.
lookupList
(
key
,
sep
)
if
(
value
is
None
):
raise
Exception
(
key
+
" not defined in config file
\n
"
)
return
value
def
lookupList
(
self
,
key
,
sep
=
"[,\s]+"
):
value
=
self
.
lookup
(
key
)
if
(
value
is
None
):
return
value
return
self
.
rex
.
split
(
sep
,
value
)
def
lookup
(
self
,
key
):
return
self
.
hash
.
get
(
key
)
#return self.hash[key]
def
lookupOrDie
(
self
,
key
):
if
(
self
.
hash
[
key
]
is
None
):
raise
Exception
(
key
+
" not defined in config file
\n
"
)
return
self
.
hash
[
key
]
def
load
(
self
,
filename
):
hash
=
self
.
hash
with
open
(
filename
,
"r"
)
as
fh
:
while
(
True
):
line
=
fh
.
readline
()
if
(
not
line
):
break
match
=
re
.
search
(
"^([^#]*)#"
,
line
);
if
(
match
):
line
=
match
.
group
(
1
)
#match=re.search("^\s*(\S+)\s*=\s*(\S+)",line);
match
=
re
.
search
(
"^\s*(\S+)\s*=\s*(\S+(\s+\S+)*)"
,
line
);
if
(
match
):
key
=
match
.
group
(
1
)
value
=
match
.
group
(
2
)
hash
[
key
]
=
value
Back
|
FazBrowse Home
|
New Git URL