Skip to content

Commit

Permalink
refactor!: remove python2 (#78)
Browse files Browse the repository at this point in the history
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 bf579e4
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 113 deletions.
38 changes: 0 additions & 38 deletions google/cloud/logging/handlers/_helpers.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions google/cloud/logging/handlers/middleware/request.py
Expand Up @@ -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


Expand Down
18 changes: 3 additions & 15 deletions noxfile.py
Expand Up @@ -26,8 +26,8 @@
'pytest',
'pytest-cov',
'flask',
'webapp2',
'webob',
'django'
)


Expand Down Expand Up @@ -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', '.')
Expand All @@ -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'])
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Expand Up @@ -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',
Expand All @@ -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,
)
53 changes: 0 additions & 53 deletions tests/unit/handlers/test__helpers.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit bf579e4

Please sign in to comment.