Skip to content

Commit fd8b1de

Browse files
Matias Carrasco KindMatias Carrasco Kind
authored andcommitted
Fixed minor bugs, added a checker
1 parent 7eb030c commit fd8b1de

File tree

5 files changed

+93
-48
lines changed

5 files changed

+93
-48
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
v1.0.0, 2015-FEB-17 -- release
2+
v1.0.1, 2015-FEB-20 -- Fix a bug at exit after error
3+
-- Added a checker of DES_SERVICES files and prompt password
4+
-- Minor bugs

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ using cx_Oracle
88

99
- Oracle Client > 11g.2
1010
- cx_Oracle
11-
- pyfits
12-
- pandas
11+
- pyfits > 3.3
12+
- pandas > 0.14
1313
- termcolor
1414
- PyTables (for hdf5 output)
1515

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

61+
### TODO
62+
- There is a bug with some versions of readline
63+
- Other small changes when loading tables
64+
- Self-upgrade

config.py

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#Config file
22
import ConfigParser
33
import getpass
4+
import time
5+
import sys
6+
import cx_Oracle
47

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

3639
if not config.has_section('easyaccess'):
3740
configwrite = True
@@ -73,37 +76,60 @@ def write_config(configfile, config_ob):
7376
return False
7477

7578

76-
def get_desconfig(desfile):
79+
def get_desconfig(desfile,db):
7780
"""
7881
Loads des config file or create one if not
79-
Returns a configParser object
82+
8083
"""
84+
server_n='leovip148.ncsa.uiuc.edu'
85+
port_n='1521'
86+
87+
if not db[:3] == 'db-': db='db-'+db
8188
config = ConfigParser.ConfigParser()
8289
configwrite = False
8390
check=config.read(desfile)
8491
if check == []:
8592
configwrite= True
86-
print '\nError in des config file, creating a new one...'
93+
print '\nError in DES_SERVICES config file, creating a new one...'
8794
print 'File might not exists or is not configured'
8895
print
89-
user=raw_input('Enter username : ')
90-
pw1=getpass.getpass(prompt='Enter password : ')
91-
print
92-
print 'By default the same password is the same for all databases'
93-
print 'If you change your password to some of the DB please modify'
94-
print 'the configuration file %s' % desfile
95-
96-
databases = ['db-desoper','db-dessci','db-destest']
97-
98-
for db in databases:
99-
if not config.has_section(db):
100-
configwrite = True
101-
config.add_section(db)
102-
if not config.has_option(db,'user'): configwrite = True ;config.set(db,'user',user)
103-
if not config.has_option(db,'passwd'): configwrite = True ;config.set(db,'passwd',pw1)
104-
if not config.has_option(db,'name'): configwrite = True ;config.set(db,'name',db[3:])
105-
if not config.has_option(db,'server'): configwrite = True ;config.set(db,'server','leovip148.ncsa.uiuc.edu')
106-
if not config.has_option(db,'port'): configwrite = True ;config.set(db,'port','1521')
96+
97+
databases = ['db-desoper','db-dessci','db-destest'] #most used ones anyways
98+
99+
if db not in databases and not config.has_section(db):
100+
check_db=raw_input('\nDB entered not dessci, desoper or destest or in DES_SERVICE file, continue anyway [y]/n\n')
101+
if check_db in ('n','N','no','No','NO'): sys.exit(0)
102+
103+
if not config.has_section(db):
104+
print '\nAdding section %s to des_service file\n' % db
105+
configwrite = True
106+
kwargs = {'host': server_n, 'port': port_n, 'service_name': db[3:]}
107+
dsn = cx_Oracle.makedsn(**kwargs)
108+
good=False
109+
for i in range(3):
110+
try:
111+
user=raw_input('Enter username : ')
112+
pw1=getpass.getpass(prompt='Enter password : ')
113+
ctemp=cx_Oracle.connect(user,pw1,dsn=dsn)
114+
good = True
115+
break
116+
except:
117+
(type, value, traceback) = sys.exc_info()
118+
print value
119+
if value.message.code == 1017: pass
120+
else: sys.exit(0)
121+
if good:
122+
ctemp.close()
123+
else:
124+
print '\n Check your credentials and/or database access\n'
125+
sys.exit(0)
126+
config.add_section(db)
127+
128+
if not config.has_option(db,'user'): configwrite = True ;config.set(db,'user',user)
129+
if not config.has_option(db,'passwd'): configwrite = True ;config.set(db,'passwd',pw1)
130+
if not config.has_option(db,'name'): configwrite = True ;config.set(db,'name',db[3:])
131+
if not config.has_option(db,'server'): configwrite = True ;config.set(db,'server',server_n)
132+
if not config.has_option(db,'port'): configwrite = True ;config.set(db,'port',port_n)
107133

