Merge lp:~joetalbott/utah/parser_update into lp:utah

Proposed by Joe Talbott
Status: Merged
Merged at revision: 756
Proposed branch: lp:~joetalbott/utah/parser_update
Merge into: lp:utah
Diff against target: 89 lines (+41/-9)
1 file modified
utah/parser.py (+41/-9)
To merge this branch: bzr merge lp:~joetalbott/utah/parser_update
Reviewer Review Type Date Requested Status
Javier Collado (community) Approve
Review via email: mp+135433@code.launchpad.net

Description of the change

This branch adds support for parsing UTAH client logs stored in string.

In addition there is improved error handling and I removed a hardcoded path.

To post a comment you must log in.
Revision history for this message
Javier Collado (javier.collado) wrote :

Looks fine to me.

I've seen http support has been added to download a remote file. Note that you
can use utah.url.url_argument to add support for launchpad url's as well:

from utah.url import url_argument

# bzr export log_file_url
log_file = url_argument(log_file_url)

review: Approve
Revision history for this message
Joe Talbott (joetalbott) wrote :

This code is to be packaged as a separate module and will be run from the dashboard. Once we get pieces of utah packaged individually I'll use the utah.url module. For the time being I'm going to merge.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'utah/parser.py'
2--- utah/parser.py 2012-11-14 22:10:40 +0000
3+++ utah/parser.py 2012-11-21 15:03:20 +0000
4@@ -1,5 +1,8 @@
5+import argparse
6+import jsonschema
7+import logging
8 import os
9-import jsonschema
10+import urllib2
11 import yaml
12
13 class UTAHParser(object):
14@@ -67,25 +70,31 @@
15 },
16 }
17
18- def parse(self, logdata):
19+ def parse(self, logdata, as_string=False):
20 """
21 Parse utah client results.
22
23 logdata should be either a filename or a handle to a file
24 object.
25
26- Returns the parsed yaml data.
27+ Returns the parsed yaml data or None.
28
29 """
30
31- if isinstance(logdata, str):
32+ if as_string:
33+ return self._parse_string(logdata)
34+ elif isinstance(logdata, str):
35 return self._parse_logfile(logdata)
36 else:
37 return self._parse_stream(logdata)
38
39 def _parse_stream(self, stream):
40 """ Parse client output from stream. """
41- data = yaml.load(stream)
42+ try:
43+ data = yaml.load(stream)
44+ except yaml.YAMLError as e:
45+ logging.error(e)
46+ return None
47
48 jsonschema.validate(data, self.CLIENT_OUTPUT_SCHEMA)
49
50@@ -96,13 +105,36 @@
51
52 data = None
53
54- with open(logfile, 'r') as fp:
55+ if logfile.startswith('http'):
56+ fp = urllib2.urlopen(logfile)
57 data = self._parse_stream(fp)
58-
59- return data
60+ else:
61+ with open(logfile, 'r') as fp:
62+ data = self._parse_stream(fp)
63+
64+ return data
65+
66+ def _parse_string(self, log_data):
67+ """ Parse client data from a string. """
68+ try:
69+ data = yaml.load(log_data)
70+ except yaml.YAMLError as e:
71+ logging.error(e)
72+ return None
73+
74+ jsonschema.validate(data, self.CLIENT_OUTPUT_SCHEMA)
75+
76+ return data
77+
78
79 if __name__ == "__main__":
80+ arg_parser = argparse.ArgumentParser(description='phoenix bootstrapper')
81+ arg_parser.add_argument('logfile', metavar='LOGFILE', type=str,
82+ help='log file to parse')
83+
84+ args = arg_parser.parse_args()
85+
86 parser = UTAHParser()
87- data = parser.parse("/tmp/utah.yaml")
88+ data = parser.parse(args.logfile)
89 print(data)
90

Subscribers

People subscribed via source and target branches