Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2cc4bf3e authored by Nicolas Gelot's avatar Nicolas Gelot
Browse files

Move to Python3

Move spot project to python 3. The support of python 2 is
removed related to the issue https://github.com/asciimoo/searx/issues/1471.

The update is mainly done with `2to3` tool and a lot of changes for
the unicode support (see https://docs.python.org/3/howto/unicode.html)

Close: #7
parent dc3f8b7b
Loading
Loading
Loading
Loading
+1 −5
Original line number Diff line number Diff line
FROM alpine:3.8
FROM python:3.7-alpine
LABEL maintainer="searx <https://github.com/asciimoo/searx>"
LABEL description="A privacy-respecting, hackable metasearch engine."

@@ -12,9 +12,6 @@ COPY requirements.txt ./requirements.txt

RUN apk -U add \
    build-base \
    python \
    python-dev \
    py-pip \
    libxml2 \
    libxml2-dev \
    libxslt \
@@ -27,7 +24,6 @@ RUN apk -U add \
 && pip install --no-cache -r requirements.txt \
 && apk del \
    build-base \
    python-dev \
    libffi-dev \
    openssl-dev \
    libxslt-dev \
+1 −1
Original line number Diff line number Diff line
@@ -5,4 +5,4 @@ RUN dnf install -y\
 python2-pip\
 npm\
&& dnf groupinstall -y "Development Tools" \
&& pip install pytest ipdb ipython \
&& pip3 install pytest ipdb ipython
+8 −8
Original line number Diff line number Diff line
@@ -16,14 +16,14 @@ ACTION="$1"
#

update_packages() {
    pip install --upgrade pip
    pip install --upgrade setuptools
    pip install -r "$BASE_DIR/requirements.txt"
    pip3 install --upgrade pip
    pip3 install --upgrade setuptools
    pip3 install -r "$BASE_DIR/requirements.txt"
}

update_dev_packages() {
    update_packages
    pip install -r "$BASE_DIR/requirements-dev.txt"
    pip3 install -r "$BASE_DIR/requirements-dev.txt"
}

install_geckodriver() {
@@ -36,7 +36,7 @@ install_geckodriver() {
        return
    fi
    GECKODRIVER_VERSION="v0.19.1"
    PLATFORM="`python -c "import six; import platform; six.print_(platform.system().lower(), platform.architecture()[0])"`"
    PLATFORM="`python3 -c "import platform; print(platform.system().lower(), platform.architecture()[0])"`"
    case "$PLATFORM" in
        "linux 32bit" | "linux2 32bit") ARCH="linux32";;
        "linux 64bit" | "linux2 64bit") ARCH="linux64";;
@@ -80,19 +80,19 @@ pep8_check() {

unit_tests() {
    echo '[!] Running unit tests'
    python -m nose2 -s "$BASE_DIR/tests/unit"
    python3 -m nose2 -s "$BASE_DIR/tests/unit"
}

py_test_coverage() {
    echo '[!] Running python test coverage'
    PYTHONPATH="`pwd`" python -m nose2 -C --log-capture --with-coverage --coverage "$SEARX_DIR" -s "$BASE_DIR/tests/unit" \
    PYTHONPATH="`pwd`" python3 -m nose2 -C --log-capture --with-coverage --coverage "$SEARX_DIR" -s "$BASE_DIR/tests/unit" \
    && coverage report \
    && coverage html
}

robot_tests() {
    echo '[!] Running robot tests'
    PYTHONPATH="`pwd`" python "$SEARX_DIR/testing.py" robot
    PYTHONPATH="`pwd`" python3 "$SEARX_DIR/testing.py" robot
}

tests() {
+3 −7
Original line number Diff line number Diff line
from os import listdir
from os.path import realpath, dirname, join, isdir
from sys import version_info
from searx.utils import load_module
from collections import defaultdict

if version_info[0] == 3:
    unicode = str


answerers_dir = dirname(realpath(__file__))

@@ -34,12 +30,12 @@ def get_answerers_by_keywords(answerers):

def ask(query):
    results = []
    query_parts = list(filter(None, query.query.split()))
    query_parts = list([_f for _f in query.query.split() if _f])

    if query_parts[0].decode('utf-8') not in answerers_by_keywords:
    if query_parts[0] not in answerers_by_keywords:
        return results

    for answerer in answerers_by_keywords[query_parts[0].decode('utf-8')]:
    for answerer in answerers_by_keywords[query_parts[0]]:
        result = answerer(query)
        if result:
            results.append(result)
+13 −17
Original line number Diff line number Diff line
@@ -11,10 +11,6 @@ keywords = ('random',)

random_int_max = 2**31

if sys.version_info[0] == 2:
    random_string_letters = string.lowercase + string.digits + string.uppercase
else:
    unicode = str
random_string_letters = string.ascii_lowercase + string.digits + string.ascii_uppercase


@@ -24,32 +20,32 @@ def random_characters():


def random_string():
    return u''.join(random_characters())
    return ''.join(random_characters())


def random_float():
    return unicode(random.random())
    return str(random.random())


def random_int():
    return unicode(random.randint(-random_int_max, random_int_max))
    return str(random.randint(-random_int_max, random_int_max))


def random_sha256():
    m = hashlib.sha256()
    m.update(b''.join(random_characters()))
    return unicode(m.hexdigest())
    m.update(''.join(random_characters()).encode())
    return m.hexdigest()


def random_uuid():
    return unicode(uuid.uuid4())
    return str(uuid.uuid4())


random_types = {b'string': random_string,
                b'int': random_int,
                b'float': random_float,
                b'sha256': random_sha256,
                b'uuid': random_uuid}
random_types = {'string': random_string,
                'int': random_int,
                'float': random_float,
                'sha256': random_sha256,
                'uuid': random_uuid}


# required answerer function
@@ -70,4 +66,4 @@ def answer(query):
def self_info():
    return {'name': gettext('Random value generator'),
            'description': gettext('Generate different random values'),
            'examples': [u'random {}'.format(x) for x in random_types]}
            'examples': ['random {}'.format(x) for x in random_types]}
Loading