コード例 #1
0
def import_folder(node, filename, task=None):
    """
    Import a folder tree as a subfolder of the current item

    node     -- node to attach folder to
    filename -- filename of folder to import
    task     -- Task object to track progress
    """

    # TODO: Exceptions, intelligent error handling
    # For windows: 
    # Deep paths are handled by unicode "\\?\" extension to filename.

    if task is None:
        # create dummy task if needed
        task = tasklib.Task()    

    # Determine number of files in advance so we can have a progress bar
    nfiles = 0
    nfilescomplete = 0 # updates progress bar
    for root, dirs, files in os.walk(filename):
        nfiles += len(files) # Add files found in current dir
        task.set_message(("text", "Found %i files..." % nfiles))

    # Make a node based on the root - so we have an origin to import to
    rootnode = node.new_child(CONTENT_TYPE_DIR, os.path.basename(filename))
    rootnode.set_attr("title", os.path.basename(filename))
    filename2node = {filename: rootnode}
    
    # Walk directory we're importing and create nodes
    for root, dirs, files in os.walk(filename):
        
        # create node for directory
        if root == filename:
            parent = rootnode
        else:
            parent2 = filename2node.get(os.path.dirname(root), None)
            if parent2 is None:
                continue
            
            parent = parent2.new_child(CONTENT_TYPE_DIR,
                                       os.path.basename(root))
            parent.set_attr("title", os.path.basename(root))
            filename2node[root] = parent

        
        # create nodes for files
        for fn in files:
            if keepnote.get_platform() is "windows":
                fn = "\\\\?\\" + os.path.join(root, fn)
            else:
                fn = os.path.join(root, fn)
            child = attach_file(fn, parent)
            
            nfilescomplete += 1
            task.set_message(("text", "Imported %i / %i files..." % 
                              (nfilescomplete, nfiles)))
            task.set_percent(float(nfilescomplete) / float(nfiles))

    task.finish()
コード例 #2
0
ファイル: pref.py プロジェクト: vatslav/perfectnote
def upgrade_user_pref_dir(old_user_pref_dir, new_user_pref_dir):
    """Moves preference data from old location to new one"""

    import sys
    
    # move user preference directory
    shutil.copytree(old_user_pref_dir, new_user_pref_dir)

    # rename takenote.xml to keepnote.xml
    oldfile = os.path.join(new_user_pref_dir, OLD_USER_PREF_FILE)
    newfile = os.path.join(new_user_pref_dir, USER_PREF_FILE)

    if os.path.exists(oldfile):
        os.rename(oldfile, newfile)
    
        # rename root xml tag
        tree = ElementTree.ElementTree(file=newfile)
        elm = tree.getroot()
        elm.tag = "keepnote"
        tree.write(newfile, encoding="UTF-8")

    # move over data files from .local/share/takenote
    if keepnote.get_platform() in ("unix", "darwin"):
        datadir = os.path.join(get_home(), ".local", "share", "takenote")
        
        old_ext_dir = os.path.join(datadir, "extensions")
        new_ext_dir = os.path.join(new_user_pref_dir, "extensions")    
        if not os.path.exists(new_ext_dir) and os.path.exists(old_ext_dir):
            shutil.copytree(old_ext_dir, new_ext_dir)

        old_ext_dir = os.path.join(datadir, "extensions_data")
        new_ext_dir = os.path.join(new_user_pref_dir, "extensions_data")    
        if not os.path.exists(new_ext_dir) and os.path.exists(old_ext_dir):
            shutil.copytree(old_ext_dir, new_ext_dir)
