コード例 #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
コード例 #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
コード例 #3
0
 def test_no_eol(self):
     with NamedTemporaryFile(mode='w+t', delete=False) as temp_file:
         temp_file.write('#!bin/bash')
         temp_file.close()
         self.assertEqual(get_hashbang(temp_file.name), '#!bin/bash')
コード例 #4
0
 def test_missing_file(self):
     self.assertIsNone(get_hashbang('does_not_exist'))
コード例 #5
0
 def test_no_hash(self):
     with NamedTemporaryFile(mode='w+t', delete=False) as temp_file:
         temp_file.write('!bin/bash')
         temp_file.close()
         self.assertIsNone(get_hashbang(temp_file.name))
コード例 #6
0
 def test_non_unicode_file(self):
     with NamedTemporaryFile(mode='w+b', delete=False) as temp_file:
         temp_file.write(b'\2000x80')
         temp_file.close()
         self.assertIsNone(get_hashbang(temp_file.name))