示例#1
0
def run() -> None:
    """Update path and run the App."""

    # update the path to ensure the App has access to required modules
    app_lib = AppLib()
    app_lib.update_path()

    # import modules after path has been updated
    # third-party
    from app import App  # pylint: disable=import-outside-toplevel
    from tcex import TcEx  # pylint: disable=import-outside-toplevel

    tcex = TcEx()

    try:
        # load App class
        app = App(tcex)

        # perform prep/setup operations
        app.setup(**{})

        # run the App logic
        app.run(**{})

        # perform cleanup/teardown operations
        app.teardown(**{})

        # explicitly call the exit method
        tcex.exit(msg=app.exit_message)

    except Exception as e:
        main_err = f'Generic Error.  See logs for more details ({e}).'
        tcex.log.error(traceback.format_exc())
        tcex.playbook.exit(1, main_err)
def run() -> None:
    """Update path and run the App."""

    # update the path to ensure the App has access to required modules
    app_lib = AppLib()
    app_lib.update_path()

    # import modules after path has been updated

    # third-party
    from tcex import TcEx  # pylint: disable=import-outside-toplevel

    # first-party
    from app import App  # pylint: disable=import-outside-toplevel

    tcex = TcEx()

    try:
        # load App class
        app = App(tcex)

        # perform prep/setup operations
        app.setup(**{})

        # run the App logic
        if hasattr(app.args, 'tc_action') and app.args.tc_action is not None:
            # if the args NameSpace has the reserved arg of "tc_action", this arg value is used to
            # triggers a call to the app.<tc_action>() method.  an exact match to the method is
            # tried first, followed by a normalization of the tc_action value, and finally an
            # attempt is made to find the reserved "tc_action_map" property to map value to method.
            tc_action: str = app.args.tc_action
            tc_action_formatted: str = tc_action.lower().replace(' ', '_')
            tc_action_map = 'tc_action_map'  # reserved property name for action to method map
            # run action method
            if hasattr(app, tc_action):
                getattr(app, tc_action)()
            elif hasattr(app, tc_action_formatted):
                getattr(app, tc_action_formatted)()
            elif hasattr(app, tc_action_map):
                app.tc_action_map.get(app.args.tc_action)()  # pylint: disable=no-member
            else:
                tcex.exit(
                    1, f'Action method ({app.args.tc_action}) was not found.')
        else:
            # default to run method
            app.run(**{})

        # write requested value for downstream Apps
        app.write_output()  # pylint: disable=no-member

        # perform cleanup/teardown operations
        app.teardown(**{})

        # explicitly call the exit method
        tcex.playbook.exit(msg=app.exit_message)

    except Exception as e:
        main_err = f'Generic Error.  See logs for more details ({e}).'
        tcex.log.error(traceback.format_exc())
        tcex.playbook.exit(1, main_err)
示例#3
0
def run(**kwargs) -> None:
    """Update path and run the App."""

    # update the path to ensure the App has access to required modules
    app_lib = AppLib()
    app_lib.update_path()

    # import modules after path has been updated

    # third-party
    from tcex import TcEx  # pylint: disable=import-outside-toplevel

    # first-party
    from app import App  # pylint: disable=import-outside-toplevel

    tcex = TcEx()

    try:
        # load App class
        app = App(tcex)

        # set app property in testing framework
        if callable(kwargs.get('set_app')):
            kwargs.get('set_app')(app)

        # perform prep/setup operations
        app.setup()

        # configure custom trigger message handler
        tcex.service.api_event_callback = app.api_event_callback

        # listen on channel/topic
        tcex.service.listen()

        # start heartbeat threads
        tcex.service.heartbeat()

        # inform TC that micro-service is Ready
        tcex.service.ready = True

        # loop until exit
        tcex.log.info('feature=app, event=loop-forever')
        while tcex.service.loop_forever(sleep=1):
            pass

        # perform cleanup/teardown operations
        app.teardown()

        # explicitly call the exit method
        tcex.exit(msg=app.exit_message)

    except Exception as e:
        main_err = f'Generic Error. See logs for more details ({e}).'
        tcex.log.error(traceback.format_exc())
        tcex.exit(1, main_err)
