add some doc to sprunge
[xo.git] / jget
blob12cf34ac605999460fc74aedf0c14bc5a55f73dd
1 #!/usr/bin/env python
3 # Simple script to export a file from the datastore
4 # Reinier Heeres, <reinier@heeres.eu>, 2007-12-24
5 # Phil Bordelon <phil@thenexusproject.org>
7 import sys
8 import os
9 import shutil
10 import optparse
12 from sugar.datastore import datastore
13 import sugar.mime
15 # Limit the number of objects returned on an ambiguous query to this number,
16 # for quicker operation.
17 RETURN_LIMIT = 2
19 def build_option_parser():
21 usage = "Usage: %prog [-o OBJECT_ID] [-q SEARCH_STR] [-t SEARCH_STR] [-m] OUTFILE"
22 parser = optparse.OptionParser(usage=usage)
24 parser.add_option("-o", "--object_id", action="store", dest="object_id",
25 help="Retrieve object with explicit ID OBJECT_ID", metavar="OBJECT_ID",
26 default=None)
28 parser.add_option("-q", "--query", action="store", dest="query",
29 help="Full-text-search the metadata for SEARCH_STR", metavar="SEARCH_STR",
30 default=None)
32 parser.add_option("-t", "--title", action="store", dest="title",
33 help="Full-text-search the title for SEARCH_STR", metavar="SEARCH_STR",
34 default=None)
36 parser.add_option("-m", "--metadata", action="store_true", dest="show_meta",
37 help="Show all non-preview metadata [default: hide]", default=False)
39 return parser
41 if __name__ == "__main__":
43 parser = build_option_parser()
44 options, args = parser.parse_args()
45 if len(args) < 1:
46 parser.print_help()
47 exit(0)
49 dsentry = None
51 # Get object directly if we were given an explicit object ID.
52 if options.object_id is not None:
53 dsentry = datastore.get(options.object_id)
55 # Compose the query based on the options provided.
56 if dsentry is None:
57 query = {}
59 if options.query is not None:
60 query['query'] = options.query
61 if options.title is not None:
62 query['title'] = options.title
64 # We only want a single file at a time; limit the number of objects
65 # returned to two, as anything more than one means the criteria were
66 # not limited enough.
67 objects, count = datastore.find(query, limit=RETURN_LIMIT, sorting='-mtime')
68 if count > 1:
69 print 'WARNING: %d objects found; retrieving most recent.' % (count)
70 for i in xrange(1, RETURN_LIMIT):
71 objects[i].destroy()
73 if count > 0:
74 dsentry = objects[0]
76 # If neither an explicit object ID nor a query gave us data, fail.
77 if dsentry is None:
78 print 'ERROR: unable to determine journal object to copy.'
79 parser.print_help()
80 exit(0)
82 # Print metadata if that is what the user asked for.
83 if options.show_meta:
84 print 'Metadata:'
85 for key, val in dsentry.metadata.get_dictionary().iteritems():
86 if key != 'preview':
87 print '%20s -> %s' % (key, val)
89 # If no file is associated with this object, we can't save it out.
90 if dsentry.get_file_path() == "":
91 print 'ERROR: no file associated with object, just metadata.'
92 dsentry.destroy()
93 exit(0)
95 outname = args[0]
96 outroot, outext = os.path.splitext(outname)
98 # Do our best to determine the output file extension, based on Sugar's
99 # MIME-type-to-extension mappings.
100 if outext == "":
101 mimetype = dsentry.metadata['mime_type']
102 outext = sugar.mime.get_primary_extension(mimetype)
103 if outext == None:
104 outext = "dsobject"
106 # Lastly, actually copy the file out of the datastore and onto the
107 # filesystem.
108 shutil.copyfile(dsentry.get_file_path(), outroot + '.' + outext)
109 print '%s -> %s' % (dsentry.get_file_path(), outroot + '.' + outext)
111 # Cleanup.
112 dsentry.destroy()