Ejemplo n.º 1
0
def add_simulation_run(project, cache_directory, scenario_name, run_name,
                       start_year, end_year, run_id):
    '''
    Creates a simulation run node and adds it to the project
    @param project (OpusProject): currently loaded project
    @param cache_directory (String): absolute path to the cache directory
    @param scenario_name (String): name of the scenario that was run
    @param run_name (String): name of the run
    @param start_year (int): start year of run
    @param end_year (int): end year of run
    @param run_id (int): ID number for the run
    '''
    ## XML-ify the name
    # run_name = run_name.replace(' ', '_')

    # Assemble the new run node
    str_atr = {'type': 'string'}
    int_atr = {'type': 'integer'}

    run_node = Element('run', {'type': 'source_data',
                               'name': run_name,
                               'hidden': 'Children',
                               'run_id': str(run_id)})
    # SubElement(run_node, 'run_id', int_atr).text = str(run_id)
    SubElement(run_node, 'scenario_name', str_atr).text = scenario_name
    SubElement(run_node, 'cache_directory', str_atr).text = cache_directory
    SubElement(run_node, 'start_year', int_atr).text = str(start_year)
    SubElement(run_node, 'end_year', int_atr).text = str(end_year)

    # Grab the results manager instance for the GUI and insert the new node
    get_manager_instance('results_manager').add_run(run_node)
    project.dirty = True
    update_mainwindow_savestate()
    def _apply_variable_changes(self):
        ''' apply the changes the user made to the expression library '''
        # Case  XML (before)     Editor (after)   Change     Action
        # ----------------------------------------------------------
        #  A    <none>           local            created    create
        #  B    local            inherited        reverted   delete
        #  C    local            <none>           deleted    delete

        # TODO: also check into the possibility that an inherited variable can have it's name
        # changed (in this case don't overwrite the orginal variable's name. Instead create a new
        # variable with the new name.

        dirty_variables = [var for var in self.model.variables if var['dirty']]
        # case A
        create_set = [var for var in dirty_variables if var['originalnode'] is None]
        delete_set = []
        for xml_node in self.original_nodes:
            if xml_node.get('inherited') is not None: # only care about local or shadowing nodes
                continue
            for variable in self.model.variables:
                if variable['originalnode'] is xml_node: # original is represented in variable list
                    if variable['inherited']:
                        delete_set.append(xml_node) # Case B
                    break # stop looking
            else: # did not find node
                delete_set.append(xml_node) # case C
        # the rest of the variables should just be updated
        update_set = [var for var in dirty_variables if
                      not var['originalnode'] in delete_set and var not in create_set]

        # Apply the changes to each set of nodes
        expression_lib = self.project.find('general/expression_library')
        for variable in create_set:
            # print 'CREATE SET ', variable
            node = node_from_variable(variable)
            # print '  node', node
            self.project.insert_node(node, expression_lib)

        for variable in update_set:
            node = node_from_variable(variable)
            original_node = variable['originalnode'] # reference to the actual XML node
            # print 'UPDATE SET %s (original %s)' %(variable['name'], original_node)
            self.project.make_local(original_node)
            for key in node.attrib:
                if not key == 'inherited':
                    original_node.set(key, node.get(key))
            if 'dataset' in original_node.attrib:
                del original_node.attrib['dataset']
            original_node.text = node.text

        for node in delete_set:
            # print 'DELETE SET %s' % (node)
            self.project.delete_node(node)
        self.initialize()
        something_changed = bool(update_set or create_set or delete_set)
        update_mainwindow_savestate(something_changed)
        get_mainwindow_instance().emit(SIGNAL('variables_updated'))
        return True
