예제 #1
0
    def test_parse_dict(self):
        self.assertEqual(
            Event.parse_dict(
                {
                    "summary": "Google I/O 2015",
                    "start": {"dateTime": "2015-05-28T09:00:00-07:00", "timeZone": "America/Los_Angeles"},
                    "end": {"dateTime": "2015-05-28T17:00:00-07:00", "timeZone": "America/Los_Angeles"},
                    "creator": {"displayName": "Foo Bar", "email": "*****@*****.**"},
                },
                "America/Los_Angeles",
            ),
            self.e0,
        )
        self.assertEqual(
            Event.parse_dict(
                {"summary": "あいうえお", "start": {"date": "2015-10-17"}, "end": {"date": "2015-10-18"}}, "Asia/Tokyo"
            ),
            self.e1,
        )

        self.assertEqual(
            Event.parse_dict(
                {
                    "summary": "あいうえお",
                    "start": {"date": "2015-10-17"},
                    "end": {"date": "2015-10-18"},
                    "creator": {"email": "*****@*****.**"},
                },
                "Asia/Tokyo",
            ),
            self.e2,
        )
예제 #2
0
    def parse_args(self, argv):
        assert self.now is not None

        # decode all args as utf-8
        option, args = arg_parser.parser.parse_args(
            [to_unicode(a, errors='ignore') for a in argv[1:]])

        try:
            if not args:
                # summary
                dt = oget(self._parse_date(option.date, self.now),
                          self.now.date())
                start_time = get_localzone().localize(
                    datetime(dt.year, dt.month, dt.day))

                fmt = (option.format
                       or (arg_parser.DEFAULT_FORMAT if option.days == 0 else
                           arg_parser.DEFAULT_FORMAT_DAYS))

                if option.days == 0:
                    # show events on one day
                    duration = timedelta(days=1)
                elif option.days < 0:
                    # show events from past several days
                    duration = timedelta(days=-option.days + 1)
                    start_time -= timedelta(days=-option.days)
                else:
                    # show events from several days from today
                    duration = timedelta(days=option.days + 1)

                operation = SummaryOperation(option.calendar, start_time,
                                             duration, option.credential, fmt,
                                             option.separator)
            elif args[0] == 'setup' and len(args) == 2:
                # setup
                operation = SetupOperation(args[1], option.credential,
                                           option.read_only, option.no_browser)
            elif args[0] == 'create' and len(args) >= 2:
                # create
                summary = ' '.join(args[1:])
                start, end = self._parse_time_range(option.date,
                                                    option.start_time,
                                                    option.end_time, self.now)
                ev = Event(start, end, summary, location=option.location)
                operation = CreateOperation(option.calendar, ev,
                                            option.credential)
            else:
                # help
                operation = HelpOperation()
        except Exception as e:
            # parse error
            operation = HelpOperation(e)
            if option.debug:
                import traceback
                traceback.print_exc()
                print()

        return self.copy(operation=operation, debug=option.debug)
