def test_parse_row(self): line = """["Jane" "Doe" nil "Fake Pty Ltd" (["Mobile" "+61 4123 456 789"] ["Home" "61 2 9876 5432"]) nil ("*****@*****.**") ((creation-date . "2001-01-01") (timestamp . "2002-02-02")) nil]""" r = parse_line(line) self.assertEqual(r['firstname'], 'Jane') self.assertEqual(r['lastname'], 'Doe') self.assertEqual(r['company'], 'Fake Pty Ltd') self.assertEqual(r['phone']['Mobile'], '+61 4123 456 789') self.assertEqual(r['phone']['Home'], '61 2 9876 5432') self.assertEqual(r['email'], ['*****@*****.**']) self.assertEqual(r['random']['creation-date'], date(2001, 01, 01))
def build_vcal_item(data): event = Event() name = " ".join(data[name] for name in ['firstname', 'lastname'] if data[name]) summary = "%s's Birthday" % name d = data['random']['anniversary'] now = date(date.today().year, d.month, d.day) if now > date.today(): d = date(now.year, d.month, d.day) else: d = date(now.year + 1, d.month, d.day) event.add('summary', summary) event.add('dtstart', d) event.add('dtend', d) event.add('dtstamp', datetime.now(UTC)) event['uid'] = "buttle/" + ''.join(c for c in summary if c in string.ascii_letters + string.digits).lower() + "-" + data['random']['anniversary'].isoformat() return event calendar = Calendar() calendar.add('version', '2.0') calendar.add('prodid', '-//buttle//ertius.org//') with open(options.input) as f: for line in f: data = parse_line(line) if data and 'anniversary' in data['random']: event = build_vcal_item(data) calendar.add_component(event) with open(options.output, 'w') as output: output.write(calendar.as_string()[:-2])
summary = "%s's Birthday" % name d = data['random']['anniversary'] now = date(date.today().year, d.month, d.day) if now > date.today(): d = date(now.year, d.month, d.day) else: d = date(now.year + 1, d.month, d.day) event.add('summary', summary) event.add('dtstart', d) event.add('dtend', d) event.add('dtstamp', datetime.now(UTC)) event['uid'] = "buttle/" + ''.join( c for c in summary if c in string.ascii_letters + string.digits).lower( ) + "-" + data['random']['anniversary'].isoformat() return event calendar = Calendar() calendar.add('version', '2.0') calendar.add('prodid', '-//buttle//ertius.org//') with open(options.input) as f: for line in f: data = parse_line(line) if data and 'anniversary' in data['random']: event = build_vcal_item(data) calendar.add_component(event) with open(options.output, 'w') as output: output.write(calendar.as_string()[:-2])
def test_parse_multiple_emails(self): line = """["Jane" "" nil "Fake Pty Ltd" nil nil ("*****@*****.**" "*****@*****.**") ((creation-date . "2001-01-01") (timestamp . "2002-02-02")) nil]""" r = parse_line(line) self.assertEqual(r['email'][0], '*****@*****.**') self.assertEqual(r['email'][1], '*****@*****.**')
def test_parse_has_no_surname(self): line = """["Jane" "" nil "Fake Pty Ltd" nil nil ("*****@*****.**") ((creation-date . "2001-01-01") (timestamp . "2002-02-02")) nil]""" r = parse_line(line) self.assertEqual(r['firstname'], 'Jane') self.assertEqual(r['lastname'], '') self.assertEqual(r['company'], 'Fake Pty Ltd')
def test_parse_comment_line(self): line = """;; this is a comment""" r = parse_line(line) self.assertEqual(r, {})
def test_parse_has_no_phone_number(self): line = """["Jane" "Doe" nil "Fake Pty Ltd" nil nil ("*****@*****.**") ((creation-date . "2001-01-01") (timestamp . "2002-02-02")) nil]""" r = parse_line(line) self.assertEqual(r['phone'], {})