]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/location-importer.in
python: Add database driver for PostgreSQL
[people/ms/libloc.git] / src / python / location-importer.in
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # libloc - A library to determine the location of someone on the Internet #
5 # #
6 # Copyright (C) 2020 IPFire Development Team <info@ipfire.org> #
7 # #
8 # This library is free software; you can redistribute it and/or #
9 # modify it under the terms of the GNU Lesser General Public #
10 # License as published by the Free Software Foundation; either #
11 # version 2.1 of the License, or (at your option) any later version. #
12 # #
13 # This library is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
16 # Lesser General Public License for more details. #
17 # #
18 ###############################################################################
19
20 import argparse
21 import logging
22 import sys
23
24 # Load our location module
25 import location
26 import location.database
27 import location.importer
28 from location.i18n import _
29
30 # Initialise logging
31 log = logging.getLogger("location.importer")
32 log.propagate = 1
33
34 class CLI(object):
35 def parse_cli(self):
36 parser = argparse.ArgumentParser(
37 description=_("Location Importer Command Line Interface"),
38 )
39
40 # Global configuration flags
41 parser.add_argument("--debug", action="store_true",
42 help=_("Enable debug output"))
43
44 # version
45 parser.add_argument("--version", action="version",
46 version="%(prog)s @VERSION@")
47
48 # Database
49 parser.add_argument("--database-host", required=True,
50 help=_("Database Hostname"), metavar=_("HOST"))
51 parser.add_argument("--database-name", required=True,
52 help=_("Database Name"), metavar=_("NAME"))
53 parser.add_argument("--database-username", required=True,
54 help=_("Database Username"), metavar=_("USERNAME"))
55 parser.add_argument("--database-password", required=True,
56 help=_("Database Password"), metavar=_("PASSWORD"))
57
58 args = parser.parse_args()
59
60 # Enable debug logging
61 if args.debug:
62 log.setLevel(logging.DEBUG)
63
64 return args
65
66 def run(self):
67 # Parse command line arguments
68 args = self.parse_cli()
69
70 # Initialise database
71 db = self._setup_database(args)
72
73 # Call function
74 ret = self.handle_import(db, args)
75
76 # Return with exit code
77 if ret:
78 sys.exit(ret)
79
80 # Otherwise just exit
81 sys.exit(0)
82
83 def _setup_database(self, ns):
84 """
85 Initialise the database
86 """
87 # Connect to database
88 db = location.database.Connection(
89 host=ns.database_host, database=ns.database_name,
90 user=ns.database_username, password=ns.database_password,
91 )
92
93 with db.transaction():
94 db.execute("""
95 CREATE TABLE IF NOT EXISTS asnums(number integer, name text);
96 CREATE UNIQUE INDEX IF NOT EXISTS asnums_number ON asnums(number);
97 """)
98
99 return db
100
101 def handle_import(self, db, ns):
102 pass
103
104
105 def main():
106 # Run the command line interface
107 c = CLI()
108 c.run()
109
110 main()