From edd61f3a5b1f97050fcd43eaed7e1645595fac73 Mon Sep 17 00:00:00 2001 From: amercader Date: Thu, 20 Jun 2013 16:45:37 +0100 Subject: [PATCH] [#24] Add clear command to empty the pycsw table --- bin/ckan_pycsw.py | 16 ++++++++++++++++ ckanext/spatial/commands/csw.py | 2 ++ 2 files changed, 18 insertions(+) diff --git a/bin/ckan_pycsw.py b/bin/ckan_pycsw.py index b72f60d..f7d4a0e 100644 --- a/bin/ckan_pycsw.py +++ b/bin/ckan_pycsw.py @@ -211,6 +211,20 @@ def load(pycsw_config, ckan_url): raise RuntimeError, 'ERROR: %s' % str(err) +def clear(pycsw_config): + + from sqlalchemy import create_engine, MetaData, Table + + database = pycsw_config.get('repository', 'database') + table_name = pycsw_config.get('repository', 'table', 'records') + + log.debug('Creating engine') + engine = create_engine(database) + records = Table(table_name, MetaData(engine)) + records.delete().execute() + log.info('Table cleared') + + def get_record(context, repo, ckan_url, ckan_id, ckan_info): query = ckan_url + 'harvest/object/%s' url = query % ckan_info['harvest_object_id'] @@ -304,6 +318,8 @@ if __name__ == '__main__': if not arg.ckan_url: raise AssertionError('You need to provide a CKAN URL with -u or --ckan_url') load(pycsw_config, arg.ckan_url) + elif arg.command == 'clear': + clear(pycsw_config) else: print 'Unknown command {0}'.format(arg.command) sys.exit(1) diff --git a/ckanext/spatial/commands/csw.py b/ckanext/spatial/commands/csw.py index acd1b76..6d2fa3f 100644 --- a/ckanext/spatial/commands/csw.py +++ b/ckanext/spatial/commands/csw.py @@ -51,5 +51,7 @@ option: elif cmd == 'load': ckan_url = self.options.ckan_url ckan_pycsw.load(config, ckan_url) + elif cmd == 'clear': + ckan_pycsw.clear(config) else: print 'Command %s not recognized' % cmd