Ejemplo n.º 1
0
    def find_objects(self,
                     names_or_ids,
                     objtype=None,
                     match=False,
                     date_range=None,
                     allow_missing=False):
        matched_objs = []
        objs = self.client.list_objects(objtype or self.objtype)
        if names_or_ids:
            for name_or_id in names_or_ids:
                if util.is_id(name_or_id):
                    matched_objs.append(apiclient.find(objs, 'id', name_or_id))
                elif match:
                    matched_objs.extend(
                        apiclient.match(objs, 'title', name_or_id))
                else:
                    try:
                        matched_objs.append(
                            apiclient.find(objs, 'title', name_or_id))
                    except apiclient.NotFound:
                        if not allow_missing:
                            raise
        else:
            matched_objs = objs

        if date_range:
            matched_objs = [
                x for x in matched_objs if self._match_date(x, date_range)
            ]

        if not names_or_ids and len(matched_objs) == len(objs):
            # Refuse to find all objects because no criteria was specified
            raise _Safety()

        return matched_objs
Ejemplo n.º 2
0
 def find_objects(self, names_or_ids, objtype=None, match=False):
     matched_objs = []
     objs = self.client.list_objects(objtype or self.objtype)
     for name_or_id in names_or_ids:
         if util.is_id(name_or_id):
             matched_objs.append(apiclient.find(objs, 'id', name_or_id))
         elif match:
             matched_objs.extend(apiclient.match(objs, 'title', name_or_id))
         else:
             matched_objs.append(apiclient.find(objs, 'title', name_or_id))
     return matched_objs
Ejemplo n.º 3
0
    def default(self, args):
        log = logging.getLogger('upload')

        if args.existing_folder:
            dst_folder = self.get_object(args.existing_folder,
                                         objtype='folder')
        else:
            dst_folder = None
        new_folder = self.client.upload_file(args.filename)

        log.debug(new_folder)
        log.info('Uploaded file to new folder %s/%s' % (
            new_folder['properties']['name'],
            new_folder['id']))

        if args.new_folder:
            dst_folder = self.client.create_object('folder',
                                                   util.make_folder(
                                                       args.new_folder))
            if not dst_folder:
                print('Uploaded file, but failed to create folder %s' % (
                    args.new_folder))
                return 1

        if dst_folder:
            # I want that...other version of a folder
            folders = self.client.list_objects('folder')
            new_folder_desc = apiclient.find(folders, 'id', new_folder['id'])
            dst_folder_desc = apiclient.find(folders, 'id', dst_folder['id'])

            log.info('Moving contents of %s to %s' % (
                new_folder['properties']['name'],
                dst_folder['properties']['name']))

            for waypoint in new_folder_desc['waypoints']:
                log.info('Moving waypoint %s' % waypoint)
                dst_folder_desc['waypoints'].append(waypoint)
            for track in new_folder_desc['tracks']:
                log.info('Moving track %s' % track)
                dst_folder_desc['tracks'].append(track)
            updated_dst = self.client.put_object('folder', dst_folder_desc)
            log.info('Updated destination folder %s' % (
                dst_folder['properties']['name']))
            if not updated_dst:
                print('Failed to move tracks and waypoints from '
                      'upload folder %s to requested folder %s' % (
                          new_folder['properties']['name'],
                          dst_folder['properties']['name']))
            log.info('Deleting temporary folder %s' % (
                new_folder['properties']['name']))
            self.client.delete_object('folder', new_folder['id'])
Ejemplo n.º 4
0
    def test_upload_file(self):
        tmpdir = tempfile.mkdtemp()
        filename = _test_name('file.gpx')
        path = os.path.join(tmpdir, filename)
        waypoint_name = _test_name('point')
        with open(path, 'w') as f:
            f.write(SAMPLE_GPX % waypoint_name)

        new_folder = self.api.upload_file(path)

        # I don't understand the distinction between these structures
        folders = self.api.list_objects('folder')
        new_folder = apiclient.find(folders, 'id', new_folder['id'])

        waypoints = self.api.list_objects('waypoint')
        waypoint = apiclient.find(waypoints, 'title', waypoint_name)
        self.assertIn(waypoint['id'], new_folder['waypoints'])
        self.api.delete_object('folder', new_folder['id'])
