Esempio n. 1
0
def language_percentage(file_paths):
    """
    Computes the percentage composition of each language, with unknown
    extensions tagged with the ``Unknown`` key.

    :param file_paths: A list of file paths.
    :return:           A dict with file name as key and the percentage
                       of occurences as the value.
    """
    if file_paths:
        delta = 100 / len(file_paths)

    results = defaultdict(lambda: 0)
    for file_path in file_paths:
        ext = os.path.splitext(file_path)[1]

        if ext in exts:
            for lang in exts[ext]:
                results[lang] += delta

            continue

        hashbang = get_hashbang(file_path)

        if not hashbang:
            continue

        language = get_language_from_hashbang(hashbang).lower()
        for ext in exts:
            for lang in exts[ext]:
                if language == lang.lower():
                    results[lang.lower()] += delta

    return results
Esempio n. 2
0
def language_percentage(file_paths):
    """
    Computes the percentage composition of each language, with unknown
    extensions tagged with the ``Unknown`` key.

    :param file_paths: A list of file paths.
    :return:           A dict with file name as key and the percentage
                       of occurences as the value.
    """
    if file_paths:
        delta = 100 / len(file_paths)

    results = defaultdict(lambda: 0)
    for file_path in file_paths:
        ext = os.path.splitext(file_path)[1]

        if ext in exts:
            for lang in exts[ext]:
                results[lang] += delta

            continue

        hashbang = get_hashbang(file_path)

        if not hashbang:
            continue

        language = get_language_from_hashbang(hashbang).lower()
        for ext in exts:
            for lang in exts[ext]:
                if language == lang.lower():
                    results[lang.lower()] += delta

    return results
def language_percentage(file_paths):
    """
    Computes the percentage composition of each language, with unknown
    extensions tagged with the ``Unknown`` key.

    :param file_paths: A list of file paths.
    :return:           A dict with file name as key and the percentage
                       of occurences as the value.
    """
    if file_paths:
        delta = 100 / len(file_paths)

    results = defaultdict(lambda: 0)
    for file_path in file_paths:
        ext = os.path.splitext(file_path)[1]

        if ext in exts:
            for lang in exts[ext]:
                results[lang] += delta

        elif os.path.exists(file_path):
            with open(file_path, 'r') as data:
                hashbang = data.readline()
                if re.match(HASHBANG_REGEX, hashbang):
                    language = get_language_from_hashbang(hashbang).lower()
                    for ext in exts:
                        for lang in exts[ext]:
                            if language == lang.lower():
                                results[lang.lower()] += delta

    return results
Esempio n. 4
0
 def test_get_language_from_hashbang(self):
     self.assertEqual(get_language_from_hashbang('#!/usr/bin/env python'),
                      'python')
     self.assertEqual(get_language_from_hashbang('#!bin/bash'), 'bash')