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
This commit is contained in:
Kfir Hadas 2017-06-01 14:40:33 +03:00 committed by Alex Ingram
parent 3e2437bba1
commit 3165389d52
6 changed files with 115 additions and 52 deletions

View File

@ -1,18 +1,18 @@
language: python language: python
python: python: "3.6"
- 3.6
dist: xenial dist: xenial
sudo: false sudo: false
matrix:
fast_finish: true
cache: pip cache: pip
install: install: pip install --upgrade pycodestyle
- pip install --upgrade pycodestyle
script: script: ./lint.sh --check
- pycodestyle nyaa/ --show-source --max-line-length=100
notifications: notifications:
email: false email: false

View File

@ -7,7 +7,7 @@ It's not impossible to run Nyaa on Windows, but this guide doesn't focus on that
### Code Quality: ### Code Quality:
- Before we get any deeper, remember to follow PEP8 style guidelines and run `./lint.sh` before committing. - 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! - Other than PEP8, try to keep your code clean and easy to understand, as well. It's only polite!
### Setting up Pyenv ### Setting up Pyenv

57
lint.sh
View File

@ -1,6 +1,51 @@
autopep8 nyaa/ \ # Lint checker/fixer
--recursive \
--in-place \ check_paths="nyaa/ utils/"
--pep8-passes 2000 \ max_line_length=100
--max-line-length 100 \
--verbose 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

View File

@ -67,12 +67,14 @@ class DeclarativeHelperBase(object):
class FlagProperty(object): class FlagProperty(object):
''' This class will act as a wrapper between the given flag and the class's ''' This class will act as a wrapper between the given flag and the class's
flag collection. ''' flag collection. '''
def __init__(self, flag, flags_attr='flags'): def __init__(self, flag, flags_attr='flags'):
self._flag = flag self._flag = flag
self._flags_attr_name = flags_attr self._flags_attr_name = flags_attr
def _get_flags(self, instance): def _get_flags(self, instance):
return getattr(instance, self._flags_attr_name) return getattr(instance, self._flags_attr_name)
def _set_flags(self, instance, value): def _set_flags(self, instance, value):
return setattr(instance, self._flags_attr_name, value) return setattr(instance, self._flags_attr_name, value)
@ -140,13 +142,13 @@ class TorrentBase(DeclarativeHelperBase):
@declarative.declared_attr @declarative.declared_attr
def __table_args__(cls): def __table_args__(cls):
return ( return (
Index(cls._table_prefix('uploader_flag_idx'), 'uploader_id', 'flags'), Index(cls._table_prefix('uploader_flag_idx'), 'uploader_id', 'flags'),
ForeignKeyConstraint( ForeignKeyConstraint(
['main_category_id', 'sub_category_id'], ['main_category_id', 'sub_category_id'],
[cls._table_prefix('sub_categories.main_category_id'), [cls._table_prefix('sub_categories.main_category_id'),
cls._table_prefix('sub_categories.id')] cls._table_prefix('sub_categories.id')]
), {} ), {}
) )
@declarative.declared_attr @declarative.declared_attr
def user(cls): def user(cls):
@ -419,7 +421,7 @@ class CommentBase(DeclarativeHelperBase):
@declarative.declared_attr @declarative.declared_attr
def torrent_id(cls): def torrent_id(cls):
return db.Column(db.Integer, db.ForeignKey( 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 @declarative.declared_attr
def user_id(cls): def user_id(cls):

View File

@ -186,6 +186,7 @@ def search_elastic(term='', user=None, sort='id', order='desc',
class QueryPairCaller(object): class QueryPairCaller(object):
''' Simple stupid class to filter one or more queries with the same args ''' ''' Simple stupid class to filter one or more queries with the same args '''
def __init__(self, *items): def __init__(self, *items):
self.items = list(items) self.items = list(items)

View File

@ -38,6 +38,7 @@ SUKEBEI_CATS = '''1_1 - Art - Anime
2_1 - Real Life - Photobooks / Pictures 2_1 - Real Life - Photobooks / Pictures
2_2 - Real Life - Videos''' 2_2 - Real Life - Videos'''
class CategoryPrintAction(argparse.Action): class CategoryPrintAction(argparse.Action):
def __init__(self, option_strings, nargs='?', help=None, **kwargs): def __init__(self, option_strings, nargs='?', help=None, **kwargs):
super().__init__(option_strings=option_strings, super().__init__(option_strings=option_strings,
@ -55,15 +56,20 @@ class CategoryPrintAction(argparse.Action):
print(NYAA_CATS) print(NYAA_CATS)
parser.exit() 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 = 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('-u', '--user', help='Username or email')
conn_group.add_argument('-p', '--password', help='Password') 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 = 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('--raw', default=False, action='store_true',
resp_group.add_argument('-m', '--magnet', default=False, action='store_true', help='Print magnet uri') 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') 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('-n', '--name', help='Display name for the torrent (optional)')
tor_group.add_argument('-i', '--information', help='Information field (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', 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('-A', '--anonymous', default=False,
tor_group.add_argument('-H', '--hidden', default=False, action='store_true', help='Hide torrent from results') action='store_true', help='Upload torrent anonymously')
tor_group.add_argument('-C', '--complete', default=False, action='store_true', help='Mark torrent as complete (eg. season batch)') tor_group.add_argument('-H', '--hidden', default=False, action='store_true',
tor_group.add_argument('-R', '--remake', default=False, action='store_true', help='Mark torrent as remake (derivative work from another release)') 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 = 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('-T', '--trusted', dest='trusted', action='store_true',
trusted_group.add_argument('--no-trusted', dest='trusted', action='store_false', help='Do not mark torrent as trusted') 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) parser.set_defaults(trusted=True)
tor_group.add_argument('torrent', metavar='TORRENT_FILE', help='The .torrent file to upload') 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) file_object.seek(-1, os.SEEK_END)
if file_object.read(1) != b'e': if file_object.read(1) != b'e':
return False return False
# Seek back to beginning # Seek back to beginning
file_object.seek(0) file_object.seek(0)
@ -118,7 +133,7 @@ if __name__ == "__main__":
# Use debug host from args or environment, if set # Use debug host from args or environment, if set
debug_host = args.host or os.getenv('NYAA_API_HOST') 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_host = (debug_host or (args.sukebei and SUKEBEI_HOST or NYAA_HOST)).rstrip('/')
api_upload_url = api_host + API_UPLOAD api_upload_url = api_host + API_UPLOAD
if args.description_file: if args.description_file:
@ -131,33 +146,33 @@ if __name__ == "__main__":
if not crude_torrent_check(torrent_file): if not crude_torrent_check(torrent_file):
raise Exception("File '{}' doesn't seem to be a torrent file".format(args.torrent)) 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') api_password = args.password or os.getenv('NYAA_API_PASSWORD')
if not (api_username and api_password): if not (api_username and api_password):
raise Exception('No authorization found from arguments or environment variables.') raise Exception('No authorization found from arguments or environment variables.')
auth = (api_username, api_password) auth = (api_username, api_password)
data = { data = {
'name' : args.name, 'name': args.name,
'category' : args.category, 'category': args.category,
'information' : args.information, 'information': args.information,
'description' : args.description, 'description': args.description,
'anonymous' : args.anonymous, 'anonymous': args.anonymous,
'hidden' : args.hidden, 'hidden': args.hidden,
'complete' : args.complete, 'complete': args.complete,
'remake' : args.remake, 'remake': args.remake,
'trusted' : args.trusted, 'trusted': args.trusted,
} }
encoded_data = { encoded_data = {
'torrent_data' : json.dumps(data) 'torrent_data': json.dumps(data)
} }
files = { files = {
'torrent' : torrent_file 'torrent': torrent_file
} }
# Go! # Go!
@ -166,7 +181,7 @@ if __name__ == "__main__":
if args.raw: if args.raw:
print(r.text) print(r.text)
else: else:
try: try:
response = r.json() response = r.json()