Package pysql :: Module pysqlmain
[hide private]
[frames] | no frames]

Source Code for Module pysql.pysqlmain

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5  Pysql is an Oracle client for all those who suffer from sqlplus. 
  6   
  7  It aims to bring confort and power to user without a heavy graphical application. 
  8   
  9  Developpers (in order of arrival in the project) : 
 10  - Sébastien Renard 
 11  - Sébastien Delcros 
 12  - <your name here if you want to contribute ! (your firstname must be Sebastien of course) 
 13   
 14  Requirements: 
 15  - Mandatory: 
 16      o python. Pysql has been tested with Python 2.4.x and 2.5.0 
 17      o cx_Oracle : http://www.cxtools.net/default.aspx?nav=cxorlb 
 18  - Optionnal: 
 19      o pyreadline for Windows user to have input completion 
 20      o cTypes for windows user to have input completion (already included in Python 2.5) 
 21      o pydot for graphical functions (show datamodel in png for example) 
 22      o pyparser as a dependancy of pydot 
 23   
 24  Supported plateforms: 
 25  - GNU/Linux (developper choice!) 
 26  - Windows (some limitations: poor completion and no colors) 
 27  - AIX (cx_Oracle is quite painfull to install, but all works correctly) 
 28   
 29  Pysql should work on many other platform (like Solaris, HPUX, MacOSX, *BSD) but has not been 
 30  tested. Feel free to report success to the pysql team. 
 31   
 32  Installation: 
 33  Very simple for the moment, you can put pysql everywhere. Just put the pysql command in your path. 
 34   
 35  Internationalisation (i18n): 
 36  Pysql is available in English (base langage for developpement) and French. 
 37  To generate other language messages, use "i18.sh" script in the pysql directory. You'll need 
 38  GNU Gettext to achieve this. 
 39  Next, to use pysql in French, set the LANG variable to fr (export LANG=fr) « et voilà » ! 
 40   
 41  Documentation: 
 42  API documentation can be found in doc/ directory. 
 43  User documentation is available in pysql itself. Type « help » to get some help. 
 44   
 45  Modules overview: 
 46  - pysql.py: main module. Just used to setup i18n and fire the shell 
 47  - pysqlshell.py: user shell interaction. All pysql « command » are here. 
 48                  pysqlshell command handle argument parsing and display. 
 49                  Real works is done in pysqlfunction. 
 50  - pysqlfunction.py :advanced functions for pysql. 
 51  - pysqlconf.py: all configuration stuff (parameters, cache, history, user sql library...) 
 52  - pysqloraobject.py: object model for main Oracle objects (table, view, package, index). 
 53                      this is used for functions like desc and edit 
 54  - pysqlgraphics.py: all graphical stuff that creates images with graphviz 
 55  - pysqlqueries.py: an effort is done to put all SQL queries in this module to have a cleaner code 
 56  - pysqlcolor.py: color bank definition 
 57  - pysqldb.py: all connection and cursor handling. Also manages background queries 
 58  - pysqlexception.py: all pysql defined exceptions 
 59   
 60  Developpement conventions: 
 61  - classes must begin with an uppercase caracter 
 62  - variables must begin with a lowercase caracter and user alternate uppercase instead of _ 
 63  - all lines must be less than 109 characters (ask developpers why) 
 64  - all patches must be sent to sebastien@digitalfox.org 
 65   
 66  @author: Sébastien Renard (sebastien.renard@digitalfox.org) 
 67  @author: Sébastien Delcros (Sebastien.Delcros@gmail.com) 
 68  @license: GNU GPL V3 
 69  """ 
 70   
 71  # Python imports: 
 72  import gettext 
 73  import locale 
 74  import os 
 75  import sys 
 76  from os.path import dirname, join, pardir 
 77  from optparse import OptionParser 
 78   
 79  # Tests if requisite modules are correctly installed 
 80  # Oracle (mandatory) 
 81  try: 
 82      import cx_Oracle 
 83  except ImportError: 
 84      # Untranslatable error message (i18n still not initialized at this step) 
 85      print "cx_Oracle module cannot be loaded.\nPlease, ensure you correctly install it from:" 
 86      print "http://cx-oracle.sf.net" 
 87      print "And that have the according Oracle client installation." 
 88      print "Get it from the Oracle site : http://www.oracle.com" 
 89      print "(press enter key to exit)" 
 90      sys.stdin.readline() 
 91      sys.exit(1) 
 92  # readline is a separate module for Windows 
 93  if os.name=="nt": 
 94      try: 
 95          import readline 
 96      except ImportError: 
 97          print "pyreadline module cannot be found on your system and is needed on Windows.\nPlease, get it at:" 
 98          print "http://ipython.scipy.org/moin/PyReadline/Intro" 
 99          print "(press enter key to exit)" 
100          sys.stdin.readline() 
101          sys.exit(1) 
102   
103  # Pysql imports: 
104  from pysqlshell import PysqlShell 
105  from pysqlconf import PysqlConf 
106  from pysqlexception import PysqlException 
107  import pysqlupdate 
108  from pysqlhelpers import printStackTrace 
109   
110 -def main():
111 """Pysql main function""" 112 rc=0 # Return code 113 114 # Options & args stuf 115 (options, argv)=parseOptions() 116 117 # i18n stuff 118 if os.name=="nt": 119 # Windows stuff is never like everybody... 120 i18nPath=join(dirname(sys.argv[0]), "share", "locale") 121 else: 122 # Unix std path 123 i18nPath=join(dirname(sys.argv[0]), pardir, "share", "locale") 124 # Loads message catalog 125 gettext.install("pysql", i18nPath, unicode=1) 126 127 # Loads config (first time) 128 conf=PysqlConf.getConfig() 129 130 # Sets the locale 131 setLocale(conf) 132 133 try: 134 if options.update: 135 try: 136 pysqlupdate.checkForUpdate(options.proxyHost, options.proxyUser, options.proxyPassword) 137 except KeyboardInterrupt: 138 print _("Aborting update") 139 elif options.version: 140 print _("PySQL - %s") % pysqlupdate.currentVersion() 141 else: 142 # Default is to launch pysql in standard mode (local client) 143 shell=PysqlShell(silent=options.silent, argv=argv) 144 if options.oneTryLogin and shell.db==None: 145 rc=1 146 else: 147 shell.loop() 148 rc=shell.rc 149 # Bye 150 if os.name=="nt" and not options.version: 151 # Don't exit too fast for windows users, else they don't see error sum up 152 print _("(press any key to exit)") 153 sys.stdin.readline() 154 except StandardError, e: 155 # Just a hook for a more pleasant error handling 156 print "\n==> Unrecoverable error during initialisation. Exiting <==" 157 printStackTrace() 158 print _("(press enter key to exit)") 159 sys.stdin.readline() 160 except PysqlException, e: 161 print "*** " + _("Pysql error") + " ***" 162 print "\t%s" % e 163 164 # Bye! 165 sys.exit(rc)
166
167 -def setLocale(conf):
168 """Sets the right encoding""" 169 try: 170 codec=locale.getpreferredencoding() 171 except: 172 # default to latin-1 173 codec="latin-1" 174 if codec is None: 175 codec="latin-1" 176 # Tests if codec exists 177 try: 178 str().encode(codec) 179 except LookupError: 180 codec="latin-1" 181 182 # Stores codec in config 183 conf.setCodec(codec) 184 185 # Sets default encoding for stdout 186 reload(sys) 187 sys.setdefaultencoding(codec)
188
189 -def parseOptions():
190 """Parses pysql command argument using optparse python module""" 191 parser=OptionParser() 192 193 # Version 194 parser.add_option("-v", "--version", dest="version", action="store_true", 195 help="prints PySQL version") 196 197 # Silent mode 198 parser.add_option("-S", "--Silent", dest="silent", action="store_true", 199 help="sets silent mode which suppresses the dispay of banner, prompts and echoing of commands") 200 201 # Login 202 parser.add_option("-L", "--Login", dest="oneTryLogin", action="store_true", 203 help="exits if login attempt failed, instead of starting not connected") 204 205 # Update mode 206 parser.add_option("-u", "--update", dest="update", action="store_true", 207 help="checks if PySQL update are available") 208 parser.add_option("-H", "--proxyHost", dest="proxyHost", type="string", default=None, 209 help="proxy hostname for PySQL update. \t\tExample: 'http://my-proxy.mydomain:8080'") 210 parser.add_option("-U", "--proxyUser", dest="proxyUser", type="string", default="", 211 help="proxy username if authentication is required") 212 parser.add_option("-P", "--proxyPassword", dest="proxyPassword", type="string", default="", 213 help="proxy password if authentication is required") 214 215 return parser.parse_args()
216 217 ###### Starts Pysql ######## 218 if __name__ == "__main__": 219 main() 220