From 3165389d52c4d357a6e6eca56d061f3751592948 Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Thu, 1 Jun 2017 14:40:33 +0300 Subject: [PATCH] Fix lint check + update lint script (#224) * Fix PEP8 E301 on nyaa/models.py * Add utils/ to lint checker * Run of lint.sh + manual fixes As suggested https://github.com/nyaadevs/nyaa/pull/157#issuecomment-305051428 * Fix backwards tick in README * Updated script * Update Travis config --- .travis.yml | 12 +++---- README.md | 2 +- lint.sh | 57 +++++++++++++++++++++++++---- nyaa/models.py | 18 +++++----- nyaa/search.py | 1 + utils/api_uploader_v2.py | 77 ++++++++++++++++++++++++---------------- 6 files changed, 115 insertions(+), 52 deletions(-) diff --git a/.travis.yml b/.travis.yml index 495b891..f5fcb2b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,18 @@ language: python -python: - - 3.6 +python: "3.6" dist: xenial sudo: false +matrix: + fast_finish: true + cache: pip -install: - - pip install --upgrade pycodestyle +install: pip install --upgrade pycodestyle -script: - - pycodestyle nyaa/ --show-source --max-line-length=100 +script: ./lint.sh --check notifications: email: false diff --git a/README.md b/README.md index 9a8dc39..7ef3410 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ It's not impossible to run Nyaa on Windows, but this guide doesn't focus on that ### Code Quality: - Before we get any deeper, remember to follow PEP8 style guidelines and run `./lint.sh` before committing. - - You may also use `pycodestyle nyaa/ --show-source --max-line-length=100ยด to see a list of warnings/problems instead of having `lint.sh` making modifications for you + - You may also use `./lint.sh -c` to see a list of warnings/problems instead of having `lint.sh` making modifications for you - Other than PEP8, try to keep your code clean and easy to understand, as well. It's only polite! ### Setting up Pyenv diff --git a/lint.sh b/lint.sh index f8c79c5..2152765 100755 --- a/lint.sh +++ b/lint.sh @@ -1,6 +1,51 @@ -autopep8 nyaa/ \ - --recursive \ - --in-place \ - --pep8-passes 2000 \ - --max-line-length 100 \ - --verbose +# Lint checker/fixer + +check_paths="nyaa/ utils/" +max_line_length=100 + +function auto_pep8() { + autopep8 ${check_paths} \ + --recursive \ + --in-place \ + --pep8-passes 2000 \ + --max-line-length ${max_line_length} \ + --verbose +} + +function check_lint() { + pycodestyle ${check_paths} \ + --show-source \ + --max-line-length=${max_line_length} \ + --format '%(path)s [%(row)s:%(col)s] %(code)s: %(text)s' +} + +# MAIN +action=auto_pep8 # default action +for arg in "$@" +do + case "$arg" in + "-h" | "--help") + echo "Lint checker/fixer" + echo "" + echo "Usage: $0 [-c|--check] [-h|--help]" + echo " No arguments : Check and auto-fix some warnings/errors" + echo " -c | --check : only check lint (don't auto-fix)" + echo " -h | --help : show this help and exit" + exit 0; + ;; + "-c" | "--check") + action=check_lint + ;; + esac +done + +${action} # run selected action +result=$? + +if [[ ${action} == check_lint ]]; then + if [[ ${result} == 0 ]]; then + echo "Looks good!" + else + echo "The code requires some changes." + fi +fi diff --git a/nyaa/models.py b/nyaa/models.py index 5e2af90..4480e72 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -67,12 +67,14 @@ class DeclarativeHelperBase(object): class FlagProperty(object): ''' This class will act as a wrapper between the given flag and the class's flag collection. ''' + def __init__(self, flag, flags_attr='flags'): self._flag = flag self._flags_attr_name = flags_attr def _get_flags(self, instance): return getattr(instance, self._flags_attr_name) + def _set_flags(self, instance, value): return setattr(instance, self._flags_attr_name, value) @@ -140,13 +142,13 @@ class TorrentBase(DeclarativeHelperBase): @declarative.declared_attr def __table_args__(cls): return ( - Index(cls._table_prefix('uploader_flag_idx'), 'uploader_id', 'flags'), - ForeignKeyConstraint( - ['main_category_id', 'sub_category_id'], - [cls._table_prefix('sub_categories.main_category_id'), - cls._table_prefix('sub_categories.id')] - ), {} - ) + Index(cls._table_prefix('uploader_flag_idx'), 'uploader_id', 'flags'), + ForeignKeyConstraint( + ['main_category_id', 'sub_category_id'], + [cls._table_prefix('sub_categories.main_category_id'), + cls._table_prefix('sub_categories.id')] + ), {} + ) @declarative.declared_attr def user(cls): @@ -419,7 +421,7 @@ class CommentBase(DeclarativeHelperBase): @declarative.declared_attr def torrent_id(cls): return db.Column(db.Integer, db.ForeignKey( - cls._table_prefix('torrents.id'), ondelete='CASCADE'), nullable=False) + cls._table_prefix('torrents.id'), ondelete='CASCADE'), nullable=False) @declarative.declared_attr def user_id(cls): diff --git a/nyaa/search.py b/nyaa/search.py index 4eac57a..83b9ebd 100644 --- a/nyaa/search.py +++ b/nyaa/search.py @@ -186,6 +186,7 @@ def search_elastic(term='', user=None, sort='id', order='desc', class QueryPairCaller(object): ''' Simple stupid class to filter one or more queries with the same args ''' + def __init__(self, *items): self.items = list(items) diff --git a/utils/api_uploader_v2.py b/utils/api_uploader_v2.py index 6956d81..9c1d8cf 100755 --- a/utils/api_uploader_v2.py +++ b/utils/api_uploader_v2.py @@ -38,6 +38,7 @@ SUKEBEI_CATS = '''1_1 - Art - Anime 2_1 - Real Life - Photobooks / Pictures 2_2 - Real Life - Videos''' + class CategoryPrintAction(argparse.Action): def __init__(self, option_strings, nargs='?', help=None, **kwargs): super().__init__(option_strings=option_strings, @@ -55,15 +56,20 @@ class CategoryPrintAction(argparse.Action): print(NYAA_CATS) parser.exit() -environment_epillog = '''You may also provide environment variables NYAA_API_HOST, NYAA_API_USERNAME and NYAA_API_PASSWORD for connection info.''' -parser = argparse.ArgumentParser(description='Upload torrents to Nyaa.si', epilog=environment_epillog) +environment_epillog = ('You may also provide environment variables NYAA_API_HOST, NYAA_API_USERNAME' + ' and NYAA_API_PASSWORD for connection info.') -parser.add_argument('--list-categories', default=False, action=CategoryPrintAction, nargs='?', help='List torrent categories. Include "sukebei" to show Sukebei categories') +parser = argparse.ArgumentParser( + description='Upload torrents to Nyaa.si', epilog=environment_epillog) + +parser.add_argument('--list-categories', default=False, action=CategoryPrintAction, nargs='?', + help='List torrent categories. Include "sukebei" to show Sukebei categories') conn_group = parser.add_argument_group('Connection options') -conn_group.add_argument('-s', '--sukebei', default=False, action='store_true', help='Upload to sukebei.nyaa.si') +conn_group.add_argument('-s', '--sukebei', default=False, + action='store_true', help='Upload to sukebei.nyaa.si') conn_group.add_argument('-u', '--user', help='Username or email') conn_group.add_argument('-p', '--password', help='Password') @@ -71,8 +77,10 @@ conn_group.add_argument('--host', help='Select another api host (for debugging p resp_group = parser.add_argument_group('Response options') -resp_group.add_argument('--raw', default=False, action='store_true', help='Print only raw response (JSON)') -resp_group.add_argument('-m', '--magnet', default=False, action='store_true', help='Print magnet uri') +resp_group.add_argument('--raw', default=False, action='store_true', + help='Print only raw response (JSON)') +resp_group.add_argument('-m', '--magnet', default=False, + action='store_true', help='Print magnet uri') tor_group = parser.add_argument_group('Torrent options') @@ -80,16 +88,23 @@ tor_group.add_argument('-c', '--category', required=True, help='Torrent category tor_group.add_argument('-n', '--name', help='Display name for the torrent (optional)') tor_group.add_argument('-i', '--information', help='Information field (optional)') tor_group.add_argument('-d', '--description', help='Description for the torrent (optional)') -tor_group.add_argument('-D', '--description-file', metavar='FILE', help='Read description from a file (optional)') +tor_group.add_argument('-D', '--description-file', metavar='FILE', + help='Read description from a file (optional)') -tor_group.add_argument('-A', '--anonymous', default=False, action='store_true', help='Upload torrent anonymously') -tor_group.add_argument('-H', '--hidden', default=False, action='store_true', help='Hide torrent from results') -tor_group.add_argument('-C', '--complete', default=False, action='store_true', help='Mark torrent as complete (eg. season batch)') -tor_group.add_argument('-R', '--remake', default=False, action='store_true', help='Mark torrent as remake (derivative work from another release)') +tor_group.add_argument('-A', '--anonymous', default=False, + action='store_true', help='Upload torrent anonymously') +tor_group.add_argument('-H', '--hidden', default=False, action='store_true', + help='Hide torrent from results') +tor_group.add_argument('-C', '--complete', default=False, action='store_true', + help='Mark torrent as complete (eg. season batch)') +tor_group.add_argument('-R', '--remake', default=False, action='store_true', + help='Mark torrent as remake (derivative work from another release)') trusted_group = tor_group.add_mutually_exclusive_group(required=False) -trusted_group.add_argument('-T', '--trusted', dest='trusted', action='store_true', help='Mark torrent as trusted, if possible. Defaults to true') -trusted_group.add_argument('--no-trusted', dest='trusted', action='store_false', help='Do not mark torrent as trusted') +trusted_group.add_argument('-T', '--trusted', dest='trusted', action='store_true', + help='Mark torrent as trusted, if possible. Defaults to true') +trusted_group.add_argument('--no-trusted', dest='trusted', + action='store_false', help='Do not mark torrent as trusted') parser.set_defaults(trusted=True) tor_group.add_argument('torrent', metavar='TORRENT_FILE', help='The .torrent file to upload') @@ -105,7 +120,7 @@ def crude_torrent_check(file_object): file_object.seek(-1, os.SEEK_END) if file_object.read(1) != b'e': return False - + # Seek back to beginning file_object.seek(0) @@ -118,7 +133,7 @@ if __name__ == "__main__": # Use debug host from args or environment, if set debug_host = args.host or os.getenv('NYAA_API_HOST') api_host = (debug_host or (args.sukebei and SUKEBEI_HOST or NYAA_HOST)).rstrip('/') - + api_upload_url = api_host + API_UPLOAD if args.description_file: @@ -131,33 +146,33 @@ if __name__ == "__main__": if not crude_torrent_check(torrent_file): raise Exception("File '{}' doesn't seem to be a torrent file".format(args.torrent)) - api_username = args.user or os.getenv('NYAA_API_USERNAME') + api_username = args.user or os.getenv('NYAA_API_USERNAME') api_password = args.password or os.getenv('NYAA_API_PASSWORD') - + if not (api_username and api_password): raise Exception('No authorization found from arguments or environment variables.') auth = (api_username, api_password) data = { - 'name' : args.name, - 'category' : args.category, + 'name': args.name, + 'category': args.category, - 'information' : args.information, - 'description' : args.description, + 'information': args.information, + 'description': args.description, - 'anonymous' : args.anonymous, - 'hidden' : args.hidden, - 'complete' : args.complete, - 'remake' : args.remake, - 'trusted' : args.trusted, + 'anonymous': args.anonymous, + 'hidden': args.hidden, + 'complete': args.complete, + 'remake': args.remake, + 'trusted': args.trusted, } - encoded_data = { - 'torrent_data' : json.dumps(data) + encoded_data = { + 'torrent_data': json.dumps(data) } - + files = { - 'torrent' : torrent_file + 'torrent': torrent_file } # Go! @@ -166,7 +181,7 @@ if __name__ == "__main__": if args.raw: print(r.text) - + else: try: response = r.json()