108134

109135
check = True

easyaccess.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env python
22
__author__ = 'Matias Carrasco Kind'
3-
__version__ = '1.0.0'
3+
__version__ = '1.0.1'
44
# TODO:
55
# add other formats in load tables from fits (like boolean or complex)
66
# clean up, comments
7-
# readline bug (GNU vs libedit)
7+
# readline bug (GNU vs libedit) for history
88
# self upgrade
99

1010
import warnings
@@ -167,7 +167,7 @@ def __init__(self, conf, desconf, db, interactive=True):
167167
self.password = self.desconfig.get('db-' + self.dbname, 'passwd')
168168
kwargs = {'host': self.dbhost, 'port': self.port, 'service_name': self.dbname}
169169
dsn = cx_Oracle.makedsn(**kwargs)
170-
print 'Connecting to DB %s ...' % self.dbname
170+
print 'Connecting to DB ** %s ** ...' % self.dbname
171171
self.con = cx_Oracle.connect(self.user, self.password, dsn=dsn)
172172
self.cur = self.con.cursor()
173173
self.cur.arraysize = self.prefetch
@@ -749,16 +749,19 @@ def do_exit(self, line):
749749
except:
750750
pass
751751
try:
752-
cur.close()
752+
self.cur.close()
753+
except:
754+
pass
755+
try:
756+
self.con.commit()
757+
self.con.close()
753758
except:
754759
pass
755-
self.con.commit()
756-
self.con.close()
757760
if readline_present:
758761
readline.write_history_file(history_file)
759762
if self.writeconfig:
760763
config_mod.write_config(config_file, self.config)
761-
sys.exit(0)
764+
os._exit(0)
762765

