<commit_before># orm/dependency.py
# Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 <NAME> <EMAIL>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

"""Relationship dependencies.

"""

from sqlalchemy import sql, util
import sqlalchemy.exceptions as sa_exc
from sqlalchemy.orm import attributes, exc, sync, unitofwork
from sqlalchemy.orm.interfaces import ONETOMANY, MANYTOONE, MANYTOMANY


def create_dependency_processor(prop):
    types = {
        ONETOMANY : OneToManyDP,
        MANYTOONE: ManyToOneDP,
        MANYTOMANY : ManyToManyDP,
    }
    return types[prop.direction](prop)

class DependencyProcessor(object):
    def __init__(self, prop):
        self.prop = prop
        self.cascade = prop.cascade
        self.mapper = prop.mapper
        self.parent = prop.parent
        self.secondary = prop.secondary
        self.direction = prop.direction
        self.post_update = prop.post_update
        self.passive_deletes = prop.passive_deletes
        self.passive_updates = prop.passive_updates
        self.enable_typechecks = prop.enable_typechecks
        self.key = prop.key
        if not self.prop.synchronize_pairs:
            raise sa_exc.ArgumentError(
                    "Can't build a DependencyProcessor for relationship %s. "
                    "No target attributes to populate between parent and "
                    "child are present" %
                     self.prop)

    def _get_instrumented_attribute(self):
        """Return the ``InstrumentedAttribute`` handled by this
        ``DependencyProecssor``.
        
        """
        return self.parent.class_manager.get_impl(self.key)

    def hasparent(self, state):
        """return True if the given object instance has a parent,
        according to the ``InstrumentedAttribute`` handled by this 
        ``DependencyProcessor``.
        
        """
        # TODO: use correct API for this
        return self._get_instrumented_attribute().hasparent(state)

    def per_property_flush_actions(self, uow):
        """establish actions and dependencies related to a flush.
        
        These actions will operate on all relevant states in
        the aggreagte.
        
        """
        if self.post_update and self._check_reverse(uow):
            return
        
        unitofwork.GetDependentObjects(uow, self, False, True)
        unitofwork.GetDependentObjects(uow, self, True, True)

        after_save = unitofwork.ProcessAll(uow, self, False, True)
        before_delete = unitofwork.ProcessAll(uow, self, True, True)

        parent_saves = unitofwork.SaveUpdateAll(
                                        uow, 
                                        self.parent.primary_base_mapper
                                        )
        child_saves = unitofwork.SaveUpdateAll(
                                        uow, 
                                        self.mapper.primary_base_mapper
                                        )

        parent_deletes = unitofwork.DeleteAll(
                                        uow, 
                                        self.parent.primary_base_mapper
                                        )
        child_deletes = unitofwork.DeleteAll(
                                        uow, 
                                        self.mapper.primary_base_mapper
                                        )
        
        self.per_property_dependencies(uow, 
                                        parent_saves, 
                                        child_saves, 
                                        parent_deletes, 
                                        child_deletes, 
                                        after_save, 
                                        before_delete)

    def per_state_flush_actions(self, uow, states, isdelete):
        """establish actions and dependencies related to a flush.
        
        These actions will operate on all relevant states 
        individually.    This occurs only if there are cycles
        in the 'aggregated' version of events.
        
        """

        if self.post_update and self._check_reverse(uow):
            # TODO: coverage here
            return iter([])

        # locate and disable the aggregate processors
        # for this dependency
        
        before_delete = unitofwork.ProcessAll(uow, self, True, True)
        before_delete.disabled = True
        after_save = unitofwork.ProcessAll(uow, self, False, True)
        after_save.disabled = True

        # check if the "child" side is part of the cycle
        
        parent_base_mapper = self.parent.primary_base_mapper
        child_base_mapper = self.mapper.primary_base_mapper
        child_saves = unitofwork.SaveUpdateAll(uow, child_base_mapper)
        child_deletes = unitofwork.DeleteAll(uow, child_base_mapper)
        
        if child_saves not in uow.cycles:
            # based on the current dependencies we use, the saves/
            # deletes should always be in the 'cycles' collection
            # together.   if this changes, we will have to break up
            # this method a bit more.
            assert child_deletes not in uow.cycles
            
            # child side is not part of the cycle, so we will link per-state
            # actions to the aggregate "saves", "deletes" actions
            child_actions = [
                (child_saves, False), (child_deletes, True)
            ]
            child_in_cycles = False
        else:
            child_in_cycles = True
        
        # check if the "parent" side is part of the cycle
        if not isdelete:
            parent_saves = unitofwork.SaveUpdateAll(
                                                uow, 
                                                self.parent.base_mapper)
            parent_deletes = before_delete = None
            if parent_saves in uow.cycles:
                parent_in_cycles = True
        else:
            parent_deletes = unitofwork.DeleteAll(
                                                uow, 
                                                self.parent.base_mapper)
            parent_saves = after_save = None
            if parent_deletes in uow.cycles:
                parent_in_cycles = True
        
        # now create actions /dependencies for each state.
        for state in states:
            # I'd like to emit the before_delete/after_save actions
            # here and have the unit of work not get confused by that
            # when it alters the list of dependencies...
            if isdelete:
                before_delete = unitofwork.ProcessState(uow, self, True, state)
                if parent_in_cycles:
                    parent_deletes = unitofwork.DeleteState(
                                                uow, 
                                                state, 
                                                parent_base_mapper)
            else:
                after_save = unitofwork.ProcessState(uow, self, False, state)
                if parent_in_cycles:
                    parent_saves = unitofwork.SaveUpdateState(
                                                uow, 
                                                state, 
                                                parent_base_mapper)
                
            if child_in_cycles:
                # locate each child state associated with the parent action,
                # create dependencies for each.
                child_actions = []
                sum_ = uow.get_attribute_history(
                                                state, 
                                                self.key, 
                                                passive=True).sum()
                if not sum_:
                    continue
                for child_state in sum_:
                    if child_state is None:
                        continue
                    if child_state not in uow.states:
                        child_action = (None, None)
                    else:
                        (deleted, listonly) = uow.states[child_state]
                        if deleted:
                            child_action = (
                                            unitofwork.DeleteState(
                                                        uow, child_state, 
                                                        child_base_mapper), 
                                            True)
                        else:
                            child_action = (
                                            unitofwork.SaveUpdateState(
                                                        uow, child_state, 
                                                        child_base_mapper), 
                                            False)
                    child_actions.append(child_action)
                    
            # establish dependencies between our possibly per-state
            # parent action and our possibly per-state child action.
            for (child_action, childisdelete) in child_actions:<commit_msg>we can load this sum ahead of time, and if there is none, we dont need a per-state/proc for this at all. this greatly reduces unnecessary crap in the UOW for complex models.<commit_after># orm/dependency.py
# Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 <NAME> <EMAIL>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

"""Relationship dependencies.

"""

from sqlalchemy import sql, util
import sqlalchemy.exceptions as sa_exc
from sqlalchemy.orm import attributes, exc, sync, unitofwork
from sqlalchemy.orm.interfaces import ONETOMANY, MANYTOONE, MANYTOMANY


def create_dependency_processor(prop):
    types = {
        ONETOMANY : OneToManyDP,
        MANYTOONE: ManyToOneDP,
        MANYTOMANY : ManyToManyDP,
    }
    return types[prop.direction](prop)

class DependencyProcessor(object):
    def __init__(self, prop):
        self.prop = prop
        self.cascade = prop.cascade
        self.mapper = prop.mapper
        self.parent = prop.parent
        self.secondary = prop.secondary
        self.direction = prop.direction
        self.post_update = prop.post_update
        self.passive_deletes = prop.passive_deletes
        self.passive_updates = prop.passive_updates
        self.enable_typechecks = prop.enable_typechecks
        self.key = prop.key
        if not self.prop.synchronize_pairs:
            raise sa_exc.ArgumentError(
                    "Can't build a DependencyProcessor for relationship %s. "
                    "No target attributes to populate between parent and "
                    "child are present" %
                     self.prop)

    def _get_instrumented_attribute(self):
        """Return the ``InstrumentedAttribute`` handled by this
        ``DependencyProecssor``.
        
        """
        return self.parent.class_manager.get_impl(self.key)

    def hasparent(self, state):
        """return True if the given object instance has a parent,
        according to the ``InstrumentedAttribute`` handled by this 
        ``DependencyProcessor``.
        
        """
        # TODO: use correct API for this
        return self._get_instrumented_attribute().hasparent(state)

    def per_property_flush_actions(self, uow):
        """establish actions and dependencies related to a flush.
        
        These actions will operate on all relevant states in
        the aggreagte.
        
        """
        if self.post_update and self._check_reverse(uow):
            return
        
        unitofwork.GetDependentObjects(uow, self, False, True)
        unitofwork.GetDependentObjects(uow, self, True, True)

        after_save = unitofwork.ProcessAll(uow, self, False, True)
        before_delete = unitofwork.ProcessAll(uow, self, True, True)

        parent_saves = unitofwork.SaveUpdateAll(
                                        uow, 
                                        self.parent.primary_base_mapper
                                        )
        child_saves = unitofwork.SaveUpdateAll(
                                        uow, 
                                        self.mapper.primary_base_mapper
                                        )

        parent_deletes = unitofwork.DeleteAll(
                                        uow, 
                                        self.parent.primary_base_mapper
                                        )
        child_deletes = unitofwork.DeleteAll(
                                        uow, 
                                        self.mapper.primary_base_mapper
                                        )
        
        self.per_property_dependencies(uow, 
                                        parent_saves, 
                                        child_saves, 
                                        parent_deletes, 
                                        child_deletes, 
                                        after_save, 
                                        before_delete)

    def per_state_flush_actions(self, uow, states, isdelete):
        """establish actions and dependencies related to a flush.
        
        These actions will operate on all relevant states 
        individually.    This occurs only if there are cycles
        in the 'aggregated' version of events.
        
        """

        if self.post_update and self._check_reverse(uow):
            # TODO: coverage here
            return iter([])

        # locate and disable the aggregate processors
        # for this dependency
        
        before_delete = unitofwork.ProcessAll(uow, self, True, True)
        before_delete.disabled = True
        after_save = unitofwork.ProcessAll(uow, self, False, True)
        after_save.disabled = True

        # check if the "child" side is part of the cycle
        
        parent_base_mapper = self.parent.primary_base_mapper
        child_base_mapper = self.mapper.primary_base_mapper
        child_saves = unitofwork.SaveUpdateAll(uow, child_base_mapper)
        child_deletes = unitofwork.DeleteAll(uow, child_base_mapper)
        
        if child_saves not in uow.cycles:
            # based on the current dependencies we use, the saves/
            # deletes should always be in the 'cycles' collection
            # together.   if this changes, we will have to break up
            # this method a bit more.
            assert child_deletes not in uow.cycles
            
            # child side is not part of the cycle, so we will link per-state
            # actions to the aggregate "saves", "deletes" actions
            child_actions = [
                (child_saves, False), (child_deletes, True)
            ]
            child_in_cycles = False
        else:
            child_in_cycles = True
        
        # check if the "parent" side is part of the cycle
        if not isdelete:
            parent_saves = unitofwork.SaveUpdateAll(
                                                uow, 
                                                self.parent.base_mapper)
            parent_deletes = before_delete = None
            if parent_saves in uow.cycles:
                parent_in_cycles = True
        else:
            parent_deletes = unitofwork.DeleteAll(
                                                uow, 
                                                self.parent.base_mapper)
            parent_saves = after_save = None
            if parent_deletes in uow.cycles:
                parent_in_cycles = True
        
        # now create actions /dependencies for each state.
        for state in states:
            # detect if there's anything changed or loaded
            # by a preprocessor on this state/attribute.  if not,
            # we should be able to skip it entirely.
            sum_ = uow.get_attribute_history(
                                            state, 
                                            self.key, 
                                            passive=True).sum()
            if not sum_:
                continue

            # I'd like to emit the before_delete/after_save actions
            # here and have the unit of work not get confused by that
            # when it alters the list of dependencies...
            if isdelete:
                before_delete = unitofwork.ProcessState(uow, self, True, state)
                if parent_in_cycles:
                    parent_deletes = unitofwork.DeleteState(
                                                uow, 
                                                state, 
                                                parent_base_mapper)
            else:
                after_save = unitofwork.ProcessState(uow, self, False, state)
                if parent_in_cycles:
                    parent_saves = unitofwork.SaveUpdateState(
                                                uow, 
                                                state, 
                                                parent_base_mapper)
                
            if child_in_cycles:
                child_actions = []
                for child_state in sum_:
                    if child_state is None:
                        continue
                    if child_state not in uow.states:
                        child_action = (None, None)
                    else:
                        (deleted, listonly) = uow.states[child_state]
                        if deleted:
                            child_action = (
                                            unitofwork.DeleteState(
                                                        uow, child_state, 
                                                        child_base_mapper), 
                                            True)
                        else:
                            child_action = (
                                            unitofwork.SaveUpdateState(
                                                        uow, child_state, 
                                                        child_base_mapper), 
                                            False)
                    child_actions.append(child_action)
                    
            # establish dependencies between our possibly per-state
            # parent action and our possibly per-state child action.
            for (child_action, childisdelete) in child_actions:
{'fraction_non_alphanumeric': 0.05390254420008624, 'fraction_numerical': 0.00258732212160414, 'mean_word_length': 1.1821924253116913, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 120, 'profanity_word_list.json': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5503601', 'n_tokens_mistral': 4327, 'n_tokens_neox': 4059, 'n_words': 1169}