예제 #3
0
    def test_parse_dict(self):
        self.assertEqual(
            Event.parse_dict(
                {
                    'summary': 'Google I/O 2015',
                    'start': {
                        'dateTime': '2015-05-28T09:00:00-07:00',
                        'timeZone': 'America/Los_Angeles'
                    },
                    'end': {
                        'dateTime': '2015-05-28T17:00:00-07:00',
                        'timeZone': 'America/Los_Angeles'
                    },
                    'creator': {
                        'displayName': 'Foo Bar',
                        'email': '*****@*****.**',
                    },
                }, 'America/Los_Angeles'), self.e0)
        self.assertEqual(
            Event.parse_dict(
                {
                    'summary': 'あいうえお',
                    'start': {
                        'date': '2015-10-17'
                    },
                    'end': {
                        'date': '2015-10-18'
                    },
                }, 'Asia/Tokyo'), self.e1)

        self.assertEqual(
            Event.parse_dict(
                {
                    'summary': 'あいうえお',
                    'start': {
                        'date': '2015-10-17'
                    },
                    'end': {
                        'date': '2015-10-18'
                    },
                    'creator': {
                        'email': '*****@*****.**'
                    },
                }, 'Asia/Tokyo'), self.e2)
 def list_events(self, calendar_id, time_min, time_max):
     """
     :return: list[Event]: event list sorted by startTime and endTime (all-day events come first)
     """
     params = {
         "calendarId": calendar_id,
         "timeMin": time_min.astimezone(pytz.utc).isoformat(),
         "timeMax": time_max.astimezone(pytz.utc).isoformat(),
         "maxResults": MAX_RESULTS,
         "singleEvents": True,
         "orderBy": "startTime",
     }
     events_result = self._service.events().list(**params).execute()
     return sorted(Event.parse_dict(d, events_result["timeZone"]) for d in events_result.get("items", []))
 def list_events(self, calendar_id, time_min, time_max):
     """
     :return: list[Event]: event list sorted by startTime and endTime (all-day events come first)
     """
     params = {
         'calendarId': calendar_id,
         'timeMin': time_min.astimezone(pytz.utc).isoformat(),
         'timeMax': time_max.astimezone(pytz.utc).isoformat(),
         'maxResults': MAX_RESULTS,
         'singleEvents': True,
         'orderBy': 'startTime'
     }
     events_result = self._service.events().list(**params).execute()
     return sorted(Event.parse_dict(d, events_result['timeZone']) for d in events_result.get('items', []))
