Beispiel #1
0
    def test_hex2rgb(self):
        """ hex2rgb should translate well-formed codes, and raise on errors.
        """
        testargs = (('',), ('00 0000',))
        for argset in testargs:
            with self.assertRaises(
                    ValueError,
                    msg=test_msg('Failed to raise.', *argset)):
                _ = hex2rgb(*argset)

        argset = ('#f00',)
        kwset = {'allow_short': False}
        with self.assertRaises(
                ValueError,
                msg=test_msg('Failed to raise without.', *argset, **kwset)):
            _ = hex2rgb(*argset, **kwset)  # noqa

        argset = (' #FF0000',)
        self.assertTupleEqual(
            hex2rgb(*argset),
            (255, 0, 0),
            msg=test_msg('Failed to convert padded hex string.', *argset))

        argset = ('#f00',)
        kwset = {'allow_short': True}
        self.assertTupleEqual(
            hex2rgb(*argset, **kwset),
            (255, 0, 0),
            msg=test_msg(
                'Failed to convert short form hex string.', *argset, **kwset))
Beispiel #2
0
    def test_hex2rgb(self):
        """ hex2rgb should translate well-formed codes, and raise on errors.
        """
        testargs = (('', ), ('00 0000', ))
        for argset in testargs:
            with self.assertRaises(ValueError,
                                   msg=call_msg('Failed to raise.', *argset)):
                _ = hex2rgb(*argset)

        argset = ('#f00', )
        kwset = {'allow_short': False}
        with self.assertRaises(ValueError,
                               msg=call_msg('Failed to raise without.',
                                            *argset, **kwset)):
            _ = hex2rgb(*argset, **kwset)  # noqa

        argset = (' #FF0000', )
        self.assertTupleEqual(hex2rgb(*argset), (255, 0, 0),
                              msg=call_msg(
                                  'Failed to convert padded hex string.',
                                  *argset))

        argset = ('#f00', )
        kwset = {'allow_short': True}
        self.assertTupleEqual(hex2rgb(*argset, **kwset), (255, 0, 0),
                              msg=call_msg(
                                  'Failed to convert short form hex string.',
                                  *argset, **kwset))
Beispiel #3
0
    def test_trans(self):
        """ Translation functions should translate codes properly. """
        for v in self.conversions:
            # hex2*
            argset = (v['hexval'],)
            self.assertTupleEqual(
                v['rgb'],
                hex2rgb(*argset),
                msg=test_msg('Failed to translate.', *argset))

            argset = (v['hexval'],)
            self.assertEqual(
                v['code'],
                hex2term(*argset),
                msg=test_msg('Failed to translate.', *argset))

            argset = (v['hexval'],)
            self.assertEqual(
                v['hexval'],
                hex2termhex(*argset),
                msg=test_msg('Failed to translate.', *argset))

            # rgb2*
            argset = v['rgb']
            self.assertEqual(
                v['hexval'],
                rgb2hex(*argset),
                msg=test_msg('Failed to translate.', *argset))

            argset = v['rgb']
            self.assertEqual(
                v['code'],
                rgb2term(*argset),
                msg=test_msg('Failed to translate.', *argset))

            argset = v['rgb']
            self.assertEqual(
                v['hexval'],
                rgb2termhex(*argset),
                msg=test_msg('Failed to translate.', *argset))

            # term2*
            argset = (v['code'],)
            self.assertEqual(
                v['hexval'],
                term2hex(*argset),
                msg=test_msg('Failed to translate.', *argset))

            argset = (v['code'],)
            self.assertTupleEqual(
                v['rgb'],
                term2rgb(*argset),
                msg=test_msg('Failed to translate.', *argset))

            for hexval, termhex in self.termhex_close:
                argset = (hexval,)
                self.assertEqual(
                    termhex,
                    hex2termhex(*argset),
                    msg=test_msg('Failed on close match.', *argset))
