Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
HDL
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RTSD
HDL
Commits
705e924f
Commit
705e924f
authored
10 years ago
by
Eric Kooistra
Browse files
Options
Downloads
Patches
Plain Diff
Added change_key_in_dict_file() to write new value for key in the filePathName file.
parent
ea7bc2d4
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tools/oneclick/base/common_dict_file.py
+25
-0
25 additions, 0 deletions
tools/oneclick/base/common_dict_file.py
with
25 additions
and
0 deletions
tools/oneclick/base/common_dict_file.py
+
25
−
0
View file @
705e924f
...
...
@@ -148,6 +148,7 @@ class CommonDictFile:
return
file_dict
def
write_dict_file
(
self
,
dicts
,
filePathNames
,
keySeparator
=
None
):
"""
Write the dictionary information to the filePathName file.
"""
if
keySeparator
==
None
:
keySeparator
=
self
.
CDF_SEPARATOR
for
fpn
,
the_dict
in
zip
(
cm
.
listify
(
filePathNames
),
cm
.
listify
(
dicts
)):
with
open
(
fpn
,
'
w
'
)
as
fp
:
...
...
@@ -155,6 +156,7 @@ class CommonDictFile:
fp
.
write
(
'
%s%s%s
\n
'
%
(
key
,
keySeparator
,
the_dict
[
key
]))
def
append_key_to_dict_file
(
self
,
filePathName
,
key
,
values
):
"""
Write append the key = value pair to the filePathName file.
"""
with
open
(
filePathName
,
'
a
'
)
as
fp
:
if
len
(
cm
.
listify
(
values
))
==
1
:
fp
.
write
(
'
%s = %s
'
%
(
key
,
values
))
...
...
@@ -163,10 +165,33 @@ class CommonDictFile:
for
v
in
cm
.
listify
(
values
):
fp
.
write
(
'
%s
\n
'
%
v
)
def
change_key_in_dict_file
(
self
,
filePathName
,
key
,
value
):
"""
Write new value for key in the filePathName file. The original key = value pair must fit on one line.
"""
# Read dict file into string
with
open
(
filePathName
,
'
r
'
)
as
fp
:
dict_string
=
fp
.
read
()
# Modify key value in dict string
key_start
=
dict_string
.
find
(
key
)
# find key
if
key_start
>
0
:
separator
=
dict_string
.
find
(
self
.
CDF_SEPARATOR
,
key_start
)
# find separator
if
separator
>
0
:
value_start
=
separator
+
1
# value start index
eol
=
dict_string
.
find
(
'
\n
'
,
value_start
)
if
eol
>
0
:
value_end
=
eol
# value end index at end of line
else
:
value_end
=
len
(
dict_string
)
# value end index at end of file
dict_string
=
dict_string
[
0
:
value_start
]
+
'
'
+
value
+
dict_string
[
value_end
:]
# Write string to dict file
with
open
(
filePathName
,
'
w
'
)
as
fp
:
fp
.
write
(
dict_string
)
def
get_filePath
(
self
,
the_dict
):
"""
Get file path to the dictionary file location.
"""
return
self
.
filePaths
[
self
.
dicts
.
index
(
the_dict
)]
def
get_filePathName
(
self
,
the_dict
):
"""
Get file path to the dictionary file location including the dictionary file name.
"""
return
self
.
filePathNames
[
self
.
dicts
.
index
(
the_dict
)]
def
get_key_values
(
self
,
key
,
dicts
=
None
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment