Last active
November 4, 2016 18:33
-
-
Save edwindotcom/17d22fdda7664b70da055d6bba0d647b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
import json | |
from pprint import pprint | |
import sys | |
sys.setrecursionlimit(1500) | |
def loadJson(json_file): | |
with open(json_file) as jdata: | |
data = json.load(jdata) | |
return data | |
employees = [] | |
out = "" | |
out_json = {} | |
def search(data, param, arg): | |
global out | |
global out_json | |
def print_nodes(node): | |
global reportsTo | |
global employees | |
global out | |
global out_json | |
for i in range(len(node)): | |
if node[i]['name'].lower().find(arg.lower()) != -1: | |
out_json = node[i] | |
employees.append(node[i]['name']) | |
out += ":: %s\n" % node[i]['name'] | |
reportsTo = node[i]['data']['reportsTo'] | |
# break | |
# return | |
if node[i]['children']: | |
print_nodes(node[i]['children']) | |
def find_by_id(node, id): | |
global out | |
for i in range(len(node)): | |
if int(node[i]['id']) == int(id): | |
out += ':: Reports to: %s' % node[i]['name'] | |
break | |
return | |
if node[i]['children']: | |
find_by_id(node[i]['children'], id) | |
print 'searching for %s in %s' % (arg, param) | |
print_nodes(data['children']) | |
if len(employees) > 1: | |
print 'Found more than one person:' | |
# pprint (employees) | |
print("\n".join(employees)) | |
print "Re run with ./orgchart 'fname lname' " | |
else: | |
find_by_id(data['children'], reportsTo) | |
pprint(out_json) | |
print '------------' | |
print out | |
print '------------' | |
sys.exit(0) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
sys.stderr.write('Usage: python org_chart.py name') | |
sys.exit(1) | |
data = loadJson('data_20161104.json') | |
search(data, 'name', sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment