+# set to a filename for using cached data when debugging
+# WARNING: does not actually connect to the peer in this case
+use_cache=None
+# for debugging specific entries - display detailed info on selected objs
+focus_type=None # set to e.g. 'Person'
+focus_ids=[] # set to a list of ids (e.g. person_ids) - remote or local ids should work
+#### example
+#use_cache="/var/log/peers/getpeerdata.pickle"
+#verbose=True
+#focus_type='Person'
+#focus_ids=[621,1088]
+
+#################### helpers
+def message (to_print=None,verbose_only=False):
+ if verbose_only and not verbose:
+ return
+ print >> log, time.strftime("%m-%d-%H-%M-%S:"),
+ if to_print:
+ print >>log, to_print
+
+def message_verbose(to_print=None, header='VERBOSE'):
+ message("%s> %r"%(header,to_print),verbose_only=True)
+
+
+#################### to avoid several instances running at the same time
+class FileLock:
+ """
+ Lock/Unlock file
+ """
+ def __init__(self, file_path, expire = 60 * 60 * 2):
+ self.expire = expire
+ self.fpath = file_path
+ self.fd = None
+
+ def lock(self):
+ if os.path.exists(self.fpath):
+ if (time.time() - os.stat(self.fpath).st_ctime) > self.expire:
+ try:
+ os.unlink(self.fpath)
+ except Exception, e:
+ message('FileLock.lock(%s) : %s' % (self.fpath, e))
+ return False
+ try:
+ self.fd = open(self.fpath, 'w')
+ fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+ except IOError, e:
+ message('FileLock.lock(%s) : %s' % (self.fpath, e))
+ return False
+ return True
+
+ def unlock(self):
+ try:
+ fcntl.flock(self.fd, fcntl.LOCK_UN | fcntl.LOCK_NB)
+ self.fd.close()
+ except IOError, e:
+ message('FileLock.unlock(%s) : %s' % (self.fpath, e))
+