__init__.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. """
  3. werkzeug
  4. ~~~~~~~~
  5. Werkzeug is the Swiss Army knife of Python web development.
  6. It provides useful classes and functions for any WSGI application to make
  7. the life of a python web developer much easier. All of the provided
  8. classes are independent from each other so you can mix it with any other
  9. library.
  10. :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
  11. :license: BSD, see LICENSE for more details.
  12. """
  13. from types import ModuleType
  14. import sys
  15. from werkzeug._compat import iteritems
  16. __version__ = '0.14.1'
  17. # This import magic raises concerns quite often which is why the implementation
  18. # and motivation is explained here in detail now.
  19. #
  20. # The majority of the functions and classes provided by Werkzeug work on the
  21. # HTTP and WSGI layer. There is no useful grouping for those which is why
  22. # they are all importable from "werkzeug" instead of the modules where they are
  23. # implemented. The downside of that is, that now everything would be loaded at
  24. # once, even if unused.
  25. #
  26. # The implementation of a lazy-loading module in this file replaces the
  27. # werkzeug package when imported from within. Attribute access to the werkzeug
  28. # module will then lazily import from the modules that implement the objects.
  29. # import mapping to objects in other modules
  30. all_by_module = {
  31. 'werkzeug.debug': ['DebuggedApplication'],
  32. 'werkzeug.local': ['Local', 'LocalManager', 'LocalProxy', 'LocalStack',
  33. 'release_local'],
  34. 'werkzeug.serving': ['run_simple'],
  35. 'werkzeug.test': ['Client', 'EnvironBuilder', 'create_environ',
  36. 'run_wsgi_app'],
  37. 'werkzeug.testapp': ['test_app'],
  38. 'werkzeug.exceptions': ['abort', 'Aborter'],
  39. 'werkzeug.urls': ['url_decode', 'url_encode', 'url_quote',
  40. 'url_quote_plus', 'url_unquote', 'url_unquote_plus',
  41. 'url_fix', 'Href', 'iri_to_uri', 'uri_to_iri'],
  42. 'werkzeug.formparser': ['parse_form_data'],
  43. 'werkzeug.utils': ['escape', 'environ_property', 'append_slash_redirect',
  44. 'redirect', 'cached_property', 'import_string',
  45. 'dump_cookie', 'parse_cookie', 'unescape',
  46. 'format_string', 'find_modules', 'header_property',
  47. 'html', 'xhtml', 'HTMLBuilder', 'validate_arguments',
  48. 'ArgumentValidationError', 'bind_arguments',
  49. 'secure_filename'],
  50. 'werkzeug.wsgi': ['get_current_url', 'get_host', 'pop_path_info',
  51. 'peek_path_info', 'SharedDataMiddleware',
  52. 'DispatcherMiddleware', 'ClosingIterator', 'FileWrapper',
  53. 'make_line_iter', 'LimitedStream', 'responder',
  54. 'wrap_file', 'extract_path_info'],
  55. 'werkzeug.datastructures': ['MultiDict', 'CombinedMultiDict', 'Headers',
  56. 'EnvironHeaders', 'ImmutableList',
  57. 'ImmutableDict', 'ImmutableMultiDict',
  58. 'TypeConversionDict',
  59. 'ImmutableTypeConversionDict', 'Accept',
  60. 'MIMEAccept', 'CharsetAccept',
  61. 'LanguageAccept', 'RequestCacheControl',
  62. 'ResponseCacheControl', 'ETags', 'HeaderSet',
  63. 'WWWAuthenticate', 'Authorization',
  64. 'FileMultiDict', 'CallbackDict', 'FileStorage',
  65. 'OrderedMultiDict', 'ImmutableOrderedMultiDict'
  66. ],
  67. 'werkzeug.useragents': ['UserAgent'],
  68. 'werkzeug.http': ['parse_etags', 'parse_date', 'http_date', 'cookie_date',
  69. 'parse_cache_control_header', 'is_resource_modified',
  70. 'parse_accept_header', 'parse_set_header', 'quote_etag',
  71. 'unquote_etag', 'generate_etag', 'dump_header',
  72. 'parse_list_header', 'parse_dict_header',
  73. 'parse_authorization_header',
  74. 'parse_www_authenticate_header', 'remove_entity_headers',
  75. 'is_entity_header', 'remove_hop_by_hop_headers',
  76. 'parse_options_header', 'dump_options_header',
  77. 'is_hop_by_hop_header', 'unquote_header_value',
  78. 'quote_header_value', 'HTTP_STATUS_CODES'],
  79. 'werkzeug.wrappers': ['BaseResponse', 'BaseRequest', 'Request', 'Response',
  80. 'AcceptMixin', 'ETagRequestMixin',
  81. 'ETagResponseMixin', 'ResponseStreamMixin',
  82. 'CommonResponseDescriptorsMixin', 'UserAgentMixin',
  83. 'AuthorizationMixin', 'WWWAuthenticateMixin',
  84. 'CommonRequestDescriptorsMixin'],
  85. 'werkzeug.security': ['generate_password_hash', 'check_password_hash'],
  86. # the undocumented easteregg ;-)
  87. 'werkzeug._internal': ['_easteregg']
  88. }
  89. # modules that should be imported when accessed as attributes of werkzeug
  90. attribute_modules = frozenset(['exceptions', 'routing'])
  91. object_origins = {}
  92. for module, items in iteritems(all_by_module):
  93. for item in items:
  94. object_origins[item] = module
  95. class module(ModuleType):
  96. """Automatically import objects from the modules."""
  97. def __getattr__(self, name):
  98. if name in object_origins:
  99. module = __import__(object_origins[name], None, None, [name])
  100. for extra_name in all_by_module[module.__name__]:
  101. setattr(self, extra_name, getattr(module, extra_name))
  102. return getattr(module, name)
  103. elif name in attribute_modules:
  104. __import__('werkzeug.' + name)
  105. return ModuleType.__getattribute__(self, name)
  106. def __dir__(self):
  107. """Just show what we want to show."""
  108. result = list(new_module.__all__)
  109. result.extend(('__file__', '__doc__', '__all__',
  110. '__docformat__', '__name__', '__path__',
  111. '__package__', '__version__'))
  112. return result
  113. # keep a reference to this module so that it's not garbage collected
  114. old_module = sys.modules['werkzeug']
  115. # setup the new module and patch it into the dict of loaded modules
  116. new_module = sys.modules['werkzeug'] = module('werkzeug')
  117. new_module.__dict__.update({
  118. '__file__': __file__,
  119. '__package__': 'werkzeug',
  120. '__path__': __path__,
  121. '__doc__': __doc__,
  122. '__version__': __version__,
  123. '__all__': tuple(object_origins) + tuple(attribute_modules),
  124. '__docformat__': 'restructuredtext en'
  125. })
  126. # Due to bootstrapping issues we need to import exceptions here.
  127. # Don't ask :-(
  128. __import__('werkzeug.exceptions')