def _doAfterExtractionProcessCompleted(self): alpha_num_only = lambda s: ' '.join(''.join([ ch if (ch.isalnum()) else ' ' for ch in s if (ch.isalnum()) or (ch in [' ', '_', '-']) ]).split(' ')) self.number = 2 self.count = 1 self.deduped_Contacts = self.dedupeContacts(self.__contacts__) self.appendText('%s :: (%d/%d) contacts.' % (ObjectTypeName.objectSignature(self), len(self.deduped_Contacts), len(self.__contacts__))) if (not os.path.exists(self._csv_model.filename)): self._csv_model.filename = os.sep.join( [self._log_path, 'data_raw.csv']) self.appendText( '%s :: BEGIN: "%s".' % (ObjectTypeName.objectSignature(self), self._csv_model.filename)) info_string = CSV.write_as_csv(self._csv_model.filename, self.__contacts__) if (len(info_string) > 0): self.appendText('%s :: %s-->%s.' % (ObjectTypeName.objectSignature(self), self._csv_model.filename, info_string)) self.count = 2 fname = os.sep.join([self._log_path, 'data_cleaned.csv']) info_string = CSV.write_as_csv(fname, self.deduped_Contacts) if (len(info_string) > 0): self.appendText( '%s :: %s-->%s.' % (ObjectTypeName.objectSignature(self), fname, info_string)) self.appendText( '%s :: END! "%s".' % (ObjectTypeName.objectSignature(self), self._csv_model.filename))
def __init__(self, __version__, __func__): '''__version__ is the version for the Pyro Server and NS Authentication to ensure the client can connect to the server.''' if (_isRunningLocal): _beginTime = time.time() Pyro_config.PYRO_COMPRESSION = 1 Pyro_config.PYRO_CHECKSUM = 1 Pyro_config.PYRO_SOCK_KEEPALIVE = 1 Pyro_config.PYRO_MULTITHREADED = 1 Pyro.core.initClient() ident = __func__(__version__) locator = Pyro.naming.NameServerLocator(identification=ident) print '%s :: Searching Naming Service...' % ( ObjectTypeName.objectSignature(self)) ns = locator.getNS() print '%s :: Naming Service found at %s port %s' % ( ObjectTypeName.objectSignature(self), ns.URI.address, ns.URI.port) print '%s :: Binding to Remote Object.' % ( ObjectTypeName.objectSignature(self)) try: URI = ns.resolve(':SalesForceProxy.version_%s' % (__version__.replace('.', '_'))) print '%s :: URI: %s' % (ObjectTypeName.objectSignature(self), URI) except Pyro.core.PyroError, x: print '%s :: Couldn\'t bind object, nameserver says: %s' % ( ObjectTypeName.objectSignature(self), x) raise SystemExit
def _rowByColumn(self,key,name_or_number): iCol = self.column_number_for_name(name_or_number) nCol = self.header[iCol] for r in self.rows: if (r[iCol] == key): rr = self.__row_by_col__[nCol][key] assert len(r) == len(rr), '%s Problem with #1.' % (ObjectTypeName.objectSignature(self)) for i in xrange(0,len(rr)): assert r[i] == rr[i], '%s Problem with #2.' % (ObjectTypeName.objectSignature(self)) return r return None
def func(*args, **kwargs): kw = kwargs.items() kw.sort() key = (args, tuple(kw)) try: v = _cache[key] if (self.isDebugging): print '\n%s :: cache :: "%s"' % (ObjectTypeName.objectSignature(self),key) if (time.time() - v[1]) > self.timeout: raise KeyError except KeyError: if (self.isDebugging): print '\n%s :: new (%d) :: "%s"' % (ObjectTypeName.objectSignature(self),len(_cache),key) v = _cache[key] = f(*args,**kwargs),time.time() return v[0]
def __init__(self, hostname, port, username, password=None, use_manual_auth=False, callback=None, auto_close=True, logger=None, logPath=os.path.abspath(os.path.dirname(sys.argv[0]))): self.__hostname__ = hostname self.__port__ = port self.__username__ = username self.__password__ = password self.__transport__ = None self.__callback__ = callback self.__autoclose__ = auto_close self.__logPath__ = logPath self.__use_manual_auth__ = use_manual_auth self.logger = paramiko.util.get_logger(__name__) if (logger): self.logger = logger else: if (self.__logPath__) and (os.path.exists( self.__logPath__)) and (os.path.isdir(self.__logPath__)): _utils._makeDirs(self.__logPath__) paramiko.util.log_to_file( os.path.join( self.__logPath__, '%s.log' % (ObjectTypeName.objectSignature(self)))) if self.hostname.find(':') >= 0: self.__hostname__, portstr = hostname.split(':') self.__port__ = int(portstr) self.__open__()
def renderHTML(self, subj, body): from vyperlogix.html import myOOHTML as oohtml h_html = oohtml.Html() if (misc.isString(subj)) and (misc.isList(body)): h_html.text(oohtml.oohtml.DOCTYPE_40_TRANSITIONAL) html_html = h_html.tag(oohtml.oohtml.HTML) head_html = html_html.tag(oohtml.oohtml.HEAD) head_html.tagOp(oohtml.oohtml.META, http_equiv=oohtml.oohtml.CONTENT_TYPE, content=oohtml.oohtml.TEXT_HTML_CHARSET_ISO_8859_1) head_html.metas(( oohtml.oohtml.AUTHOR, "Ray C Horn ([email protected])" ), (oohtml.oohtml.KEYWORDS, "SMTP Email Message"), ( oohtml.oohtml.DESCRIPTION, "The contents of this email are considered to be confidential unless otherwise specified." ), (oohtml.oohtml.ROBOTS, oohtml.oohtml.ALL)) head_html.tagTITLE('SMTP Email Message') body_html = html_html.tag(oohtml.oohtml.BODY) idContent = body_html.tag(oohtml.oohtml.DIV, id="content", style="background-color: white") idContent.html_simple_table(body) pass else: print >> sys.stderr, '%s :: "subj" parm must be of type str and "body" parm must be of type list rather than of types "%s" and "%s", respectively.' % ( ObjectTypeName.objectSignature(self), type(subj), type(body)) return h_html.toHtml()
def __init__(self, *args, **kwds): self.isVerbose = False if (kwds.has_key('isVerbose')): self.isVerbose = kwds['isVerbose'] del kwds['isVerbose'] self.delay = -1 if (kwds.has_key('delay')): delay = kwds['delay'] try: self.delay, self.min_delay, self.__delay__ = delay except: self.delay, self.min_delay, self.__delay__ = (1000, 5000, 10000) if (self.isVerbose): print '(%s) :: self.delay=%s, self.min_delay=%s, self.__delay__=%s' % ( ObjectTypeName.objectSignature(self), self.delay, self.min_delay, self.__delay__) del kwds['delay'] self.__callback__ = None if (kwds.has_key('callback')): self.__callback__ = kwds['callback'] del kwds['callback'] self.bitmapFile = '' if (kwds.has_key('bitmapFile')): self.bitmapFile = kwds['bitmapFile'] del kwds['bitmapFile'] wx.App.__init__(self, *args, **kwds)
def func(*args, **kwargs): kw = kwargs.items() kw.sort() key = (tuple([arg for arg in list(args) if (misc.isString(arg))]), tuple(kw)) try: v = _cache[key] et = time.time() - v[1] if (self.isDebugging): print '\n%s :: cache :: "%s"' % (ObjectTypeName.objectSignature(self),key) if (et) > self.timeout: raise KeyError except KeyError: v = _cache[key] = f(*args,**kwargs),time.time() if (self.isDebugging): print '\n%s :: new (%d) :: "%s"' % (ObjectTypeName.objectSignature(self),len(_cache),key) return v[0]
def openProcessForPID(self,pid): try: procHandle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, 0, pid) except Exception as details: print '(%s) :: ERROR due to "%s".' % (ObjectTypeName.objectSignature(self),str(details)) procHandle = None return procHandle
def ssh_connect_client(self): """ connect and use paramiko Transport to negotiate SSH2 across the connection """ self.__sftp__ = None if (self.transport is None): self.__transport__ = self.ssh_connect_transport() if (self.__transport__ is not None): try: self.__sftp__ = paramiko.SFTPClient.from_transport( self.transport) except Exception as e: exc_info = sys.exc_info() info_string = '\n'.join(traceback.format_exception(*exc_info)) print >> self.lastError, '\n%s :: Cannot open client connection to %s:%s using username of "%s", Reason: %s' % ( ObjectTypeName.objectSignature(self), self.hostname, self.port, self.username, info_string) try: if (self.sftp is not None): ssh_close() except: pass return None else: return None return self.sftp
def __getitem__(self, name): value = super(SmartFuzzyObject, self).__getitem__(name) if (value is None): raise AttributeError( 'INVALID use of the method that treats %s as if it were a dictionary. Try to use this object as if it had instance variables rather than key,value pairs or use the right key name next time.' % (ObjectTypeName.objectSignature(self))) return value
def end(self, evt): if (ObjectTypeName.typeName(self.frame) != 'wx._core._wxPyDeadObject'): self.__delay__ -= self.delay self.min_delay -= self.delay if (self.isVerbose): print '(%s) :: self.delay=%s, self.min_delay=%s, self.__delay__=%s' % ( ObjectTypeName.objectSignature(self), self.delay, self.min_delay, self.__delay__) bool = False if (callable(self.__callback__)): bool = self.__callback__() and (self.min_delay <= 0) if (self.isVerbose): print '(%s) :: bool=%s, self.min_delay=%s' % ( ObjectTypeName.objectSignature(self), bool, self.min_delay) if (bool) or (self.__delay__ <= 0): if (bool): self.timer.Stop() self.frame.Destroy()
def put(self, localpath, remotepath, callback=None): """ Copy a local file (localpath) to the SFTP server as remotepath. Any exception raised by operations will be passed through. This method is primarily provided as a convenience. The SFTP operations use pipelining for speed. Parameters: * localpath (str) - the local file to copy * remotepath (str) - the destination path on the SFTP server * callback (function(int iBytes, int iTotal, float xfer_rate, int elapsed_secs)) - optional callback function that accepts the bytes transferred so far and the total bytes to be transferred (since 1.7.4) Returns: SFTPAttributes an object containing attributes about the given file (since 1.7.4) """ import time from vyperlogix.misc import _utils _begin_ts = time.time() _callback_ = None def _callback(iBytes, iTotal): if (callable(_callback_)): et = time.time() - _begin_ts if (et == 0): et = 0.00000000001 xfer = iBytes / et try: _callback_(iBytes, iTotal, xfer, et) except Exception as details: print >> sys.stderr, '%s' % str(details) try: if (not callable(callback)): callback = None else: try: callback(0, 0, 0, 0) except Exception as details: print >> sys.stderr, '%s :: Cannot use the callback because it has the following problem: "%s".' % ( ObjectTypeName.objectSignature(self), str(details)) callback = None if (callback is not None): _callback_ = callback result = self.sftp.put(localpath, remotepath, _callback) _end_ts = time.time() _iTotal = result.st_size xfer = _iTotal / (_end_ts - _begin_ts) return [result, xfer] except: exc_info = sys.exc_info() info_string = _utils.asMessage('\n'.join( traceback.format_exception(*exc_info))) print >> sys.stderr, info_string return None return None
def perform_login(self,end_point='https://test.salesforce.com/services/Soap/u/14.0',isVerbose=False): self.sfContext.login_endpoint = end_point try: self.__lastError__ = '' if (isVerbose): print '(%s) :: self.username is "%s".' % (ObjectTypeName.objectSignature(self),self.username) if (self.is_running_securely_for_developers): print 'self.password is "%s".' % (self.password) if (isVerbose): print '(%s) :: self.sfContext.login_endpoint is "%s".' % (ObjectTypeName.objectSignature(self),self.sfContext.login_endpoint) self.__sfdc__ = Connection.connect(self.username, self.password, context=self.sfContext) if (isVerbose): print '(%s) :: self.__sfdc__ is "%s".' % (ObjectTypeName.objectSignature(self),self.__sfdc__) except Exception as details: info_string = _utils.formattedException(details=details) self.__lastError__ = info_string try: del self.__sfdc__ finally: self.__sfdc__ = None
def get_endpoint(self,end_point='test.salesforce.com',isVerbose=False): endPt = self.sfContext.endpoint if (len(end_point) > 0): endPt = self.sfContext.login_endpoint.replace(self.sfServers['production'],end_point) toks = endPt.split('/') v = _utils._float(toks[-1]) if ('%02.1f' % (v) == toks[-1]): toks[-1] = self.api_version endPt = '/'.join(toks) if (isVerbose): print '(%s) :: self.sfContext.login_endpoint is "%s" for "%s".' % (ObjectTypeName.objectSignature(self),endPt,end_point) return endPt
class ZoneEditProxy(MagicObject2): '''This object knows how to interface with a Pyro Server when interfacing with SalesForce using a single sign-on for SF.''' def __init__(self, __host__, __version__, __func__): '''__version__ is the version for the Pyro Server and NS Authentication to ensure the client can connect to the server.''' if (_isRunningLocal): _beginTime = time.time() Pyro_config.PYRO_COMPRESSION = 1 Pyro_config.PYRO_CHECKSUM = 1 Pyro_config.PYRO_SOCK_KEEPALIVE = 1 Pyro_config.PYRO_MULTITHREADED = 1 Pyro.core.initClient() ident = __func__(__version__) locator = Pyro.naming.NameServerLocator(identification=ident) print '%s :: Searching Naming Service...' % ( ObjectTypeName.objectSignature(self)) try: ns = locator.getNS(host=__host__.split(':')[0], port=int(__host__.split(':')[-1]), trace=1) except Exception as e: info_string = _utils.formattedException(details=e) print info_string sys.exit(1) print '%s :: Naming Service found at %s port %s' % ( ObjectTypeName.objectSignature(self), ns.URI.address, ns.URI.port) print '%s :: Binding to Remote Object.' % ( ObjectTypeName.objectSignature(self)) try: URI = ns.resolve(':ZoneEditProxy.version_%s' % (__version__.replace('.', '_'))) print '%s :: URI: %s' % (ObjectTypeName.objectSignature(self), URI) except Pyro.core.PyroError, x: print '%s :: Couldn\'t bind object, nameserver says: %s' % ( ObjectTypeName.objectSignature(self), x) raise SystemExit self.__proxy__ = Pyro.core.getProxyForURI(URI) self.__proxy__._setIdentification(ident) self.__reset_magic__() if (_isRunningLocal): _endTime = time.time() _et = _endTime - _beginTime _rate = 1.0 / _et if (_et != 0) else 1.0 print '(%2.4f, %4.2f reqs/sec) "%s"' % ( _et, _rate, ObjectTypeName.objectSignature(self))
def _doUploadToSalesForce(self): from vyperlogix.sf.sf import SalesForceQuery sfQuery = SalesForceQuery(self.__login_dialog__.sf_login_model) from vyperlogix.sf.magma.customers import SalesForceCustomers customers = SalesForceCustomers(sfQuery) timestamp = sfQuery.sfdc.getServerTimestamp() s_timestamp = str(timestamp) schemas = [] for item in self.deduped_Contacts: contact_id = item['Id'] for asset_id in item['Asset_Ids'].split(','): schemas.append( customers.new_schema(asset_id, contact_id, s_timestamp)) customers.createBatch(schemas) if (customers.save_result_isValid): self.appendText( '%s :: Sucessfully saved the batch of objects to SalesForce.' % (ObjectTypeName.objectSignature(self))) else: print >> sys.stderr, '%s :: Un-Sucessfully saved the batch of objects to SalesForce.' % ( ObjectTypeName.objectSignature(self)) pass
def ssh_connect_transport(self): """ Make an SSH connection with the server to mimic certain functions only for win32 platform to facilitate development. """ # get host key, if we know one hostkeytype = None hostkey = None try: host_keys = paramiko.util.load_host_keys( os.path.expanduser('~/.ssh/known_hosts')) except IOError: try: host_keys = paramiko.util.load_host_keys( os.path.expanduser('~/ssh/known_hosts')) except IOError: exc_info = sys.exc_info() info_string = '\n'.join(traceback.format_exception(*exc_info)) print >> self.lastError, '\n%s :: Cannot open host keys file, Reason: %s' % ( ObjectTypeName.objectSignature(self), info_string) host_keys = {} if host_keys.has_key(self.hostname): hostkeytype = host_keys[self.hostname].keys()[0] hostkey = host_keys[self.hostname][hostkeytype] else: print "%s :: Host key not found. Please create key manually using: ssh %s" % ( misc.funcName(), self.hostname) # now, connect and use paramiko Transport to negotiate SSH2 across the connection try: t = paramiko.Transport((self.hostname, self.port)) t.connect(username=self.username, password=self.password, hostkey=hostkey) except Exception as e: exc_info = sys.exc_info() info_string = '\n'.join(traceback.format_exception(*exc_info)) print >> self.lastError, '\n%s :: Cannot open connection to %s:%s using username of "%s", Reason: %s' % ( ObjectTypeName.objectSignature(self), self.hostname, self.port, self.username, info_string) return None return t
def _connect_to(self, netloc, soc): toks = netloc.split(':') if (len(toks) == 2): toks[-1] = int(toks[-1]) host_port = tuple(toks) else: host_port = netloc, 80 print "\t%s :: connect to %s" % (ObjectTypeName.objectSignature(self),':'.join([str(t) for t in list(host_port)])) try: soc.connect(host_port) except socket.error, arg: try: msg = arg[1] except: msg = arg self.send_error(404, msg) return 0
def ProgressDialogMixin_init(self): self.__number = 0 self.__count = 0 self.__timer = None self.__isDialogClosed = False self.__onProcessingDone = None self.__callback_TimerHandler = None self.__onProcessingDone = None self.__gauge_panel = None self.__thread_Q = None self.__timer_end_condition = lambda :True self.__callback_progressDialog_updated = None print >>sys.stdout, ObjectTypeName.objectSignature(self)
def acceptContacts(self, asset, contacts): clist = self.__competitors_list if (misc.isList(contacts)): if (len(contacts) > 0): contacts = [ c for c in contacts if (c['Email'] is not None) and ( c['Email'].split('@')[-1] not in clist) ] for c in contacts: c['Asset_Name'] = asset['Name'] c['Asset_Id'] = asset['Id'] self.__contacts__ += contacts else: info_string = '%s accepts a list of contacts but not when it is of type "%s".' % ( ObjectTypeName.objectSignature(self), type(contacts)) print >> sys.stderr, info_string try: wx_PopUp_Dialog(parent=self.__child_frame, msg=info_string, title='WARNING', styles=wx.ICON_WARNING | wx.CANCEL) except: pass
class SSHConnection(Cooperative): def __init__(self, hostname='', port=22, username='', password='', isLazy=False): self.__lastError__ = StringIO() self.__hostname__ = hostname self.__port__ = port self.__username__ = username self.__password__ = password self.__transport__ = None self.__sftp__ = None self.__sep__ = '/' if (not isLazy): self.ssh_connect_client() pass def __repr__(self): c = ObjectTypeName__typeName(self) return '%s(hostname="%s",port=%s,username="******",password="******")' % ( c, self.hostname, self.port, self.username) def __str__(self): return '%s@%s:%s' % (self.username, self.hostname, self.port) def sep(): doc = "seperator for host file names" def fget(self): return self.__sep__ return locals() sep = property(**sep()) def transport(): doc = "transport" def fget(self): return self.__transport__ return locals() transport = property(**transport()) def sftp(): doc = "sftp" def fget(self): if (self.__sftp__ is None): self.ssh_connect_client() return self.__sftp__ return locals() sftp = property(**sftp()) def hostname(): doc = "hostname" def fget(self): return self.__hostname__ def fset(self, hostname): self.__hostname__ = hostname return locals() hostname = property(**hostname()) def port(): doc = "port" def fget(self): return self.__port__ def fset(self, port): self.__port__ = port return locals() port = property(**port()) def username(): doc = "username" def fget(self): return self.__username__ def fset(self, username): self.__username__ = username return locals() username = property(**username()) def password(): doc = "password" def fget(self): return self.__password__ def fset(self, password): self.__password__ = password return locals() password = property(**password()) def lastError(): doc = "lastError message - use getvalue() to retrieve the message" def fget(self): return self.__lastError__ return locals() lastError = property(**lastError()) def lastErrorMessage(): doc = "lastError message" def fget(self): return self.__lastError__.getvalue() return locals() lastErrorMessage = property(**lastErrorMessage()) def ssh_connect_transport(self): """ Make an SSH connection with the server to mimic certain functions only for win32 platform to facilitate development. """ # get host key, if we know one hostkeytype = None hostkey = None try: host_keys = paramiko.util.load_host_keys( os.path.expanduser('~/.ssh/known_hosts')) except IOError: try: host_keys = paramiko.util.load_host_keys( os.path.expanduser('~/ssh/known_hosts')) except IOError: exc_info = sys.exc_info() info_string = '\n'.join(traceback.format_exception(*exc_info)) print >> self.lastError, '\n%s :: Cannot open host keys file, Reason: %s' % ( ObjectTypeName.objectSignature(self), info_string) host_keys = {} if host_keys.has_key(self.hostname): hostkeytype = host_keys[self.hostname].keys()[0] hostkey = host_keys[self.hostname][hostkeytype] else: print "%s :: Host key not found. Please create key manually using: ssh %s" % ( misc.funcName(), self.hostname) # now, connect and use paramiko Transport to negotiate SSH2 across the connection try: t = paramiko.Transport((self.hostname, self.port)) t.connect(username=self.username, password=self.password, hostkey=hostkey) except Exception as e: exc_info = sys.exc_info() info_string = '\n'.join(traceback.format_exception(*exc_info)) print >> self.lastError, '\n%s :: Cannot open connection to %s:%s using username of "%s", Reason: %s' % ( ObjectTypeName.objectSignature(self), self.hostname, self.port, self.username, info_string) return None return t def ssh_connect_client(self): """ connect and use paramiko Transport to negotiate SSH2 across the connection """ self.__sftp__ = None if (self.transport is None): self.__transport__ = self.ssh_connect_transport() if (self.__transport__ is not None): try: self.__sftp__ = paramiko.SFTPClient.from_transport( self.transport) except Exception as e: exc_info = sys.exc_info() info_string = '\n'.join(traceback.format_exception(*exc_info)) print >> self.lastError, '\n%s :: Cannot open client connection to %s:%s using username of "%s", Reason: %s' % ( ObjectTypeName.objectSignature(self), self.hostname, self.port, self.username, info_string) try: if (self.sftp is not None): ssh_close() except: pass return None else: return None return self.sftp def ssh_close(self): """Close the previously opened client connection as obtained by ssh_connect_client()""" try: self.sftp.close() self.__sftp__ = None except: pass try: self.transport.close() self.__transport__ = None except: pass return def __ssh_listdir(self, dir_path): """ Perform directoy list against an SSH accessible server. """ if ((dir_path == None) or (dir_path == '')): dir_path = '.' try: dirlist = self.sftp.listdir(dir_path) except: return None return dirlist def listdir(self, pathName): """ Return a list containing the names of the entries in the given path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the folder. This method is meant to mirror os.listdir as closely as possible. For a list of full SFTPAttributes objects, see listdir_attr. Parameters: * path (str) - path to list (defaults to '.') Returns: list of str list of filenames """ return self.__ssh_listdir(pathName) def normalize(self, dir_path): """ Return the normalized path (on the server) of a given path. This can be used to quickly resolve symbolic links or determine what the server is considering to be the "current folder" (by passing '.' as path). Parameters: * path (str) - path to be normalized Returns: str normalized form of the given path Raises: * IOError - if the path can't be resolved on the server """ if ((dir_path == None) or (dir_path == '')): dir_path = '.' try: return self.sftp.normalize(dir_path) except: return None return None def chdir(self, dir_path): """ Change the "current directory" of this SFTP session. Since SFTP doesn't really have the concept of a current working directory, this is emulated by paramiko. Once you use this method to set a working directory, all operations on this SFTPClient object will be relative to that path. Parameters: * path (str) - new current working directory Raises: * IOError - if the requested path doesn't exist on the server """ try: return self.sftp.chdir(dir_path) except: return None return None def chmod(self, path, mode): """ Change the mode (permissions) of a file. The permissions are unix-style and identical to those used by python's os.chmod function. Parameters: * path (str) - path of the file to change the permissions of * mode (int) - new permissions """ try: return self.sftp.chmod(path, mode) except: return None return None def chown(self, path, uid, gid): """ Change the owner (uid) and group (gid) of a file. As with python's os.chown function, you must pass both arguments, so if you only want to change one, use stat first to retrieve the current owner and group. Parameters: * path (str) - path of the file to change the owner and group of * uid (int) - new owner's uid * gid (int) - new group id """ try: return self.sftp.chown(path, uid, gid) except: return None return None def close(self): """ Close the SFTP session and its underlying channel. """ return self.ssh_close() def get_channel(self): """ Return the underlying Channel object for this SFTP session. This might be useful for doing things like setting a timeout on the channel. Returns: Channel the SSH channel """ try: return self.sftp.get_channel() except: return None return None def getcwd(self): """ Return the "current working directory" for this SFTP session, as emulated by paramiko. If no directory has been set with chdir, this method will return None. Returns: str the current working directory on the server, or None """ try: f = self.sftp.getcwd() return f if (f is not None) else self.home() except: return None return None def listdir_attr(self, dir_path): """ Return a list containing SFTPAttributes objects corresponding to files in the given path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the folder. The returned SFTPAttributes objects will each have an additional field: longname, which may contain a formatted string of the file's attributes, in unix format. The content of this string will probably depend on the SFTP server implementation. Parameters: * path (str) - path to list (defaults to '.') Returns: list of SFTPAttributes list of attributes """ try: return self.sftp.listdir_attr(dir_path) except: return None return None def stat(self, dir_path): """ Retrieve information about a file on the remote system. The return value is an object whose attributes correspond to the attributes of python's stat structure as returned by os.stat, except that it contains fewer fields. An SFTP server may return as much or as little info as it wants, so the results may vary from server to server. Unlike a python stat object, the result may not be accessed as a tuple. This is mostly due to the author's slack factor. The fields supported are: st_mode, st_size, st_uid, st_gid, st_atime, and st_mtime. Parameters: * path (str) - the filename to stat Returns: SFTPAttributes an object containing attributes about the given file """ try: return self.sftp.stat(dir_path) except: return None return None def lstat(self, dir_path): """ Retrieve information about a file on the remote system, without following symbolic links (shortcuts). This otherwise behaves exactly the same as stat. Parameters: * path (str) - the filename to stat Returns: SFTPAttributes an object containing attributes about the given file """ try: return self.sftp.lstat(dir_path) except: return None return None def mkdir(self, dir_path, file_mode=0777): """ Create a folder (directory) named path with numeric mode mode. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. Parameters: * path (str) - name of the folder to create * mode (int) - permissions (posix-style) for the newly-created folder """ try: return self.sftp.mkdir(dir_path, file_mode) except: return None return None def file(self, filename, mode='r', bufsize=-1): """ Open a file on the remote server. The arguments are the same as for python's built-in file (aka open). A file-like object is returned, which closely mimics the behavior of a normal python file object. The mode indicates how the file is to be opened: 'r' for reading, 'w' for writing (truncating an existing file), 'a' for appending, 'r+' for reading/writing, 'w+' for reading/writing (truncating an existing file), 'a+' for reading/appending. The python 'b' flag is ignored, since SSH treats all files as binary. The 'U' flag is supported in a compatible way. Since 1.5.2, an 'x' flag indicates that the operation should only succeed if the file was created and did not previously exist. This has no direct mapping to python's file flags, but is commonly known as the O_EXCL flag in posix. The file will be buffered in standard python style by default, but can be altered with the bufsize parameter. 0 turns off buffering, 1 uses line buffering, and any number greater than 1 (>1) uses that specific buffer size. Parameters: * filename (str) - name of the file to open * mode (str) - mode (python-style) to open in * bufsize (int) - desired buffering (-1 = default buffer size) Returns: SFTPFile a file object representing the open file Raises: * IOError - if the file could not be opened. """ try: return self.sftp.file(filename, mode, bufsize) except: return None return None def open(self, filename, mode='r', bufsize=-1): """ Open a file on the remote server. The arguments are the same as for python's built-in file (aka open). A file-like object is returned, which closely mimics the behavior of a normal python file object. The mode indicates how the file is to be opened: 'r' for reading, 'w' for writing (truncating an existing file), 'a' for appending, 'r+' for reading/writing, 'w+' for reading/writing (truncating an existing file), 'a+' for reading/appending. The python 'b' flag is ignored, since SSH treats all files as binary. The 'U' flag is supported in a compatible way. Since 1.5.2, an 'x' flag indicates that the operation should only succeed if the file was created and did not previously exist. This has no direct mapping to python's file flags, but is commonly known as the O_EXCL flag in posix. The file will be buffered in standard python style by default, but can be altered with the bufsize parameter. 0 turns off buffering, 1 uses line buffering, and any number greater than 1 (>1) uses that specific buffer size. Parameters: * filename (str) - name of the file to open * mode (str) - mode (python-style) to open in * bufsize (int) - desired buffering (-1 = default buffer size) Returns: SFTPFile a file object representing the open file Raises: * IOError - if the file could not be opened. """ try: return self.sftp.open(filename, mode, bufsize) except: return None return None def get(self, remotepath, localpath, callback=None): """ Copy a remote file (remotepath) from the SFTP server to the local host as localpath. Any exception raised by operations will be passed through. This method is primarily provided as a convenience. Parameters: * remotepath (str) - the remote file to copy * localpath (str) - the destination path on the local host * callback (function(int, int)) - optional callback function that accepts the bytes transferred so far and the total bytes to be transferred (since 1.7.4) """ try: return self.sftp.get(remotepath, localpath, callback=None) except: return None return None def put(self, localpath, remotepath, callback=None): """ Copy a local file (localpath) to the SFTP server as remotepath. Any exception raised by operations will be passed through. This method is primarily provided as a convenience. The SFTP operations use pipelining for speed. Parameters: * localpath (str) - the local file to copy * remotepath (str) - the destination path on the SFTP server * callback (function(int iBytes, int iTotal, float xfer_rate, int elapsed_secs)) - optional callback function that accepts the bytes transferred so far and the total bytes to be transferred (since 1.7.4) Returns: SFTPAttributes an object containing attributes about the given file (since 1.7.4) """ import time from vyperlogix.misc import _utils _begin_ts = time.time() _callback_ = None def _callback(iBytes, iTotal): if (callable(_callback_)): et = time.time() - _begin_ts if (et == 0): et = 0.00000000001 xfer = iBytes / et try: _callback_(iBytes, iTotal, xfer, et) except Exception as details: print >> sys.stderr, '%s' % str(details) try: if (not callable(callback)): callback = None else: try: callback(0, 0, 0, 0) except Exception as details: print >> sys.stderr, '%s :: Cannot use the callback because it has the following problem: "%s".' % ( ObjectTypeName.objectSignature(self), str(details)) callback = None if (callback is not None): _callback_ = callback result = self.sftp.put(localpath, remotepath, _callback) _end_ts = time.time() _iTotal = result.st_size xfer = _iTotal / (_end_ts - _begin_ts) return [result, xfer] except: exc_info = sys.exc_info() info_string = _utils.asMessage('\n'.join( traceback.format_exception(*exc_info))) print >> sys.stderr, info_string return None return None def readlink(self, dir_path): """ Return the target of a symbolic link (shortcut). You can use symlink to create these. The result may be either an absolute or relative pathname. Parameters: * path (str) - path of the symbolic link file Returns: str target path """ try: return self.sftp.readlink(dir_path) except: return None return None def remove(self, dir_path): """ Remove the file at the given path. This only works on files; for removing folders (directories), use rmdir. Parameters: * path (str) - path (absolute or relative) of the file to remove Raises: * IOError - if the path refers to a folder (directory) """ try: return self.sftp.remove(dir_path) except: return None return None def rename(self, old_name, new_name): """ Rename a file or folder from oldpath to newpath. Parameters: * oldpath (str) - existing name of the file or folder * newpath (str) - new name for the file or folder Raises: * IOError - if newpath is a folder, or something else goes wrong """ try: return self.sftp.rename(old_name, new_name) except: return None return None def rmdir(self, fname): """ Remove the folder named path. Parameters: * path (str) - name of the folder to remove """ try: return self.sftp.rmdir(fname) except: return None return None def symlink(self, source, dest): """ Create a symbolic link (shortcut) of the source path at destination. Parameters: * source (str) - path of the original file * dest (str) - path of the newly created symlink """ try: return self.sftp.symlink(source, dest) except: return None return None def truncate(self, fname, fsize): """ Change the size of the file specified by path. This usually extends or shrinks the size of the file, just like the truncate() method on python file objects. Parameters: * path (str) - path of the file to modify * size (int or long) - the new size of the file """ try: return self.sftp.truncate(fname, fsize) except: return None return None def unlink(self, fname): """ Remove the file at the given path. This only works on files; for removing folders (directories), use rmdir. Parameters: * path (str) - path (absolute or relative) of the file to remove Raises: * IOError - if the path refers to a folder (directory) """ try: return self.sftp.unlink(fname) except: return None return None def utime(self, fname, times): """ Set the access and modified times of the file specified by path. If times is None, then the file's access and modified times are set to the current time. Otherwise, times must be a 2-tuple of numbers, of the form (atime, mtime), which is used to set the access and modified times, respectively. This bizarre API is mimicked from python for the sake of consistency -- I apologize. Parameters: * path (str) - path of the file to modify * times (tuple(int)) - None or a tuple of (access time, modified time) in standard internet epoch time (seconds since 01 January 1970 GMT) """ try: return self.sftp.utime(fname, times) except: return None return None def home(self): """ home folder for current user """ f = self.normalize(self.getcwd()) toks = f.split(self.sep) t = toks[0:3] return self.sep.join(t) def isdir(self, pathName): """Test whether a remote path is a directory""" import stat try: st = self.stat(pathName) return False if (st == None) else stat.S_ISDIR(st.st_mode) except os.error: return False return False def islink(self, pathName): """Test whether a path is a symbolic link""" import stat try: st = self.lstat(pathName) except (os.error, AttributeError): return False return stat.S_ISLNK(st.st_mode) def walk(self, top, topdown=True, onerror=None): """Remote Directory tree generator. For each directory in the remote directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists are just names, with no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name). If optional arg 'topdown' is true or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top down). If topdown is false, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom up). When topdown is true, the caller can modify the dirnames list in-place (e.g., via del or slice assignment), and walk will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, or to impose a specific order of visiting. Modifying dirnames when topdown is false is ineffective, since the directories in dirnames have already been generated by the time dirnames itself is generated. By default errors from the os.listdir() call are ignored. If optional arg 'onerror' is specified, it should be a function; it will be called with one argument, an os.error instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the filename attribute of the exception object. Caution: if you pass a relative pathname for top, don't change the current working directory between resumptions of walk. walk never changes the current directory, and assumes that the client doesn't either. Example: from os.path import join, getsize for root, dirs, files in walk('python/Lib/email'): print root, "consumes", print sum([getsize(join(root, name)) for name in files]), print "bytes in", len(files), "non-directory files" if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories """ from posixpath import join # We may not have read permission for top, in which case we can't # get a list of the files the directory contains. os.path.walk # always suppressed the exception then, rather than blow up for a # minor reason when (say) a thousand readable directories are still # left to visit. That logic is copied here. try: # Note that listdir and error are globals in this module due # to earlier import-*. names = self.listdir(top) except error, err: if (callable(onerror)): try: onerror(err) except: pass return dirs, nondirs = [], [] if (names is not None): for name in names: if self.isdir(join(top, name)): dirs.append(name) else: nondirs.append(name) if topdown: yield top, dirs, nondirs for name in dirs: path = join(top, name) if not self.islink(path): for x in self.walk(path, topdown, onerror): yield x if not topdown: yield top, dirs, nondirs else: print '(%s) :: Possibly no files found at "%s".' % ( ObjectTypeName.objectSignature(self), top)
def processMessage(self, msg): sender, value = msg print "%s :: %s" % (ObjectTypeName.objectSignature(self), self.msg % { 'sender': sender, 'value': value })
def processMessage(self, val): print "%s :: Setting input to %s" % ( ObjectTypeName.objectSignature(self), val) self.state = val
def write_as_csv(self,fname,list_of_records=[],ordering=[]): """ Writes a list of records (dict objects) to a .CSV filename. """ info_string = '' info_strings = [] if (misc.isList(list_of_records)): if (len(list_of_records) > 0): if (all([lists.isDict(r) for r in list_of_records])): header = list_of_records[0].keys() if (misc.isList(ordering)) and (len(ordering) > 0): header = ordering s_header = ','.join(header) fOut = open(fname,'w') try: print >>fOut, s_header for rec in list_of_records: l_values = [] for h in header: l_values.append(str(rec[h]) if (str(rec[h]).find(',') == -1) else '"%s"' % (rec[h])) print >>fOut, ','.join(l_values) except Exception as details: info_string = _utils.formattedException(details=details) info_strings.append(info_string) finally: fOut.flush() fOut.close() else: info_string = '%s :: Expected list_of_records to contains dictionary objects however some do not.' % (ObjectTypeName.objectSignature(self)) info_strings.append(info_string) else: info_string = '%s :: Expected list_of_records to contains dictionary objects however list is empty.' % (ObjectTypeName.objectSignature(self)) info_strings.append(info_string) else: info_string = '%s :: Expected list_of_records to be of type list rather than type "%s".' % (ObjectTypeName.objectSignature(self),type(list_of_records)) info_strings.append(info_string) return '\n'.join(info_strings)
def MessageQ_Mixin_init(self): self.__message_Q__ = Queue.Queue(100) self.isMessageQ_Empty = lambda :self.isMessageQEmpty print >>sys.stdout, ObjectTypeName.objectSignature(self)
def fset(self, count): self.__count = count print >> self.log, '%s :: count is "%d".' % (ObjectTypeName.objectSignature(self),count)
def fset(self, number): self.__number = number print >> self.log, '%s :: number is "%d".' % (ObjectTypeName.objectSignature(self),number)
def onHide(self): while (self.IsShown()): while (self.IsShown()): self.Hide() print '%s.1' % (ObjectTypeName.objectSignature(self)) wx.MilliSleep(1000)
def getProcessPriorityByPID(self,pid): pClass = -1 if (isinstance(pClass,int)): pHand = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, 0, pid) if (pHand): pClass = win32process.GetPriorityClass(pHand) else: print >>sys.stderr, '(%s).WARNING :: Unable to get the process handler for PID of "%s".' % (ObjectTypeName.objectSignature(self),pid) win32api.CloseHandle(pHand) else: print >>sys.stderr, '(%s).ERROR :: Unable to determine how to handle the pClass of "%s" which is of type "%s".' % (ObjectTypeName.objectSignature(self),pClass,type(pClass)) return pClass