763766
def do_clear(self, line):
764767
"""
@@ -1302,23 +1305,26 @@ def to_pandas(cur):
13021305

13031306

13041307
class connect():
1305-
def __init__(self):
1308+
def __init__(self, section = ''):
13061309
conf = config_mod.get_config(config_file)
13071310
pd.set_option('display.max_rows', conf.getint('display', 'max_rows'))
13081311
pd.set_option('display.width', conf.getint('display', 'width'))
13091312
pd.set_option('display.max_columns', conf.getint('display', 'max_columns'))
1310-
desconf = config_mod.get_desconfig(desfile)
1311-
db = conf.get('easyaccess', 'database')
1313+
if section == '':
1314+
db = conf.get('easyaccess', 'database')
1315+
else:
1316+
db = section
13121317
self.prefetch = conf.getint('easyaccess', 'prefetch')
13131318
self.dbname = db
13141319
#connect to db
1320+
desconf = config_mod.get_desconfig(desfile, self.dbname)
13151321
self.user = desconf.get('db-' + self.dbname, 'user')
13161322
self.dbhost = desconf.get('db-' + self.dbname, 'server')
13171323
self.port = desconf.get('db-' + self.dbname, 'port')
13181324
self.password = desconf.get('db-' + self.dbname, 'passwd')
13191325
kwargs = {'host': self.dbhost, 'port': self.port, 'service_name': self.dbname}
13201326
dsn = cx_Oracle.makedsn(**kwargs)
1321-
print 'Connecting to DB...'
1327+
print 'Connecting to DB ** %s ** ...' % self.dbname
13221328
self.con = cx_Oracle.connect(self.user, self.password, dsn=dsn)
13231329

13241330
def ping(self):
@@ -1358,16 +1364,17 @@ def error(self, message):
13581364

13591365
try:
13601366
import readline
1361-
#save = sys.stdout
1362-
#sys.stdout = open("/dev/null","w")
1363-
readline.read_history_file(history_file)
1364-
#sys.stdout = save
13651367
readline_present = True
1366-
readline.set_history_length(conf.getint('easyaccess', 'histcache'))
13671368
except:
1368-
print sys.exc_info()
13691369
readline_present = False
13701370

1371+
if readline_present == True:
1372+
try:
1373+
readline.read_history_file(history_file)
1374+
readline.set_history_length(conf.getint('easyaccess', 'histcache'))
1375+
except:
1376+
'Print readline might give problems accesing the history of commands'
1377+
13711378
parser = MyParser(description='Easy Access', version="version: %s" % __version__)
13721379
parser.add_argument("-c", "--command", dest='command', help="Executes command and exit")
13731380
parser.add_argument("-l", "--loadsql", dest='loadsql', help="Loads a sql command, execute it and exit")
@@ -1376,27 +1383,29 @@ def error(self, message):
13761383
parser.add_argument("-s", "--db", dest='db', help="bypass database name, [dessci, desoper or destest]")
13771384
args = parser.parse_args()
13781385

1379-
desconf = config_mod.get_desconfig(desfile)
13801386

13811387
if args.db is not None:
13821388
db = args.db
1389+
if db[:3] == 'db-' : db=db[3:]
13831390
else:
13841391
db = conf.get('easyaccess', 'database')
13851392

1393+
desconf = config_mod.get_desconfig(desfile,db)
1394+
13861395
if args.command is not None:
13871396
cmdinterp = easy_or(conf, desconf, db, interactive=False)
13881397
cmdinterp.onecmd(args.command)
1389-
sys.exit(0)
1398+
os._exit(0)
13901399
elif args.loadsql is not None:
13911400
cmdinterp = easy_or(conf, desconf, db, interactive=False)
13921401
linein = "loadsql " + args.loadsql
13931402
cmdinterp.onecmd(linein)
1394-
sys.exit(0)
1403+
os._exit(0)
13951404
elif args.loadtable is not None:
13961405
cmdinterp = easy_or(conf, desconf, db, interactive=False)
13971406
linein = "load_table " + args.loadtable
13981407
cmdinterp.onecmd(linein)
1399-
sys.exit(0)
1408+
os._exit(0)
14001409
else:
14011410
os.system(['clear', 'cls'][os.name == 'nt'])
14021411
easy_or(conf, desconf, db).cmdloop()

setup.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import sys
22
import os
3-
from numpy.distutils.core import setup, Extension
3+
try:
4+
from setuptools import setup
5+
except ImportError:
6+
from distutils.core import setup
47
prjdir = os.path.dirname(__file__)
58

69
def read(filename):
@@ -12,7 +15,7 @@ def read(filename):
1215
include_dirs = []
1316
setup(
1417
name='easyaccess',
15-
version='1.0.0',
18+
version='1.0.1',
1619
author='Matias Carrasco Kind',
1720
author_email='[email protected]',
1821
scripts=['easyaccess'],
@@ -21,5 +24,5 @@ def read(filename):
2124
description='Easy Access to access DES DB',
2225
long_description=read('README.md'),
2326
url='https://github.com/mgckind/easyaccess',
24-
install_requires=['pandas','termcolor','pyfits','cx_Oracle'],
27+
install_requires=['pandas','termcolor','pyfits >= 3.3','cx_Oracle'],
2528
)

0 commit comments

Comments
 (0)