Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/scripts/rm.py
5457 views
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
# Copyright 2025 Mike Fährmann
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License version 2 as
8
# published by the Free Software Foundation.
9
10
"""Delete an extractor module"""
11
12
import os
13
import re
14
import logging
15
import argparse
16
import util
17
18
LOG = logging.getLogger("rm")
19
20
21
def remove_file(args, path):
22
try:
23
os.unlink(path)
24
except Exception:
25
pass
26
27
28
def remove_from_docs_configurationrst(args, path):
29
with util.lines(path) as lines:
30
needle = f'{args.category}`'
31
for idx, line in enumerate(lines):
32
if needle in line:
33
lines[idx] = ""
34
35
rm = False
36
needle = f'extractor.{args.category}.'
37
for idx, line in enumerate(lines):
38
if rm:
39
lines[idx] = ""
40
if line == "\n":
41
rm -= 1
42
elif line.startswith(needle):
43
lines[idx] = ""
44
rm = 2
45
46
47
def remove_from_docs_gallerydlconf(args, path):
48
rm = False
49
needle = f' "{args.category}":\n'
50
51
with util.lines(path) as lines:
52
for idx, line in enumerate(lines):
53
if rm:
54
lines[idx] = ""
55
if line.startswith(" }"):
56
break
57
elif line == needle:
58
lines[idx] = ""
59
rm = True
60
61
62
def remove_from_extractor_init(args, path):
63
needle = f' "{args.category}",\n'
64
65
with util.lines(path) as lines:
66
try:
67
lines.remove(needle)
68
except ValueError:
69
pass
70
71
72
def remove_from_scripts_supportedsites(args, path):
73
pattern = re.compile(
74
r'\s+(\w+\[)?'
75
f'"{args.category}"'
76
r'(: (\{)?|\] = )')
77
78
with util.lines(path) as lines:
79
for idx, line in enumerate(lines):
80
if pattern.match(line):
81
lines[idx] = ""
82
83
84
def update_docs_supportedsites(args, path):
85
import supportedsites
86
supportedsites.main()
87
88
89
def parse_args(args=None):
90
parser = argparse.ArgumentParser(args)
91
92
parser.add_argument("-g", "--git", action="store_true")
93
parser.add_argument("CATEGORY")
94
95
args = parser.parse_args()
96
args.category = args.cat = args.CATEGORY
97
return args
98
99
100
def main():
101
args = parse_args()
102
103
files = [
104
(util.path("gallery_dl", "extractor", f"{args.category}.py"),
105
remove_file),
106
107
(util.path("test", "results", f"{args.category}.py"),
108
remove_file),
109
110
(util.path("docs", "configuration.rst"),
111
remove_from_docs_configurationrst),
112
113
(util.path("docs", "gallery-dl.conf"),
114
remove_from_docs_gallerydlconf),
115
116
(util.path("gallery_dl", "extractor", "__init__.py"),
117
remove_from_extractor_init),
118
119
(util.path("scripts", "supportedsites.py"),
120
remove_from_scripts_supportedsites),
121
122
(util.path("docs", "supportedsites.md"),
123
update_docs_supportedsites),
124
]
125
126
for path, func in files:
127
path_tr = util.trim(path)
128
LOG.info(path_tr)
129
130
func(args, path)
131
132
if args.git:
133
util.git("add", "--", path_tr)
134
135
136
if __name__ == "__main__":
137
logging.basicConfig(
138
level=logging.DEBUG,
139
format="[%(levelname)s] %(message)s",
140
)
141
main()
142
143