generated from pythoninthegrasses/python_template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvert.py
More file actions
executable file
·224 lines (169 loc) · 6.94 KB
/
convert.py
File metadata and controls
executable file
·224 lines (169 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
# flake8: noqa
# fmt: off
# import atexit
import logging
import pandas as pd
import shutil
import subprocess
import time
from colorama import Fore
from functools import wraps
# from joblib import Parallel, delayed
from pathlib import Path
from pathvalidate import replace_symbol
from shlex import quote
from zipfile import ZipFile
# fmt: on
# logging prefixes
info = "INFO:"
error = "ERROR:"
warning = "WARNING:"
# Define a logger instance
logger = logging.getLogger(__name__)
# Define a stream handler for the console output
handler = logging.StreamHandler() # Customize the formatter on the console
formatter = logging.Formatter("[ %(asctime)s %(name)s ] %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
# Add the formatter to the handler
handler.setFormatter(formatter) # Add the stream handler to the logger that we will use
logger.addHandler(handler)
# Set the level of logging to be INFO instead of the default WARNING
logger.setLevel(logging.INFO)
def timeit(func):
@wraps(func)
def timeit_wrapper(*args, **kwargs):
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
total_time = end_time - start_time
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Function {func.__name__}{args} {kwargs} Took {total_time:.4f} seconds")
return result
return timeit_wrapper
notes_written = 0
notes_failed = 0
win_dict = {}
fail_dict = {}
# exclude files
exclude_list = ["archive", "nimbus_export", "subset"]
def unzip_files():
"""Unzip files in the current directory"""
# iterate through exclude list with pathlib rglob
for _ in Path(".").rglob("*"):
if any(x in _.name for x in exclude_list):
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Skipping {_.name}")
continue
if _.suffix == ".zip":
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Unzipping {_.name}")
with ZipFile(_) as zip_file:
zip_file.extractall(path=f"{_.parent}/{_.stem}")
def write_note(html_file, markdown_destination):
"""Convert html file to markdown and write to original directory"""
global notes_written, notes_failed
dest = Path(f"{markdown_destination}").resolve()
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Writing markdown to {dest}")
# escape quotes with shlex
html_file = quote(str(html_file))
# convert html to markdown
cmd = f"pandoc {html_file} --from html --to markdown_strict-raw_html"
try:
pandoc_run = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
if markdown_destination.exists():
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Markdown file {dest} already exists, skipping")
return
else:
with open(markdown_destination, "w", encoding="utf-8") as md_fp:
md_content = pandoc_run.decode()
md_fp.write(md_content)
notes_written += 1
win_dict[notes_written] = html_file
except (FileNotFoundError, subprocess.CalledProcessError):
logger.error(f"{Fore.RED}{error:<10}{Fore.RESET}Failed to convert {html_file}")
notes_failed += 1
fail_dict[notes_failed] = html_file
# @atexit.register
def cleanup_zip_files():
"""Remove zip files in the current directory"""
# iterate through exclude list with pathlib rglob
for _ in Path(".").rglob("*"):
if any(x in _.name for x in exclude_list):
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Skipping {_.name}")
continue
if _.suffix == ".zip":
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Removing {_.name}")
_.unlink()
# @atexit.register
def move_empties():
"""Move empty markdown files"""
# create archive directory
archive_dir = Path("./archive")
archive_dir.mkdir(exist_ok=True)
empties = [i for i in Path(".").rglob("*.md")]
for _ in empties:
if _.stat().st_size == 1:
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Archiving empty directory {_.parent.name}")
# move makdown directory to archive directory
try:
shutil.move(f"{_.parent}", f"./archive/")
except shutil.Error:
logger.error(f"{Fore.RED}{error:<10}{Fore.RESET}Failed to move {_.parent.name} to archive directory")
continue
# zip archive directory
if Path(f"{archive_dir}.zip").exists():
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Archive directory already exists, skipping")
else:
with ZipFile(f"{archive_dir}.zip", mode="w") as zip_file:
for file_path in archive_dir.rglob("*"):
zip_file.write(file_path, arcname=file_path.relative_to(archive_dir))
# remove archive directory
try:
shutil.rmtree("./archive")
except shutil.Error:
logger.error(f"{Fore.RED}{error:<10}{Fore.RESET}Failed to remove archive directory")
@timeit
def main():
# unzip files
unzip_files()
# TODO: add joblib parallelization after getting results of html files
# get html files
html_files = [i for i in Path(".").rglob("*.html")]
# convert html files to markdown
for _ in html_files:
html_file = _.absolute()
# directory name
dirname = html_file.parent.name
# sanitize filepath
sanitized = replace_symbol(dirname, exclude_symbols=[" ", "_", "-"])
# fix double whitespace
sanitized = " ".join(sanitized.split())
# rename directory to sanitized filepath with pathlib
filepath = Path(f"{html_file.parent.parent}/{sanitized}")
try:
html_file.parent.rename(filepath)
except OSError:
logger.error(f"{Fore.RED}{error:<10}{Fore.RESET}Failed to rename {html_file.parent.name} to {sanitized}")
continue
# filename
filename = html_file.stem + ".md"
# resolve new html file path
html_file = Path(f"{filepath}/{html_file.name}").resolve()
# create markdown file name
md_destination = Path(filepath / filename).absolute()
if md_destination.exists():
dest = Path(f"{md_destination}").resolve()
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Markdown file {dest} already exists, skipping")
continue
write_note(html_file, md_destination)
logger.info(f"{Fore.GREEN}{info:<10}{Fore.RESET}Converted {notes_written} notes, failed to convert {notes_failed} notes")
# create dataframe
wins = pd.DataFrame.from_dict(win_dict, orient="index", columns=["note"])
fails = pd.DataFrame.from_dict(fail_dict, orient="index", columns=["note"])
# write the dataframe to a csv file
df = pd.DataFrame(wins)
df.to_csv("win_list.csv", index=False)
df = pd.DataFrame(fails)
df.to_csv("fail_list.csv", index=False)
# QA-only; use `@atexit.register`
cleanup_zip_files()
move_empties()
if __name__ == "__main__":
main()