Skip to content

Commit 91cab38

Browse files
committed
Preprocess: output warnings only if --verbose flag is given
1 parent 4987dea commit 91cab38

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

preprocess_qch.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,49 @@
1818
# along with this program. If not, see http://www.gnu.org/licenses/.
1919

2020
from premailer import transform
21-
import os
2221
import argparse
2322
import concurrent.futures
23+
import cssutils
24+
import logging
25+
import os
26+
import warnings
27+
import io
28+
import shutil
2429

2530
def preprocess_html_merge_css(src_path, dst_path):
31+
log = logging.Logger('ignore')
32+
output = io.StringIO()
33+
handler = logging.StreamHandler(stream=output)
34+
formatter = logging.Formatter('%(levelname)s, %(message)s')
35+
handler.setFormatter(formatter)
36+
log.addHandler(handler)
37+
cssutils.log.setLog(log)
38+
2639
with open(src_path, 'r') as a_file:
27-
content = transform(a_file.read(), base_url=src_path)
40+
with warnings.catch_warnings():
41+
warnings.simplefilter("ignore")
42+
content = transform(a_file.read(), base_url=src_path)
2843
head = os.path.dirname(dst_path)
2944
os.makedirs(head, exist_ok=True)
3045
f = open(dst_path,"w")
3146
f.write(content)
3247

48+
return output.getvalue()
49+
3350
def main():
3451

3552
parser = argparse.ArgumentParser(prog='preprocess_qch.py')
36-
parser.add_argument('--src', required = True, type=str,
53+
parser.add_argument('--src', required=True, type=str,
3754
help='Source directory where raw website copy resides')
38-
parser.add_argument('--dst', required = True, type=str,
55+
parser.add_argument('--dst', required=True, type=str,
3956
help='Destination folder to put preprocessed archive to')
57+
parser.add_argument('--verbose', action='store_true', default=False,
58+
help='If set, verbose output is produced')
4059
args = parser.parse_args()
4160

42-
source_root = str(args.src)
61+
source_root = args.src
4362
dest_root = args.dst
63+
verbose = args.verbose
4464

4565
paths_list = []
4666
for root, dirs, files in os.walk(source_root):
@@ -57,10 +77,12 @@ def main():
5777
src_path, dst_path), i)
5878
for i, (src_path, dst_path) in enumerate(paths_list) ]
5979

60-
for tuple in futures:
61-
future, i = tuple
62-
future.result()
63-
print('Processed file: {}/{}'.format(i, len(paths_list)))
80+
for future, i in futures:
81+
print('Processing file: {}/{}: {}'.format(i, len(paths_list),
82+
paths_list[i][1]))
83+
output = future.result()
84+
if verbose:
85+
print(output)
6486

6587
if __name__ == "__main__":
6688
main()

0 commit comments

Comments
 (0)