-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_webpages.py
More file actions
executable file
·136 lines (116 loc) · 5.12 KB
/
generate_webpages.py
File metadata and controls
executable file
·136 lines (116 loc) · 5.12 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
#!/usr/bin/env python3
"""
Webpage Generation Script
--------------------------
Automates the generation of PHP web-pages that contain RInChIs, RAuxInfos and Keys for both the version 0.02 and 1.00
rinchi versions. Specific to RInChI website so not suitable for wide distribution.
Modifications:
- Duncan Hampshire 2017
"""
import argparse
import os
import sqlite3
from rinchi_tools import RInChI, _external, database
def create_index_page(directory, end="", start=""):
"""
Creates an index page for all file in a directory
Args:
end: The start string of the files to create an index for.
start: The end string (usually an extension) of the files to create an index for.
directory: The directory containing the files to index
"""
files = []
nfiles = []
for file in os.listdir(directory):
if file.endswith(end) and file.startswith(start):
files.append(file)
files = sorted(files)
for filename in files:
nfiles.append('<p><a href="{}{}{}">{}</a></p>'.format(directory, _external.SEPARATOR, filename, filename))
links = "\n".join(nfiles)
head = '<?php $title = "{}"; include "/var/www/template/header.php"; ?>'.format("Index")
foot = '<?php include "/var/www/template/footer.php"; ?>'
file = head + links + foot
filename = "index.php"
with open(filename, mode='w+') as f:
f.write(file)
def get_rinchis_rauxinfos(db, table_name, number=1000):
"""
Gets a list of RInChI - rauxinfo tuples
"""
db = sqlite3.connect(db)
cursor = db.cursor()
results = database._sql_search(cursor, table_name, columns=("rinchi", "rauxinfo"), limit=number)
return results
def tuple_to_html_page(data_tuple, inc_rinchi=True, inc_rauxinfo=True, inc_longkey=True, inc_shortkey=True,
inc_webkey=True, custom=None):
"""
Create HTML page text from a data_tuple
"""
if custom is None:
tag = "p"
else:
tag = custom
wrappings = ("<{}>".format(tag), "</{}>".format(tag))
head = '<?php $title = "{}"; include "/var/www/template/header.php"; ?>'.format("RInChI Example")
foot = '<?php include "/var/www/template/footer.php"; ?>'
main_text = head
web = RInChI().rinchikey_from_rinchi(data_tuple[0], "W").join(wrappings)
if inc_rinchi:
main_text += data_tuple[0].join(wrappings)
if inc_rauxinfo:
main_text += data_tuple[1].join(wrappings)
if inc_longkey:
main_text += RInChI().rinchikey_from_rinchi(data_tuple[0], "L").join(wrappings)
if inc_shortkey:
main_text += RInChI().rinchikey_from_rinchi(data_tuple[0], "S").join(wrappings)
if inc_webkey:
main_text += web
main_text += foot
return main_text
def run(db, table_name, destination, prefix, inc_rinchi=True, inc_rauxinfo=True, inc_longkey=True, inc_shortkey=True,
inc_webkey=True, custom=None, number=1000):
"""
Generates webpages and index.
Args:
db: The database path containing the rinchis
table_name: The table containing the RInChIs
destination:
prefix:
inc_rinchi:
inc_rauxinfo:
inc_longkey:
inc_shortkey:
inc_webkey:
custom:
number:
Returns:
"""
results = get_rinchis_rauxinfos(db, table_name, number)
indexer = 1
for result in results:
page = tuple_to_html_page(result, inc_rinchi, inc_rauxinfo, inc_longkey, inc_shortkey, inc_webkey, custom)
filename = "{}{}{}-{}.php".format(destination, _external.SEPARATOR, prefix, indexer)
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, mode='w+') as f:
f.write(page)
indexer += 1
create_index_page(destination, start=prefix)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generation of PHP webpage files - Duncan Hampshire 2017 \n{}".format(__doc__))
parser.add_argument("destination", help="The destination folder for the webpage files")
parser.add_argument("file_prefix", help="The prefix to name the files in the folder")
parser.add_argument("-d", default=_external.RINCHI_DATABASE, help="path to the SQL database of RInChI files")
parser.add_argument('-ri', '--rinchi', action='store_true', help='Include RInChI')
parser.add_argument('-ra', '--rauxinfo', action='store_true', help='Include RAuxInfo')
parser.add_argument('-l', '--longkey', action='store_true', help='Include LongKey')
parser.add_argument('-s', '--shortkey', action='store_true', help='Include ShortKey')
parser.add_argument('-w', '--webkey', action='store_true', help='Include WebKey')
parser.add_argument('-c', '--custom', help='Use a different HTML tag e.g. <span> , <h1>')
parser.add_argument('-n', '--number', default=1000, type=int, help='number of pages to be generated')
parser.add_argument('-t', '--table', help='name of the table', default='rinchis1-00')
print(_external.RINCHI_DATABASE)
args = parser.parse_args()
run(args.d, args.table, args.destination, args.file_prefix, args.rinchi, args.rauxinfo, args.longkey, args.shortkey,
args.webkey, args.custom, args.number)