<commit_before>from . import base
from . import messages
from . import tags
from grow.common import utils
from grow.pods import errors
from grow.pods.storage import gettext_storage as gettext
import locale
import logging
import mimetypes


class RenderedController(base.BaseController):
  KIND = messages.Kind.RENDERED

  class Defaults(object):
    LL = 'en'
    LOCALE = None
    CC = None

  def __init__(self, view=None, document=None, path=None, _pod=None):
    self.view = view
    self.document = document
    self.path = path
    super(RenderedController, self).__init__(_pod=_pod)

  def __repr__(self):
    if not self.document:
      return '<Rendered(view=\'{}\')>'.format(self.view)
    return '<Rendered(view=\'{}\', document=\'{}\')>'.format(
        self.view, self.document.pod_path)

  @property
  def mimetype(self):
    return mimetypes.guess_type(self.view)[0]

  @property
  def locale(self):
    if self.document:
      return self.document.locale

  @property
  def ll(self):
    return self.route_params.get('ll', RenderedController.Defaults.LL)

  @property
  def cc(self):
    return self.route_params.get('cc', RenderedController.Defaults.CC)

  @property
  @utils.memoize
  def _template_env(self):
    return self.pod.get_template_env()

  def _install_translations(self, ll):
    if ll is None:
      gettext_translations = gettext.NullTranslations()
    else:
      catalog = self.pod.catalogs.get(ll)
      gettext_translations = catalog.gettext_translations
    self._template_env.uninstall_gettext_translations(None)
    self._template_env.install_gettext_translations(gettext_translations, newstyle=True)

  def list_concrete_paths(self):
    if self.path:
      return [self.path]
    if not self.document:
      raise
    return [self.document.get_serving_path()]

  def render(self):
    # TODO(jeremydw): This is a bit hacky. Be more explicit about translations.
    ll = self.locale
    self._install_translations(ll)
    if self.locale is None:  # Use the system's default locale.
      locale.setlocale(locale.LC_ALL, '')
    else:
      try:
        # TODO(jeremydw): Convert from Babel locale to system locale.
        locale.setlocale(locale.LC_TIME, str(self.locale))
      except locale.Error:
        pass
    template = self._template_env.get_template(self.view.lstrip('/'))
    g = {
        'breadcrumb': lambda *args, **kwargs: tags.breadcrumb(*args, _pod=self.pod, **kwargs),
        'categories': lambda *args, **kwargs: tags.categories(*args, _pod=self.pod, **kwargs),
        'cc': self.cc,
        'csv': lambda *args, **kwargs: tags.csv(*args, _pod=self.pod, **kwargs),
        'date': lambda *args, **kwargs: tags.date(*args, _pod=self.pod, **kwargs),
        'doc': lambda *args, **kwargs: tags.get_doc(*args, _pod=self.pod, **kwargs),
        'docs': lambda *args, **kwargs: tags.docs(*args, _pod=self.pod, **kwargs),
        'll': self.ll,
        'locales': lambda *args, **kwargs: tags.locales(*args, _pod=self.pod, **kwargs),
        'nav': lambda *args, **kwargs: tags.nav(*args, _pod=self.pod, **kwargs),
        'params': self.route_params,
        'static': lambda path: tags.static(path, _pod=self.pod),
        'url': lambda *args, **kwargs: tags.url(*args, _pod=self.pod, **kwargs),
    }
    try:
      return template.render({
          'g': g,
          'doc': self.document,
          'env': self.pod.env,
          'podspec': self.pod.get_podspec(),
      }).lstrip()
    except Exception as e:
      text = 'Error building {}: {}'
      logging.exception(e)
      raise errors.BuildError(text.format(self, e))
<commit_msg>Attempt to use plain system locale, and system locale with 'utf-8' suffix.<commit_after>from . import base
from . import messages
from . import tags
from grow.common import utils
from grow.pods import errors
from grow.pods.storage import gettext_storage as gettext
import locale
import logging
import mimetypes


