77from .Log import Log
88
99
10- class Command :
10+ class Command ( object ) :
1111 timeout = 15
1212
1313 def __init__ (self , args , callback , query = None , encoding = 'utf-8' ,
14- options = None , timeout = 15 , silenceErrors = False ):
14+ options = None , timeout = 15 , silenceErrors = False , stream = False ):
1515 if options is None :
1616 options = {}
1717
18+ self .stream = stream
1819 self .args = args
1920 self .callback = callback
2021 self .query = query
@@ -43,6 +44,23 @@ def run(self):
4344 env = os .environ .copy (),
4445 startupinfo = si )
4546
47+ if self .stream :
48+ self .process .stdin .write (self .query .encode ())
49+ self .process .stdin .close ()
50+ for line in self .process .stdout :
51+ self .callback (line .decode (self .encoding ,
52+ 'replace' ).replace ('\r ' , '' ))
53+
54+ queryTimerEnd = time .time ()
55+ if 'show_query' in self .options and self .options ['show_query' ]:
56+ resultInfo = "/*\n -- Executed querie(s) at {0} took {1:.3f}ms --" .format (
57+ str (time .strftime ("%Y-%m-%d %H:%M:%S" , time .localtime (queryTimerStart ))),
58+ (queryTimerEnd - queryTimerStart ))
59+ resultLine = "-" * (len (resultInfo ) - 3 )
60+ resultString = "{0}\n {1}\n {2}\n {3}\n */" .format (
61+ resultInfo , resultLine , self .query , resultLine )
62+ return self .callback (resultString )
63+
4664 results , errors = self .process .communicate (input = self .query .encode ())
4765
4866 queryTimerEnd = time .time ()
@@ -58,17 +76,17 @@ def run(self):
5876 'replace' ).replace ('\r ' , '' )
5977
6078 if 'show_query' in self .options and self .options ['show_query' ]:
61- resultInfo = "/*\n -- Executed querie(s) at {0} took {1}ms --" .format (
79+ resultInfo = "/*\n -- Executed querie(s) at {0} took {1:.3f }ms --" .format (
6280 str (time .strftime ("%Y-%m-%d %H:%M:%S" , time .localtime (queryTimerStart ))),
63- str (queryTimerEnd - queryTimerStart ))
81+ (queryTimerEnd - queryTimerStart ))
6482 resultLine = "-" * (len (resultInfo ) - 3 )
6583 resultString = "{0}\n {1}\n {2}\n {3}\n */\n {4}" .format (
6684 resultInfo , resultLine , self .query , resultLine , resultString )
6785
6886 self .callback (resultString )
6987
7088 @staticmethod
71- def createAndRun (args , query , callback , options = None , timeout = 15 , silenceErrors = False ):
89+ def createAndRun (args , query , callback , options = None , timeout = 15 , silenceErrors = False , stream = False ):
7290 if options is None :
7391 options = {}
7492 command = Command (args , callback , query , options = options ,
@@ -78,18 +96,14 @@ def createAndRun(args, query, callback, options=None, timeout=15, silenceErrors=
7896
7997class ThreadCommand (Command , Thread ):
8098 def __init__ (self , args , callback , query = None , encoding = 'utf-8' ,
81- options = None , timeout = Command .timeout , silenceErrors = False ):
99+ options = None , timeout = Command .timeout , silenceErrors = False , stream = False ):
82100 if options is None :
83101 options = {}
84102
85- self .args = args
86- self .callback = callback
87- self .query = query
88- self .encoding = encoding
89- self .options = options
90- self .timeout = timeout
91- self .silenceErrors = silenceErrors
92- self .process = None
103+ Command .__init__ (self , args , callback , query = query ,
104+ encoding = encoding , options = options ,
105+ timeout = timeout , silenceErrors = silenceErrors ,
106+ stream = stream )
93107 Thread .__init__ (self )
94108
95109 def stop (self ):
@@ -108,13 +122,14 @@ def stop(self):
108122 pass
109123
110124 @staticmethod
111- def createAndRun (args , query , callback , options = None , timeout = Command .timeout , silenceErrors = False ):
125+ def createAndRun (args , query , callback , options = None ,
126+ timeout = Command .timeout , silenceErrors = False , stream = False ):
112127 # Don't allow empty dicts or lists as defaults in method signature,
113128 # cfr http://nedbatchelder.com/blog/200806/pylint.html
114129 if options is None :
115130 options = {}
116131 command = ThreadCommand (args , callback , query , options = options ,
117- timeout = timeout , silenceErrors = silenceErrors )
132+ timeout = timeout , silenceErrors = silenceErrors , stream = stream )
118133 command .start ()
119134 killTimeout = Timer (command .timeout , command .stop )
120135 killTimeout .start ()
0 commit comments