[#24] Add clear command to empty the pycsw table

This commit is contained in:
amercader 2013-06-20 16:45:37 +01:00
parent 108b834638
commit edd61f3a5b
2 changed files with 18 additions and 0 deletions

View File

@ -211,6 +211,20 @@ def load(pycsw_config, ckan_url):
raise RuntimeError, 'ERROR: %s' % str(err) 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): def get_record(context, repo, ckan_url, ckan_id, ckan_info):
query = ckan_url + 'harvest/object/%s' query = ckan_url + 'harvest/object/%s'
url = query % ckan_info['harvest_object_id'] url = query % ckan_info['harvest_object_id']
@ -304,6 +318,8 @@ if __name__ == '__main__':
if not arg.ckan_url: if not arg.ckan_url:
raise AssertionError('You need to provide a CKAN URL with -u or --ckan_url') raise AssertionError('You need to provide a CKAN URL with -u or --ckan_url')
load(pycsw_config, arg.ckan_url) load(pycsw_config, arg.ckan_url)
elif arg.command == 'clear':
clear(pycsw_config)
else: else:
print 'Unknown command {0}'.format(arg.command) print 'Unknown command {0}'.format(arg.command)
sys.exit(1) sys.exit(1)

View File

@ -51,5 +51,7 @@ option:
elif cmd == 'load': elif cmd == 'load':
ckan_url = self.options.ckan_url ckan_url = self.options.ckan_url
ckan_pycsw.load(config, ckan_url) ckan_pycsw.load(config, ckan_url)
elif cmd == 'clear':
ckan_pycsw.clear(config)
else: else:
print 'Command %s not recognized' % cmd print 'Command %s not recognized' % cmd