Debian Codegraph @

src/get-author-url

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 # Web example script to get HTML author URL from author in list
5 # Copyright (C) 2014-2026 by Thomas Dreibholz
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Contact: [email protected]
21 #
22
23 import csv
24 import re
25 import sys
26
27 if sys.version_info < (3, 9):
28 sys.stderr.write('ERROR: ' + sys.argv[0] + ' requires Python 3.9 or later!\n')
29 sys.exit(1)
30
31 from typing import Final, TextIO
32
33
34 # ###### Main program #######################################################
35 if len(sys.argv) != 4:
36 sys.stderr.write('Usage: ' + sys.argv[0] + ' author_list author_name default_text\n')
37 sys.exit(1)
38
39 replaceBbspStrings : Final[re.Pattern[str]] = re.compile(r'&nbsp;', re.UNICODE)
40
41 authorList : Final[str] = sys.argv[1]
42 authorName : Final[str] = replaceBbspStrings.sub(' ', sys.argv[2])
43 authorText : Final[str] = sys.argv[3]
44
45
46 csv.register_dialect('authors', delimiter=' ', quoting=csv.QUOTE_MINIMAL,skipinitialspace=True)
47
48 inputFile : TextIO = open(authorList, 'r')
49 inputCSV = csv.reader(inputFile, dialect='authors')
50
51 authorRow : list[str] | None = None
52 for row in inputCSV:
53 # print(row)
54 if row[0] == authorName:
55 if authorRow is not None:
56 # the authorName is multiple times in the table -> warning
57 sys.stderr.write('WARNING: Multiple occurrences of authorName ' + authorName + ' in ' + authorList + '!\n')
58 authorRow = row
59
60
61 if authorRow is None:
62 # The authorName is not in the table -> use default text
63 sys.stdout.write(authorText)
64 sys.stderr.write('Not in list: ' + authorName + '\n')
65 sys.exit(0)
66 else:
67 # Found the authorName -> print the result
68 sys.stdout.write('<a href="' + authorRow[1] + '">' + authorText + '</a>')