Skip to content

Commit

Permalink
Merge TypeScript to JavaScript for linguist
Browse files Browse the repository at this point in the history
  • Loading branch information
hezyin committed Jun 11, 2019
1 parent 92d7f47 commit 56f2b41
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
44 changes: 37 additions & 7 deletions persper/analytics/multi_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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')
Expand Down
21 changes: 18 additions & 3 deletions test/test_analytics/test_multi_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@


@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):
Repo.clone_from(repo_url, 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)

0 comments on commit 56f2b41

Please sign in to comment.