[a37b2b]: plugins / single_instance.py Maximize Restore History

Download this file

single_instance.py    105 lines (87 with data), 2.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
'''
This software is licensed under the GPL (GNU General Public License) version 2
as it appears here: http://www.gnu.org/copyleft/gpl.html
It is also included with this archive as `gpl.txt <gpl.txt>`_.
'''
import socket
import asyncore
import traceback
import sys
configuration = _pype
callback = None
port = 9999
def partition(string, sep):
offset = string.find(sep)
if offset == -1:
return string, '', ''
return string[:offset], sep, string[offset+len(sep):]
class PathReader(asyncore.dispatcher):
def __init__(self, conn):
asyncore.dispatcher.__init__(self, conn)
self.incoming = ''
## self.handle_error = self.handle_close
## def handle_close(self):
## self.close()
def handle_read(self):
data = self.recv(4096)
if not data:
self.handle_close()
return
self.incoming += data
fnames = []
fname, sep, self.incoming = partition(self.incoming, '\n')
while sep:
if fname:
fnames.append(fname)
fname, sep, self.incoming = partition(self.incoming, '\n')
self.incoming = fname
if fnames:
callback(fnames)
class Listener(asyncore.dispatcher):
def __init__(self):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(('127.0.0.1', port))
self.listen(5)
def handle_accept(self):
try:
conn, addr = self.accept()
except socket.error:
return
except TypeError:
return
PathReader(conn)
def send_documents(docs):
if configuration.nosocket:
return 0
try:
startup(0)
except socket.error, why:
if why[0] != 10048:
traceback.print_exc()
return 0
else:
shutdown(0)
return 0
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', port))
docs.append('')
s.sendall('\n'.join(docs))
s.close()
except socket.error:
return 0
else:
print "sent documents to already running PyPE at port %i"%(port,)
return 1
def poll():
asyncore.poll()
def startup(p=1):
if p:
print "Starting up document listener!"
Listener()
def shutdown(p=1):
if p:
print "Shutting down document listener!"
for i in asyncore.socket_map.values():
i.close()