Examples - sql manage.py

From PeformIQ Upgrade
Jump to navigation Jump to search
#!/usr/bin/env python
#
#       Author:  Peter Harding  <plh@performiq.com.au>
#
#                PerformIQ Pty. Ltd.
#                Level 6,
#                170 Queen Street,
#                MELBOURNE, VIC, 3000
#
#                Fax:     03 9578 7810
#                Mobile:  0418 375 085
#  
#           @(#) [1.0.01] sql_manage.py 2011-06-12
#
#
# NAME
#   sql_manage.py - Setup Sqlite data for appdb data preaparation
#
# SYNOPSIS
#   sql_manage.py [-dv]
#
# PARAMETERS
#   See __doc__ below
#
# DESCRIPTION
#   Analyse barcodes
#
# RETURNS
#   0 for successful completion, 1 for any error
#
# FILES
#   ...
#
#-----------------------------------------------------------------------
"""
Usage:

   $ sql_manage.py [-dv] 

Parameters:

   -d              Debug
   -w <date>       When (e.g.  -w  20110613)
   -t              User test data (equivalent to -w  TST)
   -v              Verbose


"""
#-----------------------------------------------------------------------

import os
import re
import sys
import glob
import time
import getopt
import sqlite3

#-----------------------------------------------------------------------

from datetime import datetime, timedelta
from urllib import quote

#-----------------------------------------------------------------------

import appdb

from appdb import SqliteDB, DbCustomer

#-----------------------------------------------------------------------

__version__    = "1.0.0"

debug_lvl      = 0
quiet_flg      = False
verbose_flg    = False
freq_flg       = True
countries      = []
despatches     = {}
departures     = {}
wrk_dir        = None
f_log          = None
customers      = []

DB_NAME        = 'appdb_data.db'
DB_NAME        = 'appdb_noldap.db'
DB_NAME        = 'appdb_soap.db'
DB_NAME        = 'appdb_users.sqlite'

p_event_type   = re.compile(r':EventType>(.*)<')

#=======================================================================

class Enum(set):
   pass

   #-----------------------------------------------------------------

   def __getattr__(self, name):
      if name in self:
         return name
      raise AttributeError

#=======================================================================

def load_customers(name=None):
   if not name:
      return None

   file_path = 'data/%s.csv' % name

   try:
      f_in = open(file_path, 'r')
   except IOError, msg:
      sys.stderr.write(f + ': cannot open: ' + `msg` + '\n')
      sys.exit(1)

   line_cnt    = 0
   no_events   = 0
   events      = []

   while True:
      line = f_in.readline()

      if not line: break

      line = line[:-1]

      customer = DbCustomer()

      customer.fromCsv(line)

      line_cnt += 1

      customers.append(customer)

   f_in.close()

   return customers

#-----------------------------------------------------------------------

def db_load(db_name, names=None):
   global wrk_dir

   if not names:
      return False

   wrk_dir = os.getcwd()

   session = SqliteDB(db_name)

   session.Drop('Customer')
   session.Create('Customer')

   for name in names:
      rows = load_customers(name)

      for row in rows:
         session.Insert(row)

   session.Close()
 
   return True

#-----------------------------------------------------------------------

def create(db_name, table):
   session = SqliteDB(db_name)

   session.Create(table)

   session.Close()

#-----------------------------------------------------------------------

def drop(db_name, table):
   session = SqliteDB(db_name)

   session.Drop(table)

   session.Close()

#-----------------------------------------------------------------------

def schema(db_name):
   session = SqliteDB(db_name)

   session.Schema()

   session.Close()

#-----------------------------------------------------------------------

def pick(db_name, table, no=10):
   session = SqliteDB(db_name)

   cust_list = session.SelectOkToSetup(table, no)

   session.Close()

   for cust in cust_list:
      print cust

   return list

#-----------------------------------------------------------------------

