def parse(self, fileorpath, addon=None): path = get_filepath(fileorpath) if zipfile.is_zipfile(path): zf = SafeUnzip(path) zf.is_valid() # Raises forms.ValidationError if problems. try: data = zf.extract_path('manifest.webapp') except KeyError: raise forms.ValidationError( _('The file "manifest.webapp" was not found at the root ' 'of the packaged app archive.')) else: file_ = get_file(fileorpath) data = file_.read() file_.close() enc_guess = chardet.detect(data) data = strip_bom(data) try: data = json.loads(data.decode(enc_guess['encoding'])) except (ValueError, UnicodeDecodeError), exc: msg = 'Error parsing webapp %r (encoding: %r %.2f%% sure): %s: %s' log.error( msg % (fileorpath, enc_guess['encoding'], enc_guess['confidence'] * 100.0, exc.__class__.__name__, exc)) raise forms.ValidationError( _('Could not parse webapp manifest file.'))
def get_json_data(self, fileorpath): path = get_filepath(fileorpath) if zipfile.is_zipfile(path): zf = SafeUnzip(path) zf.is_valid() # Raises forms.ValidationError if problems. try: data = zf.extract_path('manifest.webapp') except KeyError: raise forms.ValidationError( _('The file "manifest.webapp" was not found at the root ' 'of the packaged app archive.')) else: file_ = get_file(fileorpath) data = file_.read() file_.close() try: enc_guess = chardet.detect(data) data = strip_bom(data) decoded_data = data.decode(enc_guess['encoding']) except (ValueError, UnicodeDecodeError) as exc: msg = 'Error parsing webapp %r (encoding: %r %.2f%% sure): %s: %s' log.error(msg % (fileorpath, enc_guess['encoding'], enc_guess['confidence'] * 100.0, exc.__class__.__name__, exc)) raise forms.ValidationError( _('Could not decode the webapp manifest file.')) try: return json.loads(decoded_data) except Exception: raise forms.ValidationError( _('The webapp manifest is not valid JSON.'))
def get_json_data(self, fileorpath): path = get_filepath(fileorpath) if zipfile.is_zipfile(path): zf = SafeUnzip(path) zf.is_valid() # Raises forms.ValidationError if problems. try: data = zf.extract_path('manifest.webapp') except KeyError: raise forms.ValidationError( _('The file "manifest.webapp" was not found at the root ' 'of the packaged app archive.')) else: file_ = get_file(fileorpath) data = file_.read() file_.close() try: data = strip_bom(data) # Marketplace only supports UTF-8 encoded manifests. decoded_data = data.decode('utf-8') except (ValueError, UnicodeDecodeError) as exc: msg = 'Error parsing webapp %r (encoding: utf-8): %s: %s' log.error(msg % (fileorpath, exc.__class__.__name__, exc)) raise forms.ValidationError( _('Could not decode the webapp manifest file.')) try: return json.loads(decoded_data) except Exception: raise forms.ValidationError( _('The webapp manifest is not valid JSON.'))
def parse(self, fileorpath, addon=None): f = get_file(fileorpath) data = f.read() enc_guess = chardet.detect(data) data = strip_bom(data) try: data = json.loads(data.decode(enc_guess['encoding'])) except (ValueError, UnicodeDecodeError), exc: msg = 'Error parsing webapp %r (encoding: %r %.2f%% sure): %s: %s' log.error(msg % (f.name, enc_guess['encoding'], enc_guess['confidence'] * 100.0, exc.__class__.__name__, exc)) raise forms.ValidationError( _('Could not parse webapp manifest file.'))
def parse(self, fileorpath, addon=None): filename = get_filepath(fileorpath) with open(filename, 'rb') as f: data = f.read() enc_guess = chardet.detect(data) data = strip_bom(data) try: data = json.loads(data.decode(enc_guess['encoding'])) except (ValueError, UnicodeDecodeError), exc: msg = 'Error parsing webapp %r (encoding: %r %.2f%% sure): %s: %s' log.error(msg % (filename, enc_guess['encoding'], enc_guess['confidence'] * 100.0, exc.__class__.__name__, exc)) raise forms.ValidationError( _('Could not parse webapp manifest file.'))
def decode_manifest(cls, manifest): """ Returns manifest, stripped of BOMs and UTF-8 decoded, as Python dict. """ try: data = strip_bom(manifest) # Marketplace only supports UTF-8 encoded manifests. decoded_data = data.decode("utf-8") except (ValueError, UnicodeDecodeError) as exc: msg = "Error parsing manifest (encoding: utf-8): %s: %s" log.error(msg % (exc.__class__.__name__, exc)) raise forms.ValidationError(_("Could not decode the webapp manifest file.")) try: return json.loads(decoded_data) except Exception: raise forms.ValidationError(_("The webapp manifest is not valid JSON."))
def decode_manifest(cls, manifest): """ Returns manifest, stripped of BOMs and UTF-8 decoded, as Python dict. """ try: data = strip_bom(manifest) # Marketplace only supports UTF-8 encoded manifests. decoded_data = data.decode('utf-8') except (ValueError, UnicodeDecodeError) as exc: msg = 'Error parsing manifest (encoding: utf-8): %s: %s' log.error(msg % (exc.__class__.__name__, exc)) raise forms.ValidationError( _('Could not decode the webapp manifest file.')) try: return json.loads(decoded_data) except Exception: raise forms.ValidationError( _('The webapp manifest is not valid JSON.'))
def fetch_manifest(url, upload_pk=None, **kw): log.info(u'[1@None] Fetching manifest: %s.' % url) upload = FileUpload.objects.get(pk=upload_pk) try: response = _fetch_content(url) no_ct_message = _('Your manifest must be served with the HTTP ' 'header "Content-Type: %s".') wrong_ct_message = _('Your manifest must be served with the HTTP ' 'header "Content-Type: %s". We saw "%s".') check_content_type(response, 'application/x-web-app-manifest+json', no_ct_message, wrong_ct_message) size_error_message = _('Your manifest must be less than %s bytes.') content = get_content_and_check_size(response, settings.MAX_WEBAPP_UPLOAD_SIZE, size_error_message) content = strip_bom(content) check_manifest_encoding(url, content) except Exception, e: # Drop a message in the validation slot and bail. upload.update(validation=failed_validation(e.message)) return
fail(_('Your manifest file was not encoded as valid UTF-8.'), upload=upload) return # Get the individual parts of the content type. ct_split = map(str.strip, ct.split(';')) if len(ct_split) > 1: # Figure out if we've got a charset specified. kv_pairs = dict(tuple(p.split('=', 1)) for p in ct_split[1:] if '=' in p) if 'charset' in kv_pairs and kv_pairs['charset'].lower() != 'utf-8': fail(_("The manifest's encoding does not match the charset " 'provided in the HTTP Content-Type.'), upload=upload) content = strip_bom(content) return content @task @write def fetch_manifest(url, upload_pk=None, **kw): log.info(u'[1@None] Fetching manifest: %s.' % url) upload = FileUpload.objects.get(pk=upload_pk) content = _fetch_manifest(url, upload) if content is None: return upload.add_file([content], url, len(content), is_webapp=True) # Send the upload to the validator.
fail(_('Your manifest file was not encoded as valid UTF-8.'), upload=upload) return # Get the individual parts of the content type. ct_split = map(str.strip, ct.split(';')) if len(ct_split) > 1: # Figure out if we've got a charset specified. kv_pairs = dict( tuple(p.split('=', 1)) for p in ct_split[1:] if '=' in p) if 'charset' in kv_pairs and kv_pairs['charset'].lower() != 'utf-8': fail(_("The manifest's encoding does not match the charset " 'provided in the HTTP Content-Type.'), upload=upload) content = strip_bom(content) return content @task @write def fetch_manifest(url, upload_pk=None, **kw): log.info(u'[1@None] Fetching manifest: %s.' % url) upload = FileUpload.objects.get(pk=upload_pk) content = _fetch_manifest(url, upload) if content is None: return upload.add_file([content], url, len(content), is_webapp=True) # Send the upload to the validator.