Skip to content

Commit

Permalink
add backward compatibility handlers for code marked for deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault committed Feb 9, 2023
1 parent 0aac1bf commit 08e432b
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 25 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,17 @@ PYTHON_VERSION ?= `python -c 'import platform; print(platform.python_version())'
PIP_XARGS ?=
PIP_USE_FEATURE := `python -c '\
import pip; \
from distutils.version import LooseVersion; \
try: \
from packaging.version import Version as LooseVersion; \
except Exception: \
from distutils.version import LooseVersion; \
print(LooseVersion(pip.__version__) < LooseVersion("21.0"))'`
PIP_DISABLE_FEATURE := `python -c '\
import pip; \
from distutils.version import LooseVersion; \
try: \
from packaging.version import Version as LooseVersion; \
except Exception: \
from distutils.version import LooseVersion; \
print(LooseVersion(pip.__version__) >= LooseVersion("22.0"))'`
ifeq ($(findstring "--use-feature=2020-resolver",$(PIP_XARGS)),)
# feature not specified, but needed
Expand Down
2 changes: 1 addition & 1 deletion magpie/adapter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import inspect
import re
import warnings
from distutils.version import LooseVersion
from typing import TYPE_CHECKING

import requests
Expand All @@ -29,6 +28,7 @@
from magpie.api.generic import get_request_info
from magpie.api.schemas import SigninAPI
from magpie.app import setup_magpie_configs
from magpie.compat import LooseVersion
from magpie.constants import get_constant
from magpie.security import get_auth_config
from magpie.utils import (
Expand Down
2 changes: 1 addition & 1 deletion magpie/adapter/magpieowssecurity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import uuid
from copy import copy
from distutils.version import LooseVersion
from typing import TYPE_CHECKING

import requests
Expand All @@ -16,6 +15,7 @@

from magpie.api.exception import evaluate_call, verify_param
from magpie.api.schemas import ProviderSigninAPI
from magpie.compat import LooseVersion
from magpie.constants import get_constant
from magpie.db import get_connected_session
from magpie.models import Service
Expand Down
2 changes: 1 addition & 1 deletion magpie/adapter/magpieservice.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Store adapters to read data from magpie.
"""
from distutils.version import LooseVersion
from typing import TYPE_CHECKING

import requests
Expand All @@ -10,6 +9,7 @@
from pyramid.settings import asbool

from magpie.api.schemas import ServicesAPI
from magpie.compat import LooseVersion
from magpie.db import get_connected_session
from magpie.models import Service as MagpieService
from magpie.services import invalidate_service
Expand Down
2 changes: 1 addition & 1 deletion magpie/api/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def make_email_contents(config, settings, template, parameters=None):
}
params.update(parameters or {})
contents = template.render(**params)
message = u"{}".format(contents).strip(u"\n")
message = "{}".format(contents).strip("\n")
return message.encode("utf8")


Expand Down
47 changes: 47 additions & 0 deletions magpie/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Tuple, Union

try:
from packaging.version import InvalidVersion, Version as BaseVersion # pylint: disable=unused-import

class LooseVersion(BaseVersion):
@property
def version(self):
# type: () -> Tuple[Union[int, str], ...]
parts = [part for part in self._version[1:] if part is not None]
parts = tuple(part_group for part in parts for part_group in part)
return parts

@property
def patch(self):
return self.micro

except ImportError: # pragma: no cover # for backward compatibility
from distutils.version import LooseVersion as BaseVersion # pylint: disable=deprecated-module

InvalidVersion = ValueError

class LooseVersion(BaseVersion):
@property
def major(self):
# type: () -> int
num = self.version[0:1]
return int(num[0]) if num else None

@property
def minor(self):
# type: () -> int
num = self.version[1:2]
return int(num[0]) if num else None

@property
def patch(self):
# type: () -> int
num = self.version[2:3]
return int(num[0]) if num else None

@property
def micro(self):
# type: () -> int
return self.patch
15 changes: 10 additions & 5 deletions magpie/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
get_phoenix_url,
get_twitcher_protected_service_url,
islambda,
make_dirs,
print_log,
raise_log
)
Expand Down Expand Up @@ -111,7 +110,9 @@ class RegistrationConfigurationError(RegistrationValueError):


def _login_loop(login_url, cookies_file, data=None, message="Login response"):
make_dirs(cookies_file)
cookies_dir = os.path.dirname(cookies_file)
if not os.path.isdir(cookies_dir):
os.makedirs(cookies_dir) # don't use "exist_ok" for backward compatibility (Python<3.5)
data_str = ""
if data is not None and isinstance(data, dict):
for key in data:
Expand Down Expand Up @@ -173,13 +174,17 @@ def _phoenix_update_services(services_dict):
return True


def _phoenix_login(cookies):
def _phoenix_login(cookies_file):
# type: (Str) -> bool
"""
Performs Phoenix login using provided cookies.
"""
phoenix_pwd = get_constant("PHOENIX_PASSWORD")
phoenix_url = get_phoenix_url()
login_url = phoenix_url + "/account/login/phoenix"
login_data = {"password": phoenix_pwd, "submit": "submit"}
_login_loop(login_url, cookies, login_data, "Phoenix login response")
return _phoenix_login_check(cookies)
_login_loop(login_url, cookies_file, login_data, "Phoenix login response")
return _phoenix_login_check(cookies_file)


def _phoenix_login_check(cookies):
Expand Down
9 changes: 0 additions & 9 deletions magpie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,6 @@ def function(a, b, c=1): ...
return sig


# alternative to 'makedirs' with 'exists_ok' parameter only available for python>3.5
def make_dirs(path):
dir_path = os.path.dirname(path)
if not os.path.isfile(path) or not os.path.isdir(dir_path):
for subdir in mkpath(dir_path):
if not os.path.isdir(subdir):
os.mkdir(subdir)


def get_settings_from_config_ini(config_ini_path, ini_main_section_name="app:magpie_app"):
# type: (Str, Str) -> SettingsType
parser = configparser.ConfigParser()
Expand Down
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import logging
import os
import sys
from distutils.version import LooseVersion

try:
from packaging.version import Version as LooseVersion
except ImportError:
from distutils.version import LooseVersion # pylint: disable=deprecated-module

try:
from setuptools import setup
except ImportError:
from distutils.core import setup
from distutils.core import setup # pylint: disable=deprecated-module

try:
# typing only available builtin starting with Python3
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import re
import tempfile
import unittest
from distutils.version import LooseVersion

import mock
import six
Expand All @@ -23,6 +22,7 @@
from magpie.api import exception as ax
from magpie.api import generic as ag
from magpie.api import requests as ar
from magpie.compat import LooseVersion
from magpie.utils import CONTENT_TYPE_JSON, ExtendedEnum, get_header, get_magpie_url, import_target
from tests import runner, utils

Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import uuid
import warnings
from copy import deepcopy
from distutils.version import LooseVersion
from errno import EADDRINUSE
from typing import TYPE_CHECKING

Expand All @@ -35,6 +34,7 @@
from webtest.response import TestResponse

from magpie import __meta__, app, services
from magpie.compat import LooseVersion
from magpie.constants import get_constant
from magpie.permissions import Access, PermissionSet, Scope
from magpie.services import SERVICE_TYPE_DICT, ServiceAccess
Expand Down Expand Up @@ -1598,7 +1598,7 @@ def check_error_param_structure(body, # type:
if param_value is not null:
# unicode representation was explicitly returned in value only when of string type
if is_param_value_literal_unicode and isinstance(param_value, six.string_types):
param_value = u"u\'{}\'".format(param_value)
param_value = "u\'{}\'".format(param_value)
check_val_equal(body["param"], param_value)
if param_compare_exists or param_compare is not null:
check_val_is_in("param_compare", body)
Expand Down

0 comments on commit 08e432b

Please sign in to comment.