Пример #1
0
	def get_produce_function(self):
		return tweetprocessor.get_produce_function()
Пример #2
0
def main(argv=None):
    '''Command line options.'''

    global DEBUG

    if argv is None:
        argv = sys.argv
    else:
        sys.argv.extend(argv)

    program_name = os.path.basename(sys.argv[0])
    program_version = "v%s" % __version__
    program_build_date = str(__updated__)
    program_version_message = '%%(prog)s %s (%s)' % (program_version,
                                                     program_build_date)
    program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
    program_license = '''%s

  Created by user_name on %s.
  Copyright 2013 organization_name. All rights reserved.

  Licensed under the Apache License 2.0
  http://www.apache.org/licenses/LICENSE-2.0

  Distributed on an "AS IS" basis without warranties
  or conditions of any kind, either express or implied.

USAGE
''' % (program_shortdesc, str(__date__))

    verbose = False
    DEBUG = False
    with_offline_tweets = False

    try:
        # Setup argument parser
        parser = ArgumentParser(description=program_license,
                                formatter_class=RawDescriptionHelpFormatter)
        parser.add_argument("-v", "--verbose", action="store_true",
                            help="set verbosity level [default: %(default)s]")
        parser.add_argument("-d", "--debug", action="store_true",
                            help="produce debug output [default: %(default)s]")
        parser.add_argument("-o", "--with-offline-tweets", action="store_true",
                            help="use offline tweet database [default: %(default)s]")
        parser.add_argument("-r", "--run", action="store_true",
                            help="run without waiting for browser [default: %(default)s]")
        parser.add_argument("-V", "--version", action="version",
                            version=program_version_message)
        parser.add_argument("-p", "--port", type=int, default=7737,
                            metavar="N", help="set port to listen on "
                            "[default: %(default)s]")
        parser.add_argument("-s", "--speed", type=int, default=100000,
                            metavar="N", help="set rule engine speed "
                            "[default: %(default)s]")
        parser.add_argument("infile", nargs="?", type=FileType("r"),
                            default=sys.stdin, help="file containing event "
                            "messages [default: %(default)s]")

        # Process arguments
        args = parser.parse_args()

        verbose = args.verbose
        DEBUG = args.debug  # pylint: disable=W0603
        with_offline_tweets = args.with_offline_tweets
        port = args.port
        speed = args.speed
        infile = args.infile

        if verbose > 0:
            logging.basicConfig(level=logging.INFO)

        if DEBUG > 0:
            logging.basicConfig(level=logging.DEBUG)

        # if with_offline_tweets > 0:
            import dboffline as dbconnect
            logging.info("Using offline tweet database")
        # else:
            # import dbconnect
            # logging.info("Using tweets from online database")

        logging.info("noDUI.py: Verbosity level %s.", verbose)
        logging.info("noDUI.py: Running %s, output via port %s.",
                     infile.name, port)

        # Command line parameter processing done, now the real work starts.

        # 1. Connect to the database:
        # TODO: everything's currently hardcoded. Make this more flexible,
        # e.g. by reading settings.ini.
        # try:
            # dbconnect.connect_to_db('130.89.10.35', 'antwan', 'batatweets',
                                    # 'anton_tweets')
        # except Exception as ex:
            # logging.error("Cannot connect to database")
            # return 1
       
        # 2. Try to see if we can parse the input file:
        rengine.load_file_stream(infile)
        
        # 3. Start the server component of the OUI:
        # Tweetprocessor starts a HTTP server that runs forever,
        # so it needs its own thread:
        tweetprocessor_thread = threading.Thread(target = tweetprocessor.process_tweets,
                                                 args = [port],
                                                 name = "tweetprocessor_thread")
        tweetprocessor_thread.start()
        logging.info("Server started.")

        # 4. Start the rule engine:
        # TODO: this needs an observer
        produce_function = tweetprocessor.get_produce_function()
        threadsync_event = tweetprocessor.EVENT
        if args.run:
            threadsync_event = None
        result = rengine.start_rule_engine(speed = speed,
                                  produce = produce_function,
                                  threadsync_event = threadsync_event)
        if result:
            logging.error(result)
        else:
            logging.info("Control handed to rule engine")

        # 5. Wait for rule engine to finish (this can be interrupted with CTRL-C):
        while (rengine.engine_thread is not None and
               rengine.engine_thread.is_alive()):
            logging.debug("Rule engine still active")
            rengine.engine_thread.join(0.5)

        # 6. Rule engine has finished. Shut down tweetprocessor:
        cleanup("rule engine stopped")
        return 0

    except KeyboardInterrupt:
        # This exception is raised upon receiving CTRL-C or SIGINT
        cleanup("CTRL-C or SIGINT received")
        return 0
    except Exception as ex:
        if DEBUG or TESTRUN:
            raise(ex)
        indent = len(program_name) * " "
        sys.stderr.write(program_name + ": " + repr(ex) + "\n")
        sys.stderr.write(indent + "  for help use --help")
        return 2
Пример #3
0
	def get_produce_function(self):
		return tweetprocessor.get_produce_function()
