upgrade to upstream version pre1.0
authorMichael Gebetsroither <michael@mgeb.org>
Tue, 23 Nov 2010 10:38:39 +0000 (11:38 +0100)
committerMichael Gebetsroither <michael@mgeb.org>
Tue, 23 Nov 2010 10:38:39 +0000 (11:38 +0100)
grml-paste

index cba6108..fa42dbd 100755 (executable)
@@ -8,9 +8,11 @@
 import sys
 import xmlrpclib
 import optparse
 import sys
 import xmlrpclib
 import optparse
-from pprint import pprint
 import inspect
 import inspect
+import getpass
 
 
+# program defaults
+DEFAULT_SERVER='http://paste.grml.org/server.pl'
 
 class ActionFailedException(Exception):
     '''Thrown if server returned an error'''
 
 class ActionFailedException(Exception):
     '''Thrown if server returned an error'''
@@ -59,7 +61,7 @@ class Action(object):
         if len(self.args_) == 0:
             code = [ i.strip() for i in sys.stdin.readlines() ]
         code = '\n'.join(code)
         if len(self.args_) == 0:
             code = [ i.strip() for i in sys.stdin.readlines() ]
         code = '\n'.join(code)
-        result = self._callProxy(lambda s: s.paste.addPaste(code, o.name, o.expire * 3600, o.lang),
+        result = self._callProxy(lambda s: s.paste.addPaste(code, o.name, o.expire * 3600, o.lang, o.private),
                             server)
         return (result['statusmessage'], result)
 
                             server)
         return (result['statusmessage'], result)
 
@@ -69,7 +71,6 @@ class Action(object):
         <digest>    Digest of paste you want to remove.
         '''
         digest = self.args_.pop(0)
         <digest>    Digest of paste you want to remove.
         '''
         digest = self.args_.pop(0)
-
         result = self._callProxy(lambda s: s.paste.deletePaste(digest))
         return (result['statusmessage'], result)
 
         result = self._callProxy(lambda s: s.paste.deletePaste(digest))
         return (result['statusmessage'], result)
 
@@ -79,7 +80,6 @@ class Action(object):
         <id>        Id of paste you want to receive.
         '''
         id = self.args_.pop(0)
         <id>        Id of paste you want to receive.
         '''
         id = self.args_.pop(0)
-
         result = self._callProxy(lambda s: s.paste.getPaste(id))
         return (result['code'], result)
 
         result = self._callProxy(lambda s: s.paste.getPaste(id))
         return (result['code'], result)
 
@@ -88,16 +88,50 @@ class Action(object):
         result = self._callProxy(lambda s: s.paste.getLanguages())
         return ('\n'.join(result['langs']), result)
 
         result = self._callProxy(lambda s: s.paste.getLanguages())
         return ('\n'.join(result['langs']), result)
 
+    def actionAddShortUrl(self):
+        '''Add short-URL: <url>
+
+        <url>        Short-URL to add
+        '''
+        url = self.args_.pop(0)
+        result = self._callProxy(lambda s: s.paste.addShortURL(url))
+        return (result['url'], result)
+
+    def actionGetShortUrl(self):
+        '''Resolve short-URL: <url>
+
+        <url>        Short-URL to get clicks of
+        '''
+        url = self.args_.pop(0)
+        result = self._callProxy(lambda s: s.paste.resolveShortURL(url))
+        return (result['url'], result)
+
+    def actionGetShortUrlClicks(self):
+        '''Get clicks of short-URL: <url>
+
+        <url>        Short-URL to get clicks of
+        '''
+        url = self.args_.pop(0)
+        result = self._callProxy(lambda s: s.paste.ShortURLClicks(url))
+        return (result['count'], result)
+
     def actionHelp(self):
         '''Print more verbose help about specific action: <action>
 
         <action>    Topic on which you need more verbose help.
         '''
     def actionHelp(self):
         '''Print more verbose help about specific action: <action>
 
         <action>    Topic on which you need more verbose help.
         '''
