Skip to content
Permalink
Browse files
refactor!: remove python2 (#78)
refactor!: drop support for Python2

BREAKING CHANGE: removes support for webapp2 and other Python2 specific code
  • Loading branch information
daniel-sanche committed Oct 27, 2020
1 parent a58e1de commit bf579e4f871c92391a9f6f87eca931744158e31a
Showing with 5 additions and 113 deletions.
  1. +0 −38 google/cloud/logging/handlers/_helpers.py
  2. +1 −4 google/cloud/logging/handlers/middleware/request.py
  3. +3 −15 noxfile.py
  4. +1 −3 setup.py
  5. +0 −53 tests/unit/handlers/test__helpers.py
@@ -22,20 +22,10 @@
except ImportError: # pragma: NO COVER
flask = None

try:
import webapp2
except (ImportError, SyntaxError): # pragma: NO COVER
# If you try to import webapp2 under python3, you'll get a syntax
# error (since it hasn't been ported yet). We just pretend it
# doesn't exist. This is unlikely to hit in real life but does
# in the tests.
webapp2 = None

from google.cloud.logging.handlers.middleware.request import _get_django_request

_DJANGO_TRACE_HEADER = "HTTP_X_CLOUD_TRACE_CONTEXT"
_FLASK_TRACE_HEADER = "X_CLOUD_TRACE_CONTEXT"
_WEBAPP2_TRACE_HEADER = "X-CLOUD-TRACE-CONTEXT"


def format_stackdriver_json(record, message):
@@ -75,33 +65,6 @@ def get_trace_id_from_flask():
return trace_id


def get_trace_id_from_webapp2():
"""Get trace_id from webapp2 request headers.
:rtype: str
:returns: TraceID in HTTP request headers.
"""
if webapp2 is None:
return None

try:
# get_request() succeeds if we're in the middle of a webapp2
# request, or raises an assertion error otherwise:
# "Request global variable is not set".
req = webapp2.get_request()
except AssertionError:
return None

header = req.headers.get(_WEBAPP2_TRACE_HEADER)

if header is None:
return None

trace_id = header.split("/", 1)[0]

return trace_id


def get_trace_id_from_django():
"""Get trace_id from django request headers.
@@ -131,7 +94,6 @@ def get_trace_id():
checkers = (
get_trace_id_from_django,
get_trace_id_from_flask,
get_trace_id_from_webapp2,
)

for checker in checkers:
@@ -34,11 +34,8 @@ def _get_django_request():


try:
# Django >= 1.10
from django.utils.deprecation import MiddlewareMixin
except ImportError:
# Not required for Django <= 1.9, see:
# https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware
except ImportError: # pragma: NO COVER
MiddlewareMixin = object


@@ -26,8 +26,8 @@
'pytest',
'pytest-cov',
'flask',
'webapp2',
'webob',
'django'
)


@@ -71,13 +71,12 @@ def lint_setup_py(session):
session.run("python", "setup.py", "check", "--restructuredtext", "--strict")


def default(session, django_dep=('django',)):
def default(session):
"""Default unit test session.
"""

# Install all test dependencies, then install this package in-place.
deps = UNIT_TEST_DEPS
deps += django_dep

session.install(*deps)
session.install('-e', '.')
@@ -100,18 +99,7 @@ def default(session, django_dep=('django',)):
@nox.session(python=['3.5', '3.6', '3.7'])
def unit(session):
"""Run the unit test suite."""

# Testing multiple version of django
# See https://www.djangoproject.com/download/ for supported version
django_deps_27 = [
('django==1.8.19',),
('django >= 1.11.0, < 2.0.0dev',),
]

if session.virtualenv.interpreter == '2.7':
[default(session, django_dep=django) for django in django_deps_27]
else:
default(session)
default(session)


@nox.session(python=['3.6'])
@@ -70,8 +70,6 @@
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
@@ -84,7 +82,7 @@
namespace_packages=namespaces,
install_requires=dependencies,
extras_require=extras,
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',
python_requires='>=3.5',
include_package_data=True,
zip_safe=False,
)
@@ -12,18 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import unittest

import mock
import six

try:
from webapp2 import RequestHandler
except SyntaxError:
# webapp2 has not been ported to python3, so it will give a syntax
# error if we try. We'll just skip the webapp2 tests in that case.
RequestHandler = object


class Test_get_trace_id_from_flask(unittest.TestCase):
@@ -68,50 +59,6 @@ def test_valid_context_header(self):
self.assertEqual(trace_id, expected_trace_id)


class _GetTraceId(RequestHandler):
def get(self):
from google.cloud.logging.handlers import _helpers

trace_id = _helpers.get_trace_id_from_webapp2()
self.response.content_type = "application/json"
self.response.out.write(json.dumps(trace_id))


@unittest.skipIf(not six.PY2, "webapp2 is Python 2 only")
class Test_get_trace_id_from_webapp2(unittest.TestCase):
@staticmethod
def create_app():
import webapp2

app = webapp2.WSGIApplication([("/", _GetTraceId)])

return app

def test_no_context_header(self):
import webob

req = webob.BaseRequest.blank("/")
response = req.get_response(self.create_app())
trace_id = json.loads(response.body)

self.assertEqual(None, trace_id)

def test_valid_context_header(self):
import webob

webapp2_trace_header = "X-Cloud-Trace-Context"
expected_trace_id = "testtraceidwebapp2"
webapp2_trace_id = expected_trace_id + "/testspanid"

req = webob.BaseRequest.blank(
"/", headers={webapp2_trace_header: webapp2_trace_id}
)
response = req.get_response(self.create_app())
trace_id = json.loads(response.body)

self.assertEqual(trace_id, expected_trace_id)


class Test_get_trace_id_from_django(unittest.TestCase):
@staticmethod
def _call_fut():

0 comments on commit bf579e4

Please sign in to comment.