Skip to content

Commit

Permalink
add simple type option and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Aug 3, 2018
1 parent 5eccf78 commit 2d3450b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ The functions return a list of tuples like:
```
where the second number is the percentage of code detected for that language.

### Examples
The primary reason behind creating this Python Linguist wrapper is automatically detecting the repo type, so that appropriate templates can be applied *en masse* to a large number of repos.
Thus to get the repo language from the command line, as GitHub would:
```sh
ghlinguist -t
```

or as a Python module:
```python
import ghlinguist as ghl

lang = ghl.linguist('~/mypath', rpath=True)
```

Both cases simply return the string `Python` or `Fortran` etc.

## Notes
ghLinguist parses text output from
[GitHub Linguist](https://github.com/github/linguist#using-emacs-or-vim-modelines),
Expand Down
10 changes: 7 additions & 3 deletions ghLinguist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ def main():
p = ArgumentParser()
p.add_argument('path', help='path to examine with GitHub Linguist',
nargs='?', default='.')
p.add_argument('-t', '--type', help='print only detected repo type (as GitHub would declare)', action='store_true')
p = p.parse_args()

langs = ghl.linguist(Path(p.path).expanduser())
langs = ghl.linguist(Path(p.path).expanduser(), rtype=p.type)

for l in langs:
print(f'{l[0]} {l[1]}%')
if isinstance(langs, str):
print(langs)
else:
for l in langs:
print(f'{l[0]} {l[1]}%')


if __name__ == '__main__':
Expand Down
7 changes: 5 additions & 2 deletions ghlinguist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import subprocess
from typing import List, Tuple
from typing import List, Tuple, Union
from pathlib import Path


def linguist(path: Path) -> List[Tuple[str, str]]:
def linguist(path: Path, rtype: bool=False) -> Union[str, List[Tuple[str, str]]]:

ret = subprocess.check_output(['linguist', str(path)],
universal_newlines=True).split('\n')
Expand All @@ -16,4 +16,7 @@ def linguist(path: Path) -> List[Tuple[str, str]]:
break
lpct.append((L[1], L[0][:-1]))

if rtype:
return lpct[0][0]

return lpct
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ghlinguist
version = 0.9.0
version = 0.9.1
author = Michael Hirsch, Ph.D.
url = https://github.com/scivision/linguist-python
description = simple command-line parser of GitHub Linguist Ruby program.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ def test_linguist():
assert langs[0][0] == 'Python'


def test_type():
lang = ghl.linguist(R, rtype=True)

assert lang == 'Python'


if __name__ == '__main__':
pytest.main(['-x', __file__])

0 comments on commit 2d3450b

Please sign in to comment.