From 090b44edd8bf42e3d9574680dfa2e27cae8e595e Mon Sep 17 00:00:00 2001 From: Roman Gräf Date: Sun, 21 Jun 2020 02:44:06 +0200 Subject: pypi release --- .gitignore | 2 ++ README.md | 15 ++++++++--- lib/argparse/pathtype.py | 63 --------------------------------------------- lib/print_stats.py | 17 ------------ lib/stats.py | 26 ------------------- main.py | 37 -------------------------- pkg/ins.sc | 31 ---------------------- setup.py | 34 ++++++++++++++++++++++++ tabsvsspaces/__init__.py | 4 +++ tabsvsspaces/__main__.py | 4 +++ tabsvsspaces/_main.py | 31 ++++++++++++++++++++++ tabsvsspaces/pathtype.py | 63 +++++++++++++++++++++++++++++++++++++++++++++ tabsvsspaces/print_stats.py | 17 ++++++++++++ tabsvsspaces/stats.py | 26 +++++++++++++++++++ 14 files changed, 192 insertions(+), 178 deletions(-) delete mode 100644 lib/argparse/pathtype.py delete mode 100644 lib/print_stats.py delete mode 100644 lib/stats.py delete mode 100755 main.py delete mode 100644 pkg/ins.sc create mode 100644 setup.py create mode 100644 tabsvsspaces/__init__.py create mode 100644 tabsvsspaces/__main__.py create mode 100644 tabsvsspaces/_main.py create mode 100644 tabsvsspaces/pathtype.py create mode 100644 tabsvsspaces/print_stats.py create mode 100644 tabsvsspaces/stats.py diff --git a/.gitignore b/.gitignore index e265113..ee9b02d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ report venv/ +dist/ +build/ diff --git a/README.md b/README.md index d41c973..f8df54a 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,10 @@ ## Tabs Vs Spaces +Use this tool to count the amount of spaces versus the amount of tabs in your codebase. ### Setup ```bash -git clone https://github.com/romangraef/tabsvsspaces -python3.6 -m venv venv -source venv/bin/activate -chmod +x main.py +pip install tabsvsspaces ``` ### Usage @@ -15,3 +13,12 @@ chmod +x main.py tabsvsspaces folder tabsvsspaces -e folder ``` + + +### Links + + - [Github][github] + - [Issues][issues] + +[github]: https://github.com/romangraef/tabsvsspaces +[issues]: https://github.com/romangraef/tabsvsspaces/issues \ No newline at end of file diff --git a/lib/argparse/pathtype.py b/lib/argparse/pathtype.py deleted file mode 100644 index 39128cc..0000000 --- a/lib/argparse/pathtype.py +++ /dev/null @@ -1,63 +0,0 @@ -# As usual: copied form stack overflow -# https://stackoverflow.com/a/33181083 - -from argparse import ArgumentTypeError as err -import os - - -class PathType(object): - def __init__(self, exists=True, type='file', dash_ok=True): - """exists: - True: a path that does exist - False: a path that does not exist, in a valid parent directory - None: don't care - type: file, dir, symlink, None, or a function returning True for valid paths - None: don't care - dash_ok: whether to allow "-" as stdin/stdout""" - - assert exists in (True, False, None) - assert type in ('file', 'dir', 'symlink', None) or hasattr(type, '__call__') - - self._exists = exists - self._type = type - self._dash_ok = dash_ok - - def __call__(self, string): - if string == '-': - # the special argument "-" means sys.std{in,out} - if self._type == 'dir': - raise err('standard input/output (-) not allowed as directory path') - elif self._type == 'symlink': - raise err('standard input/output (-) not allowed as symlink path') - elif not self._dash_ok: - raise err('standard input/output (-) not allowed') - else: - e = os.path.exists(string) - if self._exists: - if not e: - raise err("path does not exist: '%s'" % string) - - if self._type is None: - pass - elif self._type == 'file': - if not os.path.isfile(string): - raise err("path is not a file: '%s'" % string) - elif self._type == 'symlink': - if not os.path.symlink(string): - raise err("path is not a symlink: '%s'" % string) - elif self._type == 'dir': - if not os.path.isdir(string): - raise err("path is not a directory: '%s'" % string) - elif not self._type(string): - raise err("path not valid: '%s'" % string) - else: - if not self._exists and e: - raise err("path exists: '%s'" % string) - - p = os.path.dirname(os.path.normpath(string)) or '.' - if not os.path.isdir(p): - raise err("parent path is not a directory: '%s'" % p) - elif not os.path.exists(p): - raise err("parent directory does not exist: '%s'" % p) - - return string diff --git a/lib/print_stats.py b/lib/print_stats.py deleted file mode 100644 index 9616caa..0000000 --- a/lib/print_stats.py +++ /dev/null @@ -1,17 +0,0 @@ -from lib.stats import Statistics - - -def print_stats(stats: Statistics, by_extension: bool): - print('spaces:', stats.all_spaces) - print('tabs:', stats.all_tabs) - print('mixed:', stats.all_mixed) - if by_extension: - for ext in set(stats.space_dict.keys()) | stats.tab_dict.keys() | stats.mixed_line_dict.keys(): - print(ext + ':') - print(' ', 'spaces:', stats.space_dict[ext]) - print(' ', 'tabs:', stats.tab_dict[ext]) - print(' ', 'mixed:', stats.mixed_line_dict[ext]) - if stats.all_mixed > 0: - print('files_with_mixed_lines:') - for file in stats.mixed_files: - print(' -', file) diff --git a/lib/stats.py b/lib/stats.py deleted file mode 100644 index 42e4461..0000000 --- a/lib/stats.py +++ /dev/null @@ -1,26 +0,0 @@ -from collections import defaultdict -from typing import Set - - -class Statistics: - def __init__(self): - self.space_dict = defaultdict(int) - self.tab_dict = defaultdict(int) - self.mixed_line_dict = defaultdict(int) - self.mixed_files: Set[str] = set() - self.all_tabs = 0 - self.all_spaces = 0 - self.all_mixed = 0 - - def add_spaces(self, extension='', count=1): - self.space_dict[extension] += count - self.all_spaces += count - - def add_tabs(self, extension='', count=1): - self.tab_dict[extension] += count - self.all_tabs += count - - def add_mixed_line(self, extension='', count=1, filename=''): - self.mixed_line_dict[extension] += count - self.mixed_files.add(filename) - self.all_mixed += count diff --git a/main.py b/main.py deleted file mode 100755 index ff8ad95..0000000 --- a/main.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3.6 - -import argparse -from pathlib import Path - -from lib.argparse.pathtype import PathType -from lib.find_stats import find_stats -from lib.print_stats import print_stats -from lib.stats import Statistics - - -def main(): - parser = argparse.ArgumentParser( - prog='tabsvsspaces', - description='Shows statistics about the usage of tabs and spaces in a given folder' - ) - parser.add_argument('folder', - type=PathType(type='dir', exists=True)) - parser.add_argument('--by-extension', '-e', - dest='extension', - action='store_true', - help='show distribution by file extension' - ) - parser.add_argument('--verbose', '-v', - dest='verbose', - action='store_true', - help='show debug information') - ns = parser.parse_args() - folder: str = ns.folder - extension: bool = ns.extension - verbose: bool = ns.verbose - stats: Statistics = find_stats(Path(folder), verbose=verbose) - print_stats(stats, extension) - - -if __name__ == '__main__': - main() diff --git a/pkg/ins.sc b/pkg/ins.sc deleted file mode 100644 index c580d88..0000000 --- a/pkg/ins.sc +++ /dev/null @@ -1,31 +0,0 @@ -name tabsvsspaces -user romangraef -ver 1.0.0 - -func install { - mkdir -p /usr/share/tabsvsspaces - cp -rf * /usr/share/tabsvsspaces - ln -fs /usr/share/tabsvsspaces/main.py /usr/bin/tabsvsspaces -} - -func update { - &install -} - -func install_win { - &werror -} - -func update_win -{ - &install_win -} - -func remove_win { - &werror -} - -func werror { - @echo "Leider gibt es keinen Windows-Support." -} - diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..f810b30 --- /dev/null +++ b/setup.py @@ -0,0 +1,34 @@ +import os + +from setuptools import setup + + +def read(fname): + return open(os.path.join(os.path.dirname(__file__), fname)).read() + + +setup( + name='tabsvsspaces', + version='1.1.1', + author="Roman Gräf", + author_email="romangraef@gmail.com", + description="A tool for counting spaces vs tabs in a codebase", + license="MIT", + keywords="tabs spaces linecount counting commandline", + packages=['tabsvsspaces'], + long_description=read('README.md'), + long_description_content_type="text/markdown", + url="https://github.com/romangraef/tabsvsspaces", + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Environment :: Console', + 'Intended Audience :: Developers', + 'Intended Audience :: Religion', + 'License :: OSI Approved :: MIT License', + 'Programming Language :: Python :: 3.5', + 'Typing :: Typed', + ], + entry_points=dict( + console_scripts='tabsvsspaces=tabsvsspaces:main' + ) +) diff --git a/tabsvsspaces/__init__.py b/tabsvsspaces/__init__.py new file mode 100644 index 0000000..483cd32 --- /dev/null +++ b/tabsvsspaces/__init__.py @@ -0,0 +1,4 @@ +from .find_stats import find_stats, find_all_files, find_stats_for_file, IGNORED_FOLDERS +from .print_stats import print_stats +from .stats import Statistics +from ._main import main \ No newline at end of file diff --git a/tabsvsspaces/__main__.py b/tabsvsspaces/__main__.py new file mode 100644 index 0000000..c7c70d0 --- /dev/null +++ b/tabsvsspaces/__main__.py @@ -0,0 +1,4 @@ +from . import main + +if __name__ == '__main__': + main() diff --git a/tabsvsspaces/_main.py b/tabsvsspaces/_main.py new file mode 100644 index 0000000..b9f6389 --- /dev/null +++ b/tabsvsspaces/_main.py @@ -0,0 +1,31 @@ +import argparse +from pathlib import Path + +from tabsvsspaces.pathtype import PathType +from tabsvsspaces.find_stats import find_stats +from tabsvsspaces.print_stats import print_stats +from tabsvsspaces.stats import Statistics + + +def main(args=None): + parser = argparse.ArgumentParser( + prog='tabsvsspaces', + description='Shows statistics about the usage of tabs and spaces in a given folder' + ) + parser.add_argument('folder', + type=PathType(type='dir', exists=True)) + parser.add_argument('--by-extension', '-e', + dest='extension', + action='store_true', + help='show distribution by file extension' + ) + parser.add_argument('--verbose', '-v', + dest='verbose', + action='store_true', + help='show debug information') + ns = parser.parse_args(args) + folder: str = ns.folder + extension: bool = ns.extension + verbose: bool = ns.verbose + stats: Statistics = find_stats(Path(folder), verbose=verbose) + print_stats(stats, extension) diff --git a/tabsvsspaces/pathtype.py b/tabsvsspaces/pathtype.py new file mode 100644 index 0000000..39128cc --- /dev/null +++ b/tabsvsspaces/pathtype.py @@ -0,0 +1,63 @@ +# As usual: copied form stack overflow +# https://stackoverflow.com/a/33181083 + +from argparse import ArgumentTypeError as err +import os + + +class PathType(object): + def __init__(self, exists=True, type='file', dash_ok=True): + """exists: + True: a path that does exist + False: a path that does not exist, in a valid parent directory + None: don't care + type: file, dir, symlink, None, or a function returning True for valid paths + None: don't care + dash_ok: whether to allow "-" as stdin/stdout""" + + assert exists in (True, False, None) + assert type in ('file', 'dir', 'symlink', None) or hasattr(type, '__call__') + + self._exists = exists + self._type = type + self._dash_ok = dash_ok + + def __call__(self, string): + if string == '-': + # the special argument "-" means sys.std{in,out} + if self._type == 'dir': + raise err('standard input/output (-) not allowed as directory path') + elif self._type == 'symlink': + raise err('standard input/output (-) not allowed as symlink path') + elif not self._dash_ok: + raise err('standard input/output (-) not allowed') + else: + e = os.path.exists(string) + if self._exists: + if not e: + raise err("path does not exist: '%s'" % string) + + if self._type is None: + pass + elif self._type == 'file': + if not os.path.isfile(string): + raise err("path is not a file: '%s'" % string) + elif self._type == 'symlink': + if not os.path.symlink(string): + raise err("path is not a symlink: '%s'" % string) + elif self._type == 'dir': + if not os.path.isdir(string): + raise err("path is not a directory: '%s'" % string) + elif not self._type(string): + raise err("path not valid: '%s'" % string) + else: + if not self._exists and e: + raise err("path exists: '%s'" % string) + + p = os.path.dirname(os.path.normpath(string)) or '.' + if not os.path.isdir(p): + raise err("parent path is not a directory: '%s'" % p) + elif not os.path.exists(p): + raise err("parent directory does not exist: '%s'" % p) + + return string diff --git a/tabsvsspaces/print_stats.py b/tabsvsspaces/print_stats.py new file mode 100644 index 0000000..1963744 --- /dev/null +++ b/tabsvsspaces/print_stats.py @@ -0,0 +1,17 @@ +from tabsvsspaces.stats import Statistics + + +def print_stats(stats: Statistics, by_extension: bool): + print('spaces:', stats.all_spaces) + print('tabs:', stats.all_tabs) + print('mixed:', stats.all_mixed) + if by_extension: + for ext in set(stats.space_dict.keys()) | stats.tab_dict.keys() | stats.mixed_line_dict.keys(): + print(ext + ':') + print(' ', 'spaces:', stats.space_dict[ext]) + print(' ', 'tabs:', stats.tab_dict[ext]) + print(' ', 'mixed:', stats.mixed_line_dict[ext]) + if stats.all_mixed > 0: + print('files_with_mixed_lines:') + for file in stats.mixed_files: + print(' -', file) diff --git a/tabsvsspaces/stats.py b/tabsvsspaces/stats.py new file mode 100644 index 0000000..42e4461 --- /dev/null +++ b/tabsvsspaces/stats.py @@ -0,0 +1,26 @@ +from collections import defaultdict +from typing import Set + + +class Statistics: + def __init__(self): + self.space_dict = defaultdict(int) + self.tab_dict = defaultdict(int) + self.mixed_line_dict = defaultdict(int) + self.mixed_files: Set[str] = set() + self.all_tabs = 0 + self.all_spaces = 0 + self.all_mixed = 0 + + def add_spaces(self, extension='', count=1): + self.space_dict[extension] += count + self.all_spaces += count + + def add_tabs(self, extension='', count=1): + self.tab_dict[extension] += count + self.all_tabs += count + + def add_mixed_line(self, extension='', count=1, filename=''): + self.mixed_line_dict[extension] += count + self.mixed_files.add(filename) + self.all_mixed += count -- cgit