Skip to content

Commit

Permalink
Fixed minor bugs, added a checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Matias Carrasco Kind authored and Matias Carrasco Kind committed Feb 21, 2015
1 parent 7eb030c commit fd8b1de
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 48 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
v1.0.0, 2015-FEB-17 -- release
v1.0.1, 2015-FEB-20 -- Fix a bug at exit after error
-- Added a checker of DES_SERVICES files and prompt password
-- Minor bugs
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ using cx_Oracle

- Oracle Client > 11g.2
- cx_Oracle
- pyfits
- pandas
- pyfits > 3.3
- pandas > 0.14
- termcolor
- PyTables (for hdf5 output)

Expand Down Expand Up @@ -58,3 +58,7 @@ or
The format is the same as in command line, SQL statement must end with ;
and to write output files it must be followed by > <output file>

### TODO
- There is a bug with some versions of readline
- Other small changes when loading tables
- Self-upgrade
70 changes: 48 additions & 22 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#Config file
import ConfigParser
import getpass
import time
import sys
import cx_Oracle

configcomment="""#
# Easyaccess default parameters
Expand Down Expand Up @@ -31,7 +34,7 @@ def get_config(configfile):
check=config.read(configfile)
if check == []:
configwrite= True
print '\nError in config file, creating a new one...\n'
print '\nCreating a configuration file... at %s\n' %configfile

if not config.has_section('easyaccess'):
configwrite = True
Expand Down Expand Up @@ -73,37 +76,60 @@ def write_config(configfile, config_ob):
return False


def get_desconfig(desfile):
def get_desconfig(desfile,db):
"""
Loads des config file or create one if not
Returns a configParser object
"""
server_n='leovip148.ncsa.uiuc.edu'
port_n='1521'

if not db[:3] == 'db-': db='db-'+db
config = ConfigParser.ConfigParser()
configwrite = False
check=config.read(desfile)
if check == []:
configwrite= True
print '\nError in des config file, creating a new one...'
print '\nError in DES_SERVICES config file, creating a new one...'
print 'File might not exists or is not configured'
print
user=raw_input('Enter username : ')
pw1=getpass.getpass(prompt='Enter password : ')
print
print 'By default the same password is the same for all databases'
print 'If you change your password to some of the DB please modify'
print 'the configuration file %s' % desfile

databases = ['db-desoper','db-dessci','db-destest']

for db in databases:
if not config.has_section(db):
configwrite = True
config.add_section(db)
if not config.has_option(db,'user'): configwrite = True ;config.set(db,'user',user)
if not config.has_option(db,'passwd'): configwrite = True ;config.set(db,'passwd',pw1)
if not config.has_option(db,'name'): configwrite = True ;config.set(db,'name',db[3:])
if not config.has_option(db,'server'): configwrite = True ;config.set(db,'server','leovip148.ncsa.uiuc.edu')
if not config.has_option(db,'port'): configwrite = True ;config.set(db,'port','1521')

databases = ['db-desoper','db-dessci','db-destest'] #most used ones anyways

if db not in databases and not config.has_section(db):
check_db=raw_input('\nDB entered not dessci, desoper or destest or in DES_SERVICE file, continue anyway [y]/n\n')
if check_db in ('n','N','no','No','NO'): sys.exit(0)

if not config.has_section(db):
print '\nAdding section %s to des_service file\n' % db
configwrite = True
kwargs = {'host': server_n, 'port': port_n, 'service_name': db[3:]}
dsn = cx_Oracle.makedsn(**kwargs)
good=False
for i in range(3):
try:
user=raw_input('Enter username : ')
pw1=getpass.getpass(prompt='Enter password : ')
ctemp=cx_Oracle.connect(user,pw1,dsn=dsn)
good = True
break
except:
(type, value, traceback) = sys.exc_info()
print value
if value.message.code == 1017: pass
else: sys.exit(0)
if good:
ctemp.close()
else:
print '\n Check your credentials and/or database access\n'
sys.exit(0)
config.add_section(db)

if not config.has_option(db,'user'): configwrite = True ;config.set(db,'user',user)
if not config.has_option(db,'passwd'): configwrite = True ;config.set(db,'passwd',pw1)
if not config.has_option(db,'name'): configwrite = True ;config.set(db,'name',db[3:])
if not config.has_option(db,'server'): configwrite = True ;config.set(db,'server',server_n)
if not config.has_option(db,'port'): configwrite = True ;config.set(db,'port',port_n)


check = True
Expand Down
51 changes: 30 additions & 21 deletions easyaccess.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python
__author__ = 'Matias Carrasco Kind'
__version__ = '1.0.0'
__version__ = '1.0.1'
# TODO:
# add other formats in load tables from fits (like boolean or complex)
# clean up, comments
# readline bug (GNU vs libedit)
# readline bug (GNU vs libedit) for history
# self upgrade

import warnings
Expand Down Expand Up @@ -167,7 +167,7 @@ def __init__(self, conf, desconf, db, interactive=True):
self.password = self.desconfig.get('db-' + self.dbname, 'passwd')
kwargs = {'host': self.dbhost, 'port': self.port, 'service_name': self.dbname}
dsn = cx_Oracle.makedsn(**kwargs)
print 'Connecting to DB %s ...' % self.dbname
print 'Connecting to DB ** %s ** ...' % self.dbname
self.con = cx_Oracle.connect(self.user, self.password, dsn=dsn)
self.cur = self.con.cursor()
self.cur.arraysize = self.prefetch
Expand Down Expand Up @@ -749,16 +749,19 @@ def do_exit(self, line):
except:
pass
try:
cur.close()
self.cur.close()
except:
pass
try:
self.con.commit()
self.con.close()
except:
pass
self.con.commit()
self.con.close()
if readline_present:
readline.write_history_file(history_file)
if self.writeconfig:
config_mod.write_config(config_file, self.config)
sys.exit(0)
os._exit(0)

def do_clear(self, line):
"""
Expand Down Expand Up @@ -1302,23 +1305,26 @@ def to_pandas(cur):


