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 ################################################################################
12 from xmlrpc.client import ServerProxy
15 DEFAULT_SERVER = "https://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 ActionFailedException 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(
56 _paste(opts).addPaste(
57 code, opts.name, opts.expire * 3600, opts.lang, opts.private
60 return result["statusmessage"], result
63 def action_del_paste(opts, args):
64 """Delete paste from server: <digest>
66 <digest> Digest of paste you want to remove.
69 result = _check_success(_paste(opts).deletePaste(digest))
70 return result["statusmessage"], result
73 def action_get_paste(opts, args):
74 """Get paste from server: <id>
76 <id> Id of paste you want to receive.
78 paste_id = args.pop(0)
79 result = _check_success(_paste(opts).getPaste(paste_id))
80 return result["code"], result
83 def action_get_langs(opts, args):
84 """Get supported language highlighting types from server"""
85 result = _check_success(_paste(opts).getLanguages())
86 return "\n".join(result["langs"]), result
89 def action_add_short_url(opts, args):
90 """Add short-URL: <url>
92 <url> Short-URL to add
95 result = _check_success(_paste(opts).addShortURL(url))
96 return result["url"], result
99 def action_get_short_url(opts, args):
100 """Resolve short-URL: <url>
102 <url> Short-URL to get clicks of
105 result = _check_success(_paste(opts).resolveShortURL(url))
106 return result["url"], result
109 def action_get_short_url_clicks(opts, args):
110 """Get clicks of short-URL: <url>
112 <url> Short-URL to get clicks of
115 result = _check_success(_paste(opts).ShortURLClicks(url))
116 return result["count"], result
119 def action_help(opts, args):
120 """Print more verbose help about specific action: <action>
122 <action> Topic on which you need more verbose help.
130 function_name = actions[alias]
131 print(inspect.getdoc(globals()[function_name]))
134 + " ".join([i for i in actions_r[function_name] if i != alias])
137 print("Error: No such command - %s" % (alias))
138 OPT_PARSER.print_usage()
142 # action_add_paste -> [add, a]
145 # add -> action_add_paste
146 # a -> action_add_paste
156 if __name__ == "__main__":
158 "action_add_paste add a",
159 "action_del_paste del d rm",
160 "action_get_paste get g",
161 "action_get_langs getlangs gl langs l",
162 "action_add_short_url addurl",
163 "action_get_short_url geturl",
164 "action_get_short_url_clicks getclicks",
167 for action_spec in action_specs:
168 aliases = action_spec.split()
170 actions_r[cmd] = aliases
171 for (action_name, v) in actions_r.items():
173 actions[i] = action_name
176 "usage: %prog [options] ACTION <args>\n\n"
181 % (v[0], inspect.getdoc(globals()[action_name]).splitlines()[0])
182 for (action_name, v) in actions_r.items()
186 running_user = getpass.getuser()
187 parser = optparse.OptionParser(usage=usage)
188 parser.add_option("-n", "--name", default=running_user, help="Name of poster")
195 help="Time at which paste should expire",
198 "-l", "--lang", default="Plain", help="Type of language to highlight"
206 help="Create hidden paste",
208 parser.add_option("-s", "--server", default=DEFAULT_SERVER, help="Paste server")
209 parser.add_option("-v", "--verbose", action="count", default=0, help="More output")
210 (opts, args) = parser.parse_args()
214 parser.error("Please provide me with an action")
215 elif args[0] in actions:
217 action = actions[cmd]
219 (msg, ret) = globals()[action](opts, args)
220 if opts.verbose == 0:
224 except ActionFailedException as except_inst:
225 sys.stderr.write("Server Error: %s\n" % except_inst.what())
227 print(except_inst.dwhat())
230 parser.error("Unknown action: %s" % args[0])