Changeset 5:573fdae8b1f6 in mailjam for postman/storage.py


Ignore:
Timestamp:
May 16, 2012, 11:41:47 AM (12 years ago)
Author:
Francisco de Borja Lopez Rio <borja@…>
Branch:
default
Phase:
public
Message:

Added tests for postman.daemon.Postman

Added a method to postman.daemon.Postman to "clear" all the storage associated
to a given postman instance (removing all the files from disk)

Fixed some bugs, mostly caused by typos through the sources, detected while
writing more tests

Removed the storage and archive attributes from postman.models.MailingList,
now they are methods "marked" as properties using the @property decorator.
This keeps the storage objects from being serialized into json objects by
the storage backend (de-serializing them caused some trouble and it is not
necessary at all)

Added create() and delete() methods to postman.storage.JsonStorage. the first
one ensures the path to the storage file exists (creating all subdirs if
necessary) and the second one deletes the storage file from disk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • postman/storage.py

    r4 r5  
    11# -*- coding: utf-8 -*-
    22
    3 import os, json
     3import os, errno, json
     4
    45
    56class JsonStorage():
     
    1718        return os.path.exists(self.path)   
    1819
     20    def create(self):
     21        """
     22        Ensure all the subdirectories in the path to the storage file
     23        exist
     24        """
     25        try:
     26            os.makedirs(os.path.dirname(self.path))
     27        except OSError, e:
     28            # If the dir already exists do not complain, if it is
     29            # any other error, raise the exception
     30            if e.errno != errno.EEXIST:
     31                raise
     32   
    1933    def jsonize(self, obj):
    2034        """
     
    4559       
    4660    def write(self, data):
     61        if not self.exists():
     62            # ensure the path to the storage file exists
     63            self.create()
    4764        with open(self.path, 'w') as storage:
    4865            json.dump(data, storage, sort_keys=True, indent=4,
     
    6481        return data
    6582
     83    def delete(self):
     84        """
     85        Remove the storage file
     86        """
     87        if self.exists():
     88            os.remove(self.path)
     89            return True
     90        return False
Note: See TracChangeset for help on using the changeset viewer.