mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Drop support for python2
This commit is contained in:
parent
19a40b6e9a
commit
570e53234a
9 changed files with 29 additions and 128 deletions
|
@ -5,26 +5,7 @@ import re
|
||||||
import logging
|
import logging
|
||||||
import yaml
|
import yaml
|
||||||
import glob
|
import glob
|
||||||
|
import pickle as pickle
|
||||||
import sys
|
|
||||||
if sys.version_info[0] == 3:
|
|
||||||
# python 3
|
|
||||||
import pickle as pickle
|
|
||||||
else:
|
|
||||||
# python 2
|
|
||||||
import cPickle as pickle
|
|
||||||
import codecs
|
|
||||||
import warnings
|
|
||||||
def open(file, mode='r', buffering=-1, encoding=None,
|
|
||||||
errors=None, newline=None, closefd=True, opener=None):
|
|
||||||
if newline is not None:
|
|
||||||
warnings.warn('newline is not supported in py2')
|
|
||||||
if not closefd:
|
|
||||||
warnings.warn('closefd is not supported in py2')
|
|
||||||
if opener is not None:
|
|
||||||
warnings.warn('opener is not supported in py2')
|
|
||||||
return codecs.open(filename=file, mode=mode, encoding=encoding,
|
|
||||||
errors=errors, buffering=buffering)
|
|
||||||
|
|
||||||
from time import time
|
from time import time
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
|
@ -8,24 +8,6 @@ import logging
|
||||||
import moulinette
|
import moulinette
|
||||||
from moulinette.globals import init_moulinette_env
|
from moulinette.globals import init_moulinette_env
|
||||||
|
|
||||||
import sys
|
|
||||||
if sys.version_info[0] == 3:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# python 2
|
|
||||||
import codecs
|
|
||||||
import warnings
|
|
||||||
def open(file, mode='r', buffering=-1, encoding=None,
|
|
||||||
errors=None, newline=None, closefd=True, opener=None):
|
|
||||||
if newline is not None:
|
|
||||||
warnings.warn('newline is not supported in py2')
|
|
||||||
if not closefd:
|
|
||||||
warnings.warn('closefd is not supported in py2')
|
|
||||||
if opener is not None:
|
|
||||||
warnings.warn('opener is not supported in py2')
|
|
||||||
return codecs.open(filename=file, mode=mode, encoding=encoding,
|
|
||||||
errors=errors, buffering=buffering)
|
|
||||||
|
|
||||||
logger = logging.getLogger("moulinette.core")
|
logger = logging.getLogger("moulinette.core")
|
||||||
|
|
||||||
|
|
||||||
|
@ -189,9 +171,6 @@ class Translator(object):
|
||||||
try:
|
try:
|
||||||
with open("%s/%s.json" % (self.locale_dir, locale), "r", encoding='utf-8') as f:
|
with open("%s/%s.json" % (self.locale_dir, locale), "r", encoding='utf-8') as f:
|
||||||
j = json.load(f)
|
j = json.load(f)
|
||||||
import sys
|
|
||||||
if sys.version_info[0] == 2:
|
|
||||||
j = {k.encode("utf-8"): v.encode("utf-8") for k, v in j.items()}
|
|
||||||
except IOError:
|
except IOError:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -9,13 +9,6 @@ from collections import deque, OrderedDict
|
||||||
from moulinette import msettings, m18n
|
from moulinette import msettings, m18n
|
||||||
from moulinette.core import MoulinetteError
|
from moulinette.core import MoulinetteError
|
||||||
|
|
||||||
import sys
|
|
||||||
if sys.version_info[0] == 3:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# python 2
|
|
||||||
range = xrange
|
|
||||||
|
|
||||||
logger = logging.getLogger("moulinette.interface")
|
logger = logging.getLogger("moulinette.interface")
|
||||||
|
|
||||||
GLOBAL_SECTION = "_global"
|
GLOBAL_SECTION = "_global"
|
||||||
|
|
|
@ -3,24 +3,6 @@ import re
|
||||||
import mmap
|
import mmap
|
||||||
import binascii
|
import binascii
|
||||||
|
|
||||||
import sys
|
|
||||||
if sys.version_info[0] == 3:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# python 2
|
|
||||||
import codecs
|
|
||||||
import warnings
|
|
||||||
def open(file, mode='r', buffering=-1, encoding=None,
|
|
||||||
errors=None, newline=None, closefd=True, opener=None):
|
|
||||||
if newline is not None:
|
|
||||||
warnings.warn('newline is not supported in py2')
|
|
||||||
if not closefd:
|
|
||||||
warnings.warn('closefd is not supported in py2')
|
|
||||||
if opener is not None:
|
|
||||||
warnings.warn('opener is not supported in py2')
|
|
||||||
return codecs.open(filename=file, mode=mode, encoding=encoding,
|
|
||||||
errors=errors, buffering=buffering)
|
|
||||||
|
|
||||||
# Pattern searching ----------------------------------------------------
|
# Pattern searching ----------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,10 +48,7 @@ def searchf(pattern, path, count=0, flags=re.MULTILINE):
|
||||||
"""
|
"""
|
||||||
with open(path, "rb+") as f:
|
with open(path, "rb+") as f:
|
||||||
data = mmap.mmap(f.fileno(), 0)
|
data = mmap.mmap(f.fileno(), 0)
|
||||||
if sys.version_info[0] == 3:
|
match = search(pattern, data.read().decode(), count, flags)
|
||||||
match = search(pattern, data.read().decode(), count, flags)
|
|
||||||
else:
|
|
||||||
match = search(pattern, data, count, flags)
|
|
||||||
data.close()
|
data.close()
|
||||||
return match
|
return match
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ import json
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import pytest
|
import pytest
|
||||||
import sys
|
|
||||||
|
|
||||||
from .src.ldap_server import LDAPServer
|
from .src.ldap_server import LDAPServer
|
||||||
|
|
||||||
|
@ -220,11 +219,3 @@ def ldap_server():
|
||||||
server.start()
|
server.start()
|
||||||
yield server
|
yield server
|
||||||
server.stop()
|
server.stop()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def builtin_str():
|
|
||||||
if sys.version_info[0] == 3:
|
|
||||||
return "builtins"
|
|
||||||
else:
|
|
||||||
return "__builtin__"
|
|
|
@ -5,24 +5,6 @@ except ImportError:
|
||||||
import os
|
import os
|
||||||
from moulinette.authenticators import ldap as m_ldap
|
from moulinette.authenticators import ldap as m_ldap
|
||||||
|
|
||||||
import sys
|
|
||||||
if sys.version_info[0] == 3:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# python 2
|
|
||||||
import codecs
|
|
||||||
import warnings
|
|
||||||
def open(file, mode='r', buffering=-1, encoding=None,
|
|
||||||
errors=None, newline=None, closefd=True, opener=None):
|
|
||||||
if newline is not None:
|
|
||||||
warnings.warn('newline is not supported in py2')
|
|
||||||
if not closefd:
|
|
||||||
warnings.warn('closefd is not supported in py2')
|
|
||||||
if opener is not None:
|
|
||||||
warnings.warn('opener is not supported in py2')
|
|
||||||
return codecs.open(filename=file, mode=mode, encoding=encoding,
|
|
||||||
errors=errors, buffering=buffering)
|
|
||||||
|
|
||||||
HERE = os.path.abspath(os.path.dirname(__file__))
|
HERE = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -244,7 +244,7 @@ def test_actions_map_api():
|
||||||
assert ("POST", "/test-auth/subcat/post") in amap.parser.routes
|
assert ("POST", "/test-auth/subcat/post") in amap.parser.routes
|
||||||
|
|
||||||
|
|
||||||
def test_actions_map_import_error(mocker, builtin_str):
|
def test_actions_map_import_error(mocker):
|
||||||
from moulinette.interfaces.api import ActionsMapParser
|
from moulinette.interfaces.api import ActionsMapParser
|
||||||
|
|
||||||
amap = ActionsMap(ActionsMapParser())
|
amap = ActionsMap(ActionsMapParser())
|
||||||
|
@ -261,7 +261,7 @@ def test_actions_map_import_error(mocker, builtin_str):
|
||||||
raise ImportError
|
raise ImportError
|
||||||
return orig_import(name, globals, locals, fromlist, level)
|
return orig_import(name, globals, locals, fromlist, level)
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".__import__", side_effect=import_mock)
|
mocker.patch("builtins.__import__", side_effect=import_mock)
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
amap.process({}, timeout=30, route=("GET", "/test-auth/none"))
|
amap.process({}, timeout=30, route=("GET", "/test-auth/none"))
|
||||||
|
|
||||||
|
|
|
@ -39,10 +39,10 @@ def test_read_file_missing_file():
|
||||||
assert expected_msg in str(exception)
|
assert expected_msg in str(exception)
|
||||||
|
|
||||||
|
|
||||||
def test_read_file_cannot_read_ioerror(test_file, mocker, builtin_str):
|
def test_read_file_cannot_read_ioerror(test_file, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=IOError(error))
|
mocker.patch("builtins.open", side_effect=IOError(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
read_file(str(test_file))
|
read_file(str(test_file))
|
||||||
|
|
||||||
|
@ -51,10 +51,10 @@ def test_read_file_cannot_read_ioerror(test_file, mocker, builtin_str):
|
||||||
assert expected_msg in str(exception)
|
assert expected_msg in str(exception)
|
||||||
|
|
||||||
|
|
||||||
def test_read_file_cannot_read_exception(test_file, mocker, builtin_str):
|
def test_read_file_cannot_read_exception(test_file, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=Exception(error))
|
mocker.patch("builtins.open", side_effect=Exception(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
read_file(str(test_file))
|
read_file(str(test_file))
|
||||||
|
|
||||||
|
@ -133,10 +133,10 @@ def test_read_ldif(test_ldif):
|
||||||
assert entry["cn"] == ["Alice Alison".encode("utf-8")]
|
assert entry["cn"] == ["Alice Alison".encode("utf-8")]
|
||||||
|
|
||||||
|
|
||||||
def test_read_ldif_cannot_ioerror(test_ldif, mocker, builtin_str):
|
def test_read_ldif_cannot_ioerror(test_ldif, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=IOError(error))
|
mocker.patch("builtins.open", side_effect=IOError(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
read_ldif(str(test_ldif))
|
read_ldif(str(test_ldif))
|
||||||
|
|
||||||
|
@ -145,10 +145,10 @@ def test_read_ldif_cannot_ioerror(test_ldif, mocker, builtin_str):
|
||||||
assert expected_msg in str(exception)
|
assert expected_msg in str(exception)
|
||||||
|
|
||||||
|
|
||||||
def test_read_ldif_cannot_exception(test_ldif, mocker, builtin_str):
|
def test_read_ldif_cannot_exception(test_ldif, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=Exception(error))
|
mocker.patch("builtins.open", side_effect=Exception(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
read_ldif(str(test_ldif))
|
read_ldif(str(test_ldif))
|
||||||
|
|
||||||
|
@ -171,10 +171,10 @@ def test_write_to_new_file(tmp_path):
|
||||||
assert read_file(str(new_file)) == "yolo\nswag"
|
assert read_file(str(new_file)) == "yolo\nswag"
|
||||||
|
|
||||||
|
|
||||||
def test_write_to_existing_file_bad_perms(test_file, mocker, builtin_str):
|
def test_write_to_existing_file_bad_perms(test_file, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=IOError(error))
|
mocker.patch("builtins.open", side_effect=IOError(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
write_to_file(str(test_file), "yolo\nswag")
|
write_to_file(str(test_file), "yolo\nswag")
|
||||||
|
|
||||||
|
@ -183,10 +183,10 @@ def test_write_to_existing_file_bad_perms(test_file, mocker, builtin_str):
|
||||||
assert expected_msg in str(exception)
|
assert expected_msg in str(exception)
|
||||||
|
|
||||||
|
|
||||||
def test_write_to_file_exception(test_file, mocker, builtin_str):
|
def test_write_to_file_exception(test_file, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=Exception(error))
|
mocker.patch("builtins.open", side_effect=Exception(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
write_to_file(str(test_file), "yolo\nswag")
|
write_to_file(str(test_file), "yolo\nswag")
|
||||||
|
|
||||||
|
@ -238,12 +238,12 @@ def test_write_dict_to_json(tmp_path):
|
||||||
assert _json["bar"] == ["a", "b", "c"]
|
assert _json["bar"] == ["a", "b", "c"]
|
||||||
|
|
||||||
|
|
||||||
def test_write_json_to_existing_file_bad_perms(test_file, mocker, builtin_str):
|
def test_write_json_to_existing_file_bad_perms(test_file, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
|
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=IOError(error))
|
mocker.patch("builtins.open", side_effect=IOError(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
write_to_json(str(test_file), dummy_dict)
|
write_to_json(str(test_file), dummy_dict)
|
||||||
|
|
||||||
|
@ -252,12 +252,12 @@ def test_write_json_to_existing_file_bad_perms(test_file, mocker, builtin_str):
|
||||||
assert expected_msg in str(exception)
|
assert expected_msg in str(exception)
|
||||||
|
|
||||||
|
|
||||||
def test_write_json_to_file_exception(test_file, mocker, builtin_str):
|
def test_write_json_to_file_exception(test_file, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
|
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=Exception(error))
|
mocker.patch("builtins.open", side_effect=Exception(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
write_to_json(str(test_file), dummy_dict)
|
write_to_json(str(test_file), dummy_dict)
|
||||||
|
|
||||||
|
@ -276,10 +276,10 @@ def text_write_list_to_json(tmp_path):
|
||||||
assert _json == ["foo", "bar", "baz"]
|
assert _json == ["foo", "bar", "baz"]
|
||||||
|
|
||||||
|
|
||||||
def test_write_to_json_bad_perms(test_json, mocker, builtin_str):
|
def test_write_to_json_bad_perms(test_json, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=IOError(error))
|
mocker.patch("builtins.open", side_effect=IOError(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
write_to_json(str(test_json), {"a": 1})
|
write_to_json(str(test_json), {"a": 1})
|
||||||
|
|
||||||
|
@ -307,12 +307,12 @@ def test_write_dict_to_yaml(tmp_path):
|
||||||
assert _yaml["bar"] == ["a", "b", "c"]
|
assert _yaml["bar"] == ["a", "b", "c"]
|
||||||
|
|
||||||
|
|
||||||
def test_write_yaml_to_existing_file_bad_perms(test_file, mocker, builtin_str):
|
def test_write_yaml_to_existing_file_bad_perms(test_file, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
|
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=IOError(error))
|
mocker.patch("builtins.open", side_effect=IOError(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
write_to_yaml(str(test_file), dummy_dict)
|
write_to_yaml(str(test_file), dummy_dict)
|
||||||
|
|
||||||
|
@ -321,12 +321,12 @@ def test_write_yaml_to_existing_file_bad_perms(test_file, mocker, builtin_str):
|
||||||
assert expected_msg in str(exception)
|
assert expected_msg in str(exception)
|
||||||
|
|
||||||
|
|
||||||
def test_write_yaml_to_file_exception(test_file, mocker, builtin_str):
|
def test_write_yaml_to_file_exception(test_file, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
|
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=Exception(error))
|
mocker.patch("builtins.open", side_effect=Exception(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
write_to_yaml(str(test_file), dummy_dict)
|
write_to_yaml(str(test_file), dummy_dict)
|
||||||
|
|
||||||
|
@ -345,10 +345,10 @@ def text_write_list_to_yaml(tmp_path):
|
||||||
assert _yaml == ["foo", "bar", "baz"]
|
assert _yaml == ["foo", "bar", "baz"]
|
||||||
|
|
||||||
|
|
||||||
def test_write_to_yaml_bad_perms(test_yaml, mocker, builtin_str):
|
def test_write_to_yaml_bad_perms(test_yaml, mocker):
|
||||||
error = "foobar"
|
error = "foobar"
|
||||||
|
|
||||||
mocker.patch(builtin_str + ".open", side_effect=IOError(error))
|
mocker.patch("builtins.open", side_effect=IOError(error))
|
||||||
with pytest.raises(MoulinetteError) as exception:
|
with pytest.raises(MoulinetteError) as exception:
|
||||||
write_to_yaml(str(test_yaml), {"a": 1})
|
write_to_yaml(str(test_yaml), {"a": 1})
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,4 @@ def test_prependlines():
|
||||||
|
|
||||||
|
|
||||||
def test_random_ascii():
|
def test_random_ascii():
|
||||||
import sys
|
assert isinstance(random_ascii(length=2), str)
|
||||||
if sys.version_info[0] == 3:
|
|
||||||
assert isinstance(random_ascii(length=2), str)
|
|
||||||
else:
|
|
||||||
assert isinstance(random_ascii(length=2), unicode)
|
|
||||||
|
|
Loading…
Reference in a new issue