コード例 #1
0
ファイル: listdefs.py プロジェクト: Kitware/SMTK
def list_items(parent, level, options):
    ''' Lists items contained by parent

    The level input sets the indentation
    Conditional children are prefixed with "*"
    '''
    this_indent = options.indent * level
    if hasattr(parent, 'itemDefinition'):
        # Parent is Definition or GroupItemDefinition
        n = parent.numberOfItemDefinitions()
        for i in range(n):
            item = parent.itemDefinition(i)
            concrete_item = smtk.to_concrete(item)
            type_string = smtk.attribute.Item.type2String(item.type())
            print '%s%s \"%s\"' % (this_indent, type_string, item.name())
            list_items(concrete_item, level + 1, options)
    elif hasattr(parent, 'childrenItemDefinitions'):
        # Parent is ValueItemDefinition
        item_dict = parent.childrenItemDefinitions()
        item_list = item_dict.items()
        if item_list is None:
            return

        item_list.sort()
        for name, item in item_list:
            concrete_item = smtk.to_concrete(item)
            type_string = smtk.attribute.Item.type2String(item.type())
            print '%s*%s \"%s\"' % (this_indent, type_string, name)
            list_items(concrete_item, level + 1, options)
コード例 #2
0
ファイル: listdefs.py プロジェクト: z562705/SMTK
def list_items(parent, level, options):
    ''' Lists items contained by parent

    The level input sets the indentation
    Conditional children are prefixed with "*"
    '''
    this_indent = options.indent * level
    if hasattr(parent, 'itemDefinition'):
        # Parent is Definition or GroupItemDefinition
        n = parent.numberOfItemDefinitions()
        for i in range(n):
            item = parent.itemDefinition(i)
            concrete_item = smtk.to_concrete(item)
            type_string = smtk.attribute.Item.type2String(item.type())
            print '%s%s \"%s\"' % (this_indent, type_string, item.name())
            list_items(concrete_item, level + 1, options)
    elif hasattr(parent, 'childrenItemDefinitions'):
        # Parent is ValueItemDefinition
        item_dict = parent.childrenItemDefinitions()
        item_list = item_dict.items()
        if item_list is None:
            return

        item_list.sort()
        for name, item in item_list:
            concrete_item = smtk.to_concrete(item)
            type_string = smtk.attribute.Item.type2String(item.type())
            print '%s*%s \"%s\"' % (this_indent, type_string, name)
            list_items(concrete_item, level + 1, options)
コード例 #3
0
ファイル: listdefs.py プロジェクト: Kitware/SMTK
def list_by_view(system, view, level, options):
    '''Lists definitions in indented-text format, organized by view

    Works recursively
    '''
    this_indent = options.indent * level
    sublevel = level + 1

    print '%sVIEW \"%s\"' % (this_indent, view.title())

    # Group view
    if hasattr(view, 'numberOfSubViews'):
        n = view.numberOfSubViews()
        for i in range(n):
            subview = view.subView(i)
            concrete_subview = smtk.to_concrete(subview)
            list_by_view(system, concrete_subview, sublevel, options)

    # Attribute view
    elif hasattr(view, 'numberOfDefinitions'):
        n = view.numberOfDefinitions()
        for i in range(n):
            defn = view.definition(i)
            list_definition(system, defn, sublevel, options)

    # Instanced view
    elif hasattr(view, 'numberOfInstances'):
        n = view.numberOfInstances()
        for i in range(n):
            att = view.instance(i)
            defn = att.definition()
            list_definition(system, defn, sublevel, options)
コード例 #4
0
def update_view_info(table, view, view_path=[]):
    '''Updates contents of table or view_path
  '''
    #print 'Enter update_view_info, view', str(view)
    if hasattr(view, 'numberOfSubViews'):
        # If a GroupView, process sub views recursively
        for i in range(view.numberOfSubViews()):
            subview = view.subView(i)
            next_view = smtk.to_concrete(subview)
            next_path = view_path[:]
            if subview.title() != '':
                next_path.append(subview.title())
            update_view_info(table, next_view, next_path)
    elif hasattr(view, 'numberOfInstances'):
        # If InstanceView, update table with attribute types
        for i in range(view.numberOfInstances()):
            instance = view.instance(i)
            att_type = instance.type()
            if att_type in table:
                print 'WARNING: Found same attribute type twice: ' + att_type
                continue
            table[instance.type()] = view_path
        #print table
    elif hasattr(view, 'numberOfDefinitions'):
        # If AttributeView, update table with definition types
        for i in range(view.numberOfDefinitions()):
            defn = view.definition(i)
            def_type = defn.type()
            if def_type in table:
                print 'WARNING: Found same definition type twice: ' + def_type
                continue
            table[def_type] = view_path
    else:
        print 'Skipping view with title ' + view.title(
        ) + ', type' + view.type()
