Ejemplo n.º 1
0
    def test_thingformatter_edges(self):
        # Altitude will fail with KeyError and trigger the error path
        f = util.ThingFormatter({'properties': {}})
        self.assertEqual('ERROR', f['altitude'])

        # General property fallback, missing key
        f = util.ThingFormatter({})
        self.assertEqual('UNSUPPORTED', f['snarf'])
Ejemplo n.º 2
0
 def test_thingformatter_keys(self):
     self.assertEqual(
         sorted([
             'title', 'created', 'updated', 'id', 'altitude', 'public',
             'notes'
         ]),
         sorted(util.ThingFormatter({
             'properties': {
                 'notes': ''
             }
         }).keys))
Ejemplo n.º 3
0
    def test_thingformatter_wpt(self):
        f = util.ThingFormatter(TEST_WPT)
        self.assertEqual('TestPoint 8873927a-820b-4a75-b15a-c3e40d383006',
                         '%(title)s %(id)s' % f)

        # Complex type formatting
        self.assertEqual('45.5 / -122.7',
                         '%(latitude).1f / %(longitude).1f' % f)

        # Implicit "anything in properties" should also work
        self.assertEqual('TestPoint True', '%(title)s %(is_active)s' % f)
Ejemplo n.º 4
0
 def test_thingformatter_trk(self):
     f = util.ThingFormatter(TEST_TRK)
     self.assertEqual('ph-closed2 b0298a9a30b073b3493ca54e3a1417bb',
                      '%(title)s %(id)s' % f)
     self.assertEqual('0000', '%(moving_speed)04i' % f)
Ejemplo n.º 5
0
    def list(self, args):
        if args.format and args.format.lower() == 'help':
            msg = [
                '--format takes a python-like format string, such as: ', '',
                '    %(title)s: %(latitude).4f,%(longitude).5f', '',
                'which might display something like this:', '',
                '    My Campsite: 45.1234,-122.98765', '',
                'Where the field name can be anything in "properties" ',
                'for an object (as displayed by the "show" command). ',
                'Also, the following special format keys are available: ', ''
            ]
            fmt = util.ThingFormatter({})
            for key in fmt.keys:
                doc = getattr(fmt, 'format_%s' % key).__doc__
                msg.append(' - %s: %s' % (key, doc))

            msg.extend([
                '',
                'Note that using --format causes an API call for each item ',
                'in a list in order to fetch the full set of properties. ',
                'Please limit the list in some way to avoid undue stress ',
                'on gaiagps.com.'
            ])

            print(os.linesep.join(msg))
            return 0

        folder_filter = self.folder_filter(args.in_folder)

        if args.by_id:
            return self.idlist(args)

        objtype = self.objtype
        folders = {}

        def get_folder(ident):
            if not folders:
                folders.update(
                    {f['id']: f
                     for f in self.client.list_objects('folder')})
            return folders[ident]

        if args.archived is not None:
            show_archived = args.archived
            only_archived = show_archived
        else:
            show_archived = True
            only_archived = False

        items = self.client.list_objects(objtype, archived=show_archived)
        for item in items:
            folder = (item['folder'] and get_folder(item['folder'])['title']
                      or '')
            item['folder_name'] = folder

        table = prettytable.PrettyTable(['Name', 'Updated', 'Folder'])

        def sortkey(i):
            return i['folder_name'] + ' ' + i['title']

        for item in sorted(folder_filter(items), key=sortkey):
            if args.match and not re.search(args.match, item['title']):
                continue
            if args.match_date and not self._match_date(item, args.match_date):
                continue
            if only_archived and not item['deleted']:
                continue
            if args.format:
                # This is unfortunately very heavy, but since we do not seem to
                # be able to get whole objects in list format, this is really
                # the only option at the moment.
                item = self.get_object(item['id'])
                print(args.format % util.ThingFormatter(item))
            else:
                table.add_row(
                    [item['title'],
                     util.datefmt(item), item['folder_name']])
        if not args.format:
            print(table)