Skip to content

Commit

Permalink
Replace star imports with explicit imports and exports (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Apr 10, 2024
1 parent bc048e8 commit 85fb1f5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 deletions.
49 changes: 38 additions & 11 deletions src/baseframe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,68 @@

from typing import cast

from flask_assets import Bundle
from flask_babel import gettext, lazy_gettext

from . import (
blueprint,
deprecated,
errors,
extensions,
filters,
forms,
signals,
utils,
views,
)
from ._version import __version__, __version_info__
from .assets import Version, assets
from .blueprint import * # NOQA
from .deprecated import * # NOQA
from .extensions import * # NOQA
from .extensions import GetTextProtocol
from .filters import * # NOQA
from .utils import * # NOQA
from .views import * # NOQA

from . import forms # isort:skip
from .blueprint import baseframe
from .deprecated import baseframe_css, baseframe_js
from .extensions import GetTextProtocol, babel, baseframe_translations, cache, statsd
from .utils import (
ctx_has_locale,
localize_timezone,
localized_country_list,
request_checked_xhr,
request_is_xhr,
)

# Pretend these return str, not Any or LazyString
_ = cast(GetTextProtocol, gettext)
__ = cast(GetTextProtocol, lazy_gettext)

# TODO: baseframe_js and baseframe_css are defined in deprecated.py
# and pending removal after an audit of all apps
__all__ = [ # noqa: F405
__all__ = [
'_',
'__',
'__version__',
'__version_info__',
'assets',
'babel',
'baseframe',
'baseframe_css',
'baseframe_js',
'baseframe_translations',
'baseframe',
'blueprint',
'Bundle',
'cache',
'ctx_has_locale',
'deprecated',
'errors',
'extensions',
'filters',
'filters',
'forms',
'forms',
'localize_timezone',
'localized_country_list',
'request_checked_xhr',
'request_is_xhr',
'signals',
'statsd',
'utils',
'Version',
'views',
]
2 changes: 1 addition & 1 deletion src/baseframe/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from coaster.assets import Version, VersionedAssets

from . import __version__
from ._version import __version__

__all__ = ['Version', 'assets']

Expand Down
14 changes: 13 additions & 1 deletion src/baseframe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pycountry
from babel import Locale
from flask import g, request
from flask_babel import LazyString
from flask_babel import LazyString, _get_current_context
from furl import furl
from mxsniff import mxsniff
from pytz import timezone, utc
Expand Down Expand Up @@ -169,3 +169,15 @@ def request_is_xhr() -> bool:
def request_checked_xhr() -> bool:
"""Confirm if XHR header was checked for during this request."""
return getattr(request, '_checked_xhr', False)


def ctx_has_locale() -> bool:
"""
Report if Babel was used in the current context.
For setting a ``Vary: Accept-Language`` header in the response.
"""
ctx = _get_current_context()
if ctx is None:
return False
return hasattr(ctx, 'babel_locale')
15 changes: 1 addition & 14 deletions src/baseframe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
send_from_directory,
)
from flask_assets import Bundle
from flask_babel import _get_current_context
from flask_wtf.csrf import generate_csrf

from coaster.assets import split_namespec
Expand All @@ -27,19 +26,7 @@
from .assets import assets as assets_repo
from .blueprint import baseframe
from .extensions import networkbar_cache
from .utils import request_checked_xhr, request_timestamp


def ctx_has_locale() -> bool:
"""
Report if Babel was used in the current context.
For setting a ``Vary: Accept-Language`` header in the response.
"""
ctx = _get_current_context()
if ctx is None:
return False
return hasattr(ctx, 'babel_locale')
from .utils import ctx_has_locale, request_checked_xhr, request_timestamp


@networkbar_cache.cached(key_prefix='networkbar_links')
Expand Down

0 comments on commit 85fb1f5

Please sign in to comment.