Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
groupe-calcul
spip2pelican
Commits
fbb517aa
Commit
fbb517aa
authored
Jun 27, 2018
by
Matthieu Boileau
Browse files
Color logging in process_yaml.py
Fix
#22
parent
37b8208b
Changes
1
Hide whitespace changes
Inline
Side-by-side
process_yaml.py
View file @
fbb517aa
...
...
@@ -6,12 +6,15 @@ Split YAML file into separate files and clean them
import
argparse
import
codecs
from
colorlog
import
ColoredFormatter
import
itertools
as
it
import
logging
import
os
import
re
from
ruamel.yaml
import
YAML
from
ruamel.yaml.reader
import
Reader
import
shutil
import
sys
SPIPFILES
=
"spip_auteurs.yml"
,
\
"spip_auteurs_liens.yml"
,
\
...
...
@@ -70,7 +73,7 @@ def force_encode(line, iline, codecs=('cp1252', 'utf8')):
try
:
return
line
.
decode
(
'utf-8'
).
encode
(
i
).
decode
()
except
(
UnicodeDecodeError
,
UnicodeEncodeError
)
as
e
:
print
(
f
"Line =
{
iline
}
Error =
{
e
}
"
)
logger
.
debug
(
f
"Line =
{
iline
}
Error =
{
e
}
"
)
def
clean_yaml
(
yml_filename
):
...
...
@@ -78,7 +81,7 @@ def clean_yaml(yml_filename):
base_filename
=
os
.
path
.
splitext
(
yml_filename
)[
0
]
output_filename
=
base_filename
+
"_clean.yml"
print
(
f
">>> Cleaning
{
yml_filename
}
->
{
output_filename
}
"
)
logger
.
info
(
f
">>> Cleaning
{
yml_filename
}
->
{
output_filename
}
"
)
yaml
=
YAML
(
typ
=
'safe'
)
# Load original yaml as a list
...
...
@@ -112,7 +115,7 @@ def clean_yaml(yml_filename):
def
reset_output_directory
():
"""Erase existing output files and create empty output directories"""
if
os
.
path
.
exists
(
OUTPUTDIR
):
print
(
f
">>> Cleaning output directory: ./
{
OUTPUTDIR
}
/"
)
logger
.
info
(
f
">>> Cleaning output directory: ./
{
OUTPUTDIR
}
/"
)
shutil
.
rmtree
(
OUTPUTDIR
)
os
.
makedirs
(
OUTPUTDIR
)
...
...
@@ -121,7 +124,7 @@ def reset_output_directory():
def
split_yaml
(
yml_filename
):
"""Split Spip YAML file into SPIPFILES"""
print
(
f
">>> Splitting
{
yml_filename
}
into:"
)
logger
.
info
(
f
">>> Splitting
{
yml_filename
}
into:"
)
with
codecs
.
open
(
yml_filename
,
"r"
,
encoding
=
'utf-8'
,
errors
=
'ignore'
)
as
yml_file
:
filename
=
''
...
...
@@ -131,7 +134,7 @@ def split_yaml(yml_filename):
filename
=
f
"
{
list
(
group
)[
0
].
split
(
'.'
)[
1
].
strip
()
}
.yml"
filepath
=
os
.
path
.
join
(
OUTPUTDIR
,
filename
)
if
not
key
and
filename
in
SPIPFILES
:
print
(
f
"
{
filename
}
"
)
logger
.
info
(
f
"
{
filename
}
"
)
content
=
""
.
join
(
list
(
group
))
with
open
(
filepath
,
'w'
)
as
yml_subfile
:
yml_subfile
.
write
(
content
)
...
...
@@ -152,6 +155,19 @@ def parse_cl_args():
if
__name__
==
'__main__'
:
logger
=
logging
.
getLogger
()
logger
.
setLevel
(
logging
.
DEBUG
)
# create console handler with higher log level and colored output
ch
=
logging
.
StreamHandler
(
sys
.
stdout
)
ch
.
setLevel
(
logging
.
DEBUG
)
LOGFORMAT
=
"%(log_color)s%(message)s%(reset)s"
color_formatter
=
ColoredFormatter
(
LOGFORMAT
)
ch
.
setFormatter
(
color_formatter
)
# add the handlers to logger
logger
.
addHandler
(
ch
)
args
=
parse_cl_args
()
reset_output_directory
()
split_yaml
(
args
.
spipfile
)
...
...
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