Skip to content

Commit

Permalink
converted tail to command based as well
Browse files Browse the repository at this point in the history
  • Loading branch information
sontek authored and ralphbean committed Nov 27, 2012
1 parent c3b0b3b commit d83b6ed
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 102 deletions.
2 changes: 1 addition & 1 deletion fedmsg/commands/logger.py
Expand Up @@ -109,6 +109,6 @@ def run(self):
self._log_message(self.config, line.strip())
line = sys.stdin.readline()

def main():
def logger():
command = LoggerCommand()
return command.execute()
2 changes: 1 addition & 1 deletion fedmsg/commands/relay.py
Expand Up @@ -58,6 +58,6 @@ def run(self):
from moksha.hub import main
main(options=self.config, consumers=[RelayConsumer])

def main():
def relay():
command = RelayCommand()
return command.execute()
198 changes: 100 additions & 98 deletions fedmsg/commands/tail.py
Expand Up @@ -28,103 +28,105 @@
import fedmsg
import fedmsg.encoding
import fedmsg.text
from fedmsg.commands import command


extra_args = [
(['--topic'], {
'dest': 'topic',
'help': 'The topic pattern to listen for. Everything by default.',
'default': '',
}),
(['--pretty'], {
'dest': 'pretty',
'help': 'Pretty print the JSON messages.',
'default': False,
'action': 'store_true',
}),
(['--really-pretty'], {
'dest': 'really_pretty',
'help': 'Extra-pretty print the JSON messages.',
'default': False,
'action': 'store_true',
}),
(['--terse'], {
'dest': 'terse',
'help': 'Print "english" representations of messages only.',
'default': False,
'action': 'store_true',
}),
(['--filter'], {
'dest': 'exclusive_regexp',
'metavar': 'REGEXP',
'help': 'Only show topics that do not match the supplied regexp.',
'default': '_heartbeat',
}),
(['--regexp'], {
'dest': 'inclusive_regexp',
'metavar': 'REGEXP',
'help': 'Only show topics that match the supplied regexp.',
'default': '^((?!_heartbeat).)*$',
}),
]


@command(name="fedmsg-tail", extra_args=extra_args)
def tail(**kw):
""" Watch all endpoints on the bus and print each message to stdout. """

# Disable sending
kw['publish_endpoint'] = None

# Disable timeouts. We want to tail forever!
kw['timeout'] = 0

# Even though fedmsg-tail won't be sending any messages, give it a name to
# conform with the other commands.
kw['name'] = 'relay_inbound'

# Tail is never going to send any messages, so we suppress warnings about
# having no publishing sockets established.
kw['mute'] = True

fedmsg.init(**kw)
fedmsg.text.make_processors(**kw)

# Build a message formatter
formatter = lambda d: d
if kw['pretty']:
def formatter(d):
d['timestamp'] = time.ctime(d['timestamp'])
d = fedmsg.crypto.strip_credentials(d)
return "\n" + pprint.pformat(d)
from fedmsg.commands import BaseCommand

if kw['really_pretty']:
def formatter(d):
d = fedmsg.crypto.strip_credentials(d)
fancy = pygments.highlight(
fedmsg.encoding.pretty_dumps(d),
pygments.lexers.JavascriptLexer(),
pygments.formatters.TerminalFormatter()
).strip()
return "\n" + fancy

if kw['terse']:
formatter = lambda d: "\n" + fedmsg.text.msg2repr(d, **kw)

exclusive_regexp = re.compile(kw['exclusive_regexp'])
inclusive_regexp = re.compile(kw['inclusive_regexp'])

# The "proper" fedmsg way to do this would be to spin up or connect to an
# existing Moksha Hub and register a consumer on the "*" topic that simply
# prints out each message it consumes. That seems like overkill, so we're
# just going to directly access the endpoints ourself.

for name, ep, topic, message in fedmsg.tail_messages(**kw):
if exclusive_regexp.search(topic):
continue

