Skip to content

Commit

Permalink
pep8-fy the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekziade committed Aug 17, 2015
1 parent 1073ffe commit a22705e
Show file tree
Hide file tree
Showing 47 changed files with 715 additions and 492 deletions.
5 changes: 4 additions & 1 deletion Makefile
Expand Up @@ -22,7 +22,7 @@ endif
# auslib/util/testing.py.
NOSE_ARGS += auslib/test

ALL_PY_FILES := $(shell find . \( -iname "*.py" -or -iname "*.wsgi" \) -not -path "*vendor*")
ALL_PY_FILES := $(shell find . \( -iname "*.py" -or -iname "*.wsgi" \) -not -path "*vendor*" -not -path "./lib*")

PYTHON_ENV = PYTHONDONTWRITEBYTECODE=1 PYTHONPATH=$(PYTHONPATH)
# Used to run the tests. Useful for both CI-driven tests and manual ones.
Expand Down Expand Up @@ -62,4 +62,7 @@ ci-tests: virtualenv test-in-virtualenv
pyflakes: $(ALL_PY_FILES)
pyflakes $(ALL_PY_FILES)

pep8: $(ALL_PY_FILES)
@pep8 --ignore E501,E711 $(ALL_PY_FILES)

.PHONY: FORCE
4 changes: 3 additions & 1 deletion admin.py
Expand Up @@ -10,6 +10,7 @@


class APIMiddleware(object):

def __init__(self, wrap_app):
self.wrap_app = wrap_app

Expand Down Expand Up @@ -39,7 +40,7 @@ def __call__(self, environ, start_response):
parser.add_option("--cef-log", dest="cefLog", default="cef.log")
parser.add_option("--page-title", dest="pageTitle", default="AUS Management")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
help="Verbose output")
help="Verbose output")
options, args = parser.parse_args()

# Logging needs to get set-up before importing the application
Expand Down Expand Up @@ -68,6 +69,7 @@ def __call__(self, environ, start_response):
app.config['SECRET_KEY'] = 'abc123'
app.config['DEBUG'] = True
app.config['PAGE_TITLE'] = options.pageTitle

def auth(environ, username, password):
return username == password
# The Angular app always makes requests to "/api". The WSGI app that runs
Expand Down
11 changes: 8 additions & 3 deletions auslib/AUS.py
Expand Up @@ -15,20 +15,23 @@ def isSpecialURL(url, specialForceHosts):
return True
return False


def isForbiddenUrl(url, whitelistedDomains):
domain = urlparse(url)[1]
if domain not in whitelistedDomains:
cef_event("Forbidden domain", CEF_ALERT, domain=domain)
return True
return False


def getFallbackChannel(channel):
return channel.split('-cck-')[0]


class AUSRandom:
"""Abstract getting a randint to make it easier to test the range of
possible values"""

def __init__(self, min=0, max=99):
self.min = min
self.max = max
Expand All @@ -37,9 +40,11 @@ def getInt(self):
return randint(self.min, self.max)

def getRange(self):
return range(self.min, self.max+1)
return range(self.min, self.max + 1)


class AUS:

def __init__(self):
self.specialForceHosts = None
self.rand = AUSRandom()
Expand All @@ -53,11 +58,11 @@ def evaluateRules(self, updateQuery):
fallbackChannel=getFallbackChannel(updateQuery['channel'])
)

### XXX throw any N->N update rules and keep the highest priority remaining one
# XXX throw any N->N update rules and keep the highest priority remaining one
if len(rules) < 1:
return None, None

rules = sorted(rules,key=lambda rule: rule['priority'], reverse=True)
rules = sorted(rules, key=lambda rule: rule['priority'], reverse=True)
rule = rules[0]
self.log.debug("Matching rule: %s" % rule)

Expand Down
11 changes: 7 additions & 4 deletions auslib/admin/base.py
Expand Up @@ -10,14 +10,15 @@

from auslib.admin.views.csrf import CSRFView
from auslib.admin.views.permissions import UsersView, PermissionsView, \
SpecificPermissionView
SpecificPermissionView
from auslib.admin.views.releases import SingleLocaleView, \
SingleReleaseView, ReleaseHistoryView, \
ReleasesAPIView
SingleReleaseView, ReleaseHistoryView, \
ReleasesAPIView
from auslib.admin.views.rules import RulesAPIView, \
SingleRuleView, RuleHistoryAPIView
SingleRuleView, RuleHistoryAPIView
from auslib.admin.views.history import DiffView, FieldView


@app.errorhandler(500)
def isa(error):
log.error("Caught ISE 500 error.")
Expand All @@ -28,6 +29,8 @@ def isa(error):
return error

# bug 887790: add necessary security headers


@app.after_request
def add_security_headers(response):
response.headers['X-Frame-Options'] = 'DENY'
Expand Down
9 changes: 6 additions & 3 deletions auslib/admin/views/base.py
Expand Up @@ -9,6 +9,7 @@

