コード例 #1
0
 def _add_vcards_file (repo, response, tfile):
     try:
         fp = response.open("text/plain")
         conf = configurator.default_configurator()
         update_configuration(conf)
         tal = ensure_assembly_line(conf.get("assembly-line"))
         cards = []
         try:
             parsed = vCards.myformat(tfile)
             parsed['upload'] = False
             parsed['usepng'] = True
             for card in parsed.get('parsed-cards'):
                 # see if there's already a card for this name
                 query = 'apparent-mime-type:"%s" AND vcard-name:"%s"' % (
                     vCard.format_mimetype, card.fn.value)
                 hits = repo.do_query(query)
                 if hits:
                     if 'metadata' not in parsed:
                         parsed['metadata'] = {}
                     parsed['metadata']['version-of'] = hits[0][1].id
                 p = vCard(card, parsed)
                 # calculate fingerprint
                 fd, filename = tempfile.mkstemp()
                 fp = os.fdopen(fd, "wb")
                 p.write_to_file(fp)
                 fp.close()
                 fingerprint = calculate_originals_fingerprint(filename)
                 # look up fingerprint in repo to see if we already have it
                 hits = repo.do_query('sha-hash:%s' % fingerprint)
                 if hits:
                     # already there, so skip this one
                     note(3, "skipping '%s', already in repo...", card.fn.value)
                     continue
                 # new card, so add it
                 pinst = p.process()
                 if isinstance(pinst, DocumentParser):
                     try:
                         folder = repo.create_document_folder(repo.pending_folder())
                         id = os.path.basename(folder)
                         note("using id %s for %s...", id, card.fn.value)
                         # add the tfolder to the repository
                         process_folder(repo, id, pinst.folder, True)
                         flesh_out_folder(id, None, None, repo, None, None)
                         note("added card for %s\n" % card.fn.value)
                         cards.append((id, card.fn.value))
                     except:
                         msg = "Exception processing vCard; vCard is\n%s\nException was\n%s\n" % (
                             card, ''.join(traceback.format_exception(*sys.exc_info())))
                         note(0, msg)
         finally:
             if tal:
                 from uplib.addDocument import AssemblyLine
                 shutil.rmtree(AssemblyLine)
             if os.path.exists(tfile):
                 os.unlink(tfile)
     except:
         msg = "Exception processing vcards:\n%s\n" % ''.join(traceback.format_exception(*sys.exc_info()))
         note(0, msg)
         response.error(HTTPCodes.INTERNAL_SERVER_ERROR, msg)
     else:
         response.reply('\n'.join(['%20s:  %s' % (x[0], x[1]) for x in cards]))
コード例 #2
0
 def _add_icalendar_file (repo, response, tfile):
     try:
         conf = configurator.default_configurator()
         update_configuration(conf)
         tal = ensure_assembly_line(conf.get("assembly-line"))
         try:
             parsed = iCalendar.myformat(tfile)
             if not isinstance(parsed, dict):
                 note(0, "Can't parse supposed iCalendar file %s", tfile)
                 response.error(HTTPCodes.INTERNAL_SERVER_ERROR, "Can't parse file")
                 return
             resp = response.open("text/plain")
             for event, name, uid in parsed.get('parsed-events'):
                 if hasattr(event, "dtstart"):
                     identifier = "%s @ %s" % (name, event.dtstart.value)
                 else:
                     identifier = name
                 # see if there's already a event for this name
                 query = 'apparent-mime-type:"%s" AND event-uid:"%s"' % (
                     iCalendarEventParser.format_mimetype, uid)
                 hits = repo.do_query(query)
                 if hits:
                     if 'metadata' not in parsed:
                         parsed['metadata'] = {}
                     parsed['metadata']['version-of'] = hits[0][1].id
                 if event.name == "VEVENT":
                     p = iCalendarEventParser(name,
                                              {"icsname": name,
                                               "icsuid": uid,
                                               "icsevent": event,
                                               "upload": False,
                                               "usepng": True,
                                               "metadata": parsed.get("metadata") or {},
                                               })
                 else:
                     note(3, "No supported iCalendar subtype found in %s", identifier)
                     p = None
                 if p:
                     # calculate fingerprint
                     fd, filename = tempfile.mkstemp(".ics")
                     fp = os.fdopen(fd, "wb")
                     p.write_to_file(fp)
                     fp.close()
                     fingerprint = calculate_originals_fingerprint(filename)
                     # look up fingerprint in repo to see if we already have it
                     hits = repo.do_query('sha-hash:%s' % fingerprint)
                     if hits:
                         # already there, so skip this one
                         note(3, "skipping '%s', already in repo...", identifier)
                         resp.write("skipping '%s', already in repo\n" % identifier)
                         continue
                     # new event, so add it
                     p.metadata["sha-hash"] = fingerprint
                     pinst = p.process()
                     if isinstance(pinst, DocumentParser):
                         try:
                             folder = repo.create_document_folder(repo.pending_folder())
                             id = os.path.basename(folder)
                             # add the tfolder to the repository
                             process_folder(repo, id, pinst.folder, True)
                             flesh_out_folder(id, None, None, repo, None, None)
                             resp.write("added event for %s\n" % identifier)
                         except:
                             msg = "Exception processing event; event is\n%s\nException was\n%s\n" % (
                                 event, ''.join(traceback.format_exception(*sys.exc_info())))
                             note(0, msg)
                             resp.write(msg)
         finally:
             if tal:
                 from uplib.addDocument import AssemblyLine
                 shutil.rmtree(AssemblyLine)
             if os.path.exists(tfile):
                 os.unlink(tfile)
     except:
         msg = "Exception processing iCalendar:\n%s\n" % ''.join(traceback.format_exception(*sys.exc_info()))
         note(0, msg)
         response.error(HTTPCodes.INTERNAL_SERVER_ERROR, msg)