Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
poezio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
205
Issues
205
List
Boards
Labels
Service Desk
Milestones
Merge Requests
10
Merge Requests
10
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
poezio
poezio
Commits
a13d768e
Commit
a13d768e
authored
Dec 15, 2010
by
louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config file can now have various section
parent
56dee2c7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
16 deletions
+21
-16
src/config.py
src/config.py
+21
-16
No files found.
src/config.py
View file @
a13d768e
...
...
@@ -20,6 +20,8 @@ Defines the global config instance, used to get or set (and save) values
from/to the config file
"""
DEFSECTION
=
"Poezio"
from
configparser
import
RawConfigParser
,
NoOptionError
from
os
import
environ
,
makedirs
,
path
from
shutil
import
copy2
...
...
@@ -30,12 +32,11 @@ class Config(RawConfigParser):
load/save the config to a file
"""
def
__init__
(
self
,
file_name
):
self
.
defsection
=
"Poezio"
self
.
file_name
=
file_name
RawConfigParser
.
__init__
(
self
,
None
)
RawConfigParser
.
read
(
self
,
file_name
)
def
get
(
self
,
option
,
default
):
def
get
(
self
,
option
,
default
,
section
=
DEFSECTION
):
"""
get a value from the config but return
a default value if it is not found
...
...
@@ -55,44 +56,47 @@ class Config(RawConfigParser):
return
default
return
res
def
__get
(
self
,
option
):
def
__get
(
self
,
option
,
section
=
DEFSECTION
):
"""
facility for RawConfigParser.get
"""
return
RawConfigParser
.
get
(
self
,
se
lf
.
defse
ction
,
option
)
return
RawConfigParser
.
get
(
self
,
section
,
option
)
def
getstr
(
self
,
option
):
def
getstr
(
self
,
option
,
section
=
DEFSECTION
):
"""
get a value and returns it as a string
"""
return
self
.
__get
(
option
)
return
self
.
__get
(
option
,
section
)
def
getint
(
self
,
option
):
def
getint
(
self
,
option
,
section
=
DEFSECTION
):
"""
get a value and returns it as an int
"""
try
:
return
int
(
self
.
__get
(
option
))
return
int
(
self
.
__get
(
option
,
section
))
except
ValueError
:
return
-
1
def
getfloat
(
self
,
option
):
def
getfloat
(
self
,
option
,
section
=
DEFSECTION
):
"""
get a value and returns it as a float
"""
return
float
(
self
.
__get
(
option
))
return
float
(
self
.
__get
(
option
,
section
))
def
getboolean
(
self
,
option
):
def
getboolean
(
self
,
option
,
section
=
DEFSECTION
):
"""
get a value and returns it as a boolean
"""
return
RawConfigParser
.
getboolean
(
self
,
se
lf
.
defse
ction
,
option
)
return
RawConfigParser
.
getboolean
(
self
,
section
,
option
)
def
write_in_file
(
self
,
section
,
option
,
value
):
"""
Our own way to save write the value in the file
Just find the right section, and then find the
right option, and edit it.
TODO: make it write also new values in the file, not just what did already
exist
"""
df
=
open
(
self
.
file_name
,
'r'
)
lines_before
=
[
line
.
strip
()
for
line
in
df
.
readlines
()]
...
...
@@ -105,7 +109,8 @@ class Config(RawConfigParser):
we_are_in_the_right_section
=
True
else
:
we_are_in_the_right_section
=
False
if
line
.
startswith
(
option
)
and
we_are_in_the_right_section
:
if
(
line
.
startswith
(
'%s '
%
(
option
,))
or
line
.
startswith
(
'%s='
%
(
option
,)))
and
we_are_in_the_right_section
:
line
=
'%s = %s'
%
(
option
,
value
)
result_lines
.
append
(
line
)
df
=
open
(
self
.
file_name
,
'w'
)
...
...
@@ -113,13 +118,13 @@ class Config(RawConfigParser):
df
.
write
(
'%s
\n
'
%
line
)
df
.
close
()
def
set_and_save
(
self
,
option
,
value
):
def
set_and_save
(
self
,
option
,
value
,
section
=
DEFSECTION
):
"""
set the value in the configuration then save it
to the file
"""
RawConfigParser
.
set
(
self
,
se
lf
.
defse
ction
,
option
,
value
)
self
.
write_in_file
(
se
lf
.
defse
ction
,
option
,
value
)
RawConfigParser
.
set
(
self
,
section
,
option
,
value
)
self
.
write_in_file
(
section
,
option
,
value
)
# creates the configuration directory if it doesn't exist
# and copy the default config in it
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment