-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgithub.py
More file actions
129 lines (113 loc) · 4.67 KB
/
Copy pathgithub.py
File metadata and controls
129 lines (113 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import click
import requests
from tabulate import tabulate
class github:
def __init__(self,ghf):
self.ghf=ghf
# def repos(self,org):
# if self.ghf.debug: click.echo("org:"+org+" token:"+self.ghf.token)
# url='https://api.github.com/orgs/'+org+'/repos'
# headers=self.get_auth_header()
# data=''
# r = requests.get(url, headers=headers, data=data)
# fields = ['full_name']
# if len(self.ghf.fields) > 0:
# fields = self.ghf.fields
# if self.ghf.profile:
# self.profile_keys(r.json())
# else:
# self.print_object_array_with_fields(r.json(),fields)
def repos(self,org):
url='https://api.github.com/orgs/'+org+'/repos'
fields = ['full_name']
headers=self.get_auth_header()
self.make_github_get_request(fields,url,headers)
def make_github_get_request(self,default_fields,url,headers,data=''):
if self.ghf.debug: click.echo("url:"+url+" token:"+self.ghf.token+" fields:"+",".join(default_fields))
r = requests.get(url, headers=headers, data=data)
fields = default_fields
if len(self.ghf.fields) > 0:
fields = self.ghf.fields
table = []
if self.ghf.profile:
(fields,table) = self.get_profile_table(r.json())
else:
table = self.get_table_from_object_array_with_fields(fields,r.json())
self.print_table(fields,table)
def print_table(self, field_names, table):
if self.ghf.export_csv:
separator = self.ghf.csv_separator
if self.ghf.print_header_row:
click.echo(separator.join(field_names))
for entry in table:
click.echo(separator.join(entry))
else:
if self.ghf.print_header_row:
click.echo(tabulate(table, field_names, tablefmt="simple"))
else:
click.echo(tabulate(table, tablefmt="simple"))
def test_post(self,url,headers,data):
r = requests.post(url, headers=headers, data=data)
if r.status_code == 201:
click.echo(r.json())
elif r.status_code == 401:
click.echo("Error:"+r.json()['message'])
else:
click.echo('status:'+str(r.status_code))
click.echo(r.text)
def test_get(self,url,headers,data):
r = requests.get(url, headers=headers, data=data)
if r.status_code == 201:
click.echo(r.json())
elif r.status_code == 401:
click.echo("Error:"+r.json()['message'])
else:
click.echo('status:'+str(r.status_code))
click.echo(r.text)
def get_auth_header(self):
headers={'Authorization' : 'token '+self.ghf.token}
return headers
def get_profile_table(self,json):
outter_key_hash = {}
inner_key_hash = {}
if type(json) == type([]):
for item in json:
if type(item) == type(u''):
outter_key_hash[item] = 1 if item not in outter_key_hash else outter_key_hash[item] + 1
if type(item) == type({}):
for inner_item in item:
if type(inner_item) == type(u''):
inner_key_hash[inner_item] = 1 if inner_item not in inner_key_hash else inner_key_hash[inner_item] + 1
# elif type(json) == type({}):
# None
table = []
for key, value in outter_key_hash.items():
table.append(['level1',key,str(value)])
for key, value in inner_key_hash.items():
table.append(['level2',key,str(value)])
field_names = ['level', 'name','count']
table = sorted(table, key=lambda key: key[1])
return (field_names,table)
# click.echo(tabulate(table, field_names, tablefmt="simple"))
def get_table_from_object_array_with_fields(self,fields,json):
table = []
if type(json) == type([]):
for item in json:
if type(item) == type({}):
row = []
for field in fields:
if field in item:
row.append(item[field])
else:
row.append('')
table.append(row)
headers = fields
return table
# click.echo(tabulate(table, headers, tablefmt="simple"))
# >>> js = ['name1', 'name2', {'iname1':11,'iname2':12}]
# >>> for item in js:
# ... print type(item)
# ...
# <type 'str'>
# <type 'str'>
# <type 'dict'>