From 064fc58919dccb28c7a84a44eed3dd87a9106800 Mon Sep 17 00:00:00 2001 From: Gaurav Mishra Date: Wed, 29 Jul 2020 16:41:24 +0530 Subject: [PATCH] chore(setup): Added additional metainfo 1. Added metainfo like license and platform to setup.py. 2. Added MANIFEST.in to remove test data from package. 3. New exception NotSupportedExtension to handle files with unknown extensions. 4. Remove `cwd` infront of filename. Signed-off-by: Gaurav Mishra --- MANIFEST.in | 14 ++++++++ extractor/__init__.py | 2 +- extractor/main.py | 74 +++++++++++++++++++++---------------------- setup.py | 34 +++++++++++--------- 4 files changed, 71 insertions(+), 53 deletions(-) create mode 100644 MANIFEST.in diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..4a72334 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,14 @@ +# Manifest syntax https://docs.python.org/3/distutils/sourcedist.html +recursive-exclude __pycache__ *.pyc *.pyo *.orig + +exclude *.git* +exclude *.sh +exclude extractor/languages/tests* + +include LICENSE + +prune .git +prune venv +prune .venv +prune extractor/languages/tests + diff --git a/extractor/__init__.py b/extractor/__init__.py index 81fa6cd..b82606d 100644 --- a/extractor/__init__.py +++ b/extractor/__init__.py @@ -3,4 +3,4 @@ def extract(file): return file_runner(file) -__all__ = ['file_runner','extract', 'langIdentifier'] \ No newline at end of file +__all__ = ['file_runner', 'extract', 'langIdentifier'] diff --git a/extractor/main.py b/extractor/main.py index 42ea83f..21b6536 100644 --- a/extractor/main.py +++ b/extractor/main.py @@ -28,14 +28,20 @@ from extractor.languages import * +class NotSupportedExtension(Exception): + ''' + Exception if file extension is not recognized + ''' + def __str__(self): + return "extension '" + self.args[0] + "' not supported" + + class CommentExtractor: - def __init__(self): - pass + @staticmethod def langIdentifier(file): extension = os.path.splitext(file)[1] - - + langMap = { '.py': 'python', '.m4': 'python', @@ -75,63 +81,57 @@ def langIdentifier(file): '.gl': 'text' } + if extension not in langMap: + raise NotSupportedExtension(extension) return langMap[extension] + def main(): parser = argparse.ArgumentParser() - parser.add_argument("-p","--path", help="Specify the input file/directory path to scan") - parser.add_argument("-i","--inputFile", help="Specify the input file with the source code") - parser.add_argument("-s","--string",help= "The name of file you want the code in",default="source.txt") + parser.add_argument("-p", "--path", help="Specify the input file/directory path to scan") + parser.add_argument("-i", "--inputFile", help="Specify the input file with the source code") + parser.add_argument("-s", "--string", help="The name of file you want the code in", default="source.txt") args = parser.parse_args() file = args.path inputfile = args.inputFile string_name = args.string - if file: - file_runner(file) - else: - inputfile_runner(inputfile,string_name) + try: + if file: + return file_runner(file) + else: + return inputfile_runner(inputfile, string_name) + except NotSupportedExtension as e: + print(e, file=os.sys.stderr) + - - def file_runner(file): result = [] - if os.path.basename(file): - file_name = os.path.basename(file) - current_path = os.getcwd()+'/'+file - langname = CommentExtractor.langIdentifier(file_name) - func = langname+'.'+langname+'Extractor' - output = eval(func)(current_path) + if os.path.isfile(file): + langname = CommentExtractor.langIdentifier(file) + func = langname + '.' + langname + 'Extractor' + output = eval(func)(file) result.append(output) - elif os.path.dirname(file): - for root,dirs,files in os.walk(file,topdown=True): + elif os.path.isdir(file): + for root, dirs, files in os.walk(file, topdown=True): for file in files: - current_path = os.path.join(os.path.join(os.getcwd(),root),file) try: - - if os.path.isfile(current_path): + if os.path.isfile(file): langname = CommentExtractor.langIdentifier(file) - func = langname+'.'+langname+'Extractor' - output = eval(func)(current_path) + func = langname + '.' + langname + 'Extractor' + output = eval(func)(file) result.append(output) except Exception: continue result = json.dumps(result, sort_keys=False, indent=4) - print(result) return result - -def inputfile_runner(inputfile,string_name): +def inputfile_runner(inputfile, string_name): langname = CommentExtractor.langIdentifier(inputfile) - func = langname+'.'+langname+'Source' - eval(func)(inputfile,string_name) - # python.pythonSource(inputfile,string_name) - - # result = json.dumps(result, sort_keys=False, indent=4) - # print(result) - + func = langname + '.' + langname + 'Source' + return eval(func)(inputfile, string_name) if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/setup.py b/setup.py index 58fec9f..19d3fa2 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + ''' Copyright (C) 2020 Ayush Bhardwaj (classicayush@gmail.com), Kaushlendra Pratap (kaushlendrapratap.9837@gmail.com) SPDX-License-Identifier: LGPL-2.1 @@ -37,22 +39,24 @@ setup( - name='Nirjas', - version='0.0.2', - description='A Python library to extract comments and source code out of your file(s)', - long_description=long_description, - long_description_content_type='text/markdown', - url='https://github.com/fossology/nirjas', - author='Ayush Bhardwaj, Kaushlendra Pratap', - author_email='classicayush@gmail.com, kaushlendrapratap.9837@gmail.com', + name='Nirjas', + version='0.0.4', + description='A Python library to extract comments and source code out of your file(s)', + long_description=long_description, + long_description_content_type='text/markdown', + url='https://github.com/fossology/nirjas', + author='Ayush Bhardwaj, Kaushlendra Pratap', + author_email='classicayush@gmail.com, kaushlendrapratap.9837@gmail.com', classifiers=[_f for _f in CLASSIFIERS.split('\n') if _f], - keywords='Comment Extractor, Code Comment Extractor, Source Code Extractor, Source Extractor', - packages=find_packages(), + keywords='Comment Extractor, Code Comment Extractor, Source Code Extractor, Source Extractor', + packages=find_packages(), python_requires = ">=3", entry_points = { - 'console_scripts': [ - 'nirjas = extractor.main:main' - ] - }, -) \ No newline at end of file + 'console_scripts': [ + 'nirjas = extractor.main:main' + ] + }, + license = "LGPL-2.1-or-later", + platforms = ['any'] +)