[PATCH] CLI Interface to the Sugar Datastore

Martin Dengler martin at martindengler.com
Mon Mar 31 06:58:50 EDT 2008


On Thu, Mar 27, 2008 at 01:46:36AM +0100, Michael Vorburger wrote:
> Hello,
> 
> Attached a patch for copy-to-journal.py which adds auto-detection of
> the MIME type! 

I asked Phil about this feature; his target audience isn't well served
by this option (if I understand his stance correctly).  I personally
find the feature quite useful and never fail to use it.

> Hope you find this useful.

Attached are two patches of mine I've sent to Phil privately:

1) does what your patch does, but using /usr/bin/file instead of
mimetypes.guess_blah.  I use file because it actually looks at the
contents instead of just the filename extension. To each his own.

2) Does what Phil seemed to see as a useful compromise: add an option
(not enabled by default) to guess the mime-type.

People may also be interested in a third, independent, patch, that
enables copy-to-journal and other support-tools to be run from without
Sugar/X; for example, screen / vt console login.  I attach it just in
case as I think Phil may merge it soon.

Apropos of that, I think Phil's trying to get the whole set of
support-scripts merged into olpc-utils, and that that would be a real
nice thing to happen.  Anyone interested in supporting that could
probably chime in with a +1 or something, which might help move that
process along.

> Regards,
> Michael

Martin
-------------- next part --------------
From 07cb9c739ccca7af699a268e8475e7b2f63e7f72 Mon Sep 17 00:00:00 2001
From: Martin Dengler <martin at martindengler.com>
Date: Tue, 25 Mar 2008 05:24:00 +0000
Subject: [PATCH] guess mime-type for the user if none specified

---
 copy-to-journal |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/copy-to-journal b/copy-to-journal
index 65eaee2..7f8b72d 100755
--- a/copy-to-journal
+++ b/copy-to-journal
@@ -75,8 +75,18 @@ if __name__ == "__main__":
         parser.print_help()
         exit(0)
 
-    if not options.mimetype:
-        print 'Error: No MIME-type given.'
+    mime_type = options.mimetype
+    if not mime_type:
+        # mimetypes.guess_type() just uses the extension,
+        # so we use 'file' instead
+        try:
+            guess_from_file = os.popen("file -bi %s" % fname).readlines()
+            mime_type = guess_from_file[0].split(";")[0].strip()
+        except Exception, e:
+            raise
+
+    if not mime_type:
+        print 'Error: No MIME-type given (and none could be determined).'
         parser.print_help()
         exit(0)
     
@@ -85,7 +95,7 @@ if __name__ == "__main__":
         entry.set_file_path(absname)
 
         # Set the mimetype to the provided one.
-        entry.metadata['mime_type'] = options.mimetype
+        entry.metadata['mime_type'] = mime_type
 
         # If no title is given, use the filename.
         if options.title:
@@ -104,7 +114,7 @@ if __name__ == "__main__":
             entry.metadata['tags'] = tag_string
 
         datastore.write(entry)
-        print 'Created as %s' % (entry.object_id)
+        print 'Created %s as %s (%s)' % (fname, entry.object_id, mime_type)
     
         entry.destroy()
     
-- 
1.5.3.3

-------------- next part --------------
From 75c51f83c8d91c2fd35e7e99e0f7bf787b692ce7 Mon Sep 17 00:00:00 2001
From: Martin Dengler <martin at martindengler.com>
Date: Mon, 31 Mar 2008 10:45:14 +0000
Subject: [PATCH] add -g / --guess_mimetype option instead of making that behavior the default

---
 copy-to-journal |   33 ++++++++++++++++++++-------------
 1 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/copy-to-journal b/copy-to-journal
index 2d89158..a336fba 100755
--- a/copy-to-journal
+++ b/copy-to-journal
@@ -46,7 +46,7 @@ from sugar.datastore import datastore
 
 def build_option_parser():
 
-    usage = "Usage: %prog <file> -m MIMETYPE [-t TITLE] [-d DESC] [-T tag1 [-T tag2 ...]]"
+    usage = "Usage: %prog <file> [-m MIMETYPE|-g|--guess-mimetype] [-t TITLE] [-d DESC] [-T tag1 [-T tag2 ...]]"
     parser = optparse.OptionParser(usage=usage)
 
     parser.add_option("-t", "--title", action="store", dest="title",
@@ -60,6 +60,10 @@ def build_option_parser():
      dest="mimetype", metavar="MIMETYPE",
      help="Set the file's MIME-type to MIMETYPE",
      default=None)
