[ckan-changes] [okfn/ckan] caf8ef: [2303] New paster command rdf-export will dump ALL...

GitHub noreply at github.com
Tue Apr 17 10:10:03 UTC 2012


  Branch: refs/heads/feature-2303-rdf-export-command
  Home:   https://github.com/okfn/ckan
  Commit: caf8efed4fb9a11661b4ea2f832ab8fd7bb33b98
      https://github.com/okfn/ckan/commit/caf8efed4fb9a11661b4ea2f832ab8fd7bb33b98
  Author: Ross Jones <rossdjones at gmail.com>
  Date:   2012-04-17 (Tue, 17 Apr 2012)

  Changed paths:
    M ckan/lib/cli.py
    M setup.py

  Log Message:
  -----------
  [2303] New paster command rdf-export will dump ALL active datasets to a specified folder in our default RDF format


diff --git a/ckan/lib/cli.py b/ckan/lib/cli.py
index 008cf7d..5146aa9 100644
--- a/ckan/lib/cli.py
+++ b/ckan/lib/cli.py
@@ -375,6 +375,66 @@ def command(self):
         else:
             print 'Command %s not recognized' % cmd
 
+
+class RDFExport(CkanCommand):
+    '''
+    This command dumps out all currently active datasets as RDF into the
+    specified folder.
+
+    Usage:
+      paster rdf-export /path/to/store/output
+    '''
+    summary = __doc__.split('\n')[0]
+    usage = __doc__
+
+    def command(self):
+        self._load_config()
+
+        if not self.args:
+            # default to run
+            print RDFExport.__doc__
+        else:
+            self.export_datasets( self.args[0] )
+
+    def export_datasets(self, out_folder):
+        '''
+        Export datasets as RDF to an output folder.
+        '''
+        import urlparse
+        import urllib2
+        import pylons.config as config
+        import ckan.model as model
+        import ckan.logic as logic
+        import ckan.lib.helpers as h
+
+        # Create output folder if not exists
+        if not os.path.isdir( out_folder ):
+            os.makedirs( out_folder )
+
+        fetch_url = config['ckan.site_url']
+        user = logic.get_action('get_site_user')({'model': model, 'ignore_auth': True}, {})
+        context = {'model': model, 'session': model.Session, 'user': user['name']}
+        dataset_names = logic.get_action('package_list')(context, {})
+        for dataset_name in dataset_names:
+            dd = logic.get_action('package_show')(context, {'id':dataset_name })
+            if not dd['state'] == 'active':
+                continue
+
+            url = h.url_for( controller='package',action='read',
+                                                  id=dd['name'])
+
+            url = urlparse.urljoin(fetch_url, url[1:]) + '.rdf'
+            try:
+                fname = os.path.join( out_folder, dd['name'] ) + ".rdf"
+                r = urllib2.urlopen(url).read()
+                with open(fname, 'wb') as f:
+                    f.write(r)
+            except IOError, ioe:
+                sys.stderr.write( str(ioe) + "\n" )
+
+
+
+
 class Sysadmin(CkanCommand):
     '''Gives sysadmin rights to a named user
 
@@ -720,7 +780,7 @@ class Celery(CkanCommand):
     summary = __doc__.split('\n')[0]
     usage = __doc__
 
-    def command(self):        
+    def command(self):
         if not self.args:
             self.run_()
         else:
diff --git a/setup.py b/setup.py
index a8f3389..ee1f06d 100644
--- a/setup.py
+++ b/setup.py
@@ -70,6 +70,7 @@
     rights = ckan.lib.authztool:RightsCommand
     roles = ckan.lib.authztool:RolesCommand
     celeryd = ckan.lib.cli:Celery
+    rdf-export = ckan.lib.cli:RDFExport
 
     [console_scripts]
     ckan-admin = bin.ckan_admin:Command


================================================================



More information about the ckan-changes mailing list