<commit_before>"""
Installation of vim packages.

Copyright (C) 2014 <NAME>
Distributed under the LGPL v2.1; see LICENSE.txt
"""

from zipfile import ZipFile
import contextlib
import os
import shutil
import sys
import tarfile
import threading
import urllib2

from vimp.log import (verb)
from vimp.scripts import (SCRIPTS, ALIASES)
from vimp.util import (
    copyfile,
    exists,
    joinpath,
    mkdir,
    pathname,
    readlink,
    symlink,
    unlinktree,
)


def get_full_name(name):
    return ALIASES[name] if name in ALIASES else name

def get_short_name(fullname):
    for k in ALIASES.keys():
      if ALIASES[k] == fullname:
        return k
    return fullname

def has_vimprc(text):
    vimprc = getpath("vimprc")
    with open(vimprc, "rt") as f:
        for l in f.readlines():
            if l.startswith(text):
                return True
    return False

def remove_vimprc(name, text):
    verb("Warning: Unimplemented remove_vimprc")
    pass

def add_vimprc(name, lines):
    """Adds a line to .vimp/vimrc."""
    tab = "  "
    indent = 0

    if len(lines) == 0:
        return

    # Check if text is already there
    header = "\" ==== %s ====" % name
    if has_vimprc(header):
        verb("There is already an entry in .vimp/vimrc for %s" % name)
        return

    verb("Adding entry for %s to .vimp/vimrc" % name)
    with open(getpath("vimprc"), "at") as f:
        f.write("\n" + header + "\n")

        # Check if symlinks exists before executing commands
        s = get_script(name)
        tab = "  "
        for (_, dst) in s["symlink"]:
            dst = expandvars(name, dst)
            f.write(tab*indent + "if filereadable(\"%s\")\n" % dst)
            indent += 1

        for line in lines:
            f.write(tab*indent + "%s\n" % expandvars(name, line))

        for (_, dst) in s["symlink"]:
            indent -= 1
            f.write(tab*indent + "endif\n")

def helptags(name, path):
    s =  "if !filereadable(\"%s/tags\")\n" % path
    s += "  call pathogen#helptags()\n"
    s += "endif\n"
    return s

def getpath(label, name=""):
    """Returns full path to various parts of vimp and vim."""
    full = get_full_name(name)
    vimp = os.path.expanduser(joinpath("~", ".vimp"))
    vimrc = os.path.expanduser(joinpath("~", ".vimrc"))
    vim = os.path.expanduser(joinpath("~", ".vim"))

    if label == "vim":
        return vim

    if label == "list":
        return joinpath(vimp, "list")

    if label == "vimrc":
        return vimrc

    if label == "vimp":
        return vimp

    if label == "vimprc":
        return joinpath(vimp, "vimrc")

    if label == "install":
        return joinpath(vimp, "installed", full)

    if label == "bundle":
        return joinpath(vim, "bundle", get_short_name(name))

    if label == "download":
        return joinpath(vimp, "download", full)

    if label == "colors":
        return joinpath(vim, "colors")

    print("Error: Unknown path label: %s" % label)
    sys.exit(1)

def download(url, filename, skip_existing=True):
    """Stores download in .vimp/downloads/ and returns filename."""
    if exists(filename) and skip_existing:
        return
    else:
        mkdir(pathname(filename))

    verb("Downloading %s -> %s" % (url, filename))
    resp = urllib2.urlopen(url)<commit_msg>Uses dict w/lambdas as a switch-statement.<commit_after>"""
Installation of vim packages.

Copyright (C) 2014 <NAME>
Distributed under the LGPL v2.1; see LICENSE.txt
"""

from zipfile import ZipFile
import contextlib
import os
import shutil
import sys
import tarfile
import threading
import urllib2

from vimp.log import (verb)
from vimp.scripts import (SCRIPTS, ALIASES)
from vimp.util import (
    copyfile,
    exists,
    joinpath,
    mkdir,
    pathname,
    readlink,
    symlink,
    unlinktree,
)


def get_full_name(name):
    return ALIASES[name] if name in ALIASES else name

def get_short_name(fullname):
    for k in ALIASES.keys():
      if ALIASES[k] == fullname:
        return k
    return fullname

def has_vimprc(text):
    vimprc = getpath("vimprc")
    with open(vimprc, "rt") as f:
        for l in f.readlines():
            if l.startswith(text):
                return True
    return False

def remove_vimprc(name, text):
    verb("Warning: Unimplemented remove_vimprc")
    pass

def add_vimprc(name, lines):
    """Adds a line to .vimp/vimrc."""
    tab = "  "
    indent = 0

    if len(lines) == 0:
        return

    # Check if text is already there
    header = "\" ==== %s ====" % name
    if has_vimprc(header):
        verb("There is already an entry in .vimp/vimrc for %s" % name)
        return

    verb("Adding entry for %s to .vimp/vimrc" % name)
    with open(getpath("vimprc"), "at") as f:
        f.write("\n" + header + "\n")

        # Check if symlinks exists before executing commands
        s = get_script(name)
        tab = "  "
        for (_, dst) in s["symlink"]:
            dst = expandvars(name, dst)
            f.write(tab*indent + "if filereadable(\"%s\")\n" % dst)
            indent += 1

        for line in lines:
            f.write(tab*indent + "%s\n" % expandvars(name, line))

        for (_, dst) in s["symlink"]:
            indent -= 1
            f.write(tab*indent + "endif\n")

def helptags(name, path):
    s =  "if !filereadable(\"%s/tags\")\n" % path
    s += "  call pathogen#helptags()\n"
    s += "endif\n"
    return s

def getpath(label, name=""):
    """Returns full path to various parts of vimp and vim."""
    full = get_full_name(name)
    vimp = os.path.expanduser(joinpath("~", ".vimp"))
    vimrc = os.path.expanduser(joinpath("~", ".vimrc"))
    vim = os.path.expanduser(joinpath("~", ".vim"))

    def unknown_label():
        print("Error: Unknown path label: %s" % label)
        sys.exit(1)

    # Poor man's switch-statement (I don't like long if-elif blocks, sorry!)
    return {
        "vim":      lambda: vim,
        "list":     lambda: joinpath(vimp, "list"),
        "vimrc":    lambda: vimrc,
        "vimp":     lambda: vimp,
        "vimprc":   lambda: joinpath(vimp, "vimrc"),
        "install":  lambda: joinpath(vimp, "installed", full),
        "bundle":   lambda: joinpath(vim, "bundle", get_short_name(name)),
        "download": lambda: joinpath(vimp, "download", full),
        "colors":   lambda: joinpath(vim, "colors"),
     }.get(label, unknown_label)()

def download(url, filename, skip_existing=True):
    """Stores download in .vimp/downloads/ and returns filename."""
    if exists(filename) and skip_existing:
        return
    else:
        mkdir(pathname(filename))

    verb("Downloading %s -> %s" % (url, filename))
    resp = urllib2.urlopen(url)
{'fraction_non_alphanumeric': 0.13851452530697814, 'fraction_numerical': 0.003893381251871818, 'mean_word_length': 2.5078781512605044, 'pattern_counts': {'":': 18, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3659484', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2237, 'n_words': 637}