-        alias = self.args_.pop(0)
-
-        fun = actions[alias]
-        print inspect.getdoc(self.__getattribute__(fun))
-        print "\naliase: " + " ".join([i for i in actions_r[fun] if i != alias])
+        if len(self.args_) < 1:
+            alias = "help"
+        else:
+            alias = self.args_.pop(0)
+
+        if alias in actions:
+            fun = actions[alias]
+            print inspect.getdoc(self.__getattribute__(fun))
+            print "\naliase: " + " ".join([i for i in actions_r[fun] if i != alias])
+        else:
+            print "Error: No such command - %s" % (alias)
+            OPT_PARSER.print_usage()
         sys.exit(0)
 
 
         sys.exit(0)
 
 
@@ -108,6 +142,10 @@ actions_r = {}
 # a   -> actionAddPaste
 actions   = {}
 
 # a   -> actionAddPaste
 actions   = {}
 
+# option parser
+OPT_PARSER = None
+
+
 ##
 # MAIN
 ##
 ##
 # MAIN
 ##
@@ -116,11 +154,14 @@ if __name__ == "__main__":
                    'actionDelPaste del d rm',
                    'actionGetPaste get g',
                    'actionGetLangs getlangs gl langs l',
                    'actionDelPaste del d rm',
                    'actionGetPaste get g',
                    'actionGetLangs getlangs gl langs l',
+                   'actionAddShortUrl addurl',
+                   'actionGetShortUrl geturl',
+                   'actionGetShortUrlClicks getclicks',
                    'actionHelp     help']
     for i in action_spec:
                    'actionHelp     help']
     for i in action_spec:
-        tmp = i.split()
-        cmd = tmp.pop(0)
-        actions_r[cmd] = tmp
+        aliases = i.split()
+        cmd = aliases.pop(0)
+        actions_r[cmd] = aliases
     for (k,v) in actions_r.items():
         for i in v:
             actions[i] = k
     for (k,v) in actions_r.items():
         for i in v:
             actions[i] = k
@@ -129,15 +170,19 @@ if __name__ == "__main__":
             "actions:\n" +\
             "\n".join(["%12s\t%s" % (v[0], inspect.getdoc(getattr(Action, k)).split('\n')[0]) \
                 for (k,v) in actions_r.items()])
             "actions:\n" +\
             "\n".join(["%12s\t%s" % (v[0], inspect.getdoc(getattr(Action, k)).split('\n')[0]) \
                 for (k,v) in actions_r.items()])
+    running_user = getpass.getuser()
     parser = optparse.OptionParser(usage=usage)
     parser = optparse.OptionParser(usage=usage)
-    parser.add_option('-n', '--name', default='anonymous', help="Name of poster")
+    parser.add_option('-n', '--name', default=running_user, help="Name of poster")
     parser.add_option('-e', '--expire', type=int, default=72, metavar='HOURS',
             help='Time at wich paste should expire')
     parser.add_option('-l', '--lang', default='Plain', help='Type of language to highlight')
     parser.add_option('-e', '--expire', type=int, default=72, metavar='HOURS',
             help='Time at wich paste should expire')
     parser.add_option('-l', '--lang', default='Plain', help='Type of language to highlight')
-    parser.add_option('-s', '--server', default='http://paste.grml.org/server.pl',
+    parser.add_option("-p", "--private", action="count", dest="private", default=0,
+                        help='Create hidden paste'),
+    parser.add_option('-s', '--server', default=DEFAULT_SERVER,
             help='Paste server')
     parser.add_option('-v', '--verbose', action='count', default=0, help='More output')
     (opts, args) = parser.parse_args()
             help='Paste server')
     parser.add_option('-v', '--verbose', action='count', default=0, help='More output')
     (opts, args) = parser.parse_args()
+    OPT_PARSER = parser
 
     if len(args) == 0:
         parser.error('Please provide me with an action')
 
     if len(args) == 0:
         parser.error('Please provide me with an action')
@@ -149,11 +194,11 @@ if __name__ == "__main__":
             if opts.verbose == 0:
                 print msg
             else:
             if opts.verbose == 0:
                 print msg
             else:
-                pprint(ret)
+                print ret
         except ActionFailedException, e:
             sys.stderr.write('Server Error: %s\n' % e.what())
             if opts.verbose >0:
         except ActionFailedException, e:
             sys.stderr.write('Server Error: %s\n' % e.what())
             if opts.verbose >0:
-                pprint(e.dwhat())
+                print e.dwhat()
             sys.exit(1)
     else:
         parser.error('Unknown action: %s' % args[0])
             sys.exit(1)
     else:
         parser.error('Unknown action: %s' % args[0])