From 81f497eff92bbb2fc8c3e1aac28bbc96c48eea7c Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sat, 16 Aug 2008 11:33:06 +0000 Subject: Merge commit 'v0.82.0' --- diff --git a/bin/Makefile.am b/bin/Makefile.am index 764f0ba..c583cbe 100644 --- a/bin/Makefile.am +++ b/bin/Makefile.am @@ -1,4 +1,6 @@ bin_SCRIPTS = \ - datastore-service + datastore-service \ + copy-from-journal \ + copy-to-journal EXTRA_DIST = $(bin_SCRIPTS) diff --git a/bin/copy-from-journal b/bin/copy-from-journal new file mode 100755 index 0000000..61b91c9 --- /dev/null +++ b/bin/copy-from-journal @@ -0,0 +1,113 @@ +#!/usr/bin/env python +# +# Simple script to export a file from the datastore +# Reinier Heeres, , 2007-12-24 +# Phil Bordelon + +import sys +import os +import shutil +import optparse + +from sugar.datastore import datastore +import sugar.mime + +# Limit the number of objects returned on an ambiguous query to this number, +# for quicker operation. +RETURN_LIMIT = 2 + +def build_option_parser(): + + usage = "Usage: %prog [-o OBJECT_ID] [-q SEARCH_STR] [-t SEARCH_STR] [-m] OUTFILE" + parser = optparse.OptionParser(usage=usage) + + parser.add_option("-o", "--object_id", action="store", dest="object_id", + help="Retrieve object with explicit ID OBJECT_ID", metavar="OBJECT_ID", + default=None) + + parser.add_option("-q", "--query", action="store", dest="query", + help="Full-text-search the metadata for SEARCH_STR", metavar="SEARCH_STR", + default=None) + + parser.add_option("-t", "--title", action="store", dest="title", + help="Full-text-search the title for SEARCH_STR", metavar="SEARCH_STR", + default=None) + + parser.add_option("-m", "--metadata", action="store_true", dest="show_meta", + help="Show all non-preview metadata [default: hide]", default=False) + + return parser + +if __name__ == "__main__": + + parser = build_option_parser() + options, args = parser.parse_args() + if len(args) < 1: + parser.print_help() + exit(0) + + dsentry = None + + # Get object directly if we were given an explicit object ID. + if options.object_id is not None: + dsentry = datastore.get(options.object_id) + + # Compose the query based on the options provided. + if dsentry is None: + query = {} + + if options.query is not None: + query['query'] = options.query + if options.title is not None: + query['title'] = options.title + + # We only want a single file at a time; limit the number of objects + # returned to two, as anything more than one means the criteria were + # not limited enough. + objects, count = datastore.find(query, limit=RETURN_LIMIT, sorting='-mtime') + print '%r' % query + if count > 1: + print 'WARNING: %d objects found; retrieving most recent.' % (count) + for i in xrange(1, RETURN_LIMIT): + objects[i].destroy() + + if count > 0: + dsentry = objects[0] + + # If neither an explicit object ID nor a query gave us data, fail. + if dsentry is None: + print 'ERROR: unable to determine journal object to copy.' + parser.print_help() + exit(0) + + # Print metadata if that is what the user asked for. + if options.show_meta: + print 'Metadata:' + for key, val in dsentry.metadata.get_dictionary().iteritems(): + if key != 'preview': + print '%20s -> %s' % (key, val) + + # If no file is associated with this object, we can't save it out. + if dsentry.get_file_path() == "": + print 'ERROR: no file associated with object, just metadata.' + dsentry.destroy() + exit(0) + + outname = args[0] + outroot, outext = os.path.splitext(outname) + + # Do our best to determine the output file extension, based on Sugar's + # MIME-type-to-extension mappings. + if outext == "": + mimetype = dsentry.metadata['mime_type'] + outext = sugar.mime.get_primary_extension(mimetype) + if outext == None: + outext = "dsobject" + + # Lastly, actually copy the file out of the datastore and onto the + # filesystem. + shutil.copyfile(dsentry.get_file_path(), outroot + '.' + outext) + print '%s -> %s' % (dsentry.get_file_path(), outroot + '.' + outext) + + # Cleanup. + dsentry.destroy() diff --git a/bin/copy-to-journal b/bin/copy-to-journal new file mode 100755 index 0000000..b45677a --- /dev/null +++ b/bin/copy-to-journal @@ -0,0 +1,86 @@ +#!/usr/bin/python +# +# Simple script to import a file to the datastore +# Reinier Heeres, , 2007-12-20 +# +# Modified by Phil Bordelon 2007-12-20, 2007-12-21 +# to support adding metadata. Note that the MIME-type is required, +# as otherwise the datastore will not accept the file. + +import sys +import os +import optparse + +from sugar.datastore import datastore + +def build_option_parser(): + + usage = "Usage: %prog -m MIMETYPE [-t TITLE] [-d DESC] [-T tag1 [-T tag2 ...]]" + parser = optparse.OptionParser(usage=usage) + + parser.add_option("-t", "--title", action="store", dest="title", + help="Set the title of the journal entry to TITLE", metavar="TITLE", + default=None) + parser.add_option("-d", "--description", action="store", + dest="description", metavar="DESC", + help="Set the description of the journal entry to DESC", + default=None) + parser.add_option("-m", "--mimetype", action="store", + dest="mimetype", metavar="MIMETYPE", + help="Set the file's MIME-type to MIMETYPE", + default=None) + parser.add_option("-T", "--tag", action="append", dest="tag_list", + help="Add tag TAG to the journal entry's tags; this option can be repeated", + metavar="TAG") + return parser + +if __name__ == "__main__": + + parser = build_option_parser() + options, args = parser.parse_args() + if len(args) < 1: + parser.print_help() + exit(0) + + fname = args[0] + absname = os.path.abspath(fname) + if not os.path.exists(absname): + print 'Error: File does not exist.' + parser.print_help() + exit(0) + + if not options.mimetype: + print 'Error: No MIME-type given.' + parser.print_help() + exit(0) + + try: + entry = datastore.create() + entry.set_file_path(absname) + + # Set the mimetype to the provided one. + entry.metadata['mime_type'] = options.mimetype + + # If no title is given, use the filename. + if options.title: + entry.metadata['title'] = options.title + else: + entry.metadata['title'] = fname + + # Use the description given, otherwise leave it blank. + if options.description: + entry.metadata['description'] = options.description + + # Lastly, if any tags are given, combine them into a single string + # and save them. + if options.tag_list: + tag_string = " ".join(options.tag_list) + entry.metadata['tags'] = tag_string + + datastore.write(entry) + print 'Created as %s' % (entry.object_id) + + entry.destroy() + + except Exception, e: + print 'Error: %s' % (e) diff --git a/configure.ac b/configure.ac index 07d00a7..dbcc25d 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT([sugar-datastore],[0.8.2],[],[sugar-datastore]) +AC_INIT([sugar-datastore],[0.82.0],[],[sugar-datastore]) AC_PREREQ([2.59]) diff --git a/src/olpc/datastore/xapianindex.py b/src/olpc/datastore/xapianindex.py index 888bd26..e8a5ee5 100644 --- a/src/olpc/datastore/xapianindex.py +++ b/src/olpc/datastore/xapianindex.py @@ -467,7 +467,8 @@ class IndexManager(object): if order_by: order_by = order_by[0] else: order_by = None - results = ri.search(q, start_index, end_index, sortby=order_by) + results = ri.search(q, start_index, end_index, sortby=order_by, + checkatleast=sys.maxint) count = results.matches_estimated # map the result set to model.Content items -- cgit v0.9.1