Skip to content

Commit

Permalink
Merge pull request #2329 from cjmayo/compat1
Browse files Browse the repository at this point in the history
Start removing code from _compat.py
  • Loading branch information
alanhamlett authored Oct 15, 2023
2 parents 6509ef9 + 12d0182 commit 073766a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 59 deletions.
66 changes: 13 additions & 53 deletions flask_admin/_compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# flake8: noqa
"""
flask_admin._compat
Expand All @@ -11,58 +10,23 @@
:copyright: (c) 2013 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
import sys

PY2 = sys.version_info[0] == 2
VER = sys.version_info
text_type = str
string_types = (str,)

if not PY2:
text_type = str
string_types = (str,)
integer_types = (int, )
itervalues = lambda d: iter(d.values())
iteritems = lambda d: iter(d.items())
filter_list = lambda f, l: list(filter(f, l))

iterkeys = lambda d: iter(d.keys())
itervalues = lambda d: iter(d.values())
iteritems = lambda d: iter(d.items())
filter_list = lambda f, l: list(filter(f, l))
def as_unicode(s):
if isinstance(s, bytes):
return s.decode('utf-8')

def as_unicode(s):
if isinstance(s, bytes):
return s.decode('utf-8')
return str(s)

return str(s)

def csv_encode(s):
''' Returns unicode string expected by Python 3's csv module '''
return as_unicode(s)

# Various tools
from functools import reduce
from urllib.parse import urljoin, urlparse, quote
else:
text_type = unicode
string_types = (str, unicode)
integer_types = (int, long)

iterkeys = lambda d: d.iterkeys()
itervalues = lambda d: d.itervalues()
iteritems = lambda d: d.iteritems()
filter_list = filter

def as_unicode(s):
if isinstance(s, str):
return s.decode('utf-8')

return unicode(s)

def csv_encode(s):
''' Returns byte string expected by Python 2's csv module '''
return as_unicode(s).encode('utf-8')

# Helpers
reduce = __builtins__['reduce'] if isinstance(__builtins__, dict) else __builtins__.reduce
from urlparse import urljoin, urlparse
from urllib import quote
def csv_encode(s):
''' Returns unicode string expected by Python 3's csv module '''
return as_unicode(s)


def with_metaclass(meta, *bases):
Expand All @@ -87,11 +51,7 @@ def __new__(cls, name, this_bases, d):


try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict

try:
# jinja2 3.0.0
from jinja2 import pass_context
except ImportError:
from jinja2 import contextfunction as pass_context
3 changes: 2 additions & 1 deletion flask_admin/contrib/fileadmin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import re
import shutil
from operator import itemgetter
from urllib.parse import urljoin, quote

from flask import flash, redirect, abort, request, send_file
from werkzeug.utils import secure_filename
from wtforms import fields, validators

from flask_admin import form, helpers
from flask_admin._compat import urljoin, as_unicode, quote
from flask_admin._compat import as_unicode
from flask_admin.base import BaseView, expose
from flask_admin.actions import action, ActionsMixin
from flask_admin.babel import gettext, lazy_gettext
Expand Down
3 changes: 2 additions & 1 deletion flask_admin/form/upload.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import os.path as op
from urllib.parse import urljoin

from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
Expand All @@ -12,7 +13,7 @@
from flask_admin.helpers import get_url

from flask_admin._backwards import Markup
from flask_admin._compat import string_types, urljoin
from flask_admin._compat import string_types


try:
Expand Down
4 changes: 3 additions & 1 deletion flask_admin/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from re import sub, compile
from urllib.parse import urljoin, urlparse

from flask import g, request, url_for, flash
from wtforms.validators import DataRequired, InputRequired

from flask_admin._compat import iteritems, pass_context, urljoin, urlparse
from flask_admin._compat import iteritems, pass_context

from ._compat import string_types

Expand Down
3 changes: 2 additions & 1 deletion flask_admin/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
from math import ceil
import inspect
from collections import OrderedDict

from werkzeug.utils import secure_filename

Expand All @@ -28,7 +29,7 @@
get_redirect_target, flash_errors)
from flask_admin.tools import rec_getattr
from flask_admin._backwards import ObsoleteAttr
from flask_admin._compat import (iteritems, itervalues, OrderedDict,
from flask_admin._compat import (iteritems, itervalues,
as_unicode, csv_encode, text_type, pass_context)
from .helpers import prettify_name, get_mdict_item_or_list
from .ajax import AjaxModelLoader
Expand Down
4 changes: 3 additions & 1 deletion flask_admin/model/template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from flask_admin._compat import pass_context, string_types, reduce
from functools import reduce

from flask_admin._compat import pass_context, string_types
from flask_admin.babel import gettext


Expand Down
3 changes: 2 additions & 1 deletion flask_admin/tools.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from functools import reduce
import sys
import traceback

# Python 3 compatibility
from ._compat import reduce, as_unicode
from ._compat import as_unicode

CHAR_ESCAPE = u'.'
CHAR_SEPARATOR = u','
Expand Down

0 comments on commit 073766a

Please sign in to comment.