This repository was archived by the owner on Aug 27, 2024. It is now read-only.
generated from Release-Candidate/python_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
435 lines (359 loc) · 13.6 KB
/
main.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2021 Roland Csaszar
#
# Project: Obs2Org
# File: main.py
# Date: 26.10.2021
# ===============================================================================
"""Contains the program's real main entry point, `main`."""
from __future__ import annotations
import argparse
import asyncio
import subprocess # nosec
from os import path, walk
from pathlib import Path, PurePath
from typing import Coroutine, NamedTuple
from obs2org import VERSION
from obs2org.convert import convert_single_file, correct_org_mode
################################################################################
class FilePaths(NamedTuple):
"""Class holding the path to the Markdown file to convert and the path to the
Org-Mode file to generate.
"""
in_file: Path
"""Path to the Markdown file to convert."""
out_file: Path
"""Path to the generated Org-Mode file."""
__descriptionText: str = (
"""Converts markdown formatted files to Org-Mode formatted files using Pandoc."""
)
__epilogText: str = """Examples:
python -m obs2org (which is the same as 'python -m obs2org ./')
Converts all markdown files with a suffix of '.md' in the current working
directory and all its subdirectories to files in Org-Mode format with the
same base filename but a '.org' suffix.
python -m obs2org hugo.md -o sepp.org
Converts the markdown document 'hugo.md' to an Org-Mode document named
'sepp.org'.
python -m obs2org *.md
Converts all markdown files with a suffix of '.md' in the current working
directory to files in Org-Mode format with the same base filename but a
'.org' suffix.
python -m obs2org *.md -o ../Org/
Converts all markdown files with a suffix of '.md' in the current working
directory to files in Org-Mode format with the same base filename but a
'.org' suffix in the directory '../Org'.
python -m obs2org ./Markdown -o ../Org/
Converts all markdown files with a suffix of '.md' in the directory
'./Markdown' and its subdirectories to files in Org-Mode format with
the same base filename but a '.org' suffix in the directory '../Org'.
See website https://github.com/Release-Candidate/Obs2Org for details."""
################################################################################
async def main() -> None:
"""The program's main entry point."""
cmd_line_args, cmd_line_parser = _parse_command_line()
await _convert_files(cmd_line_args=cmd_line_args, cmd_line_parser=cmd_line_parser)
###############################################################################
def _parse_command_line() -> tuple[argparse.Namespace, argparse.ArgumentParser]:
"""Parses the command line arguments of the program.
Returns a tuple containing a `Namespace` object holding all
parsed command line arguments and the command line parser object, an
`argparse.ArgumentParser`.
Returns
-------
tuple[argparse.Namespace, argparse.ArgumentParser]
The parsed command line arguments in a `argparse.Namespace` object,
the `argparse.ArgumentParser` object as the second element.
"""
cmd_line_parser = argparse.ArgumentParser(
prog="python -m obs2org",
description=__descriptionText,
epilog=__epilogText,
add_help=True,
formatter_class=argparse.RawTextHelpFormatter,
)
cmd_line_parser.add_argument(
"files",
metavar="MARKDOWN_FILES",
nargs="*",
help="""The path to the markdown files or directory to convert to
Org-Mode format. If this is a file or a list of markdown
files, these files are converted. If this is a directory,
all markdown files - files with a suffix of '.md' - in
this directory will be converted recursively descending
into subdirectories. If no markdown files or directory
are given, the current working directory is used.""",
default="./",
)
cmd_line_parser.add_argument(
"-V",
"--version",
action="version",
version=f"obs2org {VERSION}",
)
cmd_line_parser.add_argument(
"-p",
"--pandoc",
metavar="PANDOC",
type=str,
dest="pandoc_exe",
default="pandoc",
help="""PANDOC is the path to the pandoc executable, if the
Pandoc executable isn't in the PATH.""",
)
cmd_line_parser.add_argument(
"-n",
"--no-cite",
action="store_true",
dest="remove_citations",
default=False,
help="""If this flag is set, links like '[[@Name]]' are threaded
like normal links instead of Pandoc citations.
If this is not set, '[[@Name]]' is converted to '[[cite:@Name]]'""",
)
cmd_line_parser.add_argument(
"-u",
"--uuid",
action="store_true",
dest="generate_uuid",
default=False,
help="""If this flag is set, every file gets a header of the form
:PROPERTIES:
:ID: UUID
:END:
where UUID is a UUID like '16fd2706-8baf-433b-82eb-8c7fada847da'.""",
)
cmd_line_parser.add_argument(
"-o",
"--out",
metavar="OUT_PATH",
type=str,
dest="out_path",
default="./",
help="""OUT_PATH is the path to the file or directory to save the
converted Org-Mode file to.
If MARKDOWN_FILES is a single file this is used as the
filename of the converted file.
If MARKDOWN_FILES are more than one file or a directory,
this is used as the pathname of the directory to save the
converted files to.""",
)
return cmd_line_parser.parse_args(), cmd_line_parser
###############################################################################
async def _convert_files(
cmd_line_args: argparse.Namespace, cmd_line_parser: argparse.ArgumentParser
) -> None:
"""Convert the markdown files to Org-Mode files.
Convert the given markdown files or markdown files in the given
directory to Org-Mode format.
Parameters
----------
cmd_line_args : argparse.Namespace
The command line arguments of the program.
cmd_line_parser : argparse.ArgumentParser
The command line parser object to use.
"""
pandoc_path: str = _check_pandoc(
cmd_line_args=cmd_line_args, cmd_line_parser=cmd_line_parser
)
if isinstance(cmd_line_args.files, list):
path_list: list[str] = cmd_line_args.files
else:
path_list = [cmd_line_args.files]
out_path = _check_out_path(
cmd_line_args=cmd_line_args,
cmd_line_parser=cmd_line_parser,
path_list=path_list,
)
list_of_files: list[FilePaths] = []
for arg_path in path_list:
paths = _check_in_path(
cmd_line_parser=cmd_line_parser,
out_path=out_path,
arg_path=arg_path,
)
list_of_files.extend(paths)
await _do_convert_files(
pandoc_path=pandoc_path,
list_of_files=list_of_files,
add_uuid=cmd_line_args.generate_uuid,
remove_citations=cmd_line_args.remove_citations,
)
################################################################################
def _check_pandoc(
cmd_line_args: argparse.Namespace,
cmd_line_parser: argparse.ArgumentParser,
) -> str:
"""Check if the given command to call Pandoc works.
If the command does not work, the program is exited with an error message.
Parameters
----------
cmd_line_args : argparse.Namespace
The object holding all command line arguments, the pandoc command too.
cmd_line_parser : argparse.ArgumentParser
The command line parser object to use.
Returns
-------
str
The command to call the Pandoc executable on success.
"""
pandoc = cmd_line_args.pandoc_exe
pandoc_version_arg = "--version"
pandoc_out = subprocess.run(
args=" ".join([pandoc, pandoc_version_arg]),
check=False,
shell=True,
text=True,
capture_output=True,
)
if pandoc_out.returncode != 0 and pandoc_out.stderr != "":
cmd_line_parser.error(
f"Pandoc executable '{pandoc}' not found or does not work!\n"
f"Error message: '{pandoc_out.stderr.strip()}'\n"
f"Look at https://pandoc.org/installing.html for information on how to install\n"
f"pandoc"
)
return pandoc
################################################################################
def _check_in_path(
cmd_line_parser: argparse.ArgumentParser,
out_path: str,
arg_path: str,
) -> list[FilePaths]:
"""Check, if the given path contains Markdown files and return the path to
them and the Org-Mode file to generate.
Return the empty list else.
Parameters
----------
cmd_line_parser : argparse.ArgumentParser
The command line parser object to use.
out_path : str
The path to write the generated Org-Mode files to.
arg_path : str
The path to check for Markdown files.
Returns
-------
list[FilePaths]
The `FilePaths` of the Markdown file to convert and the Org-Mode file
to generate.
"""
ret_list: list[FilePaths] = []
if path.isdir(arg_path):
dir_path_list = _walk_directory(out_path=out_path, arg_path=arg_path)
ret_list.extend(dir_path_list)
elif path.isfile(arg_path):
file_obj = Path(arg_path)
out_file = path.join(out_path, file_obj.with_suffix(".org").name)
ret_list.append(FilePaths(in_file=file_obj, out_file=Path(out_file)))
else:
cmd_line_parser.error(f"no markdown file(s) found at path '{arg_path}'.")
return ret_list
################################################################################
def _walk_directory(out_path: str, arg_path: str) -> list[FilePaths]:
"""Walk through the directory `arg_path` and add all Markdown files to the
list of files to convert.
Parameters
----------
out_path : str
The path to write the generated Org-Mode files to.
arg_path : str
The directory to search for Markdown files.
Returns
-------
list[FilePaths]
A list of found Markdown files and the paths to the Org-Mode file to
generate.
"""
ret_list: list[FilePaths] = []
for dirpath, _, filenames in walk(top=arg_path, topdown=True, followlinks=True):
rel_dirpath = path.relpath(dirpath, arg_path)
out_dir = path.join(out_path, rel_dirpath)
Path(out_dir).mkdir(exist_ok=True, parents=True)
for file in filenames:
file_object = PurePath(file)
if file_object.suffix == ".md":
in_file = path.join(dirpath, file)
out_file = path.join(out_dir, file_object.with_suffix(".org"))
ret_list.append(
FilePaths(in_file=Path(in_file), out_file=Path(out_file))
)
return ret_list
################################################################################
def _check_out_path(
cmd_line_args: argparse.Namespace,
cmd_line_parser: argparse.ArgumentParser,
path_list: list[str],
) -> str:
"""Validate the path to write the Org-Mode file(s) to and return the checked
path.
If the path is wrong, e.g. a path to a file for more than one Markdown file
to convert, the program exits with an error message.
Parameters
----------
cmd_line_args : argparse.Namespace
The object holding the command line arguments.
cmd_line_parser : argparse.ArgumentParser
The command line parser object.
path_list : list[str]
The list of paths to Markdown files to convert.
Returns
-------
str
The checked path to the Org-Mode file(s) on success, exits the program
on errors.
"""
out_path: str = cmd_line_args.out_path
if path.basename(out_path) == "" or path.isdir(out_path):
print(f"Output to directory {out_path}")
Path(out_path).mkdir(exist_ok=True, parents=True)
else:
print(f"Output to file {out_path} {len(path_list)}")
if len(path_list) >= 1:
cmd_line_parser.error(
f"more than one markdown file to convert given,"
f" but just one output file '{out_path}'!"
)
return out_path
################################################################################
async def _do_convert_files(
pandoc_path: str,
list_of_files: list[FilePaths],
remove_citations: bool,
add_uuid: bool,
) -> None:
"""Converts the files in the given list.
First converts the files in `list_of_files` using pandoc and then fixes the
links to other Org-Mode files and tags and dates.
We have to do the conversion first, because links that need to be corrected
can point to files not generated yet and we must search the files the link
points to for the right section id.
Parameters
----------
pandoc_path : str
Path to the pandoc executable.
list_of_files : list[FilePaths]
List of `FilePaths` containing the path to the Markdown file to convert
and the Org-Mode file to generate.
remove_citations : bool
Whether to remove Pandoc-style citations to treat them as normal links,
or not.
add_uuid : bool
Whether to add an UUID-header to each file.
"""
convert_tasks: list[Coroutine[object, object, object]] = []
for convert_file in list_of_files:
convert_tasks.append(
asyncio.to_thread(
convert_single_file,
convert_file.in_file,
convert_file.out_file,
pandoc_path,
)
)
await asyncio.gather(*convert_tasks)
# Can't run this asynchronously, as
# we need to look up headings in converted files.
for correct_file in list_of_files:
correct_org_mode(
correct_file.out_file, remove_citations=remove_citations, add_uuid=add_uuid
)