49 | | dsentry = None |
50 | | |
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) |
54 | | |
55 | | # Compose the query based on the options provided. |
56 | | if dsentry is None: |
57 | | query = {} |
58 | | |
59 | | if options.query is not None: |
60 | | query['query'] = options.query |
61 | | |
62 | | # We only want a single file at a time; limit the number of objects |
63 | | # returned to two, as anything more than one means the criteria were |
64 | | # not limited enough. |
65 | | objects, count = datastore.find(query, limit=RETURN_LIMIT, sorting='-mtime') |
66 | | print '%r' % query |
67 | | if count > 1: |
68 | | print 'WARNING: %d objects found; retrieving most recent.' % (count) |
69 | | for i in xrange(1, RETURN_LIMIT): |
70 | | objects[i].destroy() |
71 | | |
72 | | if count > 0: |
73 | | dsentry = objects[0] |
74 | | |
75 | | # If neither an explicit object ID nor a query gave us data, fail. |
76 | | if dsentry is None: |
77 | | print 'ERROR: unable to determine journal object to copy.' |
78 | | parser.print_help() |
79 | | exit(0) |
80 | | |
81 | | # Print metadata if that is what the user asked for. |
82 | | if options.show_meta: |
83 | | print 'Metadata:' |
84 | | for key, val in dsentry.metadata.get_dictionary().iteritems(): |
85 | | if key != 'preview': |
86 | | print '%20s -> %s' % (key, val) |
87 | | |
88 | | # If no file is associated with this object, we can't save it out. |
89 | | if dsentry.get_file_path() == "": |
90 | | print 'ERROR: no file associated with object, just metadata.' |
| 50 | try: |
| 51 | dsentry = None |
| 52 | |
| 53 | # Get object directly if we were given an explicit object ID. |
| 54 | if options.object_id is not None: |
| 55 | dsentry = datastore.get(options.object_id) |
| 56 | |
| 57 | # Compose the query based on the options provided. |
| 58 | if dsentry is None: |
| 59 | query = {} |
| 60 | |
| 61 | if options.query is not None: |
| 62 | query['query'] = options.query |
| 63 | |
| 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() |
| 72 | |
| 73 | if count > 0: |
| 74 | dsentry = objects[0] |
| 75 | |
| 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) |
| 81 | |
| 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) |
| 88 | |
| 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) |
| 94 | |
| 95 | outname = args[0] |
| 96 | outroot, outext = os.path.splitext(outname) |
| 97 | |
| 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" |
| 105 | outext = '.' + outext |
| 106 | |
| 107 | # Lastly, actually copy the file out of the datastore and onto the |
| 108 | # filesystem. |
| 109 | shutil.copyfile(dsentry.get_file_path(), outroot + outext) |
| 110 | print '%s -> %s' % (dsentry.get_file_path(), outroot + outext) |
| 111 | |
| 112 | # Cleanup. |