class RenderedController(base.BaseController):
  KIND = messages.Kind.RENDERED

  class Defaults(object):
    LL = 'en'
    LOCALE = None
    CC = None

  def __init__(self, view=None, document=None, path=None, _pod=None):
    self.view = view
    self.document = document
    self.path = path
    super(RenderedController, self).__init__(_pod=_pod)

  def __repr__(self):
    if not self.document:
      return '<Rendered(view=\'{}\')>'.format(self.view)
    return '<Rendered(view=\'{}\', document=\'{}\')>'.format(
        self.view, self.document.pod_path)

  @property
  def mimetype(self):
    return mimetypes.guess_type(self.view)[0]

  @property
  def locale(self):
    if self.document:
      return self.document.locale

  @property
  def ll(self):
    return self.route_params.get('ll', RenderedController.Defaults.LL)

  @property
  def cc(self):
    return self.route_params.get('cc', RenderedController.Defaults.CC)

  @property
  @utils.memoize
  def _template_env(self):
    return self.pod.get_template_env()

  def _install_translations(self, ll):
    if ll is None:
      gettext_translations = gettext.NullTranslations()
    else:
      catalog = self.pod.catalogs.get(ll)
      gettext_translations = catalog.gettext_translations
    self._template_env.uninstall_gettext_translations(None)
    self._template_env.install_gettext_translations(gettext_translations, newstyle=True)

  def list_concrete_paths(self):
    if self.path:
      return [self.path]
    if not self.document:
      raise
    return [self.document.get_serving_path()]

  def render(self):
    # TODO(jeremydw): This is a bit hacky. Be more explicit about translations.
    ll = self.locale
    self._install_translations(ll)
    if self.locale is None:  # Use the system's default locale.
      locale.setlocale(locale.LC_ALL, '')
    else:
      # TODO(jeremydw): Properly convert from Babel locale to system locale,
      # detect system locale formatting and raise an error if the locale isn't
      # installed.
      try:
        locale.setlocale(locale.LC_TIME, str(self.locale))
      except locale.Error:
        try:
          locale.setlocale(locale.LC_TIME, str(self.locale) + '.utf-8')
        except locale.Error:
          pass
    template = self._template_env.get_template(self.view.lstrip('/'))
    g = {
        'breadcrumb': lambda *args, **kwargs: tags.breadcrumb(*args, _pod=self.pod, **kwargs),
        'categories': lambda *args, **kwargs: tags.categories(*args, _pod=self.pod, **kwargs),
        'cc': self.cc,
        'csv': lambda *args, **kwargs: tags.csv(*args, _pod=self.pod, **kwargs),
        'date': lambda *args, **kwargs: tags.date(*args, _pod=self.pod, **kwargs),
        'doc': lambda *args, **kwargs: tags.get_doc(*args, _pod=self.pod, **kwargs),
        'docs': lambda *args, **kwargs: tags.docs(*args, _pod=self.pod, **kwargs),
        'll': self.ll,
        'locales': lambda *args, **kwargs: tags.locales(*args, _pod=self.pod, **kwargs),
        'nav': lambda *args, **kwargs: tags.nav(*args, _pod=self.pod, **kwargs),
        'params': self.route_params,
        'static': lambda path: tags.static(path, _pod=self.pod),
        'url': lambda *args, **kwargs: tags.url(*args, _pod=self.pod, **kwargs),
    }
    try:
      return template.render({
          'g': g,
          'doc': self.document,
          'env': self.pod.env,
          'podspec': self.pod.get_podspec(),
      }).lstrip()
    except Exception as e:
      text = 'Error building {}: {}'
      logging.exception(e)
      raise errors.BuildError(text.format(self, e))

{'fraction_non_alphanumeric': 0.1377605558524853, 'fraction_numerical': 0.0005344735435595938, 'mean_word_length': 3.250425894378194, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5429136', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2386, 'n_words': 565}