<commit_before>#
# Copyright (c) 2006 Canonical
#
# Written by <NAME> <<EMAIL>>
#
# This file is part of Storm Object Relational Mapper.
#
# <license text goes here>
#
from storm.exceptions import WrongStoreError, NoStoreError, ClassInfoError
from storm.store import Store, get_where_for_args
from storm.expr import Select, Column, Exists, Undef, SQLRaw, compare_columns
from storm.info import *


class Reference(object):

    def __init__(self, local_key, remote_key, on_remote=False):
        self._local_key = local_key
        self._remote_key = remote_key
        self._on_remote = on_remote
        self._relation = None
        self._cls = None

    def __get__(self, local, cls=None):
        if local is None:
            if self._cls is None:
                # Must set earlier, since __eq__() has no access
                # to the used class.
                self._cls = _find_descriptor_class(cls, self)
            return self

        if self._relation is None:
            # Don't use local.__class__ here, as it might be security
            # proxied or something. # XXX UNTESTED!
            self._build_relation(get_obj_info(local).cls_info.cls)

        remote = self._relation.get_remote(local)
        if remote is not None:
            return remote

        store = Store.of(local)
        if store is None:
            return None

        if self._relation.remote_key_is_primary:
            remote = store.get(self._relation.remote_cls,
                               self._relation.get_local_variables(local))
        else:
            where = self._relation.get_where_for_remote(local)
            remote = store.find(self._relation.remote_cls, where).one()

        if remote is not None:
            self._relation.link(local, remote)

        return remote

    def __set__(self, local, remote):
        if self._relation is None:
            # Don't use local.__class__ here, as it might be security
            # proxied or something. # XXX UNTESTED!
            self._build_relation(get_obj_info(local).cls_info.cls)

        if remote is None:
            remote = self._relation.get_remote(local)
            if remote is not None:
                self._relation.unlink(get_obj_info(local),
                                      get_obj_info(remote), True)
        else:
            self._relation.link(local, remote, True)

    def _build_relation(self, used_cls):
        if self._cls is None:
            assert used_cls is not None
            self._cls = _find_descriptor_class(used_cls, self)
        resolver = PropertyResolver(self, self._cls)
        self._local_key = resolver.resolve(self._local_key)
        self._remote_key = resolver.resolve(self._remote_key)
        self._relation = Relation(self._local_key, self._remote_key,
                                  False, self._on_remote)

    def __eq__(self, other):
        if self._relation is None:
            self._build_relation(self._cls)
        return self._relation.get_where_for_local(other)


class ReferenceSet(object):

    def __init__(self, local_key1, remote_key1,
                 remote_key2=None, local_key2=None, order_by=None):
        self._local_key1 = local_key1
        self._remote_key1 = remote_key1
        self._remote_key2 = remote_key2
        self._local_key2 = local_key2
        self._order_by = order_by
        self._relation1 = None
        self._relation2 = None

    def __get__(self, local, cls=None):
        if local is None:
            return self

        if self._relation1 is None:
            # Don't use local.__class__ here, as it might be security
            # proxied or something. # XXX UNTESTED!
            self._build_relations(get_obj_info(local).cls_info.cls)

        #store = Store.of(local)
        #if store is None:
        #    return None

        if self._relation2 is None:
            return BoundReferenceSet(self._relation1, local, self._order_by)
        else:<commit_msg>One more unwrapping of security proxied objects.<commit_after>#
# Copyright (c) 2006 Canonical
#
# Written by <NAME> <<EMAIL>>
#
# This file is part of Storm Object Relational Mapper.
#
# <license text goes here>
#
from storm.exceptions import WrongStoreError, NoStoreError, ClassInfoError
from storm.store import Store, get_where_for_args
from storm.expr import Select, Column, Exists, Undef, SQLRaw, compare_columns
from storm.info import *


class Reference(object):

    def __init__(self, local_key, remote_key, on_remote=False):
        self._local_key = local_key
        self._remote_key = remote_key
        self._on_remote = on_remote
        self._relation = None
        self._cls = None

    def __get__(self, local, cls=None):
        if local is None:
            if self._cls is None:
                # Must set earlier, since __eq__() has no access
                # to the used class.
                self._cls = _find_descriptor_class(cls, self)
            return self

        if self._relation is None:
            # Don't use local.__class__ here, as it might be security
            # proxied or something. # XXX UNTESTED!
            self._build_relation(get_obj_info(local).cls_info.cls)

        remote = self._relation.get_remote(local)
        if remote is not None:
            return remote

        store = Store.of(local)
        if store is None:
            return None

        if self._relation.remote_key_is_primary:
            remote = store.get(self._relation.remote_cls,
                               self._relation.get_local_variables(local))
        else:
            where = self._relation.get_where_for_remote(local)
            remote = store.find(self._relation.remote_cls, where).one()

        if remote is not None:
            self._relation.link(local, remote)

        return remote

    def __set__(self, local, remote):
        if self._relation is None:
            # Don't use local.__class__ here, as it might be security
            # proxied or something. # XXX UNTESTED!
            self._build_relation(get_obj_info(local).cls_info.cls)

        if remote is None:
            remote = self._relation.get_remote(local)
            if remote is not None:
                self._relation.unlink(get_obj_info(local),
                                      get_obj_info(remote), True)
        else:
            self._relation.link(local, remote, True)

    def _build_relation(self, used_cls):
        if self._cls is None:
            assert used_cls is not None
            self._cls = _find_descriptor_class(used_cls, self)
        resolver = PropertyResolver(self, self._cls)
        self._local_key = resolver.resolve(self._local_key)
        self._remote_key = resolver.resolve(self._remote_key)
        self._relation = Relation(self._local_key, self._remote_key,
                                  False, self._on_remote)

    def __eq__(self, other):
        if self._relation is None:
            self._build_relation(self._cls)
        # Object may be security proxied or something. # XXX UNTESTED!
        other = get_obj_info(other).get_obj()
        return self._relation.get_where_for_local(other)


class ReferenceSet(object):

    def __init__(self, local_key1, remote_key1,
                 remote_key2=None, local_key2=None, order_by=None):
        self._local_key1 = local_key1
        self._remote_key1 = remote_key1
        self._remote_key2 = remote_key2
        self._local_key2 = local_key2
        self._order_by = order_by
        self._relation1 = None
        self._relation2 = None

    def __get__(self, local, cls=None):
        if local is None:
            return self

        if self._relation1 is None:
            # Don't use local.__class__ here, as it might be security
            # proxied or something. # XXX UNTESTED!
            self._build_relations(get_obj_info(local).cls_info.cls)

        #store = Store.of(local)
        #if store is None:
        #    return None

        if self._relation2 is None:
            return BoundReferenceSet(self._relation1, local, self._order_by)
        else:
{'fraction_non_alphanumeric': 0.07765904572564612, 'fraction_numerical': 0.005218687872763419, 'mean_word_length': 2.117350890782339, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6497120', 'n_tokens_mistral': 2324, 'n_tokens_neox': 2247, 'n_words': 604}