Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
settingsd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
etersoft
settingsd
Commits
8f2b56b5
Commit
8f2b56b5
authored
Feb 10, 2011
by
Devaev Maxim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Editor for ini-files
parent
2d272b44
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
130 additions
and
0 deletions
+130
-0
editors.py
settingsd/tools/editors.py
+130
-0
No files found.
settingsd/tools/editors.py
View file @
8f2b56b5
...
...
@@ -140,3 +140,133 @@ class PlainEditor(object) :
return
values_list
class
IniEditor
(
object
)
:
def
__init__
(
self
)
:
object
.
__init__
(
self
)
#####
self
.
__config_file_path
=
None
self
.
__config_file_data_list
=
None
###
self
.
__comments_regexp
=
re
.
compile
(
r"\s*(?<!\\)[;#]\s*"
)
self
.
__section_regexp
=
re
.
compile
(
r"^\s*\[([^];#]+)\]"
)
self
.
__variable_regexp
=
re
.
compile
(
r"\s*=\s*"
)
### Public ###
def
open
(
self
,
config_file_path
,
sample_config_file_path
=
None
)
:
if
self
.
__config_file_path
!=
None
:
raise
AlreadyAssociated
(
"This parser already associated with config
\"
%
s
\"
"
%
self
.
__config_file_path
)
if
not
os
.
access
(
config_file_path
,
os
.
F_OK
)
:
logger
.
debug
(
"{submod}: Config file
\"
%
s
\"
does not exist"
%
(
config_file_path
))
try
:
if
sample_config_file_path
!=
None
and
os
.
access
(
sample_config_file_path
,
os
.
F_OK
)
:
shutil
.
copy2
(
sample_config_file_path
,
config_file_path
)
logger
.
debug
(
"{submod}: Config file
\"
%
s
\"
has been created from sample
\"
%
s
\"
"
%
(
config_file_path
,
sample_config_file_path
))
else
:
open
(
config_file_path
,
"w"
)
.
close
()
logger
.
debug
(
"{submod}: Created empty file
\"
%
s
\"
"
%
(
config_file_path
))
except
:
logger
.
error
(
"Cannot create config file
\"
%
s
\"
"
%
(
config_file_path
))
logger
.
attachException
()
config_file
=
open
(
config_file_path
,
"r"
)
self
.
__config_file_data_list
=
config_file
.
read
()
.
split
(
"
\n
"
)
self
.
__config_file_path
=
config_file_path
try
:
config_file
.
close
()
except
:
pass
logger
.
debug
(
"{submod}: Cached and associated config file
\"
%
s
\"
"
%
(
config_file_path
))
def
save
(
self
)
:
if
self
.
__config_file_path
==
None
:
raise
NotAssociated
(
"This parser is not associated with config"
)
config_file
=
open
(
self
.
__config_file_path
,
"w"
)
config_file
.
write
(
"
\n
"
.
join
(
self
.
__config_file_data_list
))
try
:
config_file
.
close
()
except
:
pass
logger
.
debug
(
"{submod}: Saved config file
\"
%
s
\"
"
%
(
self
.
__config_file_path
))
def
close
(
self
)
:
if
self
.
__config_file_path
==
None
:
raise
NotAssociated
(
"This parser is not associated with config"
)
logger
.
debug
(
"{submod}: Unassociated parser from config file
\"
%
s
\"
"
%
(
self
.
__config_file_path
))
self
.
__config_file_data_list
=
None
self
.
__config_file_path
=
None
###
def
setValue
(
self
,
section_name
,
variable_name
,
values_list
)
:
if
self
.
__config_file_path
==
None
:
raise
NotAssociated
(
"This parser is not associated with config"
)
if
values_list
==
None
:
values_list
=
[]
elif
not
type
(
values_list
)
.
__name__
in
(
"list"
,
"tuple"
)
:
values_list
=
[
values_list
]
active_section_flag
=
False
section_exists_flag
=
False
last_variable_index
=
len
(
self
.
__config_file_data_list
)
-
1
count
=
0
while
count
<
len
(
self
.
__config_file_data_list
)
:
record
=
self
.
__comments_regexp
.
split
(
self
.
__config_file_data_list
[
count
]
.
strip
(),
1
)[
0
]
section_match
=
self
.
__section_regexp
.
search
(
record
)
if
section_match
!=
None
:
new_active_section_flag
=
(
section_match
.
group
(
1
)
==
section_name
)
if
new_active_section_flag
:
section_exists_flag
=
True
if
active_section_flag
and
not
new_active_section_flag
:
last_variable_index
=
count
active_section_flag
=
new_active_section_flag
elif
active_section_flag
:
if
self
.
__variable_regexp
.
split
(
record
,
1
)[
0
]
==
variable_name
:
self
.
__config_file_data_list
.
pop
(
count
)
last_variable_index
=
count
continue
count
+=
1
if
not
section_exists_flag
:
self
.
__config_file_data_list
.
insert
(
last_variable_index
,
""
)
self
.
__config_file_data_list
.
insert
(
last_variable_index
+
1
,
"[
%
s]"
%
(
section_name
))
last_variable_index
+=
2
for
count
in
xrange
(
len
(
values_list
))
:
self
.
__config_file_data_list
.
insert
(
last_variable_index
+
count
,
"
%
s =
%
s"
%
(
variable_name
,
str
(
values_list
[
count
])))
def
value
(
self
,
section_name
,
variable_name
)
:
if
self
.
__config_file_path
==
None
:
raise
NotAssociated
(
"This parser is not associated with config"
)
section_founded_flag
=
False
values_list
=
[]
for
config_file_data_list_item
in
self
.
__config_file_data_list
:
record
=
self
.
__comments_regexp
.
split
(
config_file_data_list_item
.
strip
(),
1
)[
0
]
section_match
=
self
.
__section_regexp
.
search
(
record
)
if
section_match
!=
None
:
section_founded_flag
=
(
section_match
.
group
(
1
)
==
section_name
)
continue
if
section_founded_flag
:
variable_parts_list
=
self
.
__variable_regexp
.
split
(
record
,
1
)
if
variable_parts_list
[
0
]
==
variable_name
:
if
len
(
variable_parts_list
)
>
1
:
values_list
.
append
(
variable_parts_list
[
1
])
else
:
values_list
.
append
(
""
)
return
values_list
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