Skip to content

Commit

Permalink
Merge pull request #36 from kadrlica/master
Browse files Browse the repository at this point in the history
A few bug fixes and minor changes to output and exit status
  • Loading branch information
mgckind committed Nov 25, 2015
2 parents 8f0b0d2 + bbb15f0 commit 182d4e7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,45 @@ For a short tutorial (To be completed) check [here](http://deslogin.cosmology.il
- Show the execution plan of a query if required
- Many more

## Basic use


## Interactive interpreter

Assuming that ```easyaccess``` is in your path, you can enter the interactive interpreter by calling ```easyaccess``` without any command line arguments:

easyaccess

### Running SQL commands
Once inside the interpreter run SQL queries by adding a ; at the end::
Once inside the interpreter run SQL queries by adding a ";" at the end::

DESDB ~> select ... from ... where ... ;

To save the results into a table add ">" after the end of the query (after ";") and namefile at the end of line

DESDB ~> select ... from ... where ... ; > test.fits

The files supported so far are (.csv, .tab, .fits, .h5) any other extension is ignored
The file types supported so far are: .csv, .tab, .fits, and .h5. Any other extension is ignored.

### Load tables
To load a table it needs to be in a csv format with columns names in the first row
the name of the table is taken from filename


DESDB ~> load_table <filename>

### Load SQL queries
To load sql queries just run:
To load SQL queries just run:

DESDB ~> loadsql <filename.sql>
or

DESDB ~> @filename.sql

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>
The query format is the same as the interpreter, SQL statement must end with ";" and to write output files the query must be followed by " > <output file>"

### Configuration

The configuration file is located at $HOME/.easyaccess/config.ini
The configuration file is located at ```$HOME/.easyaccess/config.ini``` but everything can be configured from inside easyaccess type:

but everything can be configured from inside easyaccess

type:

DESDB ~> help config
to see the meanings of all the options, and:
Expand All @@ -90,8 +90,14 @@ and to see any particular option (e.g., timeout):

DESDB ~> config timeout show

## Command-line usage

Much of the functionality provided through the interpreter is also available directly from the command line. To see a list of command-line options, use the ```--help``` option

easyaccess --help

### TODO
## TODO
- There is a bug with some versions of readline
- Other small changes when loading tables
- Self-upgrade
- Refactor the code so that it isn't in one huge file
13 changes: 9 additions & 4 deletions config_ea.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from __future__ import print_function
#from future import standard_library
#standard_library.install_aliases()
from builtins import input
from builtins import range
# Config file
import configparser

# For compatibility with old python
try:
from builtins import input, range
import configparser
except ImportError:
from __builtin__ import input, range
import ConfigParser as configparser

import getpass
import sys
import cx_Oracle
Expand Down
48 changes: 26 additions & 22 deletions easyaccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@

__author__ = 'Matias Carrasco Kind'
__version__ = '1.2.0'
from builtins import input
from builtins import str
from builtins import range

# For compatibility with old python
try:
from builtins import input, str, range
except ImportError:
from __builtin__ import input, str, range

import warnings

warnings.filterwarnings("ignore")
Expand Down Expand Up @@ -55,7 +59,7 @@ def colored(line, color):

# check if old path is there
ea_path_old = os.path.join(os.environ["HOME"], ".easyacess/")
if os.path.exists(ea_path_old):
if os.path.exists(ea_path_old) and os.path.isdir(ea_path_old):
if not os.path.exists(history_file):
shutil.copy2(os.path.join(os.environ["HOME"], ".easyacess/history"), history_file)
if not os.path.exists(config_file):
Expand Down Expand Up @@ -336,6 +340,7 @@ def __init__(self, conf, desconf, db, interactive=True, quiet=False):
kwargs = {'host': self.dbhost, 'port': self.port, 'service_name': self.dbname}
self.dsn = cx_Oracle.makedsn(**kwargs)
if not self.quiet: print('Connecting to DB ** %s ** ...' % self.dbname)
#if not self.quiet: sys.stdout.write('Connecting to DB ** %s ** ...' % self.dbname)
connected = False
for tries in range(3):
try:
Expand All @@ -354,7 +359,7 @@ def __init__(self, conf, desconf, db, interactive=True, quiet=False):
self.cur = self.con.cursor()
self.cur.arraysize = self.prefetch
msg = self.last_pass_changed()
if msg: print(msg)
if msg and not self.quiet: print(msg)


def handler(self, signum, frame):
Expand Down Expand Up @@ -777,7 +782,6 @@ def completedefault(self, text, line, begidx, lastidx):

# ## QUERY METHODS


def last_pass_changed(self):
"""
Return creation time and last time password was modified
Expand All @@ -795,8 +799,8 @@ def last_pass_changed(self):
msg = colored("*Important* ", "red") + 'Last time password change was ' + colored("%d" % ptime,
"red") + " days ago"
if ptime == ctime: msg += colored(" (Never in your case!)", "red")
msg += '\n Please change it soon using the ' + colored("set_password",
"cyan") + ' command and you will get rid of this message\n'
msg += '\n Please change it using the ' + colored("set_password",
"cyan") + ' command to get rid of this message\n'
return msg


Expand Down Expand Up @@ -1285,8 +1289,9 @@ def do_clear(self, line):
"""
# TODO: platform dependent
# tmp = sp.call('clear', shell=True)
tmp = os.system(['clear', 'cls'][os.name == 'nt'])

sys.stdout.flush()
#tmp = os.system(['clear', 'cls'][os.name == 'nt'])
pass

def do_config(self, line):
"""
Expand Down Expand Up @@ -1574,7 +1579,7 @@ def complete_user_tables(self, text, line, start_index, end_index):

def do_describe_table(self, arg, clear=True):
"""
DB:This tool is useful in noting the lack of documentation for the
DB: This tool is useful in noting the lack of documentation for the
columns. If you don't know the full table name you can use tab
completion on the table name. Tables of usual interest are described
Expand Down Expand Up @@ -1817,7 +1822,7 @@ def load_data(self, filename):
raise Exception(msg)
# Monkey patch to grab columns and values
DF.ea_get_columns = DF.columns.values.tolist
DF.eag_get_values = DF.values.tolist
DF.ea_get_values = DF.values.tolist
elif format in ('fits', 'fit'):
try:
DF = fitsio.FITS(filename)
Expand Down Expand Up @@ -2011,16 +2016,17 @@ def complete_append_table(self, text, line, start_idx, end_idx):

def do_add_comment(self, line):
"""
DB:Add comments to table and/or columns inside tables
DB: Add comments to table and/or columns inside tables
Usage:
- add_comment table <TABLE> 'Comments on table"
- add_comment column <TABLE.COLUMN> 'Comments on columns"
- add_comment table <TABLE> 'Comments on table'
- add_comment column <TABLE.COLUMN> 'Comments on columns'
Ex: add_comment table MY_TABLE 'This table contains info"
add_comment columns MY_TABLE.ID 'Id for my objects"
Ex: add_comment table MY_TABLE 'This table contains info'
add_comment columns MY_TABLE.ID 'Id for my objects'
This command support smart-autocompletion
This command supports smart-autocompletion. No `;` is
necessary (and it will be inserted into comment if used).
"""

Expand Down Expand Up @@ -2190,13 +2196,13 @@ def mytables(self):
"""
List tables in own schema
"""
self.do_mytables('', clear=False)
self.do_mytables('')

def myquota(self):
"""
Show quota in current database
"""
self.do_myquota('', clear=False)
self.do_myquota('')

def load_table(self, table_file, name=''):
"""
Expand Down Expand Up @@ -2329,5 +2335,3 @@ def colored(line, color):
else:
initial_message(args.quiet, clear=True)
easy_or(conf, desconf, db, quiet=args.quiet).cmdloop()


0 comments on commit 182d4e7

Please sign in to comment.