Browse Source

Python3 compatibility. Should work fine with python2 too.

pitchum 7 years ago
parent
commit
3aac4edcb7
3 changed files with 28 additions and 24 deletions
  1. 1 0
      .gitignore
  2. 3 3
      ispformat/bin/isp-format-validator
  3. 24 21
      ispformat/validator/schemavalidator.py

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 *~
 *~
 *.pyc
 *.pyc
 .*.swp
 .*.swp
+*.egg-info/

+ 3 - 3
ispformat/bin/isp-format-validator

@@ -10,9 +10,9 @@ if len(sys.argv) == 2:
     errs=list(validate_isp(j))
     errs=list(validate_isp(j))
     if errs:
     if errs:
         for e in errs:
         for e in errs:
-            print e
+            print(e)
     else:
     else:
-        print "All clear"
+        print("All clear")
 else:
 else:
-    print "Usage: validator.py file-name.json"
+    print("Usage: validator.py file-name.json")
 
 

+ 24 - 21
ispformat/validator/schemavalidator.py

@@ -3,7 +3,10 @@
 import ispformat.schema as _schema
 import ispformat.schema as _schema
 from jsonschema import Draft4Validator, RefResolver, draft4_format_checker
 from jsonschema import Draft4Validator, RefResolver, draft4_format_checker
 from jsonschema.exceptions import RefResolutionError, ValidationError
 from jsonschema.exceptions import RefResolutionError, ValidationError
-from urlparse import urlsplit
+try:
+    from urllib.parse import urlsplit #py3
+except ImportError as e:
+    from urlparse import urlsplit #py2
 
 
 
 
 class MyRefResolver(RefResolver):
 class MyRefResolver(RefResolver):
@@ -49,11 +52,11 @@ def validate_isp(jdict):
     Validate a json-object against the isp json-schema
     Validate a json-object against the isp json-schema
     """
     """
     if not 'version' in jdict:
     if not 'version' in jdict:
-        raise ValidationError(u'version is a required property')
+        raise ValidationError('version is a required property')
     try:
     try:
         schema=_schema.versions[jdict['version']]
         schema=_schema.versions[jdict['version']]
     except (AttributeError, TypeError, KeyError):
     except (AttributeError, TypeError, KeyError):
-        raise ValidationError(u'version %r unsupported'%jdict['version'])
+        raise ValidationError('version %r unsupported'%jdict['version'])
 
 
     v=Draft4Validator(
     v=Draft4Validator(
         schema,
         schema,
@@ -76,25 +79,25 @@ def validate_isp(jdict):
         return True
         return True
 
 
     if 'website' in jdict and not is_valid_url(jdict['website']):
     if 'website' in jdict and not is_valid_url(jdict['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'])
+        yield ValidationError('%r must be an absolute HTTP URL'%'website',
+                              instance=jdict['website'], schema=schema['properties']['website'],
+                              path=['website'], schema_path=['properties', 'website', 'description'],
+                              validator='validate_url', validator_value=jdict['website'])
 
 
     if 'logoURL' in jdict and not is_valid_url(jdict['logoURL']):
     if 'logoURL' in jdict and not is_valid_url(jdict['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'])
+        yield ValidationError('%r must be an absolute HTTP URL'%'logoURL',
+                              instance=jdict['logoURL'], schema=schema['properties']['logoURL'],
+                              path=['logoURL'], schema_path=['properties', 'logoURL', 'description'],
+                              validator='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['properties']['otherWebsites']['patternProperties']['^.+$']
+    for name, url in list(jdict.get('otherWebsites', {}).items()):
         if is_valid_url(url):
         if is_valid_url(url):
             continue
             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('%r must be an absolute HTTP URL'%name,
+                              instance=url, schema=sch, path=['otherWebsite', name],
+                              schema_path=['properties', 'otherWebsites', 'patternProperties', '^.+$', 'description'],
+                              validator='validate_url', validator_value=url)
 
 
     for i, ca in enumerate(jdict.get('coveredAreas', [])):
     for i, ca in enumerate(jdict.get('coveredAreas', [])):
         area=ca.get('area')
         area=ca.get('area')
@@ -104,9 +107,9 @@ def validate_isp(jdict):
             continue
             continue
 
 
         yield ValidationError(
         yield ValidationError(
-            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'],
+            'GeoJSON can only contain the following types: %s'%repr(geojson_allowed_types),
+            instance=ca, schema=schema['definitions']['coveredArea']['properties']['area'],
             path=['coveredAreas', i, 'area'],
             path=['coveredAreas', i, 'area'],
-            schema_path=[u'properties', u'coveredAreas', u'items', u'properties', u'area'],
-            validator=u'validate_geojson_type', validator_value=ca
+            schema_path=['properties', 'coveredAreas', 'items', 'properties', 'area'],
+            validator='validate_geojson_type', validator_value=ca
         )
         )