コード例 #5
0
ファイル: listdefs.py プロジェクト: z562705/SMTK
def list_by_view(system, view, level, options):
    '''Lists definitions in indented-text format, organized by view

    Works recursively
    '''
    this_indent = options.indent * level
    sublevel = level + 1

    print '%sVIEW \"%s\"' % (this_indent, view.title())

    # Group view
    if hasattr(view, 'numberOfSubViews'):
        n = view.numberOfSubViews()
        for i in range(n):
            subview = view.subView(i)
            concrete_subview = smtk.to_concrete(subview)
            list_by_view(system, concrete_subview, sublevel, options)

    # Attribute view
    elif hasattr(view, 'numberOfDefinitions'):
        n = view.numberOfDefinitions()
        for i in range(n):
            defn = view.definition(i)
            list_definition(system, defn, sublevel, options)

    # Instanced view
    elif hasattr(view, 'numberOfInstances'):
        n = view.numberOfInstances()
        for i in range(n):
            att = view.instance(i)
            defn = att.definition()
            list_definition(system, defn, sublevel, options)
コード例 #6
0
def write_nfile(scope):
    '''
  '''
    file_complete = False
    with open(scope.nfilename, 'w') as scope.file:
        print 'Writing ', scope.nfilename

        file_header = [
            '# Proteus Poisson numerics file', '# Generated by Kitware CMB'
        ]
        write_lines(scope, file_header, insert_blank_line=False)

        # Get filename of the _p file. We presume it is in the same dir
        pfilename = os.path.basename(scope.pfilename)

        # Write fixed imports
        python_imports = [
            'from proteus import *',
            'from proteus.default_n import *',
            'from %s import *' % pfilename[:-3]  # strip ".py"
        ]
        write_lines(scope, python_imports)

        for info in nfile_formats:
            print '  write', info.lvalue
            if info.standard_format:
                write_format_info_instance(scope, *info.args, **info.kwargs)
            # Custom formatting logic:
            elif 'quadrature' == info.lvalue:
                write_quadrature(scope, info)
            elif 'shockCapturing' == info.lvalue:
                write_shockCapturing(scope, info)
            elif 'subgridError' == info.lvalue:
                write_subgridError(scope, info)
            else:
                msg = 'Unrecognized custom lvalue \"%s\"' % info.lvalue
                print 'ERROR: ', msg
                scope.logger.addError(msg)

        # Handle numerics_other as special case
        alt_name_dict = {'tolFace': 'tolFac'}
        nu_item = find_instance(scope, 'numerics_other', 'othernumerics')
        if nu_item is not None:
            ui_comment = create_ui_comment(scope, 'numerics_other', nu_item)
            nu_list = [ui_comment]
            num_items = nu_item.numberOfItemsPerGroup()
            for i in range(num_items):
                subitem = smtk.to_concrete(nu_item.item(i))
                name = subitem.name()
                value = None
                if name in alt_name_dict:
                    name = alt_name_dict.get(name)
                if subitem.type() == smtk.attribute.Item.GROUP:
                    value = dict()
                    num_components = subitem.numberOfItemsPerGroup()
                    for j in range(num_components):
                        comp_item = smtk.to_concrete(subitem.item(j))
                        comp_value = get_item_value(comp_item)
                        if comp_value is not None:
                            value[comp_item.name()] = comp_value
                else:
                    value = get_item_value(subitem)

                if value is not None:
                    nu_list.append('%s = %s' % (name, value))
            write_lines(scope, nu_list)

    return True
コード例 #7
0
ファイル: GSSHA.py プロジェクト: johnkit/cmb-workflows
def ExportCMB(spec):
    '''Entry function, called by CMB to write export files

    Returns boolean indicating success
    Parameters
    ----------
    spec: Top-level object passed in from CMB
    '''
    #print 'Enter ExportCMB()'

    # Initialize scope instance to store spec values and other info
    scope = ExportScope()
    scope.logger = spec.getLogger()
    scope.sim_atts = spec.getSimulationAttributes()
    if scope.sim_atts is not None:
        scope.model_manager = scope.sim_atts.refModelManager()

    export_spec_att = None
    scope.export_atts = spec.getExportAttributes()
    if scope.export_atts is not None:
        att_list = scope.export_atts.findAttributes('ExportSpec')
        if att_list:
            export_spec_att = att_list[0]

    if export_spec_att is None:
        msg = 'No ExportSpec instance -- cannot export'
        print 'WARNING:', msg
        scope.logger.addError(msg)
        return False

    # Get project name
    project_name_item = export_spec_att.findString('ProjectName')
    project_name = project_name_item.value(0)
    if not project_name:
        project_name = 'gssha'
        msg = 'No Project Name specified; using \"%s\"' % project_name
        print 'WARNING:', msg
        scope.logger.addWarning(msg)
    print 'project_name', project_name
    scope.project_name = project_name

    # Initialize project path (folder)
    project_path = None
    item = export_spec_att.find('ProjectPath')
    if item is not None:
        dir_item = smtk.to_concrete(item)
        project_path = dir_item.value(0)
        #print 'project_path', project_path

    if not project_path:
        msg = 'No project_path specified -- cannot export'
        print 'ERROR:', msg
        scope.logger.addWarning(msg)
        return False
    scope.project_path = project_path

    # Create folder if needed
    if not os.path.exists(project_path):
        os.makedirs(project_path)

    completed = write_project_file(scope)
    print 'Export completion status: %s' % completed
    sys.stdout.flush()
    return completed