本文整理汇总了Python中mailpile.crypto.streamer.EncryptingStreamer类的典型用法代码示例。如果您正苦于以下问题:Python EncryptingStreamer类的具体用法?Python EncryptingStreamer怎么用?Python EncryptingStreamer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EncryptingStreamer类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_pickle
def save_pickle(self, obj, pfn):
if self.prefs.obfuscate_index:
from mailpile.crypto.streamer import EncryptingStreamer
fd = EncryptingStreamer(self.prefs.obfuscate_index, dir=self.workdir)
cPickle.dump(obj, fd, protocol=0)
fd.save(os.path.join(self.workdir, pfn))
else:
fd = open(os.path.join(self.workdir, pfn), "wb")
cPickle.dump(obj, fd, protocol=0)
fd.close()
示例2: _open_log
def _open_log(self):
if self._log_fd:
self._log_fd.close()
if not os.path.exists(self.logdir):
os.mkdir(self.logdir)
self._log_start_id = NewEventId()
enc_key = self.encryption_key_func()
if enc_key:
self._log_fd = EncryptingStreamer(enc_key,
dir=self.logdir,
name='EventLog/ES',
use_filter=False,
long_running=True)
self._log_fd.save(self._save_filename(), finish=False)
self._log_write = self._log_fd.write_pad_and_flush
else:
self._log_fd = open(self._save_filename(), 'wb', 0)
self._log_write = self._log_fd.write
# Write any incomplete events to the new file
for e in self.incomplete():
self._log_write('%s\n')
# We're starting over, incomplete events don't count
self._logged = 0
示例3: save
def save(self, filename=None, gpg_recipient=None, encryption_key=None):
filename = filename or self.filename
if filename:
gpg_recipient = gpg_recipient or self.gpg_recipient()
encryption_key = encryption_key or self.encryption_key()
if encryption_key:
from mailpile.crypto.streamer import EncryptingStreamer
es = EncryptingStreamer(encryption_key,
dir=os.path.dirname(filename))
es.write(self.as_vCard())
es.save(filename)
else:
fd = gpg_open(filename, gpg_recipient, 'wb')
fd.write(self.as_vCard())
fd.close()
return self
else:
raise ValueError('Save to what file?')
示例4: _open_log
def _open_log(self):
if self._log_fd:
self._log_fd.close()
if not os.path.exists(self.logdir):
os.mkdir(self.logdir)
self._log_start_id = NewEventId()
enc_key = self.encryption_key_func()
if enc_key:
self._log_fd = EncryptingStreamer(enc_key, dir=self.logdir)
self._log_fd.save(self._save_filename(), finish=False)
else:
self._log_fd = open(self._save_filename(), 'w', 0)
# Write any incomplete events to the new file
for e in self.incomplete():
self._log_fd.write('%s\n' % e)
# We're starting over, incomplete events don't count
self._logged = 0
示例5: EventLog
class EventLog(object):
"""
This is the Mailpile Event Log.
The log is written encrypted to disk on an ongoing basis (rotated
every N lines), but entries are kept in RAM as well. The event log
allows for recording of incomplete events, to help different parts
of the app "remember" tasks which have yet to complete or may need
to be retried.
"""
def __init__(self, logdir, encryption_key_func, rollover=10240):
self.logdir = logdir
self.encryption_key_func = encryption_key_func
self.rollover = rollover
self.events = {}
# Internals...
self._lock = threading.Lock()
self._log_fd = None
def _save_filename(self):
return os.path.join(self.logdir, self._log_start_id)
def _open_log(self):
if self._log_fd:
self._log_fd.close()
if not os.path.exists(self.logdir):
os.mkdir(self.logdir)
self._log_start_id = NewEventId()
enc_key = self.encryption_key_func()
if enc_key:
self._log_fd = EncryptingStreamer(enc_key, dir=self.logdir)
self._log_fd.save(self._save_filename(), finish=False)
else:
self._log_fd = open(self._save_filename(), 'w', 0)
# Write any incomplete events to the new file
for e in self.incomplete():
self._log_fd.write('%s\n' % e)
# We're starting over, incomplete events don't count
self._logged = 0
def _maybe_rotate_log(self):
if self._logged > self.rollover:
self._log_fd.close()
kept_events = {}
for e in self.incomplete():
kept_events[e.event_id] = e
self.events = kept_events
self.open_log()
def _match(self, event, filters):
return True
def incomplete(self, **filters):
"""Return all the incomplete events, in order."""
for ek in sorted(self.events.keys()):
e = self.events.get(ek, None)
if (e is not None and
e.state != Event.COMPLETE and
self._match(e, filters)):
yield e
def since(self, ts, **filters):
"""Return all events since a given time, in order."""
for ek in sorted(self.events.keys()):
e = self.events.get(ek, None)
if (e is not None and
e.ts >= ts and
self._match(e, filters)):
yield e
def log_event(self, event):
"""Log an Event object."""
self._lock.acquire()
try:
if not self._log_fd:
self._open_log()
self._log_fd.write('%s\n' % event)
self.events[event.event_id] = event
self._logged += 1
self._maybe_rotate_log()
finally:
self._lock.release()
return event
def log(self, *args, **kwargs):
"""Log a new event."""
return self.log_event(Event(*args, **kwargs))
def close(self):
self._lock.acquire()
try:
self._log_fd.close()
self._log_fd = None
#.........这里部分代码省略.........
示例6: EventLog
class EventLog(object):
"""
This is the Mailpile Event Log.
The log is written encrypted to disk on an ongoing basis (rotated
every N lines), but entries are kept in RAM as well. The event log
allows for recording of incomplete events, to help different parts
of the app "remember" tasks which have yet to complete or may need
to be retried.
"""
KEEP_LOGS = 2
def __init__(self, logdir, encryption_key_func, rollover=10240):
self.logdir = logdir
self.encryption_key_func = encryption_key_func
self.rollover = rollover
self._events = {}
# Internals...
self._waiter = threading.Condition()
self._lock = threading.Lock()
self._log_fd = None
def _notify_waiters(self):
self._waiter.acquire()
self._waiter.notifyAll()
self._waiter.release()
def wait(self, timeout=None):
self._waiter.acquire()
self._waiter.wait(timeout)
self._waiter.release()
def _save_filename(self):
return os.path.join(self.logdir, self._log_start_id)
def _open_log(self):
if self._log_fd:
self._log_fd.close()
if not os.path.exists(self.logdir):
os.mkdir(self.logdir)
self._log_start_id = NewEventId()
enc_key = self.encryption_key_func()
if enc_key:
self._log_fd = EncryptingStreamer(enc_key, dir=self.logdir)
self._log_fd.save(self._save_filename(), finish=False)
else:
self._log_fd = open(self._save_filename(), 'w', 0)
# Write any incomplete events to the new file
for e in self.incomplete():
self._log_fd.write('%s\n' % e)
# We're starting over, incomplete events don't count
self._logged = 0
def _maybe_rotate_log(self):
if self._logged > self.rollover:
self._log_fd.close()
kept_events = {}
for e in self.incomplete():
kept_events[e.event_id] = e
self._events = kept_events
self._open_log()
self.purge_old_logfiles()
def _list_logfiles(self):
return sorted([l for l in os.listdir(self.logdir)
if not l.startswith('.')])
def _save_events(self, events):
if not self._log_fd:
self._open_log()
events.sort(key=lambda ev: ev.ts)
for event in events:
self._log_fd.write('%s\n' % event)
self._events[event.event_id] = event
def _load_logfile(self, lfn):
enc_key = self.encryption_key_func()
with open(os.path.join(self.logdir, lfn)) as fd:
if enc_key:
lines = fd.read()
else:
with DecryptingStreamer(enc_key, fd) as streamer:
lines = streamer.read()
if lines:
for line in lines.splitlines():
event = Event.Parse(line)
self._events[event.event_id] = event
def _match(self, event, filters):
for kw, rule in filters.iteritems():
if kw.endswith('!'):
truth, okw, kw = False, kw, kw[:-1]
else:
truth, okw = True, kw
#.........这里部分代码省略.........
示例7: EventLog
class EventLog(object):
"""
This is the Mailpile Event Log.
The log is written encrypted to disk on an ongoing basis (rotated
every N lines), but entries are kept in RAM as well. The event log
allows for recording of incomplete events, to help different parts
of the app "remember" tasks which have yet to complete or may need
to be retried.
"""
KEEP_LOGS = 2
def __init__(self, logdir, decryption_key_func, encryption_key_func,
rollover=1024):
self.logdir = logdir
self.decryption_key_func = decryption_key_func or (lambda: None)
self.encryption_key_func = encryption_key_func or (lambda: None)
self.rollover = rollover
self._events = {}
# Internals...
self._watching_uis = []
self._waiter = threading.Condition(EventRLock())
self._lock = EventLock()
self._log_fd = None
def _notify_waiters(self):
with self._waiter:
self._waiter.notifyAll()
def wait(self, timeout=None):
with self._waiter:
self._waiter.wait(timeout)
def _save_filename(self):
return os.path.join(self.logdir, self._log_start_id)
def _open_log(self):
if self._log_fd:
self._log_fd.close()
if not os.path.exists(self.logdir):
os.mkdir(self.logdir)
self._log_start_id = NewEventId()
enc_key = self.encryption_key_func()
if enc_key:
self._log_fd = EncryptingStreamer(enc_key,
dir=self.logdir,
name='EventLog/ES',
long_running=True)
self._log_fd.save(self._save_filename(), finish=False)
else:
self._log_fd = open(self._save_filename(), 'wb', 0)
# Write any incomplete events to the new file
for e in self.incomplete():
self._log_fd.write('%s\n' % e)
# We're starting over, incomplete events don't count
self._logged = 0
def _maybe_rotate_log(self):
if self._logged > self.rollover:
self._log_fd.close()
kept_events = {}
for e in self.incomplete():
kept_events[e.event_id] = e
self._events = kept_events
self._open_log()
self.purge_old_logfiles()
def _list_logfiles(self):
return sorted([l for l in os.listdir(self.logdir)
if not l.startswith('.')])
def _save_events(self, events, recursed=False):
if not self._log_fd:
self._open_log()
events.sort(key=lambda ev: ev.ts)
try:
for event in events:
self._log_fd.write('%s\n' % event)
self._events[event.event_id] = event
except IOError:
if recursed:
raise
else:
self._unlocked_close()
return self._save_events(events, recursed=True)
def _load_logfile(self, lfn):
enc_key = self.decryption_key_func()
with open(os.path.join(self.logdir, lfn)) as fd:
if enc_key:
with DecryptingStreamer(fd, mep_key=enc_key,
name='EventLog/DS') as streamer:
lines = streamer.read()
streamer.verify(_raise=IOError)
#.........这里部分代码省略.........
示例8: EventLog
class EventLog(object):
"""
This is the Mailpile Event Log.
The log is written encrypted to disk on an ongoing basis (rotated
every N lines), but entries are kept in RAM as well. The event log
allows for recording of incomplete events, to help different parts
of the app "remember" tasks which have yet to complete or may need
to be retried.
"""
KEEP_LOGS = 2
def __init__(self, logdir, encryption_key_func, rollover=10240):
self.logdir = logdir
self.encryption_key_func = encryption_key_func
self.rollover = rollover
self.events = {}
# Internals...
self._lock = threading.Lock()
self._log_fd = None
def _save_filename(self):
return os.path.join(self.logdir, self._log_start_id)
def _open_log(self):
if self._log_fd:
self._log_fd.close()
if not os.path.exists(self.logdir):
os.mkdir(self.logdir)
self._log_start_id = NewEventId()
enc_key = self.encryption_key_func()
if enc_key:
self._log_fd = EncryptingStreamer(enc_key, dir=self.logdir)
self._log_fd.save(self._save_filename(), finish=False)
else:
self._log_fd = open(self._save_filename(), 'w', 0)
# Write any incomplete events to the new file
for e in self.incomplete():
self._log_fd.write('%s\n' % e)
# We're starting over, incomplete events don't count
self._logged = 0
def _maybe_rotate_log(self):
if self._logged > self.rollover:
self._log_fd.close()
kept_events = {}
for e in self.incomplete():
kept_events[e.event_id] = e
self.events = kept_events
self._open_log()
self.purge_old_logfiles()
def _match(self, event, filters):
return True
def _list_logfiles(self):
return sorted([l for l in os.listdir(self.logdir)
if not l.startswith('.')])
def _save_events(self, events):
if not self._log_fd:
self._open_log()
events.sort(key=lambda ev: ev.ts)
for event in events:
self._log_fd.write('%s\n' % event)
self.events[event.event_id] = event
def _load_logfile(self, lfn):
enc_key = self.encryption_key_func()
with open(os.path.join(self.logdir, lfn)) as fd:
if enc_key:
lines = fd.read()
else:
with DecryptingStreamer(enc_key, fd) as streamer:
lines = fd.read()
if lines:
for line in lines.splitlines():
event = Event.Parse(line)
if Event.COMPLETE in event.flags:
if event.event_id in self.events:
del self.events[event.event_id]
else:
self.events[event.event_id] = event
self._save_events(self.events.values())
def incomplete(self, **filters):
"""Return all the incomplete events, in order."""
for ek in sorted(self.events.keys()):
e = self.events.get(ek, None)
if (e is not None and
Event.COMPLETE not in e.flags and
self._match(e, filters)):
yield e
#.........这里部分代码省略.........