Ejemplo n.º 5
0
 def test_create_delete_folder(self):
     name = _test_name('folder')
     folder = self.api.create_object('folder', util.make_folder(name))
     self.assertIn('id', folder)
     self.assertEqual(name, folder['properties']['name'])
     folders = self.api.list_objects('folder')
     the_folder = apiclient.find(folders, 'id', folder['id'])
     self.assertEqual(name, the_folder['title'])
     self.assertEqual(folder['id'], the_folder['id'])
     self._clean()
Ejemplo n.º 6
0
 def test_create_waypoint(self):
     name = test_name('waypoint')
     wpt = self.api.create_object('waypoint',
                                  util.make_waypoint(name, 45.0, -122.0))
     self.assertIn('id', wpt)
     self.assertEqual(name, wpt['properties']['title'])
     waypoints = self.api.list_objects('waypoint')
     the_wpt = apiclient.find(waypoints, 'id', wpt['id'])
     self.assertEqual(name, the_wpt['title'])
     self.assertEqual(wpt['id'], the_wpt['id'])
     self._clean()
Ejemplo n.º 7
0
    def test_folder_move_ops(self):
        test_objs = {}
        for i, name in enumerate(['wpt1', 'wpt2']):
            wpt = self.api.create_object(
                'waypoint',
                util.make_waypoint(_test_name(name), 45.0 + i, -122.0 - i))
            test_objs[name] = wpt
        for name in ('folder', 'subfolder'):
            fld = self.api.create_object('folder',
                                         util.make_folder(_test_name(name)))
            test_objs[name] = fld

        self.api.add_object_to_folder(test_objs['folder']['id'], 'waypoint',
                                      test_objs['wpt1']['id'])
        self.api.add_object_to_folder(test_objs['subfolder']['id'], 'waypoint',
                                      test_objs['wpt2']['id'])

        # Make sure the waypoints are in their respective folders
        folders = self.api.list_objects('folder')
        folder = apiclient.find(folders, 'id', test_objs['folder']['id'])
        self.assertEqual([test_objs['wpt1']['id']], folder['waypoints'])
        subfolder = apiclient.find(folders, 'id', test_objs['subfolder']['id'])
        self.assertEqual([test_objs['wpt2']['id']], subfolder['waypoints'])

        self.api.add_object_to_folder(test_objs['folder']['id'], 'folder',
                                      test_objs['subfolder']['id'])

        # Make sure the waypoints are in their respective folders, and that
        # the subfolder is in the main folder
        folders = self.api.list_objects('folder')
        folder = apiclient.find(folders, 'id', test_objs['folder']['id'])
        self.assertEqual([test_objs['wpt1']['id']], folder['waypoints'])
        self.assertEqual([test_objs['subfolder']['id']], folder['children'])
        subfolder = apiclient.find(folders, 'id', test_objs['subfolder']['id'])
        self.assertEqual([test_objs['wpt2']['id']], subfolder['waypoints'])

        # Move a waypoint out of its folder
        self.api.remove_object_from_folder(test_objs['subfolder']['id'],
                                           'waypoint', test_objs['wpt2']['id'])
        folders = self.api.list_objects('folder')
        subfolder = apiclient.find(folders, 'id', test_objs['subfolder']['id'])
        self.assertEqual([], subfolder['waypoints'])

        # Delete the top-level folder and make sure everything is gone,
        # not including wpt2, which was moved out to the root
        self.api.delete_object('folder', test_objs['folder']['id'])
        folders = self.api.list_objects('folder')
        self.assertRaises(apiclient.NotFound, apiclient.find, folders, 'id',
                          test_objs['folder']['id'])
        self.assertRaises(apiclient.NotFound, apiclient.find, folders, 'id',
                          test_objs['subfolder']['id'])
        waypoints = self.api.list_objects('waypoint')
        self.assertRaises(apiclient.NotFound, apiclient.find, waypoints, 'id',
                          test_objs['wpt1']['id'])
        wpt2 = apiclient.find(waypoints, 'id', test_objs['wpt2']['id'])
        self.api.delete_object('waypoint', wpt2['id'])
