-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodepressor.py
More file actions
106 lines (77 loc) · 2.81 KB
/
Copy pathcodepressor.py
File metadata and controls
106 lines (77 loc) · 2.81 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
from argparse import ArgumentParser
from sys import argv
from core.plugins import PluginsSpace, Plugin, scan_folder
from core.utils import log, error
ap = ArgumentParser()
ap.add_argument(
'action',
choices = ('process', 'list'),
help = 'What you want this program to do.'
)
ap.add_argument(
'target',
help = 'Action subject.'
)
ap.add_argument(
'output',
nargs = '?',
default = None,
help = 'Path to the output file.'
)
ap.add_argument(
'-p', '--plugin',
default = None,
help = 'Forcefully uses plugin, restricting codepressor to choose it automatically.'
)
args = ap.parse_args(argv[1:])
plugins = PluginsSpace()
plugins_fnames = scan_folder('plugins/')
for plugin_fname in plugins_fnames:
with open(plugin_fname, 'r') as file:
plugin = Plugin.from_source(file)
plugins.register_plugin(plugin)
match args.action:
case 'process':
if args.plugin is None:
try:
target_plugin = plugins.get_plugin_by_target_fname(args.target)
except PluginsSpace.StagingError as exc:
error(f'{exc.__class__.__name__}: {str(exc).strip("'")}')
exit(0) # Calling explicitly to shut up static checkers and stuff
else:
target_plugin = plugins.get_plugin_by_title(args.plugin)
with open(args.target, 'r') as file:
source = file.read()
result = target_plugin.process(source)
if args.output is None:
log(result)
exit(0)
with open(args.output, 'w') as file:
file.write(result)
case 'list':
match args.target:
case 'plugins':
for plugin in plugins.plugin_iter():
assigned_to = 'nothing'
if plugin.exts:
assigned_to = ', '.join([ext for ext in plugin.exts]) + ' files'
log(
f'- {plugin.title} v{'.'.join(map(str, plugin.version))}',
f' {plugin.description}',
f' Created by {', '.join(map(str, plugin.authors))}',
f' Assigned to process {assigned_to}',
sep = '\n',
end = '\n\n'
)
log('Notice: the earlier a plugin appears in this list, the higher its priority.')
case _:
error(f'Invalid target for list: "{args.target}"', code = 0)
case 'fetch':
error('Not implemented!', code = 0)
match args.target:
case 'logs':
error('Not implemented!', code = 0)
case _:
error(f'Invalid target for fetch: "{args.target}"', code = 0)
case _:
error('Invalid action:', args.action, code = 0)