if not inclusive_regexp.search(topic):
continue

print name, ep, topic, formatter(message)
class TailCommand(BaseCommand):
""" Watch all endpoints on the bus and print each message to stdout. """
name="fedmsg-tail"
extra_args = [
(['--topic'], {
'dest': 'topic',
'help': 'The topic pattern to listen for. Everything by default.',
'default': '',
}),
(['--pretty'], {
'dest': 'pretty',
'help': 'Pretty print the JSON messages.',
'default': False,
'action': 'store_true',
}),
(['--really-pretty'], {
'dest': 'really_pretty',
'help': 'Extra-pretty print the JSON messages.',
'default': False,
'action': 'store_true',
}),
(['--terse'], {
'dest': 'terse',
'help': 'Print "english" representations of messages only.',
'default': False,
'action': 'store_true',
}),
(['--filter'], {
'dest': 'exclusive_regexp',
'metavar': 'REGEXP',
'help': 'Only show topics that do not match the supplied regexp.',
'default': '_heartbeat',
}),
(['--regexp'], {
'dest': 'inclusive_regexp',
'metavar': 'REGEXP',
'help': 'Only show topics that match the supplied regexp.',
'default': '^((?!_heartbeat).)*$',
}),
]
def run(self):
# Disable sending
self.config['publish_endpoint'] = None

# Disable timeouts. We want to tail forever!
self.config['timeout'] = 0

# Even though fedmsg-tail won't be sending any messages, give it a name to
# conform with the other commands.
self.config['name'] = 'relay_inbound'

# Tail is never going to send any messages, so we suppress warnings about
# having no publishing sockets established.
self.config['mute'] = True

fedmsg.init(**self.config)
fedmsg.text.make_processors(**self.config)

# Build a message formatter
formatter = lambda d: d
if self.config['pretty']:
def formatter(d):
d['timestamp'] = time.ctime(d['timestamp'])
d = fedmsg.crypto.strip_credentials(d)
return "\n" + pprint.pformat(d)

if self.config['really_pretty']:
def formatter(d):
d = fedmsg.crypto.strip_credentials(d)
fancy = pygments.highlight(
fedmsg.encoding.pretty_dumps(d),
pygments.lexers.JavascriptLexer(),
pygments.formatters.TerminalFormatter()
).strip()
return "\n" + fancy

if self.config['terse']:
formatter = lambda d: "\n" + fedmsg.text.msg2repr(d, **self.config)

exclusive_regexp = re.compile(self.config['exclusive_regexp'])
inclusive_regexp = re.compile(self.config['inclusive_regexp'])

# The "proper" fedmsg way to do this would be to spin up or connect to an
# existing Moksha Hub and register a consumer on the "*" topic that simply
# prints out each message it consumes. That seems like overkill, so we're
# just going to directly access the endpoints ourself.

for name, ep, topic, message in fedmsg.tail_messages(**self.config):
if exclusive_regexp.search(topic):
continue

if not inclusive_regexp.search(topic):
continue

self.logger.info("%s, %s, %s, %s" % (name, ep, topic, formatter(message)))

def tail():
command = TailCommand()
return command.execute()
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -108,10 +108,10 @@
],
entry_points={
'console_scripts': [
"fedmsg-logger=fedmsg.commands.logger:main",
"fedmsg-logger=fedmsg.commands.logger:logger",
"fedmsg-tail=fedmsg.commands.tail:tail",
"fedmsg-hub=fedmsg.commands.hub:hub",
"fedmsg-relay=fedmsg.commands.relay:main",
"fedmsg-relay=fedmsg.commands.relay:relay",
"fedmsg-gateway=fedmsg.commands.gateway:gateway",
#"fedmsg-config=fedmsg.commands.config:config",
"fedmsg-irc=fedmsg.commands.ircbot:ircbot",
Expand Down

0 comments on commit d83b6ed

Please sign in to comment.