Changeset 281 for ibidas.py


Ignore:
Timestamp:
Dec 7, 2009, 10:48:42 AM (11 years ago)
Author:
jjbot
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ibidas.py

    r212 r281  
     1
     2import sys
     3import getopt
     4import getpass
     5
    16from IPython.Shell import IPShellEmbed
    27
     8# Container and subclasses not needed here but are handy to have available
     9# when using the IPython commandline.
    310import container
    411from container import iany,iall,imax,imin,inot,readfile,writefile,vector,table,scalar
     
    714
    815_ConfigDefault = {
    9     "database.name":                "ibidas_test",
    10     "database.user":                "bla",
    11     "database.password":            "bla",
     16    "database.name":                "ibidas_test_nieuw",
     17    "database.user":                "jan",
     18    "database.password":            "geen",
    1219    "database.host":                "127.0.0.1",
    1320    "database.port":                "5432",
     21    "system.tmp_dir":               "/tmp"
    1422}
    1523
     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    """
    1634
    17 config = config.loadConfig('ibidas.ini',_ConfigDefault)
     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'] + "]: ")
    1842
     43    # system setup
     44    sys_tmp = input("Temp dir: " + config['system.tmp_dir'] + "]: ")
    1945
    20 #print config
    21 cd = postgres.openConnection(config['database.host'],\
     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'],\
    2283            config['database.port'],config['database.name'],\
    2384            config['database.user'],config['database.password']).getConSet()
    2485
    25 
    26 ipshell = IPShellEmbed(banner='Welcome to the IBIDAS system',exit_msg='IBIDAS shutting down ...')
    27 del ipshell.IP.user_ns['_']
    28 ipshell()
     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 TracChangeset for help on using the changeset viewer.