Commit fffa78ed authored by keoma's avatar keoma

Merge branch 'to-python3' into 'master'

to python3

See merge request !1
parents 3f751cc1 fe115822
__version__ = '0.2.0'
__version__ = '0.3.0'
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
......@@ -6,13 +6,13 @@ import json
from ispformat.validator import validate_isp
if len(sys.argv) == 2:
j=json.load(open(sys.argv[1]))
errs=list(validate_isp(j))
j = json.load(open(sys.argv[1]))
errs = list(validate_isp(j))
if errs:
for e in errs:
print e
print(e)
else:
print "All clear"
print("All clear")
else:
print "Usage: validator.py file-name.json"
print("Usage: validator.py file-name.json")
from ispformat.validator.schemavalidator import *
from ispformat.validator.schemavalidator import *
\ No newline at end of file
......@@ -3,7 +3,7 @@
import ispformat.schema as _schema
from jsonschema import Draft4Validator, RefResolver, draft4_format_checker
from jsonschema.exceptions import RefResolutionError, ValidationError
from urlparse import urlsplit
from urllib.parse import urlsplit
class MyRefResolver(RefResolver):
......@@ -12,12 +12,12 @@ class MyRefResolver(RefResolver):
raise RefResolutionError("Refusing to resolve remote URI: '{}'".format(uri))
geojson_allowed_types=('Polygon', 'MultiPolygon')
geojson_allowed_types = ('Polygon', 'MultiPolygon')
def validate_geojson_type(d):
"""
Make sure a geojson dict only contains allowed geometry types
"""
type_=d.get('type')
type_ = d.get('type')
if type_ not in geojson_allowed_types:
return False
return True
......@@ -35,7 +35,7 @@ def validate_geojson(geodict):
format_checker=draft4_format_checker,
)
for err in v.iter_errors(geodict):
for _ in v.iter_errors(geodict):
return False
if not validate_geojson_type(geodict):
......@@ -44,18 +44,18 @@ def validate_geojson(geodict):
return True
def validate_isp(jdict):
def validate_isp(jdict: dict):
"""
Validate a json-object against the isp json-schema
"""
if not 'version' in jdict:
if 'version' not in jdict:
raise ValidationError(u'version is a required property')
try:
schema=_schema.versions[jdict['version']]
schema = _schema.versions[jdict['version']]
except (AttributeError, TypeError, KeyError):
raise ValidationError(u'version %r unsupported'%jdict['version'])
raise ValidationError(u'version %r unsupported' % jdict['version'])
v=Draft4Validator(
v = Draft4Validator(
schema,
resolver=MyRefResolver.from_schema(schema, store=_schema.deps_for_version(jdict['version'])),
format_checker=draft4_format_checker,
......@@ -66,7 +66,7 @@ def validate_isp(jdict):
def is_valid_url(u):
try:
pu=urlsplit(u)
pu = urlsplit(u)
except:
return False
if pu.scheme not in ('', 'http', 'https'):
......@@ -76,35 +76,37 @@ def validate_isp(jdict):
return True
if 'website' in jdict and not is_valid_url(jdict['website']):
yield ValidationError(u'%r must be an absolute HTTP URL'%u'website',
yield ValidationError(u'%r must be an absolute HTTP URL' % u'website',
instance=jdict[u'website'], schema=schema[u'properties'][u'website'],
path=[u'website'], schema_path=[u'properties', u'website', u'description'],
validator=u'validate_url', validator_value=jdict['website'])
if 'logoURL' in jdict and not is_valid_url(jdict['logoURL']):
yield ValidationError(u'%r must be an absolute HTTP URL'%u'logoURL',
yield ValidationError(u'%r must be an absolute HTTP URL' % u'logoURL',
instance=jdict[u'logoURL'], schema=schema[u'properties'][u'logoURL'],
path=[u'logoURL'], schema_path=[u'properties', u'logoURL', u'description'],
validator=u'validate_url', validator_value=jdict['logoURL'])
sch=schema[u'properties'][u'otherWebsites'][u'patternProperties'][u'^.+$']
for name, url in jdict.get('otherWebsites', {}).iteritems():
sch = schema[u'properties'][u'otherWebsites'][u'patternProperties'][u'^.+$']
for name, url in jdict.get('otherWebsites', {}).items():
if is_valid_url(url):
continue
yield ValidationError(u'%r must be an absolute HTTP URL'%name,
instance=url, schema=sch, path=[u'otherWebsite', name],
schema_path=[u'properties', u'otherWebsites', u'patternProperties', u'^.+$', 'description'],
validator=u'validate_url', validator_value=url)
yield ValidationError(
u'%r must be an absolute HTTP URL' % name,
instance=url, schema=sch, path=[u'otherWebsite', name],
schema_path=[u'properties', u'otherWebsites', u'patternProperties', u'^.+$', 'description'],
validator=u'validate_url', validator_value=url
)
for i, ca in enumerate(jdict.get('coveredAreas', [])):
area=ca.get('area')
area = ca.get('area')
if area and validate_geojson_type(area):
continue
elif not area:
continue
yield ValidationError(
u'GeoJSON can only contain the following types: %s'%repr(geojson_allowed_types),
u'GeoJSON can only contain the following types: %s' % repr(geojson_allowed_types),
instance=ca, schema=schema[u'definitions'][u'coveredArea'][u'properties'][u'area'],
path=['coveredAreas', i, 'area'],
schema_path=[u'properties', u'coveredAreas', u'items', u'properties', u'area'],
......
......@@ -30,6 +30,7 @@ setup(
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.7',
'Topic :: Internet :: WWW/HTTP',
],
install_requires=[
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment