Permalink
Browse files

Round 2 of import updates

* Added @export decorator
* Fixed @memoize and @wraps_api to use functools @wraps to ensure __name__ and
  __module__ are preserved
* refactored a few files to handle cycles
* added test_imports.py to allow 'compiling' all the python files and ensure
  they
* r2.config up to import snuff
* r2.controllers up to import snuff
* removed large import blocks and replaced with use of <module>.<symbol> style
* Fixed a lot of kw args with a key = value style to use key=value (only fixed
  where other change was needed)
* Started work on r2.lib, but not finished yet
  • Loading branch information...
1 parent 235f5b7 commit 25155c2f4bf41fba56aad2a65e63592f5a1db90f @kkress committed Sep 9, 2012
Showing with 3,255 additions and 2,704 deletions.
  1. +5 −0 r2/r2/config/environment.py
  2. +17 −0 r2/r2/config/extensions.py
  3. +8 −4 r2/r2/config/middleware.py
  4. +6 −0 r2/r2/config/rewrites.py
  5. +1 −2 r2/r2/config/routing.py
  6. +27 −27 r2/r2/config/templates.py
  7. +52 −38 r2/r2/controllers/__init__.py
  8. +14 −9 r2/r2/controllers/ads.py
  9. +610 −659 r2/r2/controllers/api.py
  10. +14 −3 r2/r2/controllers/api_docs.py
  11. +5 −0 r2/r2/controllers/apiv1.py
  12. +24 −26 r2/r2/controllers/awards.py
  13. +36 −40 r2/r2/controllers/buttons.py
  14. +5 −0 r2/r2/controllers/captcha.py
  15. +5 −0 r2/r2/controllers/embed.py
  16. +5 −0 r2/r2/controllers/error.py
  17. +13 −7 r2/r2/controllers/errorlog.py
  18. +5 −0 r2/r2/controllers/errors.py
  19. +12 −7 r2/r2/controllers/feedback.py
  20. +377 −442 r2/r2/controllers/front.py
  21. +5 −0 r2/r2/controllers/health.py
  22. +26 −26 r2/r2/controllers/ipn.py
  23. +125 −153 r2/r2/controllers/listingcontroller.py
  24. +5 −1 r2/r2/controllers/mediaembed.py
  25. +45 −37 r2/r2/controllers/oauth2.py
  26. +59 −56 r2/r2/controllers/post.py
  27. +136 −152 r2/r2/controllers/promotecontroller.py
  28. +5 −1 r2/r2/controllers/querycontroller.py
  29. +48 −65 r2/r2/controllers/reddit_base.py
  30. +5 −0 r2/r2/controllers/redirect.py
  31. +10 −3 r2/r2/controllers/toolbar.py
  32. +187 −1 r2/r2/controllers/validator/validator.py
  33. +20 −9 r2/r2/lib/amqp.py
  34. +61 −43 r2/r2/lib/app_globals.py
  35. +3 −1 r2/r2/lib/authentication.py
  36. +1 −0 r2/r2/lib/authorize/__init__.py
  37. +11 −7 r2/r2/lib/authorize/api.py
  38. +5 −2 r2/r2/lib/authorize/interaction.py
  39. +23 −7 r2/r2/lib/cloudsearch.py
  40. +1 −1 r2/r2/lib/cssfilter.py
  41. +41 −0 r2/r2/lib/export.py
  42. +12 −8 r2/r2/lib/jsonresponse.py
  43. +2 −1 r2/r2/lib/memoize.py
  44. +13 −6 r2/r2/lib/menus.py
  45. +6 −5 r2/r2/lib/mr_account.py
  46. +3 −1 r2/r2/lib/pages/__init__.py
  47. +16 −3 r2/r2/lib/pages/admin_pages.py
  48. +439 −291 r2/r2/lib/pages/pages.py
  49. +9 −40 r2/r2/lib/pages/things.py
  50. +11 −7 r2/r2/lib/pages/trafficpages.py
  51. +2 −2 r2/r2/lib/wrapped.pyx
  52. +76 −0 r2/r2/lib/wrapper.py
  53. +35 −23 r2/r2/models/account.py
  54. +3 −5 r2/r2/models/account_subreddit.py
  55. +9 −2 r2/r2/models/ad.py
  56. +38 −25 r2/r2/models/admintools.py
  57. +5 −8 r2/r2/models/award.py
  58. +76 −80 r2/r2/models/bidding.py
  59. +8 −10 r2/r2/models/builder.py
  60. +22 −16 r2/r2/models/builders.py
  61. +6 −9 r2/r2/models/flair.py
  62. +52 −50 r2/r2/models/gold.py
  63. +6 −8 r2/r2/models/jury.py
  64. +4 −5 r2/r2/models/keyvalue.py
  65. +3 −5 r2/r2/models/last_modified.py
  66. +37 −26 r2/r2/models/link.py
  67. +11 −9 r2/r2/models/listing.py
  68. +109 −110 r2/r2/models/mail_queue.py
  69. +3 −7 r2/r2/models/modaction.py
  70. +4 −2 r2/r2/models/populatedb.py
  71. +3 −5 r2/r2/models/printable.py
  72. +4 −5 r2/r2/models/promo.py
  73. +12 −14 r2/r2/models/query_cache.py
  74. +3 −7 r2/r2/models/report.py
  75. +37 −29 r2/r2/models/subreddit.py
  76. +10 −12 r2/r2/models/token.py
  77. +13 −16 r2/r2/models/traffic.py
  78. +3 −7 r2/r2/models/trial.py
  79. +17 −15 r2/r2/models/vote.py
  80. +69 −0 r2/r2/tests/functional/test_imports.py
  81. +1 −1 r2/r2/tests/test_models.py
@@ -30,11 +30,16 @@
from r2.config import routing
from r2.lib.app_globals import Globals
from r2.lib.configparse import ConfigValue
+from r2.lib.export import export
+__all__ = [
+ #Constants Only, use @export for functions/classes
+ ]
mimetypes.init()
+@export
def load_environment(global_conf={}, app_conf={}, setup_globals=True):
# Setup our paths
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -22,16 +22,31 @@
from pylons import c
+from r2.lib.export import export
+
+__all__ = [
+ #Constants Only, use @export for functions/classes
+ "API_TYPES",
+ "extension_mapping",
+ ]
+
+
+@export
def api_type(subtype = ''):
return 'api-' + subtype if subtype else 'api'
+
+@export
def is_api(subtype = ''):
return c.render_style and c.render_style.startswith(api_type(subtype))
+
+@export
def get_api_subtype():
if is_api() and c.render_style.startswith('api-'):
return c.render_style[4:]
+
extension_mapping = {
"rss": ("xml", "text/xml; charset=UTF-8"),
"xml": ("xml", "text/xml; charset=UTF-8"),
@@ -50,8 +65,10 @@ def get_api_subtype():
"i": ("compact", "text/html; charset=UTF-8"),
}
+
API_TYPES = ('api', 'json')
+@export
def set_extension(environ, ext):
environ["extension"] = ext
environ["render_style"], environ["content_type"] = extension_mapping[ext]
@@ -24,22 +24,26 @@
import re
import urllib
import tempfile
-import urlparse
from threading import Lock
from paste.cascade import Cascade
+from paste.deploy.converters import asbool
from paste.registry import RegistryManager
from paste.urlparser import StaticURLParser
-from paste.deploy.converters import asbool
from pylons import config, Response
from pylons.error import error_template
from pylons.middleware import ErrorDocuments, ErrorHandler, StaticJavascripts
from pylons.wsgiapp import PylonsApp, PylonsBaseWSGIApp
+from r2.lib.utils import is_subdomain
+
from r2.config.environment import load_environment
-from r2.config.rewrites import rewrites
from r2.config.extensions import extension_mapping, set_extension
-from r2.lib.utils import is_subdomain
+from r2.config.rewrites import rewrites
+
+__all__ = [
+ #Constants Only, use @export for functions/classes
+ ]
# hack in Paste support for HTTP 429 "Too Many Requests"
@@ -22,6 +22,12 @@
import re
+__all__ = [
+ #Constants Only, use @export for functions/classes
+ "rewrites",
+ ]
+
+
rewrites = (#these first two rules prevent the .embed rewrite from
#breaking other js that should work
("\A/_(.*)", "/_$1"),
@@ -23,9 +23,8 @@
"""
Setup your Routes options here
"""
-import os
-from routes import Mapper
from pylons import config
+from routes import Mapper
def make_map():
map = Mapper()
@@ -20,8 +20,8 @@
# Inc. All Rights Reserved.
###############################################################################
+import r2.lib.jsontemplates as jtmpl
from r2.lib.manager import tp_manager
-from r2.lib.jsontemplates import *
tpm = tp_manager.tp_manager()
@@ -31,35 +31,35 @@ def api(type, cls):
tpm.add_handler(type, 'api-compact', cls())
# blanket fallback rule
-api('templated', NullJsonTemplate)
+api('templated', jtmpl.NullJsonTemplate)
# class specific overrides
-api('link', LinkJsonTemplate)
-api('promotedlink', PromotedLinkJsonTemplate)
-api('comment', CommentJsonTemplate)
-api('message', MessageJsonTemplate)
-api('subreddit', SubredditJsonTemplate)
-api('morerecursion', MoreCommentJsonTemplate)
-api('morechildren', MoreCommentJsonTemplate)
-api('reddit', RedditJsonTemplate)
-api('panestack', PanestackJsonTemplate)
-api('listing', ListingJsonTemplate)
-api('modlist', UserListJsonTemplate)
-api('userlist', UserListJsonTemplate)
-api('contributorlist', UserListJsonTemplate)
-api('bannedlist', UserListJsonTemplate)
-api('friendlist', UserListJsonTemplate)
-api('usertableitem', UserTableItemJsonTemplate)
-api('account', AccountJsonTemplate)
+api('link', jtmpl.LinkJsonTemplate)
+api('promotedlink', jtmpl.PromotedLinkJsonTemplate)
+api('comment', jtmpl.CommentJsonTemplate)
+api('message', jtmpl.MessageJsonTemplate)
+api('subreddit', jtmpl.SubredditJsonTemplate)
+api('morerecursion', jtmpl.MoreCommentJsonTemplate)
+api('morechildren', jtmpl.MoreCommentJsonTemplate)
+api('reddit', jtmpl.RedditJsonTemplate)
+api('panestack', jtmpl.PanestackJsonTemplate)
+api('listing', jtmpl.ListingJsonTemplate)
+api('modlist', jtmpl.UserListJsonTemplate)
+api('userlist', jtmpl.UserListJsonTemplate)
+api('contributorlist', jtmpl.UserListJsonTemplate)
+api('bannedlist', jtmpl.UserListJsonTemplate)
+api('friendlist', jtmpl.UserListJsonTemplate)
+api('usertableitem', jtmpl.UserTableItemJsonTemplate)
+api('account', jtmpl.AccountJsonTemplate)
-api('organiclisting', OrganicListingJsonTemplate)
-api('subreddittraffic', TrafficJsonTemplate)
-api('takedownpane', TakedownJsonTemplate)
+api('organiclisting', jtmpl.OrganicListingJsonTemplate)
+api('subreddittraffic', jtmpl.TrafficJsonTemplate)
+api('takedownpane', jtmpl.TakedownJsonTemplate)
-api('flairlist', FlairListJsonTemplate)
-api('flaircsv', FlairCsvJsonTemplate)
+api('flairlist', jtmpl.FlairListJsonTemplate)
+api('flaircsv', jtmpl.FlairCsvJsonTemplate)
-api('subredditstylesheet', StylesheetTemplate)
-api('createsubreddit', SubredditSettingsTemplate)
+api('subredditstylesheet', jtmpl.StylesheetTemplate)
+api('createsubreddit', jtmpl.SubredditSettingsTemplate)
-tpm.add_handler('usertableitem', 'api-html', UserItemHTMLJsonTemplate())
+tpm.add_handler('usertableitem', 'api-html', jtmpl.UserItemHTMLJsonTemplate())
@@ -20,9 +20,18 @@
# Inc. All Rights Reserved.
###############################################################################
+from r2.lib.export import export
+
+__all__ = [
+ #Constants Only, use @export for functions/classes
+ ]
+
+
_reddit_controllers = {}
_plugin_controllers = {}
+
+@export
def get_controller(name):
name = name.lower() + 'controller'
if name in _reddit_controllers:
@@ -32,53 +41,58 @@ def get_controller(name):
else:
raise KeyError(name)
+
+@export
def add_controller(controller):
name = controller.__name__.lower()
assert name not in _plugin_controllers
_plugin_controllers[name] = controller
return controller
+
+@export
def load_controllers():
- from listingcontroller import ListingController
- from listingcontroller import HotController
- from listingcontroller import NewController
- from listingcontroller import BrowseController
- from listingcontroller import MessageController
- from listingcontroller import RedditsController
- from listingcontroller import ByIDController
- from listingcontroller import RandomrisingController
- from listingcontroller import UserController
- from listingcontroller import CommentsController
+ from r2.controllers.listingcontroller import ListingController
+ from r2.controllers.listingcontroller import HotController
+ from r2.controllers.listingcontroller import NewController
+ from r2.controllers.listingcontroller import BrowseController
+ from r2.controllers.listingcontroller import MessageController
+ from r2.controllers.listingcontroller import RedditsController
+ from r2.controllers.listingcontroller import ByIDController
+ from r2.controllers.listingcontroller import RandomrisingController
+ from r2.controllers.listingcontroller import UserController
+ from r2.controllers.listingcontroller import CommentsController
- from listingcontroller import MyredditsController
+ from r2.controllers.listingcontroller import MyredditsController
- from feedback import FeedbackController
- from front import FormsController
- from front import FrontController
- from health import HealthController
- from buttons import ButtonsController
- from captcha import CaptchaController
- from embed import EmbedController
- from error import ErrorController
- from post import PostController
- from toolbar import ToolbarController
- from awards import AwardsController
- from ads import AdsController
- from usage import UsageController
- from errorlog import ErrorlogController
- from promotecontroller import PromoteController
- from mediaembed import MediaembedController
- from mediaembed import AdController
+ from r2.controllers.feedback import FeedbackController
+ from r2.controllers.front import FormsController
+ from r2.controllers.front import FrontController
+ from r2.controllers.health import HealthController
+ from r2.controllers.buttons import ButtonsController
+ from r2.controllers.captcha import CaptchaController
+ from r2.controllers.embed import EmbedController
+ from r2.controllers.error import ErrorController
+ from r2.controllers.post import PostController
+ from r2.controllers.toolbar import ToolbarController
+ from r2.controllers.awards import AwardsController
+ from r2.controllers.ads import AdsController
+ from r2.controllers.usage import UsageController
+ from r2.controllers.errorlog import ErrorlogController
+ from r2.controllers.promotecontroller import PromoteController
+ from r2.controllers.mediaembed import MediaembedController
+ from r2.controllers.mediaembed import AdController
- from querycontroller import QueryController
+ from r2.controllers.querycontroller import QueryController
- from api import ApiController
- from api import ApiminimalController
- from api_docs import ApidocsController
- from apiv1 import APIv1Controller
- from oauth2 import OAuth2FrontendController
- from oauth2 import OAuth2AccessController
- from redirect import RedirectController
- from ipn import IpnController
+ from r2.controllers.api import ApiController
+ from r2.controllers.api import ApiminimalController
+ from r2.controllers.api_docs import ApidocsController
+ from r2.controllers.apiv1 import APIv1Controller
+ from r2.controllers.oauth2 import OAuth2FrontendController
+ from r2.controllers.oauth2 import OAuth2AccessController
+ from r2.controllers.redirect import RedirectController
+ from r2.controllers.ipn import IpnController
- _reddit_controllers.update((name.lower(), obj) for name, obj in locals().iteritems())
+ controllers = [(name.lower(), obj) for name, obj in locals().iteritems()]
+ _reddit_controllers.update(controllers)
@@ -22,25 +22,30 @@
from pylons.controllers.util import abort
+from r2.lib.export import export
from r2.lib.pages import AdminPage, AdminAds, AdminAdAssign, AdminAdSRs
+import r2.controllers.validator as validator
from r2.controllers.reddit_base import RedditController
-from r2.controllers.validator import (VAdByCodename,
- VSponsorAdmin,
- validate,
- )
+from r2.controllers.validator import validate
+__all__ = [
+ #Constants Only, use @export for functions/classes
+ ]
+
+
+@export
class AdsController(RedditController):
- @validate(VSponsorAdmin())
+ @validate(validator.VSponsorAdmin())
def GET_index(self):
res = AdminPage(content = AdminAds(),
show_sidebar = False,
title = 'ads').render()
return res
- @validate(VSponsorAdmin(),
- ad = VAdByCodename('adcn'))
+ @validate(validator.VSponsorAdmin(),
+ ad = validator.VAdByCodename('adcn'))
def GET_assign(self, ad):
if ad is None:
abort(404, 'page not found')
@@ -50,8 +55,8 @@ def GET_assign(self, ad):
title='assign an ad to a community').render()
return res
- @validate(VSponsorAdmin(),
- ad = VAdByCodename('adcn'))
+ @validate(validator.VSponsorAdmin(),
+ ad = validator.VAdByCodename('adcn'))
def GET_srs(self, ad):
if ad is None:
abort(404, 'page not found')
Oops, something went wrong.

0 comments on commit 25155c2

Please sign in to comment.