def run() -> None:
    """Update path and run the App."""

    # update the path to ensure the App has access to required modules
    app_lib = AppLib()
    app_lib.update_path()

    # import modules after path has been updated
    # third-party
    from tcex import TcEx  # pylint: disable=import-outside-toplevel

    # first-party
    from app import App  # pylint: disable=import-outside-toplevel

    config_file = 'app_config.json'
    if not os.path.isfile(config_file):
        print(f'Missing {config_file} config file.')

    tcex = TcEx(config_file=config_file)

    try:
        # load App class
        app = App(tcex)

        # perform prep/setup operations
        app.setup()

        # run the App logic
        app.run()  # pylint: disable=no-member

        # perform cleanup/teardown operations
        app.teardown()

        # explicitly call the exit method
        tcex.exit(msg=app.exit_message)

    except Exception as e:
        main_err = f'Generic Error.  See logs for more details ({e}).'
        tcex.log.error(traceback.format_exc())
        tcex.exit(1, main_err)
示例#5
0
文件: run.py 项目: fcodiaz/tcex
    # update the path to ensure the App has access to required modules
    _update_app_path()

    # import modules after path has been updated
    from tcex import TcEx
    from app import App

    tcex = TcEx()

    try:
        # load App class
        app = App(tcex)

        # perform prep/setup operations
        app.setup()

        # run the App logic
        app.run()

        # perform cleanup/teardown operations
        app.teardown()

        # explicitly call the exit method
        tcex.exit(msg=app.exit_message)

    except Exception as e:
        main_err = f'Generic Error.  See logs for more details ({e}).'
        tcex.log.error(traceback.format_exc())
        tcex.playbook.exit(1, main_err)
示例#6
0
            # tried first, followed by a normalization of the tc_action value, and finally an
            # attempt is made to find the reserved "tc_action_map" property to map value to method.
            tc_action = app.args.tc_action
            tc_action_formatted = tc_action.lower().replace(' ', '_')
            tc_action_map = 'tc_action_map'  # reserved property name for action to method map
            try:
                # run action method
                if hasattr(app, tc_action):
                    getattr(app, tc_action)()
                elif hasattr(app, tc_action_formatted):
                    getattr(app, tc_action_formatted)()
                elif hasattr(app, tc_action_map):
                    app.tc_action_map.get(app.args.tc_action)()
            except AttributeError:
                tcex.exit(
                    1, 'Action method ({}) was not found.'.format(
                        app.args.tc_action))
        else:
            # default to run method
            app.run()

        # write requested value for downstream Apps
        tcex.playbook.write_output()
        app.write_output()  # pylint: disable=E1101

        # perform cleanup operations
        app.done()

        # explicitly call the exit method
        tcex.playbook.exit(msg=app.exit_message)
示例#7
0
    tcex.playbook.create_output('json', blob.json)
    tcex.playbook.create_output(
        'nGrams', [str(n_gram) for n_gram in blob.ngrams(n=n_gram_number)])
    tcex.playbook.create_output('nounPhrases', blob.noun_phrases)
    tcex.playbook.create_output('npCounts', blob.np_counts[1])
    tcex.playbook.create_output('polarity', blob.polarity)
    tcex.playbook.create_output('sentences',
                                [str(sentence) for sentence in blob.sentences])
    tcex.playbook.create_output('subjectivity', blob.subjectivity)
    tcex.playbook.create_output('tags', tags)
    tcex.playbook.create_output('tokens', blob.tokens)
    tcex.playbook.create_output('wordCounts', blob.word_counts[1])
    tcex.playbook.create_output('words', blob.words)

    tcex.exit(0)


if __name__ == "__main__":
    tcex = TcEx()
    try:
        # start the app
        main()
    except SystemExit:
        pass
    except Exception as e:  # if there are any strange errors, log it to the logging in the UI
        err = 'Generic Error.  See logs for more details ({}).'.format(e)
        tcex.log.error(traceback.format_exc())
        tcex.message_tc(err)
        tcex.exit(1)