Ejemplo n.º 3
0
    def insertRow(self, row, parent_index, node, reinserting=False):
        '''
        Insert a row into the data model
        @param row (int): row to insert into.
        @param parent_index (QModelIndex): index of parent item
        @param node (Element): node to insert
        @param reinserting (bool): if True; assume that the project has already reinserted the node
        and just insert it into the internal model. Also skip making it local after inserting.
        @return: True if the sibling was inserted, False otherwise
        '''
        if row < 0 or row > self.rowCount(parent_index):
            return False

        self.emit(SIGNAL("layoutAboutToBeChanged()"))
        self.beginInsertRows(parent_index, row, row)

        # Get a valid parent_item
        if parent_index == QModelIndex():
            parent_item = self._root_item
        else:
            parent_item = parent_index.internalPointer()
        parent_node = parent_item.node

        if self.project is None:  # no inheritance simple insert into tree
            parent_node.insert(row, node)
        else:
            # when dealing with a project and inheritance we have two cases --
            # either insertRow is inserting a node that already exists in the project
            # (reinserting is True) or we are inserting a new node.
            if not reinserting:
                inserted_node = self.project.insert_node(
                    node, parent_node, row)
                if inserted_node is None:
                    # raise RuntimeError('Could not insert node into model')
                    print 'WARNING: Could not insert %s:%s' % (
                        node.tag, node.get('name'))
                    return False
                self.project.make_local(inserted_node)
            else:
                inserted_node = node

        new_item = XmlItem(inserted_node, parent_item)
        new_item.rebuild()
        parent_item.child_items.insert(row, new_item)
        self.endInsertRows()
        self.emit(SIGNAL("layoutChanged()"))

        # If the item was created we store it so that XmlViews can access it
        self.last_inserted_index = self.index(
            row, 0, parent_index) if new_item else None
        update_mainwindow_savestate()
        return True
Ejemplo n.º 4
0
    def insertRow(self, row, parent_index, node, reinserting = False):
        '''
        Insert a row into the data model
        @param row (int): row to insert into.
        @param parent_index (QModelIndex): index of parent item
        @param node (Element): node to insert
        @param reinserting (bool): if True; assume that the project has already reinserted the node
        and just insert it into the internal model. Also skip making it local after inserting.
        @return: True if the sibling was inserted, False otherwise
        '''
        if row < 0 or row > self.rowCount(parent_index):
            return False

        self.emit(SIGNAL("layoutAboutToBeChanged()"))
        self.beginInsertRows(parent_index, row, row)

        # Get a valid parent_item
        if parent_index == QModelIndex():
            parent_item = self._root_item
        else:
            parent_item = parent_index.internalPointer()
        parent_node = parent_item.node

        if self.project is None: # no inheritance simple insert into tree
            parent_node.insert(row, node)
        else:
            # when dealing with a project and inheritance we have two cases --
            # either insertRow is inserting a node that already exists in the project
            # (reinserting is True) or we are inserting a new node.
            if not reinserting:
                inserted_node = self.project.insert_node(node, parent_node, row)
                if inserted_node is None:
                    # raise RuntimeError('Could not insert node into model')
                    print 'WARNING: Could not insert %s:%s' % (node.tag, node.get('name'))
                    return False
                self.project.make_local(inserted_node)
            else:
                inserted_node = node

        new_item = XmlItem(inserted_node, parent_item)
        new_item.rebuild()
        parent_item.child_items.insert(row, new_item)
        self.endInsertRows()
        self.emit(SIGNAL("layoutChanged()"))

        # If the item was created we store it so that XmlViews can access it
        self.last_inserted_index = self.index(row, 0, parent_index) if new_item else None
        update_mainwindow_savestate()
        return True
