Example #1
0
    def add(self, args):
        if args.existing_folder:
            folder = self.get_object(args.existing_folder, objtype='folder')
        else:
            folder = None

        self.verbose('Creating folder %r' % args.name)
        if not args.dry_run:
            new_folder = self.client.create_object('folder',
                                                   util.make_folder(args.name))
        else:
            new_folder = {'id': 'dry-run'}
        if not new_folder:
            print('Failed to add folder')
            return 1

        if folder:
            self.verbose('Adding folder %r to folder %r' %
                         (args.name, args.existing_folder))
            if not args.dry_run:
                updated = self.client.add_object_to_folder(
                    folder['id'], 'folder', new_folder['id'])
                if not updated:
                    print('Created folder, but failed to add it to '
                          'existing folder')
                    return 1

        if args.dry_run:
            print('Dry run; no action taken')
Example #2
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'])
Example #3
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()
Example #4
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'])
Example #5
0
    def add(self, args):
        try:
            args.latitude = util.validate_lat(args.latitude)
            args.longitude = util.validate_lon(args.longitude)
            args.altitude = util.validate_alt(args.altitude)
        except ValueError as e:
            print('Unable to add waypoint: %r' % e)
            return 1

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

        if args.icon and args.icon in util.ICON_ALIASES:
            args.icon = util.ICON_ALIASES[args.icon]

        self.verbose('Creating waypoint %r' % args.name)
        if not args.dry_run:
            wpt = self.client.create_object(
                'waypoint',
                util.make_waypoint(args.name,
                                   args.latitude,
                                   args.longitude,
                                   alt=args.altitude,
                                   notes=args.notes,
                                   icon=args.icon))
        else:
            wpt = {'id': 'dry-run'}

        if not wpt:
            print('Failed to create waypoint')
            return 1

        if args.new_folder:
            self.verbose('Creating new folder %r' % args.new_folder)
            if not args.dry_run:
                folder = self.client.create_object(
                    'folder', util.make_folder(args.new_folder))
            else:
                folder = {'properties': {'name': args.new_folder}}
        if folder:
            self.verbose('Adding waypoint %r to folder %r' %
                         (args.name, folder['properties']['name']))
            if not args.dry_run:
                self.client.add_object_to_folder(folder['id'], 'waypoint',
                                                 wpt['id'])

        if args.dry_run:
            print('Dry run; no action taken')
Example #6
0
    def test_get_as_gpx(self):
        test_objs = []
        for name in ('test1', 'test2'):
            wpt = self.api.create_object(
                'waypoint', util.make_waypoint(_test_name(name), 45.0, -122.0))
            test_objs.append(wpt)

        fld = self.api.create_object('folder',
                                     util.make_folder(_test_name('folder')))
        for obj in test_objs:
            self.api.add_object_to_folder(fld['id'], 'waypoint', obj['id'])

        gpx_data = self.api.get_object('folder', id_=fld['id'],
                                       fmt='gpx').decode()
        self.assertIn('gpx.xsd', gpx_data)
        self.assertIn('test1', gpx_data)
        self.assertIn('test2', gpx_data)
        self._clean()
Example #7
0
    def add(self, args):
        if args.existing_folder:
            folder = self.get_object(args.existing_folder,
                                     objtype='folder')
        else:
            folder = None

        new_folder = self.client.create_object('folder',
                                               util.make_folder(args.name))
        if not new_folder:
            print('Failed to add folder')
            return 1

        if folder:
            if not self.client.add_object_to_folder(folder['id'], 'folder',
                                                    new_folder['id']):
                print('Created folder, but failed to add it to '
                      'existing folder')
                return 1
Example #8
0
    def add(self, args):
        try:
            args.latitude = util.validate_lat(args.latitude)
            args.longitude = util.validate_lon(args.longitude)
            args.altitude = util.validate_alt(args.altitude)
        except ValueError as e:
            print('Unable to add waypoint: %r' % e)
            return 1

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

        self.verbose('Creating waypoint %r' % args.name)
        wpt = self.client.create_object('waypoint',
                                        util.make_waypoint(args.name,
                                                           args.latitude,
                                                           args.longitude,
                                                           args.altitude))

        if not wpt:
            print('Failed to create waypoint')
            return 1

        if args.new_folder:
            self.verbose('Creating new folder %r' % args.new_folder)
            folder = self.client.create_object(
                'folder',
                util.make_folder(args.new_folder))

        if folder:
            self.verbose('Adding waypoint %r to folder %r' % (
                args.name, folder['properties']['name']))
            self.client.add_object_to_folder(
                folder['id'], 'waypoint', wpt['id'])
Example #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'])