예제 #6
0
    def test_parse_args(self):
        # create
        a0 = ['calendar-cli', 'create', '--date', '20151018', '--start', '1030', '--end', '1100',
              'あいう'.encode('utf-8'), 'えお'.encode('utf-8')]
        s0 = Setting().parse_args(a0)

        self.assertIsInstance(s0.operation, CreateOperation)
        self.assertEqual(s0.operation.calendar_id, 'primary')
        self.assertEqual(s0.operation.event, Event(
            EventTime(True, self._localize(2015, 10, 18, 10, 30)),
            EventTime(True, self._localize(2015, 10, 18, 11, 00)),
            'あいう えお'
        ))

        # summary
        t = datetime.now()
        today = get_localzone().localize(datetime(t.year, t.month, t.day))

        a = ['calendar-cli']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SummaryOperation)
        self.assertEqual(s.operation.calendar_id, 'primary')
        self.assertEqual(s.operation.start_time, today)
        self.assertEqual(s.operation.duration, timedelta(days=1))
        self.assertEqual(s.operation.format, '[%T] %S%L%C')

        a = ['calendar-cli', '--days', '0']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SummaryOperation)
        self.assertEqual(s.operation.calendar_id, 'primary')
        self.assertEqual(s.operation.start_time, today)
        self.assertEqual(s.operation.duration, timedelta(days=1))
        self.assertEqual(s.operation.format, '[%T] %S%L%C')

        a = ['calendar-cli', '--days', '1']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SummaryOperation)
        self.assertEqual(s.operation.calendar_id, 'primary')
        self.assertEqual(s.operation.start_time, today)
        self.assertEqual(s.operation.duration, timedelta(days=2))
        self.assertEqual(s.operation.format, '%D [%T] %S%L%C')

        a = ['calendar-cli', '--days', '6']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SummaryOperation)
        self.assertEqual(s.operation.calendar_id, 'primary')
        self.assertEqual(s.operation.start_time, today)
        self.assertEqual(s.operation.duration, timedelta(days=7))
        self.assertEqual(s.operation.format, '%D [%T] %S%L%C')

        a = ['calendar-cli', '--days', '6', '--format', '%D %S']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SummaryOperation)
        self.assertEqual(s.operation.calendar_id, 'primary')
        self.assertEqual(s.operation.start_time, today)
        self.assertEqual(s.operation.duration, timedelta(days=7))
        self.assertEqual(s.operation.format, '%D %S')

        a = ['calendar-cli', '--days', '-1']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SummaryOperation)
        self.assertEqual(s.operation.calendar_id, 'primary')
        self.assertEqual(s.operation.start_time, today - timedelta(days=1))
        self.assertEqual(s.operation.duration, timedelta(days=2))
        self.assertEqual(s.operation.format, '%D [%T] %S%L%C')

        a = ['calendar-cli', '--date', '20151018']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SummaryOperation)
        self.assertEqual(s.operation.calendar_id, 'primary')
        self.assertEqual(s.operation.start_time, self._localize(2015, 10, 18, 0, 0))
        self.assertEqual(s.operation.duration, timedelta(days=1))
        self.assertEqual(s.operation.format, '[%T] %S%L%C')

        a = ['calendar-cli', '--date', '20151018', '--days', '-3', '--format', '%S']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SummaryOperation)
        self.assertEqual(s.operation.calendar_id, 'primary')
        self.assertEqual(s.operation.start_time, self._localize(2015, 10, 15, 0, 0))
        self.assertEqual(s.operation.duration, timedelta(days=4))
        self.assertEqual(s.operation.format, '%S')

        # setup
        a = ['calendar-cli', 'setup', 'client_secret.json']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SetupOperation)
        self.assertEqual(s.operation.secret_path, 'client_secret.json')
        self.assertEqual(s.operation.read_only, False)
        self.assertEqual(s.operation.no_browser, False)

        a = ['calendar-cli', 'setup', 'client_secret.json', '--read-only', '--no-browser']
        s = Setting().parse_args(a)

        self.assertIsInstance(s.operation, SetupOperation)
        self.assertEqual(s.operation.secret_path, 'client_secret.json')
        self.assertEqual(s.operation.read_only, True)
        self.assertEqual(s.operation.no_browser, True)
    def test_make_output(self):
        so1 = SummaryOperation('primary',
                               datetime(2015, 10, 17, 0, 0, 0, 0, pytz.utc),
                               timedelta(days=3), 'dummy_path', '%D [%T] %S',
                               None)
        so2 = SummaryOperation('primary',
                               datetime(2015, 10, 17, 0, 0, 0, 0, pytz.utc),
                               timedelta(days=3), 'dummy_path', '%D [%T] %S',
                               '')
        so3 = SummaryOperation('primary',
                               datetime(2015, 10, 17, 0, 0, 0, 0, pytz.utc),
                               timedelta(days=3), 'dummy_path', '%D [%T] %S',
                               '====')

        et1 = EventTime(True, datetime(2015, 10, 17, 9, 0, 0, 0, pytz.utc))
        et2 = EventTime(True, datetime(2015, 10, 17, 18, 0, 0, 0, pytz.utc))

        et3 = EventTime(False, datetime(2015, 10, 18, 0, 0, 0, 0, pytz.utc))
        et4 = EventTime(True, datetime(2015, 10, 18, 12, 15, 0, 0, pytz.utc))
        et5 = EventTime(True, datetime(2015, 10, 18, 12, 45, 0, 0, pytz.utc))

        et6 = EventTime(False, datetime(2015, 10, 19, 0, 0, 0, 0, pytz.utc))

        et7 = EventTime(False, datetime(2015, 10, 20, 0, 0, 0, 0, pytz.utc))

        ev1 = Event(et1, et2, 'Google I/O 2015', 'Foo Bar', '*****@*****.**')
        ev2 = Event(et3, et6, 'event 2', None, None)
        ev3 = Event(et4, et5, 'event 3', None, None)
        ev4 = Event(et6, et7, 'event 4', None, None)

        self.assertEqual(
            so1._make_output([ev1, ev2, ev3, ev4]), '\n'.join([
                '2015-10-17 %s [09:00-18:00] Google I/O 2015' %
                MSG_WEEK_DAY[5],
                '2015-10-18 %s [%s] event 2' % (MSG_WEEK_DAY[6], MSG_ALL_DAY),
                '2015-10-18 %s [12:15-12:45] event 3' % MSG_WEEK_DAY[6],
                '2015-10-19 %s [%s] event 4' % (MSG_WEEK_DAY[0], MSG_ALL_DAY),
            ]))
        self.assertEqual(
            so2._make_output([ev1, ev2, ev3, ev4]), '\n'.join([
                '2015-10-17 %s [09:00-18:00] Google I/O 2015' %
                MSG_WEEK_DAY[5],
                '',
                '2015-10-18 %s [%s] event 2' % (MSG_WEEK_DAY[6], MSG_ALL_DAY),
                '2015-10-18 %s [12:15-12:45] event 3' % MSG_WEEK_DAY[6],
                '',
                '2015-10-19 %s [%s] event 4' % (MSG_WEEK_DAY[0], MSG_ALL_DAY),
            ]))
        self.assertEqual(
            so3._make_output([ev1, ev2, ev3, ev4]), '\n'.join([
                '2015-10-17 %s [09:00-18:00] Google I/O 2015' %
                MSG_WEEK_DAY[5],
                '====',
                '2015-10-18 %s [%s] event 2' % (MSG_WEEK_DAY[6], MSG_ALL_DAY),
                '2015-10-18 %s [12:15-12:45] event 3' % MSG_WEEK_DAY[6],
                '====',
                '2015-10-19 %s [%s] event 4' % (MSG_WEEK_DAY[0], MSG_ALL_DAY),
            ]))
        self.assertEqual(
            so3._make_output([ev2, ev3]), '\n'.join([
                '2015-10-18 %s [%s] event 2' % (MSG_WEEK_DAY[6], MSG_ALL_DAY),
                '2015-10-18 %s [12:15-12:45] event 3' % MSG_WEEK_DAY[6],
            ]))
        self.assertEqual(so3._make_output([]), '')
