<commit_before>__author__                      = '<NAME>'
__copyright__                   = 'Copyright 2010 - 2012, <NAME>'
__contact__                     = '<EMAIL>'
__license__                     = 'http://www.php-suit.com/dpl'

import threading

import hlib.api
import hlib.database
import hlib.error

import lib
import lib.chat

# pylint: disable-msg=F0401
import hruntime

class PlayableError(hlib.error.BaseError):
  pass

class WrongPasswordError(PlayableError):
  def __init__(self, *args, **kwargs):
    kwargs.update({
      'msg':			'Wrong password',
      'reply_status':		401,
      'dont_log':		True
    })

    super(WrongPasswordError, self).__init__(*args, **kwargs)

class AlreadyStartedError(PlayableError):
  def __init__(self, *args, **kwargs):
    kwargs.update({
      'msg':			'Already started',
      'reply_status':		401
    })

    super(AlreadyStartedError, self).__init__(*args, **kwargs)

class AlreadyJoinedError(PlayableError):
  def __init__(self, *args, **kwargs):
    kwargs.update({
      'msg':			'Already joined',
      'reply_status':		402
    })

    super(AlreadyJoinedError, self).__init__(*args, **kwargs)

class CannotBeArchivedError(PlayableError):
  def __init__(self, *args, **kwargs):
    kwargs.update({
      'msg':			'Can not be archived yet',
      'reply_status':		402
    })

    super(CannotBeArchivedError, self).__init__(*args, **kwargs)

class Player(hlib.database.DBObject):
  def __init__(self, user):
    hlib.database.DBObject.__init__(self)

    self.user			= user
    self.confirmed		= True
    self.last_board		= -1

  def __getattr__(self, name):
    if name == 'is_on_turn':
      return False

    return hlib.database.DBObject.__getattr__(self, name)

  def to_api(self):
    return {
      'user':			hlib.api.User(self.user),
      'is_confirmed':		self.confirmed,
      'is_on_turn':		self.is_on_turn
    }

  def to_state(self):
    return {
      'user':			hlib.api.User(self.user)
    }

class Playable(hlib.database.DBObject):
  def __init__(self, flags):
    hlib.database.DBObject.__init__(self)

    self.flags			= flags

    self.id			= None
    self.name			= flags.name
    self.owner			= flags.owner
    self.password		= <PASSWORD>
    self.limit			= flags.limit
    self.round			= 0
    self.kind			= flags.kind
    self.last_pass		= hruntime.time

    self.chat_posts		= lib.chat.ChatPosts()

    self.events			= hlib.database.IndexedMapping()

    self._v_user_to_player	= None

    if not flags.password or flags.password == '':
      self.password = None

    else:
      self.password = lib.pwcrypt(flags.password)

  def __getattr__(self, name):
    if name == 'user_to_player':
      if not hasattr(self, '_v_user_to_player') or self._v_user_to_player == None:
        self._v_user_to_player = lib.UserToPlayerMap(self)

      return self._v_user_to_player

    if name == 'my_player':
      return self.user_to_player[hruntime.user]

    if name == 'is_password_protected':
      return self.password != None and len(self.password) > 0

    if name == 'archive_deadline':
      if self.is_active:
        raise CannotBeArchivedError()

      atime = self.last_pass

      for p in self.players.values():
        chat_lister = self.chat_class(self, accessed_by = p)
        if chat_lister.unread > 0:
          raise CannotBeArchivedError()

        atime = max(atime, p.last_board)

      return atime + (86400 * 7)

    if name == 'can_be_archived':
      try:
        if hruntime.time > self.archive_deadline:
          return True

      except CannotBeArchivedError, e:
        return False

      return False

    return hlib.database.DBObject.__getattr__(self, name)

  def has_player(self, user):
    return user in self.user_to_player

  def to_api(self):
    ret = {
      'id':			self.id,
      'kind':			self.kind,
      'name':			self.name,
      'round':			self.round,
      'is_finished':		self.is_finished,
      'is_present':		self.has_player(hruntime.user),
      'has_password':		self.is_password_protected,
      'chat_posts':		self.chat.unread if self.chat.unread > 0 else False,
      'players':		[p.to_api() for p in self.players.values()]
    }

    try:
      ret['archive_deadline']	= self.archive_deadline
    except CannotBeArchivedError:
      ret['archive_deadline']	= False

    return ret

  def to_state(self):
    return {
      'name':			self.name,
      'round':			self.round,
      'limit':			self.limit,
      'players':		[p.to_state() for p in self.players.values()],
      'events':			[e.to_api() for e in self.events.values() if e.hidden != True]
    }

class PlayableLists(object):
  def __init__(self):
    super(PlayableLists, self).__init__()

    self._lock          = threading.RLock()

    self._active        = {}
    self._inactive      = {}
    self._archived      = {}

  def __get_f_list(self, name, user):
    cache = getattr(self, '_' + name)

    with self._lock:
      if user not in cache:<commit_msg>Fixed cache access<commit_after>__author__                      = '<NAME>'