+    parser.add_option("-g", "--guess-mimetype", action="store_true",
+     dest="guess_mimetype", metavar="GUESS_MIMETYPE",
+     help="Guess the file's MIME-type instead of requiring it to be passed to -m",
+     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")
@@ -82,18 +86,21 @@ if __name__ == "__main__":
 
     mime_type = options.mimetype
     if not mime_type:
-        # mimetypes.guess_type() just uses the extension,
-        # so we use 'file' instead
-        try:
-            guess_from_file = os.popen("file -bi %s" % fname).readlines()
-            mime_type = guess_from_file[0].split(";")[0].strip()
-        except Exception, e:
-            raise
-
-    if not mime_type:
-        print 'Error: No MIME-type given (and none could be determined).'
-        parser.print_help()
-        exit(0)
+        if options.guess_mimetype:
+            # mimetypes.guess_type() just uses the extension,
+            # so we use 'file' instead
+            try:
+                guess_from_file = os.popen("file -bi %s" % fname).readlines()
+                mime_type = guess_from_file[0].split(";")[0].strip()
+            except Exception, e:
+                raise
+        if not mime_type:    
+            print 'Error: No MIME-type given%s' % (
+                options.guess_mimetype
+                and ' and none could be determined).'
+                or '.')
+            parser.print_help()
+            exit(0)
     
     try:
         entry = datastore.create()
-- 
1.5.3.3

-------------- next part --------------
From 76dea67e0d95472372f955c639ed6c8a91f21702 Mon Sep 17 00:00:00 2001
From: Martin Dengler <martin at martindengler.com>
Date: Tue, 25 Mar 2008 06:02:59 +0000
Subject: [PATCH] support running from the console

This is a matter (on the XO) of defining DBUS_SESSION_BUS_ADDRESS and
DISPLAY if the are unset.
---
 copy-from-journal |    5 +++++
 copy-to-journal   |    5 +++++
 lsjournal         |    5 +++++
 purge-previews    |    5 +++++
 4 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/copy-from-journal b/copy-from-journal
index 8e41b58..f700aa5 100755
--- a/copy-from-journal
+++ b/copy-from-journal
@@ -37,6 +37,11 @@ import os
 import shutil
 import optparse
 
+if 'DBUS_SESSION_BUS_ADDRESS' not in os.environ:
+    os.environ['DBUS_SESSION_BUS_ADDRESS'] = 'unix:path=/tmp/olpc-session-bus'
+if 'DISPLAY' not in os.environ:
+    os.environ['DISPLAY'] = ':0.0'
+
 from sugar.datastore import datastore
 import sugar.mime
 
diff --git a/copy-to-journal b/copy-to-journal
index 7f8b72d..2d89158 100755
--- a/copy-to-journal
+++ b/copy-to-journal
@@ -37,6 +37,11 @@ import sys
 import os
 import optparse
 
+if 'DBUS_SESSION_BUS_ADDRESS' not in os.environ:
+    os.environ['DBUS_SESSION_BUS_ADDRESS'] = 'unix:path=/tmp/olpc-session-bus'
+if 'DISPLAY' not in os.environ:
+    os.environ['DISPLAY'] = ':0.0'
+
 from sugar.datastore import datastore
 
 def build_option_parser():
diff --git a/lsjournal b/lsjournal
index 003dd6e..b94860b 100755
--- a/lsjournal
+++ b/lsjournal
@@ -36,6 +36,11 @@
 from os.path import basename
 import optparse
 
+if 'DBUS_SESSION_BUS_ADDRESS' not in os.environ:
+    os.environ['DBUS_SESSION_BUS_ADDRESS'] = 'unix:path=/tmp/olpc-session-bus'
+if 'DISPLAY' not in os.environ:
+    os.environ['DISPLAY'] = ':0.0'
+
 from sugar.datastore import datastore
 import sugar.mime
 
diff --git a/purge-previews b/purge-previews
index 1a4f11f..ba6a206 100755
--- a/purge-previews
+++ b/purge-previews
@@ -35,6 +35,11 @@ import os
 import os.path
 import sys
 
+if 'DBUS_SESSION_BUS_ADDRESS' not in os.environ:
+    os.environ['DBUS_SESSION_BUS_ADDRESS'] = 'unix:path=/tmp/olpc-session-bus'
+if 'DISPLAY' not in os.environ:
+    os.environ['DISPLAY'] = ':0.0'
+
 from sugar.datastore import datastore
 
 # Note that DATASTORE_PATH should almost certainly not be hardcoded, but as
-- 
1.5.3.3

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.laptop.org/pipermail/devel/attachments/20080331/15c87e97/attachment.sig>


More information about the Devel mailing list