示例#8
0
            # if the args NameSpace has the reserved arg of "tc_action", this arg value is used to
            # triggers a call to the app.<tc_action>() method.  an exact match to the method is
            # tried first, followed by a normalization of the tc_action value, and finally an
            # attempt is made to find the reserved "tc_action_map" property to map value to method.
            tc_action = app.args.tc_action
            tc_action_formatted = tc_action.lower().replace(' ', '_')
            tc_action_map = 'tc_action_map'  # reserved property name for action to method map
            # run action method
            if hasattr(app, tc_action):
                getattr(app, tc_action)()
            elif hasattr(app, tc_action_formatted):
                getattr(app, tc_action_formatted)()
            elif hasattr(app, tc_action_map):
                app.tc_action_map.get(app.args.tc_action)()  # pylint: disable=no-member
            else:
                tcex.exit(1, f'Action method ({app.args.tc_action}) was not found.')
        else:
            # default to run method
            app.run()

        # write requested value for downstream Apps
        tcex.playbook.write_output()
        app.write_output()  # pylint: disable=no-member

        # perform cleanup/teardown operations
        app.teardown()

        # explicitly call the exit method
        tcex.playbook.exit(msg=app.exit_message)

    except Exception as e:
示例#9
0
        self.my_output.append(self.my_choice)
        self.my_output.extend(self.my_multi)
        self.my_output.append(str(self.my_boolean))
        self.my_output.append(self.my_hidden)

    def write_output(self):
        """Write the Playbook output variables."""
        tcex.log.info('writing output')
        tcex.playbook.create_output('mypbapp.output.0', self.my_output[0])
        tcex.playbook.create_output('mypbapp.output', self.my_output)


if __name__ == '__main__':
    try:
        app = MyApp(args)
        tcex.log.info('my_input: {}'.format(app.my_input))

        # do something
        app.do_the_thing()

        # write requested value for downstream Apps
        app.write_output()

        # write message and gracefully exit the App
        tcex.exit(0, 'Success.')

    except Exception as e:
        main_err = 'Generic Error.  See logs for more details ({}).'.format(e)
        tcex.log.error(traceback.format_exc())
        tcex.playbook.exit(1, main_err)
        app = App(tcex)

        # parse args
        app.parse_args()

        # perform prep/startup operations
        app.start()

        # run the App logic
        if hasattr(app.args, 'tc_action') and app.args.tc_action is not None:
            try:
                # run action method
                getattr(app, app.args.tc_action)()
            except AttributeError:
                tcex.exit(
                    1, 'Action method ({}) was not found.'.format(
                        app.args.tc_action))
        else:
            # default to run method
            app.run()

        # perform cleanup operations
        app.done()

        # explicitly call the exit method
        tcex.exit(msg=app.exit_message)

    except Exception as e:
        main_err = 'Generic Error.  See logs for more details ({}).'.format(e)
        tcex.log.error(traceback.format_exc())
        tcex.playbook.exit(1, main_err)
示例#11
0
        self.my_output.append(self.args.password)
        self.my_output.append(self.args.my_choice)
        self.my_output.extend(self.args.my_multi)
        self.my_output.append(str(self.args.my_boolean))
        self.my_output.append(self.args.my_hidden)


if __name__ == '__main__':
    try:
        # get app init timestamp for last_run
        last_run = time.time()

        # get MyApp instance
        app = MyApp(args)
        tcex.log.info('username: {}'.format(args.username))

        # do something
        app.do_the_thing()

        # store persistent value
        tcex.results_tc('last_run', last_run)

        # write message and gracefully exit the App
        tcex.exit(0, 'Success.')

    except Exception as e:
        tcex.results_tc('last_run', args.last_run)
        main_err = 'Generic Error.  See logs for more details ({}).'.format(e)
        tcex.log.error(traceback.format_exc())
        tcex.exit(1, main_err)