Esempio n. 1
0
def iterRecords(master_file_name):
    config = pyisis.config.config
    config.load("isis/cds.ini")
    Engine.setup(config)
    mst = MasterFile(master_file_name, config=config)
    for record in mst:
        fields = {}
        if SKIP_INACTIVE and (record.status != 0): 
            continue
        else:
            fields[ISIS_ACTIVE_KEY] = record.status == 0
        fields[ISIS_MFN_KEY] = record.mfn
        for field in record:
            field_key = str(field.tag)
            field_occurrences = fields.setdefault(field_key,[])
            subfields = {}
            for key in field._get_subfields(field.data).keys():
                subfield_key = key
                if key == '*':
                    subfields['_'] = field._get_subfields(field.data)[key]
                else:   
                    subfield_occurrences = subfields.setdefault(subfield_key,[])
                    subfield_occurrences.append(field._get_subfields(field.data)[key])
                field_occurrences.append(subfields)
        yield fields
Esempio n. 2
0
def main():
    codecs.register(load_codec_modules)

    set_lang(pyisis.config.config)
    parser = OptionParser(
             usage=banner,
             version="%prog "+__version__,
             description="Python-based ISIS-NBP Cell.")

    parser.add_option("-f", "--config",
                      action="store", type="string", dest="configfile",
                      help=_("Specify the path to the configuration file"))

    parser.add_option("-m", "--inputdb",
                      action="store", type="string", dest="inputdb",
                      help=_("Specify the path to the master file"))

    parser.add_option("-o", "--outputdb",
                      action="store", type="string", dest="outputdb",
                      help=_("Specify the path to the output master file"))

    parser.add_option("-s", "--server",
                      action="store_true",   dest="server",
                      help=_("Start in server mode."))

    parser.add_option("-b", "--browse",
                      action="store_true",  dest="browse",
                      help=_("Browse the the master file interactively"))

    parser.add_option("-l", "--list",
                      action="store_true",  dest="list",
                      help=_("List the contents of the master file"))

    parser.add_option("-p", "--profile",
                      action="store_true",  dest="profile",
                      help=_("Profile the execution of --list option"))

    parser.add_option("-x", "--xml-dump",
                      action="store_true",  dest="xml",
                      help=_("Convert master file contents to XML"))

    parser.add_option("-i", "--interactive",
                      action="store_true",  dest="interactive",
                      help=_("Interactive mode of operation"))

    parser.add_option("-V",
                    action="store_true",  dest="version",
                    help=_("Prints current version"))

    parser.add_option("-c", "--control",
                      action="store_true", dest="control",
                      help=_("Dumps the control record"))

    parser.add_option("--copy",
                      action="store_true", dest="copy",
                      help=_("Copy inputdb into outputdb"))

    (options, args) = parser.parse_args()

    config = pyisis.config.config
    if options.configfile:
        config.load(options.configfile)
    set_lang(config)

    # Initialize engine
    Engine.setup(config)

    # Setup logging
    logger = logging.getLogger('pyisis')
    logger.propagate = False
    logger.setLevel(config.LOG_LEVEL)
    handler = FileHandler(filename=config.LOG_PATH)
    handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
    logger.addHandler(handler)
    logger.info("Isis-NBP Cell version " + __version__)


    if options.version:
        print "Isis-NBP Cell version ",__version__
        print "Python", sys.version
        sys.exit(0)

    if options.server:
        gateway.run(Engine)

    # Setup master file objects
    mf = None
    if options.inputdb:
        input_filepath = normalize_path(options.inputdb)
        if not exists(input_filepath):
            print _("File %s does not exist or is inaccessible.") % input_filepath
            sys.exit(0)
        mf = MasterFile(input_filepath, config=config)

    if options.outputdb:
        output_filepath = normalize_path(options.outputdb)
        dest_mf = MasterFile(output_filepath, config=config)


    # dump control record
    if options.control:
        
        if not mf:
            print _('Use -m or --inputdb to set input database.')
            sys.exit(0)
            
        template_header = "%10s %10s %10s %2s %10s %10s %10s %10s"
        template_values = "%10d %10d %10d %2d %10d %10d %10d %10d"
        print template_header%('nxtmfn', 'nxtmfb', 'nxtmfp', 't',
                               'reccnt', 'mfcxx1', 'mfcxx2', 'mfcxx3')
        print template_values%(mf.nxtmfn, mf.nxtmfb, mf.nxtmfp, mf.mftype,
                               mf.reccnt, mf.mfcxx1, mf.mfcxx1, mf.mfcxx1)
        print

    if options.browse:
        if not mf:
            print _('Use -m or --inputdb to set input database.')
            sys.exit(0)
        browse(mf)

    if options.list:
        if not mf:
            print _('Use -m or --inputdb to set input database.')
            sys.exit(0)
        list_all(mf)

    if options.xml:
        if not mf:
            print _('Use -m or --inputdb to set input database.')
            sys.exit(0)
        
        dump_xml(mf)

    if options.copy:
        # Check dependencies
        if not options.inputdb:
            print _("Use -m or --inputdb to set input database.")
            sys.exit(0)
        if not options.outputdb:
            print _("Use -o or --outputdb to set output database.")
            sys.exit(0)
        # do copy
        for rec in mf:
            if rec.status==ACTIVE:
                rec.save(dest_mf)


    if options.interactive:
        if options.inputdb and input_filepath:
            Engine.collection[options.inputdb] = IsisCollection(options.inputdb,
                                                                input_filepath)
        interactive(Engine.collection)

    if options.profile:
        if not options.inputdb:
            print _("Use -m or --inputdb to set input database.")
            sys.exit(0)
        
        def inspected():
            list_all(mf)
            return [1,2]

        import hotshot, hotshot.stats
        prof = hotshot.Profile("listall")
        benchtime, stones = prof.runcall(inspected)
        print benchtime, stones
        prof.close()
        s = hotshot.stats.load("listall")
        s.sort_stats("cumulative").print_stats()
Esempio n. 3
0
def initialize():
    """Prepare test environment"""
    set_i18n(base_config)
    Engine.setup(base_config)
    return Engine.config