99 :license: BSD, see LICENSE for more details.
1010"""
1111
12- __version__ = "0.14.0-dev0"
13-
14-
1512import sys
1613import functools
1714import inspect
2320
2421_py2 = sys .version_info [0 ] == 2
2522
23+ __version__ = "0.14.0-dev0"
24+
2625
2726def route (rule , ** options ):
2827 """A decorator that is used to define custom routes for methods in
@@ -34,7 +33,7 @@ def decorator(f):
3433 # Put the rule cache on the method itself instead of globally
3534 if not hasattr (f , '_rule_cache' ) or f ._rule_cache is None :
3635 f ._rule_cache = {f .__name__ : [(rule , options )]}
37- elif not f .__name__ in f ._rule_cache :
36+ elif f .__name__ not in f ._rule_cache :
3837 f ._rule_cache [f .__name__ ] = [(rule , options )]
3938 else :
4039 f ._rule_cache [f .__name__ ].append ((rule , options ))
@@ -54,7 +53,9 @@ class FlaskView(object):
5453 route_base = None
5554 route_prefix = None
5655 trailing_slash = True
57- method_dashified = False # TODO(hoatle): make True as default instead, this is not a compatible change
56+ # TODO(hoatle): make method_dashified=True as default instead,
57+ # this is not a compatible change
58+ method_dashified = False
5859 special_methods = {
5960 "get" : ["GET" ],
6061 "put" : ["PUT" ],
@@ -91,14 +92,16 @@ def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
9192 :param route_prefix: A prefix to be applied to all routes registered
9293 for this class. Precedes route_base. Overrides
9394 the class' route_prefix if it has been set.
94- :param trailing_slash: An option to put trailing slashes at the end of routes
95- without parameters.
96- :param method_dashified: An option to dashify method name from some_route to /some-route/
97- route instead of default /some_route/
95+ :param trailing_slash: An option to put trailing slashes at the end of
96+ routes without parameters.
97+ :param method_dashified: An option to dashify method name from
98+ some_route to /some-route/ route instead of
99+ default /some_route/
98100 """
99101
100102 if cls is FlaskView :
101- raise TypeError ("cls must be a subclass of FlaskView, not FlaskView itself" )
103+ raise TypeError (
104+ "cls must be a subclass of FlaskView, not FlaskView itself" )
102105
103106 if route_base :
104107 cls .orig_route_base = cls .route_base
@@ -144,15 +147,19 @@ def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
144147 else :
145148 endpoint = "{0!s}_{1:d}" .format (route_name , idx )
146149
147- app .add_url_rule (rule , endpoint , proxy , subdomain = subdomain , ** options )
150+ app .add_url_rule (
151+ rule , endpoint , proxy ,
152+ subdomain = subdomain , ** options )
148153
149154 elif name in cls .special_methods :
150155 methods = cls .special_methods [name ]
151156
152157 rule = cls .build_rule ("/" , value )
153158 if not cls .trailing_slash and rule != '/' :
154159 rule = rule .rstrip ("/" )
155- app .add_url_rule (rule , route_name , proxy , methods = methods , subdomain = subdomain )
160+ app .add_url_rule (
161+ rule , route_name , proxy ,
162+ methods = methods , subdomain = subdomain )
156163
157164 else :
158165 if cls .method_dashified is True :
@@ -161,9 +168,12 @@ def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
161168 if not cls .trailing_slash :
162169 route_str = route_str .rstrip ('/' )
163170 rule = cls .build_rule (route_str , value )
164- app .add_url_rule (rule , route_name , proxy , subdomain = subdomain )
171+ app .add_url_rule (
172+ rule , route_name , proxy , subdomain = subdomain )
165173 except DecoratorCompatibilityError :
166- raise DecoratorCompatibilityError ("Incompatible decorator detected on {0!s} in class {1!s}" .format (name , cls .__name__ ))
174+ raise DecoratorCompatibilityError (
175+ "Incompatible decorator detected on {0!s} in class {1!s}"
176+ .format (name , cls .__name__ ))
167177
168178 if hasattr (cls , "orig_route_base" ):
169179 cls .route_base = cls .orig_route_base
@@ -203,7 +213,8 @@ def make_proxy_method(cls, name):
203213 i = cls ()
204214 view = getattr (i , name )
205215
206- # Since the view is a bound instance method, first make it an actual function
216+ # Since the view is a bound instance method,
217+ # first make it an actual function
207218 # So function attributes work correctly
208219 def make_func (fn ):
209220 @functools .wraps (fn )
@@ -246,23 +257,26 @@ def proxy(**forgettable_view_args):
246257 if not isinstance (response , ResponseBase ):
247258
248259 if not cls .representations :
249- # No representations defined, then the default is to just output
250- # what the view function returned as a response
260+ # No representations defined, then the default is to just
261+ # output what the view function returned as a response
251262 response = make_response (response , code , headers )
252263 else :
253- # Return the representation that best matches the representations
254- # in the Accept header
264+ # Return the representation that best matches the
265+ # representations in the Accept header
255266 resp_representation = request .accept_mimetypes .best_match (
256267 cls .representations .keys ())
257268
258269 if resp_representation :
259- response = cls .representations [resp_representation ](response , code , headers )
270+ response = cls .representations [
271+ resp_representation
272+ ](response , code , headers )
260273 else :
261- # Nothing adequate found, make the response any one of the representations
262- # defined
274+ # Nothing adequate found, make the response any one of
275+ # the representations defined
263276 # TODO(hoatle): or just make_response?
264- response = cls .representations [list (cls .representations .keys ())[0 ]](
265- response , code , headers )
277+ response = cls .representations [
278+ list (cls .representations .keys ())[0 ]
279+ ](response , code , headers )
266280
267281 # If the header or code is set, regenerate the response
268282 elif any (x is not None for x in (code , headers )):
@@ -280,7 +294,6 @@ def proxy(**forgettable_view_args):
280294
281295 return response
282296
283-
284297 return proxy
285298
286299 @classmethod
@@ -313,12 +326,12 @@ def build_rule(cls, rule, method=None):
313326 if method :
314327 argspec = get_true_argspec (method )
315328 args = argspec [0 ]
316- query_params = argspec [3 ] # All default args that should be ignored
329+ query_params = argspec [3 ] # All default args should be ignored
317330 annotations = getattr (argspec , 'annotations' , {})
318331 for i , arg in enumerate (args ):
319332 if arg not in ignored_rule_args :
320333 if not query_params or len (args ) - i > len (query_params ):
321- # This is not optional param, so it's not query argument
334+ # This isn't optional param, so it's not query argument
322335 rule_part = "<{0!s}>" .format (arg )
323336 if not _py2 :
324337 # in py3, try to determine url variable converters
@@ -330,7 +343,6 @@ def build_rule(cls, rule, method=None):
330343 result = "/{0!s}" .format ("/" .join (rule_parts ))
331344 return re .sub (r'(/)\1+' , r'\1' , result )
332345
333-
334346 @classmethod
335347 def get_route_base (cls ):
336348 """Returns the route base to use for the current class."""
@@ -354,7 +366,6 @@ def default_route_base(cls):
354366
355367 return route_base
356368
357-
358369 @classmethod
359370 def build_route_name (cls , method_name ):
360371 """Creates a unique route name based on the combination of the class
@@ -367,11 +378,12 @@ def build_route_name(cls, method_name):
367378
368379def _dashify_uppercase (name ):
369380 """convert somethingWithUppercase into something-with-uppercase"""
370- first_cap_re = re .compile ('(.)([A-Z][a-z]+)' ) # better to define this once
381+ first_cap_re = re .compile ('(.)([A-Z][a-z]+)' ) # better to define this once
371382 all_cap_re = re .compile ('([a-z0-9])([A-Z])' )
372383 s1 = first_cap_re .sub (r'\1-\2' , name )
373384 return all_cap_re .sub (r'\1-\2' , s1 ).lower ()
374385
386+
375387def _dashify_underscore (name ):
376388 """convert something_with_underscore into something-with-underscore"""
377389 return '-' .join (re .split ('_' , name ))
@@ -385,14 +397,21 @@ def get_interesting_members(base_class, cls):
385397 all_members = inspect .getmembers (cls , predicate = predicate )
386398 return [member for member in all_members
387399 if not member [0 ] in base_members
388- and ((hasattr (member [1 ], "__self__" ) and not member [1 ].__self__ in inspect .getmro (cls )) if _py2 else True )
400+ and (
401+ (hasattr (member [1 ], "__self__" )
402+ and not member [1 ].__self__ in inspect .getmro (cls ))
403+ if _py2 else True
404+ )
389405 and not member [0 ].startswith ("_" )
390406 and not member [0 ].startswith ("before_" )
391407 and not member [0 ].startswith ("after_" )]
392408
393409
394410def get_true_argspec (method ):
395- """Drills through layers of decorators attempting to locate the actual argspec for the method."""
411+ """
412+ Drills through layers of decorators attempting to locate the actual argspec
413+ for the method.
414+ """
396415
397416 try :
398417 argspec = inspect .getargspec (method )
@@ -412,8 +431,8 @@ def get_true_argspec(method):
412431 inner_method = cell .cell_contents
413432 if inner_method is method :
414433 continue
415- if not inspect .isfunction (inner_method ) \
416- and not inspect .ismethod (inner_method ):
434+ if not inspect .isfunction (inner_method )\
435+ and not inspect .ismethod (inner_method ):
417436 continue
418437 true_argspec = get_true_argspec (inner_method )
419438 if true_argspec :
0 commit comments