コード例 #3
0
def import_folder(node, filename, task=None):
    """
    Import a folder tree as a subfolder of the current item

    node     -- node to attach folder to
    filename -- filename of folder to import
    task     -- Task object to track progress
    """

    # TODO: Exceptions, intelligent error handling
    # For windows:
    # Deep paths are handled by unicode "\\?\" extension to filename.

    if task is None:
        # create dummy task if needed
        task = tasklib.Task()

    # Determine number of files in advance so we can have a progress bar
    nfiles = 0
    nfilescomplete = 0  # updates progress bar
    for root, dirs, files in os.walk(filename):
        nfiles += len(files)  # Add files found in current dir
        task.set_message(("text", "Found %i files..." % nfiles))

    # Make a node based on the root - so we have an origin to import to
    rootnode = node.new_child(CONTENT_TYPE_DIR, os.path.basename(filename))
    rootnode.set_attr("title", os.path.basename(filename))
    filename2node = {filename: rootnode}

    # Walk directory we're importing and create nodes
    for root, dirs, files in os.walk(filename):

        # create node for directory
        if root == filename:
            parent = rootnode
        else:
            parent2 = filename2node.get(os.path.dirname(root), None)
            if parent2 is None:
                continue

            parent = parent2.new_child(CONTENT_TYPE_DIR,
                                       os.path.basename(root))
            parent.set_attr("title", os.path.basename(root))
            filename2node[root] = parent

        # create nodes for files
        for fn in files:
            if keepnote.get_platform() is "windows":
                fn = "\\\\?\\" + os.path.join(root, fn)
            else:
                fn = os.path.join(root, fn)
            child = attach_file(fn, parent)

            nfilescomplete += 1
            task.set_message(
                ("text",
                 "Imported %i / %i files..." % (nfilescomplete, nfiles)))
            task.set_percent(float(nfilescomplete) / float(nfiles))

    task.finish()
コード例 #4
0
ファイル: basetreeview.py プロジェクト: brotchie/keepnote
    def _on_drag_data_received(self, treeview, drag_context, x, y,
                               selection_data, info, eventtime):

        """
        Callback for when data is received from source widget
        """
        
        # override gtk's data received code
        self.stop_emission("drag-data-received")

        # NOTE: force one more call to motion, since Windows ignores
        # cross app drag calls
        self._on_drag_motion(treeview, drag_context, x, y, eventtime,
                             stop_emit=False)

        
        # if no destination, give up.  Occurs when drop is not allowed
        if self._dest_row is None:
            drag_context.finish(False, False, eventtime)
            return

        
        if "drop_node" in drag_context.targets:
            # process node drops
            self._on_drag_node_received(treeview, drag_context, x, y,
                                        selection_data, info, eventtime)
            
        elif "text/uri-list" in drag_context.targets:
            target_path, drop_position  = self._dest_row
            target = self.model.get_iter(target_path)
            target_node = self.model.get_value(target, self._node_col)
            
            if self._drop_allowed(None, target_node, drop_position):
                new_path = compute_new_path(self.model, target, drop_position)
                parent = self._get_node_from_path(new_path[:-1])

                uris = parse_utf(selection_data.data)
                uris = [x for x in (urllib.unquote(uri.strip())
                                    for uri in uris.split("\n"))
                        if len(x) > 0 and x[0] != "#"]

                for uri in reversed(uris):
                    if uri.startswith("file://"):
                        uri = uri[7:]
                        if keepnote.get_platform() == "windows":
                            # remove one more '/' for windows
                            uri  = uri[1:]
                    self.emit("drop-file", parent, new_path[-1], uri)
            drag_context.finish(True, False, eventtime)
            
        else:
            # unknown drop type, reject
            drag_context.finish(False, False, eventtime)