__copyright__                   = 'Copyright 2010 - 2012, <NAME>'
__contact__                     = '<EMAIL>'
__license__                     = 'http://www.php-suit.com/dpl'

import threading

import hlib.api
import hlib.database
import hlib.error

import lib
import lib.chat

# pylint: disable-msg=F0401
import hruntime

class PlayableError(hlib.error.BaseError):
  pass

class WrongPasswordError(PlayableError):
  def __init__(self, *args, **kwargs):
    kwargs.update({
      'msg':			'Wrong password',
      'reply_status':		401,
      'dont_log':		True
    })

    super(WrongPasswordError, self).__init__(*args, **kwargs)

class AlreadyStartedError(PlayableError):
  def __init__(self, *args, **kwargs):
    kwargs.update({
      'msg':			'Already started',
      'reply_status':		401
    })

    super(AlreadyStartedError, self).__init__(*args, **kwargs)

class AlreadyJoinedError(PlayableError):
  def __init__(self, *args, **kwargs):
    kwargs.update({
      'msg':			'Already joined',
      'reply_status':		402
    })

    super(AlreadyJoinedError, self).__init__(*args, **kwargs)

class CannotBeArchivedError(PlayableError):
  def __init__(self, *args, **kwargs):
    kwargs.update({
      'msg':			'Can not be archived yet',
      'reply_status':		402
    })

    super(CannotBeArchivedError, self).__init__(*args, **kwargs)

class Player(hlib.database.DBObject):
  def __init__(self, user):
    hlib.database.DBObject.__init__(self)

    self.user			= user
    self.confirmed		= True
    self.last_board		= -1

  def __getattr__(self, name):
    if name == 'is_on_turn':
      return False

    return hlib.database.DBObject.__getattr__(self, name)

  def to_api(self):
    return {
      'user':			hlib.api.User(self.user),
      'is_confirmed':		self.confirmed,
      'is_on_turn':		self.is_on_turn
    }

  def to_state(self):
    return {
      'user':			hlib.api.User(self.user)
    }

class Playable(hlib.database.DBObject):
  def __init__(self, flags):
    hlib.database.DBObject.__init__(self)

    self.flags			= flags

    self.id			= None
    self.name			= flags.name
    self.owner			= flags.owner
    self.password		= <PASSWORD>.password
    self.limit			= flags.limit
    self.round			= 0
    self.kind			= flags.kind
    self.last_pass		= hruntime.time

    self.chat_posts		= lib.chat.ChatPosts()

    self.events			= hlib.database.IndexedMapping()

    self._v_user_to_player	= None

    if not flags.password or flags.password == '':
      self.password = None

    else:
      self.password = lib.pwcrypt(flags.password)

  def __getattr__(self, name):
    if name == 'user_to_player':
      if not hasattr(self, '_v_user_to_player') or self._v_user_to_player == None:
        self._v_user_to_player = lib.UserToPlayerMap(self)

      return self._v_user_to_player

    if name == 'my_player':
      return self.user_to_player[hruntime.user]

    if name == 'is_password_protected':
      return self.password != None and len(self.password) > 0

    if name == 'archive_deadline':
      if self.is_active:
        raise CannotBeArchivedError()

      atime = self.last_pass

      for p in self.players.values():
        chat_lister = self.chat_class(self, accessed_by = p)
        if chat_lister.unread > 0:
          raise CannotBeArchivedError()

        atime = max(atime, p.last_board)

      return atime + (86400 * 7)

    if name == 'can_be_archived':
      try:
        if hruntime.time > self.archive_deadline:
          return True

      except CannotBeArchivedError, e:
        return False

      return False

    return hlib.database.DBObject.__getattr__(self, name)

  def has_player(self, user):
    return user in self.user_to_player

  def to_api(self):
    ret = {
      'id':			self.id,
      'kind':			self.kind,
      'name':			self.name,
      'round':			self.round,
      'is_finished':		self.is_finished,
      'is_present':		self.has_player(hruntime.user),
      'has_password':		self.is_password_protected,
      'chat_posts':		self.chat.unread if self.chat.unread > 0 else False,
      'players':		[p.to_api() for p in self.players.values()]
    }

    try:
      ret['archive_deadline']	= self.archive_deadline
    except CannotBeArchivedError:
      ret['archive_deadline']	= False

    return ret

  def to_state(self):
    return {
      'name':			self.name,
      'round':			self.round,
      'limit':			self.limit,
      'players':		[p.to_state() for p in self.players.values()],
      'events':			[e.to_api() for e in self.events.values() if e.hidden != True]
    }

class PlayableLists(object):
  def __init__(self):
    super(PlayableLists, self).__init__()

    self._lock          = threading.RLock()

    self._active        = {}
    self._inactive      = {}
    self._archived      = {}

  def __get_f_list(self, name, user):
    with self._lock:
      cache = getattr(self, '_' + name)

      if user not in cache:
{'fraction_non_alphanumeric': 0.12149626940915507, 'fraction_numerical': 0.007057874571486187, 'mean_word_length': 2.6227173119065013, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '3691776', 'n_tokens_mistral': 3582, 'n_tokens_neox': 3355, 'n_words': 693}