Example #1
0
 def populate_obj(self, obj, name):
     field = getattr(obj, name)
     bbox = self.form.bbox.data or None
     filename = self.form.filename.data or None
     if filename and filename in tmp:
         with tmp.open(filename) as infile:
             field.save(infile, filename, bbox=bbox)
         tmp.delete(filename)
Example #2
0
 def populate_obj(self, obj, name):
     field = getattr(obj, name)
     bbox = self.form.bbox.data or None
     filename = self.form.filename.data or None
     if filename and filename in tmp:
         with tmp.open(filename, 'rb') as infile:
             field.save(infile, filename, bbox=bbox)
         tmp.delete(filename)
Example #3
0
def load(filename=DEFAULT_GEOZONES_FILE, drop=False):
    '''
    Load a geozones archive from <filename>

    <filename> can be either a local path or a remote URL.
    '''
    ts = datetime.now().isoformat().replace('-', '').replace(':', '').split('.')[0]
    prefix = 'geozones-{0}'.format(ts)
    if filename.startswith('http'):
        log.info('Downloading GeoZones bundle: %s', filename)
        # Use tmp.open to make sure that the directory exists in FS
        with tmp.open(GEOZONE_FILENAME, 'wb') as newfile:
            newfile.write(requests.get(filename).content)
            filename = tmp.path(GEOZONE_FILENAME)

    log.info('Extracting GeoZones bundle')
    with handle_error(prefix):
        with contextlib.closing(lzma.LZMAFile(filename)) as xz:
            with tarfile.open(fileobj=xz) as f:
                f.extractall(tmp.path(prefix))

    log.info('Loading GeoZones levels')

    log.info('Loading levels.msgpack')
    levels_filepath = tmp.path(prefix + '/levels.msgpack')
    if drop and GeoLevel.objects.count():
        name = '_'.join((GeoLevel._get_collection_name(), ts))
        target = GeoLevel._get_collection_name()
        with switch_collection(GeoLevel, name):
            with handle_error(prefix, GeoLevel):
                total = load_levels(GeoLevel, levels_filepath)
                GeoLevel.objects._collection.rename(target, dropTarget=True)
    else:
        with handle_error(prefix):
            total = load_levels(GeoLevel, levels_filepath)
    log.info('Loaded {total} levels'.format(total=total))

    log.info('Loading zones.msgpack')
    zones_filepath = tmp.path(prefix + '/zones.msgpack')
    if drop and GeoZone.objects.count():
        name = '_'.join((GeoZone._get_collection_name(), ts))
        target = GeoZone._get_collection_name()
        with switch_collection(GeoZone, name):
            with handle_error(prefix, GeoZone):
                total = load_zones(GeoZone, zones_filepath)
                GeoZone.objects._collection.rename(target, dropTarget=True)
    else:
        with handle_error(prefix):
            total = load_zones(GeoZone, zones_filepath)
    log.info('Loaded {total} zones'.format(total=total))

    cleanup(prefix)