-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_links.py
116 lines (103 loc) · 3.71 KB
/
edit_links.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
# Importing necessary modules
import argparse
import sys
import os
if len(sys.argv) < 2:
print("usage: python edit_links.py <action> <short> [long] [description]")
sys.exit(1)
# use argparse to get an add/del argument, a short, long, and description argument for add, and a short argument for del
parser = argparse.ArgumentParser()
parser.add_argument("action", help="add or del")
parser.add_argument("short", help="short link to add or delete")
parser.add_argument(
"long",
help="full url of the link, required if adding",
nargs="?" if sys.argv[1] == "del" else 1,
)
parser.add_argument(
"description",
help="description of the link, required if adding",
nargs="?" if sys.argv[1] == "del" else 1,
)
args = parser.parse_args()
# Checking if the user wants to add a new link
if args.action == "add":
# Checking if the user has provided all the required arguments
# if len(sys.argv) != 5:
# print("usage: python edit_links.py add <short> <long> <description>")
# sys.exit(1)
# Assigning the arguments to variables
short = args.short
long = args.long[0]
description = args.description[0]
# Generating the HTML code for the redirect
html = '<meta http-equiv="Refresh" content="0; url=\'' + long + "'\" />"
# Checking if the link already exists
if short in os.listdir("."):
print("Error: link '" + short + "' already exists")
sys.exit(1)
# Creating a new folder with the short name and adding an index.html file inside containing the HTML code
os.mkdir(short)
f = open(short + "/index.html", "w")
f.write(html)
f.close()
# Adding the new link to the jmp/index.html table
f = open("index.html", "r")
lines = f.readlines()
f.close()
f = open("index.html", "w")
for line in lines:
# Checking if the line contains the end of the table
if "</table>" in line:
# Adding the new link to the table
f.write(
" <tr>\n"
+ ' <th><a href="../'
+ short
+ '">'
+ short
+ "</a></th>\n"
+ " <td>"
+ description
+ "</td>\n"
+ " </tr>\n"
)
f.write(line)
f.close()
# Checking if the user wants to delete a link
elif args.action == "del":
# Checking if the user has provided all the required arguments
# if len(sys.argv) != 3:
# print("usage: python edit_links.py del <short>")
# sys.exit(1)
# Assigning the arguments to variables
short = args.short
# delete folder with [short] name if it exists
try:
os.remove(short + "/index.html")
os.rmdir(short)
except FileNotFoundError:
print("Error: link '" + short + "' does not exist")
sys.exit(1)
# delete link from jmp/index.html table
f = open("index.html", "r")
lines = f.readlines()
f.close()
f = open("index.html", "w")
# parse through lines and delete the <tr> with the link
linenum = 0
for i, line in enumerate(lines):
# if currently on one of the lines of the <tr> with the link, skip it
if linenum > 0 and linenum < 4:
linenum += 1
# if the next line has the link, the current line is the beginning of the relevant <tr>
elif (
i < len(lines) - 1
and '<th><a href="../' + short + '">' + short + "</a></th>" in lines[i + 1]
):
linenum = 1
# if the next line does not have the link or the current line is after the last line of the <tr> to be deleted, write the current line
else:
linenum = 0
f.write(line)
f.close()