Tomb/extras/pytomb/tomblib/undertaker.py
Jaromil 3eb93acc18 Directory reorganization
This commit re-organizes all the source distribution contents to
present users with the simple script, while moving the rest in extras.
Also autoconf/automake scripts were removed, back to minimalism.

The rationale of this change is that Tomb really only consists of a
script and users with no extra needs should just be presented with
it with no need for anything else. Any other thing on top of the Tomb
script is an extra and can be even distributed separately or integrated
in distributions.
2013-05-18 17:29:37 +02:00

80 lines
2.2 KiB
Python

import subprocess
from tempfile import NamedTemporaryFile
import parser
class Undertaker(object):
'''
This is similar to Tomb class, and provides a wrapper on undertaker.
TODO:
* methods for automagical scan
* output parsing, giving meaningful output
Due to the non-interactive nature of undertaker, it's simpler than Tomb
'''
undertakerexec = 'undertaker'
@classmethod
def check(cls, paths):
'''Will check if there are keys available there, as in --path
paths can be a string (one address), or a list of
'''
#TODO: more solid check: something like
if type(paths) is not str:
out = []
for p in paths:
try:
res = cls.check(p)
except:
continue
else:
if res:
out.extend(res)
return out
buf = NamedTemporaryFile()
try:
subprocess.check_call([cls.undertakerexec, paths, '--batch',
'--path'], stderr=buf)
except subprocess.CalledProcessError as exc:
return False
out = []
buf.seek(0)
for line in buf:
ret = parser.parse_line(line)
if ret and ret['type'] == 'found':
out.append(ret['content'])
return out
@classmethod
def get(cls, paths):
'''
Similar to check, but truly get the key content.
If paths is iterable, stop at the first successful path
'''
if type(paths) is not str:
for p in paths:
try:
res = cls.get(p)
except:
continue
else:
if res:
return res
buf = NamedTemporaryFile()
try:
subprocess.check_call([cls.undertakerexec, paths, '--batch'],
stdout=buf)
except subprocess.CalledProcessError:
return False
buf.seek(0)
return buf.read()
if __name__ == '__main__':
Undertaker.undertakerexec = '/home/davide/coding/projects/tomb/src/undertaker'
print Undertaker.get('near:///home/davide/Desktop/testing.tomb')