forked from CMSCompOps/WmAgentScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrelloClient.py
executable file
·81 lines (70 loc) · 2.59 KB
/
TrelloClient.py
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
import os
import json
class TrelloClient:
def __init__(self):
for line in open('Unified/secret_trello.txt'):
line = line.replace('\n','')
try:
k,v = line.split(':')
###print k,v
setattr(self,k,v)
except:
print line,"does not follow convention key:value"
## created by hand
self.lists = {
'draining' : '58da314ad8064a3772a8b2b7',
'running' : '58da313230415813a0f3c31c',
'standby' : '58da314194946756ba09b66e',
'drained' : '58da315628847a392b663927',
'offline' : '58da315628847a392b663927'
}
## a map of agents
self.getAgents()
#print json.dumps( self.agents , indent=2)
def getAgents(self):
self.agents = {}
for c in self.getBoard():
n = c.get('name',None)
fn = n.split(' - ')[0].strip()
dom = '.cern.ch' if 'vocms' in fn else '.fnal.gov'
fn+=dom
#print n,fn,c.get('id')
self.agents[fn] = c.get('id')
def _auth(self):
return 'key=%s&token=%s'%(self.key, self.token)
def _put(self, o, oid, pars):
url = 'https://api.trello.com/1/%s/%s?%s&%s'%( o, oid, '&'.join(['%s=%s'%(k,v) for (k,v) in pars.items()]), self._auth())
print url
r = os.popen('curl -s --request PUT --url "%s"'% url)
#print r
d = {}
try:
d = json.loads(r.read())
except:
print "Failed to put %s to %s %s"%( json.dumps( pars),
o, oid)
return d
def _get(self, o, oid,supp='?'):
url = 'https://api.trello.com/1/%s/%s%s&%s'% ( o,oid,supp, self._auth())
#print url
r = os.popen('curl -s "%s"'%url)
d = {}
try:
d = json.loads(r.read())
except:
print "Failed to get %s %s with info %s"%( o, oid, supp)
return d
def changeList(self, cn, ln):
ln = self.lists.get(ln, ln)
new_list_id = self.getList(ln).get('id',None)
c = self.getCard(cn)
old_list_id =c.get('idList',None)
if new_list_id and old_list_id and old_list_id!=new_list_id:
self._put('cards',c.get('id'), {'idList' : new_list_id})
def getList(self, ln):
return self._get('lists',ln,)
def getBoard(self, bn = '4np6TByB'):
return self._get('boards', bn, '/cards?fields=name,url')
def getCard(self, cn):
cn = self.agents.get(cn,cn)
return self._get('cards', cn)