Beispiel #4
0
    def test_trans(self):
        """ Translation functions should translate codes properly. """
        for v in self.conversions:
            # hex2*
            argset = (v['hexval'], )
            self.assertTupleEqual(v['rgb'],
                                  hex2rgb(*argset),
                                  msg=call_msg('Failed to translate.',
                                               *argset))

            argset = (v['hexval'], )
            self.assertEqual(v['code'],
                             hex2term(*argset),
                             msg=call_msg('Failed to translate.', *argset))

            argset = (v['hexval'], )
            self.assertEqual(v['hexval'],
                             hex2termhex(*argset),
                             msg=call_msg('Failed to translate.', *argset))

            # rgb2*
            argset = v['rgb']
            self.assertEqual(v['hexval'],
                             rgb2hex(*argset),
                             msg=call_msg('Failed to translate.', *argset))

            argset = v['rgb']
            self.assertEqual(v['code'],
                             rgb2term(*argset),
                             msg=call_msg('Failed to translate.', *argset))

            argset = v['rgb']
            self.assertEqual(v['hexval'],
                             rgb2termhex(*argset),
                             msg=call_msg('Failed to translate.', *argset))

            # term2*
            argset = (v['code'], )
            self.assertEqual(v['hexval'],
                             term2hex(*argset),
                             msg=call_msg('Failed to translate.', *argset))

            argset = (v['code'], )
            self.assertTupleEqual(v['rgb'],
                                  term2rgb(*argset),
                                  msg=call_msg('Failed to translate.',
                                               *argset))

            for hexval, termhex in self.termhex_close:
                argset = (hexval, )
                self.assertEqual(termhex,
                                 hex2termhex(*argset),
                                 msg=call_msg('Failed on close match.',
                                              *argset))
Beispiel #5
0
    def test_hex_colors(self):
        """ colr tool should recognize hex colors. """
        argd = {'TEXT': 'Hello World', 'FORE': 'd7d7d7'}
        for _ in range(10):
            argd['FORE'] = r.choice(self.valid_hex_vals)
            argd['BACK'] = r.choice(self.valid_hex_vals)
            self.assertMain(argd, msg='main() failed on hex colors.')

        # Without -T, close matches should be used.
        argd = {'TEXT': 'Hello World', '--truecolor': False}
        hexvals = {
            '010203': '000000',
            '040506': '000000',
        }
        for hexval, closematch in hexvals.items():
            argd['FORE'] = hexval
            self.assertEqual(
                get_colr(argd['TEXT'], self.make_argd(argd)),
                Colr(argd['TEXT'], closematch),
                msg='Hex value close match failed without --truecolor.',
            )
        # With -T, rgb mode should be used.
        argd = {'TEXT': 'Hello World', '--truecolor': True}
        hexvals = (
            '010203',
            '040506',
        )
        for hexval in hexvals:
            argd['FORE'] = hexval
            self.assertEqual(
                get_colr(argd['TEXT'], self.make_argd(argd)),
                Colr(argd['TEXT'], hex2rgb(argd['FORE'])),
                msg='Hex value failed with --truecolor.',
            )
        # Invalid color values should raise a InvalidColr.
        argd = {'TEXT': 'Hello World', '--truecolor': False}
        badargsets = (
            {
                'FORE': 'ffooll',
                'BACK': r.choice(self.valid_hex_vals)
            },
            {
                'BACK': 'oopsie',
                'FORE': r.choice(self.valid_hex_vals)
            },
        )
        for argset in badargsets:
            argd.update(argset)
            with self.assertRaises(InvalidColr):
                self.run_main_test(argd, should_fail=True)
Beispiel #6
0
def main():
    """
    retrieve colorIDs from Google Calendar API and display them.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('gcalendar', 'v3', credentials=creds)
    colors = service.colors().get().execute()

    # Print available calendarListEntry colors.
    for id, color in colors['gcalendar'].items():
        # print('colorId: %s' % id)
        # print('Background: %s' % color['background'])
        # print('Foreground: %s' % color['foreground'])
        # use colr library to print coloured text in terminal
        print(
            co('###colorId: {}###'.format(id),
               fore=hex2rgb(color['foreground']),
               back=hex2rgb(color['background'])))