Beispiel #1
0
    def enterScope(self, name):
        """This is called when you enter a scope

        @param: string name

        @raise RuntimeException         When the parent scope is inactive
        @raise InvalidArgumentException When the scope does not exist

        @api

        """

        if name not in self._scopes:
            raise InvalidArgumentException(
                'The scope "{0}" does not exist.'.format(name))

        if self.SCOPE_CONTAINER != self._scopes[name] and self._scopes[
                name] not in self._scopedServices:
            raise RuntimeException(
                'The parent scope "{0}" must be active when entering this '
                'scope.'.format(self._scopes[name]))

        # check if a scope of this name is already active, if so we need to
        # remove all services of this scope, and those of any of its child
        # scopes from the global services map
        if name in self._scopedServices:
            services = OrderedDict()
            services[0] = self._services
            services[name] = self._scopedServices[name]
            self._scopedServices.pop(name, None)

            for child in self._scopeChildren[name]:
                if child in self._scopedServices:
                    services[child] = self._scopedServices[child]
                    self._scopedServices.pop(child, None)

            # update global map
            self._services = Array.diffKey(*services.values())
            services.pop(0)

            # add stack entry for this scope so we can restore the removed services later
            if name not in self._scopeStacks:
                self._scopeStacks[name] = list()

            self._scopeStacks[name].append(services)

        self._scopedServices[name] = dict()
Beispiel #2
0
 def testDiffKeyName(self):
     input_array = {
         0: '0',
         1: '1',
         -10: '-10',
         'True': 1,
         'False': 0
     }
     boolean_indx_array = {
         True: 'boolt',
         False: 'boolf',
         True: 'boolT',
         False: 'boolF'
     }
     expected = {-10: "-10", "True": 1, "False": 0}
     self.assertEqual(expected,
                      Array.diffKey(input_array, boolean_indx_array))
Beispiel #3
0
 def testDiffKeyBasic(self):
     array1 = {
         'blue': 1,
         'red': 2,
         'green': 3,
         'purple': 4
     }
     array2 = {
         'green': 5,
         'blue': 6,
         'yellow': 7,
         'cyan': 8
     }
     self.assertEqual({
         "red": 2,
         "purple": 4
     }, Array.diffKey(array1, array2))
Beispiel #4
0
    def leaveScope(self, name):
        """This is called to leave the current scope, and move back to the parent
        scope.

        @param: string name The name of the scope to leave

        @raise InvalidArgumentException if the scope is not active:

        @api

        """

        if name not in self._scopedServices:
            raise InvalidArgumentException(
                'The scope "{0}" is not active.'.format(name))

        # remove all services of this scope, or any of its child scopes from
        # the global service map
        services = [self._services, self._scopedServices[name]]
        self._scopedServices.pop(name, None)
        for child in self._scopeChildren[name]:
            if child not in self._scopedServices:
                continue

            services.append(self._scopedServices[child])
            self._scopedServices.pop(child, None)

        self._services = Array.diffKey(*services)

        # check if we need to restore services of a previous scope of this type:
        if name in self._scopeStacks and self._scopeStacks[name]:
            services = self._scopeStacks[name].pop()
            self._scopedServices.update(services)

            for scopeServices in services.values():
                self._services.update(scopeServices)
Beispiel #5
0
 def testDiffKeyName(self):
     input_array = {0 : '0', 1 : '1', -10 : '-10', 'True' : 1, 'False' : 0};
     boolean_indx_array = {True : 'boolt', False : 'boolf', True : 'boolT', False : 'boolF'};
     expected = {-10 :"-10","True":1,"False":0}
     self.assertEqual(expected, Array.diffKey(input_array, boolean_indx_array));
Beispiel #6
0
 def testDiffKeyBasic(self):
     array1 = {'blue' : 1, 'red': 2, 'green' : 3, 'purple' : 4};
     array2 = {'green': 5, 'blue' : 6, 'yellow': 7, 'cyan' : 8};
     self.assertEqual({"red":2,"purple":4}, Array.diffKey(array1, array2));