def _filter_select_dateds(context: Context, dateds: Iterable[Dated], date: Optional[Datey]) -> Iterator[Dated]: if date is None: date = resolve_or_missing(context, 'today') return filter( lambda dated: dated.date is None or dated.date.comparable and dated. date in date, dateds)
def _filter_select_localizeds( context: Context, localizeds: Iterable[Localized]) -> Iterable[Localized]: locale = resolve_or_missing(context, 'locale') for localized in localizeds: if negotiate_locale(locale, [localized.locale]) is not None: yield localized
async def _filter_wikipedia_links( self, context, links: Iterable[Link]) -> Iterable[Entry]: locale = parse_locale(resolve_or_missing(context, 'locale'), '-')[0] return filter( None, await asyncio.gather( *[self._filter_wikipedia_link(locale, link) for link in links]))
def _filter_url(context: Context, resource: Any, media_type: Optional[str] = None, locale: Optional[str] = None, **kwargs) -> str: media_type = 'text/html' if media_type is None else media_type locale = locale if locale else resolve_or_missing(context, 'locale') return context.environment.app.localized_url_generator.generate( resource, media_type, locale=locale, **kwargs)
def _filter_wikipedia_links(self, context, links: Iterable[Link]) -> Iterable[Entry]: locale = parse_locale(resolve_or_missing(context, 'locale'), '-')[0] futures = [ self._app.executor.submit(self._filter_wikipedia_link, locale, link) for link in links ] return filter(None, [future.result() for future in as_completed(futures)])
def resolve(self, key): if self._legacy_resolve_mode: rv = resolve_or_missing(self, key) else: rv = self.resolve_or_missing(key) if rv is missing: raise KeyError(key) if rv is None: raise ValueError(key) return rv
def _filter_json(context: Context, data: Any, indent: Optional[int] = None) -> str: """ Converts a value to a JSON string. """ return stdjson.dumps(data, indent=indent, cls=JSONEncoder.get_factory( context.environment.app, resolve_or_missing(context, 'locale')))
def _filter_url(context, resource, media_type=None, locale=None, **kwargs): media_type = media_type if media_type else 'text/html' locale = locale if locale else resolve_or_missing( context, 'locale') return site.localized_url_generator.generate(resource, media_type, locale=locale, **kwargs)
def _filter_select_localizeds( context: Context, localizeds: Iterable[Localized], include_unspecified: bool = False) -> Iterable[Localized]: locale = resolve_or_missing(context, 'locale') for localized in localizeds: if include_unspecified and localized.locale in { None, 'mis', 'mul', 'und', 'zxx' }: yield localized if localized.locale is not None and negotiate_locale( locale, [localized.locale]) is not None: yield localized
def _filter_sort_localizeds(context: Context, localizeds: Iterable[Localized], localized_attribute: str, sort_attribute: str) -> Iterable[Localized]: locale = resolve_or_missing(context, 'locale') get_localized_attr = make_attrgetter(context.environment, localized_attribute) get_sort_attr = make_attrgetter(context.environment, sort_attribute) def _get_sort_key(x): return get_sort_attr( negotiate_localizeds(locale, get_localized_attr(x))) return sorted(localizeds, key=_get_sort_key)
def _filter_format_date(context: Context, date: Datey) -> str: locale = resolve_or_missing(context, 'locale') return format_datey(date, locale)
def _filter_negotiate_localizeds( context: Context, localizeds: Iterable[Localized]) -> Optional[Localized]: locale = resolve_or_missing(context, 'locale') return negotiate_localizeds(locale, list(localizeds))
def _filter_json(context, data, indent=None): return stdjson.dumps(data, indent=indent, cls=JSONEncoder.get_factory( site, resolve_or_missing(context, 'locale')))