def __init__(self, graph, topDir): """ sparqlhttp.graph2.SyncGraph object """ assert not topDir.endswith('/') self.graph, self.topDir = graph, topDir self.rootUri = URIRef(SITE.rstrip('/'))
def ingest(fileObj, mock=False, newImageCb=lambda uri: None): #f = open("/my/mail/drewp/cur/1283804816.32729_0.bang:2,S") msg = maillib.Message.from_file(fileObj) attachments = findAttachments(msg) if not attachments: log.info("no attachments with image extensions") return uri = URIRef("mid:" + urllib.quote(msg.headers['Message-ID'].strip('<>'))) msgDate = msg.date.date().isoformat() now = Literal(datetime.datetime.now(tzlocal())) sesameImport = restkit.Resource("http://bang:9042/", timeout=5) stmts = emailStatements(uri, msg) errs = [] for filename, content in attachments: outDir = "/my/pic/email/%s" % msgDate if not os.path.isdir(outDir): os.mkdir(outDir) for outPath in filenameAttempts(filename, outDir): img = uriOfFilename(rootUri=URIRef(SITE.rstrip('/')), root='/my/pic', filename=outPath) if os.path.exists(outPath) and open(outPath).read() == content: log.info("already wrote this to %s" % outPath) else: try: if not mock: writeExcl(outPath, content) # jhead -autorot might be good to run on the result except OSError, e: log.error(e) continue # next suffix attempt stmts.extend([ (uri, DCTERMS['hasPart'], img), (img, PHO['filenameInEmail'], Literal(filename)), (img, PHO['emailReadTime'], now), ]) log.info(" described new image: %s", img) try: newImageCb(img) except Exception, e: log.error("newImageCb failed on %r: %s" % (img, e)) if not mock: log.debug(" post to sesameImport") sesameImport.post(file=outPath) break