<commit_before>
from client import *


"""
two-phase commit idea...
On first query, get moment number and get all assets for query from HISTORY 
at that moment. In other words, objects won't change over transaction.

Collect changes to objects.

Then something where you only update objects if their last moment was
the cached moment, or else you lost. 

"""



class BaseAsset(object):
  """
     Provides common methods for the dynamically derived asset type classes
     built by V1Meta.asset_class
  """

  @classmethod
  def find_by_id(Class, asset_id):
    'Takes an asset id (e.g. "1004") and returns the (possibly old) instance representing it'
    cache_key = (Class._v1_asset_type_name, asset_id)
    cache = Class._v1_v1meta.global_cache
    if cache.has_key(cache_key):
      return cache[cache_key]
    new_asset_instance = Class(asset_id)
    cache[cache_key] = new_asset_instance
    return new_asset_instance
    
  @classmethod
  def query(Class, where=''):
    'Takes a V1 Data query string and returns an iterable of all matching items'
    match = Class._v1_v1meta.query(Class._v1_asset_type_name, where)
    for asset in match.findall('Asset'):
      idref = asset.get('id')
      yield Class._v1_v1meta.asset_from_oid(idref)

  @classmethod
  def create(Class, newdata):
    create_response = Class._v1_v1meta.create_asset(Class._v1_asset_type_name, newdata)
    new_oid = create_response.find('Asset').get('idref')
    return Class._v1_v1meta.asset_from_oid(new_oid)
      
  def __init__(self, oid):
    'Takes an asset id and always instantiates a new asset instance'
    self._v1_oid = oid
    self._v1_new_data = {}
    self._v1_current_data = {}
    self._v1_needs_refresh = True

  def __getattr__(self, attr):
    'Syncs up the object if needed, and preferentially returns any pending data. '
    'else returns the data read from the server at last sync.'
    if self._v1_needs_refresh:
      self._v1_refresh()
    if self._v1_new_data.has_key(attr):
      value = self._v1_new_data[attr]
    value = self._v1_current_data[attr]
    if isinstance(value, list) and attr not in self._v1_multi_valued_relations:
      if value:
        value = value[0]
      else:
        value = None
    return value
    
  def __setattr__(self, attr, value):
    'Stores a new value for later commit'
    if attr.startswith('_v1_'):
      object.__setattr__(self, attr, value)
    else:
      self._v1_new_data[attr] = value
      self._v1_needs_commit = True


  def _v1_commit(self):
    'Commits the object to the server and invalidates its sync state'
    if self._v1_needs_commit:
      self._v1_v1meta.update_asset(self._v1_asset_type_name, self._v1_oid, self._v1_new_data)
    self._v1_needs_refresh = True
    
  def _v1_refresh(self):
    'Syncs the objects from current server data'<commit_msg>correct if/else logic<commit_after>from client import *


"""
two-phase commit idea...
On first query, get moment number and get all assets for query from HISTORY 
at that moment. In other words, objects won't change over transaction.

Collect changes to objects.

Then something where you only update objects if their last moment was
the cached moment, or else you lost. 

"""



class BaseAsset(object):
  """
     Provides common methods for the dynamically derived asset type classes
     built by V1Meta.asset_class
  """

  @classmethod
  def find_by_id(Class, asset_id):
    'Takes an asset id (e.g. "1004") and returns the (possibly old) instance representing it'
    cache_key = (Class._v1_asset_type_name, asset_id)
    cache = Class._v1_v1meta.global_cache
    if cache.has_key(cache_key):
      return cache[cache_key]
    new_asset_instance = Class(asset_id)
    cache[cache_key] = new_asset_instance
    return new_asset_instance
    
  @classmethod
  def query(Class, where=''):
    'Takes a V1 Data query string and returns an iterable of all matching items'
    match = Class._v1_v1meta.query(Class._v1_asset_type_name, where)
    for asset in match.findall('Asset'):
      idref = asset.get('id')
      yield Class._v1_v1meta.asset_from_oid(idref)

  @classmethod
  def create(Class, newdata):
    create_response = Class._v1_v1meta.create_asset(Class._v1_asset_type_name, newdata)
    new_oid = create_response.find('Asset').get('idref')
    return Class._v1_v1meta.asset_from_oid(new_oid)
      
  def __init__(self, oid):
    'Takes an asset id and always instantiates a new asset instance'
    self._v1_oid = oid
    self._v1_new_data = {}
    self._v1_current_data = {}
    self._v1_needs_refresh = True

  def __getattr__(self, attr):
    'Syncs up the object if needed, and preferentially returns any pending data. '
    'else returns the data read from the server at last sync.'
    if self._v1_needs_refresh:
      self._v1_refresh()
    if self._v1_new_data.has_key(attr):
      value = self._v1_new_data[attr]
    else:
      value = self._v1_current_data[attr]
    if isinstance(value, list) and attr not in self._v1_multi_valued_relations:
      if value:
        value = value[0]
      else:
        value = None
    return value
    
  def __setattr__(self, attr, value):
    'Stores a new value for later commit'
    if attr.startswith('_v1_'):
      object.__setattr__(self, attr, value)
    else:
      self._v1_new_data[attr] = value
      self._v1_needs_commit = True


  def _v1_commit(self):
    'Commits the object to the server and invalidates its sync state'
    if self._v1_needs_commit:
      self._v1_v1meta.update_asset(self._v1_asset_type_name, self._v1_oid, self._v1_new_data)
    self._v1_needs_refresh = True
    
  def _v1_refresh(self):
    'Syncs the objects from current server data'
{'fraction_non_alphanumeric': 0.08272936185257203, 'fraction_numerical': 0.014848859819692417, 'mean_word_length': 3.4762658227848102, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1022869', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1841, 'n_words': 531}