예제 #8
0
class TestEvent(unittest.TestCase):
    tz_la = pytz.timezone('America/Los_Angeles')
    tz_tokyo = pytz.timezone('Asia/Tokyo')
    t0 = EventTime(True, tz_la.localize(datetime(2015, 5, 28, 9, 0, 0, 0)))
    t1 = EventTime(True, tz_la.localize(datetime(2015, 5, 28, 17, 0, 0, 0)))
    t2 = EventTime(False, tz_tokyo.localize(datetime(2015, 10, 17, 0, 0, 0,
                                                     0)))
    t3 = EventTime(False, tz_tokyo.localize(datetime(2015, 10, 18, 0, 0, 0,
                                                     0)))
    t4 = EventTime(False, datetime(2015, 10, 18, 0, 0, 0, 0, pytz.utc))

    e0 = Event(t0, t1, 'Google I/O 2015', 'Foo Bar', '*****@*****.**')
    e1 = Event(t2, t3, 'あいうえお', None, None)
    e2 = Event(t2, t3, 'あいうえお', None, '*****@*****.**')
    e3 = Event(t0, t1, 'Google I/O 2016', 'Foo Bar', '*****@*****.**',
               'Mountain View')

    def test_init(self):
        self.assertEqual(self.e0.start_time, self.t0)
        self.assertEqual(self.e0.end_time, self.t1)
        self.assertEqual(self.e0.summary, 'Google I/O 2015')
        self.assertEqual(self.e0.creator_name, 'Foo Bar')
        self.assertEqual(self.e0.creator_email, '*****@*****.**')

        self.assertEqual(self.e1.start_time, self.t2)
        self.assertEqual(self.e1.end_time, self.t3)
        self.assertEqual(self.e1.summary, 'あいうえお')
        self.assertEqual(self.e1.creator_name, None)
        self.assertEqual(self.e1.creator_email, None)

    def test_init_error(self):
        """todo"""

    def test_to_format(self):
        self.assertEqual(self.e0.to_format('[%T] %S%L%C'),
                         '[09:00-17:00] Google I/O 2015 (Foo Bar)')
        self.assertEqual(self.e1.to_format('[%T] %S%L%C'),
                         '[%s] あいうえお' % MSG_ALL_DAY)
        self.assertEqual(self.e2.to_format('[%T] %S%L%C'),
                         '[%s] あいうえお ([email protected])' % MSG_ALL_DAY)
        self.assertEqual(
            self.e3.to_format('[%T] %S%L%C'),
            '[09:00-17:00] Google I/O 2016 @Mountain View (Foo Bar)')

    def test_to_long_summary(self):
        self.assertEqual(
            self.e0.to_long_summary(),
            '2015-05-28 %s [09:00-17:00] Google I/O 2015' % MSG_WEEK_DAY[3])
        self.assertEqual(
            self.e1.to_long_summary(),
            '2015-10-17 %s [%s] あいうえお' % (MSG_WEEK_DAY[5], MSG_ALL_DAY))
        self.assertEqual(
            self.e2.to_long_summary(),
            '2015-10-17 %s [%s] あいうえお' % (MSG_WEEK_DAY[5], MSG_ALL_DAY))

    def test_to_dict(self):
        self.assertEqual(
            self.e0.to_dict(), {
                'summary': 'Google I/O 2015',
                'start': {
                    'dateTime': '2015-05-28T09:00:00-07:00',
                    'timeZone': 'America/Los_Angeles'
                },
                'end': {
                    'dateTime': '2015-05-28T17:00:00-07:00',
                    'timeZone': 'America/Los_Angeles'
                },
                'creator': {
                    'displayName': 'Foo Bar',
                    'email': '*****@*****.**',
                },
            })
        self.assertEqual(
            self.e1.to_dict(), {
                'summary': 'あいうえお',
                'start': {
                    'date': '2015-10-17',
                    'timeZone': 'Asia/Tokyo'
                },
                'end': {
                    'date': '2015-10-18',
                    'timeZone': 'Asia/Tokyo'
                },
            })
        self.assertEqual(
            self.e2.to_dict(), {
                'summary': 'あいうえお',
                'start': {
                    'date': '2015-10-17',
                    'timeZone': 'Asia/Tokyo'
                },
                'end': {
                    'date': '2015-10-18',
                    'timeZone': 'Asia/Tokyo'
                },
                'creator': {
                    'email': '*****@*****.**'
                },
            })

    def test_parse_dict(self):
        self.assertEqual(
            Event.parse_dict(
                {
                    'summary': 'Google I/O 2015',
                    'start': {
                        'dateTime': '2015-05-28T09:00:00-07:00',
                        'timeZone': 'America/Los_Angeles'
                    },
                    'end': {
                        'dateTime': '2015-05-28T17:00:00-07:00',
                        'timeZone': 'America/Los_Angeles'
                    },
                    'creator': {
                        'displayName': 'Foo Bar',
                        'email': '*****@*****.**',
                    },
                }, 'America/Los_Angeles'), self.e0)
        self.assertEqual(
            Event.parse_dict(
                {
                    'summary': 'あいうえお',
                    'start': {
                        'date': '2015-10-17'
                    },
                    'end': {
                        'date': '2015-10-18'
                    },
                }, 'Asia/Tokyo'), self.e1)

        self.assertEqual(
            Event.parse_dict(
                {
                    'summary': 'あいうえお',
                    'start': {
                        'date': '2015-10-17'
                    },
                    'end': {
                        'date': '2015-10-18'
                    },
                    'creator': {
                        'email': '*****@*****.**'
                    },
                }, 'Asia/Tokyo'), self.e2)