3 # Purpose: XmlRpc interface client to paste.grml.org
4 # Author: Michael Gebetsroither <gebi@grml.org>
5 # License: This file is licensed under the GPL v2.
6 ################################################################################
9 from xmlrpc.client import ServerProxy
15 DEFAULT_SERVER = "http://paste.grml.org/server.pl"
18 class ActionFailedException(Exception):
19 """Thrown if server returned an error"""
21 def __init__(self, errormsg, ret):
22 Exception.__init__(self, errormsg, ret)
25 """Get errormessage"""
29 """Get more verbose errormessage"""
34 """Get paste proxy object"""
35 return (ServerProxy(opts.server, verbose=False).paste)
38 def _check_success(return_data):
39 """Check if call was successful, raise AcitonFailedException otherwise"""
40 if return_data["rc"] != 0:
41 raise ActionFailedException(return_data["statusmessage"], return_data)
45 def action_add_paste(opts, code):
46 """Add paste to the server: <1.line> <2.line> ...
48 default Read paste from stdin.
49 [text] Every argument on the commandline will be interpreted as
50 a seperate line of paste.
53 code = [line.rstrip() for line in sys.stdin.readlines()]
54 code = "\n".join(code)
55 result = _check_success(_paste(opts).addPaste(code, opts.name, opts.expire * 3600, opts.lang, opts.private))
56 return result["statusmessage"], result
59 def action_del_paste(opts, args):
60 """Delete paste from server: <digest>
62 <digest> Digest of paste you want to remove.
65 result = _check_success(_paste(opts).deletePaste(digest))
66 return result["statusmessage"], result
69 def action_get_paste(opts, args):
70 """Get paste from server: <id>
72 <id> Id of paste you want to receive.
74 paste_id = args.pop(0)
75 result = _check_success(_paste(opts).getPaste(paste_id))
76 return result["code"], result
79 def action_get_langs(opts, args):
80 """Get supported language highlighting types from server"""
81 result = _check_success(_paste(opts).getLanguages())
82 return "\n".join(result["langs"]), result
85 def action_add_short_url(opts, args):
86 """Add short-URL: <url>
88 <url> Short-URL to add
91 result = _check_success(_paste(opts).addShortURL(url))
92 return result["url"], result
95 def action_get_short_url(opts, args):
96 """Resolve short-URL: <url>
98 <url> Short-URL to get clicks of
101 result = _check_success(_paste(opts).resolveShortURL(url))
102 return result["url"], result
105 def action_get_short_url_clicks(opts, args):
106 """Get clicks of short-URL: <url>
108 <url> Short-URL to get clicks of
111 result = _check_success(_paste(opts).ShortURLClicks(url))
112 return result["count"], result
115 def action_help(opts, args):
116 """Print more verbose help about specific action: <action>
118 <action> Topic on which you need more verbose help.
126 function_name = actions[alias]
127 print(inspect.getdoc(globals()[function_name]))
128 print("\naliases: " + " ".join([i for i in actions_r[function_name] if i != alias]))
130 print("Error: No such command - %s" % (alias))
131 OPT_PARSER.print_usage()
135 # action_add_paste -> [add, a]
138 # add -> action_add_paste
139 # a -> action_add_paste
149 if __name__ == "__main__":
151 "action_add_paste add a",
152 "action_del_paste del d rm",
153 "action_get_paste get g",
154 "action_get_langs getlangs gl langs l",
155 "action_add_short_url addurl",
156 "action_get_short_url geturl",
157 "action_get_short_url_clicks getclicks",
160 for action_spec in action_specs:
161 aliases = action_spec.split()
163 actions_r[cmd] = aliases
164 for (action_name, v) in actions_r.items():
166 actions[i] = action_name
168 usage = "usage: %prog [options] ACTION <args>\n\n" +\
170 "\n".join(["%12s\t%s" % (v[0], inspect.getdoc(globals()[action_name]).splitlines()[0])
171 for (action_name, v) in actions_r.items()])
172 running_user = getpass.getuser()
173 parser = optparse.OptionParser(usage=usage)
174 parser.add_option("-n", "--name", default=running_user, help="Name of poster")
175 parser.add_option("-e", "--expire", type=int, default=72, metavar="HOURS",
176 help="Time at wich paste should expire")
177 parser.add_option("-l", "--lang", default="Plain", help="Type of language to highlight")
178 parser.add_option("-p", "--private", action="count", dest="private", default=0,
179 help="Create hidden paste"),
180 parser.add_option("-s", "--server", default=DEFAULT_SERVER,
182 parser.add_option("-v", "--verbose", action="count", default=0, help="More output")
183 (opts, args) = parser.parse_args()
187 parser.error("Please provide me with an action")
188 elif args[0] in actions:
190 action = actions[cmd]
192 (msg, ret) = globals()[action](opts, args)
193 if opts.verbose == 0:
197 except ActionFailedException as except_inst:
198 sys.stderr.write("Server Error: %s\n" % except_inst.what())
200 print(except_inst.dwhat())
203 parser.error("Unknown action: %s" % args[0])