source: ibidas.py

Last change on this file was 281, checked in by jjbot, 10 years ago

Added parsing of commandline arguments. Can now set: host, port, db_name & db_user.

File size: 3.3 KB
Line 
1
2import sys
3import getopt
4import getpass
5
6from IPython.Shell import IPShellEmbed
7
8# Container and subclasses not needed here but are handy to have available
9# when using the IPython commandline.
10import container
11from container import iany,iall,imax,imin,inot,readfile,writefile,vector,table,scalar
12import config
13from container import postgres
14
15_ConfigDefault = {
16    "database.name":                "ibidas_test_nieuw",
17    "database.user":                "jan",
18    "database.password":            "geen",
19    "database.host":                "127.0.0.1",
20    "database.port":                "5432",
21    "system.tmp_dir":               "/tmp"
22}
23
24def usage():
25    print \
26    """Ibidas commandline arguments:
27    -d  --database  Name of the database to connect to.
28    -h  --host      Host to connect to.
29    -p  --port      Port to connect to.
30    -u  --user      Username to use for the database.
31        --initial   Asks for user inputs for all the required arguments and
32                    writes them to the ibidas config file.
33    """
34
35def initial_config():
36    conf = _ConfigDefault
37    # database setup
38    db_host = input("Database host [" + conf['database.host'] + "]: ")
39    db_port = input("Database host port [ " + conf['database.port'] + "]: ")
40    db_name = input("Database name [" + conf['database.name'] + "]: ")
41    db_user = input("Database user ["+ conf['database.user'] + "]: ")
42
43    # system setup
44    sys_tmp = input("Temp dir: " + config['system.tmp_dir'] + "]: ")
45
46    if not db_host == "": conf['database.host'] = db_host
47    if not db_port == "": conf['database.port'] = db_port
48    if not db_name == "": conf['database.name'] = db_name
49    if not db_user == "": conf['database.user'] = db_user
50
51    if not sys_tmp == "": conf['system.tmp_dir'] = sys_tmp
52    config.writeConfig(conf, "ibidas.ini")
53
54if(__name__ == '__main__'):
55    config = config.loadConfig('ibidas.ini', _ConfigDefault)
56
57    try:
58        opts, args = getopt.getopt(sys.argv[1:], "h:p:u:d",
59                ["host=", "port=", "user=", "database=", "initial"])
60        for o, a in opts:
61            if o in ("-h", "--host"):
62                config['database.host'] = a
63            elif o in ("-p", "--port"):
64                config['database.port'] = a
65            elif o in ("-u", "--user"):
66                config['database.user'] = a
67                config['database.password'] = \
68                        getpass.getpass("Database password: ")
69            elif o in ("-d", "--database"):
70                config['database.name'] = a
71            elif o in ("--initial"):
72                initial_config()
73    except getopt.GetoptError, err:
74        # print help information and exit:
75        print str(err) # will print something like "option -a not recognized"
76        usage()
77        sys.exit(2)
78
79    if(not config.has_key("database.password")):
80        config['database.password'] = getpass.getpass("Database password: ")
81
82    cd = postgres.openConnection(config['database.host'],\
83            config['database.port'],config['database.name'],\
84            config['database.user'],config['database.password']).getConSet()
85
86    sys.argv = sys.argv[0] # remove commandline arguments so ipython doesn't see them
87    ipshell = IPShellEmbed(banner='Welcome to the IBIDAS system',exit_msg='IBIDAS shutting down ...')
88    del ipshell.IP.user_ns['_']
89    ipshell()
Note: See TracBrowser for help on using the repository browser.