Page Menu
Home
Wildfire Games
Search
Configure Global Search
Log In
Paste
P264
Fix D4476.
Active
Public
Actions
Authored by
Freagarach
on Feb 23 2022, 6:11 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
None
Subscribers
Stan
#!/usr/bin/env python3
import
fileinput
import
glob
import
os
import
xml.etree.ElementTree
as
ET
class
SedLike
:
def
sed
(
path
,
changes
):
for
line
in
fileinput
.
input
(
path
,
inplace
=
True
):
for
change
in
changes
:
line
=
line
.
replace
(
change
[
0
],
change
[
1
])
print
(
line
,
end
=
""
)
class
TemplateFixer
:
def
__init__
(
self
,
vfs_root
):
self
.
template_folder
=
os
.
path
.
join
(
vfs_root
,
'simulation'
,
'templates'
)
def
fix_template
(
self
,
template_path
):
tree
=
ET
.
parse
(
template_path
)
root
=
tree
.
getroot
()
cmp_formation
=
root
.
find
(
'Formation'
)
if
cmp_formation
==
None
:
return
False
generic_name
=
cmp_formation
.
find
(
'FormationName'
)
icon
=
cmp_formation
.
find
(
'Icon'
)
cmp_identity
=
root
.
find
(
'Identity'
)
if
cmp_identity
==
None
:
cmp_identity
=
ET
.
Element
(
'Identity'
)
root
.
append
(
cmp_identity
)
if
generic_name
!=
None
:
cmp_identity
.
append
(
generic_name
)
cmp_formation
.
remove
(
generic_name
)
if
icon
!=
None
:
cmp_identity
.
append
(
icon
)
cmp_formation
.
remove
(
icon
)
if
cmp_formation
.
__len__
()
==
0
:
root
.
remove
(
cmp_formation
)
if
cmp_identity
.
__len__
()
==
0
:
root
.
remove
(
cmp_identity
)
return
False
root
[:]
=
sorted
(
root
,
key
=
lambda
x
:
x
.
tag
)
ET
.
indent
(
tree
)
tree
.
write
(
template_path
,
xml_declaration
=
True
,
encoding
=
'utf-8'
)
return
True
def
fix_style
(
self
,
template_path
):
self
.
changes
=
[
[
' />'
,
'/>'
],
[
"version='1.0'"
,
'version="1.0"'
],
[
"'utf-8'"
,
'"utf-8"'
]
]
SedLike
.
sed
(
template_path
,
self
.
changes
)
with
open
(
template_path
,
'a'
)
as
file
:
file
.
write
(
'
\n
'
)
def
run
(
self
):
for
template
in
glob
.
iglob
(
self
.
template_folder
+
'/**/*.xml'
,
recursive
=
True
):
if
self
.
fix_template
(
template
):
self
.
fix_style
(
template
)
if
__name__
==
'__main__'
:
script_dir
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
))
template_fixer
=
TemplateFixer
(
script_dir
)
template_fixer
.
run
()
Event Timeline
Freagarach
created this paste.
Feb 23 2022, 6:11 PM
Freagarach
created this object with visibility "Public (No Login Required)".
Log In to Comment