def __assertTagsAreNonEmpty(self, xml_root): header_tags = [ FIELD_NAMES_TO_XML_NAMES[field] if field in FIELD_NAMES_TO_XML_NAMES else field for field in common.fields_of(catbase.XmlHeader) if field not in IGNORED_XML_HEADER_CLASS_FIELDS ] mandatory_header_tags = set(header_tags) - set(OPTIONAL_HEADER_TAGS) for header_tag in header_tags: header_tag = header_tag.replace("remixParentsUrlString", "url").replace( "remixGrandparentsUrlString", "remixOf") tag = "header/" + header_tag xml_node = xml_root.find(tag) assert xml_node is not None, "XML file error: tag '{}' must be available".format( tag) if header_tag in mandatory_header_tags: assert xml_node.text is not None, "XML file error: Value for tag '{}' must be set".format( tag) if header_tag == "screenMode": assert xml_node.text == catcommon.ScreenModes.STRETCH.toString( ) # @UndefinedVariable elif header_tag == "remixOf": try: with tempfile.NamedTemporaryFile() as temp: common.download_file(xml_node.text, temp.name) with open(temp.name, "r") as f: assert f.read() is not None except: self.fail("Expection '{}' with url '{}'".format( sys.exc_info()[0], xml_node.text))
def download_project_code(project_id, target_dir): # TODO: consolidate with classes from scratch module from scratchtocatrobat.tools import common from scratchtocatrobat.scratch import scratch project_code_url = helpers.config.get("SCRATCH_API", "project_url_template").format(project_id) project_file_path = os.path.join(target_dir, scratch._PROJECT_FILE_NAME) try: common.download_file(project_code_url, project_file_path) except common.ScratchtobatHTTP404Error as _: _log.error("This seems to be an old Scratch program! Scratch 1.x programs are not supported!")
def run(self): resource_url = self._kwargs["resource_url"] target_dir = self._kwargs["target_dir"] md5_file_name = self._kwargs["md5_file_name"] progress_bar = self._kwargs["progress_bar"] resource_file_path = os.path.join(target_dir, md5_file_name) try: common.download_file(resource_url, resource_file_path) except (SocketTimeoutException, SocketException, UnknownHostException, IOException) as e: raise ScratchWebApiError("Error with {}: '{}'".format(resource_url, e)) verify_hash = helpers.md5_of_file(resource_file_path) assert verify_hash == os.path.splitext(md5_file_name)[0], "MD5 hash of response data not matching" if progress_bar != None: progress_bar.update(ProgressType.DOWNLOAD_MEDIA_FILE)
def __assertTagsAreNonEmpty(self, xml_root): header_tags = [FIELD_NAMES_TO_XML_NAMES[field] if field in FIELD_NAMES_TO_XML_NAMES else field for field in common.fields_of(catbase.XmlHeader) if field not in IGNORED_XML_HEADER_CLASS_FIELDS] mandatory_header_tags = set(header_tags) - set(OPTIONAL_HEADER_TAGS) for header_tag in header_tags: tag = "header/" + header_tag xml_node = xml_root.find(tag) assert xml_node is not None, "XML file error: tag '{}' must be available".format(tag) if header_tag in mandatory_header_tags: assert xml_node.text is not None, "XML file error: Value for tag '{}' must be set".format(tag) if header_tag == "screenMode": assert xml_node.text == catcommon.ScreenModes.STRETCH.toString() # @UndefinedVariable elif header_tag == "remixOf": try: with tempfile.NamedTemporaryFile() as temp: common.download_file(xml_node.text, temp.name) with open(temp.name, "r") as f: assert f.read() is not None except: self.fail("Expection '{}' with url '{}'".format(sys.exc_info()[0], xml_node.text))
def request_project_remixes_for(project_id): global _cached_remix_info_data if project_id in _cached_remix_info_data: _log.debug("Cache hit: Remix tree data!") return _cached_remix_info_data[project_id] from scratchtocatrobat.tools import common scratch_project_remix_tree_url = SCRATCH_PROJECT_REMIX_TREE_URL_TEMPLATE.format( project_id) try: import tempfile with tempfile.NamedTemporaryFile() as tempf: common.download_file(scratch_project_remix_tree_url, tempf.name) tempf.flush() json_data_string = tempf.read() if json_data_string is None: return [] json_data_string = unicode(json_data_string) try: json_data = json.loads(json_data_string) except Exception as e: json_data = [] remix_info = extract_project_remixes_from_data( json_data, project_id) _cached_remix_info_data[project_id] = remix_info return remix_info except Exception as e: _log.warn("Cannot fetch remix tree data: " + str(e)) return None except common.ScratchtobatHTTP404Error as _: _log.error("Cannot fetch remix tree data: HTTP-Status-Code 404") return None
def request_project_remixes_for(project_id): global _cached_remix_info_data if project_id in _cached_remix_info_data: _log.debug("Cache hit: Remix tree data!") return _cached_remix_info_data[project_id] from scratchtocatrobat.tools import common scratch_project_remix_tree_url = SCRATCH_PROJECT_REMIX_TREE_URL_TEMPLATE.format(project_id) try: import tempfile with tempfile.NamedTemporaryFile() as tempf: common.download_file(scratch_project_remix_tree_url, tempf.name) tempf.flush() json_data_string = tempf.read() if json_data_string is None: return [] json_data_string = unicode(json_data_string) try: json_data = json.loads(json_data_string) except Exception as e: json_data = [] remix_info = extract_project_remixes_from_data(json_data, project_id) _cached_remix_info_data[project_id] = remix_info return remix_info except Exception as e: _log.warn("Cannot fetch remix tree data: " + str(e)) return None except common.ScratchtobatHTTP404Error as _: _log.error("Cannot fetch remix tree data: HTTP-Status-Code 404") return None