Last active
December 15, 2015 18:39
-
-
Save drewkerrigan/5305903 to your computer and use it in GitHub Desktop.
riak-admin status to json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| def is_number(s): | |
| try: | |
| float(s) | |
| return True | |
| except ValueError: | |
| return False | |
| def status_to_json(raw_stats): | |
| # remove all unnecessary symbols and whitespace | |
| stats = "" | |
| for line in raw_stats.splitlines(): | |
| stats += line.strip() + '\n' | |
| stats = stats.replace('<<', '').replace('>>', '') | |
| stats = stats.replace('\\n', '').replace(',\n', ',') | |
| stats = stats.replace(": '",': ').replace("'\n","\n") | |
| stats = stats.replace(': "[', ': [').replace(']"\n',"]\n") | |
| stats = stats.replace('"',"'") | |
| matchObj = re.compile(r"^(.*) : (.*)", re.M|re.I) | |
| json_stats = '{' | |
| for match in matchObj.finditer(stats): | |
| key,value = match.groups() | |
| if (value[0] == "'"): | |
| value = value[1:-1] | |
| if not is_number(value): | |
| value = '"' + value + '"' | |
| json_stats += '"' + key + '":' + value + ',' | |
| json_stats = json_stats[0:-1] | |
| json_stats += '}' | |
| return json_stats | |
| f = open('test.txt', 'r') | |
| print status_to_json(f.read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment