Helpviewer/Bookmarks/BookmarksImporters/IExplorerImporter.py

Wed, 02 Jan 2013 10:31:48 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 02 Jan 2013 10:31:48 +0100
changeset 2302
f29e9405c851
parent 1717
ba85828cd357
child 2403
e3d7a861547c
child 3163
9f50365a0870
permissions
-rw-r--r--

Updated copyright for 2013.

# -*- coding: utf-8 -*-

# Copyright (c) 2012 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing an importer for Internet Explorer bookmarks.
"""

import os

from PyQt4.QtCore import QCoreApplication, QDate, Qt

from ..BookmarkNode import BookmarkNode

from .BookmarksImporter import BookmarksImporter

import UI.PixmapCache
import Globals


def getImporterInfo(id):
    """
    Module function to get information for the given source id.
    
    @return tuple with an icon (QPixmap), readable name (string), name of
        the default bookmarks file (string), an info text (string),
        a prompt (string) and the default directory of the bookmarks file (string)
    """
    if id == "ie":
        if Globals.isWindowsPlatform():
            standardDir = os.path.expandvars(
                "%USERPROFILE%\\Favorites")
        else:
            standardDir = ""
        return (
            UI.PixmapCache.getPixmap("internet_explorer.png"),
            "Internet Explorer",
            "",
            QCoreApplication.translate("IExplorerImporter",
                """Internet Explorer stores its bookmarks in the <b>Favorites</b> """
                """folder This folder is usually located in"""),
            QCoreApplication.translate("IExplorerImporter",
                """Please choose the folder to begin importing bookmarks."""),
            standardDir,
        )
    else:
        raise ValueError("Unsupported browser ID given ({0}).".format(id))


class IExplorerImporter(BookmarksImporter):
    """
    Class implementing the Chrome bookmarks importer.
    """
    def __init__(self, id="", parent=None):
        """
        Constructor
        
        @param id source ID (string)
        @param parent reference to the parent object (QObject)
        """
        super().__init__(id, parent)
        
        self.__fileName = ""
    
    def setPath(self, path):
        """
        Public method to set the path of the bookmarks file or directory.
        
        @param path bookmarks file or directory (string)
        """
        self.__fileName = path
    
    def open(self):
        """
        Public method to open the bookmarks file.
        
        @return flag indicating success (boolean)
        """
        if not os.path.exists(self.__fileName):
            self._error = True
            self._errorString = self.trUtf8("Folder '{0}' does not exist.")\
                .format(self.__fileName)
            return False
        if not os.path.isdir(self.__fileName):
            self._error = True
            self._errorString = self.trUtf8("'{0}' is not a folder.")\
                .format(self.__fileName)
        return True
    
    def importedBookmarks(self):
        """
        Public method to get the imported bookmarks.
        
        @return imported bookmarks (BookmarkNode)
        """
        folders = {}
        
        importRootNode = BookmarkNode(BookmarkNode.Folder)
        folders[self.__fileName] = importRootNode
        
        for dir, subdirs, files in os.walk(self.__fileName):
            for subdir in subdirs:
                path = os.path.join(dir, subdir)
                if dir in folders:
                    folder = BookmarkNode(BookmarkNode.Folder, folders[dir])
                else:
                    folder = BookmarkNode(BookmarkNode.Folder, importRootNode)
                folder.title = subdir.replace("&", "&&")
                folders[path] = folder
            
            for file in files:
                name, ext = os.path.splitext(file)
                if ext.lower() == ".url":
                    path = os.path.join(dir, file)
                    try:
                        f = open(path, "r")
                        contents = f.read()
                        f.close()
                    except IOError:
                        continue
                    url = ""
                    for line in contents.splitlines():
                        if line.startswith("URL="):
                            url = line.replace("URL=", "")
                            break
                    if url:
                        if dir in folders:
                            bookmark = BookmarkNode(BookmarkNode.Bookmark, folders[dir])
                        else:
                            bookmark = BookmarkNode(BookmarkNode.Bookmark, importRootNode)
                        bookmark.url = url
                        bookmark.title = name.replace("&", "&&")
        
        if self._id == "ie":
            importRootNode.title = self.trUtf8("Internet Explorer Import")
        else:
            importRootNode.title = self.trUtf8("Imported {0}")\
                .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate))
        return importRootNode

eric ide

mercurial