コード例 #1
0
ファイル: test_config.py プロジェクト: kpreid/shinysdr
 def test_default_config(self):
     write_default_config(self.__config_name)
     self.assertTrue(os.path.isdir(self.__config_name))
     
     # Don't try to open a real device
     with open(self.__dirpath('config.py'), 'r') as f:
         conf_text = f.read()
     DEFAULT_DEVICE = "OsmoSDRDevice('')"
     self.assertIn(DEFAULT_DEVICE, conf_text)
     conf_text = conf_text.replace(DEFAULT_DEVICE, "OsmoSDRDevice('file=/dev/null,rate=100000')")
     with open(self.__dirpath('config.py'), 'w') as f:
         f.write(conf_text)
     
     execute_config(self.__config, self.__config_name)
     
     self.assertTrue(os.path.isdir(self.__dirpath('dbs-read-only')))
     return self.__config._wait_and_validate()
コード例 #2
0
ファイル: test_config.py プロジェクト: rsumner33/shinysdr
 def test_default_config(self):
     write_default_config(self.__config_name)
     self.assertTrue(os.path.isdir(self.__config_name))
     
     # Don't try to open a real device
     with open(self.__dirpath('config.py'), 'r') as f:
         conf_text = f.read()
     DEFAULT_DEVICE = "OsmoSDRDevice('')"
     self.assertIn(DEFAULT_DEVICE, conf_text)
     conf_text = conf_text.replace(DEFAULT_DEVICE, "OsmoSDRDevice('file=/dev/null,rate=100000')")
     with open(self.__dirpath('config.py'), 'w') as f:
         f.write(conf_text)
     
     execute_config(self.__config, self.__config_name)
     
     self.assertTrue(os.path.isdir(self.__dirpath('dbs-read-only')))
     return self.__config._wait_and_validate()
コード例 #3
0
def _main_async(reactor, argv=None, _abort_for_test=False):
    if argv is None:
        argv = sys.argv
    
    if not _abort_for_test:
        # Some log messages would be discarded if we did not set up things early.
        configure_logging()
    
    # Option parsing is done before importing the main modules so as to avoid the cost of initializing gnuradio if we are aborting early. TODO: Make that happen for createConfig too.
    argParser = argparse.ArgumentParser(prog=argv[0])
    argParser.add_argument('config_path', metavar='CONFIG',
        help='path of configuration directory or file')
    argParser.add_argument('--create', dest='createConfig', action='store_true',
        help='write template configuration file to CONFIG and exit')
    argParser.add_argument('-g, --go', dest='openBrowser', action='store_true',
        help='open the UI in a web browser')
    argParser.add_argument('--force-run', dest='force_run', action='store_true',
        help='Run DSP even if no client is connected (for debugging).')
    args = argParser.parse_args(args=argv[1:])

    # Verify we can actually run.
    # Note that this must be done before we actually load core modules, because we might get an import error then.
    version_report = yield _check_versions()
    if version_report:
        print(version_report, file=sys.stderr)
        sys.exit(1)

    # Write config file and exit if asked ...
    if args.createConfig:
        write_default_config(args.config_path)
        _log.info('Created default configuration at: {config_path}', config_path=args.config_path)
        sys.exit(0)  # TODO: Consider using a return value or something instead
    
    # ... else read config file
    config_obj = Config(reactor=reactor, log=_log)
    execute_config(config_obj, args.config_path)
    yield config_obj._wait_and_validate()
    
    _log.info('Constructing...')
    app = config_obj._create_app()
    
    reactor.addSystemEventTrigger('during', 'shutdown', app.close_all_devices)
    
    _log.info('Restoring state...')
    pfg = PersistenceFileGlue(
        reactor=reactor,
        root_object=app,
        filename=config_obj._state_filename,
        get_defaults=_app_defaults)
    
    _log.info('Starting web server...')
    services = MultiService()
    for maker in config_obj._service_makers:
        IService(maker(app)).setServiceParent(services)
    services.startService()
    
    _log.info('ShinySDR is ready.')
    
    for service in services:
        # TODO: should have an interface (currently no proper module to put it in)
        service.announce(args.openBrowser)
    
    if args.force_run:
        _log.debug('force_run')
        # TODO kludge, make this less digging into guts
        app.get_receive_flowgraph().get_monitor().state()['fft'].subscribe2(lambda v: None, the_subscription_context)
    
    if _abort_for_test:
        services.stopService()
        yield pfg.sync()
        defer.returnValue(app)
    else:
        yield defer.Deferred()  # never fires