import logging


def requirelogin(f):
def decorated(*args, **kwargs):
username = request.environ.get('REMOTE_USER')
Expand All @@ -18,6 +19,7 @@ def decorated(*args, **kwargs):
return f(*args, changed_by=username, **kwargs)
return decorated


def requirepermission(url, options=['product']):
def wrap(f):
def decorated(*args, **kwargs):
Expand All @@ -41,7 +43,9 @@ def decorated(*args, **kwargs):
return decorated
return wrap


class AdminView(MethodView):

def __init__(self, *args, **kwargs):
self.log = logging.getLogger(self.__class__.__name__)
MethodView.__init__(self, *args, **kwargs)
Expand All @@ -68,11 +72,10 @@ class HistoryAdminView(AdminView):

def getAllRevisionKeys(self, revisions, primary_keys):
try:
all_keys = [
all_keys = sorted([
x for x in revisions[0].keys()
if x not in self.history_keys and x not in primary_keys
]
all_keys.sort()
])
except IndexError:
all_keys = None
return all_keys
Expand Down
3 changes: 3 additions & 0 deletions auslib/admin/views/csrf.py
Expand Up @@ -5,13 +5,16 @@

__all__ = ["CSRFView"]


def get_csrf_headers():
# Instantiating a Form makes sure there's a CSRF token available
# and puts an hmac key in the session.
form = Form()
return {'X-CSRF-Token': form.csrf_token._value()}


class CSRFView(AdminView):
"""/csrf_token"""

def get(self):
return Response(headers=get_csrf_headers())
68 changes: 41 additions & 27 deletions auslib/admin/views/forms.py
Expand Up @@ -8,11 +8,14 @@
import logging
log = logging.getLogger(__name__)


class DisableableTextInput(TextInput):
"""A TextInput widget that supports being disabled."""

def __init__(self, disabled, *args, **kwargs):
self.disabled = disabled
TextInput.__init__(self, *args, **kwargs)

def __call__(self, *args, **kwargs):
if self.disabled:
kwargs['disabled'] = 'disabled'
Expand All @@ -21,12 +24,13 @@ def __call__(self, *args, **kwargs):

class JSONStringField(StringField):
"""StringField that parses incoming data as JSON."""

def process_formdata(self, valuelist):
if valuelist and valuelist[0]:
try:
self.data = json.loads(valuelist[0])
# XXX: use JSONDecodeError when the servers support it
except ValueError, e:
except ValueError as e:
# WTForms catches ValueError, which JSONDecodeError is a child
# of. Because of this, we need to wrap this error in something
# else in order for it to be properly raised.
Expand All @@ -45,6 +49,7 @@ def _value(self):

class NullableStringField(StringField):
"""StringField that parses incoming data converting empty strings to None's."""

def process_formdata(self, valuelist):
if valuelist and valuelist[0]:
if valuelist[0] == '':
Expand All @@ -56,6 +61,7 @@ def process_formdata(self, valuelist):
log.debug('No value list, setting self.data to None')
self.data = None