Ejemplo n.º 8
0
 def test_create_waypoint(self):
     name = _test_name('waypoint')
     wpt = self.api.create_object(
         'waypoint',
         util.make_waypoint(name, 45.0, -122.0,
                            alt=123,
                            notes='These are the notes',
                            icon='chemist-24.png'))
     self.assertIn('id', wpt)
     self.assertEqual(name, wpt['properties']['title'])
     waypoints = self.api.list_objects('waypoint')
     the_wpt = apiclient.find(waypoints, 'id', wpt['id'])
     self.assertEqual(name, the_wpt['title'])
     self.assertEqual(wpt['id'], the_wpt['id'])
     self.assertEqual(wpt['properties']['icon'], 'chemist-24.png')
     self.assertEqual(wpt['properties']['notes'], 'These are the notes')
     self._clean()
Ejemplo n.º 9
0
    def default(self, args):
        log = logging.getLogger('upload')

        if args.strip_gpx_extensions:
            tmpfile = os.path.join(
                os.path.dirname(args.filename),
                'clean-%s' % os.path.basename(args.filename))
            self.verbose('Stripping GPX extensions from input file')
            util.strip_gpx_extensions(args.filename, tmpfile)
            args.filename = tmpfile

        if args.existing_folder:
            dst_folder = self.get_object(args.existing_folder,
                                         objtype='folder')
        else:
            dst_folder = None

        new_folder = self.client.upload_file(args.filename)

        if not new_folder and args.poll:
            new_folder = self._poll_for_upload(os.path.basename(args.filename))

        if not new_folder:
            print('File upload has been queued at the server and '
                  'may take time to appear.')
            if dst_folder:
                print('Unable to move to destination folder until '
                      'processing is complete.')
            return

        log.debug(new_folder)
        log.info('Uploaded file to new folder %s/%s' % (
            new_folder['properties']['name'],
            new_folder['id']))

        if args.colorize_tracks:
            track_cmd = track.Track(self.client, verbose=args.verbose)
            args.name = []
            args.match = None
            args.random = None
            args.dry_run = None
            args.from_gpx_file = args.filename
            args.in_folder = new_folder['properties']['name']
            try:
                track_cmd.colorize(args)
            except Exception as e:
                log.debug(traceback.format_exc())
                print('Failed to colorize track: %s' % e)

        if args.new_folder:
            dst_folder = self.client.create_object('folder',
                                                   util.make_folder(
                                                       args.new_folder))
            if not dst_folder:
                print('Uploaded file, but failed to create folder %s' % (
                    args.new_folder))
                return 1

        if dst_folder:
            # I want that...other version of a folder
            folders = self.client.list_objects('folder')
            new_folder_desc = apiclient.find(folders, 'id', new_folder['id'])
            dst_folder_desc = apiclient.find(folders, 'id', dst_folder['id'])

            log.info('Moving contents of %s to %s' % (
                new_folder['properties']['name'],
                dst_folder['properties']['name']))

            for waypoint in new_folder_desc['waypoints']:
                log.info('Moving waypoint %s' % waypoint)
                dst_folder_desc['waypoints'].append(waypoint)
            for t in new_folder_desc['tracks']:
                log.info('Moving track %s' % t)
                dst_folder_desc['tracks'].append(t)
            updated_dst = self.client.put_object('folder', dst_folder_desc)
            log.info('Updated destination folder %s' % (
                dst_folder['properties']['name']))
            if not updated_dst:
                print('Failed to move tracks and waypoints from '
                      'upload folder %s to requested folder %s' % (
                          new_folder['properties']['name'],
                          dst_folder['properties']['name']))
                return 1
            log.info('Deleting temporary folder %s' % (
                new_folder['properties']['name']))
            self.client.delete_object('folder', new_folder['id'])