Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
nomad-lab
nomad-meta-info
Commits
85c0a87a
Commit
85c0a87a
authored
Jul 12, 2018
by
Mohamed, Fawzi Roberto (fawzi)
Browse files
reformatting code
parent
65db883f
Changes
1
Hide whitespace changes
Inline
Side-by-side
meta_info/renames/renamer.py
View file @
85c0a87a
...
...
@@ -7,6 +7,7 @@ import logging
from
io
import
open
from
io
import
StringIO
def
loadRenamesFile
(
renamesPath
):
"""Loads the content of a rename file"""
renamesRe
=
re
.
compile
(
...
...
@@ -24,6 +25,7 @@ def loadRenamesFile(renamesPath):
logging
.
warn
(
"Unexpected line %r in %s"
,
line
,
renamesPath
)
return
renames
def
renamesSearchRe
(
renames
):
"""creates a regular expression that matches the words that should be renamed"""
res
=
StringIO
()
...
...
@@ -39,11 +41,13 @@ def renamesSearchRe(renames):
renameReStr
=
res
.
getvalue
()
return
re
.
compile
(
renameReStr
)
def
replaceInFile
(
filePath
,
replacements
):
"""performs the replacements in the given file"""
renameRe
=
renamesSearchRe
(
replacements
)
outF
=
tempfile
.
NamedTemporaryFile
(
mode
=
"w"
,
suffix
=
''
,
prefix
=
'tmp'
,
dir
=
os
.
path
.
dirname
(
filePath
),
delete
=
False
,
encoding
=
'utf-8'
)
mode
=
"w"
,
suffix
=
''
,
prefix
=
'tmp'
,
dir
=
os
.
path
.
dirname
(
filePath
),
delete
=
False
,
encoding
=
'utf-8'
)
didReplace
=
{}
lineNr
=
0
with
outF
:
...
...
@@ -74,9 +78,9 @@ def replaceInFile(filePath, replacements):
os
.
rename
(
filePath
,
bkPath
)
os
.
rename
(
outF
.
name
,
filePath
)
print
(
"%r: {"
%
filePath
)
for
k
,
v
in
didReplace
.
items
():
print
(
k
,
'->'
,
replacements
[
k
],
":"
,
v
)
print
(
"}
\n
Backup in "
,
bkPath
)
for
k
,
v
in
didReplace
.
items
():
print
(
k
,
'->'
,
replacements
[
k
],
":"
,
v
)
print
(
"}
\n
Backup in "
,
bkPath
)
if
__name__
==
"__main__"
:
import
sys
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment