autopep8-0.9.1/ 0000775 0001750 0001750 00000000000 12153410305 014430 5 ustar hattori hattori 0000000 0000000 autopep8-0.9.1/setup.py 0000775 0001750 0001750 00000003430 12153410031 016141 0 ustar hattori hattori 0000000 0000000 #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Setup for autopep8."""
import ast
from setuptools import setup
def version():
"""Return version string."""
with open('autopep8.py') as input_file:
for line in input_file:
if line.startswith('__version__'):
return ast.parse(line).body[0].value.s
with open('README.rst') as readme:
setup(
name='autopep8',
version=version(),
description='A tool that automatically formats Python code to conform '
'to the PEP 8 style guide',
long_description=readme.read(),
license='Expat License',
author='Hideo Hattori',
author_email='hhatto.jp@gmail.com',
url='https://github.com/hhatto/autopep8',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Software Development :: Quality Assurance',
],
keywords='automation, pep8, format',
install_requires=['pep8 >= 1.4.5'],
test_suite='test.test_autopep8',
py_modules=['autopep8'],
zip_safe=False,
entry_points={'console_scripts': ['autopep8 = autopep8:main']},
)
autopep8-0.9.1/MANIFEST.in 0000664 0001750 0001750 00000000267 12153410014 016170 0 ustar hattori hattori 0000000 0000000 include README.rst
include test/__init__.py
include test/bad_encoding.py
include test/bad_encoding2.py
include test/e101_example.py
include test/example.py
include test/iso_8859_1.py
autopep8-0.9.1/test/ 0000775 0001750 0001750 00000000000 12153410305 015407 5 ustar hattori hattori 0000000 0000000 autopep8-0.9.1/test/bad_encoding.py 0000664 0001750 0001750 00000000033 12153410014 020346 0 ustar hattori hattori 0000000 0000000 # -*- coding: zlatin-1 -*-
autopep8-0.9.1/test/example.py 0000664 0001750 0001750 00000004236 12153410014 017416 0 ustar hattori hattori 0000000 0000000 import sys, os
def foo():
import subprocess, argparse
import copy; import math, email
print(1)
print(2) # e261
d = {1: 2,# e261
3: 4}
print(2) ## e262
print(2) #### e262
print(2) #e262
print(2) # e262
1 /1
1 *2
1 +1
1 -1
1 **2
def dummy1 ( a ):
print(a)
print(a)
def dummy2(a) :
if 1 in a:
print("a")
print(1+1) # e225
print(1 +1) # e225
print(1+ 1) # e225
print(1 +1) # e221+e225
print(1 + 1) # e221
print(1 * 1) # e221
print(1 + 1) # e222
print(1 * 1) # e222
print(a)
def func1():
print("A")
return 0
def func11():
a = (1,2, 3,"a")
b = [1, 2, 3,"b"]
c = 0,11/2
return 1
# comment after too empty lines
def func2():
pass
def func22():
pass;
def func_oneline(): print(1)
def func_last():
if True: print(1)
pass
def func_e251(a, b=1, c = 3):
pass
def func_e251_t(a, b=1, c = 3, d = 4):
pass
# e201
( 1)
[ 1]
{ 1: 2}
# e202
(1 )
[1 ]
{1: 2 }
# e203
{4 : 2}
[4 , 2]
# e211
d = [1]
d [0]
dummy1 (0)
def func_e702():
4; 1;
4; 1;
4; 1;
4; 1;
print(2); print(4); 6;8
if True:
1; 2; 3
0; 1
2;3
4; 5;
def func_w602():
raise ValueError, "w602 test"
raise ValueError, "w602 test" # my comment
raise ValueError
raise ValueError # comment
raise ValueError, 'arg' ; print(1)
raise ValueError, 'arg' ; print(2) # my comment
raise ValueError, \
'arg no comment'
raise ValueError, \
'arg' # my comment
raise ValueError, \
"""arg""" # my comment
raise ValueError, \
"""arg
""" # my comment
raise ValueError, \
'''multiline
''' # my comment
a = 'a'
raise ValueError, "%s" % (a,)
raise 'string'
def func_w603():
if 1 <> 2:
if 2 <> 2:
print(True)
else:
print(False)
def func_w604():
a = 1.1
b = ```a```
def func_e101():
print('abc')
if True:
print('hello')
if __name__ == '__main__': func_last()
autopep8-0.9.1/test/iso_8859_1.py 0000664 0001750 0001750 00000000035 12153410014 017463 0 ustar hattori hattori 0000000 0000000 # -*- coding: iso-8859-1 -*-
autopep8-0.9.1/test/__init__.py 0000664 0001750 0001750 00000000000 12153410014 017503 0 ustar hattori hattori 0000000 0000000 autopep8-0.9.1/test/e101_example.py 0000664 0001750 0001750 00000127321 12153410014 020145 0 ustar hattori hattori 0000000 0000000 # -*- coding: utf-8 -*-
# From https://github.com/coto/gae-boilerplate/blob/233a88c59e46bb10de7a901ef4e6a5b60d0006a5/web/handlers.py
"""
This example will take a long time if we don't filter innocuous E101
errors from pep8.
"""
import models.models as models
from webapp2_extras.auth import InvalidAuthIdError
from webapp2_extras.auth import InvalidPasswordError
from webapp2_extras import security
from lib import utils
from lib import captcha
from lib.basehandler import BaseHandler
from lib.basehandler import user_required
from google.appengine.api import taskqueue
import logging
import config
import webapp2
import web.forms as forms
from webapp2_extras.i18n import gettext as _
from webapp2_extras.appengine.auth.models import Unique
from lib import twitter
class LoginBaseHandler(BaseHandler):
"""
Base class for handlers with login form.
"""
@webapp2.cached_property
def form(self):
return forms.LoginForm(self)
class RegisterBaseHandler(BaseHandler):
"""
Base class for handlers with registration and login forms.
"""
@webapp2.cached_property
def form(self):
if self.is_mobile:
return forms.RegisterMobileForm(self)
else:
return forms.RegisterForm(self)
@webapp2.cached_property
def form_login(self):
return forms.LoginForm(self)
@webapp2.cached_property
def forms(self):
return {'form_login' : self.form_login,
'form' : self.form}
class SendEmailHandler(BaseHandler):
"""
Handler for sending Emails
Use with TaskQueue
"""
def post(self):
to = self.request.get("to")
subject = self.request.get("subject")
body = self.request.get("body")
sender = self.request.get("sender")
utils.send_email(to, subject, body, sender)
class LoginHandler(LoginBaseHandler):
"""
Handler for authentication
"""
def get(self):
""" Returns a simple HTML form for login """
if self.user:
self.redirect_to('home', id=self.user_id)
params = {}
return self.render_template('boilerplate_login.html', **params)
def post(self):
"""
username: Get the username from POST dict
password: Get the password from POST dict
"""
if not self.form.validate():
return self.get()
username = self.form.username.data.lower()
try:
if utils.is_email_valid(username):
user = models.User.get_by_email(username)
if user:
auth_id = user.auth_ids[0]
else:
raise InvalidAuthIdError
else:
auth_id = "own:%s" % username
user = models.User.get_by_auth_id(auth_id)
password = self.form.password.data.strip()
remember_me = True if str(self.request.POST.get('remember_me')) == 'on' else False
# Password to SHA512
password = utils.encrypt(password, config.salt)
# Try to login user with password
# Raises InvalidAuthIdError if user is not found
# Raises InvalidPasswordError if provided password
# doesn't match with specified user
self.auth.get_user_by_password(
auth_id, password, remember=remember_me)
# if user account is not activated, logout and redirect to home
if (user.activated == False):
# logout
self.auth.unset_session()
# redirect to home with error message
resend_email_uri = self.uri_for('resend-account-activation', encoded_email=utils.encode(user.email))
message = _('Sorry, your account') + ' {0:>s}'.format(username) + " " +\
_('has not been activated. Please check your email to activate your account') + ". " +\
_('Or click') + " " + _('this') + " " + _('to resend the email')
self.add_message(message, 'error')
return self.redirect_to('home')
# check twitter association in session
twitter_helper = twitter.TwitterAuth(self)
twitter_association_data = twitter_helper.get_association_data()
if twitter_association_data is not None:
if models.SocialUser.check_unique(user.key, 'twitter', str(twitter_association_data['id'])):
social_user = models.SocialUser(
user = user.key,
provider = 'twitter',
uid = str(twitter_association_data['id']),
extra_data = twitter_association_data
)
social_user.put()
logVisit = models.LogVisit(
user=user.key,
uastring=self.request.user_agent,
ip=self.request.remote_addr,
timestamp=utils.get_date_time()
)
logVisit.put()
self.redirect_to('home')
except (InvalidAuthIdError, InvalidPasswordError), e:
# Returns error message to self.response.write in
# the BaseHandler.dispatcher
message = _("Login invalid, Try again.") + "
" + _("Don't have an account?") + \
' ' + _("Sign Up") + ''
self.add_message(message, 'error')
return self.redirect_to('login')
class SocialLoginHandler(BaseHandler):
"""
Handler for Social authentication
"""
def get(self, provider_name):
provider_display_name = models.SocialUser.PROVIDERS_INFO[provider_name]['label']
if not config.enable_federated_login:
message = _('Federated login is disabled.')
self.add_message(message,'warning')
return self.redirect_to('login')
callback_url = "%s/social_login/%s/complete" % (self.request.host_url, provider_name)
if provider_name == "twitter":
twitter_helper = twitter.TwitterAuth(self, redirect_uri=callback_url)
self.redirect(twitter_helper.auth_url())
else:
message = _('%s authentication is not implemented yet.') % provider_display_name
self.add_message(message,'warning')
self.redirect_to('edit-profile')
class CallbackSocialLoginHandler(BaseHandler):
"""
Callback (Save Information) for Social Authentication
"""
def get(self, provider_name):
if not config.enable_federated_login:
message = _('Federated login is disabled.')
self.add_message(message,'warning')
return self.redirect_to('login')
if provider_name == "twitter":
oauth_token = self.request.get('oauth_token')
oauth_verifier = self.request.get('oauth_verifier')
twitter_helper = twitter.TwitterAuth(self)
user_data = twitter_helper.auth_complete(oauth_token,
oauth_verifier)
if self.user:
# new association with twitter
user_info = models.User.get_by_id(long(self.user_id))
if models.SocialUser.check_unique(user_info.key, 'twitter', str(user_data['id'])):
social_user = models.SocialUser(
user = user_info.key,
provider = 'twitter',
uid = str(user_data['id']),
extra_data = user_data
)
social_user.put()
message = _('Twitter association added!')
self.add_message(message,'success')
else:
message = _('This Twitter account is already in use!')
self.add_message(message,'error')
self.redirect_to('edit-profile')
else:
# login with twitter
social_user = models.SocialUser.get_by_provider_and_uid('twitter',
str(user_data['id']))
if social_user:
# Social user exists. Need authenticate related site account
user = social_user.user.get()
self.auth.set_session(self.auth.store.user_to_dict(user), remember=True)
logVisit = models.LogVisit(
user = user.key,
uastring = self.request.user_agent,
ip = self.request.remote_addr,
timestamp = utils.get_date_time()
)
logVisit.put()
self.redirect_to('home')
else:
# Social user does not exists. Need show login and registration forms
twitter_helper.save_association_data(user_data)
message = _('Account with association to your Twitter does not exist. You can associate it right now, if you login with existing site account or create new on Sign up page.')
self.add_message(message,'info')
self.redirect_to('login')
# Debug Callback information provided
# for k,v in user_data.items():
# print(k +":"+ v )
# google, myopenid, yahoo OpenID Providers
elif provider_name in models.SocialUser.open_id_providers():
provider_display_name = models.SocialUser.PROVIDERS_INFO[provider_name]['label']
# get info passed from OpenId Provider
from google.appengine.api import users
current_user = users.get_current_user()
if current_user:
if current_user.federated_identity():
uid = current_user.federated_identity()
else:
uid = current_user.user_id()
email = current_user.email()
else:
message = _('No user authentication information received from %s. Please ensure you are logging in from an authorized OpenID Provider (OP).' % provider_display_name)
self.add_message(message,'error')
return self.redirect_to('login')
if self.user:
# add social account to user
user_info = models.User.get_by_id(long(self.user_id))
if models.SocialUser.check_unique(user_info.key, provider_name, uid):
social_user = models.SocialUser(
user = user_info.key,
provider = provider_name,
uid = uid
)
social_user.put()
message = provider_display_name + _(' association added!')
self.add_message(message,'success')
else:
message = _('This %s account is already in use!' % provider_display_name)
self.add_message(message,'error')
self.redirect_to('edit-profile')
else:
# login with OpenId Provider
social_user = models.SocialUser.get_by_provider_and_uid(provider_name, uid)
if social_user:
# Social user found. Authenticate the user
user = social_user.user.get()
self.auth.set_session(self.auth.store.user_to_dict(user), remember=True)
logVisit = models.LogVisit(
user = user.key,
uastring = self.request.user_agent,
ip = self.request.remote_addr,
timestamp = utils.get_date_time()
)
logVisit.put()
self.redirect_to('home')
else:
# Social user does not exist yet so create it with the federated identity provided (uid)
# and create prerequisite user and log the user account in
if models.SocialUser.check_unique_uid(provider_name, uid):
# create user
# Returns a tuple, where first value is BOOL.
# If True ok, If False no new user is created
# Assume provider has already verified email address
# if email is provided so set activated to True
auth_id = "%s:%s" % (provider_name, uid)
if email:
unique_properties = ['email']
user_info = self.auth.store.user_model.create_user(
auth_id, unique_properties, email=email,
activated=True
)
else:
user_info = self.auth.store.user_model.create_user(
auth_id, activated=True
)
if not user_info[0]: #user is a tuple
message = _('This %s account is already in use!' % provider_display_name)
self.add_message(message, 'error')
return self.redirect_to('register')
user = user_info[1]
# create social user and associate with user
social_user = models.SocialUser(
user = user.key,
provider = provider_name,
uid = uid
)
social_user.put()
# authenticate user
self.auth.set_session(self.auth.store.user_to_dict(user), remember=True)
logVisit = models.LogVisit(
user = user.key,
uastring = self.request.user_agent,
ip = self.request.remote_addr,
timestamp = utils.get_date_time()
)
logVisit.put()
self.redirect_to('home')
message = provider_display_name + _(' association added!')
self.add_message(message,'success')
self.redirect_to('home')
else:
message = _('This %s account is already in use!' % provider_display_name)
self.add_message(message,'error')
self.redirect_to('login')
else:
message = _('%s authentication is not implemented yet.') % provider_display_name
self.add_message(message,'warning')
self.redirect_to('login')
class DeleteSocialProviderHandler(BaseHandler):
"""
Delete Social association with an account
"""
@user_required
def get(self, provider_name):
if self.user:
user_info = models.User.get_by_id(long(self.user_id))
social_user = models.SocialUser.get_by_user_and_provider(user_info.key, provider_name)
if social_user:
social_user.key.delete()
message = provider_name + _(' disassociated!')
self.add_message(message,'success')
else:
message = _('Social account on ') + provider_name + _(' not found for this user!')
self.add_message(message,'error')
self.redirect_to('edit-profile')
class LogoutHandler(BaseHandler):
"""
Destroy user session and redirect to login
"""
def get(self):
if self.user:
message = _("You've signed out successfully. Warning: Please clear all cookies and logout \
of OpenId providers too if you logged in on a public computer.") # Info message
self.add_message(message, 'info')
self.auth.unset_session()
# User is logged out, let's try redirecting to login page
try:
self.redirect(self.auth_config['login_url'])
except (AttributeError, KeyError), e:
return _("User is logged out, but there was an error "\
"on the redirection.")
class RegisterHandler(RegisterBaseHandler):
"""
Handler for Sign Up Users
"""
def get(self):
""" Returns a simple HTML form for create a new user """
if self.user:
self.redirect_to('home', id=self.user_id)
params = {}
return self.render_template('boilerplate_register.html', **params)
def post(self):
""" Get fields from POST dict """
if not self.form.validate():
return self.get()
username = self.form.username.data.lower()
name = self.form.name.data.strip()
last_name = self.form.last_name.data.strip()
email = self.form.email.data.lower()
password = self.form.password.data.strip()
country = self.form.country.data
# Password to SHA512
password = utils.encrypt(password, config.salt)
# Passing password_raw=password so password will be hashed
# Returns a tuple, where first value is BOOL.
# If True ok, If False no new user is created
unique_properties = ['username', 'email']
auth_id = "own:%s" % username
user = self.auth.store.user_model.create_user(
auth_id, unique_properties, password_raw=password,
username=username, name=name, last_name=last_name, email=email,
country=country, activated=False
)
if not user[0]: #user is a tuple
message = _('Sorry, This user') + ' {0:>s}'.format(username) + " " +\
_('is already registered.')
self.add_message(message, 'error')
return self.redirect_to('register')
else:
# User registered successfully
# But if the user registered using the form, the user has to check their email to activate the account ???
try:
user_info = models.User.get_by_email(email)
if (user_info.activated == False):
# send email
subject = config.app_name + " Account Verification Email"
encoded_email = utils.encode(email)
confirmation_url = self.uri_for("account-activation",
encoded_email = encoded_email,
_full = True)
# load email's template
template_val = {
"app_name": config.app_name,
"username": username,
"confirmation_url": confirmation_url,
"support_url": self.uri_for("contact", _full=True)
}
body_path = "emails/account_activation.txt"
body = self.jinja2.render_template(body_path, **template_val)
email_url = self.uri_for('taskqueue-send-email')
taskqueue.add(url = email_url, params={
'to': str(email),
'subject' : subject,
'body' : body,
})
message = _('Congratulations') + ", " + str(username) + "! " + _('You are now registered') +\
". " + _('Please check your email to activate your account')
self.add_message(message, 'success')
return self.redirect_to('home')
# If the user didn't register using registration form ???
db_user = self.auth.get_user_by_password(user[1].auth_ids[0], password)
# Check twitter association in session
twitter_helper = twitter.TwitterAuth(self)
twitter_association_data = twitter_helper.get_association_data()
if twitter_association_data is not None:
if models.SocialUser.check_unique(user[1].key, 'twitter', str(twitter_association_data['id'])):
social_user = models.SocialUser(
user = user[1].key,
provider = 'twitter',
uid = str(twitter_association_data['id']),
extra_data = twitter_association_data
)
social_user.put()
message = _('Welcome') + " " + str(username) + ", " + _('you are now logged in.')
self.add_message(message, 'success')
return self.redirect_to('home')
except (AttributeError, KeyError), e:
message = _('Unexpected error creating '\
'user') + " " + '{0:>s}.'.format(username)
self.add_message(message, 'error')
self.abort(403)
class AccountActivationHandler(BaseHandler):
"""
Handler for account activation
"""
def get(self, encoded_email):
try:
email = utils.decode(encoded_email)
user = models.User.get_by_email(email)
# activate the user's account
user.activated = True
user.put()
message = _('Congratulations') + "! " + _('Your account') + " (@" + user.username + ") " +\
_('has just been activated') + ". " + _('Please login to your account')
self.add_message(message, "success")
self.redirect_to('login')
except (AttributeError, KeyError, InvalidAuthIdError), e:
message = _('Unexpected error activating '\
'account') + " " + '{0:>s}.'.format(user.username)
self.add_message(message, 'error')
self.abort(403)
class ResendActivationEmailHandler(BaseHandler):
"""
Handler to resend activation email
"""
def get(self, encoded_email):
try:
email = utils.decode(encoded_email)
user = models.User.get_by_email(email)
if (user.activated == False):
# send email
subject = config.app_name + " Account Verification Email"
encoded_email = utils.encode(email)
confirmation_url = self.uri_for("account-activation",
encoded_email = encoded_email,
_full = True)
# load email's template
template_val = {
"app_name": config.app_name,
"username": user.username,
"confirmation_url": confirmation_url,
"support_url": self.uri_for("contact", _full=True)
}
body_path = "emails/account_activation.txt"
body = self.jinja2.render_template(body_path, **template_val)
email_url = self.uri_for('taskqueue-send-email')
taskqueue.add(url = email_url, params={
'to': str(email),
'subject' : subject,
'body' : body,
})
message = _('The verification email has been resent to') + " " + str(email) + ". " +\
_('Please check your email to activate your account')
self.add_message(message, "success")
return self.redirect_to('home')
else:
message = _('Your account has been activated') + ". " +\
_('Please login to your account')
self.add_message(message, "warning")
return self.redirect_to('home')
except (KeyError, AttributeError), e:
message = _('Sorry') + ". " + _('Some error occurred') + "."
self.add_message(message, "error")
return self.redirect_to('home')
class ContactHandler(BaseHandler):
"""
Handler for Contact Form
"""
def get(self):
""" Returns a simple HTML for contact form """
if self.user:
user_info = models.User.get_by_id(long(self.user_id))
if user_info.name or user_info.last_name:
self.form.name.data = user_info.name + " " + user_info.last_name
if user_info.email:
self.form.email.data = user_info.email
params = {
"exception" : self.request.get('exception')
}
return self.render_template('boilerplate_contact.html', **params)
def post(self):
""" validate contact form """
if not self.form.validate():
return self.get()
remoteip = self.request.remote_addr
user_agent = self.request.user_agent
exception = self.request.POST.get('exception')
name = self.form.name.data.strip()
email = self.form.email.data.lower()
message = self.form.message.data.strip()
try:
subject = _("Contact")
body = ""
# exceptions for error pages that redirect to contact
if exception != "":
body = "* Exception error: %s" % exception
body = body + """
* IP Address: %s
* Web Browser: %s
* Sender name: %s
* Sender email: %s
* Message: %s
""" % (remoteip, user_agent, name, email, message)
email_url = self.uri_for('taskqueue-send-email')
taskqueue.add(url = email_url, params={
'to': config.contact_recipient,
'subject' : subject,
'body' : body,
'sender' : config.contact_sender,
})
message = _('Message sent successfully.')
self.add_message(message, 'success')
return self.redirect_to('contact')
except (AttributeError, KeyError), e:
message = _('Error sending the message. Please try again later.')
self.add_message(message, 'error')
return self.redirect_to('contact')
@webapp2.cached_property
def form(self):
return forms.ContactForm(self)
class EditProfileHandler(BaseHandler):
"""
Handler for Edit User Profile
"""
@user_required
def get(self):
""" Returns a simple HTML form for edit profile """
params = {}
if self.user:
user_info = models.User.get_by_id(long(self.user_id))
self.form.username.data = user_info.username
self.form.name.data = user_info.name
self.form.last_name.data = user_info.last_name
self.form.country.data = user_info.country
providers_info = user_info.get_social_providers_info()
params['used_providers'] = providers_info['used']
params['unused_providers'] = providers_info['unused']
params['country'] = user_info.country
return self.render_template('boilerplate_edit_profile.html', **params)
def post(self):
""" Get fields from POST dict """
if not self.form.validate():
return self.get()
username = self.form.username.data.lower()
name = self.form.name.data.strip()
last_name = self.form.last_name.data.strip()
country = self.form.country.data
try:
user_info = models.User.get_by_id(long(self.user_id))
try:
message=''
# update username if it has changed and it isn't already taken
if username != user_info.username:
user_info.unique_properties = ['username','email']
uniques = [
'User.username:%s' % username,
'User.auth_id:own:%s' % username,
]
# Create the unique username and auth_id.
success, existing = Unique.create_multi(uniques)
if success:
# free old uniques
Unique.delete_multi(['User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username])
# The unique values were created, so we can save the user.
user_info.username=username
user_info.auth_ids[0]='own:%s' % username
message+= _('Your new username is ') + '' + username + '.'
else:
message+= _('Username') + " " + username + " " + _('is already taken. It is not changed.')
# At least one of the values is not unique.
self.add_message(message,'error')
return self.get()
user_info.name=name
user_info.last_name=last_name
user_info.country=country
user_info.put()
message+= " " + _('Your profile has been updated!')
self.add_message(message,'success')
return self.get()
except (AttributeError, KeyError, ValueError), e:
message = _('Unable to update profile!')
logging.error('Unable to update profile: ' + e)
self.add_message(message,'error')
return self.get()
except (AttributeError, TypeError), e:
login_error_message = _('Sorry you are not logged in!')
self.add_message(login_error_message,'error')
self.redirect_to('login')
@webapp2.cached_property
def form(self):
return forms.EditProfileForm(self)
class EditPasswordHandler(BaseHandler):
"""
Handler for Edit User Password
"""
@user_required
def get(self):
""" Returns a simple HTML form for editing password """
params = {}
return self.render_template('boilerplate_edit_password.html', **params)
def post(self):
""" Get fields from POST dict """
if not self.form.validate():
return self.get()
current_password = self.form.current_password.data.strip()
password = self.form.password.data.strip()
try:
user_info = models.User.get_by_id(long(self.user_id))
auth_id = "own:%s" % user_info.username
# Password to SHA512
current_password = utils.encrypt(current_password, config.salt)
try:
user = models.User.get_by_auth_password(auth_id, current_password)
# Password to SHA512
password = utils.encrypt(password, config.salt)
user.password = security.generate_password_hash(password, length=12)
user.put()
# send email
subject = config.app_name + " Account Password Changed"
# load email's template
template_val = {
"app_name": config.app_name,
"first_name": user.name,
"username": user.username,
"email": user.email,
"reset_password_url": self.uri_for("password-reset", _full=True)
}
email_body_path = "emails/password_changed.txt"
email_body = self.jinja2.render_template(email_body_path, **template_val)
email_url = self.uri_for('taskqueue-send-email')
taskqueue.add(url = email_url, params={
'to': user.email,
'subject' : subject,
'body' : email_body,
'sender' : config.contact_sender,
})
# Login User
self.auth.get_user_by_password(user.auth_ids[0], password)
self.add_message(_('Password changed successfully'), 'success')
return self.redirect_to('edit-profile')
except (InvalidAuthIdError, InvalidPasswordError), e:
# Returns error message to self.response.write in
# the BaseHandler.dispatcher
message = _("Your Current Password is wrong, please try again")
self.add_message(message, 'error')
return self.redirect_to('edit-password')
except (AttributeError,TypeError), e:
login_error_message = _('Sorry you are not logged in!')
self.add_message(login_error_message,'error')
self.redirect_to('login')
@webapp2.cached_property
def form(self):
if self.is_mobile:
return forms.EditPasswordMobileForm(self)
else:
return forms.EditPasswordForm(self)
class EditEmailHandler(BaseHandler):
"""
Handler for Edit User's Email
"""
@user_required
def get(self):
""" Returns a simple HTML form for edit email """
params = {}
if self.user:
user_info = models.User.get_by_id(long(self.user_id))
self.form.new_email.data = user_info.email
return self.render_template('boilerplate_edit_email.html', **params)
def post(self):
""" Get fields from POST dict """
if not self.form.validate():
return self.get()
new_email = self.form.new_email.data.strip()
password = self.form.password.data.strip()
try:
user_info = models.User.get_by_id(long(self.user_id))
auth_id = "own:%s" % user_info.username
# Password to SHA512
password = utils.encrypt(password, config.salt)
try:
# authenticate user by its password
user = models.User.get_by_auth_password(auth_id, password)
# if the user change his/her email address
if new_email != user.email:
# check whether the new email has been used by another user
aUser = models.User.get_by_email(new_email)
if aUser is not None:
message = _("The email %s is already registered." % new_email)
self.add_message(message, "error")
return self.redirect_to("edit-email")
# send email
subject = config.app_name + " Email Changed Notification"
user_token = models.User.create_auth_token(self.user_id)
confirmation_url = self.uri_for("email-changed-check",
user_id = user_info.get_id(),
encoded_email = utils.encode(new_email),
token = user_token,
_full = True)
# load email's template
template_val = {
"app_name": config.app_name,
"first_name": user.name,
"username": user.username,
"new_email": new_email,
"confirmation_url": confirmation_url,
"support_url": self.uri_for("contact", _full=True)
}
old_body_path = "emails/email_changed_notification_old.txt"
old_body = self.jinja2.render_template(old_body_path, **template_val)
new_body_path = "emails/email_changed_notification_new.txt"
new_body = self.jinja2.render_template(new_body_path, **template_val)
email_url = self.uri_for('taskqueue-send-email')
taskqueue.add(url = email_url, params={
'to': user.email,
'subject' : subject,
'body' : old_body,
})
email_url = self.uri_for('taskqueue-send-email')
taskqueue.add(url = email_url, params={
'to': new_email,
'subject' : subject,
'body' : new_body,
})
logging.error(user)
# display successful message
msg = _("Please check your new email for confirmation. Your email will be updated after confirmation.")
self.add_message(msg, 'success')
return self.redirect_to('edit-profile')
else:
self.add_message(_("You didn't change your email"), "warning")
return self.redirect_to("edit-email")
except (InvalidAuthIdError, InvalidPasswordError), e:
# Returns error message to self.response.write in
# the BaseHandler.dispatcher
message = _("Your password is wrong, please try again")
self.add_message(message, 'error')
return self.redirect_to('edit-email')
except (AttributeError,TypeError), e:
login_error_message = _('Sorry you are not logged in!')
self.add_message(login_error_message,'error')
self.redirect_to('login')
@webapp2.cached_property
def form(self):
return forms.EditEmailForm(self)
class PasswordResetHandler(LoginBaseHandler):
"""
Password Reset Handler with Captcha
"""
reCaptcha_public_key = config.captcha_public_key
reCaptcha_private_key = config.captcha_private_key
def get(self):
chtml = captcha.displayhtml(
public_key = self.reCaptcha_public_key,
use_ssl = False,
error = None)
params = {
'captchahtml': chtml,
}
return self.render_template('boilerplate_password_reset.html', **params)
def post(self):
# check captcha
challenge = self.request.POST.get('recaptcha_challenge_field')
response = self.request.POST.get('recaptcha_response_field')
remoteip = self.request.remote_addr
cResponse = captcha.submit(
challenge,
response,
self.reCaptcha_private_key,
remoteip)
if cResponse.is_valid:
# captcha was valid... carry on..nothing to see here
pass
else:
logging.warning(cResponse.error_code)
_message = _('Wrong image verification code. Please try again.')
self.add_message(_message, 'error')
return self.redirect_to('password-reset')
# check if we got an email or username
email_or_username = str(self.request.POST.get('email_or_username')).lower().strip()
if utils.is_email_valid(email_or_username):
user = models.User.get_by_email(email_or_username)
_message = _("If the e-mail address you entered") + " (%s) " % email_or_username
else:
auth_id = "own:%s" % email_or_username
user = models.User.get_by_auth_id(auth_id)
_message = _("If the username you entered") + " (%s) " % email_or_username
if user is not None:
user_id = user.get_id()
token = models.User.create_auth_token(user_id)
email_url = self.uri_for('taskqueue-send-email')
reset_url = self.uri_for('password-reset-check', user_id=user_id, token=token, _full=True)
subject = _("Password reminder")
body = _('Please click below to create a new password:') +\
"""
%s
""" % reset_url
taskqueue.add(url = email_url, params={
'to': user.email,
'subject' : subject,
'body' : body,
'sender' : config.contact_sender,
})
_message = _message + _("is associated with an account in our records, you will receive "\
"an e-mail from us with instructions for resetting your password. "\
"
If you don't receive this e-mail, please check your junk mail folder or ") +\
'' + _('contact us') + ' ' + _("for further assistance.")
self.add_message(_message, 'success')
return self.redirect_to('login')
_message = _('Your email / username was not found. Please try another or ') + '' + _('create an account') + ''
self.add_message(_message, 'error')
return self.redirect_to('password-reset')
class PasswordResetCompleteHandler(LoginBaseHandler):
"""
Handler to process the link of reset password that received the user
"""
def get(self, user_id, token):
verify = models.User.get_by_auth_token(int(user_id), token)
params = {}
if verify[0] is None:
message = _('There was an error or the link is outdated. Please copy and paste the link from your email or enter your details again below to get a new one.')
self.add_message(message, 'warning')
return self.redirect_to('password-reset')
else:
return self.render_template('boilerplate_password_reset_complete.html', **params)
def post(self, user_id, token):
verify = models.User.get_by_auth_token(int(user_id), token)
user = verify[0]
password = self.form.password.data.strip()
if user and self.form.validate():
# Password to SHA512
password = utils.encrypt(password, config.salt)
user.password = security.generate_password_hash(password, length=12)
user.put()
# Delete token
models.User.delete_auth_token(int(user_id), token)
# Login User
self.auth.get_user_by_password(user.auth_ids[0], password)
self.add_message(_('Password changed successfully'), 'success')
return self.redirect_to('home')
else:
self.add_message(_('Please correct the form errors.'), 'error')
return self.redirect_to('password-reset-check', user_id=user_id, token=token)
@webapp2.cached_property
def form(self):
if self.is_mobile:
return forms.PasswordResetCompleteMobileForm(self)
else:
return forms.PasswordResetCompleteForm(self)
class EmailChangedCompleteHandler(BaseHandler):
"""
Handler for completed email change
Will be called when the user click confirmation link from email
"""
def get(self, user_id, encoded_email, token):
verify = models.User.get_by_auth_token(int(user_id), token)
email = utils.decode(encoded_email)
if verify[0] is None:
self.add_message('There was an error or the link is outdated. Please copy and paste the link from your email.', 'warning')
self.redirect_to('home')
else:
# save new email
user = verify[0]
user.email = email
user.put()
# delete token
models.User.delete_auth_token(int(user_id), token)
# add successful message and redirect
self.add_message("Your email has been successfully updated!", "success")
self.redirect_to('edit-profile')
class SecureRequestHandler(BaseHandler):
"""
Only accessible to users that are logged in
"""
@user_required
def get(self, **kwargs):
user_session = self.user
user_session_object = self.auth.store.get_session(self.request)
user_info = models.User.get_by_id(long( self.user_id ))
user_info_object = self.auth.store.user_model.get_by_auth_token(
user_session['user_id'], user_session['token'])
try:
params = {
"user_session" : user_session,
"user_session_object" : user_session_object,
"user_info" : user_info,
"user_info_object" : user_info_object,
"userinfo_logout-url" : self.auth_config['logout_url'],
}
return self.render_template('boilerplate_secure_zone.html', **params)
except (AttributeError, KeyError), e:
return _("Secure zone error:") + " %s." % e
class HomeRequestHandler(RegisterBaseHandler):
"""
Handler to show the home page
"""
def get(self):
""" Returns a simple HTML form for home """
params = {}
return self.render_template('boilerplate_home.html', **params)
autopep8-0.9.1/test/test_autopep8.py 0000775 0001750 0001750 00000302770 12153410031 020575 0 ustar hattori hattori 0000000 0000000 #!/usr/bin/env python
# coding: utf-8
from __future__ import unicode_literals
import os
import sys
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
import contextlib
from subprocess import Popen, PIPE
from tempfile import mkstemp
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
ROOT_DIR = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0]
sys.path.insert(0, ROOT_DIR)
import autopep8
if 'AUTOPEP8_COVERAGE' in os.environ and int(os.environ['AUTOPEP8_COVERAGE']):
AUTOPEP8_CMD_TUPLE = ('coverage', 'run', '--branch', '--parallel',
'--omit=*/site-packages/*',
os.path.join(ROOT_DIR, 'autopep8.py'),)
else:
# We need to specify the executable to make sure the correct Python
# interpreter gets used.
AUTOPEP8_CMD_TUPLE = (sys.executable,
os.path.join(ROOT_DIR,
'autopep8.py'),) # pragma: no cover
class UnitTests(unittest.TestCase):
def test_find_newline_only_cr(self):
source = ['print 1\r', 'print 2\r', 'print3\r']
self.assertEqual(autopep8.CR, autopep8.find_newline(source))
def test_find_newline_only_lf(self):
source = ['print 1\n', 'print 2\n', 'print3\n']
self.assertEqual(autopep8.LF, autopep8.find_newline(source))
def test_find_newline_only_crlf(self):
source = ['print 1\r\n', 'print 2\r\n', 'print3\r\n']
self.assertEqual(autopep8.CRLF, autopep8.find_newline(source))
def test_find_newline_cr1_and_lf2(self):
source = ['print 1\n', 'print 2\r', 'print3\n']
self.assertEqual(autopep8.LF, autopep8.find_newline(source))
def test_find_newline_cr1_and_crlf2(self):
source = ['print 1\r\n', 'print 2\r', 'print3\r\n']
self.assertEqual(autopep8.CRLF, autopep8.find_newline(source))
def test_detect_encoding(self):
self.assertEqual(
'utf-8',
autopep8.detect_encoding(
os.path.join(ROOT_DIR, 'setup.py')))
def test_detect_encoding_with_cookie(self):
self.assertEqual(
'iso-8859-1',
autopep8.detect_encoding(
os.path.join(ROOT_DIR, 'test', 'iso_8859_1.py')))
def test_read_from_filename_with_bad_encoding(self):
"""Bad encoding should not cause an exception."""
self.assertEqual(
'# -*- coding: zlatin-1 -*-\n',
autopep8.read_from_filename(
os.path.join(ROOT_DIR, 'test', 'bad_encoding.py')))
def test_read_from_filename_with_bad_encoding2(self):
"""Bad encoding should not cause an exception."""
# This causes a warning on Python 3.
import warnings
with warnings.catch_warnings(record=True):
self.assertTrue(autopep8.read_from_filename(
os.path.join(ROOT_DIR, 'test', 'bad_encoding2.py')))
def test_fix_whitespace(self):
self.assertEqual(
'a b',
autopep8.fix_whitespace('a b', offset=1, replacement=' '))
def test_fix_whitespace_with_tabs(self):
self.assertEqual(
'a b',
autopep8.fix_whitespace('a\t \t b', offset=1, replacement=' '))
def test_break_multiline(self):
self.assertEqual(
'foo_bar_zap_bing_bang_boom(\n 111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333,\n',
autopep8.break_multiline(
'foo_bar_zap_bing_bang_boom(111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333,\n',
newline='\n', indent_word=' '))
def test_break_multiline_with_long_function(self):
self.assertEqual(
'foo_bar_zap_bing_bang_boom_foo_bar_zap_bing_bang_boom_foo_bar_zap_bing_bang_boom(\n'
' 333,\n',
autopep8.break_multiline(
'foo_bar_zap_bing_bang_boom_foo_bar_zap_bing_bang_boom_foo_bar_zap_bing_bang_boom(333,\n',
newline='\n', indent_word=' '))
def test_break_multiline_should_not_break_too_short_line(self):
self.assertEqual(
None,
autopep8.break_multiline(
'fo(111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333, 333,\n',
newline='\n', indent_word=' '))
def test_break_multiline_should_not_modify_comment(self):
self.assertEqual(
None,
autopep8.break_multiline(
'# foo_bar_zap_bing_bang_boom(111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333,\n',
newline='\n', indent_word=' '))
def test_break_multiline_should_not_modify_lonely_brace(self):
self.assertEqual(
None,
autopep8.break_multiline(
'(111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333, 222, 222, 222, 222, 333,\n',
newline='\n', indent_word=' '))
def test_multiline_string_lines(self):
self.assertEqual(
set([2]),
autopep8.multiline_string_lines(
"""
'''
'''
""".lstrip()))
def test_multiline_string_lines_with_many(self):
self.assertEqual(
set([2, 7, 10, 11, 12]),
autopep8.multiline_string_lines(
"""
'''
'''
''''''
''''''
''''''
'''
'''
'''
'''
""".lstrip()))
def test_multiline_string_should_not_report_single_line(self):
self.assertEqual(
set(),
autopep8.multiline_string_lines(
"""
'''abc'''
""".lstrip()))
def test_multiline_string_should_not_report_docstrings(self):
self.assertEqual(
set([5]),
autopep8.multiline_string_lines(
"""
def foo():
'''Foo.
Bar.'''
hello = '''
'''
""".lstrip()))
def test_supported_fixes(self):
self.assertIn('E101', [f[0] for f in autopep8.supported_fixes()])
def test_shorten_comment(self):
self.assertEqual('# ' + '=' * 72 + '\n',
autopep8.shorten_comment('# ' + '=' * 100 + '\n',
'\n',
max_line_length=79))
def test_shorten_comment_should_not_split_numbers(self):
line = '# ' + '0' * 100 + '\n'
self.assertEqual(line,
autopep8.shorten_comment(line,
newline='\n',
max_line_length=79))
def test_shorten_comment_should_not_split_words(self):
line = '# ' + 'a' * 100 + '\n'
self.assertEqual(line,
autopep8.shorten_comment(line,
newline='\n',
max_line_length=79))
def test_shorten_comment_should_not_split_urls(self):
line = '# http://foo.bar/' + 'abc-' * 100 + '\n'
self.assertEqual(line,
autopep8.shorten_comment(line,
newline='\n',
max_line_length=79))
def test_shorten_comment_should_not_modify_special_comments(self):
line = '#!/bin/blah ' + ' x' * 90 + '\n'
self.assertEqual(line,
autopep8.shorten_comment(line,
newline='\n',
max_line_length=79))
def test_format_block_comments(self):
self.assertEqual(
'# abc',
autopep8.fix_e26('#abc'))
self.assertEqual(
'# abc',
autopep8.fix_e26('####abc'))
self.assertEqual(
'# abc',
autopep8.fix_e26('## # ##abc'))
def test_format_block_comments_with_multiple_lines(self):
self.assertEqual(
"""
# abc
# blah blah
# four space indentation
''' #do not modify strings
#do not modify strings
#do not modify strings
#do not modify strings'''
#
""".lstrip(),
autopep8.fix_e26("""
# abc
#blah blah
#four space indentation
''' #do not modify strings
#do not modify strings
#do not modify strings
#do not modify strings'''
#
""".lstrip()))
def test_format_block_comments_should_not_corrupt_special_comments(self):
self.assertEqual(
'#: abc',
autopep8.fix_e26('#: abc'))
self.assertEqual(
'#!/bin/bash\n',
autopep8.fix_e26('#!/bin/bash\n'))
def test_fix_file(self):
self.assertIn(
'import ',
autopep8.fix_file(
filename=os.path.join(ROOT_DIR, 'test', 'example.py')))
def test_fix_file_with_diff(self):
filename = os.path.join(ROOT_DIR, 'test', 'example.py')
self.assertIn(
'@@',
autopep8.fix_file(
filename=filename,
options=autopep8.parse_args(['--diff', filename])[0]))
def test_fix_lines(self):
self.assertEqual(
'print(123)\n',
autopep8.fix_lines(['print( 123 )\n'],
options=autopep8.parse_args([''])[0]))
def test_fix_string(self):
self.assertEqual(
'print(123)\n',
autopep8.fix_string('print( 123 )\n'))
def test_fix_string_with_empty_string(self):
self.assertEqual(
'',
autopep8.fix_string(''))
def test_fix_string_with_multiple_lines(self):
self.assertEqual(
'print(123)\nx = 4\n',
autopep8.fix_string('print( 123 )\nx =4'))
def test_normalize_line_endings(self):
self.assertEqual(
['abc\n', 'def\n', '123\n', 'hello\n', 'world\n'],
autopep8.normalize_line_endings(
['abc\n', 'def\n', '123\r', 'hello\r\n', 'world\r']))
def test_normalize_line_endings_with_crlf(self):
self.assertEqual(
['abc\r\n', 'def\r\n', '123\r\n', 'hello\r\n', 'world\r\n'],
autopep8.normalize_line_endings(
['abc\n', 'def\r\n', '123\r\n', 'hello\r\n', 'world\r']))
def test_code_match(self):
self.assertTrue(autopep8.code_match('E2', select='E2,E3', ignore=''))
self.assertTrue(autopep8.code_match('E26', select='E2,E3', ignore=''))
self.assertFalse(autopep8.code_match('E26', select='', ignore='E'))
self.assertFalse(
autopep8.code_match('E2', select='E2,E3', ignore='E2'))
self.assertFalse(autopep8.code_match('E26', select='W', ignore=''))
self.assertFalse(autopep8.code_match('E26', select='W', ignore='E1'))
def test_split_at_offsets(self):
self.assertEqual([''], autopep8.split_at_offsets('', [0]))
self.assertEqual(['1234'], autopep8.split_at_offsets('1234', [0]))
self.assertEqual(['1', '234'], autopep8.split_at_offsets('1234', [1]))
self.assertEqual(['12', '34'], autopep8.split_at_offsets('1234', [2]))
self.assertEqual(['12', '3', '4'],
autopep8.split_at_offsets('1234', [2, 3]))
def test_split_at_offsets_with_out_of_order(self):
self.assertEqual(['12', '3', '4'],
autopep8.split_at_offsets('1234', [3, 2]))
def test_is_probably_inside_string_or_comment(self):
self.assertTrue(autopep8.is_probably_inside_string_or_comment(
'# abc', 1))
self.assertFalse(autopep8.is_probably_inside_string_or_comment(
'hello # abc', 1))
self.assertTrue(autopep8.is_probably_inside_string_or_comment(
'"abc"', 1))
self.assertFalse(autopep8.is_probably_inside_string_or_comment(
'hello "abc"', 1))
self.assertTrue(autopep8.is_probably_inside_string_or_comment(
'"abc"', 0))
self.assertFalse(autopep8.is_probably_inside_string_or_comment(
' "abc"', 0))
def test_fix_w6(self):
self.assertEqual(
'try: pass\nexcept ValueError as e: pass\n',
autopep8.fix_w6('try: pass\nexcept ValueError, e: pass\n'))
self.assertEqual(
'while True: pass\n',
autopep8.fix_w6('while 1: pass\n'))
self.assertEqual(
"""\
import sys
sys.maxsize
""",
autopep8.fix_w6("""\
import sys
sys.maxint
"""))
def test_is_python_file(self):
self.assertTrue(autopep8.is_python_file(
os.path.join(ROOT_DIR, 'autopep8.py')))
with temporary_file_context('#!/usr/bin/env python') as filename:
self.assertTrue(autopep8.is_python_file(filename))
with temporary_file_context('#!/usr/bin/python') as filename:
self.assertTrue(autopep8.is_python_file(filename))
with temporary_file_context('#!/usr/bin/python3') as filename:
self.assertTrue(autopep8.is_python_file(filename))
with temporary_file_context('#!/usr/bin/pythonic') as filename:
self.assertFalse(autopep8.is_python_file(filename))
with temporary_file_context('###!/usr/bin/python') as filename:
self.assertFalse(autopep8.is_python_file(filename))
self.assertFalse(autopep8.is_python_file(os.devnull))
self.assertFalse(autopep8.is_python_file('/bin/bash'))
def test_match_file(self):
with temporary_file_context('', suffix='.py', prefix='.') as filename:
self.assertFalse(autopep8.match_file(filename, exclude=[]),
msg=filename)
self.assertFalse(autopep8.match_file(os.devnull, exclude=[]))
with temporary_file_context('', suffix='.py', prefix='') as filename:
self.assertTrue(autopep8.match_file(filename, exclude=[]),
msg=filename)
def test_line_shortening_rank(self):
self.assertGreater(
autopep8.line_shortening_rank('(1\n+1)\n',
newline='\n', indent_word=' '),
autopep8.line_shortening_rank('(1+\n1)\n',
newline='\n', indent_word=' '))
self.assertGreaterEqual(
autopep8.line_shortening_rank('(1+\n1)\n',
newline='\n', indent_word=' '),
autopep8.line_shortening_rank('(1+1)\n',
newline='\n', indent_word=' '))
self.assertGreater(
autopep8.line_shortening_rank('\n',
newline='\n', indent_word=' '),
autopep8.line_shortening_rank('x\n',
newline='\n', indent_word=' '))
self.assertGreater(
autopep8.line_shortening_rank('[foo(\nx) for x in y]\n',
newline='\n', indent_word=' '),
autopep8.line_shortening_rank('[foo(x)\nfor x in y]\n',
newline='\n', indent_word=' '))
def test_extract_code_from_function(self):
def fix_e123():
pass # pragma: no cover
self.assertEqual('e123', autopep8.extract_code_from_function(fix_e123))
def foo():
pass # pragma: no cover
self.assertEqual(None, autopep8.extract_code_from_function(foo))
def fix_foo():
pass # pragma: no cover
self.assertEqual(None, autopep8.extract_code_from_function(fix_foo))
def e123():
pass # pragma: no cover
self.assertEqual(None, autopep8.extract_code_from_function(e123))
def fix_():
pass # pragma: no cover
self.assertEqual(None, autopep8.extract_code_from_function(fix_))
def test_reindenter(self):
reindenter = autopep8.Reindenter(['if True:\n', ' pass\n'],
'\n')
self.assertEqual(set([1, 2]), reindenter.run())
self.assertEqual(['if True:\n', ' pass\n'],
reindenter.fixed_lines())
def test_reindenter_with_good_input(self):
lines = ['if True:\n', ' pass\n']
reindenter = autopep8.Reindenter(lines,
'\n')
self.assertEqual(set(), reindenter.run())
self.assertEqual(lines,
reindenter.fixed_lines())
def test_reindenter_should_leave_stray_comment_alone(self):
lines = [' #\n', 'if True:\n', ' pass\n']
reindenter = autopep8.Reindenter(lines,
'\n')
self.assertEqual(set([1, 2, 3]), reindenter.run())
self.assertEqual([' #\n', 'if True:\n', ' pass\n'],
reindenter.fixed_lines())
def test_fix_e225_avoid_failure(self):
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents=' 1\n')
self.assertEqual(
[],
fix_pep8.fix_e225({'line': 1,
'column': 5}))
def test_fix_e271_ignore_redundant(self):
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents='x = 1\n')
self.assertEqual(
[],
fix_pep8.fix_e271({'line': 1,
'column': 2}))
def test_fix_e401_avoid_non_import(self):
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents=' 1\n')
self.assertEqual(
[],
fix_pep8.fix_e401({'line': 1,
'column': 5}))
def test_fix_e401_avoid_semicolon(self):
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents='import foo; import bar\n')
self.assertEqual(
[],
fix_pep8.fix_e401({'line': 1,
'column': 5}))
def test_fix_e711_avoid_failure(self):
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents='None == x\n')
self.assertEqual(
[],
fix_pep8.fix_e711({'line': 1,
'column': 6}))
self.assertEqual(
[],
fix_pep8.fix_e711({'line': 1,
'column': 700}))
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents='x <> None\n')
self.assertEqual(
[],
fix_pep8.fix_e711({'line': 1,
'column': 3}))
def test_fix_e712_avoid_failure(self):
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents='True == x\n')
self.assertEqual(
[],
fix_pep8.fix_e712({'line': 1,
'column': 5}))
self.assertEqual(
[],
fix_pep8.fix_e712({'line': 1,
'column': 700}))
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents='x != True\n')
self.assertEqual(
[],
fix_pep8.fix_e712({'line': 1,
'column': 3}))
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents='x == False\n')
self.assertEqual(
[],
fix_pep8.fix_e712({'line': 1,
'column': 3}))
def test_get_logical_with_empty_string(self):
fix_pep8 = autopep8.FixPEP8(filename='',
options=autopep8.parse_args(['']),
contents='')
self.assertEqual(None,
fix_pep8._get_logical({'line': 1,
'column': 1}))
def test_get_diff_text(self):
# We ignore the first two lines since it differs on Python 2.6.
self.assertEqual(
"""\
-foo
+bar
""",
'\n'.join(autopep8.get_diff_text(['foo\n'],
['bar\n'],
'').split('\n')[3:]))
def test_get_diff_text_without_newline(self):
# We ignore the first two lines since it differs on Python 2.6.
self.assertEqual(
"""\
-foo
\ No newline at end of file
+foo
""",
'\n'.join(autopep8.get_diff_text(['foo'],
['foo\n'],
'').split('\n')[3:]))
def test_count_unbalanced_brackets(self):
self.assertEqual(
0,
autopep8.count_unbalanced_brackets('()'))
self.assertEqual(
1,
autopep8.count_unbalanced_brackets('('))
self.assertEqual(
2,
autopep8.count_unbalanced_brackets('(['))
self.assertEqual(
1,
autopep8.count_unbalanced_brackets('[])'))
self.assertEqual(
1,
autopep8.count_unbalanced_brackets(
"'','.join(['%s=%s' % (col, col)')"))
class SystemTests(unittest.TestCase):
def test_e101(self):
line = """
while True:
if True:
\t1
""".lstrip()
fixed = """
while True:
if True:
1
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e101_should_not_expand_non_indentation_tabs(self):
line = """
while True:
if True:
\t1 == '\t'
""".lstrip()
fixed = """
while True:
if True:
1 == '\t'
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e101_should_ignore_multiline_strings(self):
line = """
x = '''
while True:
if True:
\t1
'''
""".lstrip()
fixed = """
x = '''
while True:
if True:
\t1
'''
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e101_should_fix_docstrings(self):
line = """
class Bar(object):
def foo():
'''
\tdocstring
'''
""".lstrip()
fixed = """
class Bar(object):
def foo():
'''
docstring
'''
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e101_when_pep8_mistakes_first_tab_in_string(self):
# pep8 will complain about this even if the tab indentation found
# elsewhere is in a multiline string.
line = """
x = '''
\tHello.
'''
if True:
123
""".lstrip()
fixed = """
x = '''
\tHello.
'''
if True:
123
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e101_should_ignore_multiline_strings_complex(self):
line = """
print(3 <> 4, '''
while True:
if True:
\t1
\t''', 4 <> 5)
""".lstrip()
fixed = """
print(3 != 4, '''
while True:
if True:
\t1
\t''', 4 != 5)
""".lstrip()
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e101_with_comments(self):
line = """
while True: # My inline comment
# with a hanging
# comment.
# Hello
if True:
\t# My comment
\t1
\t# My other comment
""".lstrip()
fixed = """
while True: # My inline comment
# with a hanging
# comment.
# Hello
if True:
# My comment
1
# My other comment
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e101_skip_if_bad_indentation(self):
line = """
try:
\t pass
except:
pass
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e101_skip_innocuous(self):
# pep8 will complain about this even if the tab indentation found
# elsewhere is in a multiline string. If we don't filter the innocuous
# report properly, the below command will take a long time.
p = Popen(list(AUTOPEP8_CMD_TUPLE) +
['-vvv', '--select=E101', '--diff',
os.path.join(ROOT_DIR, 'test', 'e101_example.py')],
stdout=PIPE, stderr=PIPE)
output = [x.decode('utf-8') for x in p.communicate()][0]
self.assertEqual('', output)
def test_e111_short(self):
line = 'class Dummy:\n\n def __init__(self):\n pass\n'
fixed = 'class Dummy:\n\n def __init__(self):\n pass\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e111_long(self):
line = 'class Dummy:\n\n def __init__(self):\n pass\n'
fixed = 'class Dummy:\n\n def __init__(self):\n pass\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e111_longer(self):
line = """
while True:
if True:
1
elif True:
2
""".lstrip()
fixed = """
while True:
if True:
1
elif True:
2
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e111_multiple_levels(self):
line = """
while True:
if True:
1
# My comment
print('abc')
""".lstrip()
fixed = """
while True:
if True:
1
# My comment
print('abc')
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e111_with_dedent(self):
line = """
def foo():
if True:
2
1
""".lstrip()
fixed = """
def foo():
if True:
2
1
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e111_with_other_errors(self):
line = """
def foo():
if True:
(2 , 1)
1
if True:
print('hello')\t
2
""".lstrip()
fixed = """
def foo():
if True:
(2, 1)
1
if True:
print('hello')
2
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e111_should_not_modify_string_contents(self):
line = """
if True:
x = '''
1
'''
""".lstrip()
fixed = """
if True:
x = '''
1
'''
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e12_reindent(self):
line = """
def foo_bar(baz, frop,
fizz, bang): # E128
pass
if True:
x = {
} # E123
#: E121
print "E121", (
"dent")
#: E122
print "E122", (
"dent")
#: E124
print "E124", ("visual",
"indent_two"
)
#: E125
if (row < 0 or self.moduleCount <= row or
col < 0 or self.moduleCount <= col):
raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
#: E126
print "E126", (
"dent")
#: E127
print "E127", ("over-",
"over-indent")
#: E128
print "E128", ("under-",
"under-indent")
"""
fixed = """
def foo_bar(baz, frop,
fizz, bang): # E128
pass
if True:
x = {
} # E123
#: E121
print "E121", (
"dent")
#: E122
print "E122", (
"dent")
#: E124
print "E124", ("visual",
"indent_two"
)
#: E125
if (row < 0 or self.moduleCount <= row or
col < 0 or self.moduleCount <= col):
raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
#: E126
print "E126", (
"dent")
#: E127
print "E127", ("over-",
"over-indent")
#: E128
print "E128", ("under-",
"under-indent")
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e12_reindent_with_multiple_fixes(self):
line = """
sql = 'update %s set %s %s' % (from_table,
','.join(['%s=%s' % (col, col) for col in cols]),
where_clause)
"""
fixed = """
sql = 'update %s set %s %s' % (from_table,
','.join(
['%s=%s' % (col, col) for col in cols]),
where_clause)
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e12_tricky(self):
line = """
#: E126
if (
x == (
3
) or
x == (
3
) or
y == 4):
pass
"""
fixed = """
#: E126
if (
x == (
3
) or
x == (
3
) or
y == 4):
pass
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e12_large(self):
self.maxDiff = None
line = """
class BogusController(controller.CementBaseController):
class Meta:
pass
class BogusController2(controller.CementBaseController):
class Meta:
pass
class BogusController3(controller.CementBaseController):
class Meta:
pass
class BogusController4(controller.CementBaseController):
class Meta:
pass
class TestBaseController(controller.CementBaseController):
class Meta:
pass
class TestBaseController2(controller.CementBaseController):
class Meta:
pass
class TestStackedController(controller.CementBaseController):
class Meta:
arguments = [
]
class TestDuplicateController(controller.CementBaseController):
class Meta:
config_defaults = dict(
foo='bar',
)
arguments = [
(['-f2', '--foo2'], dict(action='store'))
]
def my_command(self):
pass
"""
fixed = """
class BogusController(controller.CementBaseController):
class Meta:
pass
class BogusController2(controller.CementBaseController):
class Meta:
pass
class BogusController3(controller.CementBaseController):
class Meta:
pass
class BogusController4(controller.CementBaseController):
class Meta:
pass
class TestBaseController(controller.CementBaseController):
class Meta:
pass
class TestBaseController2(controller.CementBaseController):
class Meta:
pass
class TestStackedController(controller.CementBaseController):
class Meta:
arguments = [
]
class TestDuplicateController(controller.CementBaseController):
class Meta:
config_defaults = dict(
foo='bar',
)
arguments = [
(['-f2', '--foo2'], dict(action='store'))
]
def my_command(self):
pass
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e12_with_bad_indentation(self):
line = r"""
def bar():
foo(1,
2)
def baz():
pass
pass
"""
with autopep8_context(line, options=['--select=E12']) as result:
self.assertEqual(line, result)
def test_e121_with_stupid_fallback(self):
line = """\
list(''.join([
'%d'
% 1,
list(''),
''
]))
"""
fixed = """\
list(''.join([
'%d'
% 1,
list(''),
''
]))
"""
with autopep8_context(line, options=['--select=E12']) as result:
self.assertEqual(fixed, result)
def test_e123(self):
line = """
if True:
foo = (
)
"""
fixed = """
if True:
foo = (
)
"""
with autopep8_context(line, options=['--select=E12']) as result:
self.assertEqual(fixed, result)
def test_e123_with_escaped_newline(self):
line = r"""
x = \
(
)
"""
fixed = r"""
x = \
(
)
"""
with autopep8_context(line, options=['--select=E12']) as result:
self.assertEqual(fixed, result)
def test_e125(self):
line = """
for k, v in sys.modules.items():
if k in ('setuptools', 'pkg_resources') or (
not os.path.exists(os.path.join(v.__path__[0], '__init__.py'))):
sys.modules.pop(k)
"""
fixed = """
for k, v in sys.modules.items():
if k in ('setuptools', 'pkg_resources') or (
not os.path.exists(os.path.join(v.__path__[0], '__init__.py'))):
sys.modules.pop(k)
"""
with autopep8_context(line, options=['--select=E12']) as result:
self.assertEqual(fixed, result)
def test_e126(self):
line = """
if True:
posted = models.DateField(
default=datetime.date.today,
help_text="help"
)
"""
fixed = """
if True:
posted = models.DateField(
default=datetime.date.today,
help_text="help"
)
"""
with autopep8_context(line, options=['--select=E12']) as result:
self.assertEqual(fixed, result)
def test_e127(self):
line = """
if True:
if True:
chksum = (sum([int(value[i]) for i in xrange(0, 9, 2)]) * 7 -
sum([int(value[i]) for i in xrange(1, 9, 2)])) % 10
"""
fixed = """
if True:
if True:
chksum = (sum([int(value[i]) for i in xrange(0, 9, 2)]) * 7 -
sum([int(value[i]) for i in xrange(1, 9, 2)])) % 10
"""
with autopep8_context(line, options=['--select=E12']) as result:
self.assertEqual(fixed, result)
def test_e127_align_visual_indent(self):
line = """
def draw(self):
color = [([0.2, 0.1, 0.3], [0.2, 0.1, 0.3], [0.2, 0.1, 0.3]),
([0.9, 0.3, 0.5], [0.5, 1.0, 0.5], [0.3, 0.3, 0.9]) ][self._p._colored ]
self.draw_background(color)
""".lstrip()
fixed = """
def draw(self):
color = [([0.2, 0.1, 0.3], [0.2, 0.1, 0.3], [0.2, 0.1, 0.3]),
([0.9, 0.3, 0.5], [0.5, 1.0, 0.5], [0.3, 0.3, 0.9])][self._p._colored]
self.draw_background(color)
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e127_with_backslash(self):
line = r"""
if True:
if True:
self.date = meta.session.query(schedule.Appointment)\
.filter(schedule.Appointment.id ==
appointment_id).one().agenda.endtime
""".lstrip()
fixed = r"""
if True:
if True:
self.date = meta.session.query(schedule.Appointment)\
.filter(schedule.Appointment.id ==
appointment_id).one().agenda.endtime
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e127_with_bracket_then_parenthesis(self):
line = r"""
if True:
foo = [food(1)
for bar in bars]
""".lstrip()
fixed = r"""
if True:
foo = [food(1)
for bar in bars]
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e12_with_backslash(self):
line = r"""
if True:
assert reeval == parsed, \
'Repr gives different object:\n %r !=\n %r' % (parsed, reeval)
"""
fixed = r"""
if True:
assert reeval == parsed, \
'Repr gives different object:\n %r !=\n %r' % (parsed, reeval)
"""
with autopep8_context(line, options=['--select=E12']) as result:
self.assertEqual(fixed, result)
def test_w191(self):
line = """
while True:
\tif True:
\t\t1
""".lstrip()
fixed = """
while True:
if True:
1
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e201(self):
line = '( 1)\n'
fixed = '(1)\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e202(self):
line = '(1 )\n[2 ]\n{3 }\n'
fixed = '(1)\n[2]\n{3}\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e202_skip_multiline(self):
"""We skip this since pep8 reports the error as being on line 1."""
line = """
('''
a
b
c
''' )
"""
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e202_skip_multiline_with_escaped_newline(self):
"""We skip this since pep8 reports the error as being on line 1."""
line = r"""
('c\
' )
"""
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e203_colon(self):
line = '{4 : 3}\n'
fixed = '{4: 3}\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e203_comma(self):
line = '[1 , 2 , 3]\n'
fixed = '[1, 2, 3]\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e203_semicolon(self):
line = "print(a, end=' ') ; nl = 0\n"
fixed = "print(a, end=' '); nl = 0\n"
with autopep8_context(line, options=['--select=E203']) as result:
self.assertEqual(fixed, result)
def test_e203_with_newline(self):
line = "print(a\n, end=' ')\n"
fixed = "print(a, end=' ')\n"
with autopep8_context(line, options=['--select=E203']) as result:
self.assertEqual(fixed, result)
def test_e211(self):
line = 'd = [1, 2, 3]\nprint d [0]\n'
fixed = 'd = [1, 2, 3]\nprint d[0]\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e221(self):
line = 'a = 1 + 1\n'
fixed = 'a = 1 + 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e221_should_skip_multiline(self):
line = '''
def javascript(self):
return u"""
""" % {
}
'''.lstrip()
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e222(self):
line = 'a = 1 + 1\n'
fixed = 'a = 1 + 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e223(self):
line = 'a = 1 + 1\n' # include TAB
fixed = 'a = 1 + 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e223_double(self):
line = 'a = 1 + 1\n' # include TAB
fixed = 'a = 1 + 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e223_with_tab_indentation(self):
line = """
class Foo():
\tdef __init__(self):
\t\tx= 1\t+ 3
""".lstrip()
fixed = """
class Foo():
\tdef __init__(self):
\t\tx = 1 + 3
""".lstrip()
with autopep8_context(line, options=['--ignore=W191']) as result:
self.assertEqual(fixed, result)
def test_e224(self):
line = 'a = 11 + 1\n' # include TAB
fixed = 'a = 11 + 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e224_double(self):
line = 'a = 11 + 1\n' # include TAB
fixed = 'a = 11 + 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e224_with_tab_indentation(self):
line = """
class Foo():
\tdef __init__(self):
\t\tx= \t3
""".lstrip()
fixed = """
class Foo():
\tdef __init__(self):
\t\tx = 3
""".lstrip()
with autopep8_context(line, options=['--ignore=W191']) as result:
self.assertEqual(fixed, result)
def test_e225(self):
line = '1+1\n2 +2\n3+ 3\n'
fixed = '1 + 1\n2 + 2\n3 + 3\n'
with autopep8_context(line, options=['--select=E,W']) as result:
self.assertEqual(fixed, result)
def test_e225_with_indentation_fix(self):
line = """
class Foo(object):
def bar(self):
return self.elephant is not None
""".lstrip()
fixed = """
class Foo(object):
def bar(self):
return self.elephant is not None
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e226(self):
line = '1*1\n2*2\n3*3\n'
fixed = '1 * 1\n2 * 2\n3 * 3\n'
with autopep8_context(line, options=['--select=E22']) as result:
self.assertEqual(fixed, result)
def test_e227(self):
line = '1&1\n2&2\n3&3\n'
fixed = '1 & 1\n2 & 2\n3 & 3\n'
with autopep8_context(line, options=['--select=E22']) as result:
self.assertEqual(fixed, result)
def test_e228(self):
line = '1%1\n2%2\n3%3\n'
fixed = '1 % 1\n2 % 2\n3 % 3\n'
with autopep8_context(line, options=['--select=E22']) as result:
self.assertEqual(fixed, result)
def test_e231(self):
line = '[1,2,3]\n'
fixed = '[1, 2, 3]\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e231_with_many_commas(self):
fixed = str(list(range(200))) + '\n'
import re
line = re.sub(', ', ',', fixed)
with autopep8_context(line, options=['--select=E231']) as result:
self.assertEqual(fixed, result)
def test_e231_with_colon_after_comma(self):
"""ws_comma fixer ignores this case."""
line = 'a[b1,:]\n'
fixed = 'a[b1, :]\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e241(self):
line = 'l = (1, 2)\n'
fixed = 'l = (1, 2)\n'
with autopep8_context(line, options=['--select=E']) as result:
self.assertEqual(fixed, result)
def test_e241_should_be_enabled_by_aggressive(self):
line = 'l = (1, 2)\n'
fixed = 'l = (1, 2)\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e241_double(self):
line = 'l = (1, 2)\n'
fixed = 'l = (1, 2)\n'
with autopep8_context(line, options=['--select=E']) as result:
self.assertEqual(fixed, result)
def test_e242(self):
line = 'l = (1,\t2)\n'
fixed = 'l = (1, 2)\n'
with autopep8_context(line, options=['--select=E']) as result:
self.assertEqual(fixed, result)
def test_e242_double(self):
line = 'l = (1,\t\t2)\n'
fixed = 'l = (1, 2)\n'
with autopep8_context(line, options=['--select=E']) as result:
self.assertEqual(fixed, result)
def test_e251(self):
line = 'def a(arg = 1):\n print arg\n'
fixed = 'def a(arg=1):\n print arg\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e251_with_escaped_newline(self):
line = '1\n\n\ndef a(arg=\\\n1):\n print(arg)\n'
fixed = '1\n\n\ndef a(arg=1):\n print(arg)\n'
with autopep8_context(line, options=['--select=E251']) as result:
self.assertEqual(fixed, result)
def test_e251_with_calling(self):
line = 'foo(bar= True)\n'
fixed = 'foo(bar=True)\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e251_with_argument_on_next_line(self):
line = 'foo(bar\n=None)\n'
fixed = 'foo(bar=None)\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e261(self):
line = "print 'a b '# comment\n"
fixed = "print 'a b ' # comment\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e261_with_dictionary(self):
line = 'd = {# comment\n1: 2}\n'
fixed = 'd = { # comment\n 1: 2}\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e261_with_dictionary_no_space(self):
line = 'd = {#comment\n1: 2}\n'
fixed = 'd = { # comment\n 1: 2}\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e261_with_comma(self):
line = '{1: 2 # comment\n , }\n'
fixed = '{1: 2 # comment\n , }\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e262_more_space(self):
line = "print 'a b ' # comment\n"
fixed = "print 'a b ' # comment\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e262_none_space(self):
line = "print 'a b ' #comment\n"
fixed = "print 'a b ' # comment\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e262_hash_in_string(self):
line = "print 'a b #string' #comment\n"
fixed = "print 'a b #string' # comment\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e262_hash_in_string_and_multiple_hashes(self):
line = "print 'a b #string' #comment #comment\n"
fixed = "print 'a b #string' # comment #comment\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e262_more_complex(self):
line = "print 'a b ' #comment\n123\n"
fixed = "print 'a b ' # comment\n123\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e271(self):
line = 'True and False\n'
fixed = 'True and False\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e272(self):
line = 'True and False\n'
fixed = 'True and False\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e273(self):
line = 'True and\tFalse\n'
fixed = 'True and False\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e274(self):
line = 'True\tand False\n'
fixed = 'True and False\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e301(self):
line = 'class k:\n s = 0\n def f():\n print 1\n'
fixed = 'class k:\n s = 0\n\n def f():\n print 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e301_extended(self):
line = 'class Foo:\n def bar():\n print 1\n'
fixed = 'class Foo:\n\n def bar():\n print 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e301_extended_with_docstring(self):
line = '''\
class Foo(object):
"""Test."""
def foo(self):
"""Test."""
def bar():
pass
'''
fixed = '''\
class Foo(object):
"""Test."""
def foo(self):
"""Test."""
def bar():
pass
'''
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e302(self):
line = 'def f():\n print 1\n\ndef ff():\n print 2\n'
fixed = 'def f():\n print 1\n\n\ndef ff():\n print 2\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e303(self):
line = '\n\n\n# alpha\n\n1\n'
fixed = '\n\n# alpha\n1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e303_extended(self):
line = '''\
def foo():
"""Document."""
'''
fixed = '''\
def foo():
"""Document."""
'''
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e304(self):
line = '@contextmanager\n\ndef f():\n print 1\n'
fixed = '@contextmanager\ndef f():\n print 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e304_with_comment(self):
line = '@contextmanager\n# comment\n\ndef f():\n print 1\n'
fixed = '@contextmanager\n# comment\ndef f():\n print 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e401(self):
line = 'import os, sys\n'
fixed = 'import os\nimport sys\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e401_with_indentation(self):
line = 'def a():\n import os, sys\n'
fixed = 'def a():\n import os\n import sys\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e401_should_ignore_commented_comma(self):
line = 'import bdist_egg, egg # , not a module, neither is this\n'
fixed = 'import bdist_egg\nimport egg # , not a module, neither is this\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e401_should_ignore_commented_comma_with_indentation(self):
line = 'if True:\n import bdist_egg, egg # , not a module, neither is this\n'
fixed = 'if True:\n import bdist_egg\n import egg # , not a module, neither is this\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e401_should_ignore_false_positive(self):
line = 'import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe)\n'
with autopep8_context(line, options=['--select=E401']) as result:
self.assertEqual(line, result)
def test_e401_with_escaped_newline_case(self):
line = 'import foo, \\\n bar\n'
fixed = 'import foo\nimport \\\n bar\n'
with autopep8_context(line, options=['--select=E401']) as result:
self.assertEqual(fixed, result)
def test_e501_basic(self):
line = """
print(111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333, 333, 333)
"""
fixed = """
print(111, 111, 111, 111, 222, 222, 222, 222,
222, 222, 222, 222, 222, 333, 333, 333, 333)
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_basic_should_prefer_balanced_brackets(self):
line = """\
if True:
reconstructed = iradon(radon(image), filter="ramp", interpolation="nearest")
"""
fixed = """\
if True:
reconstructed = iradon(
radon(image), filter="ramp", interpolation="nearest")
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_with_very_long_line(self):
line = """\
x = [3244234243234, 234234234324, 234234324, 23424234, 234234234, 234234, 234243, 234243, 234234234324, 234234324, 23424234, 234234234, 234234, 234243, 234243]
"""
fixed = """\
x = [
3244234243234,
234234234324,
234234324,
23424234,
234234234,
234234,
234243,
234243,
234234234324,
234234324,
23424234,
234234234,
234234,
234243,
234243]
"""
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e501_shorten_at_commas_skip(self):
line = """\
parser.add_argument('source_corpus', help='corpus name/path relative to an nltk_data directory')
parser.add_argument('target_corpus', help='corpus name/path relative to an nltk_data directory')
"""
fixed = """\
parser.add_argument(
'source_corpus',
help='corpus name/path relative to an nltk_data directory')
parser.add_argument(
'target_corpus',
help='corpus name/path relative to an nltk_data directory')
"""
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e501_with_shorter_length(self):
line = "foooooooooooooooooo('abcdefghijklmnopqrstuvwxyz')\n"
fixed = "foooooooooooooooooo(\n 'abcdefghijklmnopqrstuvwxyz')\n"
with autopep8_context(line, options=['--max-line-length=40']) as result:
self.assertEqual(fixed, result)
def test_e501_with_indent(self):
line = """
def d():
print(111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333, 333, 333)
"""
fixed = """
def d():
print(111, 111, 111, 111, 222, 222, 222, 222,
222, 222, 222, 222, 222, 333, 333, 333, 333)
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_alone_with_indentation(self):
line = """
if True:
print(111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333, 333, 333)
"""
fixed = """
if True:
print(111, 111, 111, 111, 222, 222, 222, 222,
222, 222, 222, 222, 222, 333, 333, 333, 333)
"""
with autopep8_context(line, options=['--select=E501']) as result:
self.assertEqual(fixed, result)
def test_e501_alone_with_tuple(self):
line = """
fooooooooooooooooooooooooooooooo000000000000000000000000 = [1,
('TransferTime', 'FLOAT')
]
"""
fixed = """
fooooooooooooooooooooooooooooooo000000000000000000000000 = [1,
('TransferTime',
'FLOAT')
]
"""
with autopep8_context(line, options=['--select=E501']) as result:
self.assertEqual(fixed, result)
def test_e501_arithmetic_operator_with_indent(self):
line = """
def d():
111 + 111 + 111 + 111 + 111 + 222 + 222 + 222 + 222 + 222 + 222 + 222 + 222 + 222 + 333 + 333 + 333 + 333
"""
fixed = r"""
def d():
111 + 111 + 111 + 111 + 111 + 222 + 222 + 222 + 222 + \
222 + 222 + 222 + 222 + 222 + 333 + 333 + 333 + 333
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_more_complicated(self):
line = """
blahblah = os.environ.get('blahblah') or os.environ.get('blahblahblah') or os.environ.get('blahblahblahblah')
"""
fixed = """
blahblah = os.environ.get('blahblah') or os.environ.get(
'blahblahblah') or os.environ.get('blahblahblahblah')
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_skip_even_more_complicated(self):
line = """
if True:
if True:
if True:
blah = blah.blah_blah_blah_bla_bl(blahb.blah, blah.blah,
blah=blah.label, blah_blah=blah_blah,
blah_blah2=blah_blah)
"""
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e501_prefer_to_break_at_begnning(self):
"""We prefer not to leave part of the arguments hanging."""
line = """
looooooooooooooong = foo(one, two, three, four, five, six, seven, eight, nine, ten)
"""
fixed = """
looooooooooooooong = foo(
one, two, three, four, five, six, seven, eight, nine, ten)
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_avoid_breaking_at_empty_parentheses_if_possible(self):
line = """\
someverylongindenttionwhatnot().foo().bar().baz("and here is a long string 123456789012345678901234567890")
"""
fixed = """\
someverylongindenttionwhatnot().foo().bar().baz(
"and here is a long string 123456789012345678901234567890")
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_with_multiple_lines(self):
line = """
foo_bar_zap_bing_bang_boom(111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333,
111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333)
"""
fixed = """
foo_bar_zap_bing_bang_boom(
111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333,
111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333)
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_with_multiple_lines_and_quotes(self):
line = """
if True:
xxxxxxxxxxx = xxxxxxxxxxxxxxxxx(xxxxxxxxxxx, xxxxxxxxxxxxxxxx={'xxxxxxxxxxxx': 'xxxxx',
'xxxxxxxxxxx': xx,
'xxxxxxxx': False,
})
"""
fixed = """
if True:
xxxxxxxxxxx = xxxxxxxxxxxxxxxxx(
xxxxxxxxxxx, xxxxxxxxxxxxxxxx={'xxxxxxxxxxxx': 'xxxxx',
'xxxxxxxxxxx': xx,
'xxxxxxxx': False,
})
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_do_not_break_on_keyword(self):
# We don't want to put a newline after equals for keywords as this
# violates PEP 8.
line = """
if True:
long_variable_name = tempfile.mkstemp(prefix='abcdefghijklmnopqrstuvwxyz0123456789')
"""
fixed = """
if True:
long_variable_name = tempfile.mkstemp(
prefix='abcdefghijklmnopqrstuvwxyz0123456789')
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_do_not_begin_line_with_comma(self):
self.maxDiff = None
# This fix is incomplete. (The line is still too long.) But it is here
# just to confirm that we do not put a comma at the beginning of a
# line.
line = """
def dummy():
if True:
if True:
if True:
object = ModifyAction( [MODIFY70.text, OBJECTBINDING71.text, COLON72.text], MODIFY70.getLine(), MODIFY70.getCharPositionInLine() )
"""
fixed = """
def dummy():
if True:
if True:
if True:
object = ModifyAction(
[MODIFY70.text, OBJECTBINDING71.text, COLON72.text], MODIFY70.getLine(), MODIFY70.getCharPositionInLine())
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_should_not_break_on_dot(self):
line = """
if True:
if True:
raise xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx('xxxxxxxxxxxxxxxxx "{d}" xxxxxxxxxxxxxx'.format(d='xxxxxxxxxxxxxxx'))
""".lstrip()
fixed = """
if True:
if True:
raise xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
'xxxxxxxxxxxxxxxxx "{d}" xxxxxxxxxxxxxx'.format(d='xxxxxxxxxxxxxxx'))
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_with_comment(self):
line = """123
# This is a long comment that should be wrapped. I will wrap it using textwrap to be within 72 characters.
# http://foo.bar/abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-
"""
fixed = """123
# This is a long comment that should be wrapped. I will
# wrap it using textwrap to be within 72 characters.
# http://foo.bar/abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-abc-
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_with_comment_should_not_modify_docstring(self):
line = '''
def foo():
"""
# This is a long comment that should be wrapped. I will wrap it using textwrap to be within 72 characters.
"""
'''.lstrip()
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e501_should_only_modify_last_comment(self):
line = """123
# This is a long comment that should be wrapped. I will wrap it using textwrap to be within 72 characters.
# 1. This is a long comment that should be wrapped. I will wrap it using textwrap to be within 72 characters.
# 2. This is a long comment that should be wrapped. I will wrap it using textwrap to be within 72 characters.
# 3. This is a long comment that should be wrapped. I will wrap it using textwrap to be within 72 characters.
"""
fixed = """123
# This is a long comment that should be wrapped. I will wrap it using textwrap to be within 72 characters.
# 1. This is a long comment that should be wrapped. I will wrap it using textwrap to be within 72 characters.
# 2. This is a long comment that should be wrapped. I will wrap it using textwrap to be within 72 characters.
# 3. This is a long comment that should be wrapped. I
# will wrap it using textwrap to be within 72
# characters.
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_should_not_interfere_with_non_comment(self):
line = '''
"""
# not actually a comment %d. 12345678901234567890, 12345678901234567890, 12345678901234567890.
""" % (0,)
'''
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e501_should_cut_comment_pattern(self):
line = """123
# -- Useless lines ----------------------------------------------------------------------
321
"""
fixed = """123
# -- Useless lines -------------------------------------------------------
321
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e501_with_function_should_not_break_on_colon(self):
line = r"""
class Useless(object):
def _table_field_is_plain_widget(self, widget):
if widget.__class__ == Widget or\
(widget.__class__ == WidgetMeta and Widget in widget.__bases__):
return True
return False
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e501_with_aggressive(self):
self.maxDiff = None
line = """\
models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
}
"""
fixed = """\
models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {
'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')",
'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
}
"""
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e501_with_multiple_keys_and_aggressive(self):
line = """\
one_two_three_four_five_six = {'one two three four five': 12345, 'asdfsdflsdkfjl sdflkjsdkfkjsfjsdlkfj sdlkfjlsfjs': '343',
1: 1}
"""
fixed = """\
one_two_three_four_five_six = {
'one two three four five': 12345,
'asdfsdflsdkfjl sdflkjsdkfkjsfjsdlkfj sdlkfjlsfjs': '343',
1: 1}
"""
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e501_should_ignore_imports(self):
line = """\
import logging, os, bleach, commonware, urllib2, json, time, requests, urlparse, re
"""
with autopep8_context(line, options=['--select=E501']) as result:
self.assertEqual(line, result)
def test_e501_should_not_do_useless_things(self):
line = """\
foo(' ')
"""
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e501_aggressive_with_percent(self):
line = """\
raise MultiProjectException("Ambiguous workspace: %s=%s, %s" % ( varname, varname_path, os.path.abspath(config_filename)))
"""
fixed = """\
raise MultiProjectException(
"Ambiguous workspace: %s=%s, %s" %
(varname, varname_path, os.path.abspath(config_filename)))
"""
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e501_aggressive_with_def(self):
line = """\
def foo(sldfkjlsdfsdf, kksdfsdfsf,sdfsdfsdf, sdfsdfkdk, szdfsdfsdf, sdfsdfsdfsdlkfjsdlf, sdfsdfddf,sdfsdfsfd, sdfsdfdsf):
pass
"""
fixed = """\
def foo(sldfkjlsdfsdf, kksdfsdfsf, sdfsdfsdf, sdfsdfkdk,
szdfsdfsdf, sdfsdfsdfsdlkfjsdlf, sdfsdfddf, sdfsdfsfd, sdfsdfdsf):
pass
"""
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e501_more_aggressive_with_def(self):
line = """\
def foo(sldfkjlsdfsdf, kksdfsdfsf,sdfsdfsdf, sdfsdfkdk, szdfsdfsdf, sdfsdfsdfsdlkfjsdlf, sdfsdfddf,sdfsdfsfd, sdfsdfdsf):
pass
"""
fixed = """\
def foo(
sldfkjlsdfsdf,
kksdfsdfsf,
sdfsdfsdf,
sdfsdfkdk,
szdfsdfsdf,
sdfsdfsdfsdlkfjsdlf,
sdfsdfddf,
sdfsdfsfd,
sdfsdfdsf):
pass
"""
with autopep8_context(line, options=['--aggressive', '--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e502(self):
line = "print('abc'\\\n 'def')\n"
fixed = "print('abc'\n 'def')\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e701(self):
line = 'if True: print True\n'
fixed = 'if True:\n print True\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e701_with_escaped_newline(self):
line = 'if True:\\\nprint True\n'
fixed = 'if True:\n print True\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e701_with_escaped_newline_and_spaces(self):
line = 'if True: \\ \nprint True\n'
fixed = 'if True:\n print True\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702(self):
line = 'print 1; print 2\n'
fixed = 'print 1\nprint 2\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_with_semicolon_at_end(self):
line = 'print 1;\n'
fixed = 'print 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_with_semicolon_and_space_at_end(self):
line = 'print 1; \n'
fixed = 'print 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_with_whitespace(self):
line = 'print 1 ; print 2\n'
fixed = 'print 1\nprint 2\n'
with autopep8_context(line, options=['--select=E702']) as result:
self.assertEqual(fixed, result)
def test_e702_with_non_ascii_file(self):
line = """
# -*- coding: utf-8 -*-
# French comment with accent é
# Un commentaire en français avec un accent é
import time
time.strftime('%d-%m-%Y');
""".lstrip()
fixed = """
# -*- coding: utf-8 -*-
# French comment with accent é
# Un commentaire en français avec un accent é
import time
time.strftime('%d-%m-%Y')
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_with_escaped_newline(self):
line = '1; \\\n2\n'
fixed = '1\n2\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_with_escaped_newline_with_indentation(self):
line = '1; \\\n 2\n'
fixed = '1\n2\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_more_complicated(self):
line = """\
def foo():
if bar : bar+=1; bar=bar*bar ; return bar
"""
fixed = """\
def foo():
if bar:
bar += 1
bar = bar * bar
return bar
"""
with autopep8_context(line, options=['--select=E,W']) as result:
self.assertEqual(fixed, result)
def test_e702_with_semicolon_in_string(self):
line = 'print(";");\n'
fixed = 'print(";")\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_with_semicolon_in_string_to_the_right(self):
line = 'x = "x"; y = "y;y"\n'
fixed = 'x = "x"\ny = "y;y"\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_indent_correctly(self):
line = """
(
1,
2,
3); 4; 5; 5 # pyflakes
"""
fixed = """
(
1,
2,
3)
4
5
5 # pyflakes
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_with_triple_quote(self):
line = '"""\n hello\n """; 1\n'
fixed = '"""\n hello\n """\n1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_with_triple_quote_and_indent(self):
line = ' """\n hello\n """; 1\n'
fixed = ' """\n hello\n """\n 1\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e702_with_semicolon_after_string(self):
line = """
raise IOError('abc '
'def.');
""".lstrip()
fixed = """
raise IOError('abc '
'def.')
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_e711(self):
line = 'foo == None\n'
fixed = 'foo is None\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e711_in_conditional(self):
line = 'if foo == None and None == foo:\npass\n'
fixed = 'if foo is None and None == foo:\npass\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e711_in_conditional_with_multiple_instances(self):
line = 'if foo == None and bar == None:\npass\n'
fixed = 'if foo is None and bar is None:\npass\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e711_with_not_equals_none(self):
line = 'foo != None\n'
fixed = 'foo is not None\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e712(self):
line = 'foo == True\n'
fixed = 'foo\n'
with autopep8_context(line, options=['--aggressive', '--select=E712']) as result:
self.assertEqual(fixed, result)
def test_e712_in_conditional_with_multiple_instances(self):
line = 'if foo == True and bar == True:\npass\n'
fixed = 'if foo and bar:\npass\n'
with autopep8_context(line, options=['--aggressive', '--select=E712']) as result:
self.assertEqual(fixed, result)
def test_e712_with_false(self):
line = 'foo != False\n'
fixed = 'foo\n'
with autopep8_context(line, options=['--aggressive', '--select=E712']) as result:
self.assertEqual(fixed, result)
def test_e712_only_if_aggressive(self):
line = 'foo == True\n'
with autopep8_context(line) as result:
self.assertEqual(line, result)
def test_e711_and_e712(self):
line = 'if (foo == None and bar == True) or (foo != False and bar != None):\npass\n'
fixed = 'if (foo is None and bar) or (foo and bar is not None):\npass\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e721(self):
line = "type('') == type('')\n"
fixed = "isinstance('', type(''))\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e721_with_str(self):
line = "str == type('')\n"
fixed = "isinstance('', str)\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_e721_in_conditional(self):
line = "if str == type(''):\n pass\n"
fixed = "if isinstance('', str):\n pass\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_should_preserve_vertical_tab(self):
line = """
#Memory Bu\vffer Register:
""".lstrip()
fixed = """
# Memory Bu\vffer Register:
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_w191_should_ignore_multiline_strings(self):
line = """
print(3 <> 4, '''
while True:
if True:
\t1
\t''', 4 <> 5)
if True:
\t123
""".lstrip()
fixed = """
print(3 != 4, '''
while True:
if True:
\t1
\t''', 4 != 5)
if True:
123
""".lstrip()
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w191_should_ignore_tabs_in_strings(self):
line = """
if True:
\tx = '''
\t\tblah
\tif True:
\t1
\t'''
if True:
\t123
else:
\t32
""".lstrip()
fixed = """
if True:
x = '''
\t\tblah
\tif True:
\t1
\t'''
if True:
123
else:
32
""".lstrip()
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_w291(self):
line = "print 'a b '\t \n"
fixed = "print 'a b '\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_w291_with_comment(self):
line = "print 'a b ' # comment\t \n"
fixed = "print 'a b ' # comment\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_w292(self):
line = '1\n2'
fixed = '1\n2\n'
with autopep8_context(line, options=['--select=W292']) as result:
self.assertEqual(fixed, result)
def test_w293(self):
line = '1\n \n2\n'
fixed = '1\n\n2\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_w391(self):
line = ' \n'
fixed = ''
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_w391_more_complex(self):
line = '123\n456\n \n'
fixed = '123\n456\n'
with autopep8_context(line) as result:
self.assertEqual(fixed, result)
def test_w601(self):
line = 'a = {0: 1}\na.has_key(0)\n'
fixed = 'a = {0: 1}\n0 in a\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w601_word(self):
line = 'my_dict = {0: 1}\nmy_dict.has_key(0)\n'
fixed = 'my_dict = {0: 1}\n0 in my_dict\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w601_conditional(self):
line = 'a = {0: 1}\nif a.has_key(0):\n print 1\n'
fixed = 'a = {0: 1}\nif 0 in a:\n print 1\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w601_self(self):
line = 'self.a.has_key(0)\n'
fixed = '0 in self.a\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w601_self_with_conditional(self):
line = 'if self.a.has_key(0):\n print 1\n'
fixed = 'if 0 in self.a:\n print 1\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w601_with_multiple(self):
line = 'a.has_key(0) and b.has_key(0)\n'
fixed = '0 in a and 0 in b\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w601_with_multiple_nested(self):
line = 'alpha.has_key(nested.has_key(12)) and beta.has_key(1)\n'
fixed = '(12 in nested) in alpha and 1 in beta\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w601_with_more_complexity(self):
line = 'y.has_key(0) + x.has_key(x.has_key(0) + x.has_key(x.has_key(0) + x.has_key(1)))\n'
fixed = '(0 in y) + ((0 in x) + ((0 in x) + (1 in x) in x) in x)\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w601_precedence(self):
line = 'if self.a.has_key(1 + 2):\n print 1\n'
fixed = 'if 1 + 2 in self.a:\n print 1\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w601_with_parens(self):
line = 'foo(12) in alpha\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(line, result)
def test_w601_with_multiline(self):
line = """
a.has_key(
0
)
""".lstrip()
fixed = '0 in a\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
@unittest.skipIf(sys.version_info < (2, 6, 4),
'older versions of 2.6 may be buggy')
def test_w601_with_non_ascii(self):
line = """
# -*- coding: utf-8 -*-
## éはe
correct = dict().has_key('good syntax ?')
""".lstrip()
fixed = """
# -*- coding: utf-8 -*-
# éはe
correct = 'good syntax ?' in dict()
""".lstrip()
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_arg_is_string(self):
line = "raise ValueError, \"w602 test\"\n"
fixed = "raise ValueError(\"w602 test\")\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_arg_is_string_with_comment(self):
line = "raise ValueError, \"w602 test\" # comment\n"
fixed = "raise ValueError(\"w602 test\") # comment\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_skip_ambiguous_case(self):
line = "raise 'a', 'b', 'c'\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(line, result)
def test_w602_with_logic(self):
line = "raise TypeError, e or 'hello'\n"
fixed = "raise TypeError(e or 'hello')\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_triple_quotes(self):
line = 'raise ValueError, """hello"""\n1\n'
fixed = 'raise ValueError("""hello""")\n1\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_multiline(self):
line = 'raise ValueError, """\nhello"""\n'
fixed = 'raise ValueError("""\nhello""")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_with_complex_multiline(self):
line = 'raise ValueError, """\nhello %s %s""" % (\n 1, 2)\n'
fixed = 'raise ValueError("""\nhello %s %s""" % (\n 1, 2))\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_multiline_with_trailing_spaces(self):
line = 'raise ValueError, """\nhello""" \n'
fixed = 'raise ValueError("""\nhello""")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_multiline_with_escaped_newline(self):
line = 'raise ValueError, \\\n"""\nhello"""\n'
fixed = 'raise ValueError("""\nhello""")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_multiline_with_escaped_newline_and_comment(self):
line = 'raise ValueError, \\\n"""\nhello""" # comment\n'
fixed = 'raise ValueError("""\nhello""") # comment\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_multiline_with_multiple_escaped_newlines(self):
line = 'raise ValueError, \\\n\\\n\\\n"""\nhello"""\n'
fixed = 'raise ValueError("""\nhello""")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_multiline_with_nested_quotes(self):
line = 'raise ValueError, """hello\'\'\'blah"a"b"c"""\n'
fixed = 'raise ValueError("""hello\'\'\'blah"a"b"c""")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_with_multiline_with_single_quotes(self):
line = "raise ValueError, '''\nhello'''\n"
fixed = "raise ValueError('''\nhello''')\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_multiline_string_stays_the_same(self):
line = 'raise """\nhello"""\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(line, result)
def test_w602_escaped_lf(self):
line = 'raise ValueError, \\\n"hello"\n'
fixed = 'raise ValueError("hello")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_escaped_crlf(self):
line = 'raise ValueError, \\\r\n"hello"\n'
fixed = 'raise ValueError("hello")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_indentation(self):
line = 'def foo():\n raise ValueError, "hello"\n'
fixed = 'def foo():\n raise ValueError("hello")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_escaped_cr(self):
line = 'raise ValueError, \\\r"hello"\n'
fixed = 'raise ValueError("hello")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_multiple_statements(self):
line = 'raise ValueError, "hello";print 1\n'
fixed = 'raise ValueError("hello")\nprint 1\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_raise_argument_with_indentation(self):
line = 'if True:\n raise ValueError, "error"\n'
fixed = 'if True:\n raise ValueError("error")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_skip_raise_argument_triple(self):
line = 'raise ValueError, "info", traceback\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(line, result)
def test_w602_skip_raise_argument_triple_with_comment(self):
line = 'raise ValueError, "info", traceback # comment\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(line, result)
def test_w602_raise_argument_triple_fake(self):
line = 'raise ValueError, "info, info2"\n'
fixed = 'raise ValueError("info, info2")\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_with_list_comprehension(self):
line = 'raise Error, [x[0] for x in probs]\n'
fixed = 'raise Error([x[0] for x in probs])\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w602_with_bad_syntax(self):
line = "raise Error, 'abc\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(line, result)
def test_w603(self):
line = 'if 2 <> 2:\n print False'
fixed = 'if 2 != 2:\n print False\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w604(self):
line = '`1`\n'
fixed = 'repr(1)\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w604_with_multiple_instances(self):
line = '``1`` + ``b``\n'
fixed = 'repr(repr(1)) + repr(repr(b))\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
def test_w604_with_multiple_lines(self):
line = '`(1\n )`\n'
fixed = 'repr((1\n ))\n'
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)
class CommandLineTests(unittest.TestCase):
def test_diff(self):
line = "'abc' \n"
fixed = "-'abc' \n+'abc'\n"
with autopep8_subprocess(line, ['--diff']) as result:
self.assertEqual(fixed, '\n'.join(result.split('\n')[3:]))
def test_diff_with_empty_file(self):
with autopep8_subprocess('', ['--diff']) as result:
self.assertEqual('\n'.join(result.split('\n')[3:]), '')
def test_diff_with_nonexistent_file(self):
p = Popen(list(AUTOPEP8_CMD_TUPLE) + ['--diff', 'non_existent_file'],
stdout=PIPE, stderr=PIPE)
error = p.communicate()[1].decode('utf-8')
self.assertIn('non_existent_file', error)
def test_pep8_passes(self):
line = "'abc' \n"
fixed = "'abc'\n"
with autopep8_subprocess(line, ['--pep8-passes', '0']) as result:
self.assertEqual(fixed, result)
def test_pep8_ignore(self):
line = "'abc' \n"
with autopep8_subprocess(line, ['--ignore=E,W']) as result:
self.assertEqual(line, result)
def test_help(self):
p = Popen(list(AUTOPEP8_CMD_TUPLE) + ['-h'],
stdout=PIPE)
self.assertIn('Usage:', p.communicate()[0].decode('utf-8'))
def test_verbose(self):
line = 'bad_syntax)'
with temporary_file_context(line) as filename:
p = Popen(list(AUTOPEP8_CMD_TUPLE) + [filename, '-vvv'],
stdout=PIPE, stderr=PIPE)
verbose_error = p.communicate()[1].decode('utf-8')
self.assertIn("'fix_e901' is not defined", verbose_error)
def test_verbose_diff(self):
line = '+'.join(100 * ['323424234234'])
with temporary_file_context(line) as filename:
p = Popen(list(AUTOPEP8_CMD_TUPLE) +
[filename, '-vvvv', '--diff'],
stdout=PIPE, stderr=PIPE)
verbose_error = p.communicate()[1].decode('utf-8')
self.assertIn('------------', verbose_error)
def test_in_place(self):
line = "'abc' \n"
fixed = "'abc'\n"
with temporary_file_context(line) as filename:
p = Popen(list(AUTOPEP8_CMD_TUPLE) + [filename, '--in-place'])
p.wait()
with open(filename) as f:
self.assertEqual(fixed, f.read())
def test_parallel_jobs(self):
line = "'abc' \n"
fixed = "'abc'\n"
with temporary_file_context(line) as filename_a:
with temporary_file_context(line) as filename_b:
p = Popen(list(AUTOPEP8_CMD_TUPLE) +
[filename_a, filename_b, '--jobs=3', '--in-place'])
p.wait()
with open(filename_a) as f:
self.assertEqual(fixed, f.read())
with open(filename_b) as f:
self.assertEqual(fixed, f.read())
def test_parallel_jobs_with_automatic_cpu_count(self):
line = "'abc' \n"
fixed = "'abc'\n"
with temporary_file_context(line) as filename_a:
with temporary_file_context(line) as filename_b:
p = Popen(list(AUTOPEP8_CMD_TUPLE) +
[filename_a, filename_b, '--jobs=0', '--in-place'])
p.wait()
with open(filename_a) as f:
self.assertEqual(fixed, f.read())
with open(filename_b) as f:
self.assertEqual(fixed, f.read())
def test_in_place_with_empty_file(self):
line = ''
with temporary_file_context(line) as filename:
p = Popen(list(AUTOPEP8_CMD_TUPLE) + [filename, '--in-place'])
p.wait()
self.assertEqual(0, p.returncode)
with open(filename) as f:
self.assertEqual(f.read(), line)
def test_in_place_and_diff(self):
line = "'abc' \n"
with temporary_file_context(line) as filename:
p = Popen(
list(AUTOPEP8_CMD_TUPLE) + [filename,
'--in-place', '--diff'],
stderr=PIPE)
result = p.communicate()[1].decode('utf-8')
self.assertIn('--in-place and --diff are mutually exclusive', result)
def test_recursive(self):
import tempfile
temp_directory = tempfile.mkdtemp(dir='.')
try:
with open(os.path.join(temp_directory, 'a.py'), 'w') as output:
output.write("'abc' \n")
os.mkdir(os.path.join(temp_directory, 'd'))
with open(os.path.join(temp_directory, 'd', 'b.py'),
'w') as output:
output.write('123 \n')
p = Popen(list(AUTOPEP8_CMD_TUPLE) +
[temp_directory, '--recursive', '--diff'],
stdout=PIPE)
result = p.communicate()[0].decode('utf-8')
self.assertEqual(
"-'abc' \n+'abc'",
'\n'.join(result.split('\n')[3:5]))
self.assertEqual(
'-123 \n+123',
'\n'.join(result.split('\n')[8:10]))
finally:
import shutil
shutil.rmtree(temp_directory)
def test_recursive_should_ignore_hidden(self):
import tempfile
temp_directory = tempfile.mkdtemp(dir='.')
temp_subdirectory = tempfile.mkdtemp(prefix='.', dir=temp_directory)
try:
with open(os.path.join(temp_subdirectory, 'a.py'), 'w') as output:
output.write("'abc' \n")
p = Popen(list(AUTOPEP8_CMD_TUPLE) +
[temp_directory, '--recursive', '--diff'],
stdout=PIPE)
result = p.communicate()[0].decode('utf-8')
self.assertEqual(0, p.returncode)
self.assertEqual('', result)
finally:
import shutil
shutil.rmtree(temp_directory)
def test_exclude(self):
import tempfile
temp_directory = tempfile.mkdtemp(dir='.')
try:
with open(os.path.join(temp_directory, 'a.py'), 'w') as output:
output.write("'abc' \n")
os.mkdir(os.path.join(temp_directory, 'd'))
with open(os.path.join(temp_directory, 'd', 'b.py'),
'w') as output:
output.write('123 \n')
p = Popen(list(AUTOPEP8_CMD_TUPLE) +
[temp_directory, '--recursive', '--exclude=a*',
'--diff'],
stdout=PIPE)
result = p.communicate()[0].decode('utf-8')
self.assertNotIn('abc', result)
self.assertIn('123', result)
finally:
import shutil
shutil.rmtree(temp_directory)
def test_invalid_option_combinations(self):
line = "'abc' \n"
with temporary_file_context(line) as filename:
for options in [['--recursive', filename], # without --diff
['--jobs=0', filename],
['--exclude=foo', filename], # without --recursive
['--max-line-length=0', filename],
[], # no argument
['-', '--in-place'],
['-', '--recursive'],
['-', filename],
]:
p = Popen(list(AUTOPEP8_CMD_TUPLE) + options,
stderr=PIPE)
result = p.communicate()[1].decode('utf-8')
self.assertNotEqual(0, p.returncode, msg=str(options))
self.assertTrue(len(result))
def test_list_fixes(self):
with autopep8_subprocess('', options=['--list-fixes']) as result:
self.assertIn('E101', result)
def test_fixpep8_class_constructor(self):
line = 'print 1\nprint 2\n'
with temporary_file_context(line) as filename:
pep8obj = autopep8.FixPEP8(filename, None)
self.assertEqual(''.join(pep8obj.source), line)
def test_inplace_with_multi_files(self):
exception = None
with disable_stderr():
try:
autopep8.parse_args(['test.py', 'dummy.py'])
except SystemExit as e:
exception = e
self.assertTrue(exception)
self.assertEqual(exception.code, 2)
def test_standard_out_should_use_native_line_ending(self):
line = '1\r\n2\r\n3\r\n'
with temporary_file_context(line) as filename:
process = Popen(list(AUTOPEP8_CMD_TUPLE) +
[filename],
stdout=PIPE)
self.assertEqual(
os.linesep.join(['1', '2', '3', '']),
process.communicate()[0].decode('utf-8'))
def test_standard_out_should_use_native_line_ending_with_cr_input(self):
line = '1\r2\r3\r'
with temporary_file_context(line) as filename:
process = Popen(list(AUTOPEP8_CMD_TUPLE) +
[filename],
stdout=PIPE)
self.assertEqual(
os.linesep.join(['1', '2', '3', '']),
process.communicate()[0].decode('utf-8'))
def test_standard_in(self):
line = 'print( 1 )\n'
fixed = 'print(1)' + os.linesep
process = Popen(list(AUTOPEP8_CMD_TUPLE) +
['-'],
stdout=PIPE,
stdin=PIPE)
self.assertEqual(
fixed,
process.communicate(line.encode('utf-8'))[0].decode('utf-8'))
@contextlib.contextmanager
def autopep8_context(line, options=None):
if not options:
options = []
with temporary_file_context(line) as filename:
options, _ = autopep8.parse_args([filename] + list(options))
yield autopep8.fix_file(filename=filename, options=options)
@contextlib.contextmanager
def autopep8_subprocess(line, options):
with temporary_file_context(line) as filename:
p = Popen(list(AUTOPEP8_CMD_TUPLE) + [filename] + options,
stdout=PIPE)
yield p.communicate()[0].decode('utf-8')
@contextlib.contextmanager
def temporary_file_context(text, suffix='', prefix=''):
tempfile = mkstemp(suffix=suffix, prefix=prefix)
os.close(tempfile[0])
with autopep8.open_with_encoding(tempfile[1],
encoding='utf-8',
mode='w') as temp_file:
temp_file.write(text)
yield tempfile[1]
os.remove(tempfile[1])
@contextlib.contextmanager
def disable_stderr():
sio = StringIO()
with capture_stderr(sio):
yield
@contextlib.contextmanager
def capture_stderr(sio):
_tmp = sys.stderr
sys.stderr = sio
try:
yield
finally:
sys.stderr = _tmp
if __name__ == '__main__':
unittest.main()
autopep8-0.9.1/test/bad_encoding2.py 0000664 0001750 0001750 00000000036 12153410014 020433 0 ustar hattori hattori 0000000 0000000 #coding: utf8
print('我')
autopep8-0.9.1/README.rst 0000664 0001750 0001750 00000022317 12153410031 016120 0 ustar hattori hattori 0000000 0000000 ========
autopep8
========
.. image:: https://travis-ci.org/hhatto/autopep8.png?branch=master
:target: https://travis-ci.org/hhatto/autopep8
:alt: Build status
.. image:: https://coveralls.io/repos/hhatto/autopep8/badge.png?branch=master
:target: https://coveralls.io/r/hhatto/autopep8
:alt: Test coverage status
About
=====
autopep8 automatically formats Python code to conform to the `PEP 8`_ style
guide. It uses the pep8_ utility to determine what parts of the code needs to
be formatted. autopep8 is capable of fixing most of the formatting issues_ that
can be reported by pep8.
.. _PEP 8: http://www.python.org/dev/peps/pep-0008
.. _issues: https://pep8.readthedocs.org/en/latest/intro.html#error-codes
Installation
============
From pip::
$ pip install --upgrade autopep8
From easy_install::
$ easy_install -ZU autopep8
Requirements
============
autopep8 requires pep8_ (>= 1.4.5).
.. _pep8: https://github.com/jcrocholl/pep8
Usage
=====
To modify a file in place (with all fixes enabled)::
$ autopep8 --in-place --aggressive
Before running autopep8.
.. code-block:: python
import sys, os;
def someone_likes_semicolons( foo = None ,\
bar='bar'):
"""Hello; bye."""; print( 'A'<>foo) #<> is a deprecated form of !=
return 0;
def func11():
a=( 1,2, 3,"a" );
####This is a long comment. This should be wrapped to fit within 72 characters.
x = [a,[100,200,300,9876543210,'This is a long string that goes on and on']]
def func22(): return {True: True}.has_key({'foo': 2}.has_key('foo'));
class UselessClass( object ):
def __init__ ( self, bar ):
#Comments should have a space after the hash.
if bar : bar+=1; bar=bar* bar ; return bar
else:
indentation_in_strings_should_not_be_touched = """
hello
world
"""
raise ValueError, indentation_in_strings_should_not_be_touched
def my_method(self):
print(self);
After running autopep8.
.. code-block:: python
import sys
import os
def someone_likes_semicolons(foo=None,
bar='bar'):
"""Hello; bye."""
print('A' != foo) # <> is a deprecated form of !=
return 0
def func11():
a = (1, 2, 3, "a")
# This is a long comment. This should be wrapped to fit within 72
# characters.
x = [a, [100, 200, 300, 9876543210,
'This is a long string that goes on and on']]
def func22():
return ('foo' in {'foo': 2}) in {True: True}
class UselessClass(object):
def __init__(self, bar):
# Comments should have a space after the hash.
if bar:
bar += 1
bar = bar * bar
return bar
else:
indentation_in_strings_should_not_be_touched = """
hello
world
"""
raise ValueError(indentation_in_strings_should_not_be_touched)
def my_method(self):
print(self)
Options::
Usage: autopep8 [options] [filename [filename ...]]
Use filename '-' for stdin.
Automatically formats Python code to conform to the PEP 8 style guide.
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose print verbose messages; multiple -v result in more
verbose messages
-d, --diff print the diff for the fixed source
-i, --in-place make changes to files in place
-r, --recursive run recursively; must be used with --in-place or
--diff
-j n, --jobs=n number of parallel jobs; match CPU count if value is
less than 1
-p n, --pep8-passes=n
maximum number of additional pep8 passes (default:
infinite)
-a, --aggressive enable non-whitespace changes; multiple -a result in
more aggressive changes
--exclude=globs exclude files/directories that match these comma-
separated globs
--list-fixes list codes for fixes; used by --ignore and --select
--ignore=errors do not fix these errors/warnings (default: E24,W6)
--select=errors fix only these errors/warnings (e.g. E4,W)
--max-line-length=n set maximum allowed line length (default: 79)
Features
========
autopep8 fixes the following issues_ reported by pep8_::
E101 - Reindent all lines.
E111 - Reindent all lines.
E121 - Fix indentation to be a multiple of four.
E122 - Add absent indentation for hanging indentation.
E123 - Align closing bracket to match opening bracket.
E124 - Align closing bracket to match visual indentation.
E125 - Indent to distinguish line from next logical line.
E126 - Fix over-indented hanging indentation.
E127 - Fix visual indentation.
E128 - Fix visual indentation.
E20 - Remove extraneous whitespace.
E211 - Remove extraneous whitespace.
E22 - Fix extraneous whitespace around keywords.
E224 - Remove extraneous whitespace around operator.
E22 - Fix missing whitespace around operator.
E231 - Add missing whitespace.
E241 - Fix extraneous whitespace around keywords.
E242 - Remove extraneous whitespace around operator.
E251 - Remove whitespace around parameter '=' sign.
E26 - Fix spacing after comment hash.
E27 - Fix extraneous whitespace around keywords.
E301 - Add missing blank line.
E302 - Add missing 2 blank lines.
E303 - Remove extra blank lines.
E304 - Remove blank line following function decorator.
E401 - Put imports on separate lines.
E501 - Try to make lines fit within --max-line-length characters.
E502 - Remove extraneous escape of newline.
E701 - Put colon-separated compound statement on separate lines.
E70 - Put semicolon-separated compound statement on separate lines.
E711 - Fix comparison with None.
E712 - Fix comparison with boolean.
W191 - Reindent all lines.
W291 - Remove trailing whitespace.
W293 - Remove trailing whitespace on blank line.
W391 - Remove trailing blank lines.
E26 - Format block comments.
W6 - Fix various deprecated code (via lib2to3).
W602 - Fix deprecated form of raising exception.
autopep8 also fixes some issues not found by pep8_.
- Correct deprecated or non-idiomatic Python code (via ``lib2to3``). (This is
triggered if ``W6`` is enabled.)
- Format block comments. (This is triggered if ``E26`` is enabled.)
- Normalize files with mixed line endings.
- Put a blank line between a class declaration and its first method
declaration. (Enabled with ``E301``.)
- Remove blank lines between a function declaration and its docstring. (Enabled
with ``E303``.)
More advanced usage
===================
To enable only a subset of the fixes, use the ``--select`` option. For example,
to fix various types of indentation issues::
$ autopep8 --select=E1,W1
Similarly, to just fix deprecated code::
$ autopep8 --select=W6
The above is useful when trying to port a single code base to work with both
Python 2 and Python 3 at the same time.
If the file being fixed is large, you may want to enable verbose progress
messages::
$ autopep8 -v
By default autopep8 only makes whitespace changes. Thus, by default, it does
not fix ``E711`` and ``E712``. (Changing ``x == None`` to ``x is None`` may
change the meaning of the program if ``x`` has its ``__eq__`` method
overridden.) Nor does it correct deprecated code ``W6``. To enable these
more aggressive fixes, use the ``--aggressive`` option::
$ autopep8 --aggressive
``--aggressive`` will also shorten lines more aggressively.
Use as a module
===============
The simplest way of using autopep8 as a module is via the ``fix_string()``
function.
.. code-block:: python
>>> import autopep8
>>> autopep8.fix_string('x= 123\n')
'x = 123\n'
Testing
=======
Test cases are in ``test/test_autopep8.py``. They can be run directly via
``python test/test_autopep8.py`` or via tox_. The latter is useful for
testing against multiple Python interpreters. (We currently test against
CPython versions 2.6, 2.7, 3.2, and 3.3. We also test against PyPy.)
.. _`tox`: https://pypi.python.org/pypi/tox
Broad spectrum testing is available via ``test/acid.py``. This script runs
autopep8 against Python code and checks for correctness and completeness of the
code fixes. It can check that the bytecode remains identical.
``test/acid_pypi.py`` makes use of ``acid.py`` to test against the latest
released packages on PyPI. In a similar fashion, ``test/acid_github.py`` tests
against Python code in Github repositories.
Links
=====
* PyPI_
* GitHub_
* `Travis CI`_
* Jenkins_
.. _PyPI: https://pypi.python.org/pypi/autopep8/
.. _GitHub: https://github.com/hhatto/autopep8
.. _`Travis CI`: https://travis-ci.org/hhatto/autopep8
.. _Jenkins: http://jenkins.hexacosa.net/job/autopep8/
autopep8-0.9.1/autopep8.py 0000775 0001750 0001750 00000230736 12153410111 016560 0 ustar hattori hattori 0000000 0000000 #!/usr/bin/env python
#
# Copyright (C) 2010-2011 Hideo Hattori
# Copyright (C) 2011-2013 Hideo Hattori, Steven Myint
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""Automatically formats Python code to conform to the PEP 8 style guide.
Fixes that only need be done once can be added by adding a function of the form
"fix_(source)" to this module. They should return the fixed source code.
These fixes are picked up by apply_global_fixes().
Fixes that depend on pep8 should be added as methods to FixPEP8. See the class
documentation for more information.
"""
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import codecs
import copy
import fnmatch
import inspect
import os
import re
import signal
import sys
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import token
import tokenize
from optparse import OptionParser
import difflib
import pep8
try:
unicode
except NameError:
unicode = str
__version__ = '0.9.1'
CR = '\r'
LF = '\n'
CRLF = '\r\n'
PYTHON_SHEBANG_REGEX = re.compile(r'^#!.*\bpython[23]?\b')
# For generating line shortening candidates.
SHORTEN_OPERATOR_GROUPS = frozenset([
frozenset([',']),
frozenset(['%']),
frozenset([',', '(', '[', '{']),
frozenset([',', '(', '[', '{', '%', '+', '-', '*', '/', '//']),
])
DEFAULT_IGNORE = 'E24,W6'
def open_with_encoding(filename, encoding=None, mode='r'):
"""Return opened file with a specific encoding."""
if not encoding:
encoding = detect_encoding(filename)
import io
return io.open(filename, mode=mode, encoding=encoding,
newline='') # Preserve line endings
def detect_encoding(filename):
"""Return file encoding."""
try:
with open(filename, 'rb') as input_file:
from lib2to3.pgen2 import tokenize as lib2to3_tokenize
encoding = lib2to3_tokenize.detect_encoding(input_file.readline)[0]
# Check for correctness of encoding
with open_with_encoding(filename, encoding) as test_file:
test_file.read()
return encoding
except (LookupError, SyntaxError, UnicodeDecodeError):
return 'latin-1'
def read_from_filename(filename, readlines=False):
"""Return contents of file."""
with open_with_encoding(filename) as input_file:
return input_file.readlines() if readlines else input_file.read()
def extended_blank_lines(logical_line,
blank_lines,
indent_level,
previous_logical):
"""Check for missing blank lines after class declaration."""
if (previous_logical.startswith('class ')):
if (logical_line.startswith(('def ', 'class ', '@')) or
pep8.DOCSTRING_REGEX.match(logical_line)):
if indent_level and not blank_lines:
yield (0, 'E301 expected 1 blank line, found 0')
elif previous_logical.startswith('def '):
if blank_lines and pep8.DOCSTRING_REGEX.match(logical_line):
yield (0, 'E303 too many blank lines ({0})'.format(blank_lines))
elif pep8.DOCSTRING_REGEX.match(previous_logical):
if (
indent_level and
not blank_lines and
logical_line.startswith(('def ')) and
'(self' in logical_line
):
yield (0, 'E301 expected 1 blank line, found 0')
pep8.register_check(extended_blank_lines)
class FixPEP8(object):
"""Fix invalid code.
Fixer methods are prefixed "fix_". The _fix_source() method looks for these
automatically.
The fixer method can take either one or two arguments (in addition to
self). The first argument is "result", which is the error information from
pep8. The second argument, "logical", is required only for logical-line
fixes.
The fixer method can return the list of modified lines or None. An empty
list would mean that no changes were made. None would mean that only the
line reported in the pep8 error was modified. Note that the modified line
numbers that are returned are indexed at 1. This typically would correspond
with the line number reported in the pep8 error information.
[fixed method list]
- e111
- e121,e122,e123,e124,e125,e126,e127,e128,e129
- e201,e202,e203
- e211
- e221,e222,e223,e224,e225
- e231
- e251
- e261,e262
- e271,e272,e273,e274
- e301,e302,e303
- e401
- e502
- e701,e702
- e711
- w291,w293
- w391
"""
def __init__(self, filename, options, contents=None):
self.filename = filename
if contents is None:
self.source = read_from_filename(filename, readlines=True)
else:
sio = StringIO(contents)
self.source = sio.readlines()
self.newline = find_newline(self.source)
self.options = options
self.indent_word = _get_indentword(''.join(self.source))
# method definition
self.fix_e111 = self.fix_e101
self.fix_e128 = self.fix_e127
self.fix_e129 = self.fix_e125
self.fix_e202 = self.fix_e201
self.fix_e203 = self.fix_e201
self.fix_e211 = self.fix_e201
self.fix_e221 = self.fix_e271
self.fix_e222 = self.fix_e271
self.fix_e223 = self.fix_e271
self.fix_e226 = self.fix_e225
self.fix_e227 = self.fix_e225
self.fix_e228 = self.fix_e225
self.fix_e241 = self.fix_e271
self.fix_e242 = self.fix_e224
self.fix_e261 = self.fix_e262
self.fix_e272 = self.fix_e271
self.fix_e273 = self.fix_e271
self.fix_e274 = self.fix_e271
self.fix_e703 = self.fix_e702
self.fix_w191 = self.fix_e101
def _fix_source(self, results):
completed_lines = set()
for result in sorted(results, key=_priority_key):
if result['line'] in completed_lines:
continue
fixed_methodname = 'fix_%s' % result['id'].lower()
if hasattr(self, fixed_methodname):
fix = getattr(self, fixed_methodname)
is_logical_fix = len(inspect.getargspec(fix).args) > 2
if is_logical_fix:
# Do not run logical fix if any lines have been modified.
if completed_lines:
continue
logical = self._get_logical(result)
if not logical:
continue
modified_lines = fix(result, logical)
else:
modified_lines = fix(result)
if modified_lines:
completed_lines.update(modified_lines)
elif modified_lines == []: # Empty list means no fix
if self.options.verbose >= 2:
print(
'---> Not fixing {f} on line {l}'.format(
f=result['id'], l=result['line']),
file=sys.stderr)
else: # We assume one-line fix when None
completed_lines.add(result['line'])
else:
if self.options.verbose >= 3:
print("---> '%s' is not defined." % fixed_methodname,
file=sys.stderr)
info = result['info'].strip()
print('---> %s:%s:%s:%s' % (self.filename,
result['line'],
result['column'],
info),
file=sys.stderr)
def fix(self):
"""Return a version of the source code with PEP 8 violations fixed."""
pep8_options = {
'ignore': self.options.ignore,
'select': self.options.select,
'max_line_length': self.options.max_line_length,
}
results = _execute_pep8(pep8_options, self.source)
if self.options.verbose:
progress = {}
for r in results:
if r['id'] not in progress:
progress[r['id']] = set()
progress[r['id']].add(r['line'])
print('---> {n} issue(s) to fix {progress}'.format(
n=len(results), progress=progress), file=sys.stderr)
self._fix_source(filter_results(source=''.join(self.source),
results=results,
aggressive=self.options.aggressive))
return ''.join(self.source)
def fix_e101(self, _):
"""Reindent all lines."""
reindenter = Reindenter(self.source, self.newline)
modified_line_numbers = reindenter.run()
if modified_line_numbers:
self.source = reindenter.fixed_lines()
return modified_line_numbers
else:
return []
def _find_logical(self):
# make a variable which is the index of all the starts of lines
logical_start = []
logical_end = []
last_newline = True
sio = StringIO(''.join(self.source))
parens = 0
for t in tokenize.generate_tokens(sio.readline):
if t[0] in [tokenize.COMMENT, tokenize.DEDENT,
tokenize.INDENT, tokenize.NL,
tokenize.ENDMARKER]:
continue
if not parens and t[0] in [
tokenize.NEWLINE, tokenize.SEMI
]:
last_newline = True
logical_end.append((t[3][0] - 1, t[2][1]))
continue
if last_newline and not parens:
logical_start.append((t[2][0] - 1, t[2][1]))
last_newline = False
if t[0] == tokenize.OP:
if t[1] in '([{':
parens += 1
elif t[1] in '}])':
parens -= 1
return (logical_start, logical_end)
def _get_logical(self, result):
"""Return the logical line corresponding to the result.
Assumes input is already E702-clean.
"""
try:
(logical_start, logical_end) = self._find_logical()
except (SyntaxError, tokenize.TokenError):
return None
row = result['line'] - 1
col = result['column'] - 1
ls = None
le = None
for i in range(0, len(logical_start), 1):
x = logical_end[i]
if x[0] > row or (x[0] == row and x[1] > col):
le = x
ls = logical_start[i]
break
if ls is None:
return None
original = self.source[ls[0]:le[0] + 1]
return ls, le, original
def _fix_reindent(self, result, logical):
"""Fix a badly indented line.
This is done by adding or removing from its initial indent only.
"""
assert logical
ls, _, original = logical
rewrapper = Wrapper(original)
valid_indents = rewrapper.pep8_expected()
if not rewrapper.rel_indent:
return [] # pragma: no cover
if result['line'] > ls[0]:
# got a valid continuation line number from pep8
row = result['line'] - ls[0] - 1
# always pick the first option for this
valid = valid_indents[row]
got = rewrapper.rel_indent[row]
else:
return [] # pragma: no cover
line = ls[0] + row
# always pick the expected indent, for now.
indent_to = valid[0]
if got != indent_to:
orig_line = self.source[line]
new_line = ' ' * (indent_to) + orig_line.lstrip()
if new_line == orig_line:
return []
else:
self.source[line] = new_line
return [line + 1] # Line indexed at 1
else:
return [] # pragma: no cover
def fix_e121(self, result, logical):
"""Fix indentation to be a multiple of four."""
# Fix by adjusting initial indent level.
return self._fix_reindent(result, logical)
def fix_e122(self, result, logical):
"""Add absent indentation for hanging indentation."""
# Fix by adding an initial indent.
return self._fix_reindent(result, logical)
def fix_e123(self, result, logical):
"""Align closing bracket to match opening bracket."""
# Fix by deleting whitespace to the correct level.
assert logical
logical_lines = logical[2]
line_index = result['line'] - 1
original_line = self.source[line_index]
fixed_line = (_get_indentation(logical_lines[0]) +
original_line.lstrip())
if fixed_line == original_line:
# Fall back to slower method.
return self._fix_reindent(result, logical)
else:
self.source[line_index] = fixed_line
def fix_e124(self, result, logical):
"""Align closing bracket to match visual indentation."""
# Fix by inserting whitespace before the closing bracket.
return self._fix_reindent(result, logical)
def fix_e125(self, result, logical):
"""Indent to distinguish line from next logical line."""
# Fix by indenting the line in error to the next stop.
modified_lines = self._fix_reindent(result, logical)
if modified_lines:
return modified_lines
else:
# Fallback
line_index = result['line'] - 1
original_line = self.source[line_index]
self.source[line_index] = self.indent_word + original_line
def fix_e126(self, result, logical):
"""Fix over-indented hanging indentation."""
# fix by deleting whitespace to the left
assert logical
logical_lines = logical[2]
line_index = result['line'] - 1
original = self.source[line_index]
fixed = (_get_indentation(logical_lines[0]) +
self.indent_word + original.lstrip())
if fixed == original:
# Fall back to slower method.
return self._fix_reindent(result, logical) # pragma: no cover
else:
self.source[line_index] = fixed
def fix_e127(self, result, logical):
"""Fix visual indentation."""
# Fix by inserting/deleting whitespace to the correct level.
modified_lines = self._align_visual_indent(result, logical)
if modified_lines != []:
return modified_lines
else:
# Fall back to slower method.
return self._fix_reindent(result, logical)
def _align_visual_indent(self, result, logical):
"""Correct visual indent.
This includes over (E127) and under (E128) indented lines.
"""
assert logical
logical_lines = logical[2]
line_index = result['line'] - 1
original = self.source[line_index]
fixed = original
if logical_lines[0].rstrip().endswith('\\'):
fixed = (_get_indentation(logical_lines[0]) +
self.indent_word + original.lstrip())
else:
start_index = None
for symbol in '([{':
if symbol in logical_lines[0]:
found_index = logical_lines[0].find(symbol)
if start_index is None:
start_index = found_index
else:
start_index = min(start_index, found_index)
if start_index is not None:
fixed = start_index * ' ' + original.lstrip()
if fixed == original:
return []
else:
self.source[line_index] = fixed
def fix_e201(self, result):
"""Remove extraneous whitespace."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
# When multiline strings are involved, pep8 reports the error as
# being at the start of the multiline string, which doesn't work
# for us.
if ('"""' in target or
"'''" in target or
target.rstrip().endswith('\\')):
return []
fixed = fix_whitespace(target,
offset=offset,
replacement='')
if fixed == target:
return []
else:
self.source[line_index] = fixed
def fix_e224(self, result):
"""Remove extraneous whitespace around operator."""
target = self.source[result['line'] - 1]
offset = result['column'] - 1
fixed = target[:offset] + target[offset:].replace('\t', ' ')
self.source[result['line'] - 1] = fixed
def fix_e225(self, result):
"""Fix missing whitespace around operator."""
target = self.source[result['line'] - 1]
offset = result['column'] - 1
fixed = target[:offset] + ' ' + target[offset:]
# Only proceed if non-whitespace characters match.
# And make sure we don't break the indentation.
if (fixed.replace(' ', '') == target.replace(' ', '') and
_get_indentation(fixed) == _get_indentation(target)):
self.source[result['line'] - 1] = fixed
else:
return []
def fix_e231(self, result):
"""Add missing whitespace."""
# Optimize for comma case. This will fix all commas in the full source
# code in one pass.
if ',' in result['info']:
original = ''.join(self.source)
new = refactor(original, ['ws_comma'])
if original.strip() != new.strip():
self.source = [new]
return range(1, 1 + len(original))
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column']
fixed = target[:offset] + ' ' + target[offset:]
self.source[line_index] = fixed
def fix_e251(self, result):
"""Remove whitespace around parameter '=' sign."""
line_index = result['line'] - 1
target = self.source[line_index]
# This is necessary since pep8 sometimes reports columns that goes
# past the end of the physical line. This happens in cases like,
# foo(bar\n=None)
c = min(result['column'] - 1,
len(target) - 1)
if target[c].strip():
fixed = target
else:
fixed = target[:c].rstrip() + target[c:].lstrip()
# There could be an escaped newline
#
# def foo(a=\
# 1)
if (fixed.endswith('=\\\n') or
fixed.endswith('=\\\r\n') or
fixed.endswith('=\\\r')):
self.source[line_index] = fixed.rstrip('\n\r \t\\')
self.source[line_index + 1] = self.source[line_index + 1].lstrip()
return [line_index + 1, line_index + 2] # Line indexed at 1
self.source[result['line'] - 1] = fixed
def fix_e262(self, result):
"""Fix spacing after comment hash."""
target = self.source[result['line'] - 1]
offset = result['column']
code = target[:offset].rstrip(' \t#')
comment = target[offset:].lstrip(' \t#')
fixed = code + (' # ' + comment if comment.strip()
else self.newline)
self.source[result['line'] - 1] = fixed
def fix_e271(self, result):
"""Fix extraneous whitespace around keywords."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
# When multiline strings are involved, pep8 reports the error as
# being at the start of the multiline string, which doesn't work
# for us.
if ('"""' in target or
"'''" in target or
target.rstrip().endswith('\\')):
return []
fixed = fix_whitespace(target,
offset=offset,
replacement=' ')
if fixed == target:
return []
else:
self.source[line_index] = fixed
def fix_e301(self, result):
"""Add missing blank line."""
cr = self.newline
self.source[result['line'] - 1] = cr + self.source[result['line'] - 1]
def fix_e302(self, result):
"""Add missing 2 blank lines."""
add_linenum = 2 - int(result['info'].split()[-1])
cr = self.newline * add_linenum
self.source[result['line'] - 1] = cr + self.source[result['line'] - 1]
def fix_e303(self, result):
"""Remove extra blank lines."""
delete_linenum = int(result['info'].split('(')[1].split(')')[0]) - 2
delete_linenum = max(1, delete_linenum)
# We need to count because pep8 reports an offset line number if there
# are comments.
cnt = 0
line = result['line'] - 2
modified_lines = []
while cnt < delete_linenum and line >= 0:
if not self.source[line].strip():
self.source[line] = ''
modified_lines.append(1 + line) # Line indexed at 1
cnt += 1
line -= 1
return modified_lines
def fix_e304(self, result):
"""Remove blank line following function decorator."""
line = result['line'] - 2
if not self.source[line].strip():
self.source[line] = ''
def fix_e401(self, result):
"""Put imports on separate lines."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
if not target.lstrip().startswith('import'):
return []
# pep8 (1.3.1) reports false positive if there is an import statement
# followed by a semicolon and some unrelated statement with commas in
# it.
if ';' in target:
return []
indentation = re.split(pattern=r'\bimport\b',
string=target, maxsplit=1)[0]
fixed = (target[:offset].rstrip('\t ,') + self.newline +
indentation + 'import ' + target[offset:].lstrip('\t ,'))
self.source[line_index] = fixed
def fix_e501(self, result):
"""Try to make lines fit within --max-line-length characters."""
line_index = result['line'] - 1
target = self.source[line_index]
if target.lstrip().startswith('#'):
# Shorten comment if it is the last comment line.
try:
if self.source[line_index + 1].lstrip().startswith('#'):
return []
except IndexError:
pass
# Wrap commented lines.
fixed = shorten_comment(
line=target,
newline=self.newline,
max_line_length=self.options.max_line_length)
if fixed == self.source[line_index]:
return []
else:
self.source[line_index] = fixed
return
indent = _get_indentation(target)
source = target[len(indent):]
assert source.lstrip() == source
sio = StringIO(source)
# Check for multiline string.
try:
tokens = list(tokenize.generate_tokens(sio.readline))
except (SyntaxError, tokenize.TokenError):
multiline_candidate = break_multiline(
target, newline=self.newline,
indent_word=self.indent_word)
if multiline_candidate:
self.source[line_index] = multiline_candidate
return
else:
return []
candidates = shorten_line(
tokens, source, indent,
self.indent_word, newline=self.newline,
aggressive=self.options.aggressive)
candidates = list(sorted(
set(candidates),
key=lambda x: line_shortening_rank(x,
self.newline,
self.indent_word)))
if self.options.verbose >= 4:
print(('-' * 79 + '\n').join([''] + candidates + ['']),
file=codecs.getwriter('utf-8')(sys.stderr.buffer
if hasattr(sys.stderr,
'buffer')
else sys.stderr))
for _candidate in candidates:
assert _candidate is not None
if (get_longest_length(_candidate, self.newline) >=
get_longest_length(target, self.newline)):
continue
self.source[line_index] = _candidate
return
return []
def fix_e502(self, result):
"""Remove extraneous escape of newline."""
line_index = result['line'] - 1
target = self.source[line_index]
self.source[line_index] = target.rstrip('\n\r \t\\') + self.newline
def fix_e701(self, result):
"""Put colon-separated compound statement on separate lines."""
line_index = result['line'] - 1
target = self.source[line_index]
c = result['column']
fixed_source = (target[:c] + self.newline +
_get_indentation(target) + self.indent_word +
target[c:].lstrip('\n\r \t\\'))
self.source[result['line'] - 1] = fixed_source
def fix_e702(self, result, logical):
"""Put semicolon-separated compound statement on separate lines."""
logical_lines = logical[2]
line_index = result['line'] - 1
target = self.source[line_index]
if target.rstrip().endswith('\\'):
# Normalize '1; \\\n2' into '1; 2'.
self.source[line_index] = target.rstrip('\n \r\t\\')
self.source[line_index + 1] = self.source[line_index + 1].lstrip()
return [line_index + 1, line_index + 2]
if target.rstrip().endswith(';'):
self.source[line_index] = target.rstrip('\n \r\t;') + self.newline
return
offset = result['column'] - 1
first = target[:offset].rstrip(';').rstrip()
second = (_get_indentation(logical_lines[0]) +
target[offset:].lstrip(';').lstrip())
self.source[line_index] = first + self.newline + second
def fix_e711(self, result):
"""Fix comparison with None."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
right_offset = offset + 2
if right_offset >= len(target):
return []
left = target[:offset].rstrip()
center = target[offset:right_offset]
right = target[right_offset:].lstrip()
if not right.startswith('None'):
return []
if center.strip() == '==':
new_center = 'is'
elif center.strip() == '!=':
new_center = 'is not'
else:
return []
self.source[line_index] = ' '.join([left, new_center, right])
def fix_e712(self, result):
"""Fix comparison with boolean."""
line_index = result['line'] - 1
target = self.source[line_index]
offset = result['column'] - 1
right_offset = offset + 2
if right_offset >= len(target):
return []
left = target[:offset].rstrip()
center = target[offset:right_offset]
right = target[right_offset:].lstrip()
# Handle simple cases only.
new_right = None
if center.strip() == '==':
if re.match(r'\bTrue\b', right):
new_right = re.sub(r'\bTrue\b *', '', right, count=1)
elif center.strip() == '!=':
if re.match(r'\bFalse\b', right):
new_right = re.sub(r'\bFalse\b *', '', right, count=1)
if new_right is None:
return []
if new_right[0].isalnum():
new_right = ' ' + new_right
self.source[line_index] = left + new_right
def fix_w291(self, result):
"""Remove trailing whitespace."""
fixed_line = self.source[result['line'] - 1].rstrip()
self.source[result['line'] - 1] = '%s%s' % (fixed_line, self.newline)
def fix_w293(self, result):
"""Remove trailing whitespace on blank line."""
assert not self.source[result['line'] - 1].strip()
self.source[result['line'] - 1] = self.newline
def fix_w391(self, _):
"""Remove trailing blank lines."""
blank_count = 0
for line in reversed(self.source):
line = line.rstrip()
if line:
break
else:
blank_count += 1
original_length = len(self.source)
self.source = self.source[:original_length - blank_count]
return range(1, 1 + original_length)
def fix_e26(source):
"""Format block comments."""
if '#' not in source:
# Optimization.
return source
string_line_numbers = multiline_string_lines(source,
include_docstrings=True)
fixed_lines = []
sio = StringIO(source)
for (line_number, line) in enumerate(sio.readlines(), start=1):
if (line.lstrip().startswith('#') and
line_number not in string_line_numbers):
indentation = _get_indentation(line)
line = line.lstrip()
# Normalize beginning if not a shebang.
if len(line) > 1:
# Leave multiple spaces like '# ' alone.
if line.count('#') > 1 or line[1].isalnum():
line = '# ' + line.lstrip('# \t')
fixed_lines.append(indentation + line)
else:
fixed_lines.append(line)
return ''.join(fixed_lines)
def refactor(source, fixer_names, ignore=None):
"""Return refactored code using lib2to3.
Skip if ignore string is produced in the refactored code.
"""
from lib2to3 import pgen2
try:
new_text = refactor_with_2to3(source,
fixer_names=fixer_names)
except (pgen2.parse.ParseError,
SyntaxError,
UnicodeDecodeError,
UnicodeEncodeError):
return source
if ignore:
if ignore in new_text and ignore not in source:
return source
return new_text
def fix_w602(source):
"""Fix deprecated form of raising exception."""
return refactor(source, ['raise'],
ignore='with_traceback')
def fix_w6(source):
"""Fix various deprecated code (via lib2to3)."""
return refactor(source,
['apply',
'except',
'exec',
'execfile',
'exitfunc',
'has_key',
'idioms',
'import',
'methodattrs', # Python >= 2.6
'ne',
'numliterals',
'operator',
'paren',
'reduce',
'renames',
'repr',
'standarderror',
'sys_exc',
'throw',
'tuple_params',
'types',
'xreadlines'])
def find_newline(source):
"""Return type of newline used in source."""
cr, lf, crlf = 0, 0, 0
for s in source:
if s.endswith(CRLF):
crlf += 1
elif s.endswith(CR):
cr += 1
elif s.endswith(LF):
lf += 1
_max = max(lf, cr, crlf)
if _max == lf:
return LF
elif _max == crlf:
return CRLF
else:
return CR
def _get_indentword(source):
"""Return indentation type."""
sio = StringIO(source)
indent_word = ' ' # Default in case source has no indentation
try:
for t in tokenize.generate_tokens(sio.readline):
if t[0] == token.INDENT:
indent_word = t[1]
break
except (SyntaxError, tokenize.TokenError):
pass
return indent_word
def _get_indentation(line):
"""Return leading whitespace."""
if line.strip():
non_whitespace_index = len(line) - len(line.lstrip())
return line[:non_whitespace_index]
else:
return ''
def get_diff_text(old, new, filename):
"""Return text of unified diff between old and new."""
newline = '\n'
diff = difflib.unified_diff(
old, new,
'original/' + filename,
'fixed/' + filename,
lineterm=newline)
text = ''
for line in diff:
text += line
# Work around missing newline (http://bugs.python.org/issue2142).
if not line.endswith(newline):
text += newline + r'\ No newline at end of file' + newline
return text
def _priority_key(pep8_result):
"""Key for sorting PEP8 results.
Global fixes should be done first. This is important for things
like indentation.
"""
priority = [
# Global fixes.
'e101', 'e111', 'w191',
# Fix multiline colon-based before semicolon based.
'e701',
# Break multiline statements early.
'e702',
# Things that make lines longer.
'e225', 'e231',
# Remove extraneous whitespace before breaking lines.
'e201',
# Before breaking lines.
'e121', 'e122', 'e123', 'e124', 'e125', 'e126', 'e127', 'e128', 'e129',
]
key = pep8_result['id'].lower()
if key in priority:
return priority.index(key)
else:
# Lowest priority
return len(priority)
def shorten_line(tokens, source, indentation, indent_word, newline,
aggressive=False):
"""Separate line at OPERATOR.
Multiple candidates will be yielded.
"""
for candidate in _shorten_line(tokens=tokens,
source=source,
indentation=indentation,
indent_word=indent_word,
newline=newline,
aggressive=aggressive):
yield candidate
if aggressive:
for key_token_strings in SHORTEN_OPERATOR_GROUPS:
shortened = _shorten_line_at_tokens(
tokens=tokens,
source=source,
indentation=indentation,
indent_word=indent_word,
newline=newline,
key_token_strings=key_token_strings,
aggressive=aggressive)
if shortened is not None and shortened != source:
yield shortened
def _shorten_line(tokens, source, indentation, indent_word, newline,
aggressive=False):
"""Separate line at OPERATOR.
Multiple candidates will be yielded.
"""
for tkn in tokens:
# Don't break on '=' after keyword as this violates PEP 8.
if token.OP == tkn[0] and tkn[1] != '=':
assert tkn[0] != token.INDENT
offset = tkn[2][1] + 1
first = source[:offset]
second_indent = indentation
if first.rstrip().endswith('('):
second_indent += indent_word
elif '(' in first:
second_indent += ' ' * (1 + first.find('('))
else:
second_indent += indent_word
second = (second_indent + source[offset:].lstrip())
if not second.strip():
continue
# Do not begin a line with a comma
if second.lstrip().startswith(','):
continue
# Do end a line with a dot
if first.rstrip().endswith('.'):
continue
if tkn[1] in '+-*/':
fixed = first + ' \\' + newline + second
else:
fixed = first + newline + second
# Only fix if syntax is okay.
if check_syntax(normalize_multiline(fixed, newline=newline)
if aggressive else fixed):
yield indentation + fixed
def _shorten_line_at_tokens(tokens, source, indentation, indent_word, newline,
key_token_strings, aggressive):
"""Separate line by breaking at tokens in key_token_strings.
This will always break the line at the first parenthesis.
"""
offsets = []
first_paren = True
for tkn in tokens:
token_type = tkn[0]
token_string = tkn[1]
next_offset = tkn[2][1] + 1
assert token_type != token.INDENT
if token_string in key_token_strings or (first_paren and
token_string == '('):
# Don't split right before newline.
if next_offset < len(source) - 1:
offsets.append(next_offset)
if token_string == '(':
first_paren = False
current_indent = None
fixed = None
for line in split_at_offsets(source, offsets):
if fixed:
fixed += newline + current_indent + line
for symbol in '([{':
if line.endswith(symbol):
current_indent += indent_word
else:
# First line.
fixed = line
assert not current_indent
current_indent = indent_word
assert fixed is not None
if check_syntax(normalize_multiline(fixed, newline=newline)
if aggressive > 1 else fixed):
return indentation + fixed
else:
return None
def normalize_multiline(line, newline):
"""Remove multiline-related code that will cause syntax error.
This is for purposes of checking syntax.
"""
for quote in '\'"':
dict_pattern = r'^{q}[^{q}]*{q} *: *'.format(q=quote)
if re.match(dict_pattern, line):
if not line.strip().endswith('}'):
line += '}'
return '{' + line
if line.startswith('def ') and line.rstrip().endswith(':'):
# Do not allow ':' to be alone. That is invalid.
split_line = [item.strip() for item in line.split(newline)]
if ':' not in split_line and 'def' not in split_line:
return line[len('def'):].strip().rstrip(':')
return line
def fix_whitespace(line, offset, replacement):
"""Replace whitespace at offset and return fixed line."""
# Replace escaped newlines too
left = line[:offset].rstrip('\n\r \t\\')
right = line[offset:].lstrip('\n\r \t\\')
if right.startswith('#'):
return line
else:
return left + replacement + right
def _execute_pep8(pep8_options, source):
"""Execute pep8 via python method calls."""
class QuietReport(pep8.BaseReport):
"""Version of checker that does not print."""
def __init__(self, options):
super(QuietReport, self).__init__(options)
self.__full_error_results = []
def error(self, line_number, offset, text, _):
"""Collect errors."""
code = super(QuietReport, self).error(line_number, offset, text, _)
if code:
self.__full_error_results.append(
{'id': code,
'line': line_number,
'column': offset + 1,
'info': text})
def full_error_results(self):
"""Return error results in detail.
Results are in the form of a list of dictionaries. Each dictionary
contains 'id', 'line', 'column', and 'info'.
"""
return self.__full_error_results
checker = pep8.Checker('', lines=source,
reporter=QuietReport, **pep8_options)
checker.check_all()
return checker.report.full_error_results()
class Reindenter(object):
"""Reindents badly-indented code to uniformly use four-space indentation.
Released to the public domain, by Tim Peters, 03 October 2000.
"""
def __init__(self, input_text, newline):
self.newline = newline
# Raw file lines.
self.raw = input_text
self.after = None
self.string_content_line_numbers = multiline_string_lines(
''.join(self.raw))
# File lines, rstripped & tab-expanded. Dummy at start is so
# that we can use tokenize's 1-based line numbering easily.
# Note that a line is all-blank iff it is a newline.
self.lines = []
for line_number, line in enumerate(self.raw, start=1):
# Do not modify if inside a multiline string.
if line_number in self.string_content_line_numbers:
self.lines.append(line)
else:
# Only expand leading tabs.
self.lines.append(_get_indentation(line).expandtabs() +
line.strip() + newline)
self.lines.insert(0, None)
self.index = 1 # index into self.lines of next line
def run(self):
"""Fix indentation and return modified line numbers.
Line numbers are indexed at 1.
"""
try:
stats = reindent_stats(tokenize.generate_tokens(self.getline))
except (SyntaxError, tokenize.TokenError):
return set()
# Remove trailing empty lines.
lines = self.lines
while lines and lines[-1] == self.newline:
lines.pop()
# Sentinel.
stats.append((len(lines), 0))
# Map count of leading spaces to # we want.
have2want = {}
# Program after transformation.
after = self.after = []
# Copy over initial empty lines -- there's nothing to do until
# we see a line with *something* on it.
i = stats[0][0]
after.extend(lines[1:i])
for i in range(len(stats) - 1):
thisstmt, thislevel = stats[i]
nextstmt = stats[i + 1][0]
have = _leading_space_count(lines[thisstmt])
want = thislevel * 4
if want < 0:
# A comment line.
if have:
# An indented comment line. If we saw the same
# indentation before, reuse what it most recently
# mapped to.
want = have2want.get(have, -1)
if want < 0:
# Then it probably belongs to the next real stmt.
for j in range(i + 1, len(stats) - 1):
jline, jlevel = stats[j]
if jlevel >= 0:
if have == _leading_space_count(lines[jline]):
want = jlevel * 4
break
if want < 0: # Maybe it's a hanging
# comment like this one,
# in which case we should shift it like its base
# line got shifted.
for j in range(i - 1, -1, -1):
jline, jlevel = stats[j]
if jlevel >= 0:
want = (have + _leading_space_count(
after[jline - 1]) -
_leading_space_count(lines[jline]))
break
if want < 0:
# Still no luck -- leave it alone.
want = have
else:
want = 0
assert want >= 0
have2want[have] = want
diff = want - have
if diff == 0 or have == 0:
after.extend(lines[thisstmt:nextstmt])
else:
for line_number, line in enumerate(lines[thisstmt:nextstmt],
start=thisstmt):
if line_number in self.string_content_line_numbers:
after.append(line)
elif diff > 0:
if line == self.newline:
after.append(line)
else:
after.append(' ' * diff + line)
else:
remove = min(_leading_space_count(line), -diff)
after.append(line[remove:])
if self.raw == self.after:
return set()
else:
return (set(range(1, 1 + len(self.raw))) -
self.string_content_line_numbers)
def fixed_lines(self):
return self.after
def getline(self):
"""Line-getter for tokenize."""
if self.index >= len(self.lines):
line = ''
else:
line = self.lines[self.index]
self.index += 1
return line
def reindent_stats(tokens):
"""Return list of (lineno, indentlevel) pairs.
One for each stmt and comment line. indentlevel is -1 for comment lines, as
a signal that tokenize doesn't know what to do about them; indeed, they're
our headache!
"""
find_stmt = 1 # next token begins a fresh stmt?
level = 0 # current indent level
stats = []
for t in tokens:
token_type = t[0]
sline = t[2][0]
line = t[4]
if token_type == tokenize.NEWLINE:
# A program statement, or ENDMARKER, will eventually follow,
# after some (possibly empty) run of tokens of the form
# (NL | COMMENT)* (INDENT | DEDENT+)?
find_stmt = 1
elif token_type == tokenize.INDENT:
find_stmt = 1
level += 1
elif token_type == tokenize.DEDENT:
find_stmt = 1
level -= 1
elif token_type == tokenize.COMMENT:
if find_stmt:
stats.append((sline, -1))
# but we're still looking for a new stmt, so leave
# find_stmt alone
elif token_type == tokenize.NL:
pass
elif find_stmt:
# This is the first "real token" following a NEWLINE, so it
# must be the first token of the next program statement, or an
# ENDMARKER.
find_stmt = 0
if line: # not endmarker
stats.append((sline, level))
return stats
class Wrapper(object):
"""Class for functions relating to continuation lines and line folding.
Each instance operates on a single logical line.
"""
SKIP_TOKENS = frozenset([
tokenize.COMMENT, tokenize.NL, tokenize.INDENT,
tokenize.DEDENT, tokenize.NEWLINE, tokenize.ENDMARKER
])
def __init__(self, physical_lines):
self.lines = physical_lines
self.tokens = []
self.rel_indent = None
sio = StringIO(''.join(physical_lines))
for t in tokenize.generate_tokens(sio.readline):
if not len(self.tokens) and t[0] in self.SKIP_TOKENS:
continue
if t[0] != tokenize.ENDMARKER:
self.tokens.append(t)
self.logical_line = self.build_tokens_logical(self.tokens)
def build_tokens_logical(self, tokens):
"""Build a logical line from a list of tokens.
Return the logical line and a list of (offset, token) tuples. Does
not mute strings like the version in pep8.py.
"""
# from pep8.py with minor modifications
logical = []
previous = None
for t in tokens:
token_type, text = t[0:2]
if token_type in self.SKIP_TOKENS:
continue
if previous:
end_line, end = previous[3]
start_line, start = t[2]
if end_line != start_line: # different row
prev_text = self.lines[end_line - 1][end - 1]
if prev_text == ',' or (prev_text not in '{[('
and text not in '}])'):
logical.append(' ')
elif end != start: # different column
fill = self.lines[end_line - 1][end:start]
logical.append(fill)
logical.append(text)
previous = t
logical_line = ''.join(logical)
assert logical_line.lstrip() == logical_line
assert logical_line.rstrip() == logical_line
return logical_line
def pep8_expected(self):
"""Replicate logic in pep8.py, to know what level to indent things to.
Return a list of lists; each list represents valid indent levels for
the line in question, relative from the initial indent. However, the
first entry is the indent level which was expected.
"""
# What follows is an adjusted version of
# pep8.py:continuation_line_indentation. All of the comments have been
# stripped and the 'yield' statements replaced with 'pass'.
if not self.tokens:
return # pragma: no cover
first_row = self.tokens[0][2][0]
nrows = 1 + self.tokens[-1][2][0] - first_row
# here are the return values
valid_indents = [list()] * nrows
indent_level = self.tokens[0][2][1]
valid_indents[0].append(indent_level)
if nrows == 1:
# bug, really.
return valid_indents # pragma: no cover
indent_next = self.logical_line.endswith(':')
row = depth = 0
parens = [0] * nrows
self.rel_indent = rel_indent = [0] * nrows
indent = [indent_level]
indent_chances = {}
last_indent = (0, 0)
last_token_multiline = None
for token_type, text, start, end, _ in self.tokens:
newline = row < start[0] - first_row
if newline:
row = start[0] - first_row
newline = (not last_token_multiline and
token_type not in (tokenize.NL, tokenize.NEWLINE))
if newline:
# This is where the differences start. Instead of looking at
# the line and determining whether the observed indent matches
# our expectations, we decide which type of indentation is in
# use at the given indent level, and return the offset. This
# algorithm is susceptible to "carried errors", but should
# through repeated runs eventually solve indentation for
# multiline expressions less than PEP8_PASSES_MAX lines long.
if depth:
for open_row in range(row - 1, -1, -1):
if parens[open_row]:
break
else:
open_row = 0
# That's all we get to work with. This code attempts to
# "reverse" the below logic, and place into the valid indents
# list
vi = []
add_second_chances = False
if token_type == tokenize.OP and text in ']})':
# this line starts with a closing bracket, so it needs to
# be closed at the same indent as the opening one.
if indent[depth]:
# hanging indent
vi.append(indent[depth])
else:
# visual indent
vi.append(indent_level + rel_indent[open_row])
elif depth and indent[depth]:
# visual indent was previously confirmed.
vi.append(indent[depth])
add_second_chances = True
elif depth and True in indent_chances.values():
# visual indent happened before, so stick to
# visual indent this time.
if depth > 1 and indent[depth - 1]:
vi.append(indent[depth - 1])
else:
# stupid fallback
vi.append(indent_level + 4)
add_second_chances = True
elif not depth:
vi.append(indent_level + 4)
else:
# must be in hanging indent
hang = rel_indent[open_row] + 4
vi.append(indent_level + hang)
# about the best we can do without look-ahead
if (indent_next and vi[0] == indent_level + 4 and
nrows == row + 1):
vi[0] += 4
if add_second_chances:
# visual indenters like to line things up.
min_indent = vi[0]
for col, what in indent_chances.items():
if col > min_indent and (
what is True or
(what == str and token_type == tokenize.STRING) or
(what == text and token_type == tokenize.OP)
):
vi.append(col)
vi = sorted(vi)
valid_indents[row] = vi
# Returning to original continuation_line_indentation() from
# pep8.
visual_indent = indent_chances.get(start[1])
last_indent = start
rel_indent[row] = start[1] - indent_level
hang = rel_indent[row] - rel_indent[open_row]
if token_type == tokenize.OP and text in ']})':
pass
elif visual_indent is True:
if not indent[depth]:
indent[depth] = start[1]
# line altered: comments shouldn't define a visual indent
if parens[row] and not indent[depth] and token_type not in (
tokenize.NL, tokenize.COMMENT
):
indent[depth] = start[1]
indent_chances[start[1]] = True
elif token_type == tokenize.STRING or text in (
'u', 'ur', 'b', 'br'
):
indent_chances[start[1]] = str
if token_type == tokenize.OP:
if text in '([{':
depth += 1
indent.append(0)
parens[row] += 1
elif text in ')]}' and depth > 0:
prev_indent = indent.pop() or last_indent[1]
for d in range(depth):
if indent[d] > prev_indent:
indent[d] = 0
for ind in list(indent_chances):
if ind >= prev_indent:
del indent_chances[ind]
depth -= 1
if depth and indent[depth]: # modified
indent_chances[indent[depth]] = True
for idx in range(row, -1, -1):
if parens[idx]:
parens[idx] -= 1
break
assert len(indent) == depth + 1
if start[1] not in indent_chances:
indent_chances[start[1]] = text
last_token_multiline = (start[0] != end[0])
return valid_indents
def _leading_space_count(line):
"""Return number of leading spaces in line."""
i = 0
while i < len(line) and line[i] == ' ':
i += 1
return i
def refactor_with_2to3(source_text, fixer_names):
"""Use lib2to3 to refactor the source.
Return the refactored source code.
"""
from lib2to3.refactor import RefactoringTool
fixers = ['lib2to3.fixes.fix_' + name for name in fixer_names]
tool = RefactoringTool(fixer_names=fixers, explicit=fixers)
return unicode(tool.refactor_string(source_text, name=''))
def break_multiline(source_text, newline, indent_word):
"""Break first line of multiline code.
Return None if a break is not possible.
"""
indentation = _get_indentation(source_text)
# Handle special case only.
for symbol in '([{':
# Only valid if symbol is not on a line by itself.
if (symbol in source_text and not source_text.strip() == symbol):
if not source_text.rstrip()[-1] == ',':
continue
index = 1 + source_text.find(symbol)
if index <= len(indent_word) + len(indentation):
continue
if is_probably_inside_string_or_comment(source_text, index - 1):
continue
return (
source_text[:index].rstrip() + newline +
indentation + indent_word +
source_text[index:].lstrip())
return None
def is_probably_inside_string_or_comment(line, index):
"""Return True if index may be inside a string or comment."""
# Make sure we are not in a string.
for quote in ['"', "'"]:
if quote in line:
if line.find(quote) <= index:
return True
# Make sure we are not in a comment.
if '#' in line:
if line.find('#') <= index:
return True
return False
def check_syntax(code):
"""Return True if syntax is okay."""
try:
return compile(code, '', 'exec')
except (SyntaxError, TypeError, UnicodeDecodeError):
return False
def filter_results(source, results, aggressive=False):
"""Filter out spurious reports from pep8.
If aggressive is True, we allow possibly unsafe fixes (E711, E712).
"""
non_docstring_string_line_numbers = multiline_string_lines(
source, include_docstrings=False)
all_string_line_numbers = multiline_string_lines(
source, include_docstrings=True)
split_source = [None] + source.splitlines()
for r in results:
issue_id = r['id'].lower()
if r['line'] in non_docstring_string_line_numbers:
if issue_id.startswith('e1'):
continue
elif issue_id in ['e501', 'w191']:
continue
if r['line'] in all_string_line_numbers:
if issue_id in ['e501']:
continue
# Filter out incorrect E101 reports when there are no tabs.
# pep8 will complain about this even if the tab indentation found
# elsewhere is in a multiline string.
if issue_id == 'e101' and '\t' not in split_source[r['line']]:
continue
if issue_id in ['e711', 'e712'] and not aggressive:
continue
yield r
def multiline_string_lines(source, include_docstrings=False):
"""Return line numbers that are within multiline strings.
The line numbers are indexed at 1.
Docstrings are ignored.
"""
sio = StringIO(source)
line_numbers = set()
previous_token_type = ''
try:
for t in tokenize.generate_tokens(sio.readline):
token_type = t[0]
start_row = t[2][0]
end_row = t[3][0]
start_row = t[2][0]
end_row = t[3][0]
if (token_type == tokenize.STRING and start_row != end_row):
if (include_docstrings or
previous_token_type != tokenize.INDENT):
# We increment by one since we want the contents of the
# string.
line_numbers |= set(range(1 + start_row, 1 + end_row))
previous_token_type = token_type
except (SyntaxError, tokenize.TokenError):
pass
return line_numbers
def shorten_comment(line, newline, max_line_length):
"""Return trimmed or split long comment line."""
assert len(line) > max_line_length
line = line.rstrip()
# PEP 8 recommends 72 characters for comment text.
indentation = _get_indentation(line) + '# '
max_line_length = min(max_line_length,
len(indentation) + 72)
MIN_CHARACTER_REPEAT = 5
if (len(line) - len(line.rstrip(line[-1])) >= MIN_CHARACTER_REPEAT and
not line[-1].isalnum()):
# Trim comments that end with things like ---------
return line[:max_line_length] + newline
elif re.match(r'\s*#+\s*\w+', line):
import textwrap
split_lines = textwrap.wrap(line.lstrip(' \t#'),
initial_indent=indentation,
subsequent_indent=indentation,
width=max_line_length,
break_long_words=False,
break_on_hyphens=False)
return newline.join(split_lines) + newline
else:
return line + newline
def normalize_line_endings(lines):
"""Return fixed line endings.
All lines will be modified to use the most common line ending.
"""
newline = find_newline(lines)
return [line.rstrip('\n\r') + newline for line in lines]
def mutual_startswith(a, b):
return b.startswith(a) or a.startswith(b)
def code_match(code, select, ignore):
if ignore:
for ignored_code in [c.strip() for c in ignore]:
if mutual_startswith(code.lower(), ignored_code.lower()):
return False
if select:
for selected_code in [c.strip() for c in select]:
if mutual_startswith(code.lower(), selected_code.lower()):
return True
return False
return True
def fix_string(source, options=None):
"""Return fixed source code."""
if not options:
options = parse_args([''])[0]
sio = StringIO(source)
return fix_lines(sio.readlines(), options=options)
def fix_lines(source_lines, options, filename=''):
"""Return fixed source code."""
tmp_source = ''.join(normalize_line_endings(source_lines))
# Keep a history to break out of cycles.
previous_hashes = set([hash(tmp_source)])
# Apply global fixes only once (for efficiency).
fixed_source = apply_global_fixes(tmp_source, options)
passes = 0
while True:
if options.pep8_passes >= 0 and passes > options.pep8_passes:
break
passes += 1
tmp_source = copy.copy(fixed_source)
fix = FixPEP8(filename, options, contents=tmp_source)
fixed_source = fix.fix()
if hash(fixed_source) in previous_hashes:
break
else:
previous_hashes.add(hash(fixed_source))
return fixed_source
def fix_file(filename, options=None, output=None):
if not options:
options = parse_args([filename])[0]
original_source = read_from_filename(filename, readlines=True)
fixed_source = original_source
if options.in_place or output:
encoding = detect_encoding(filename)
if output:
output = codecs.getwriter(encoding)(output.buffer
if hasattr(output, 'buffer')
else output)
output = LineEndingWrapper(output)
fixed_source = fix_lines(fixed_source, options, filename=filename)
if options.diff:
new = StringIO(fixed_source)
new = new.readlines()
diff = get_diff_text(original_source, new, filename)
if output:
output.write(diff)
output.flush()
else:
return diff
elif options.in_place:
fp = open_with_encoding(filename, encoding=encoding,
mode='w')
fp.write(fixed_source)
fp.close()
else:
if output:
output.write(fixed_source)
output.flush()
else:
return fixed_source
def global_fixes():
"""Yield multiple (code, function) tuples."""
for function in globals().values():
if inspect.isfunction(function):
arguments = inspect.getargspec(function)[0]
if arguments != ['source']:
continue
code = extract_code_from_function(function)
if code:
yield (code, function)
def apply_global_fixes(source, options):
"""Run global fixes on source code.
Thsese are fixes that only need be done once (unlike those in FixPEP8,
which are dependent on pep8).
"""
for (code, function) in global_fixes():
if code_match(code, select=options.select, ignore=options.ignore):
if options.verbose:
print('---> Applying global fix for {0}'.format(code.upper()),
file=sys.stderr)
source = function(source)
return source
def extract_code_from_function(function):
"""Return code handled by function."""
if not function.__name__.startswith('fix_'):
return None
code = re.sub('^fix_', '', function.__name__)
if not code:
return None
try:
int(code[1:])
except ValueError:
return None
return code
def parse_args(args):
"""Parse command-line options."""
parser = OptionParser(usage='Usage: autopep8 [options] '
'[filename [filename ...]]'
'\nUse filename \'-\' for stdin.',
version='%prog {0}'.format(__version__),
description=__doc__.split('\n')[0],
prog='autopep8')
parser.add_option('-v', '--verbose', action='count', dest='verbose',
default=0,
help='print verbose messages; '
'multiple -v result in more verbose messages')
parser.add_option('-d', '--diff', action='store_true', dest='diff',
help='print the diff for the fixed source')
parser.add_option('-i', '--in-place', action='store_true',
help='make changes to files in place')
parser.add_option('-r', '--recursive', action='store_true',
help='run recursively; must be used with --in-place or '
'--diff')
parser.add_option('-j', '--jobs', type=int, metavar='n', default=1,
help='number of parallel jobs; '
'match CPU count if value is less than 1')
parser.add_option('-p', '--pep8-passes', metavar='n',
default=-1, type=int,
help='maximum number of additional pep8 passes '
'(default: infinite)')
parser.add_option('-a', '--aggressive', action='count', default=0,
help='enable non-whitespace changes; '
'multiple -a result in more aggressive changes')
parser.add_option('--exclude', metavar='globs',
help='exclude files/directories that match these '
'comma-separated globs')
parser.add_option('--list-fixes', action='store_true',
help='list codes for fixes; '
'used by --ignore and --select')
parser.add_option('--ignore', metavar='errors', default='',
help='do not fix these errors/warnings '
'(default: {0})'.format(DEFAULT_IGNORE))
parser.add_option('--select', metavar='errors', default='',
help='fix only these errors/warnings (e.g. E4,W)')
parser.add_option('--max-line-length', metavar='n', default=79, type=int,
help='set maximum allowed line length '
'(default: %default)')
options, args = parser.parse_args(args)
if not len(args) and not options.list_fixes:
parser.error('incorrect number of arguments')
if '-' in args:
if len(args) > 1:
parser.error('cannot mix stdin and regular files')
if options.diff:
parser.error('--diff cannot be used with standard input')
if options.in_place:
parser.error('--in-place cannot be used with standard input')
if options.recursive:
parser.error('--recursive cannot be used with standard input')
if len(args) > 1 and not (options.in_place or options.diff):
parser.error('autopep8 only takes one filename as argument '
'unless the "--in-place" or "--diff" options are '
'used')
if options.recursive and not (options.in_place or options.diff):
parser.error('--recursive must be used with --in-place or --diff')
if options.exclude and not options.recursive:
parser.error('--exclude is only relevant when used with --recursive')
if options.in_place and options.diff:
parser.error('--in-place and --diff are mutually exclusive')
if options.max_line_length <= 0:
parser.error('--max-line-length must be greater than 0')
if options.select:
options.select = options.select.split(',')
if options.ignore:
options.ignore = options.ignore.split(',')
elif not options.select:
if options.aggressive:
# Enable everything by default if aggressive.
options.select = ['E', 'W']
else:
options.ignore = DEFAULT_IGNORE.split(',')
if options.exclude:
options.exclude = options.exclude.split(',')
else:
options.exclude = []
if options.jobs < 1:
# Do not import multiprocessing globally in case it is not supported
# on the platform.
import multiprocessing
options.jobs = multiprocessing.cpu_count()
if options.jobs > 1 and not options.in_place:
parser.error('parallel jobs requires --in-place')
return options, args
def supported_fixes():
"""Yield pep8 error codes that autopep8 fixes.
Each item we yield is a tuple of the code followed by its description.
"""
instance = FixPEP8(filename=None, options=None, contents='')
for attribute in dir(instance):
code = re.match('fix_([ew][0-9][0-9][0-9])', attribute)
if code:
yield (code.group(1).upper(),
re.sub(r'\s+', ' ',
getattr(instance, attribute).__doc__))
for (code, function) in sorted(global_fixes()):
yield (code.upper() + (4 - len(code)) * ' ',
re.sub(r'\s+', ' ', function.__doc__))
def line_shortening_rank(candidate, newline, indent_word):
"""Return rank of candidate.
This is for sorting candidates.
"""
rank = 0
if candidate.strip():
lines = candidate.split(newline)
offset = 0
if lines[0].rstrip()[-1] not in '([{':
for symbol in '([{':
offset = max(offset, 1 + lines[0].find(symbol))
max_length = max([offset + len(x.strip()) for x in lines])
rank += max_length
rank += len(lines)
bad_staring_symbol = {
'(': ')',
'[': ']',
'{': '}'}.get(lines[0][-1], None)
if len(lines) > 1:
if (bad_staring_symbol and
lines[1].lstrip().startswith(bad_staring_symbol)):
rank += 20
else:
rank -= 10
if lines[0].endswith('(['):
rank += 10
for current_line in lines:
for bad_start in ['.', '%', '+', '-', '/']:
if current_line.startswith(bad_start):
rank += 100
for ending in '([{':
# Avoid lonely opening. They result in longer lines.
if (current_line.endswith(ending) and
len(current_line.strip()) <= len(indent_word)):
rank += 100
if current_line.endswith('%'):
rank -= 20
# Try to break list comprehensions at the "for".
if current_line.lstrip().startswith('for'):
rank -= 50
rank += 10 * count_unbalanced_brackets(current_line)
else:
rank = 100000
return max(0, rank)
def count_unbalanced_brackets(line):
"""Return number of unmatched open/close brackets."""
count = 0
for opening, closing in ['()', '[]', '{}']:
count += abs(line.count(opening) - line.count(closing))
return count
def split_at_offsets(line, offsets):
"""Split line at offsets.
Return list of strings.
"""
result = []
previous_offset = 0
current_offset = 0
for current_offset in sorted(offsets):
if current_offset < len(line) and previous_offset != current_offset:
result.append(line[previous_offset:current_offset])
previous_offset = current_offset
result.append(line[current_offset:])
return result
def get_longest_length(text, newline):
"""Return length of longest line."""
return max([len(line) for line in text.split(newline)])
class LineEndingWrapper(object):
r"""Replace line endings to work with sys.stdout.
It seems that sys.stdout expects only '\n' as the line ending, no matter
the platform. Otherwise, we get repeated line endings.
"""
def __init__(self, output):
self.__output = output
def write(self, s):
self.__output.write(s.replace('\r\n', '\n').replace('\r', '\n'))
def flush(self):
self.__output.flush()
def match_file(filename, exclude):
"""Return True if file is okay for modifying/recursing."""
if os.path.basename(filename).startswith('.'):
return False
for pattern in exclude:
if fnmatch.fnmatch(filename, pattern):
return False
if not is_python_file(filename):
return False
return True
def find_files(filenames, recursive, exclude):
"""Yield filenames."""
while filenames:
name = filenames.pop(0)
if recursive and os.path.isdir(name):
for root, directories, children in os.walk(name):
filenames += [os.path.join(root, f) for f in children
if match_file(f, exclude)]
directories[:] = [d for d in directories
if not d.startswith('.')]
else:
yield name
def _fix_file(parameters):
"""Helper function for optionally running fix_file() in parallel."""
if parameters[1].verbose:
print('[file:{0}]'.format(parameters[0]), file=sys.stderr)
try:
fix_file(*parameters)
except IOError as error:
print(str(error), file=sys.stderr)
def fix_multiple_files(filenames, options, output=None):
"""Fix list of files.
Optionally fix files recursively.
"""
filenames = find_files(filenames, options.recursive, options.exclude)
if options.jobs > 1:
import multiprocessing
pool = multiprocessing.Pool(options.jobs)
pool.map(_fix_file,
[(name, options) for name in filenames])
else:
for name in filenames:
_fix_file((name, options, output))
def is_python_file(filename):
"""Return True if filename is Python file."""
if filename.endswith('.py'):
return True
try:
with open_with_encoding(filename) as f:
first_line = f.readlines(1)[0]
except (IOError, IndexError):
return False
if len(first_line) > 200:
# This is probably not even a text file.
return False
if not PYTHON_SHEBANG_REGEX.match(first_line):
return False
return True
def main():
"""Tool main."""
try:
# Exit on broken pipe.
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
except AttributeError: # pragma: no cover
# SIGPIPE is not available on Windows.
pass
try:
options, args = parse_args(sys.argv[1:])
if options.list_fixes:
for code, description in supported_fixes():
print('{code} - {description}'.format(
code=code, description=description))
return 0
if args == ['-']:
assert not options.in_place
# LineEndingWrapper is unnecessary here due to the symmetry between
# standard in and standard out.
sys.stdout.write(fix_string(sys.stdin.read(),
options))
else:
if options.in_place or options.diff:
filenames = list(set(args))
else:
assert len(args) == 1
assert not options.recursive
filenames = args[:1]
fix_multiple_files(filenames, options, sys.stdout)
except KeyboardInterrupt:
return 1 # pragma: no cover
if __name__ == '__main__':
sys.exit(main())
autopep8-0.9.1/PKG-INFO 0000664 0001750 0001750 00000030760 12153410305 015533 0 ustar hattori hattori 0000000 0000000 Metadata-Version: 1.1
Name: autopep8
Version: 0.9.1
Summary: A tool that automatically formats Python code to conform to the PEP 8 style guide
Home-page: https://github.com/hhatto/autopep8
Author: Hideo Hattori
Author-email: hhatto.jp@gmail.com
License: Expat License
Description: ========
autopep8
========
.. image:: https://travis-ci.org/hhatto/autopep8.png?branch=master
:target: https://travis-ci.org/hhatto/autopep8
:alt: Build status
.. image:: https://coveralls.io/repos/hhatto/autopep8/badge.png?branch=master
:target: https://coveralls.io/r/hhatto/autopep8
:alt: Test coverage status
About
=====
autopep8 automatically formats Python code to conform to the `PEP 8`_ style
guide. It uses the pep8_ utility to determine what parts of the code needs to
be formatted. autopep8 is capable of fixing most of the formatting issues_ that
can be reported by pep8.
.. _PEP 8: http://www.python.org/dev/peps/pep-0008
.. _issues: https://pep8.readthedocs.org/en/latest/intro.html#error-codes
Installation
============
From pip::
$ pip install --upgrade autopep8
From easy_install::
$ easy_install -ZU autopep8
Requirements
============
autopep8 requires pep8_ (>= 1.4.5).
.. _pep8: https://github.com/jcrocholl/pep8
Usage
=====
To modify a file in place (with all fixes enabled)::
$ autopep8 --in-place --aggressive
Before running autopep8.
.. code-block:: python
import sys, os;
def someone_likes_semicolons( foo = None ,\
bar='bar'):
"""Hello; bye."""; print( 'A'<>foo) #<> is a deprecated form of !=
return 0;
def func11():
a=( 1,2, 3,"a" );
####This is a long comment. This should be wrapped to fit within 72 characters.
x = [a,[100,200,300,9876543210,'This is a long string that goes on and on']]
def func22(): return {True: True}.has_key({'foo': 2}.has_key('foo'));
class UselessClass( object ):
def __init__ ( self, bar ):
#Comments should have a space after the hash.
if bar : bar+=1; bar=bar* bar ; return bar
else:
indentation_in_strings_should_not_be_touched = """
hello
world
"""
raise ValueError, indentation_in_strings_should_not_be_touched
def my_method(self):
print(self);
After running autopep8.
.. code-block:: python
import sys
import os
def someone_likes_semicolons(foo=None,
bar='bar'):
"""Hello; bye."""
print('A' != foo) # <> is a deprecated form of !=
return 0
def func11():
a = (1, 2, 3, "a")
# This is a long comment. This should be wrapped to fit within 72
# characters.
x = [a, [100, 200, 300, 9876543210,
'This is a long string that goes on and on']]
def func22():
return ('foo' in {'foo': 2}) in {True: True}
class UselessClass(object):
def __init__(self, bar):
# Comments should have a space after the hash.
if bar:
bar += 1
bar = bar * bar
return bar
else:
indentation_in_strings_should_not_be_touched = """
hello
world
"""
raise ValueError(indentation_in_strings_should_not_be_touched)
def my_method(self):
print(self)
Options::
Usage: autopep8 [options] [filename [filename ...]]
Use filename '-' for stdin.
Automatically formats Python code to conform to the PEP 8 style guide.
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose print verbose messages; multiple -v result in more
verbose messages
-d, --diff print the diff for the fixed source
-i, --in-place make changes to files in place
-r, --recursive run recursively; must be used with --in-place or
--diff
-j n, --jobs=n number of parallel jobs; match CPU count if value is
less than 1
-p n, --pep8-passes=n
maximum number of additional pep8 passes (default:
infinite)
-a, --aggressive enable non-whitespace changes; multiple -a result in
more aggressive changes
--exclude=globs exclude files/directories that match these comma-
separated globs
--list-fixes list codes for fixes; used by --ignore and --select
--ignore=errors do not fix these errors/warnings (default: E24,W6)
--select=errors fix only these errors/warnings (e.g. E4,W)
--max-line-length=n set maximum allowed line length (default: 79)
Features
========
autopep8 fixes the following issues_ reported by pep8_::
E101 - Reindent all lines.
E111 - Reindent all lines.
E121 - Fix indentation to be a multiple of four.
E122 - Add absent indentation for hanging indentation.
E123 - Align closing bracket to match opening bracket.
E124 - Align closing bracket to match visual indentation.
E125 - Indent to distinguish line from next logical line.
E126 - Fix over-indented hanging indentation.
E127 - Fix visual indentation.
E128 - Fix visual indentation.
E20 - Remove extraneous whitespace.
E211 - Remove extraneous whitespace.
E22 - Fix extraneous whitespace around keywords.
E224 - Remove extraneous whitespace around operator.
E22 - Fix missing whitespace around operator.
E231 - Add missing whitespace.
E241 - Fix extraneous whitespace around keywords.
E242 - Remove extraneous whitespace around operator.
E251 - Remove whitespace around parameter '=' sign.
E26 - Fix spacing after comment hash.
E27 - Fix extraneous whitespace around keywords.
E301 - Add missing blank line.
E302 - Add missing 2 blank lines.
E303 - Remove extra blank lines.
E304 - Remove blank line following function decorator.
E401 - Put imports on separate lines.
E501 - Try to make lines fit within --max-line-length characters.
E502 - Remove extraneous escape of newline.
E701 - Put colon-separated compound statement on separate lines.
E70 - Put semicolon-separated compound statement on separate lines.
E711 - Fix comparison with None.
E712 - Fix comparison with boolean.
W191 - Reindent all lines.
W291 - Remove trailing whitespace.
W293 - Remove trailing whitespace on blank line.
W391 - Remove trailing blank lines.
E26 - Format block comments.
W6 - Fix various deprecated code (via lib2to3).
W602 - Fix deprecated form of raising exception.
autopep8 also fixes some issues not found by pep8_.
- Correct deprecated or non-idiomatic Python code (via ``lib2to3``). (This is
triggered if ``W6`` is enabled.)
- Format block comments. (This is triggered if ``E26`` is enabled.)
- Normalize files with mixed line endings.
- Put a blank line between a class declaration and its first method
declaration. (Enabled with ``E301``.)
- Remove blank lines between a function declaration and its docstring. (Enabled
with ``E303``.)
More advanced usage
===================
To enable only a subset of the fixes, use the ``--select`` option. For example,
to fix various types of indentation issues::
$ autopep8 --select=E1,W1
Similarly, to just fix deprecated code::
$ autopep8 --select=W6
The above is useful when trying to port a single code base to work with both
Python 2 and Python 3 at the same time.
If the file being fixed is large, you may want to enable verbose progress
messages::
$ autopep8 -v
By default autopep8 only makes whitespace changes. Thus, by default, it does
not fix ``E711`` and ``E712``. (Changing ``x == None`` to ``x is None`` may
change the meaning of the program if ``x`` has its ``__eq__`` method
overridden.) Nor does it correct deprecated code ``W6``. To enable these
more aggressive fixes, use the ``--aggressive`` option::
$ autopep8 --aggressive
``--aggressive`` will also shorten lines more aggressively.
Use as a module
===============
The simplest way of using autopep8 as a module is via the ``fix_string()``
function.
.. code-block:: python
>>> import autopep8
>>> autopep8.fix_string('x= 123\n')
'x = 123\n'
Testing
=======
Test cases are in ``test/test_autopep8.py``. They can be run directly via
``python test/test_autopep8.py`` or via tox_. The latter is useful for
testing against multiple Python interpreters. (We currently test against
CPython versions 2.6, 2.7, 3.2, and 3.3. We also test against PyPy.)
.. _`tox`: https://pypi.python.org/pypi/tox
Broad spectrum testing is available via ``test/acid.py``. This script runs
autopep8 against Python code and checks for correctness and completeness of the
code fixes. It can check that the bytecode remains identical.
``test/acid_pypi.py`` makes use of ``acid.py`` to test against the latest
released packages on PyPI. In a similar fashion, ``test/acid_github.py`` tests
against Python code in Github repositories.
Links
=====
* PyPI_
* GitHub_
* `Travis CI`_
* Jenkins_
.. _PyPI: https://pypi.python.org/pypi/autopep8/
.. _GitHub: https://github.com/hhatto/autopep8
.. _`Travis CI`: https://travis-ci.org/hhatto/autopep8
.. _Jenkins: http://jenkins.hexacosa.net/job/autopep8/
Keywords: automation,pep8,format
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.6
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.2
Classifier: Programming Language :: Python :: 3.3
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Quality Assurance
autopep8-0.9.1/setup.cfg 0000664 0001750 0001750 00000000073 12153410305 016251 0 ustar hattori hattori 0000000 0000000 [egg_info]
tag_build =
tag_date = 0
tag_svn_revision = 0
autopep8-0.9.1/autopep8.egg-info/ 0000775 0001750 0001750 00000000000 12153410305 017667 5 ustar hattori hattori 0000000 0000000 autopep8-0.9.1/autopep8.egg-info/dependency_links.txt 0000664 0001750 0001750 00000000001 12153410305 023735 0 ustar hattori hattori 0000000 0000000
autopep8-0.9.1/autopep8.egg-info/entry_points.txt 0000664 0001750 0001750 00000000054 12153410305 023164 0 ustar hattori hattori 0000000 0000000 [console_scripts]
autopep8 = autopep8:main
autopep8-0.9.1/autopep8.egg-info/top_level.txt 0000664 0001750 0001750 00000000011 12153410305 022411 0 ustar hattori hattori 0000000 0000000 autopep8
autopep8-0.9.1/autopep8.egg-info/PKG-INFO 0000664 0001750 0001750 00000030760 12153410305 020772 0 ustar hattori hattori 0000000 0000000 Metadata-Version: 1.1
Name: autopep8
Version: 0.9.1
Summary: A tool that automatically formats Python code to conform to the PEP 8 style guide
Home-page: https://github.com/hhatto/autopep8
Author: Hideo Hattori
Author-email: hhatto.jp@gmail.com
License: Expat License
Description: ========
autopep8
========
.. image:: https://travis-ci.org/hhatto/autopep8.png?branch=master
:target: https://travis-ci.org/hhatto/autopep8
:alt: Build status
.. image:: https://coveralls.io/repos/hhatto/autopep8/badge.png?branch=master
:target: https://coveralls.io/r/hhatto/autopep8
:alt: Test coverage status
About
=====
autopep8 automatically formats Python code to conform to the `PEP 8`_ style
guide. It uses the pep8_ utility to determine what parts of the code needs to
be formatted. autopep8 is capable of fixing most of the formatting issues_ that
can be reported by pep8.
.. _PEP 8: http://www.python.org/dev/peps/pep-0008
.. _issues: https://pep8.readthedocs.org/en/latest/intro.html#error-codes
Installation
============
From pip::
$ pip install --upgrade autopep8
From easy_install::
$ easy_install -ZU autopep8
Requirements
============
autopep8 requires pep8_ (>= 1.4.5).
.. _pep8: https://github.com/jcrocholl/pep8
Usage
=====
To modify a file in place (with all fixes enabled)::
$ autopep8 --in-place --aggressive
Before running autopep8.
.. code-block:: python
import sys, os;
def someone_likes_semicolons( foo = None ,\
bar='bar'):
"""Hello; bye."""; print( 'A'<>foo) #<> is a deprecated form of !=
return 0;
def func11():
a=( 1,2, 3,"a" );
####This is a long comment. This should be wrapped to fit within 72 characters.
x = [a,[100,200,300,9876543210,'This is a long string that goes on and on']]
def func22(): return {True: True}.has_key({'foo': 2}.has_key('foo'));
class UselessClass( object ):
def __init__ ( self, bar ):
#Comments should have a space after the hash.
if bar : bar+=1; bar=bar* bar ; return bar
else:
indentation_in_strings_should_not_be_touched = """
hello
world
"""
raise ValueError, indentation_in_strings_should_not_be_touched
def my_method(self):
print(self);
After running autopep8.
.. code-block:: python
import sys
import os
def someone_likes_semicolons(foo=None,
bar='bar'):
"""Hello; bye."""
print('A' != foo) # <> is a deprecated form of !=
return 0
def func11():
a = (1, 2, 3, "a")
# This is a long comment. This should be wrapped to fit within 72
# characters.
x = [a, [100, 200, 300, 9876543210,
'This is a long string that goes on and on']]
def func22():
return ('foo' in {'foo': 2}) in {True: True}
class UselessClass(object):
def __init__(self, bar):
# Comments should have a space after the hash.
if bar:
bar += 1
bar = bar * bar
return bar
else:
indentation_in_strings_should_not_be_touched = """
hello
world
"""
raise ValueError(indentation_in_strings_should_not_be_touched)
def my_method(self):
print(self)
Options::
Usage: autopep8 [options] [filename [filename ...]]
Use filename '-' for stdin.
Automatically formats Python code to conform to the PEP 8 style guide.
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose print verbose messages; multiple -v result in more
verbose messages
-d, --diff print the diff for the fixed source
-i, --in-place make changes to files in place
-r, --recursive run recursively; must be used with --in-place or
--diff
-j n, --jobs=n number of parallel jobs; match CPU count if value is
less than 1
-p n, --pep8-passes=n
maximum number of additional pep8 passes (default:
infinite)
-a, --aggressive enable non-whitespace changes; multiple -a result in
more aggressive changes
--exclude=globs exclude files/directories that match these comma-
separated globs
--list-fixes list codes for fixes; used by --ignore and --select
--ignore=errors do not fix these errors/warnings (default: E24,W6)
--select=errors fix only these errors/warnings (e.g. E4,W)
--max-line-length=n set maximum allowed line length (default: 79)
Features
========
autopep8 fixes the following issues_ reported by pep8_::
E101 - Reindent all lines.
E111 - Reindent all lines.
E121 - Fix indentation to be a multiple of four.
E122 - Add absent indentation for hanging indentation.
E123 - Align closing bracket to match opening bracket.
E124 - Align closing bracket to match visual indentation.
E125 - Indent to distinguish line from next logical line.
E126 - Fix over-indented hanging indentation.
E127 - Fix visual indentation.
E128 - Fix visual indentation.
E20 - Remove extraneous whitespace.
E211 - Remove extraneous whitespace.
E22 - Fix extraneous whitespace around keywords.
E224 - Remove extraneous whitespace around operator.
E22 - Fix missing whitespace around operator.
E231 - Add missing whitespace.
E241 - Fix extraneous whitespace around keywords.
E242 - Remove extraneous whitespace around operator.
E251 - Remove whitespace around parameter '=' sign.
E26 - Fix spacing after comment hash.
E27 - Fix extraneous whitespace around keywords.
E301 - Add missing blank line.
E302 - Add missing 2 blank lines.
E303 - Remove extra blank lines.
E304 - Remove blank line following function decorator.
E401 - Put imports on separate lines.
E501 - Try to make lines fit within --max-line-length characters.
E502 - Remove extraneous escape of newline.
E701 - Put colon-separated compound statement on separate lines.
E70 - Put semicolon-separated compound statement on separate lines.
E711 - Fix comparison with None.
E712 - Fix comparison with boolean.
W191 - Reindent all lines.
W291 - Remove trailing whitespace.
W293 - Remove trailing whitespace on blank line.
W391 - Remove trailing blank lines.
E26 - Format block comments.
W6 - Fix various deprecated code (via lib2to3).
W602 - Fix deprecated form of raising exception.
autopep8 also fixes some issues not found by pep8_.
- Correct deprecated or non-idiomatic Python code (via ``lib2to3``). (This is
triggered if ``W6`` is enabled.)
- Format block comments. (This is triggered if ``E26`` is enabled.)
- Normalize files with mixed line endings.
- Put a blank line between a class declaration and its first method
declaration. (Enabled with ``E301``.)
- Remove blank lines between a function declaration and its docstring. (Enabled
with ``E303``.)
More advanced usage
===================
To enable only a subset of the fixes, use the ``--select`` option. For example,
to fix various types of indentation issues::
$ autopep8 --select=E1,W1
Similarly, to just fix deprecated code::
$ autopep8 --select=W6
The above is useful when trying to port a single code base to work with both
Python 2 and Python 3 at the same time.
If the file being fixed is large, you may want to enable verbose progress
messages::
$ autopep8 -v
By default autopep8 only makes whitespace changes. Thus, by default, it does
not fix ``E711`` and ``E712``. (Changing ``x == None`` to ``x is None`` may
change the meaning of the program if ``x`` has its ``__eq__`` method
overridden.) Nor does it correct deprecated code ``W6``. To enable these
more aggressive fixes, use the ``--aggressive`` option::
$ autopep8 --aggressive
``--aggressive`` will also shorten lines more aggressively.
Use as a module
===============
The simplest way of using autopep8 as a module is via the ``fix_string()``
function.
.. code-block:: python
>>> import autopep8
>>> autopep8.fix_string('x= 123\n')
'x = 123\n'
Testing
=======
Test cases are in ``test/test_autopep8.py``. They can be run directly via
``python test/test_autopep8.py`` or via tox_. The latter is useful for
testing against multiple Python interpreters. (We currently test against
CPython versions 2.6, 2.7, 3.2, and 3.3. We also test against PyPy.)
.. _`tox`: https://pypi.python.org/pypi/tox
Broad spectrum testing is available via ``test/acid.py``. This script runs
autopep8 against Python code and checks for correctness and completeness of the
code fixes. It can check that the bytecode remains identical.
``test/acid_pypi.py`` makes use of ``acid.py`` to test against the latest
released packages on PyPI. In a similar fashion, ``test/acid_github.py`` tests
against Python code in Github repositories.
Links
=====
* PyPI_
* GitHub_
* `Travis CI`_
* Jenkins_
.. _PyPI: https://pypi.python.org/pypi/autopep8/
.. _GitHub: https://github.com/hhatto/autopep8
.. _`Travis CI`: https://travis-ci.org/hhatto/autopep8
.. _Jenkins: http://jenkins.hexacosa.net/job/autopep8/
Keywords: automation,pep8,format
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.6
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.2
Classifier: Programming Language :: Python :: 3.3
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Quality Assurance
autopep8-0.9.1/autopep8.egg-info/not-zip-safe 0000664 0001750 0001750 00000000001 12153410301 022111 0 ustar hattori hattori 0000000 0000000
autopep8-0.9.1/autopep8.egg-info/requires.txt 0000664 0001750 0001750 00000000015 12153410305 022263 0 ustar hattori hattori 0000000 0000000 pep8 >= 1.4.5 autopep8-0.9.1/autopep8.egg-info/SOURCES.txt 0000664 0001750 0001750 00000000626 12153410305 021557 0 ustar hattori hattori 0000000 0000000 MANIFEST.in
README.rst
autopep8.py
setup.py
autopep8.egg-info/PKG-INFO
autopep8.egg-info/SOURCES.txt
autopep8.egg-info/dependency_links.txt
autopep8.egg-info/entry_points.txt
autopep8.egg-info/not-zip-safe
autopep8.egg-info/requires.txt
autopep8.egg-info/top_level.txt
test/__init__.py
test/bad_encoding.py
test/bad_encoding2.py
test/e101_example.py
test/example.py
test/iso_8859_1.py
test/test_autopep8.py