class connect():
def __init__(self):
def __init__(self, section = ''):
conf = config_mod.get_config(config_file)
pd.set_option('display.max_rows', conf.getint('display', 'max_rows'))
pd.set_option('display.width', conf.getint('display', 'width'))
pd.set_option('display.max_columns', conf.getint('display', 'max_columns'))
desconf = config_mod.get_desconfig(desfile)
db = conf.get('easyaccess', 'database')
if section == '':
db = conf.get('easyaccess', 'database')
else:
db = section
self.prefetch = conf.getint('easyaccess', 'prefetch')
self.dbname = db
#connect to db
desconf = config_mod.get_desconfig(desfile, self.dbname)
self.user = desconf.get('db-' + self.dbname, 'user')
self.dbhost = desconf.get('db-' + self.dbname, 'server')
self.port = desconf.get('db-' + self.dbname, 'port')
self.password = desconf.get('db-' + self.dbname, 'passwd')
kwargs = {'host': self.dbhost, 'port': self.port, 'service_name': self.dbname}
dsn = cx_Oracle.makedsn(**kwargs)
print 'Connecting to DB...'
print 'Connecting to DB ** %s ** ...' % self.dbname
self.con = cx_Oracle.connect(self.user, self.password, dsn=dsn)

def ping(self):
Expand Down Expand Up @@ -1358,16 +1364,17 @@ def error(self, message):

try:
import readline
#save = sys.stdout
#sys.stdout = open("/dev/null","w")
readline.read_history_file(history_file)
#sys.stdout = save
readline_present = True
readline.set_history_length(conf.getint('easyaccess', 'histcache'))
except:
print sys.exc_info()
readline_present = False

if readline_present == True:
try:
readline.read_history_file(history_file)
readline.set_history_length(conf.getint('easyaccess', 'histcache'))
except:
'Print readline might give problems accesing the history of commands'

parser = MyParser(description='Easy Access', version="version: %s" % __version__)
parser.add_argument("-c", "--command", dest='command', help="Executes command and exit")
parser.add_argument("-l", "--loadsql", dest='loadsql', help="Loads a sql command, execute it and exit")
Expand All @@ -1376,27 +1383,29 @@ def error(self, message):
parser.add_argument("-s", "--db", dest='db', help="bypass database name, [dessci, desoper or destest]")
args = parser.parse_args()

desconf = config_mod.get_desconfig(desfile)

if args.db is not None:
db = args.db
if db[:3] == 'db-' : db=db[3:]
else:
db = conf.get('easyaccess', 'database')

desconf = config_mod.get_desconfig(desfile,db)

if args.command is not None:
cmdinterp = easy_or(conf, desconf, db, interactive=False)
cmdinterp.onecmd(args.command)
sys.exit(0)
os._exit(0)
elif args.loadsql is not None:
cmdinterp = easy_or(conf, desconf, db, interactive=False)
linein = "loadsql " + args.loadsql
cmdinterp.onecmd(linein)
sys.exit(0)
os._exit(0)
elif args.loadtable is not None:
cmdinterp = easy_or(conf, desconf, db, interactive=False)
linein = "load_table " + args.loadtable
cmdinterp.onecmd(linein)
sys.exit(0)
os._exit(0)
else:
os.system(['clear', 'cls'][os.name == 'nt'])
easy_or(conf, desconf, db).cmdloop()
Expand Down
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import sys
import os
from numpy.distutils.core import setup, Extension
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
prjdir = os.path.dirname(__file__)

def read(filename):
Expand All @@ -12,7 +15,7 @@ def read(filename):
include_dirs = []
setup(
name='easyaccess',
version='1.0.0',
version='1.0.1',
author='Matias Carrasco Kind',
author_email='[email protected]',
scripts=['easyaccess'],
Expand All @@ -21,5 +24,5 @@ def read(filename):
description='Easy Access to access DES DB',
long_description=read('README.md'),
url='https://github.com/mgckind/easyaccess',
install_requires=['pandas','termcolor','pyfits','cx_Oracle'],
install_requires=['pandas','termcolor','pyfits >= 3.3','cx_Oracle'],
)

0 comments on commit fd8b1de

Please sign in to comment.