Пример #4
0
def main(argv=None):
    '''Command line options.'''

    global DEBUG

    if argv is None:
        argv = sys.argv
    else:
        sys.argv.extend(argv)

    program_name = os.path.basename(sys.argv[0])
    program_version = "v%s" % __version__
    program_build_date = str(__updated__)
    program_version_message = '%%(prog)s %s (%s)' % (program_version,
                                                     program_build_date)
    program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
    program_license = '''%s

  Created by user_name on %s.
  Copyright 2013 organization_name. All rights reserved.

  Licensed under the Apache License 2.0
  http://www.apache.org/licenses/LICENSE-2.0

  Distributed on an "AS IS" basis without warranties
  or conditions of any kind, either express or implied.

USAGE
''' % (program_shortdesc, str(__date__))

    verbose = False
    DEBUG = False
    with_offline_tweets = False

    try:
        # Setup argument parser
        parser = ArgumentParser(description=program_license,
                                formatter_class=RawDescriptionHelpFormatter)
        parser.add_argument("-v",
                            "--verbose",
                            action="store_true",
                            help="set verbosity level [default: %(default)s]")
        parser.add_argument("-d",
                            "--debug",
                            action="store_true",
                            help="produce debug output [default: %(default)s]")
        parser.add_argument(
            "-o",
            "--with-offline-tweets",
            action="store_true",
            help="use offline tweet database [default: %(default)s]")
        parser.add_argument(
            "-r",
            "--run",
            action="store_true",
            help="run without waiting for browser [default: %(default)s]")
        parser.add_argument("-V",
                            "--version",
                            action="version",
                            version=program_version_message)
        parser.add_argument("-p",
                            "--port",
                            type=int,
                            default=7737,
                            metavar="N",
                            help="set port to listen on "
                            "[default: %(default)s]")
        parser.add_argument("-s",
                            "--speed",
                            type=int,
                            default=100000,
                            metavar="N",
                            help="set rule engine speed "
                            "[default: %(default)s]")
        parser.add_argument(
            "-b",
            "--begin",
            type=str,
            default='',
            metavar="yyyy-MM-dd[:HH[:mm[:ss]]]",
            help="set begin date and time [default: first tweet]")
        parser.add_argument("-e",
                            "--end",
                            type=str,
                            default='',
                            metavar="yyyy-MM-dd[:HH[:mm[:ss]]]",
                            help="set end date [default: last tweet]")
        parser.add_argument("infile",
                            nargs="?",
                            type=FileType("r"),
                            default=sys.stdin,
                            help="file containing event "
                            "messages [default: %(default)s]")

        # Process arguments
        args = parser.parse_args()

        verbose = args.verbose
        DEBUG = args.debug  # pylint: disable=W0603
        with_offline_tweets = args.with_offline_tweets
        port = args.port
        speed = args.speed
        begin_time = str2timefloat.ds2tf(args.begin)
        end_time = str2timefloat.ds2tf(args.end)
        infile = args.infile

        if verbose > 0:
            logging.basicConfig(level=logging.INFO)

        if DEBUG > 0:
            logging.basicConfig(level=logging.DEBUG)

            # if with_offline_tweets > 0:
            import dboffline as dbconnect
            logging.info("Using offline tweet database")
        # else:
        # import dbconnect
        # logging.info("Using tweets from online database")

        logging.info("noDUI.py: Verbosity level %s.", verbose)
        logging.info("noDUI.py: Running %s, output via port %s.", infile.name,
                     port)

        # Command line parameter processing done, now the real work starts.

        # 1. Connect to the database:
        # TODO: everything's currently hardcoded. Make this more flexible,
        # e.g. by reading settings.ini.
        # try:
        # dbconnect.connect_to_db('130.89.10.35', 'antwan', 'batatweets',
        # 'anton_tweets')
        # except Exception as ex:
        # logging.error("Cannot connect to database")
        # return 1

        # 2. Try to see if we can parse the input file:
        rengine.load_file_stream(infile)

        # 3. Start the server component of the OUI:
        # Tweetprocessor starts a HTTP server that runs forever,
        # so it needs its own thread:
        tweetprocessor_thread = threading.Thread(
            target=tweetprocessor.process_tweets,
            args=[port],
            name="tweetprocessor_thread")
        tweetprocessor_thread.start()
        logging.info("Server started.")

        # 4. Start the rule engine:
        # TODO: this needs an observer
        produce_function = tweetprocessor.get_produce_function()
        threadsync_event = tweetprocessor.EVENT
        if args.run:
            threadsync_event = None
        result = rengine.start_rule_engine(start_time=begin_time,
                                           stop_time=end_time,
                                           speed=speed,
                                           produce=produce_function,
                                           threadsync_event=threadsync_event)
        if result:
            logging.error(result)
        else:
            logging.info("Control handed to rule engine")

        # 5. Wait for rule engine to finish (this can be interrupted with CTRL-C):
        while (rengine.engine_thread is not None
               and rengine.engine_thread.is_alive()):
            logging.debug("Rule engine still active")
            rengine.engine_thread.join(0.5)

        # 6. Rule engine has finished. Shut down tweetprocessor:
        cleanup("rule engine stopped")
        return 0

    except KeyboardInterrupt:
        # This exception is raised upon receiving CTRL-C or SIGINT
        cleanup("CTRL-C or SIGINT received")
        return 0
    except Exception as ex:
        if DEBUG or TESTRUN:
            raise (ex)
        indent = len(program_name) * " "
        sys.stderr.write(program_name + ": " + repr(ex) + "\n")
        sys.stderr.write(indent + "  for help use --help")
        return 2