bookmark.py :  » Network » FtpCube » ftpcube-0.5.1 » libftpcube » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » FtpCube 
FtpCube » ftpcube 0.5.1 » libftpcube » bookmark.py
"""
FtpCube
Copyright (C) Michael Gilfix

This file is part of FtpCube.

You should have received a file COPYING containing license terms
along with this program; if not, write to Michael Gilfix
(mgilfix@eecs.tufts.edu) for a copy.

This version of FtpCube is open source; you can redistribute it and/or
modify it under the terms listed in the file COPYING.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""

from libftpcube import parser# Avoid python package conflict
utils

import os
import exceptions

class BookmarkException(exceptions.Exception):
    """Exception for bookmark processing."""

    def __init__(self, args=None):
        self.args = args

class BookmarkEntry(parser.SimpleConfigParser):
    """Bookmark entry containing bookmark connection information.

    This extends the simple configuration parser to store bookmark information in
    the INI style format. The type map contains a description of bookmark attributes
    and their mappings to python types."""

    SECTION = "ftpcube-bookmark"

    def __init__(self, file):
        """Initializes a bookmark entry but does not perform any loading during
        initialization."""
        if __debug__:
            print "Initializing bookmark entry: %s" %file
        parser.SimpleConfigParser.__init__(self, file, self.SECTION, load_initial=False)

class BookmarkManager:
    """Bookmark manager class.

    A bookmark consists of a configuration file in the bookmark directory that
    can be manipulated within the bookmark window. The bookmark directory is a
    directory under the configuration directory where all bookmarks are stored.
    The name of the bookmark file corresponds to the name of the bookmark.

    This class provides methods for manipulating bookmark entries (files), as
    well as loading and saving their contents. Bookmarks are essentially
    dictionaries of attribute value/pairs that are stored in a simple INI format.
    """

    # Directory for storing bookmarks in the configuration directory
    BOOKMARK_DIRECTORY = "bookmarks"

    def __init__(self):
        """Creates a new bookmark manager.

        Bookmark data cannot be accessed until it is loaded. This initializes the
        bookmark storage directory."""
        config_dir = utils.getApplicationConfigDirectory()
        self.book_dir = os.path.join(config_dir, self.BOOKMARK_DIRECTORY)

        if not os.path.exists(self.book_dir):
            try:
                os.mkdir(self.book_dir)
                os.chmod(self.book_dir, 0700)
            except OSError, strerror:
                raise BookmarkException, strerror

    def getBookmarkDirectory(self):
        """Returns the path to the bookmark directory."""
        return self.book_dir

    def saveBookmark(self, file, opts):
        """Saves a bookmark to the specified file, whose contents are in the 'opts'
        dictionary.

        The specified file is a relative file path under the bookmark directory."""
        path = os.path.join(self.getBookmarkDirectory(), file)
        try:
            entry = BookmarkEntry(path)
            entry.setOptions(opts)
            entry.save()
        except parser.ParserError, strerror:
            raise BookmarkException, strerror

        # Set the bookmark permission to make sure it's private
        try:
            os.chmod(path, 0600)
        except OSError, strerror:
            raise BookmarkException, strerror

    def loadBookmark(self, file):
        """Loads a bookmark from the specified file and return the contents of the
        bookmark as a dictionary.

        The specified file is a relative file path under the bookmark directory. If
        an error occurs loading the bookmark, then None is returned."""
        options = None
        path = os.path.join(self.getBookmarkDirectory(), file)
        try:
            entry = BookmarkEntry(path)
            entry.load()
            options = entry.getOptions()
        except parser.ParserError, strerror:
            raise BookmarkException, strerror
        return options

    def renameBookmark(self, old_name, new_name):
        """Renames a bookmark.

        This renames the bookmark file to the new name, effectively changing the name
        used by the bookmark management system. The files names are relative names
        under the bookmark directory."""
        old = os.path.join(self.getBookmarkDirectory(), old_name)
        new = os.path.join(self.getBookmarkDirectory(), new_name)
        try:
            os.rename(old, new)
        except OSError, strerror:
            raise BookmarkException, strerror

    def removeBookmark(self, file):
        """Removes a bookmark from the bookmark directory.

        The specified file name is a relative path under the bookmark directory."""
        path = os.path.join(self.getBookmarkDirectory(), file)
        try:
            os.unlink(path)
        except OSError, strerror:
            raise BookmarkException, strerror

    def addBookmarkFolder(self, folder):
        """Adds a new sub-folder to the bookmark tree.

        The specified folder is a relative folder path name under the bookmark directory."""
        path = os.path.join(self.getBookmarkDirectory(), folder)
        try:
            os.mkdir(path, 0700)
        except OSError, strerror:
            raise BookmarkException, strerror

    def renameBookmarkFolder(self, old_name, new_name):
        """Renames a bookmark folder.

        The specified folder is a relative folder path name under the bookmark directory."""
        self.renameBookmark(old_name, new_name)

    def removeBookmarkFolder(self, folder):
        """Removes a bookmark folder.

        The specified folder is a relative folder path under the bookmark directory."""
        path = os.path.join(self.getBookmarkDirectory(), folder)
        try:
            os.rmdir(path)
        except OSError, strerror:
            raise BookmarkException, strerror
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.