def import_tdl_module (filename,text=None,config=0):
  """Imports a TDL module.
  Parameters:
    filename: script location
    text: text of module. If none, file will be re-read.

  Return value:
    a tuple of (module,text), where module is the newly-imported
    TDL module, and text is the module text.

  Exceptions thrown:
    Any import error results in an exception. This is always
    a TDL.CumulativeError exception containing an error list.
    Other exceptions may indicate an internal failure.
  """;
  _dprint(1,"importing",filename);
  global _current_filename;
  _current_filename = filename;
  # initialize global nodescope (and repository)
  meqds.clear_forest();
  try:
    reload(Timba.TDL.Settings);
    # reset TDL script options, unless config=None ('0' is used as default, causing the filename to be substituted)
    TDLOptions.init_script(filename);
    if config is not None:
      TDLOptions.init_options(config or filename);
    # remove .pyo file as that can have cached paths and directories may have changed
    # (see bug 677)
    try:
      os.unlink(os.path.splitext(filename)[0]+'.pyo');
    except:
      pass;
    # open file
    infile = file(filename,'r');
    if text is None:
      text = infile.read();
      infile.seek(0);
    # infile is now an open input file object, and text is the script

    # flush all modules imported via previous TDL run
    global _tdlmodlist;
    _dprint(1,'clearing out TDL-imported modules',_tdlmodlist);
    for m in _tdlmodlist:
      try: del sys.modules[m];
      except KeyError: pass;
    # remember which modules are imported
    global _prior_compile_modules;
    _prior_compile_modules = set(sys.modules.iterkeys());
    modname = '__tdlruntime';
    try:
      TDLOptions.enable_save_config(False);
      imp.acquire_lock();
      _tdlmod = imp.load_source(modname,filename,infile);
    finally:
      TDLOptions.enable_save_config(True);
      TDLOptions.save_config();
      imp.release_lock();
      infile.close();
      _update_modlist();
    return (_tdlmod,text);

  # CumulativeError exceptions returned as is
  except TDL.CumulativeError:
    _dprint(0,'cumulative error importing TDL file:',filename);
    traceback.print_exc();
    args = sys.exc_info()[1].args;
    _dprint(0,'number of errors in list:',len(args));
    _dprint(1,'errors are: {',args,'}');
    raise;
  # Other exceptions wrapped in a CumulativeError, and
  # location information is added in
  except:
    (etype,exc,tb) = sys.exc_info();
    _dprint(0,'exception importing TDL file:',filename);
    traceback.print_exception(etype,exc,tb);
    # use TDL add_error() to process the error, since this automatically
    # adds location information
    ns = TDL.NodeScope();
    ns.AddError(exc,traceback.extract_tb(tb),error_limit=None);
    # re-raise as a CumulativeError
    raise TDL.CumulativeError(*ns.GetErrors());
示例#2
0
def import_tdl_module(filename, text=None, config=0):
    """Imports a TDL module.
  Parameters:
    filename: script location
    text: text of module. If none, file will be re-read.

  Return value:
    a tuple of (module,text), where module is the newly-imported
    TDL module, and text is the module text.

  Exceptions thrown:
    Any import error results in an exception. This is always
    a TDL.CumulativeError exception containing an error list.
    Other exceptions may indicate an internal failure.
  """
    _dprint(1, "importing", filename)
    global _current_filename
    _current_filename = filename
    # initialize global nodescope (and repository)
    meqds.clear_forest()
    try:
        reload(Timba.TDL.Settings)
        # reset TDL script options, unless config=None ('0' is used as default, causing the filename to be substituted)
        TDLOptions.init_script(filename)
        if config is not None:
            TDLOptions.init_options(config or filename)
        # remove .pyo file as that can have cached paths and directories may have changed
        # (see bug 677)
        try:
            os.unlink(os.path.splitext(filename)[0] + '.pyo')
        except:
            pass
        # open file
        infile = file(filename, 'r')
        if text is None:
            text = infile.read()
            infile.seek(0)
        # infile is now an open input file object, and text is the script

        # flush all modules imported via previous TDL run
        global _tdlmodlist
        _dprint(1, 'clearing out TDL-imported modules', _tdlmodlist)
        for m in _tdlmodlist:
            try:
                del sys.modules[m]
            except KeyError:
                pass
        # remember which modules are imported
        global _prior_compile_modules
        _prior_compile_modules = set(sys.modules.iterkeys())
        modname = '__tdlruntime'
        try:
            TDLOptions.enable_save_config(False)
            imp.acquire_lock()
            _tdlmod = imp.load_source(modname, filename, infile)
        finally:
            TDLOptions.enable_save_config(True)
            TDLOptions.save_config()
            imp.release_lock()
            infile.close()
            _update_modlist()
        return (_tdlmod, text)

    # CumulativeError exceptions returned as is
    except TDL.CumulativeError:
        _dprint(0, 'cumulative error importing TDL file:', filename)
        traceback.print_exc()
        args = sys.exc_info()[1].args
        _dprint(0, 'number of errors in list:', len(args))
        _dprint(1, 'errors are: {', args, '}')
        raise
    # Other exceptions wrapped in a CumulativeError, and
    # location information is added in
    except:
        (etype, exc, tb) = sys.exc_info()
        _dprint(0, 'exception importing TDL file:', filename)
        traceback.print_exception(etype, exc, tb)
        # use TDL add_error() to process the error, since this automatically
        # adds location information
        ns = TDL.NodeScope()
        ns.AddError(exc, traceback.extract_tb(tb), error_limit=None)
        # re-raise as a CumulativeError
        raise TDL.CumulativeError(*ns.GetErrors())