def fetch_online_directories(self): """Fetch online directory of repositories.""" downloader = NetworkManager(self.DIRECTORY_URL) status, _ = downloader.fetch() if status: directory_file = QTemporaryFile() if directory_file.open(): directory_file.write(downloader.content) directory_file.close() with open(directory_file.fileName()) as csv_file: reader = csv.DictReader(csv_file, fieldnames=('name', 'url')) for row in reader: self._online_directories[row['name']] = row['url'].strip() # Save it to cache settings = QSettings() settings.beginGroup(repo_settings_group()) settings.setValue('online_directories', self._online_directories) settings.endGroup() else: # Just use cache from previous use settings = QSettings() settings.beginGroup(repo_settings_group()) self._online_directories = settings.value('online_directories', {}) settings.endGroup()
def download_collection(self, id, register_name): """Download a collection given its ID. For zip collection, we will download the zip, and extract the collection to collections dir. :param id: The ID of the collection. :type id: str :param register_name: The register name of the collection (the section name of the collection) :type register_name: unicode """ # Download the zip first collection_path = "collections/%s.zip" % register_name network_manager = NetworkManager(self.file_url(collection_path)) status, description = network_manager.fetch() if not status: return False, description # Create the zip file zip_file = QTemporaryFile() if zip_file.open(): zip_file.write(network_manager.content) zip_file.close() zf = ZipFile(zip_file.fileName()) zf.extractall(path=local_collection_path(id)) return True, None
def _end_of_request(self): tf = QTemporaryFile(os.path.join(QDir.tempPath(), 'request-XXXXXX.osm')) tf.setAutoRemove(False) tf.open(QIODevice.WriteOnly | QIODevice.Text) tf.write(self.network_reply.readAll().simplified()) tf.close() self.result_path = tf.fileName() self.loop.quit()
def finalize(self): archive_info = { "cache_size_multiply": 0, "levels": [], 'max_level': max(self.levels.keys()), 'min_level': min(self.levels.keys()), "name": self.__layerName, "renderer_properties": { "alpha": 255, "antialias": True, "brightness": 0, "contrast": 1, "dither": True, "filterbitmap": True, "greyscale": False, "type": "tms_renderer" }, "tms_type": 2, "type": 32, "visible": True } for level, coords in self.levels.items(): level_json = { "level": level, "bbox_maxx": max(coords["x"]), "bbox_maxy": max(coords["y"]), "bbox_minx": min(coords["x"]), "bbox_miny": min(coords["y"]), } archive_info["levels"].append(level_json) tempFile = QTemporaryFile() tempFile.setAutoRemove(False) tempFile.open(QIODevice.WriteOnly) tempFile.write(json.dumps(archive_info)) tempFileName = tempFile.fileName() tempFile.close() self.zipFile.write(tempFileName, "%s.json" % self.rootDir) ZipWriter.finalize(self)
def fetch_online_directories(self): """Fetch online directory of repositories.""" downloader = NetworkManager(self.DIRECTORY_URL) status, _ = downloader.fetch() if status: directory_file = QTemporaryFile() if directory_file.open(): directory_file.write(downloader.content) directory_file.close() with open(directory_file.fileName()) as csv_file: reader = csv.DictReader(csv_file, fieldnames=("name", "url")) for row in reader: repName = row["name"] repUrl = row["url"] # Check name and URL for None before stripping and adding if repName is not None and repUrl is not None: self._online_directories[row["name"]] = repUrl.strip() else: if repName is None: # No name LOGGER.warning("Missing name for repository" " - not added") else: # No URL LOGGER.warning( "Missing URL for repository" + str(row["name"]) + " - not added" ) # Save to settings settings = QgsSettings() settings.beginGroup(repo_settings_group()) settings.setValue("online_directories", self._online_directories) settings.endGroup() else: # Use settings settings = QgsSettings() settings.beginGroup(repo_settings_group()) self._online_directories = settings.value("online_directories", {}) settings.endGroup()