コード例 #4
0
ファイル: main.py プロジェクト: misterdevil/shinysdr
def _main_async(reactor, argv=None, _abort_for_test=False):
    if argv is None:
        argv = sys.argv

    if not _abort_for_test:
        # Configure logging. Some log messages would be discarded if we did not set up things early
        # TODO: Consult best practices for Python and Twisted logging.
        # TODO: Logs which are observably relevant should be sent to the client (e.g. the warning of refusing to have more receivers active)
        logging.basicConfig(level=logging.INFO)
        log.startLoggingWithObserver(
            log.PythonLoggingObserver(loggerName='shinysdr').emit, False)

    # Option parsing is done before importing the main modules so as to avoid the cost of initializing gnuradio if we are aborting early. TODO: Make that happen for createConfig too.
    argParser = argparse.ArgumentParser(prog=argv[0])
    argParser.add_argument('config_path',
                           metavar='CONFIG',
                           help='path of configuration directory or file')
    argParser.add_argument(
        '--create',
        dest='createConfig',
        action='store_true',
        help='write template configuration file to CONFIG and exit')
    argParser.add_argument('-g, --go',
                           dest='openBrowser',
                           action='store_true',
                           help='open the UI in a web browser')
    argParser.add_argument(
        '--force-run',
        dest='force_run',
        action='store_true',
        help='Run DSP even if no client is connected (for debugging).')
    args = argParser.parse_args(args=argv[1:])

    # Verify we can actually run.
    # Note that this must be done before we actually load core modules, because we might get an import error then.
    version_report = yield _check_versions()
    if version_report:
        print >> sys.stderr, version_report
        sys.exit(1)

    # We don't actually use shinysdr.devices directly, but we want it to be guaranteed available in the context of the config file.
    # pylint: disable=unused-variable
    import shinysdr.devices as lazy_devices
    import shinysdr.source as lazy_source  # legacy shim

    # Load config file
    if args.createConfig:
        write_default_config(args.config_path)
        log.msg('Created default configuration at: ' + args.config_path)
        sys.exit(0)  # TODO: Consider using a return value or something instead
    else:
        configObj = Config(reactor)
        execute_config(configObj, args.config_path)
        yield configObj._wait_and_validate()

        stateFile = configObj._state_filename

    def restore(root, get_defaults):
        if stateFile is not None:
            if os.path.isfile(stateFile):
                root.state_from_json(json.load(open(stateFile, 'r')))
                # make a backup in case this code version misreads the state and loses things on save (but only if the load succeeded, in case the file but not its backup is bad)
                shutil.copyfile(stateFile, stateFile + '~')
            else:
                root.state_from_json(get_defaults(root))

    log.msg('Constructing...')
    app = configObj._create_app()

    singleton_reactor.addSystemEventTrigger('during', 'shutdown',
                                            app.close_all_devices)

    log.msg('Restoring state...')
    restore(app, _app_defaults)

    # Set up persistence
    if stateFile is not None:

        def eventually_write():
            log.msg('Scheduling state write.')

            def actually_write():
                log.msg('Performing state write...')
                current_state = pcd.get()
                with open(stateFile, 'w') as f:
                    json.dump(current_state, f)
                log.msg('...done')

            reactor.callLater(_PERSISTENCE_DELAY, actually_write)

        pcd = PersistenceChangeDetector(app, eventually_write,
                                        the_subscription_context)
        # Start implicit write-to-disk loop, but don't actually write.
        # This is because it is useful in some failure modes to not immediately overwrite a good state file with a bad one on startup.
        pcd.get()

    log.msg('Starting web server...')
    services = MultiService()
    for maker in configObj._service_makers:
        IService(maker(app)).setServiceParent(services)
    services.startService()

    log.msg('ShinySDR is ready.')

    for service in services:
        # TODO: should have an interface (currently no proper module to put it in)
        service.announce(args.openBrowser)

    if args.force_run:
        log.msg('force_run')
        from gnuradio.gr import msg_queue
        # TODO kludge, make this less digging into guts
        app.get_receive_flowgraph().monitor.get_fft_distributor().subscribe(
            msg_queue(limit=2))

    if _abort_for_test:
        services.stopService()
        defer.returnValue(app)
    else:
        yield defer.Deferred()  # never fires
コード例 #5
0
ファイル: main.py プロジェクト: thefinn93/shinysdr
def _main_async(reactor, argv=None, _abort_for_test=False):
    if argv is None:
        argv = sys.argv
    
    if not _abort_for_test:
        # Some log messages would be discarded if we did not set up things early.
        configure_logging()
    
    # Option parsing is done before importing the main modules so as to avoid the cost of initializing gnuradio if we are aborting early. TODO: Make that happen for createConfig too.
    argParser = argparse.ArgumentParser(prog=argv[0])
    argParser.add_argument('config_path', metavar='CONFIG',
        help='path of configuration directory or file')
    argParser.add_argument('--create', dest='createConfig', action='store_true',
        help='write template configuration file to CONFIG and exit')
    argParser.add_argument('-g, --go', dest='openBrowser', action='store_true',
        help='open the UI in a web browser')
    argParser.add_argument('--force-run', dest='force_run', action='store_true',
        help='Run DSP even if no client is connected (for debugging).')
    args = argParser.parse_args(args=argv[1:])

    # Verify we can actually run.
    # Note that this must be done before we actually load core modules, because we might get an import error then.
    version_report = yield _check_versions()
    if version_report:
        print >>sys.stderr, version_report
        sys.exit(1)

    # Write config file and exit if asked ...
    if args.createConfig:
        write_default_config(args.config_path)
        log.msg('Created default configuration at: ' + args.config_path)
        sys.exit(0)  # TODO: Consider using a return value or something instead
    
    # ... else read config file
    config_obj = Config(reactor)
    execute_config(config_obj, args.config_path)
    yield config_obj._wait_and_validate()
    
    log.msg('Constructing...')
    app = config_obj._create_app()
    
    reactor.addSystemEventTrigger('during', 'shutdown', app.close_all_devices)
    
    log.msg('Restoring state...')
    pfg = PersistenceFileGlue(
        reactor=reactor,
        root_object=app,
        filename=config_obj._state_filename,
        get_defaults=_app_defaults)
    
    log.msg('Starting web server...')
    services = MultiService()
    for maker in config_obj._service_makers:
        IService(maker(app)).setServiceParent(services)
    services.startService()
    
    log.msg('ShinySDR is ready.')
    
    for service in services:
        # TODO: should have an interface (currently no proper module to put it in)
        service.announce(args.openBrowser)
    
    if args.force_run:
        log.msg('force_run')
        from gnuradio.gr import msg_queue
        # TODO kludge, make this less digging into guts
        app.get_receive_flowgraph().monitor.get_fft_distributor().subscribe(msg_queue(limit=2))
    
    if _abort_for_test:
        services.stopService()
        yield pfg.sync()
        defer.returnValue(app)
    else:
        yield defer.Deferred()  # never fires