diff options
author | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-08-24 13:59:06 +0900 |
---|---|---|
committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-09-12 13:50:25 +0900 |
commit | 2265e6b910d8549d5de616010119050d36fb9b55 (patch) | |
tree | b02fa5744a892d49a31fbc50caae21496fa1b360 /example.py | |
parent | eedae563f0445b3557039ec0af66dc78d2ad2fee (diff) | |
download | pygerrit-2265e6b910d8549d5de616010119050d36fb9b55.tar.gz |
Add an example of how the Gerrit client class is used
Change-Id: Ia6df5a588e4789fdf24825a8492f6c11b35e899b
Diffstat (limited to 'example.py')
-rwxr-xr-x | example.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/example.py b/example.py new file mode 100755 index 0000000..95c7c21 --- /dev/null +++ b/example.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +""" Example of using the Gerrit client class. """ + +import logging +import optparse +import sys +import time + +from pygerrit.client import GerritClient +from pygerrit.stream import GerritStreamErrorEvent + + +def _main(): + usage = "usage: %prog [options]" + parser = optparse.OptionParser(usage=usage) + parser.add_option('-g', '--gerrit-hostname', dest='hostname', + default='review', + help='gerrit server hostname (default: %default)') + parser.add_option('-b', '--blocking', dest='blocking', + action='store_true', + help='block on event get (default: False)') + parser.add_option('-t', '--timeout', dest='timeout', + default=None, type='int', + help='timeout (seconds) for blocking event get ' + '(default: None)') + + (options, _args) = parser.parse_args() + if options.timeout and not options.blocking: + parser.error('Can only use -t with -b') + + logging.basicConfig(format='%(message)s', level=logging.INFO) + + gerrit = GerritClient(host=options.hostname) + gerrit.start_event_stream() + try: + while True: + event = gerrit.get_event(block=options.blocking, + timeout=options.timeout) + if event: + logging.info("Event: %s", event.name) + if isinstance(event, GerritStreamErrorEvent): + logging.error(event.error) + break + else: + logging.info("No event") + if not options.blocking: + time.sleep(1) + except KeyboardInterrupt: + logging.info("Terminated by user") + gerrit.stop_event_stream() + + +if __name__ == "__main__": + sys.exit(_main()) |