Ejemplo n.º 5
0
    def _apply_variable_changes(self):
        ''' apply the changes the user made to the expression library '''
        # TODO: also check into the possibility that an inherited variable can have its name
        # changed (in this case don't overwrite the original variable's name. Instead create a new
        # variable with the new name.)

        dirty_variables = [var for var in self.model.all_variables if var['dirty']]

        # partition dirty variables into create, delete and update sets
        create_set = [var for var in dirty_variables if var['originalnode'] is None]
        delete_set = [var for var in dirty_variables if var['delete'] and var['originalnode'] is not None]
        # the rest of the variables should just be updated
        update_set = [var for var in dirty_variables if
                      not var['originalnode'] in delete_set and var not in create_set]
        
        # verify if we have a partition
        assert(set([str(var) for var in create_set]) | set([str(var) for var in delete_set]) | set([str(var) for var in update_set]) 
               == set([str(var) for var in dirty_variables]))
        assert(set([str(var) for var in create_set]) & set([str(var) for var in delete_set]) & set([str(var) for var in update_set]) 
               == set())

        # Apply the changes to each set of nodes
        expression_lib = self.project.find('general/expression_library')
        for variable in create_set:
            # print 'CREATE SET ', variable
            node = node_from_variable(variable)
            # print '  node', node
            self.project.insert_node(node, expression_lib)

        for variable in update_set:
            node = node_from_variable(variable)
            original_node = variable['originalnode'] # reference to the actual XML node
            # print 'UPDATE SET %s (original %s)' %(variable['name'], original_node)
            self.project.make_local(original_node)
            for key in node.attrib:
                if not key == 'inherited':
                    original_node.set(key, node.get(key))
            if 'dataset' in original_node.attrib:
                del original_node.attrib['dataset']
            original_node.text = node.text

        for variable in delete_set:
            # print 'DELETE SET %s' % (node)
            self.project.delete_node(variable['originalnode'])
        self.initialize()
        something_changed = bool(update_set or create_set or delete_set)
        update_mainwindow_savestate(something_changed)
        get_mainwindow_instance().emit(SIGNAL('variables_updated'))
        return True
Ejemplo n.º 6
0
    def _apply_variable_changes(self):
        ''' apply the changes the user made to the expression library '''
        # TODO: also check into the possibility that an inherited variable can have its name
        # changed (in this case don't overwrite the original variable's name. Instead create a new
        # variable with the new name.)

        dirty_variables = [
            var for var in self.model.all_variables if var['dirty']
        ]

        # partition dirty variables into create, delete and update sets
        create_set = [
            var for var in dirty_variables if var['originalnode'] is None
        ]
        delete_set = [
            var for var in dirty_variables
            if var['delete'] and var['originalnode'] is not None
        ]
        # the rest of the variables should just be updated
        update_set = [
            var for var in dirty_variables
            if not var['originalnode'] in delete_set and var not in create_set
        ]

        # verify if we have a partition
        assert (set([str(var) for var in create_set])
                | set([str(var) for var in delete_set])
                | set([str(var) for var in update_set]) == set(
                    [str(var) for var in dirty_variables]))
        assert (set([str(var) for var in create_set])
                & set([str(var) for var in delete_set])
                & set([str(var) for var in update_set]) == set())

        # Apply the changes to each set of nodes
        expression_lib = self.project.find('general/expression_library')
        for variable in create_set:
            # print 'CREATE SET ', variable
            node = node_from_variable(variable)
            # print '  node', node
            self.project.insert_node(node, expression_lib)

        for variable in update_set:
            node = node_from_variable(variable)
            original_node = variable[
                'originalnode']  # reference to the actual XML node
            # print 'UPDATE SET %s (original %s)' %(variable['name'], original_node)
            self.project.make_local(original_node)
            for key in node.attrib:
                if not key == 'inherited':
                    original_node.set(key, node.get(key))
            if 'dataset' in original_node.attrib:
                del original_node.attrib['dataset']
            original_node.text = node.text

        for variable in delete_set:
            # print 'DELETE SET %s' % (node)
            self.project.delete_node(variable['originalnode'])
        self.initialize()
        something_changed = bool(update_set or create_set or delete_set)
        update_mainwindow_savestate(something_changed)
        get_mainwindow_instance().emit(SIGNAL('variables_updated'))
        return True
Ejemplo n.º 7
0
 def __set_dirty(self, dirty):
     self._dirty = dirty
     update_mainwindow_savestate()
Ejemplo n.º 8
0
 def __set_dirty(self, dirty):
     self._dirty = dirty
     update_mainwindow_savestate()