<commit_before># Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""App Engine data model (schema) definition for Chromium port of Rietveld."""

import json
import logging
import sys

from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.ext import db


class UrlMap(db.Model):
  """Mapping between base url and source code viewer url."""

  base_url_template = db.StringProperty(required=True)
  source_code_url_template = db.StringProperty(required=True)


class Key(db.Model):
  """Hash to be able to push data from a server."""
  hash = db.StringProperty()


def to_dict(self):
  """Converts a db.Model instance into a dict.

  Useful for json serialization.
  """
  def convert(item):
    if isinstance(item, (int, float, None.__class__, bool)):
      return item
    elif isinstance(item, (list, tuple)):
      return [convert(i) for i in item]
    elif isinstance(item, users.User):
      return item.email()
    else:
      return unicode(item)
  result = dict([(p, convert(getattr(self, p))) for p in self.properties()])
  try:
    result['key'] = str(self.key())
  except db.NotSavedError:
    pass
  return result

# Monkey-patch db.Model to make it easier to JSON-serialize it.
db.Model.to_dict = to_dict


class DefaultBuilderList(db.Model):
  """An instance to hold the list of default builder names for trying patchsets.

  Each instance of this class holds the list of builder names for use with
  a given build master server.  Instances are updated periodically by
  a cron job (not very often since this list does not change quickly) and
  are used while processing the show() view.

  The key of the instance is the name of the build master server.

  Attributes:
    default_builders: List of strings, where each string is the name of one
        builder for the given try server.
  """
  # Constants for default builders memcache.
  _DEFAULT_BUILDER_MEMCACHE_KEY = 'default_builders_'
  _DEFAULT_BUILDER_MEMCACHE_EXPIRY_SECS = 60 * 60 * 12

  _DEFAULT_CHROMIUM_TRYSERVER_NAME = 'tryserver.chromium'

  default_builders = db.StringListProperty(default=[])

  @classmethod
  def _get_instance(cls, name):
    """Gets the single instance of the default builder list.

    Args:
      name: The name of the build master, like 'tryserver.chromium'.

    Returns:
      An instance of DefaultBuilderList for the given build master.
    """
    return cls.get_or_insert(name, default_builders=[])

  @classmethod
  def get_builders(cls, base_url):
    """Gets the list of default builders.

    This function will first attempt to get the list from the memcache. If its
    not available there, it will get it from the datastore and then update the
    memcache.

    Args:
      base_url: The base URL we want a list of default builders for.

    Returns:
      A list of strings where each string is the name of one builder. The
      names are sorted alphabetically.
    """
    # Remove contents after '@' to avoid explosion due to git branches.
    base_url = base_url.rsplit('@', 1)[0]
    # Look for the list of builders using the specified base url in memcache.
    base_url_key = cls._DEFAULT_BUILDER_MEMCACHE_KEY + base_url
    builders = memcache.get(base_url_key, namespace=base_url)
    if builders is None:
      # If the list of builders does not exist in memcache get it from the
      # datastore in 2 steps.
      # 1. Get the tryserver name from the datastore using the base_url.
      tryserver_name = BaseUrlTryServer.get_instance(<commit_msg>Handle missing base URLs on issues<commit_after># Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""App Engine data model (schema) definition for Chromium port of Rietveld."""

import json
import logging
import sys

from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.ext import db


class UrlMap(db.Model):
  """Mapping between base url and source code viewer url."""

  base_url_template = db.StringProperty(required=True)
  source_code_url_template = db.StringProperty(required=True)


class Key(db.Model):
  """Hash to be able to push data from a server."""
  hash = db.StringProperty()


def to_dict(self):
  """Converts a db.Model instance into a dict.

  Useful for json serialization.
  """
  def convert(item):
    if isinstance(item, (int, float, None.__class__, bool)):
      return item
    elif isinstance(item, (list, tuple)):
      return [convert(i) for i in item]
    elif isinstance(item, users.User):
      return item.email()
    else:
      return unicode(item)
  result = dict([(p, convert(getattr(self, p))) for p in self.properties()])
  try:
    result['key'] = str(self.key())
  except db.NotSavedError:
    pass
  return result

# Monkey-patch db.Model to make it easier to JSON-serialize it.
db.Model.to_dict = to_dict


class DefaultBuilderList(db.Model):
  """An instance to hold the list of default builder names for trying patchsets.

  Each instance of this class holds the list of builder names for use with
  a given build master server.  Instances are updated periodically by
  a cron job (not very often since this list does not change quickly) and
  are used while processing the show() view.

  The key of the instance is the name of the build master server.

  Attributes:
    default_builders: List of strings, where each string is the name of one
        builder for the given try server.
  """
  # Constants for default builders memcache.
  _DEFAULT_BUILDER_MEMCACHE_KEY = 'default_builders_'
  _DEFAULT_BUILDER_MEMCACHE_EXPIRY_SECS = 60 * 60 * 12

  _DEFAULT_CHROMIUM_TRYSERVER_NAME = 'tryserver.chromium'

  default_builders = db.StringListProperty(default=[])

  @classmethod
  def _get_instance(cls, name):
    """Gets the single instance of the default builder list.

    Args:
      name: The name of the build master, like 'tryserver.chromium'.

    Returns:
      An instance of DefaultBuilderList for the given build master.
    """
    return cls.get_or_insert(name, default_builders=[])

  @classmethod
  def get_builders(cls, base_url):
    """Gets the list of default builders.

    This function will first attempt to get the list from the memcache. If its
    not available there, it will get it from the datastore and then update the
    memcache.

    Args:
      base_url: The base URL we want a list of default builders for.

    Returns:
      A list of strings where each string is the name of one builder. The
      names are sorted alphabetically.
    """
    # Remove contents after '@' to avoid explosion due to git branches.
    base_url = base_url.rsplit('@', 1)[0]
    # Look for the list of builders using the specified base url in memcache.
    base_url_key = cls._DEFAULT_BUILDER_MEMCACHE_KEY + base_url
    builders = memcache.get(base_url_key, namespace=base_url)
    if builders is None:
      # If the list of builders does not exist in memcache get it from the
      # datastore in 2 steps.
      # 1. Get the tryserver name from the datastore using the base_url.
      if not base_url:
        # Do not support try servers if the base URL is missing.
        return []
      tryserver_name = BaseUrlTryServer.get_instance(
{'fraction_non_alphanumeric': 0.0737044607430564, 'fraction_numerical': 0.004328483828303475, 'mean_word_length': 3.585446527012128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2619337', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2335, 'n_words': 1007}