diff --git a/persper/analytics/multi_analyzer.py b/persper/analytics/multi_analyzer.py index 6b424aa81c8..9e5e04c1871 100644 --- a/persper/analytics/multi_analyzer.py +++ b/persper/analytics/multi_analyzer.py @@ -2,6 +2,7 @@ import json import pickle from git import Repo +from enum import Enum from Naked.toolshed.shell import muterun_rb from persper.util.path import root_path from persper.analytics.c import CGraphServer @@ -12,6 +13,20 @@ from persper.analytics.analyzer2 import Analyzer, AnalyzerObserver, emptyAnalyzerObserver +class Linguist(Enum): + """ + Encodes the relationship between a language and its name in linguist's output + https://github.com/github/linguist/blob/master/lib/linguist/languages.yml + """ + C = "C" + CPP = "C++" + GO = "Go" + JAVA = "Java" + JAVASCRIPT = "JavaScript" + TYPESCRIPT = "TypeScript" + VUE = "Vue" + + class MultiAnalyzer: LANGUAGE_THRESHOLD = 0.3 @@ -35,18 +50,33 @@ def _set_linguist(self): for k in lang_dict.keys(): lang_dict[k] = lang_dict[k] * 1.0 / total_lines + js = Linguist.JAVASCRIPT.value + ts = Linguist.TYPESCRIPT.value + vue = Linguist.VUE.value + + print(lang_dict) + if vue in lang_dict: + print("Merging Vue to Javascript...") + if js not in lang_dict: + lang_dict[js] = 0 + lang_dict[js] += lang_dict[vue] + del lang_dict[vue] + print(lang_dict) + + if ts in lang_dict: + print("Merging TypeScript to JavaScript...") + if js not in lang_dict: + lang_dict[js] = 0 + lang_dict[js] += lang_dict[ts] + del lang_dict[ts] + print(lang_dict) + + # intersect with what languages we support for lang, value in lang_dict.items(): if lang in self._supported_analyzers().keys(): self._linguist[lang] = value print(self._linguist) - if "Vue" in self._linguist: - print("Merging Vue to Javascript...") - if "JavaScript" not in self._linguist: - self._linguist["JavaScript"] = 0 - self._linguist["JavaScript"] += self._linguist["Vue"] - del self._linguist["Vue"] - print(self._linguist) else: print('Analyzing Language Error from Linguist') diff --git a/test/test_analytics/test_multi_analyzer.py b/test/test_analytics/test_multi_analyzer.py index 1e9b70d1942..c2bbaa6ae56 100644 --- a/test/test_analytics/test_multi_analyzer.py +++ b/test/test_analytics/test_multi_analyzer.py @@ -6,7 +6,7 @@ @pytest.fixture(scope='module') -def repo_path(): +def vue_repo_path(): repo_path = os.path.join(root_path, "repos/vue-realworld-example-app") repo_url = 'https://github.com/gothinkster/vue-realworld-example-app' if not os.path.exists(repo_path): @@ -14,5 +14,20 @@ def repo_path(): return repo_path -def test_set_linguist(repo_path): - maz = MultiAnalyzer(repo_path) +@pytest.fixture(scope='module') +def ts_repo_path(): + repo_path = os.path.join(root_path, "repos/TypeScriptSamples") + repo_url = 'https://github.com/microsoft/TypeScriptSamples' + if not os.path.exists(repo_path): + Repo.clone_from(repo_url, repo_path) + return repo_path + + +def test_set_linguist_vue(vue_repo_path): + # _set_linguist is called during the initialization of MultiAnalyzer + maz = MultiAnalyzer(vue_repo_path) + + +def test_set_linguist_ts(ts_repo_path): + # _set_linguist is called during the initialization of MultiAnalyzer + maz = MultiAnalyzer(ts_repo_path)