コード例 #5
0
    def _on_drag_data_received(self, treeview, drag_context, x, y,
                               selection_data, info, eventtime):

        """
        Callback for when data is received from source widget
        """
        
        # override gtk's data received code
        self.stop_emission("drag-data-received")

        # NOTE: force one more call to motion, since Windows ignores
        # cross app drag calls
        self._on_drag_motion(treeview, drag_context, x, y, eventtime,
                             stop_emit=False)

        
        # if no destination, give up.  Occurs when drop is not allowed
        if self._dest_row is None:
            drag_context.finish(False, False, eventtime)
            return

        
        if "drop_node" in drag_context.targets:
            # process node drops
            self._on_drag_node_received(treeview, drag_context, x, y,
                                        selection_data, info, eventtime)
            
        elif "text/uri-list" in drag_context.targets:
            target_path, drop_position  = self._dest_row
            target = self.model.get_iter(target_path)
            target_node = self.model.get_value(target, self._node_col)
            
            if self._drop_allowed(None, target_node, drop_position):
                new_path = compute_new_path(self.model, target, drop_position)
                parent = self._get_node_from_path(new_path[:-1])

                uris = parse_utf(selection_data.data)
                uris = [x for x in (urllib.unquote(uri.strip())
                                    for uri in uris.split("\n"))
                        if len(x) > 0 and x[0] != "#"]

                for uri in reversed(uris):
                    if uri.startswith("file://"):
                        uri = uri[7:]
                        if keepnote.get_platform() == "windows":
                            # remove one more '/' for windows
                            uri  = uri[1:]
                    self.emit("drop-file", parent, new_path[-1], uri)
            drag_context.finish(True, False, eventtime)
            
        else:
            # unknown drop type, reject
            drag_context.finish(False, False, eventtime)
コード例 #6
0
ファイル: pref.py プロジェクト: vatslav/perfectnote
def get_new_user_pref_dir(home=None):
    """Returns the directory of the application preference file"""
    
    p = keepnote.get_platform()
    if p == "unix" or p == "darwin":
        
        if home is None:
            home = get_home()
        return xdg.get_config_file(USER_PREF_DIR, default=True)

    elif p == "windows":
        appdata = keepnote.get_win_env("APPDATA")
        if appdata is None:
            raise keepnote.EnvError("APPDATA environment variable must be specified")
        return os.path.join(appdata, USER_PREF_DIR)

    else:
        raise Exception("unknown platform '%s'" % p)
コード例 #7
0
ファイル: __init__.py プロジェクト: brotchie/keepnote
# constants

MAX_RECENT_NOTEBOOKS = 20
ACCEL_FILE = u"accel.txt"
IMAGE_DIR = u"images"
CONTEXT_MENU_ACCEL_PATH = "<main>/context_menu"

DEFAULT_AUTOSAVE_TIME = 10 * 1000  # 10 sec (in msec)


# font constants
DEFAULT_FONT_FAMILY = "Sans"
DEFAULT_FONT_SIZE = 10
DEFAULT_FONT = "%s %d" % (DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE)

if keepnote.get_platform() == "darwin":
    CLIPBOARD_NAME = gdk.SELECTION_PRIMARY
else:
    CLIPBOARD_NAME = "CLIPBOARD"


DEFAULT_COLORS_FLOAT = [
    # lights
    (1, 0.6, 0.6),
    (1, 0.8, 0.6),
    (1, 1, 0.6),
    (0.6, 1, 0.6),
    (0.6, 1, 1),
    (0.6, 0.6, 1),
    (1, 0.6, 1),
    # trues
コード例 #8
0
ファイル: __init__.py プロジェクト: InfosecSapper/keepnote
#=============================================================================
# constants

MAX_RECENT_NOTEBOOKS = 20
ACCEL_FILE = u"accel.txt"
IMAGE_DIR = u"images"
CONTEXT_MENU_ACCEL_PATH = "<main>/context_menu"

DEFAULT_AUTOSAVE_TIME = 10 * 1000  # 10 sec (in msec)

# font constants
DEFAULT_FONT_FAMILY = "Sans"
DEFAULT_FONT_SIZE = 10
DEFAULT_FONT = "%s %d" % (DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE)

if keepnote.get_platform() == "darwin":
    CLIPBOARD_NAME = gdk.SELECTION_PRIMARY
else:
    CLIPBOARD_NAME = "CLIPBOARD"

DEFAULT_COLORS_FLOAT = [
    # lights
    (1, .6, .6),
    (1, .8, .6),
    (1, 1, .6),
    (.6, 1, .6),
    (.6, 1, 1),
    (.6, .6, 1),
    (1, .6, 1),

    # trues