[Sugar-devel] [PATCH] Journal entry sharing using a mass storage device

Simon Schampijer simon at schampijer.de
Mon Feb 14 17:13:40 EST 2011


http://wiki.sugarlabs.org/go/Features/Journal_Entry_Sharing
---
 src/jarabe/journal/model.py |  188 +++++++++++++++++++++++++++++++++++++++----
 1 files changed, 171 insertions(+), 17 deletions(-)

diff --git a/src/jarabe/journal/model.py b/src/jarabe/journal/model.py
index 320e577..67d2f19 100644
--- a/src/jarabe/journal/model.py
+++ b/src/jarabe/journal/model.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2007-2010, One Laptop per Child
+# Copyright (C) 2007-2011, One Laptop per Child
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -20,13 +20,15 @@ import errno
 from datetime import datetime
 import time
 import shutil
+import tempfile
 from stat import S_IFLNK, S_IFMT, S_IFDIR, S_IFREG
 import re
 from operator import itemgetter
+import json
+from gettext import gettext as _
 
 import gobject
 import dbus
-import gconf
 import gio
 
 from sugar import dispatch
@@ -46,6 +48,8 @@ PROPERTIES = ['activity', 'activity_id', 'buddies', 'bundle_id',
 MIN_PAGES_TO_CACHE = 3
 MAX_PAGES_TO_CACHE = 5
 
+JOURNAL_METADATA_DIR = '.Sugar-Metadata'
+
 _datastore = None
 created = dispatch.Signal()
 updated = dispatch.Signal()
@@ -295,8 +299,9 @@ class InplaceResultSet(BaseResultSet):
         files = self._file_list[offset:offset + limit]
 
         entries = []
-        for file_path, stat, mtime_, size_ in files:
-            metadata = _get_file_metadata(file_path, stat)
+        for file_path, stat, mtime_, metadata, size_ in files:
+            if metadata is None:
+                metadata = _get_file_metadata(file_path, stat)
             metadata['mountpoint'] = self._mount_point
             entries.append(metadata)
 
@@ -324,6 +329,7 @@ class InplaceResultSet(BaseResultSet):
 
     def _scan_a_file(self):
         full_path = self._pending_files.pop(0)
+        metadata = None
 
         try:
             stat = os.lstat(full_path)
@@ -365,7 +371,20 @@ class InplaceResultSet(BaseResultSet):
 
         if self._regex is not None and \
                 not self._regex.match(full_path):
-            return
+            filename = os.path.basename(full_path)
+            dir_path = os.path.dirname(full_path)
+            metadata = _get_file_metadata_from_json( \
+                dir_path, filename, preview=False)
+            add_to_list = False
+            if metadata is not None:
+                for f in ['fulltext', 'title',
+                          'description', 'tags']:
+                    if f in metadata and \
+                            self._regex.match(metadata[f]):
+                        add_to_list = True
+                        break
+            if not add_to_list:
+                return
 
         if self._date_start is not None and stat.st_mtime < self._date_start:
             return
@@ -378,7 +397,8 @@ class InplaceResultSet(BaseResultSet):
             if mime_type not in self._mime_types:
                 return
 
-        file_info = (full_path, stat, int(stat.st_mtime), stat.st_size)
+        file_info = (full_path, stat, int(stat.st_mtime), metadata,
+                     stat.st_size)
         self._file_list.append(file_info)
 
         return
@@ -401,7 +421,17 @@ class InplaceResultSet(BaseResultSet):
 
 
 def _get_file_metadata(path, stat):
-    client = gconf.client_get_default()
+    """Returns the metadata from the corresponding file
+    on the external device or does create the metadata
+    based on the file properties.
+
+    """
+    filename = os.path.basename(path)
+    dir_path = os.path.dirname(path)
+    metadata = _get_file_metadata_from_json(dir_path, filename, preview=True)
+    if metadata:
+        return metadata
+
     return {'uid': path,
             'title': os.path.basename(path),
             'timestamp': stat.st_mtime,
@@ -409,10 +439,46 @@ def _get_file_metadata(path, stat):
             'mime_type': gio.content_type_guess(filename=path),
             'activity': '',
             'activity_id': '',
-            'icon-color': client.get_string('/desktop/sugar/user/color'),
+            'icon-color': '#000000,#ffffff',
             'description': path}
 
 
+def _get_file_metadata_from_json(dir_path, filename, preview=False):
+    """Returns the metadata from the json file and the preview
+    stored on the external device. If the metadata is corrupted
+    we do remove it and the preview as well.
+
+    """
+    metadata = None
+    metadata_path = os.path.join(dir_path, JOURNAL_METADATA_DIR,
+                                 filename + '.metadata')
+    preview_path = os.path.join(dir_path, JOURNAL_METADATA_DIR,
+                                filename + '.preview')
+
+    if os.path.exists(metadata_path):
+        try:
+            metadata = json.load(open(metadata_path))
+        except ValueError:
+            os.unlink(metadata_path)
+            if os.path.exists(preview_path):
+                os.unlink(preview_path)
+            logging.debug("Could not read metadata for file %r on " \
+                              "external device.", filename)
+        else:
+            metadata['uid'] = os.path.join(dir_path, filename)
+    if preview:
+        if os.path.exists(preview_path):
+            try:
+                metadata['preview'] = dbus.ByteArray(open(preview_path).read())
+            except:
+                logging.debug("Could not read preview for file %r on " \
+                                  "external device.", filename)
+    else:
+        if metadata and 'preview' in metadata:
+            del(metadata['preview'])
+    return metadata
+
+
 def _get_datastore():
     global _datastore
     if _datastore is None:
@@ -519,6 +585,18 @@ def delete(object_id):
     """
     if os.path.exists(object_id):
         os.unlink(object_id)
+        dir_path = os.path.dirname(object_id)
+        filename = os.path.basename(object_id)
+        old_files = [os.path.join(dir_path, JOURNAL_METADATA_DIR,
+                                  filename + '.metadata'),
+                     os.path.join(dir_path, JOURNAL_METADATA_DIR,
+                                  filename + '.preview')]
+        for old_file in old_files:
+            if os.path.exists(old_file):
+                try:
+                    os.unlink(old_file)
+                except:
+                    pass
         deleted.send(None, object_id=object_id)
     else:
         _get_datastore().delete(object_id)
@@ -556,22 +634,98 @@ def write(metadata, file_path='', update_mtime=True, transfer_ownership=True):
                                                  file_path,
                                                  transfer_ownership)
     else:
-        if not os.path.exists(file_path):
-            raise ValueError('Entries without a file cannot be copied to '
-                             'removable devices')
+        object_id = _write_entry_on_external_device(metadata, file_path)
+
+    return object_id
+
+
+def _write_entry_on_external_device(metadata, file_path):
+    """This creates and updates an entry copied from the
+    DS to external storage device. Besides copying the
+    associated file a hidden file for the preview and one
+    for the metadata are stored in the hidden directory
+    .Sugar-Metadata.
 
-        file_name = _get_file_name(metadata['title'], metadata['mime_type'])
-        file_name = _get_unique_file_name(metadata['mountpoint'], file_name)
+    This function handles renames of an entry on the
+    external device and avoids name collisions. Renames are
+    handled failsafe.
 
+    """
+    if 'uid' in metadata and os.path.exists(metadata['uid']):
+        file_path = metadata['uid']
+
+    if not file_path or not os.path.exists(file_path):
+        raise ValueError('Entries without a file cannot be copied to '
+                         'removable devices')
+
+    if metadata['title'] == '':
+        metadata['title'] = _('Untitled')
+    file_name = get_file_name(metadata['title'], metadata['mime_type'])
+
+    destination_path = os.path.join(metadata['mountpoint'], file_name)
+    if destination_path != file_path:
+        file_name = get_unique_file_name(metadata['mountpoint'], file_name)
         destination_path = os.path.join(metadata['mountpoint'], file_name)
+        clean_name, extension_ = os.path.splitext(file_name)
+        metadata['title'] = clean_name
+
+    metadata_copy = metadata.copy()
+    del metadata_copy['mountpoint']
+    if 'uid' in metadata_copy:
+        del metadata_copy['uid']
+
+    metadata_dir_path = os.path.join(metadata['mountpoint'],
+                                     JOURNAL_METADATA_DIR)
+    if not os.path.exists(metadata_dir_path):
+        os.mkdir(metadata_dir_path)
+
+    if 'preview' in metadata_copy:
+        preview = metadata_copy['preview']
+        preview_fname = file_name + '.preview'
+        preview_path = os.path.join(metadata['mountpoint'],
+                                    JOURNAL_METADATA_DIR, preview_fname)
+        metadata_copy['preview'] = preview_fname
+
+        (fh, fn) = tempfile.mkstemp(dir=metadata['mountpoint'])
+        os.write(fh, preview)
+        os.close(fh)
+        os.rename(fn, preview_path)
+
+    metadata_path = os.path.join(metadata['mountpoint'],
+                                 JOURNAL_METADATA_DIR,
+                                 file_name + '.metadata')
+    (fh, fn) = tempfile.mkstemp(dir=metadata['mountpoint'])
+    os.write(fh, json.dumps(metadata_copy))
+    os.close(fh)
+    os.rename(fn, metadata_path)
+
+    if os.path.dirname(destination_path) == os.path.dirname(file_path):
+        old_file_path = file_path
+        if old_file_path != destination_path:
+            os.rename(file_path, destination_path)
+            old_fname = os.path.basename(file_path)
+            old_files = [os.path.join(metadata['mountpoint'],
+                                      JOURNAL_METADATA_DIR,
+                                      old_fname + '.metadata'),
+                         os.path.join(metadata['mountpoint'],
+                                      JOURNAL_METADATA_DIR,
+                                      old_fname + '.preview')]
+            for ofile in old_files:
+                if os.path.exists(ofile):
+                    try:
+                        os.unlink(ofile)
+                    except:
+                        pass
+    else:
         shutil.copy(file_path, destination_path)
-        object_id = destination_path
-        created.send(None, object_id=object_id)
+
+    object_id = destination_path
+    created.send(None, object_id=object_id)
 
     return object_id
 
 
-def _get_file_name(title, mime_type):
+def get_file_name(title, mime_type):
     file_name = title
 
     extension = mime.get_primary_extension(mime_type)
@@ -596,7 +750,7 @@ def _get_file_name(title, mime_type):
     return file_name
 
 
-def _get_unique_file_name(mount_point, file_name):
+def get_unique_file_name(mount_point, file_name):
     if os.path.exists(os.path.join(mount_point, file_name)):
         i = 1
         name, extension = os.path.splitext(file_name)
-- 
1.7.4



More information about the Sugar-devel mailing list