def NoneOrType(type_):
"""A helper method for SelectField's that returns the value coerced to
the specified type when it is not None. By default, a SelectField coerces
Expand All @@ -67,18 +73,23 @@ def coercer(value):
return type_(value)
return coercer


class DbEditableForm(Form):
data_version = IntegerField('data_version', validators=[Required()], widget=HiddenInput())


class PermissionForm(DbEditableForm):
options = JSONStringField('Options')


class NewPermissionForm(PermissionForm):
permission = StringField('Permission', validators=[Required()])


class ExistingPermissionForm(PermissionForm):
permission = StringField('Permission', validators=[Required()], widget=DisableableTextInput(disabled=True))


class PartialReleaseForm(Form):
# Because we do implicit release creation in the Releases views, we can't
# have data_version be Required(). The views are responsible for checking
Expand All @@ -92,39 +103,42 @@ class PartialReleaseForm(Form):
copyTo = JSONStringField('Copy To', default=list)
alias = JSONStringField('Alias', default=list)


class RuleForm(Form):
backgroundRate = IntegerField('Background Rate', validators=[Required(), NumberRange(0, 100) ])
backgroundRate = IntegerField('Background Rate', validators=[Required(), NumberRange(0, 100)])
priority = IntegerField('Priority', validators=[Required()])
mapping = SelectField('Mapping', validators=[])
product = NullableStringField('Product', validators=[Length(0, 15)] )
version = NullableStringField('Version', validators=[Length(0,10) ])
buildID = NullableStringField('BuildID', validators=[Length(0,20) ])
channel = NullableStringField('Channel', validators=[Length(0,75) ])
locale = NullableStringField('Locale', validators=[Length(0,200) ])
distribution = NullableStringField('Distribution', validators=[Length(0,100) ])
buildTarget = NullableStringField('Build Target', validators=[Length(0,75) ])
osVersion = NullableStringField('OS Version', validators=[Length(0,1000) ])
distVersion = NullableStringField('Dist Version', validators=[Length(0,100) ])
comment = NullableStringField('Comment', validators=[Length(0,500) ])
update_type = SelectField('Update Type', choices=[('minor','minor'), ('major', 'major')], validators=[])
headerArchitecture = NullableStringField('Header Architecture', validators=[Length(0,10) ])
product = NullableStringField('Product', validators=[Length(0, 15)])
version = NullableStringField('Version', validators=[Length(0, 10)])
buildID = NullableStringField('BuildID', validators=[Length(0, 20)])
channel = NullableStringField('Channel', validators=[Length(0, 75)])
locale = NullableStringField('Locale', validators=[Length(0, 200)])
distribution = NullableStringField('Distribution', validators=[Length(0, 100)])
buildTarget = NullableStringField('Build Target', validators=[Length(0, 75)])
osVersion = NullableStringField('OS Version', validators=[Length(0, 1000)])
distVersion = NullableStringField('Dist Version', validators=[Length(0, 100)])
comment = NullableStringField('Comment', validators=[Length(0, 500)])
update_type = SelectField('Update Type', choices=[('minor', 'minor'), ('major', 'major')], validators=[])
headerArchitecture = NullableStringField('Header Architecture', validators=[Length(0, 10)])


class EditRuleForm(DbEditableForm):
backgroundRate = IntegerField('Background Rate', validators=[Optional(), NumberRange(0, 100) ])
backgroundRate = IntegerField('Background Rate', validators=[Optional(), NumberRange(0, 100)])
priority = IntegerField('Priority', validators=[Optional()])
mapping = SelectField('Mapping', validators=[Optional()], coerce=NoneOrType(unicode))
product = NullableStringField('Product', validators=[Optional(), Length(0, 15)] )
version = NullableStringField('Version', validators=[Optional(), Length(0,10) ])
buildID = NullableStringField('BuildID', validators=[Optional(), Length(0,20) ])
channel = NullableStringField('Channel', validators=[Optional(), Length(0,75) ])
locale = NullableStringField('Locale', validators=[Optional(), Length(0,200) ])
distribution = NullableStringField('Distribution', validators=[Optional(), Length(0,100) ])
buildTarget = NullableStringField('Build Target', validators=[Optional(), Length(0,75) ])
osVersion = NullableStringField('OS Version', validators=[Optional(), Length(0,1000) ])
distVersion = NullableStringField('Dist Version', validators=[Optional(), Length(0,100) ])
comment = NullableStringField('Comment', validators=[Optional(), Length(0,500) ])
update_type = SelectField('Update Type', choices=[('minor','minor'), ('major', 'major')], validators=[Optional()], coerce=NoneOrType(unicode))
headerArchitecture = NullableStringField('Header Architecture', validators=[Optional(), Length(0,10) ])
product = NullableStringField('Product', validators=[Optional(), Length(0, 15)])
version = NullableStringField('Version', validators=[Optional(), Length(0, 10)])
buildID = NullableStringField('BuildID', validators=[Optional(), Length(0, 20)])
channel = NullableStringField('Channel', validators=[Optional(), Length(0, 75)])
locale = NullableStringField('Locale', validators=[Optional(), Length(0, 200)])
distribution = NullableStringField('Distribution', validators=[Optional(), Length(0, 100)])
buildTarget = NullableStringField('Build Target', validators=[Optional(), Length(0, 75)])
osVersion = NullableStringField('OS Version', validators=[Optional(), Length(0, 1000)])
distVersion = NullableStringField('Dist Version', validators=[Optional(), Length(0, 100)])
comment = NullableStringField('Comment', validators=[Optional(), Length(0, 500)])
update_type = SelectField('Update Type', choices=[('minor', 'minor'), ('major', 'major')], validators=[Optional()], coerce=NoneOrType(unicode))
headerArchitecture = NullableStringField('Header Architecture', validators=[Optional(), Length(0, 10)])


class CompleteReleaseForm(Form):
name = StringField('Name', validators=[Required()])
Expand Down
5 changes: 3 additions & 2 deletions auslib/admin/views/history.py
Expand Up @@ -43,17 +43,18 @@ def format_value(self, value):
def get(self, type_, change_id, field):
try:
value = self.get_value(type_, change_id, field)
except KeyError, msg:
except KeyError as msg:
cef_event("Bad input", CEF_WARN, errors=str(msg), field=field)
return Response(status=400, response=str(msg))
except ValueError, msg:
except ValueError as msg:
return Response(status=404, response=str(msg))
value = self.format_value(value)
return Response(value, content_type='text/plain')


class DiffView(FieldView):
"""/diff/:type/:id/:field"""

def get(self, type_, change_id, field):
value = self.get_value(type_, change_id, field)
previous = self.get_value(type_, int(change_id) - 1, field)
Expand Down

0 comments on commit a22705e

Please sign in to comment.