示例#1
0
def load(config):
    """Returns a dictonary (key=path, value=weight) loaded from data file."""
    xdg_aj_home = os.path.join(os.path.expanduser('~'), '.local', 'share',
                               'autojump')

    if is_osx() and os.path.exists(xdg_aj_home):
        migrate_osx_xdg_data(config)

    if not os.path.exists(config['data_path']):
        return {}

    # example: u'10.0\t/home/user\n' -> ['10.0', u'/home/user']
    parse = lambda line: line.strip().split('\t')

    correct_length = lambda x: len(x) == 2

    # example: ['10.0', u'/home/user'] -> (u'/home/user', 10.0)
    tupleize = lambda x: (x[1], float(x[0]))

    try:
        with open(config['data_path'], 'r', encoding='utf-8',
                  errors='replace') as f:
            return dict(imap(tupleize, ifilter(correct_length, imap(parse,
                                                                    f))))
    except (IOError, EOFError):
        return load_backup(config)
示例#2
0
def load(config):
    """Returns a dictonary (key=path, value=weight) loaded from data file."""
    xdg_aj_home = os.path.join(
        os.path.expanduser('~'),
        '.local',
        'share',
        'autojump')

    if is_osx() and os.path.exists(xdg_aj_home):
        migrate_osx_xdg_data(config)

    if not os.path.exists(config['data_path']):
        return {}

    # example: u'10.0\t/home/user\n' -> ['10.0', u'/home/user']
    parse = lambda line: line.strip().split('\t')

    correct_length = lambda x: len(x) == 2

    # example: ['10.0', u'/home/user'] -> (u'/home/user', 10.0)
    tupleize = lambda x: (x[1], float(x[0]))

    try:
        with open(
                config['data_path'],
                'r', encoding='utf-8',
                errors='replace') as f:
            return dict(
                imap(
                    tupleize,
                    ifilter(correct_length, imap(parse, f))))
    except (IOError, EOFError):
        return load_backup(config)
示例#3
0
def migrate_osx_xdg_data(config):
    """
    Older versions incorrectly used Linux XDG_DATA_HOME paths on OS X. This
    migrates autojump files from ~/.local/share/autojump to ~/Library/autojump
    """
    assert is_osx(), 'This function should only be run on OS X.'

    xdg_data_home = os.path.join(os.path.expanduser('~'), '.local', 'share')
    xdg_aj_home = os.path.join(xdg_data_home, 'autojump')
    data_path = os.path.join(xdg_aj_home, 'autojump.txt')
    backup_path = os.path.join(xdg_aj_home, 'autojump.txt.bak')

    if os.path.exists(data_path):
        move_file(data_path, config['data_path'])
    if os.path.exists(backup_path):
        move_file(backup_path, config['backup_path'])

    # cleanup
    shutil.rmtree(xdg_aj_home)
    if len(os.listdir(xdg_data_home)) == 0:
        shutil.rmtree(xdg_data_home)
示例#4
0
def migrate_osx_xdg_data(config):
    """
    Older versions incorrectly used Linux XDG_DATA_HOME paths on OS X. This
    migrates autojump files from ~/.local/share/autojump to ~/Library/autojump
    """
    assert is_osx(), 'This function should only be run on OS X.'

    xdg_data_home = os.path.join(os.path.expanduser('~'), '.local', 'share')
    xdg_aj_home = os.path.join(xdg_data_home, 'autojump')
    data_path = os.path.join(xdg_aj_home, 'autojump.txt')
    backup_path = os.path.join(xdg_aj_home, 'autojump.txt.bak')

    if os.path.exists(data_path):
        move_file(data_path, config['data_path'])
    if os.path.exists(backup_path):
        move_file(backup_path, config['backup_path'])

    # cleanup
    shutil.rmtree(xdg_aj_home)
    if len(os.listdir(xdg_data_home)) == 0:
        shutil.rmtree(xdg_data_home)
示例#5
0
def migrate_osx_xdg_data(config):
    """
    Older versions incorrectly used Linux XDG_DATA_HOME paths on OS X. This
    migrates autojump files from ~/.local/share/autojump to ~/Library/autojump
    """
    assert is_osx(), "This function should only be run on OS X."

    xdg_data_home = os.path.join(os.path.expanduser("~"), ".local", "share")
    xdg_aj_home = os.path.join(xdg_data_home, "autojump")
    data_path = os.path.join(xdg_aj_home, "autojump.txt")
    backup_path = os.path.join(xdg_aj_home, "autojump.txt.bak")

    if os.path.exists(data_path):
        move_file(data_path, config["data_path"])
    if os.path.exists(backup_path):
        move_file(backup_path, config["backup_path"])

    # cleanup
    shutil.rmtree(xdg_aj_home)
    if len(os.listdir(xdg_data_home)) == 0:
        shutil.rmtree(xdg_data_home)
示例#6
0
def migrate_osx_xdg_data(config):
    """
    Older versions incorrectly used Linux XDG_DATA_HOME paths on OS X. This
    migrates autojump files from ~/.local/share/autojump to ~/Library/autojump
    """
    assert is_osx(), "This function should only be run on OS X."

    xdg_data_home = os.path.join(os.path.expanduser("~"), ".local", "share")
    xdg_aj_home = os.path.join(xdg_data_home, "autojump")
    data_path = (os.path.join(xdg_aj_home, "autojump.txt"),)
    backup_path = (os.path.join(xdg_aj_home, "autojump.txt.bak"),)

    if os.path.exists(data_path):
        move_file(data_path, config["data_path"])
    if os.path.exists(backup_path):
        move_file(backup_path, config["backup_path"])

    # cleanup
    shutil.rmtree(xdg_aj_home)
    if len(os.listdir(xdg_data_home)) == 0:
        shutil.rmtree(xdg_data_home)
示例#7
0
def load(config):
    """Returns a dictonary (key=path, value=weight) loaded from data file."""
    xdg_aj_home = os.path.join(os.path.expanduser("~"), ".local", "share", "autojump")

    if is_osx() and os.path.exists(xdg_aj_home):
        migrate_osx_xdg_data(config)

    if os.path.exists(config["data_path"]):
        # example: u'10.0\t/home/user\n' -> ['10.0', u'/home/user']
        parse = lambda line: line.strip().split("\t")

        correct_length = lambda x: len(x) == 2

        # example: ['10.0', u'/home/user'] -> (u'/home/user', 10.0)
        tupleize = lambda x: (x[1], float(x[0]))

        try:
            with open(config["data_path"], "r", encoding="utf-8", errors="replace") as f:
                return dict(imap(tupleize, ifilter(correct_length, imap(parse, f))))
        except (IOError, EOFError):
            return load_backup(config)

    return {}