예제 #1
0
파일: dexter.py 프로젝트: blundercon/dexter
def main(config=None):
    '''
    Main entry point.
    '''
    if config is not None:
        try:
            with open(config) as fh:
                configuration = json.load(fh)
        except Exception as e:
            LOG.fatal("Failed to parse config file '%s': %s" % (config, e))
            sys.exit(1)
    else:
        configuration = CONFIG

    # And spawn it
    dexter = Dexter(configuration)
    dexter.run()
예제 #2
0
    def _start(self):
        '''
        Start the system going.
        '''
        # Start the notifiers
        try:
            self._state.start()
        except Exception as e:
            LOG.fatal("Failed to start notifiers: %s" % (e, ))
            sys.exit(1)

        # And the components
        for component in self._inputs + self._outputs + self._services:
            # If these throw then it's fatal
            LOG.info("Starting %s" % (component, ))
            try:
                component.start()
            except Exception as e:
                LOG.fatal("Failed to start %s: %s" % (component, e))
                sys.exit(1)
예제 #3
0
파일: dexter.py 프로젝트: iamsrp/dexter
def main(log_level=None, config=None):
    """
    Dexter is a personal assistant which responds to natural language for its
    commands.
    """
    # Set the log level, if supplied
    if log_level is not None:
        try:
            LOG.getLogger().setLevel(int(log_level))
        except:
            LOG.getLogger().setLevel(log_level.upper())

    # Load in any configuration
    if config is not None:
        try:
            with open(config) as fh:
                configuration = json.load(fh)
        except Exception as e:
            LOG.fatal("Failed to parse config file '%s': %s" % (config, e))
            sys.exit(1)
    else:
        configuration = CONFIG

    # Handle environment variables in the component kwargs, in the form of
    # "${VARNAME}". This isn't overly pretty, but it works.
    for typ in configuration['components']:
        # We might have kwargs for all the components
        for component in configuration['components'][typ]:
            (which, kwargs) = component
            if kwargs is None:
                continue

            # Look at all the kwargs which we have and check for environment
            # variables in the value names.
            for name in kwargs:
                value = kwargs[name]
                try:
                    while True:
                        # Pull out the variable name, if it exists
                        start = value.index('${')
                        end = value.index('}', start)
                        varname = value[start + 2:end]
                        varvalue = os.environ.get(varname, '')

                        # Special handling for some variables
                        if not varvalue:
                            # These are not always set in the environment but
                            # people tend to expect it to be, so we are nice and
                            # provide them
                            if varname == "HOSTNAME":
                                varvalue = socket.gethostname()
                            elif varname == "USER":
                                varvalue = getpass.getuser()

                        # And replace it
                        value = (value[:start] + varvalue + value[end + 1:])
                except:
                    # This means we failed to find the opening or closing
                    # varname container in the string, so we're done
                    pass

                # If we changed it then save it back in
                if value != kwargs[name]:
                    LOG.info(
                        "Expanded component %s:%s:%s argument from '%s' to '%s'"
                        % (typ, which, name, kwargs[name], value))
                    kwargs[name] = value

    # And spawn it
    dexter = Dexter(configuration)
    dexter.run()