-
Notifications
You must be signed in to change notification settings - Fork 82
/
vzclient
executable file
·104 lines (98 loc) · 3.83 KB
/
vzclient
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @copyright Copyright (c) 2011, The volkszaehler.org project
# @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
# Author Sven Anders <volkszaehler2011@sven.anders.im> 2011
#
# This file is part of volkzaehler.org
#
# volkzaehler.org is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# volkzaehler.org is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
#
import argparse
import ConfigParser
import urllib
import urllib2
import os
import json
Config = ConfigParser.ConfigParser()
version="0.9";
parser = argparse.ArgumentParser(description='A python client for volkszaehler.org',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="Syntax of /etc/vzclient.conf or ~/.vzclient.conf: \n"+
"------------------------------------------------------------\n"+
"[default]\n"+
" url:http://www.example.org/subdir/volkszaehler/middleware.php\n"+
" format:json\n");
parser.add_argument("operation",choices=["add","get","edit","delete"],help="what to do");
parser.add_argument('context',choices=["data","channel","group","capabilities"],help="the context of the operation");
parser.add_argument('--url',action='store',help="url to the volkszaehler.org middleware.php (tip: put this in config file, see below)");
parser.add_argument('-u', '--uuid',action='append',help="the uuid to edit, get or delete");
parser.add_argument('-f', '--format',choices=["json","xml","csv","png","gif","jpg"],action='store',help="the format");
parser.add_argument('-e', '--eval',action='store',help="get comma-separated attribute from json response (Example: entity,uuid will extract the uuid from a json string: {\"entity\":{\"uuid\":\"4...\"}})");
parser.add_argument('-j', '--json',action='store',help="send json request");
parser.add_argument('param',nargs='*',help="Paramter always in syntax key=value");
args = parser.parse_args();
Config.read(["/etc/vzclient.conf",os.path.expanduser('~/.vzclient.conf'),".vzclient.conf"]);
url=args.url;
if (url is None):
try:
url=Config.get("default", "url");
except:
print "ERROR: Please specify an url!";
exit(1);
format=args.format;
if (format is None):
try:
format=Config.get("default","format");
except:
format="json";
context=args.context;
url=url+"/"+context;
uuid=args.uuid;
if (uuid is not None) and len(uuid) == 1:
url=url+"/"+uuid[0];
url=url+"."+format+"?operation="+args.operation
if (uuid is not None) and len(uuid) > 1:
url=url+"&uuid[]="+"&uuid[]=".join(uuid);
for p in args.param:
(key,value)=p.split("=");
url=url+"&"+urllib2.quote(key)+"="+urllib2.quote(value);
# print url;
req = urllib2.Request(url)
req.add_header('User-agent', 'vzclient/$version')
try:
if (args.json):
req.add_header('Content-Type', 'application/json');
f=urllib2.urlopen(url, args.json);
else:
f=urllib2.urlopen(url);
jsonstr=f.read();
if (args.eval is None):
print jsonstr;
else:
obj=json.loads(jsonstr);
for ev in args.eval.split(",") :
if (isinstance(obj,(list,tuple))):
try:
ev=int(ev) # array access
except: pass
try:
obj=obj[ev]
except KeyError:
print "Key: "+ev+" not found in str: "+jsonstr;
print obj;
exit(0);
except urllib2.HTTPError,error:
print error.read();
exit(1);