def main():
   global debug_lvl
   global quiet_flg, verbose_flg
   global f_log

   try:
      username = os.environ['USER']
   except:
      print "Set USER environment variable and re-run"
      sys.exit(0)

   try:
      opts, args = getopt.getopt(sys.argv[1:], "C:dD:f:IlL:o:P:qstvVw:X:?")
   except getopt.error, msg:
      print __doc__
      return 1

   Modes = Enum(["Create", "Drop", "List", "Load", "Pick"])


   customers  = None
   names      = []
   log_flg    = False
   db_name    = DB_NAME
   mode       = None
   tables     = []

   for opt, arg in opts:
      if opt == '-d':
         debug_lvl       += 1
      elif opt == '-D':
         debug_lvl        = int(arg)
      elif opt == '-C':
         tables.append(arg)
         mode             = Modes.Create
      elif opt == '-f':
         fname            = arg
      elif opt == '-I':
         mode             = Modes.Create
         tables           = appdb.Tables
      elif opt == '-l':
         log_flg          = True
      elif opt == '-L':
         file_name        = arg
         mode             = Modes.Load
      elif opt == '-n':
         db_name          = arg
      elif opt == '-o':
         orig             = arg
      elif opt == '-P':
         table            = arg
         mode             = Modes.Pick
      elif opt == '-q':
         quiet_flg        = True
      elif opt == '-w':
         names.append(arg)
      elif opt == '-s':
         schema(db_name)
         return 0
      elif opt == '-t':
         names.append('TST')
      elif opt == '-v':
         verbose_flg      = True
      elif opt == '-V':
         print "Version: %s" % __version__
         return 0
      elif opt == '-X':
         table            = arg
         mode             = Modes.Drop
      elif opt == '-?':
         print __doc__
         return 0

   if log_flg:
      try:
         f_log = open('loader.log', 'a+')
      except IOError, msg:
         sys.stderr.write(f + ': cannot open: ' + `msg` + '\n')
         sys.exit(1)

   if verbose_flg:  print ">>> %s" % mode

   if mode == Modes.Create:
      for table in tables:
         create(db_name, table)
   elif mode == Modes.Drop:
      drop(db_name, table)
   elif mode == Modes.Load:
      db_load(db_name, [file_name, ])
   elif mode == Modes.List:
      pass
   elif mode == Modes.Pick:
      customers = pick(db_name, table)

   return 0

#-----------------------------------------------------------------------

if __name__ == '__main__' or __name__ == sys.argv[0]:
   sys.exit(main())

#-----------------------------------------------------------------------

"""
Revision History:

     Date     Who   Description
   --------   ---   --------------------------------------------------
   20110612   plh   Initial implementation

Problems to fix:

To Do:

Issues:


Sqlite Help:

   .backup ?DB? FILE      Backup DB (default "main") to FILE
   .bail ON|OFF           Stop after hitting an error.  Default OFF
   .databases             List names and files of attached databases
   .dump ?TABLE? ...      Dump the database in an SQL text format
                            If TABLE specified, only dump tables matching
                            LIKE pattern TABLE.
   .echo ON|OFF           Turn command echo on or off
   .exit                  Exit this program
   .explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.
                            With no args, it turns EXPLAIN on.
   .header(s) ON|OFF      Turn display of headers on or off
   .help                  Show this message
   .import FILE TABLE     Import data from FILE into TABLE
   .indices ?TABLE?       Show names of all indices
                            If TABLE specified, only show indices for tables
                            matching LIKE pattern TABLE.
   .load FILE ?ENTRY?     Load an extension library
   .log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
   .mode MODE ?TABLE?     Set output mode where MODE is one of:
                            csv      Comma-separated values
                            column   Left-aligned columns.  (See .width)
                            html     HTML <table> code
                            insert   SQL insert statements for TABLE
                            line     One value per line
                            list     Values delimited by .separator string
                            tabs     Tab-separated values
                            tcl      TCL list elements
   .nullvalue STRING      Print STRING in place of NULL values
   .output FILENAME       Send output to FILENAME
   .output stdout         Send output to the screen
   .prompt MAIN CONTINUE  Replace the standard prompts
   .quit                  Exit this program
   .read FILENAME         Execute SQL in FILENAME
   .restore ?DB? FILE     Restore content of DB (default "main") from FILE
   .schema ?TABLE?        Show the CREATE statements
                            If TABLE specified, only show tables matching
                            LIKE pattern TABLE.
   .separator STRING      Change separator used by output mode and .import
   .show                  Show the current values for various settings
   .stats ON|OFF          Turn stats on or off
   .tables ?TABLE?        List names of tables
                            If TABLE specified, only list tables matching
                            LIKE pattern TABLE.
   .timeout MS            Try opening locked tables for MS milliseconds
   .width NUM1 NUM2 ...   Set column widths for "column" mode
   .timer ON|OFF          Turn the CPU timer measurement on or off
"""