Skip to content

Instantly share code, notes, and snippets.

@badzil
Created December 3, 2010 18:59
Show Gist options
  • Save badzil/727369 to your computer and use it in GitHub Desktop.
Save badzil/727369 to your computer and use it in GitHub Desktop.
Invenio automatic collection creator
#!/usr/bin/python
from invenio.dbquery import run_sql
from invenio.websearchadminlib import add_col
def collection_exist(tag):
"""
Checks if a collection exists.
@return: The name of the collection if it exists, None otherwise.
"""
dbquery = 'collection:%s' % tag
collection_name = run_sql("SELECT name FROM collection WHERE dbquery=%s",
(dbquery,))
if collection_name:
return collection_name
dbquery = '980__a:%s' % tag
collection_name = run_sql("SELECT name FROM collection WHERE dbquery=%s",
(dbquery,))
if collection_name:
return collection_name
else:
return None
def main():
# Get all collection tags.
res = run_sql("SELECT DISTINCT(value) FROM bib98x WHERE tag='980__a'")
tags = [row[0] for row in res]
for tag in tags:
print "Creating new collection for tag '%s'." % tag
if collection_exist(tag):
print ".. The collection already exists under the name '%s'." % \
collection_exist(tag)
else:
collection_name = raw_input('.. Collection name? ')
dbquery = 'collection:%s' % tag
code, collection_id = add_col(collection_name, dbquery=dbquery)
if code == 1:
print ".. Collection '%s' was successfully created with id " \
"'%d'." % (collection_name, collection_id)
else:
print "Something went wrong. Exiting."
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment