pywebutil 1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ Metadata-Version: 2.4
2
+ Name: pywebutil
3
+ Version: 1.0
4
+ Summary: Common utilities and handler code for Python web apps
5
+ Author-email: Ryan Barrett <webutil@ryanb.org>
6
+ Project-URL: Homepage, https://github.com/snarfed/webutil
7
+ Project-URL: Documentation, https://oauth-dropins.readthedocs.io/en/latest/source/oauth_dropins.webutil.html
8
+ Keywords: App Engine,Flask,HTTP,web,utilities
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: Public Domain
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Development Status :: 5 - Production/Stable
14
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
15
+ Requires-Python: >=3.9
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: cachetools>=3.1
19
+ Requires-Dist: cryptography
20
+ Requires-Dist: domain2idna>=1.12
21
+ Requires-Dist: Flask>=2.0
22
+ Requires-Dist: humanize==4.12.0
23
+ Requires-Dist: requests>=2.0
24
+ Requires-Dist: requests-hardened>=1.0
25
+ Requires-Dist: werkzeug
26
+ Provides-Extra: tests
27
+ Requires-Dist: mox3<2.0,>=0.28; extra == "tests"
28
+ Requires-Dist: flask-caching; extra == "tests"
29
+ Requires-Dist: httplib2; extra == "tests"
30
+ Requires-Dist: lxml; extra == "tests"
31
+ Requires-Dist: mf2py; extra == "tests"
32
+ Requires-Dist: oauthlib; extra == "tests"
33
+ Requires-Dist: prawcore; extra == "tests"
34
+ Requires-Dist: python-tumblpy; extra == "tests"
35
+ Requires-Dist: tweepy; extra == "tests"
36
+ Requires-Dist: ujson; extra == "tests"
37
+ Requires-Dist: urllib3; extra == "tests"
38
+ Requires-Dist: webob; extra == "tests"
39
+ Requires-Dist: websockets; extra == "tests"
40
+ Provides-Extra: html
41
+ Requires-Dist: beautifulsoup4>=4.8; extra == "html"
42
+ Requires-Dist: mf2util>=0.5.0; extra == "html"
43
+ Provides-Extra: google-cloud
44
+ Requires-Dist: google-api-python-client; extra == "google-cloud"
45
+ Requires-Dist: google-cloud-logging>=3.0.0; extra == "google-cloud"
46
+ Requires-Dist: google-cloud-ndb>=2.0; extra == "google-cloud"
47
+ Dynamic: license-file
48
+
49
+ webutil [![Circle CI](https://circleci.com/gh/snarfed/oauth-dropins.svg?style=svg)](https://circleci.com/gh/snarfed/oauth-dropins) [![Coverage Status](https://coveralls.io/repos/github/snarfed/oauth-dropins/badge.svg?branch=master)](https://coveralls.io/github/snarfed/oauth-dropins?branch=master)
50
+ ===
51
+
52
+ Common utilities and handler code for Python web apps:
53
+ * [`flask_util`](https://oauth-dropins.readthedocs.io/en/stable/source/oauth_dropins.webutil.html#module-oauth_dropins.webutil.flask_util): [Flask](https://flask.palletsprojects.com/) decorators and handlers for caching, exception handling, regular expression URL routing, domain-wide redirects, rate limiting, headers, and serving XRD and JRD templates.
54
+ * [`logs`](https://oauth-dropins.readthedocs.io/en/stable/source/oauth_dropins.webutil.html#module-oauth_dropins.webutil.logs): Flask request handler that collects full trace logs from [Google Cloud Logging](https://cloud.google.com/logging/docs), formats them nicely, and serves them
55
+ * [`models`](https://oauth-dropins.readthedocs.io/en/stable/source/oauth_dropins.webutil.html#module-oauth_dropins.webutil.models): minor utility [Model classes](https://googleapis.dev/python/python-ndb/latest/model.html) for the [Google Cloud Datastore](https://console.cloud.google.com/datastore/) [ndb library](https://github.com/googleapis/python-ndb)
56
+ * [`testutil`](https://oauth-dropins.readthedocs.io/en/stable/source/oauth_dropins.webutil.html#module-oauth_dropins.webutil.testutil): misc utilities and helpers for [unittest](https://docs.python.org/3.9/library/unittest.html), [mox](https://pypi.org/project/mox3/), [requests](http://python-requests.org), and [urllib](https://docs.python.org/3.9/library/urllib.html)
57
+ * [`util`](https://oauth-dropins.readthedocs.io/en/stable/source/oauth_dropins.webutil.html#module-oauth_dropins.webutil.util): wide variety of utilities for data structures, web code, etc.
58
+ * [`webmention`](https://oauth-dropins.readthedocs.io/en/stable/source/oauth_dropins.webutil.html#module-oauth_dropins.webutil.webmention): [Webmention](https://webmention.net/) endpoint discovery and sending
59
+
60
+ webutil is not (yet) developed, maintained, or distributed as a standalone package. Instead, it's distributed as part of the [oauth-dropins](https://oauth-dropins.readthedocs.io/) library.
61
+
62
+ * Install with `pip install oauth-dropins`.
63
+ * Supports Python 3.7+.
64
+ * [Reference documentation.](https://oauth-dropins.readthedocs.io/en/stable/source/oauth_dropins.webutil.html)
65
+
66
+ webutil is dedicated to the public domain. You may also use it under the [CC0 public domain dedication](https://creativecommons.org/share-your-work/public-domain/cc0/).
@@ -0,0 +1,25 @@
1
+ pywebutil-1.0.dist-info/licenses/LICENSE,sha256=ogEPNDSH0_dhiv_lT3ifVIdgIzHAqNA_SemnxUfPBJk,7048
2
+ webutil/__init__.py,sha256=tUZ4GRJHWYMLc-1Foo_v9exmw91S4ysjFKW1orAHu5Y,1041
3
+ webutil/appengine_config.py,sha256=tkEq8yDnA8KQIxZUIqLjB7w3KCkKWNx5-oyEJKMzNiI,2900
4
+ webutil/appengine_info.py,sha256=qEgLwmdKEwTr3fEfqGb9bwGqHOIe_3rolgkuojYvTqQ,1192
5
+ webutil/flask_util.py,sha256=QojOPqPM25DAk419uRkeRD_y3MGA3oFe_mFaeQnnePw,18578
6
+ webutil/instance_info.py,sha256=K9J4VLoTHVo_AMkKQ5Zdu5mU9w-JCsDsY7XijU3tTP4,2089
7
+ webutil/logs.py,sha256=Y90W9iuUFlt46IC5YrO55hxIsC4SVaIC21wuZhl9coI,8797
8
+ webutil/models.py,sha256=A51m77ZzgFb9s4_ebh_W2iI5PnheJm9JDA-AmdGbECc,8767
9
+ webutil/testutil.py,sha256=v1i15zHgz9c0OPGqNDzl26C4hHO4Kx7ROlNQsdqExRg,18984
10
+ webutil/util.py,sha256=eWKmtKtPKXh3T4a7MEJd3YPC7RCo2fkjfOl2ck5jKDM,80488
11
+ webutil/webmention.py,sha256=VW8cSTdupCkGXFbq6j2tW87ecOEOjm1zecKc63a89MU,4249
12
+ webutil/templates/instance_info.html,sha256=blZBi4jedWRF5-uK-qAYFxMxzgl69dKEnoM_CCjMnB4,939
13
+ webutil/tests/__init__.py,sha256=gAGzmjiyHjr3xxFAJDz6gLP8GyRwKmeXaqYhYsOCZGQ,1042
14
+ webutil/tests/test_flask_util.py,sha256=dskKiabnvX4haLjxqmzmxYf0UbrLx7rvH0issXTMzb4,13483
15
+ webutil/tests/test_handler_get.tmpl,sha256=IOaB5HfpLw7afAtgRxwH_2EdVUcs1DuQ4heqflS3Khw,38
16
+ webutil/tests/test_handler_template.jrd,sha256=vqlRHLmCqfP-NG1SpKf_bdPYgg46Vwvc9eQ6fbAqmIw,23
17
+ webutil/tests/test_handler_template.xrd,sha256=VtmaiFeHRlP3aHu3zP9o0MsVe0eRGaZiIGOwyEly08A,32
18
+ webutil/tests/test_logs.py,sha256=H9LdXWZusAJADvyCaaxKTqTXV9mKPW55Oc32CABwX1I,2161
19
+ webutil/tests/test_models.py,sha256=w94_v_NedDFCJTOWatOcbay2MffM0K3gGWqvW5V4j_I,7343
20
+ webutil/tests/test_util.py,sha256=tc0OkDFk1adwPrPY8yZr6uaXjMKlQDr_X3VEdW35B7k,79674
21
+ webutil/tests/test_webmention.py,sha256=atAoVvQnqF1rEttklWWRtbh66-M8s5agLi-z18UFBtI,6722
22
+ pywebutil-1.0.dist-info/METADATA,sha256=C1N_qHMf0t3Cz3GLAO-Z3R-VM_1hG4zpds3WsBobg5A,4608
23
+ pywebutil-1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
24
+ pywebutil-1.0.dist-info/top_level.txt,sha256=Z39zkn_esVcLWjZV3vhCW0IwiglH72mF43_SW1DPAy4,8
25
+ pywebutil-1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,121 @@
1
+ Creative Commons Legal Code
2
+
3
+ CC0 1.0 Universal
4
+
5
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12
+ HEREUNDER.
13
+
14
+ Statement of Purpose
15
+
16
+ The laws of most jurisdictions throughout the world automatically confer
17
+ exclusive Copyright and Related Rights (defined below) upon the creator
18
+ and subsequent owner(s) (each and all, an "owner") of an original work of
19
+ authorship and/or a database (each, a "Work").
20
+
21
+ Certain owners wish to permanently relinquish those rights to a Work for
22
+ the purpose of contributing to a commons of creative, cultural and
23
+ scientific works ("Commons") that the public can reliably and without fear
24
+ of later claims of infringement build upon, modify, incorporate in other
25
+ works, reuse and redistribute as freely as possible in any form whatsoever
26
+ and for any purposes, including without limitation commercial purposes.
27
+ These owners may contribute to the Commons to promote the ideal of a free
28
+ culture and the further production of creative, cultural and scientific
29
+ works, or to gain reputation or greater distribution for their Work in
30
+ part through the use and efforts of others.
31
+
32
+ For these and/or other purposes and motivations, and without any
33
+ expectation of additional consideration or compensation, the person
34
+ associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35
+ is an owner of Copyright and Related Rights in the Work, voluntarily
36
+ elects to apply CC0 to the Work and publicly distribute the Work under its
37
+ terms, with knowledge of his or her Copyright and Related Rights in the
38
+ Work and the meaning and intended legal effect of CC0 on those rights.
39
+
40
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
41
+ protected by copyright and related or neighboring rights ("Copyright and
42
+ Related Rights"). Copyright and Related Rights include, but are not
43
+ limited to, the following:
44
+
45
+ i. the right to reproduce, adapt, distribute, perform, display,
46
+ communicate, and translate a Work;
47
+ ii. moral rights retained by the original author(s) and/or performer(s);
48
+ iii. publicity and privacy rights pertaining to a person's image or
49
+ likeness depicted in a Work;
50
+ iv. rights protecting against unfair competition in regards to a Work,
51
+ subject to the limitations in paragraph 4(a), below;
52
+ v. rights protecting the extraction, dissemination, use and reuse of data
53
+ in a Work;
54
+ vi. database rights (such as those arising under Directive 96/9/EC of the
55
+ European Parliament and of the Council of 11 March 1996 on the legal
56
+ protection of databases, and under any national implementation
57
+ thereof, including any amended or successor version of such
58
+ directive); and
59
+ vii. other similar, equivalent or corresponding rights throughout the
60
+ world based on applicable law or treaty, and any national
61
+ implementations thereof.
62
+
63
+ 2. Waiver. To the greatest extent permitted by, but not in contravention
64
+ of, applicable law, Affirmer hereby overtly, fully, permanently,
65
+ irrevocably and unconditionally waives, abandons, and surrenders all of
66
+ Affirmer's Copyright and Related Rights and associated claims and causes
67
+ of action, whether now known or unknown (including existing as well as
68
+ future claims and causes of action), in the Work (i) in all territories
69
+ worldwide, (ii) for the maximum duration provided by applicable law or
70
+ treaty (including future time extensions), (iii) in any current or future
71
+ medium and for any number of copies, and (iv) for any purpose whatsoever,
72
+ including without limitation commercial, advertising or promotional
73
+ purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74
+ member of the public at large and to the detriment of Affirmer's heirs and
75
+ successors, fully intending that such Waiver shall not be subject to
76
+ revocation, rescission, cancellation, termination, or any other legal or
77
+ equitable action to disrupt the quiet enjoyment of the Work by the public
78
+ as contemplated by Affirmer's express Statement of Purpose.
79
+
80
+ 3. Public License Fallback. Should any part of the Waiver for any reason
81
+ be judged legally invalid or ineffective under applicable law, then the
82
+ Waiver shall be preserved to the maximum extent permitted taking into
83
+ account Affirmer's express Statement of Purpose. In addition, to the
84
+ extent the Waiver is so judged Affirmer hereby grants to each affected
85
+ person a royalty-free, non transferable, non sublicensable, non exclusive,
86
+ irrevocable and unconditional license to exercise Affirmer's Copyright and
87
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
88
+ maximum duration provided by applicable law or treaty (including future
89
+ time extensions), (iii) in any current or future medium and for any number
90
+ of copies, and (iv) for any purpose whatsoever, including without
91
+ limitation commercial, advertising or promotional purposes (the
92
+ "License"). The License shall be deemed effective as of the date CC0 was
93
+ applied by Affirmer to the Work. Should any part of the License for any
94
+ reason be judged legally invalid or ineffective under applicable law, such
95
+ partial invalidity or ineffectiveness shall not invalidate the remainder
96
+ of the License, and in such case Affirmer hereby affirms that he or she
97
+ will not (i) exercise any of his or her remaining Copyright and Related
98
+ Rights in the Work or (ii) assert any associated claims and causes of
99
+ action with respect to the Work, in either case contrary to Affirmer's
100
+ express Statement of Purpose.
101
+
102
+ 4. Limitations and Disclaimers.
103
+
104
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
105
+ surrendered, licensed or otherwise affected by this document.
106
+ b. Affirmer offers the Work as-is and makes no representations or
107
+ warranties of any kind concerning the Work, express, implied,
108
+ statutory or otherwise, including without limitation warranties of
109
+ title, merchantability, fitness for a particular purpose, non
110
+ infringement, or the absence of latent or other defects, accuracy, or
111
+ the present or absence of errors, whether or not discoverable, all to
112
+ the greatest extent permissible under applicable law.
113
+ c. Affirmer disclaims responsibility for clearing rights of other persons
114
+ that may apply to the Work or any use thereof, including without
115
+ limitation any person's Copyright and Related Rights in the Work.
116
+ Further, Affirmer disclaims responsibility for obtaining any necessary
117
+ consents, permissions or other rights required for any use of the
118
+ Work.
119
+ d. Affirmer understands and acknowledges that Creative Commons is not a
120
+ party to this document and has no duty or obligation with respect to
121
+ this CC0 or use of the Work.
@@ -0,0 +1 @@
1
+ webutil
webutil/__init__.py ADDED
@@ -0,0 +1,28 @@
1
+ import io, logging, os, sys
2
+
3
+ from .appengine_info import DEBUG
4
+
5
+ assert DEBUG
6
+ creds = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
7
+ assert not creds or creds.endswith('fake_user_account.json')
8
+
9
+ logging.getLogger('chardet').setLevel(logging.INFO)
10
+ logging.getLogger('google.cloud').setLevel(logging.INFO)
11
+ logging.getLogger('negotiator').setLevel(logging.WARNING)
12
+ logging.getLogger('urllib3').setLevel(logging.INFO)
13
+
14
+ # Piggyback on unittest's -v and -q flags to show/hide logging.
15
+ logging.basicConfig()
16
+ if '-v' in sys.argv:
17
+ logging.getLogger().setLevel(logging.DEBUG)
18
+ else:
19
+ # used to be:
20
+ # elif 'discover' in sys.argv or '-q' in sys.argv or '--quiet' in sys.argv:
21
+ # dropped that to suppress logging when running full single test files
22
+
23
+ # don't emit logs. do this instead of setLevel() or disable() so that the log
24
+ # messages still get evaluated and raise the same exceptions that they would if
25
+ # they got emitted.
26
+ handler = logging.getLogger().handlers[0]
27
+ if hasattr(handler, 'setStream'):
28
+ handler.setStream(io.StringIO())
@@ -0,0 +1,83 @@
1
+ """App Engine config. local vs prod, logging, Google API clients, etc."""
2
+ import logging
3
+ import os
4
+ import threading
5
+
6
+ from .appengine_info import DEBUG, LOCAL_SERVER
7
+
8
+ # Use lxml for BeautifulSoup explicitly.
9
+ from . import util
10
+ util.beautifulsoup_parser = 'lxml'
11
+
12
+ thread_local = threading.local()
13
+
14
+ # make oauthlib let us use non-SSL http://localhost when running locally
15
+ # https://oauthlib.readthedocs.io/en/latest/oauth2/security.html#envvar-OAUTHLIB_INSECURE_TRANSPORT
16
+ os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'true'
17
+
18
+ #
19
+ # Google API clients
20
+ #
21
+ if DEBUG:
22
+ os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.join(
23
+ os.path.dirname(__file__), 'fake_user_account.json')
24
+ os.environ.setdefault('CLOUDSDK_CORE_PROJECT', 'app')
25
+ os.environ.setdefault('DATASTORE_DATASET', 'app')
26
+ os.environ.setdefault('GOOGLE_CLOUD_PROJECT', 'app')
27
+ os.environ.setdefault('DATASTORE_EMULATOR_HOST', 'localhost:8089')
28
+
29
+ # NDB (Cloud Datastore)
30
+ try:
31
+ # https://googleapis.dev/python/python-ndb/latest/migrating.html#setting-up-a-connection
32
+ from google.cloud import ndb
33
+ db = os.environ.get('DATASTORE_DB')
34
+ ndb_client = thread_local.ndb_client = \
35
+ ndb.Client(database=db) if db else ndb.Client()
36
+ except ImportError:
37
+ pass
38
+
39
+ # Tasks
40
+ # https://docs.cloud.google.com/python/docs/reference/cloudtasks/latest/google.cloud.tasks_v2.services.cloud_tasks.CloudTasksClient
41
+ try:
42
+ from google.cloud import tasks_v2
43
+ tasks_client = tasks_v2.CloudTasksClient()
44
+ if DEBUG or LOCAL_SERVER:
45
+ tasks_client.host = 'localhost:9999'
46
+ tasks_client.secure = False
47
+ except ImportError:
48
+ pass
49
+
50
+ # Error Reporting
51
+ # https://docs.cloud.google.com/python/docs/reference/clouderrorreporting/latest/client
52
+ try:
53
+ from google.cloud import error_reporting
54
+ error_reporting_client = error_reporting.Client()
55
+ if DEBUG or LOCAL_SERVER:
56
+ error_reporting_client.host = 'localhost:9999'
57
+ error_reporting_client.secure = False
58
+ except ImportError:
59
+ pass
60
+
61
+ # needed to make logging visible locally under flask run, etc
62
+ logging.basicConfig()
63
+ logging.getLogger().setLevel(logging.DEBUG)
64
+
65
+ # Logging
66
+ # https://docs.cloud.google.com/python/docs/reference/logging/latest/google.cloud.logging_v2.client.Client
67
+ try:
68
+ import google.cloud.logging
69
+ logging_client = thread_local.logging_client = None
70
+
71
+ if not DEBUG and not LOCAL_SERVER:
72
+ logging_client = thread_local.logging_client = Nonegoogle.cloud.logging.Client()
73
+ logging_client.setup_logging(log_level=logging.DEBUG)
74
+ # this currently occasionally hits the 256KB batch limit and
75
+ # crashes in the background service. i've tried batch_size=1 and
76
+ # SyncTransport, but no luck, same thing.
77
+ # https://stackoverflow.com/questions/59398479
78
+ except ImportError:
79
+ pass
80
+
81
+ for logger in ('charset_normalizer', 'google.cloud', 'oauthlib', 'requests',
82
+ 'requests_cache', 'requests_oauthlib', 'urllib3'):
83
+ logging.getLogger(logger).setLevel(logging.INFO)
@@ -0,0 +1,32 @@
1
+ """HTTP request and server information derived from the environment.
2
+
3
+ App Engine:
4
+ * https://cloud.google.com/appengine/docs/standard/python3/runtime#environment_variables
5
+ * https://cloud.google.com/appengine/docs/flexible/python/runtime#environment_variables
6
+
7
+ WSGI: https://www.python.org/dev/peps/pep-3333/
8
+
9
+ To run against the prod datastore, make a service account, download its JSON
10
+ credentials file, then run eg:
11
+
12
+ env GOOGLE_APPLICATION_CREDENTIALS=service_account_creds.json FLASK_ENV=development flask run -p 8080
13
+ """
14
+ import os, sys
15
+
16
+ PROJECT = os.getenv('GOOGLE_CLOUD_PROJECT') or os.getenv('GAE_APPLICATION') or ''
17
+ APP_ID = PROJECT.split('~')[-1]
18
+
19
+ creds = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
20
+ gae_env = os.environ.get('GAE_ENV') # App Engine Standard
21
+ gae_instance = 'GAE_INSTANCE' in os.environ # App Engine Flex
22
+
23
+ READ_ONLY = bool(os.environ.get('READ_ONLY'))
24
+
25
+ args = ' '.join(sys.argv)
26
+ TESTING = 'unittest' in args or 'pytest' in args
27
+ if creds and not creds.endswith('fake_user_account.json'):
28
+ DEBUG = False
29
+ LOCAL_SERVER = True
30
+ else:
31
+ DEBUG = gae_env in (None, 'localdev') and not gae_instance
32
+ LOCAL_SERVER = gae_env != 'standard' and not gae_instance and not TESTING