Commits

Nick Stenning committed 8036566

Add 'paster ckan check <pkgname>' command, which shows a summary of import status and relevant URLs for a CKAN package.

Comments (0)

Files changed (2)

wdmmg/lib/ckan.py

 
     return [Package(name) for name in group.get('packages')]
 
-class AmbiguousResourceError(Exception):
+class ResourceError(Exception):
     pass
 
-class MissingResourceError(Exception):
+class AmbiguousResourceError(ResourceError):
+    pass
+
+class MissingResourceError(ResourceError):
     pass
 
 class Package(object):

wdmmg/lib/command/ckan.py

 
         if cmd == 'show':
             self._cmd_show()
+        if cmd == 'check':
+            self._cmd_check()
         elif cmd == 'hintadd':
             self._cmd_hintadd()
         elif cmd == 'hintrm':
 
         pprint(self.c.package_entity_get(package_name))
 
+    def _cmd_check(self):
+        if len(self.args) != 2:
+            raise self.BadCommand("Usage: paster ckan check <pkgname>")
+
+        package_name = self.args[1]
+        from wdmmg.lib import ckan, json
+
+        p = ckan.Package(package_name)
+
+        def _get_url_or_error(resource_name):
+            try:
+                r = p.openspending_resource(resource_name)
+                if r:
+                    return r["url"]
+                else:
+                    return r
+            except ckan.ResourceError as e:
+                return str(e)
+
+        res = {
+            "is_importable": p.is_importable(),
+            "data": _get_url_or_error('data'),
+            "model": _get_url_or_error('model'),
+            "model:mapping": _get_url_or_error('model:mapping')
+        }
+
+        print(json.dumps(res, indent=2, sort_keys=True))
+
     def _cmd_hintadd(self):
         if len(self.args) != 4:
             raise self.BadCommand("Usage: paster ckan hintadd <pkgname> <uuid> <hint>")