def initFromData(self, data): """ Initialize a recurrence from ICalendar formatted String """ self._data = data try: v = vobject.readComponents(data).next() except: # some stupid Google Calendar recurrence entries do come without time zone information # this cause ParseError in vobject lib; therefore we do another attempt with manually attached # UTC information (stupid, but seems to work) v = vobject.readComponents(data + "\n" + UTC_STRING).next() self._allDay = False try: self._dtstart = vobject.icalendar.DateOrDateTimeBehavior.transformToNative(v.dtstart).value if type(self._dtstart) == datetime.date: self._allDay = True elif self._dtstart.tzinfo == None: self._dtstart = self._dtstart.replace(tzinfo = UTC()) except BaseException: self._dtstart = None try: self._dtend = vobject.icalendar.DateOrDateTimeBehavior.transformToNative(v.dtend).value if type(self._dtend) == datetime.datetime and self._dtend.tzinfo == None: self._dtend = self._dtend.replace(tzinfo = UTC()) except BaseException: self._dtend = None try: self._rrule = v.rrule except BaseException: self._rrule = None
def parse(self, input): """parse the given file or file source string""" if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' if hasattr(input, "read"): inisrc = input.read() input.close() input = inisrc if isinstance(input, str): input = StringIO(input) self._icalfile = vobject.readComponents(input).next() else: self._icalfile = vobject.readComponents(open(input)).next() for component in self._icalfile.components(): if component.name == "VEVENT": for property in component.getChildren(): if property.name in ('SUMMARY', 'DESCRIPTION', 'COMMENT', 'LOCATION'): newunit = self.addsourceunit(property.value) newunit.addnote("Start date: %s" % component.dtstart.value) newunit.addlocation( "[%s]%s" % (component.uid.value, property.name))
def main(): applelist = [] for x in vobject.readComponents(file("vCard-kort.vcf")): card = parsevCard(x) card.apple = True applelist.append(vCardWithMatches(card)) googlelist = [] for x in vobject.readComponents(file("contacts.vcf")): card = parsevCard(x) card.apple = False googlelist.append(vCardWithMatches(card)) merged = [] all = [] all.extend(applelist) all.extend(googlelist) for vcard in all: match = filter(lambda m: vcard.matches(m), merged) if not match: merged.append(vcard) else: match[0].merge(vcard) if len(match) > 1: if (len(match[0].name & match[1].name) >= 2 and len(match) <= 2): match[0].merge(match[1]) merged.remove(match[1]) else: raise Exception( "Length %(l)d > 1, first two elements are %(a)s and %(b)s," % { "l": len(match), "a": match[0], "b": match[1] }) import codecs f = codecs.open('result.txt', encoding='utf-16', mode='w+') try: for x in merged: if (len(x.name) > 0 and len(x.emails) > 0 and x.apple): f.write( unicode(u"%(name)s: %(emails)s\n" % { "name": list(x.name), "emails": list(x.emails) })) finally: f.close()
def read_file(file_vcard, test): vcards = [] if test: ext = file_vcard.split('.')[-1] if ext == 'vcard' or ext == 'vcf': with open('{}'.format(file_vcard), 'r') as fh: vcards_tmp = vobject.readComponents(fh) for v in vcards_tmp: vcards.append(v) else: vcards = vobject.readComponents(file_vcard) return vcards
def calendar_events(url, event_name, timerange): "Get events named event_name from the calendar file at url in the specified timerange" events = [] fd = urlopen(url) cal = vobject.readComponents(fd.read()) for component in cal: if component.name == "VCALENDAR": for event in component.components(): if event.name == 'VEVENT' and event.summary.value == event_name: try: description = event.description.value except AttributeError: description = None if event.rruleset: for date in event.rruleset.between( *(time.replace( tzinfo=event.dtstart.value.tzinfo) for time in timerange)): events.append( Event(date=date.replace(tzinfo=None), comment=description)) elif timerange[0] <= event.dtstart.value.replace( tzinfo=None) <= timerange[1]: events.append( Event( date=event.dtstart.value.replace(tzinfo=None), comment=description)) return events
def read_vcf(self): with open(self.path_vcf, 'r') as f: a = f.readlines() vcf_contacts = vobject.readComponents(''.join(a)) li = [] # TODO identifier label pour les numéros (31 numéro non pas un nbr de label équivalent contre 17) """ li = [] for i, client in enumerate(x.df): if 'tel' in client and 'x-ablabel' in client: if len(client['tel']) == len(client['x-ablabel']): li.append(True) else: li.append(False) """ for i, contact in enumerate(vcf_contacts): di = {} for key in contact.contents.keys(): di[key] = [] # drop photo du json if key == 'photo': continue for k, value in enumerate(contact.contents[key]): if type(value.__dict__['value']) in [list, str, bytes]: di[key].append(value.__dict__['value']) else: di[key].append(value.__dict__['value'].__dict__) li.append(di) self.list_contact = li
def readVcf(vcfFile): """Reads a vcf-file and returns a list of tuples of (FN, categories_list, telephonenumber_list)""" vobjs = vobject.readComponents(vcfFile) contacts = [] for v in vobjs: fn = '' try: fn = v.fn.value except: continue categories = [] try: categories = v.categories.value except: pass tel_list = [] try: for t in v.tel_list: tel_list.append( (t.value, t.params) ) except: continue contacts.append( (fn, categories, tel_list) ) return contacts
def get_books(self, filename, vipGroups, picture_path, debug=False): cards = [] with codecs.open(filename, "r", "utf-8") as infile: data = infile.read() for card in vobject.readComponents(data): cards.append(card) return self.get_books_by_cards(cards, vipGroups, picture_path, debug)
def test_my_dates(self): url = reverse('vcal.my_dates') response = self.client.get(url) eq_(response.status_code, 404) response = self.client.get(url, {'user': '******'}) eq_(response.status_code, 404) # add some slots and stuff tom = User.objects.create(username='******', first_name='Tom', last_name='smith', email='*****@*****.**') dick = User.objects.create(username='******', ) today = datetime.date.today() Slot.objects.create( user=tom, date=today - datetime.timedelta(days=7), ) Slot.objects.create(user=tom, date=today) Slot.objects.create(user=dick, date=today + datetime.timedelta(days=1)) Slot.objects.create( user=tom, date=today + datetime.timedelta(days=7), ) response = self.client.get(url, {'user': tom.email}) eq_(response.status_code, 200) eq_(response['Content-Type'], 'text/calendar;charset=utf-8') ok_('My Sheriff Duty.ics' in response['Content-Disposition']) payload = response.content parsed = vobject.readComponents(payload)
def import_vcf(filename): """ imports contacts from VCF file """ # append file extension if not included if filename[-4:] != ".vcf": filename += ".vcf" try: with open(filename, "r") as vcf_file: for entry in vobject.readComponents(vcf_file): name = entry.fn.value name = name.split(" ") if len(name) == 2: first = name[0] last = name[1] else: first = name[0] last = None mobile = entry.tel.value try: affiliation = entry.org.value[0] except: affiliation = None create_entry(first, last, mobile, affiliation) except Exception as e: print("Error processing VCF file: {}".format(e)) return None else: print("Successfully imported from VCF") return None
def test_all_dates(self): url = reverse('vcal.all_dates') response = self.client.get(url) eq_(response.status_code, 200) eq_(response['Content-Type'], 'text/calendar;charset=utf-8') ok_('Sheriff Duty.ics' in response['Content-Disposition']) # add some slots and stuff tom = User.objects.create(username='******', first_name='Tom', last_name='smith', email='*****@*****.**') dick = User.objects.create(username='******', ) today = datetime.date.today() Slot.objects.create(user=tom, date=today) Slot.objects.create(user=dick, date=today + datetime.timedelta(days=1)) url = reverse('vcal.all_dates') response = self.client.get(url) eq_(response.status_code, 200) payload = response.content ok_(-1 < payload.find(get_user_name(tom)) < payload.find( get_user_name(dick))) parsed = vobject.readComponents(payload) first = parsed.next() eq_(first.vevent.dtstart.value, today) ok_(get_user_name(tom) in first.vevent.summary.value) ok_(fmt(today) in first.vevent.description.value) url = first.vevent.url.value ok_(url.startswith('http')) response = self.client.get(url) eq_(response.status_code, 200)
def load(self): """ Loads all attributes for all contact entries from the SyncML backend For each entry a new L{contacts.contacts.Contact} instance is created and stored in the instance dictionary L{contacts.AbstractContactSynchronizationModule._allContacts}. For data parsing (VCF) the tools layer in L{vobjecttools} is used. """ pisiprogress.getCallback().progress.push(0, 100) pisiprogress.getCallback().verbose("SyncML: Loading") if_contacts = SyncmlModule.SyncmlContactsInterface(self._url, self._username, self._password, self._database, SYNCMLSERVER_IDENTIFIER) contacts_raw = if_contacts.downloadItems() # load if_contacts.finish() pisiprogress.getCallback().progress.setProgress(20) pisiprogress.getCallback().update("Loading") i = 0 for x in contacts_raw.keys(): content = vobject.readComponents(contacts_raw[x]) for y in content: atts = vobjecttools.extractVcfEntry(y) id = contacts.assembleID(atts) c = contacts.Contact(id, atts) self._allContacts[id] = c self._mapping[id] = x i += 1 pisiprogress.getCallback().progress.setProgress(20 + ((i*80) / len(contacts_raw))) pisiprogress.getCallback().update('Loading') pisiprogress.getCallback().progress.drop()
def import_file(self, path): """Merge items from ``path`` to collection. """ try: new_object = vobject.readComponents( codecs.open(path, encoding='utf-8').read()) for new_ics in new_object: if new_ics.name == 'VCALENDAR': events = new_ics.vevent_list for ve in events: # Check for events with both dtstart and duration entries and # delete the duration one if 'dtstart' in ve.contents and 'duration' in ve.contents: del ve.contents['duration'] new_ics.vevent_list = [ve] new_item = Item(new_ics.serialize().decode('utf-8'), None, path, self.urlpath) self.import_item(new_item, path) else: new_item = Item(new_ics.serialize().decode('utf-8'), None, path, self.urlpath) self.import_item(new_item, path) return True except Exception as ex: self.log.exception("Failed to import: %s", path) return False
def upload( url, filename, user, passwd, auth, verify ): if not url.endswith( '/' ): url += '/' print '[i] Uploading from', filename, 'to', url, '...' print '[i] Processing cards in', filename, '...' f = open( filename, 'r' ) cards = [] for card in vobject.readComponents( f, validate=True ): cards.append( card ) nCards = len(cards) print '[i] Successfuly read and validated', nCards, 'entries' print '[i] Connecting to', url, '...' dav = carddav.PyCardDAV( url, user=user, passwd=passwd, auth=auth, write_support=True, verify=verify ) curr = 1 for card in cards: print "\r[i] Uploading", curr, "of", nCards, sys.stdout.flush() curr += 1 if hasattr(card, 'prodid' ): del card.prodid if not hasattr( card, 'uid' ): card.add('uid') card.uid.value = str( uuid.uuid4() ) try: dav.upload_new_card( card.serialize().decode('utf-8') ) except Exception, e: print '' raise
def _parse(text, item_types, name=None): """Find items with type in ``item_types`` in ``text``. If ``name`` is given, give this name to new items in ``text``. Return a dict of items. """ item_tags = {item_type.tag: item_type for item_type in item_types} items = {} root = next(vobject.readComponents(text)) components = (root.components() if root.name in ("VADDRESSBOOK", "VCALENDAR") else (root, )) for component in components: item_name = None if component.name == "VTIMEZONE" else name item_type = item_tags[component.name] item = item_type(component.serialize(), item_name) if item.name in items: text = "\r\n".join((item.text, items[item.name].text)) items[item.name] = item_type(text, item.name) else: items[item.name] = item return items
def _get_local_events(local_calendar_uids): event_dict = {} for cal_uid in local_calendar_uids: if cal_uid == "system-calendar": cal_uid = "system" ical_path = os.path.join(EDS_CAL_PATH, cal_uid + "/calendar.ics") with open(ical_path, 'r') as ical: icalstream = ical.read() icalstream = fix_ical(ical_path) #print (icalstream) cal_obj = vobject.readComponents(icalstream) for event in cal_obj: event_uid = event.vevent.uid.value if cal_uid == "system": cal_uid = "system-calendar" event_uid = cal_uid + ":" + event_uid title = event.vevent.summary.value due = event.vevent.dtstart.value if int(due.strftime("%Y%m%d")) < int(EV_BEGIN) or \ int(due.strftime("%Y%m%d")) > int(EV_END): continue if isinstance(due, datetime.datetime): due = due.strftime("%A %d %B %Y %I:%M %p") else: due = due.strftime("%A %d %B %Y") if 'location' in event.vevent.contents: loc = event.vevent.location.value else: loc = None event_dict[event_uid] = {"name": title, "due": due, "loc": loc} return event_dict
def testWriteICalendarUnicodeBug3338(self): event = Calendar.CalendarEvent(itsView = self.view) event.summary = u"unicode \u0633\u0644\u0627\u0645" event.startTime = datetime.datetime(2010, 1, 1, 10, tzinfo=self.view.tzinfo.default) event.endTime = datetime.datetime(2010, 1, 1, 11, tzinfo=self.view.tzinfo.default) event.rruleset = self._makeRecurrenceRuleSet() coll = ListCollection("testcollection", itsParent=self.sandbox) coll.displayName = "test" coll.add(event.itsItem) # the view needs to be committed for event to be seen by sync self.view.commit() filename = u"unicode_export.ics" def delFile(): try: os.remove(filename) except OSError: pass delFile() sharing.exportFile(self.view, os.path.join(".", filename), coll) cal = vobject.readComponents(file(filename, 'rb')).next() self.assertEqual(cal.vevent.summary.value, event.summary) # no modifications should be serialized self.assertEqual(len(cal.vevent_list), 1) delFile()
def parse(self, input): """parse the given file or file source string""" if hasattr(input, "name"): self.filename = input.name elif not getattr(self, "filename", ""): self.filename = "" if isinstance(input, BytesIO): input = input.getvalue() elif hasattr(input, "read"): inisrc = input.read() input.close() input = inisrc if isinstance(input, bytes): input = input.decode(self.encoding) self._icalfile = next(vobject.readComponents(input)) for component in self._icalfile.components(): if component.name == "VEVENT": for property in component.getChildren(): if property.name in ( "SUMMARY", "DESCRIPTION", "COMMENT", "LOCATION", ): newunit = self.addsourceunit(property.value) newunit.addnote("Start date: %s" % component.dtstart.value) newunit.addlocation(f"[{component.uid.value}]{property.name}")
def main(argv): inputfile = '' #Exit if no parameters are provided if len(argv) == 0: print('simple_contract_trimmer.py -i <inputfile>') sys.exit() try: opts, args = getopt.getopt(argv,"hi:",["ifile="]) except getopt.GetoptError: print('simple_contract_trimmer.py -i <inputfile>') sys.exit(2) #Exit if anything other than the input file is provided for opt, arg in opts: if opt in ("-i", "--ifile"): inputfile = arg else: print('Useage: simple_contract_trimmer.py -i <inputfile>') sys.exit() try: contacts = open(inputfile, "r") except IOError as e: print(e) final_contact = open("modified_contact.vcf", "w", newline="") # Writes all entries with the 'tel' attribute to a new .vcf file vcard = vobject.readComponents(contacts.read()) for entry in vcard: if hasattr(entry, 'tel'): final_contact.write(entry.serialize())
def import_file(self, path): """Merge items from ``path`` to collection. """ try: new_object = vobject.readComponents(codecs.open(path,encoding='utf-8').read()) for new_ics in new_object: if new_ics.name == 'VCALENDAR': events = new_ics.vevent_list for ve in events: # Check for events with both dtstart and duration entries and # delete the duration one if ve.contents.has_key('dtstart') and ve.contents.has_key('duration'): del ve.contents['duration'] new_ics.vevent_list = [ve] new_item = Item(new_ics.serialize().decode('utf-8'), None, path) self.import_item(new_item, path) else: new_item = Item(new_ics.serialize().decode('utf-8'), None, path) self.import_item(new_item, path) return True except Exception, ex: self.log.exception("Failed to import: %s", path) return False
def readEvents(self): data = urlopen(config["calendar"]).read().decode('utf-8') # data = open("calendar.ics").read() # useful for debugging localTZ = pytz.timezone(config["timezone"]) now = datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(localTZ) nearestEvent = False meetsPattern = "(https://meet.google.com/[a-zA-Z0-9_-]+)" if (config["hourFormat"] == 24): hourFormat = "%H:%M" else: hourFormat = "%I:%M %p" # find the most immediate event for cal in vobject.readComponents(data): for component in cal.components(): if component.name != "VEVENT": continue try: eventDate = component.dtstart.valueRepr().replace( tzinfo=pytz.utc).astimezone(localTZ) except: print("Invalid date, skipping event " + component.summary.valueRepr()) continue # Repeating events are somehow special, they don't tell you if today meets the criteria, so you gotta adapt # RRULE format: FREQ=WEEKLY;UNTIL=20200318T225959Z;BYDAY=FR,MO,TH,TU,WE if hasattr(component, 'rrule'): allEventDates = component.getrruleset(addRDate=True) expectedDate = allEventDates[0].replace(day=now.day, month=now.month, year=now.year) # ugly hack to keep proper event hour/minute while checking if happens today too if expectedDate in allEventDates: eventDate = expectedDate if eventDate > now and eventDate.day == now.day and ( nearestEvent == False or eventDate < nearestEvent.date): nearestEvent = CalendarEvent(eventDate, component) if (nearestEvent == False): self.indicator.set_label("No more events for today! :P", "8.8") else: hasMeetUrl = re.search( meetsPattern, nearestEvent.component.description.valueRepr()) if (hasMeetUrl): self.meetUrl = hasMeetUrl.group() else: self.meetUrl = False self.indicator.set_label( "Today at " + nearestEvent.date.strftime(hourFormat) + " -> " + nearestEvent.component.summary.valueRepr(), "8.8") return True # required for timeout_add_seconds to work
def parse_vcard(myfile, newsletter, ignore_errors=False): import vobject try: myvcards = vobject.readComponents(myfile) except vobject.VObjectError, e: raise forms.ValidationError(_(u"Error reading vCard file: %s" % e))
def load(self): """ Load all data from backend """ pisiprogress.getCallback().verbose("Evolution: Loading") pisiprogress.getCallback().progress.push(0, 100) file = bsddb.hashopen(self._path) pisiprogress.getCallback().update("Loading") amount = len(file.keys()) i = 0 for key in file.keys(): data = file[key] # print data if not data.startswith("BEGIN:VCARD"): continue comps = vobject.readComponents( data[: len(data) - 1] ) # there is some problem with a traling white space in each entry for x in comps: # print x.n.value.given atts = extractVcfEntry(x) id = contacts.assembleID(atts) c = contacts.Contact(id, atts) self._allContacts[id] = c self._rawData[id] = x self._edsIDs[id] = key i += 1 pisiprogress.getCallback().progress.setProgress((i * 100) / amount) pisiprogress.getCallback().update("Loading") pisiprogress.getCallback().progress.drop()
def _parse(text, item_types, name=None): """Find items with type in ``item_types`` in ``text``. If ``name`` is given, give this name to new items in ``text``. Return a dict of items. """ item_tags = {item_type.tag: item_type for item_type in item_types} items = {} root = next(vobject.readComponents(text)) components = ( root.components() if root.name in ("VADDRESSBOOK", "VCALENDAR") else (root,)) for component in components: item_name = None if component.name == "VTIMEZONE" else name item_type = item_tags[component.name] item = item_type(component.serialize(), item_name) if item.name in items: text = "\r\n".join((item.text, items[item.name].text)) items[item.name] = item_type(text, item.name) else: items[item.name] = item return items
def generate_preview_content(filename, mime): """ Creates a preview content for the given file and mime type. Supported types: png, jpg, gif, vcard (vcf) """ image_mimes = { 'image/png': 'PNG', 'image/jpeg': 'JPEG', 'image/gif': 'GIF' } if mime in image_mimes: im = Image.open(filename) im.thumbnail((128, 128), Image.ANTIALIAS) buf = StringIO.StringIO() im.save(buf, format=image_mimes[mime]) return buf.getvalue() vcard_mimes = ['text/x-vcard', 'text/vcard'] if mime in vcard_mimes: f = open(filename, 'r') count = 0 s = first = vobject.readComponents(f).next() while s: count += 1 try: s = s.next() except: break if count > 1: return '%d vCard' % count else: return str(first.n.value).strip()
def parse_vcf_file(vcf_list): ''' Parse the contents of VCF-file entries ''' string_buffer = io.StringIO(u''.join(vcf_list)) try: vcards_raw = vobject.readComponents(string_buffer, validate=False, ignoreUnreadable=True) contacts = set(vcard.serialize(validate=False) for vcard in vcards_raw) stringio = io.StringIO(''.join(contacts)) vcards = vobject.readComponents(stringio, ignoreUnreadable=True) except Exception as e: raise e return vcards
def get_contacts(self, credentials): for card in vobject.readComponents(credentials["stream"]): try: yield {"email": card.email.value, "name": card.fn.value} except AttributeError: # if a person doesn"t have an email or a name ignore them continue
def sub_list(args): print 'exec list' fname = args.file f = open(fname) for item in vobject.readComponents(f): print_info(item) f.close()
def sub_split(args): print "exec split" fname = args.file output = args.dir f = open(fname) for item in vobject.readComponents(f): write_file(item, output) f.close()
def main(): applelist = [] for x in vobject.readComponents(file("vCard-kort.vcf")): card=parsevCard(x) card.apple = True applelist.append(vCardWithMatches(card)) googlelist = [] for x in vobject.readComponents(file("contacts.vcf")): card = parsevCard(x) card.apple = False googlelist.append(vCardWithMatches(card)) merged = [] all = [] all.extend(applelist) all.extend(googlelist) for vcard in all: match = filter(lambda m:vcard.matches(m),merged) if not match: merged.append(vcard) else: match[0].merge(vcard) if len(match)>1 : if (len( match[0].name & match[1].name )>=2 and len(match)<=2): match[0].merge(match[1]) merged.remove(match[1]) else: raise Exception("Length %(l)d > 1, first two elements are %(a)s and %(b)s,"%{"l":len(match),"a":match[0],"b":match[1]}) import codecs f = codecs.open('result.txt', encoding='utf-16', mode='w+') try: for x in merged: if (len(x.name)>0 and len(x.emails)>0 and x.apple): f.write(unicode( u"%(name)s: %(emails)s\n"%{"name":list(x.name), "emails":list(x.emails)})) finally: f.close()
def read_vcard(vcard_path): """ read the vcard file and extract contacts from it """ # open/read the input file vcard_data = None try: vcard_data = open(vcard_path, "rb").read() except Exception as e: print("[-] open %s failed (%s)" % (vcard_path, e)) return None vcard_data = vcard_data.decode('utf-8', 'ignore') # use the vobject library to parse the input file data vcard_list = vobject.readComponents(vcard_data) contacts = [] for vcard in vcard_list: new_contact = {} # contact name new_contact['name'] = vcard.contents['fn'][0].value # addresses new_contact['adr'] = [] if 'adr' in vcard.contents: for adr in vcard.contents['adr']: new_contact['adr'].append(str(adr.value)) # phone numbers new_contact['tel'] = [] if 'tel' in vcard.contents: for tel in vcard.contents['tel']: t = tel.value if "TYPE" in tel.params: typ = tel.params["TYPE"][0] if typ in conversion: t += " (%s)" % conversion[typ] else: t += " (%s)" % typ new_contact['tel'].append(t) # email new_contact['email'] = [] if 'email' in vcard.contents: for email in vcard.contents['email']: new_contact['email'].append(str(email.value)) # photo new_contact['photo'] = "" if 'photo' in vcard.contents: new_contact['photo'] = vcard.contents['photo'][0].value contacts.append(new_contact) print("[+] %d contact(s) found" % len(contacts)) return contacts
def ingest(self, file_path, entity): entity.schema = model.get('PlainText') text = self.read_file_decoded(entity, file_path) entity.set('bodyText', text) try: for card in vobject.readComponents(text): self.ingest_card(entity, card) except vobject.base.ParseError as err: raise ProcessingException('Cannot parse vcard: %s' % err) from err
def main(args: list) -> int: if len(args) < 4: print("Too few arguments: diff.py [File A] [File B] [Output File]") return -1 first = list() second = list() with open(args[1]) as filea: first = [x for x in vobject.readComponents(filea, validate=True)] with open(args[2]) as fileb: second = [x.fn for x in vobject.readComponents(fileb, validate=True)] with open(args[3], 'w') as file: for card in first: if not card.fn in second: file.write(card.serialize())
def parseICS(classes_to_ignore): mypath = os.path.dirname(os.path.realpath(__file__)) if not os.path.isdir('temp'): os.mkdir('temp') print('Parsing .ics files to csv') with open(os.path.join(mypath, "temp", 'events.csv'), mode='w', encoding='latin1') as csv_out: csv_writer = csv.writer(csv_out, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) csv_writer.writerow( ['WHAT', 'WHERE', 'FROM', 'TO', 'DESCRIPTION', 'CATEGORY']) # read the data from the file Cals = {} for f in os.listdir(mypath + '/Calendars'): if 'pdf' not in f: #print(mypath + '\Calendars\\' + f) print(f) Cals[f[:-4]] = open((mypath + '/Calendars/' + f), encoding='latin-1').read() # iterate through the contents of the .ics corresponding to the calendar of the first semester ignored_count = 0 try: for key in Cals: for cal in vobject.readComponents(Cals[key]): for component in cal.components(): if component.name == "VEVENT": event_name = restoreAccents( component.summary.valueRepr()) if event_name.replace(" ", "").lower().split( ",")[0] in classes_to_ignore: #print(event_name, "event ignored") ignored_count += 1 continue # # write to csv csv_writer.writerow([ event_name, restoreAccents(component.location.valueRepr()), component.dtstart.valueRepr(), component.dtend.valueRepr(), restoreAccents( component.description.valueRepr()), component.categories.valueRepr() ]) print(ignored_count, "events ignored") print(str(key), 'succesfuly parsed') except Exception as e: print(e) print("There was an error parsing the calendar(s)") return False return True
def all(cls, f): stuff = list() for c in vobject.readComponents(f): try: r = cls(c) stuff.append(r) except: pass return stuff
def anonymizeData(directoryMap, data): vobj = vobject.readComponents(data).next() # Delete property from the top level try: for prop in vobj.contents['x-wr-calname']: prop.value = anonymize(prop.value) except KeyError: pass for comp in vobj.components(): # Replace with anonymized CUAs: for propName in ('organizer', 'attendee'): try: for prop in list(comp.contents[propName]): cua = prop.value record = directoryMap.lookupCUA(cua) if record is None: # print "Can't find record for", cua record = directoryMap.addRecord(cua=cua) if record is None: comp.remove(prop) continue prop.value = "urn:uuid:%s" % (record['guid'],) if prop.params.has_key('X-CALENDARSERVER-EMAIL'): prop.params['X-CALENDARSERVER-EMAIL'] = (record['email'],) else: prop.params['EMAIL'] = (record['email'],) prop.params['CN'] = (record['name'],) except KeyError: pass # Replace with anonymized text: for propName in ('summary', 'location', 'description'): try: for prop in comp.contents[propName]: prop.value = anonymize(prop.value) except KeyError: pass # Replace with anonymized URL: try: for prop in comp.contents['url']: prop.value = "http://example.com/%s/" % (anonymize(prop.value),) except KeyError: pass # Remove properties: for propName in ('x-apple-dropbox', 'attach'): try: for prop in list(comp.contents[propName]): comp.remove(prop) except KeyError: pass return vobj.serialize()
def parse_vcard(myfile, newsletter, ignore_errors=False): import vobject try: myvcards = vobject.readComponents(myfile) except vobject.VObjectError, e: raise forms.ValidationError( _(u"Error reading vCard file: %s" % e) )
def tryDodgyWay(): import bsddb import vobject file = bsddb.hashopen("/home/michael/.evolution/addressbook/local/system/addressbook.db") rem = None print len(file.keys()) for key in file.keys(): data = file[key] print "\n\n", data if not data.startswith('BEGIN:VCARD'): continue comps = vobject.readComponents(data[:len(data)-1]) for x in comps: # print x.n.value.given, key, ":" print x if x.n.value.given == "Juliane": rem = [key, data, x] # # test modify # x.n.value.given = "KichKasch" # # file[rem[0]] = rem[2].serialize() # # test delete # if rem: # print "Deleting ", rem[0] # del file[rem[0]] # #test add # import vobject # j = vobject.vCard() # nameEntry = vobject.vcard.Name(family = "Gust", given = "Juliane", additional = "") # n = j.add('n') # n.value = nameEntry # fn = j.add('fn') # fn.value = "Juliane Gust" # email = j.add('email') # email.value="*****@*****.**" # email.type_param = "HOME" # # id = _generateEDS_ID() # jid = j.add('uid') # jid.value = id # # print "adding ", id, "\n", j.serialize() # file[id] = j.serialize() # # del all # for x in file.keys(): # del file[x] print "Committing ..." file.sync() # commit
def load_vcf(file_path: str): """ Load the contents of the .vcf file. :param file_path: The file path to load contacts from. :rtype: Generator object. """ # Load .vcf file. with open(file_path) as vcf_file: vcf_data = vobject.readComponents(vcf_file.read()) return vcf_data
def get_contacts(self, credentials): for card in vobject.readComponents(credentials["stream"]): try: yield { "email": card.email.value, "name": card.fn.value } except AttributeError: # if a person doesn"t have an email or a name ignore them continue
def parse_vcf(string): """ Parses a vcf string, potentially containing many vcards @returns: A list of Contacts """ contacts = [] for vobj in vobject.readComponents(string): if vobj.behavior == vobject.vcard.VCard3_0: contacts.append(Contact(vcard=vobj)) return contacts
def ingest(self, file_path, entity): entity.schema = model.get("PlainText") text = self.read_file_decoded(entity, file_path) text = sanitize_text(text) entity.set("bodyText", text) try: for card in vobject.readComponents(text): self.ingest_card(entity, card) except (ParseError, UnicodeDecodeError) as err: raise ProcessingException("Cannot parse vcard: %s" % err) from err
def vcard_contacts_import(stream, mailing_list=None, segment=None): """Import contacts from a VCard file""" contacts = [] vcards = vobject.readComponents(stream) for vcard in vcards: contact = {"email": vcard.email.value, "first_name": vcard.n.value.given, "last_name": vcard.n.value.family} contacts.append(contact) return create_contacts(contacts, "vcard", mailing_list, segment)
def parse_vcard(myfile, newsletter, ignore_errors=False): try: import vobject except ImportError: raise ImportError("Must have vobject installed") try: myvcards = vobject.readComponents(myfile) except vobject.VObjectError as e: raise forms.ValidationError( _(u"Error reading vCard file: %s" % e) ) addresses = {} for myvcard in myvcards: if hasattr(myvcard, 'fn'): name = check_name(myvcard.fn.value, ignore_errors) else: name = None # Do we have an email address? # If not: either continue to the next vcard or # raise a validation error. if hasattr(myvcard, 'email'): email = check_email(myvcard.email.value, ignore_errors) elif not ignore_errors: raise forms.ValidationError( _("Entry '%s' contains no email address.") % name) else: continue try: validate_email(email) addr = make_subscription(newsletter, email, name) except ValidationError: if not ignore_errors: raise forms.ValidationError( _("Entry '%s' does not contain a valid e-mail address.") % name ) if addr: if email in addresses and not ignore_errors: raise forms.ValidationError( _("The address file contains duplicate entries for '%s'.") % email ) addresses.update({email: addr}) elif not ignore_errors: raise forms.ValidationError( _("Some entries are already subscribed to.")) return addresses
def vcard_contacts_import(stream, workgroups=[]): contacts = [] vcards = vobject.readComponents(stream) for vcard in vcards: contact = {'email': vcard.email.value, 'first_name': vcard.n.value.given, 'last_name': vcard.n.value.family} contacts.append(contact) return create_contacts(contacts, 'vcard', workgroups)
def test_filter_dst_event_out_of_dst(): components = list(vobject.readComponents(CALENDAR_DATA)) events = components[0].vevent_list results = list( filter.by_date_range( events, datetime.datetime(2010, 3, 20, tzinfo=utc), datetime.datetime(2010, 3, 28, tzinfo=utc), )) assert len(results) == 1 return
def process(self): import vobject self.fp = self.connector.open('r') s = "".join(self.fp.readlines()) reader = vobject.readComponents(s) while True: row = {} data = reader.next() for d in data.contents: row[unicode(d)] = eval('data.' + unicode(d) + '.value') yield row, 'main'
def import_contacts(self, user_id, stream): count = 0 excluded = [] for card in vobject.readComponents(stream): cfields = parse_vcard_object(card) try: self.add_user_contact(*cfields) count += 1 except IntegrityError: excluded.append(card) return count, excluded
def vcard_contacts_import(stream, mailing_list=None, segment=None): """Import contacts from a VCard file""" contacts = [] vcards = vobject.readComponents(stream) for vcard in vcards: contact = {'email': vcard.email.value, 'full_name': "%s %s" % (vcard.n.value.given, vcard.n.value.family)} contacts.append(contact) return create_contacts(contacts, 'vcard', mailing_list, segment)
def myformat(pathname): filename, ext = os.path.splitext(pathname) if ext != ".vcf": return False try: cards = [card for card in vobject.readComponents(open(pathname, "r"))] # cards = parsefile(open(pathname, "r")) # with bitpim parser except: return False note(3, "%d cards in vCard file %s", len(cards), pathname) return { "parsed-cards" : cards }
def import_vcards(stream, user): """ Imports the given vcard stream into the contacts of the given user. Returns a tuple of (number imported, total number of cards). """ #Contact.objects.filter(owner=user, type='V', user__isnull=True).delete() total = 0 imported = 0 for card in vobject.readComponents(stream): contact_vals = {} try: if card.fn.value != 'null': contact_vals['name'] = card.fn.value contact_vals['email'] = card.email.value try: name_field = card.contents.get('n') try: contact_vals['last_name'] = name_field[0].value.family.strip() contact_vals['first_name'] = name_field[0].value.given.strip() except: pass except: pass try: for tel in card.contents.get('tel'): try: type = tel.params except AttributeError: type = [] for t in type: if t == 'CELL' or t == 'MOBILE': contact_vals['mobile'] = tel.value break if t == 'FAX': contact_vals['fax'] = tel.value break else: if not contact_vals.has_key('phone') or t == 'pref': contact_vals['phone'] = t.value break except: pass contact, created = create_contact_from_values(owner=user, type='V', **contact_vals) if created: imported += 1 if contact: total += 1 except AttributeError: pass # missing value so don't add anything return imported, total
def __init__(self, fileName, dataPath): super(VCardCalendarBackend, self).__init__(fileName, dataPath) vcevents = [] try: for card in vobject.readComponents(readFile(fileName)): if hasattr(card, "bday"): vcevents.append({"name": card.fn.value, "date": card.bday.value, "yearly": True}) self.events = [Event(vcevent, title=self.title) for vcevent in vcevents] except Exception as e: print("Failed to load file {0}: {1}".format(fileName, str(e)))
def abook_file(vcard, bookfile): """Write a new Abook file with the given vcards""" book = ConfigObj(encoding='utf-8', list_values=False) book.filename = bookfile.name book.initial_comment = ['abook addressbook file'] book['format'] = {} book['format']['program'] = 'abook' book['format']['version'] = '0.6.0pre2' for (i, card) in enumerate(readComponents(vcard.read())): Abook.to_abook(card, str(i), book) Abook._write(book)
def get_events(self): """Generator that yields all events in the calendar. """ ics_filenames = glob(os.path.join(self.path, 'Events', '*.ics')) for ics_filename in ics_filenames: with open(ics_filename, 'rt') as ics_file: for component in vobject.readComponents(ics_file): try: for event in component.vevent_list: yield event except AttributeError: continue return
def parse(self, input): """parse the given file or file source string""" if hasattr(input, 'name'): self.filename = input.name elif not getattr(self, 'filename', ''): self.filename = '' if hasattr(input, "read"): inisrc = input.read() input.close() input = inisrc if isinstance(input, bytes): input = BytesIO(input) self._icalfile = next(vobject.readComponents(input)) else: self._icalfile = next(vobject.readComponents(open(input))) for component in self._icalfile.components(): if component.name == "VEVENT": for property in component.getChildren(): if property.name in ('SUMMARY', 'DESCRIPTION', 'COMMENT', 'LOCATION'): newunit = self.addsourceunit(property.value) newunit.addnote("Start date: %s" % component.dtstart.value) newunit.addlocation("[%s]%s" % (component.uid.value, property.name))
def do_PUT(self, environ, read_collections, write_collections, content, user): """Manage PUT request.""" if not write_collections: return NOT_ALLOWED collection = write_collections[0] content_type = environ.get("CONTENT_TYPE") if content_type: tags = {value: key for key, value in storage.MIMETYPES.items()} tag = tags.get(content_type.split(";")[0]) if tag: collection.set_meta("tag", tag) headers = {} item_name = xmlutils.name_from_path(environ["PATH_INFO"], collection) item = collection.get(item_name) etag = environ.get("HTTP_IF_MATCH", "") match = environ.get("HTTP_IF_NONE_MATCH", "") == "*" if (not item and not etag) or ( item and ((etag or item.etag) == item.etag) and not match): # PUT allowed in 3 cases # Case 1: No item and no ETag precondition: Add new item # Case 2: Item and ETag precondition verified: Modify item # Case 3: Item and no Etag precondition: Force modifying item items = list(vobject.readComponents(content or "")) if item: # PUT is modifying an existing item if items: new_item = collection.update(item_name, items[0]) else: new_item = None elif item_name: # PUT is adding a new item if items: new_item = collection.upload(item_name, items[0]) else: new_item = None else: # PUT is replacing the whole collection collection.delete() new_item = self.Collection.create_collection( environ["PATH_INFO"], items) if new_item: headers["ETag"] = new_item.etag status = client.CREATED else: # PUT rejected in all other cases status = client.PRECONDITION_FAILED return status, headers, None