Package json_to_relation :: Module output_disposition
[hide private]
[frames] | no frames]

Source Code for Module json_to_relation.output_disposition

  1  ''' 
  2  Created on Sep 14, 2013 
  3   
  4  @author: paepcke 
  5  ''' 
  6  import csv 
  7  import sys 
  8   
  9   
10 -class OutputDisposition(object):
11 ''' 12 Specifications for where completed relation rows 13 should be deposited, and in which format. Source 14 options are files, stdout, and a MySQL table. 15 16 Also defined here are available output formats, of 17 which there are two: CSV, and SQL insert statements. 18 '''
19 - def __init__(self, outputDest):
20 ''' 21 @param outputDest: os file descriptor for output 22 @type outputDest: File 23 ''' 24 self.outputDest = outputDest
25
26 - def __enter__(self):
27 return self.outputDest
28
29 - def __exit__(self,excType, excValue, excTraceback):
30 try: 31 self.outputDest.close() 32 except: 33 # If the conversion itself when fine, then 34 # raise this exception from the closing attempt. 35 # But if the conversion failed, then have the 36 # system re-raise that earlier exception: 37 if excValue is None: 38 raise IOError("Could not close the output of the conversion: %s" % sys.exc_info()[0]) 39 40 # Return False to indicate that if the conversion 41 # threw an error, the exception should now be re-raised. 42 # If the conversion worked fine, then this return value 43 # is ignored. 44 return False
45 46 #--------------------- Available Output Formats 47
48 - class OutputFormat():
49 CSV = 0 50 SQL_INSERT_STATEMENTS = 1
51 52 #--------------------- Available Output Destination Options: 53
54 -class OutputPipe(OutputDisposition):
55 - def __init__(self):
56 self.fileHandle = sys.stdout 57 # Make file name accessible as property just like 58 # Python file objects do: 59 self.name = "<stdout>" # @UnusedVariable 60 self.csvWriter = csv.writer(sys.stdout, dialect='excel', delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
61
62 - def close(self):
63 pass # don't close stdout
64
65 - def __str__(self):
66 return "<OutputPipe:<stdout>"
67
68 - def writerow(self, colElementArray):
69 self.csvWriter.writerow(colElementArray)
70
71 -class OutputFile(OutputDisposition):
72 - def __init__(self, fileName, options='ab'):
73 # Make file name accessible as property just like 74 # Python file objects do: 75 self.name = fileName # @UnusedVariable 76 # Open the output file as 'append' and 'binary' 77 # The latter is needed for Windows. 78 self.fileHandle = open(fileName, options) 79 self.csvWriter = csv.writer(self.fileHandle, dialect='excel', delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
80
81 - def close(self):
82 self.fileHandle.close()
83
84 - def __str__(self):
85 return "<OutputFile:%s>" % self.name
86
87 - def writerow(self, colElementArray):
88 self.csvWriter.writerow(colElementArray)
89
90 -class OutputMySQLTable(OutputDisposition):
91 - def __init__(self, server, pwd, dbName, tbleName):
92 raise NotImplementedError("MySQL connector not yet implemented") 93 self.name = "<MySQL:%s:%s:%s>" % (server,dbName,tbleName) 94 self.server = server 95 self.pwd = pwd 96 self.dbName = dbName 97 self.tbleName = tbleName 98 self.fileHandle = self.connect()
99
100 - def connect(self):
101 raise NotImplementedError("MySQL connector not yet implemented")
102
103 - def close(self):
104 raise NotImplementedError("MySQL connector not yet implemented")
105
106 - def __str__(self):
107 return "<OutputMySQLTable--%s>" % self.name
108
109 - def writerow(self, fd, colElementArray):
110 raise NotImplementedError("MySQL connector not yet implemented")
111