changeset 670:687633456631

MoinMoin.script: move _util stuff to __init__
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 20 May 2006 23:40:21 +0200
parents e3b84c7488e8
children 68d93872d4c9
files MoinMoin/script/__init__.py MoinMoin/script/_util.py MoinMoin/script/account/check.py MoinMoin/script/account/create.py MoinMoin/script/account/disable.py MoinMoin/script/cli/show.py MoinMoin/script/export/dump.py MoinMoin/script/import/irclog.py MoinMoin/script/lupy/build.py MoinMoin/script/maint/cleancache.py MoinMoin/script/maint/cleanpage.py MoinMoin/script/maint/globaledit.py MoinMoin/script/maint/mkpagepacks.py MoinMoin/script/maint/reducewiki.py MoinMoin/script/migration/data.py MoinMoin/script/moin.py
diffstat 16 files changed, 160 insertions(+), 169 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/__init__.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/__init__.py	Sat May 20 23:40:21 2006 +0200
@@ -2,7 +2,8 @@
 """
     MoinMoin - Extension Script Package
 
-    @copyright: 2006 by Thomas Waldmann
+    @copyright: 2000-2002 by Jürgen Hermann <jh@web.de>,
+                2006 by MoinMoin:ThomasWaldmann
     @license: GNU GPL, see COPYING for details.
 """
 
@@ -12,3 +13,137 @@
 extension_scripts = pysupport.getPackageModules(__file__)
 modules = extension_scripts
 
+import os, sys, time
+
+flag_quiet = 0
+script_module = '__main__'
+
+#############################################################################
+### Logging
+#############################################################################
+
+def fatal(msgtext, **kw):
+    """ Print error msg to stderr and exit. """
+    sys.stderr.write("FATAL ERROR: " + msgtext + "\n")
+    if kw.get('usage', 0):
+        maindict = vars(sys.modules[script_module])
+        if maindict.has_key('usage'):
+            maindict['usage']()
+    sys.exit(1)
+
+
+def log(msgtext):
+    """ Optionally print error msg to stderr. """
+    if not flag_quiet:
+        sys.stderr.write(msgtext + "\n")
+
+
+#############################################################################
+### Commandline Support
+#############################################################################
+
+class Script:
+    def __init__(self, script, usage, argv=None, def_values=None):
+        #print "argv:", argv, "def_values:", repr(def_values)
+        if argv is None:
+            self.argv = sys.argv[1:]
+        else:
+            self.argv = argv
+        self.def_values = def_values
+        self.script_module = sys.modules[script]
+
+        global _start_time
+        _start_time = time.clock()
+
+        import optparse
+        from MoinMoin import version
+
+        cmd = self.script_module.__name__.split('.')[-1].replace('_', '-')
+        rev = "%s %s [%s]" % (version.project, version.release, version.revision)
+        sys.argv[0] = cmd
+
+        self.parser = optparse.OptionParser(
+            usage="%(cmd)s %(usage)s\n\n" % {'cmd': cmd, 'usage': usage, },
+            version=rev)
+        self.parser.allow_interspersed_args = False
+        if def_values:
+            self.parser.set_defaults(**def_values.__dict__)
+        self.parser.add_option(
+            "-q", "--quiet", 
+            action="store_true", dest="quiet",
+            help="Be quiet (no informational messages)"
+        )
+        self.parser.add_option(
+            "--show-timing", 
+            action="store_true", dest="show_timing", default=False,
+            help="Show timing values [default: %default]"
+        )
+
+    def run(self, showtime=1):
+        """ Run the main function of a command. """
+        global flag_quiet
+        try:
+            try:
+                self.options, self.args = self.parser.parse_args(self.argv)
+                flag_quiet = self.options.quiet
+                self.mainloop()
+            except KeyboardInterrupt:
+                log("*** Interrupted by user!")
+            except SystemExit:
+                showtime = 0
+                raise
+        finally:
+            if showtime:
+                self.logRuntime()
+
+    def logRuntime(self):
+        """ Print the total command run time. """
+        if self.options.show_timing:
+            log("Needed %.3f secs." % (time.clock() - _start_time,))
+
+
+class MoinScript(Script):
+    """ Moin main script class """
+
+    def __init__(self, argv=None, def_values=None):
+        Script.__init__(self, __name__, "[options]", argv, def_values)
+        # those are options potentially useful for all sub-commands:
+        self.parser.add_option(
+            "--config-dir", metavar="DIR", dest="config_dir",
+            help=("Path to the directory containing the wiki "
+                  "configuration files. [default: current directory]")
+        )
+        self.parser.add_option(
+            "--wiki-url", metavar="WIKIURL", dest="wiki_url",
+            help="URL of a single wiki to migrate e.g. localhost/mywiki/ [default: CLI]"
+        )
+        self.parser.add_option(
+            "--page", dest="page", default='',
+            help="wiki page name [default: %default]"
+        )
+    
+    def init_request(self):
+        """ create request """
+        from MoinMoin.request import CLI
+        if self.options.wiki_url:
+            self.request = CLI.Request(self.options.wiki_url, self.options.page)
+        else:
+            self.request = CLI.Request(pagename=self.options.page)
+        
+    def mainloop(self):
+        # Insert config dir or the current directory to the start of the path.
+        config_dir = self.options.config_dir
+        if config_dir and not os.path.isdir(config_dir):
+            fatal("bad path given to --config-dir option")
+        sys.path.insert(0, os.path.abspath(config_dir or os.curdir))
+
+        args = self.args
+        if len(args) < 2:
+            self.parser.error("you must specify a command module and name.")
+            sys.exit(1)
+
+        cmd_module, cmd_name = args[:2]
+        from MoinMoin import wikiutil
+        plugin_class = wikiutil.importBuiltinPlugin('script.%s' % cmd_module, cmd_name, 'PluginScript')
+        plugin_class(args[2:], self.options).run() # all starts again there
+
--- a/MoinMoin/script/_util.py	Thu May 18 20:27:25 2006 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,142 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-    MoinMoin - Command line utilities
-
-    @copyright: 2000, 2001, 2002 by Jürgen Hermann <jh@web.de>
-    @license: GNU GPL, see COPYING for details.
-"""
-
-import os, sys, time
-
-flag_quiet = 0
-script_module = '__main__'
-
-#############################################################################
-### Logging
-#############################################################################
-
-def fatal(msgtext, **kw):
-    """ Print error msg to stderr and exit. """
-    sys.stderr.write("FATAL ERROR: " + msgtext + "\n")
-    if kw.get('usage', 0):
-        maindict = vars(sys.modules[script_module])
-        if maindict.has_key('usage'):
-            maindict['usage']()
-    sys.exit(1)
-
-
-def log(msgtext):
-    """ Optionally print error msg to stderr. """
-    if not flag_quiet:
-        sys.stderr.write(msgtext + "\n")
-
-
-#############################################################################
-### Commandline Support
-#############################################################################
-
-class Script:
-    def __init__(self, script, usage, argv=None, def_values=None):
-        #print "argv:", argv, "def_values:", repr(def_values)
-        if argv is None:
-            self.argv = sys.argv[1:]
-        else:
-            self.argv = argv
-        self.def_values = def_values
-        self.script_module = sys.modules[script]
-
-        global _start_time
-        _start_time = time.clock()
-
-        import optparse
-        from MoinMoin import version
-
-        cmd = self.script_module.__name__.split('.')[-1].replace('_', '-')
-        rev = "%s %s [%s]" % (version.project, version.release, version.revision)
-        sys.argv[0] = cmd
-
-        self.parser = optparse.OptionParser(
-            usage="%(cmd)s %(usage)s\n\n" % {'cmd': cmd, 'usage': usage, },
-            version=rev)
-        self.parser.allow_interspersed_args = False
-        if def_values:
-            self.parser.set_defaults(**def_values.__dict__)
-        self.parser.add_option(
-            "-q", "--quiet", 
-            action="store_true", dest="quiet",
-            help="Be quiet (no informational messages)"
-        )
-        self.parser.add_option(
-            "--show-timing", 
-            action="store_true", dest="show_timing", default=False,
-            help="Show timing values [default: %default]"
-        )
-
-    def run(self, showtime=1):
-        """ Run the main function of a command. """
-        global flag_quiet
-        try:
-            try:
-                self.options, self.args = self.parser.parse_args(self.argv)
-                flag_quiet = self.options.quiet
-                self.mainloop()
-            except KeyboardInterrupt:
-                log("*** Interrupted by user!")
-            except SystemExit:
-                showtime = 0
-                raise
-        finally:
-            if showtime:
-                self.logRuntime()
-
-    def logRuntime(self):
-        """ Print the total command run time. """
-        if self.options.show_timing:
-            log("Needed %.3f secs." % (time.clock() - _start_time,))
-
-
-class MoinScript(Script):
-    """ Moin main script class """
-
-    def __init__(self, argv=None, def_values=None):
-        Script.__init__(self, __name__, "[options]", argv, def_values)
-        # those are options potentially useful for all sub-commands:
-        self.parser.add_option(
-            "--config-dir", metavar="DIR", dest="config_dir",
-            help=("Path to the directory containing the wiki "
-                  "configuration files. [default: current directory]")
-        )
-        self.parser.add_option(
-            "--wiki-url", metavar="WIKIURL", dest="wiki_url",
-            help="URL of a single wiki to migrate e.g. localhost/mywiki/ [default: CLI]"
-        )
-        self.parser.add_option(
-            "--page", dest="page", default='',
-            help="wiki page name [default: %default]"
-        )
-    
-    def init_request(self):
-        """ create request """
-        from MoinMoin.request import CLI
-        if self.options.wiki_url:
-            self.request = CLI.Request(self.options.wiki_url, self.options.page)
-        else:
-            self.request = CLI.Request(pagename=self.options.page)
-        
-    def mainloop(self):
-        # Insert config dir or the current directory to the start of the path.
-        config_dir = self.options.config_dir
-        if config_dir and not os.path.isdir(config_dir):
-            fatal("bad path given to --config-dir option")
-        sys.path.insert(0, os.path.abspath(config_dir or os.curdir))
-
-        args = self.args
-        if len(args) < 2:
-            self.parser.error("you must specify a command module and name.")
-            sys.exit(1)
-
-        cmd_module, cmd_name = args[:2]
-        from MoinMoin import wikiutil
-        plugin_class = wikiutil.importBuiltinPlugin('script.%s' % cmd_module, cmd_name, 'PluginScript')
-        plugin_class(args[2:], self.options).run() # all starts again there
-
--- a/MoinMoin/script/account/check.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/account/check.py	Sat May 20 23:40:21 2006 +0200
@@ -66,7 +66,7 @@
 
 import os
 
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 from MoinMoin import user, wikiutil
 
 class PluginScript(MoinScript):
--- a/MoinMoin/script/account/create.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/account/create.py	Sat May 20 23:40:21 2006 +0200
@@ -6,7 +6,7 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 class PluginScript(MoinScript):
     def __init__(self, argv, def_values):
--- a/MoinMoin/script/account/disable.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/account/disable.py	Sat May 20 23:40:21 2006 +0200
@@ -6,7 +6,7 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 class PluginScript(MoinScript):
     def __init__(self, argv, def_values):
--- a/MoinMoin/script/cli/show.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/cli/show.py	Sat May 20 23:40:21 2006 +0200
@@ -8,7 +8,7 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 class PluginScript(MoinScript):
     """ show page script class """
--- a/MoinMoin/script/export/dump.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/export/dump.py	Sat May 20 23:40:21 2006 +0200
@@ -14,8 +14,7 @@
 import sys, os, time, StringIO, codecs, shutil, re, errno
 
 from MoinMoin import config, wikiutil, Page
-from MoinMoin.script import _util
-from MoinMoin.script._util import MoinScript
+from MoinMoin import script
 from MoinMoin.action import AttachFile
 
 url_prefix = "."
@@ -65,22 +64,22 @@
             try:
                 os.makedirs(dest_dir)
             except:
-                _util.fatal("Cannot create attachment directory '%s'" % dest_dir)
+                script.fatal("Cannot create attachment directory '%s'" % dest_dir)
         elif not os.path.isdir(dest_dir):
-            _util.fatal("'%s' is not a directory" % dest_dir)
+            script.fatal("'%s' is not a directory" % dest_dir)
 
         shutil.copyfile(source_file, dest_file)
-        _util.log('Writing "%s"...' % dest_url)
+        script.log('Writing "%s"...' % dest_url)
         return dest_url
     else:
         return ""
   
 
-class PluginScript(MoinScript):
+class PluginScript(script.MoinScript):
     """ Dump script class """
     
     def __init__(self, argv=None, def_values=None):
-        MoinScript.__init__(self, argv, def_values)
+        script.MoinScript.__init__(self, argv, def_values)
         self.parser.add_option(
             "-t", "--target-dir", dest="target_dir",
             help="Write html dump to DIRECTORY"
@@ -93,17 +92,17 @@
         outputdir = os.path.abspath(self.options.target_dir)
         try:
             os.mkdir(outputdir)
-            _util.log("Created output directory '%s'!" % outputdir)
+            script.log("Created output directory '%s'!" % outputdir)
         except OSError, err:
             if err.errno != errno.EEXIST:
-                _util.fatal("Cannot create output directory '%s'!" % outputdir)
+                script.fatal("Cannot create output directory '%s'!" % outputdir)
 
         # Insert config dir or the current directory to the start of the path.
         config_dir = self.options.config_dir
         if config_dir and os.path.isfile(config_dir):
             config_dir = os.path.dirname(config_dir)
         if config_dir and not os.path.isdir(config_dir):
-            _util.fatal("bad path given to --config-dir option")
+            script.fatal("bad path given to --config-dir option")
         sys.path.insert(0, os.path.abspath(config_dir or os.curdir))
 
         self.init_request()
@@ -138,7 +137,7 @@
         for pagename in pages:
             # we have the same name in URL and FS
             file = wikiutil.quoteWikinameURL(pagename) 
-            _util.log('Writing "%s"...' % file)
+            script.log('Writing "%s"...' % file)
             try:
                 pagehtml = ''
                 page = Page.Page(request, pagename)
--- a/MoinMoin/script/import/irclog.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/import/irclog.py	Sat May 20 23:40:21 2006 +0200
@@ -24,7 +24,7 @@
 import os
 
 from MoinMoin.PageEditor import PageEditor
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 def decodeLinewise(text):
     resultList = []
--- a/MoinMoin/script/lupy/build.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/lupy/build.py	Sat May 20 23:40:21 2006 +0200
@@ -11,8 +11,7 @@
 
 import os
 
-from MoinMoin.script import _util
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 from MoinMoin.request import RequestCLI
 from MoinMoin.lupy import Index
 
--- a/MoinMoin/script/maint/cleancache.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/maint/cleancache.py	Sat May 20 23:40:21 2006 +0200
@@ -20,7 +20,7 @@
 
 import os
 
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 class PluginScript(MoinScript):
     def __init__(self, argv, def_values):
--- a/MoinMoin/script/maint/cleanpage.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/maint/cleanpage.py	Sat May 20 23:40:21 2006 +0200
@@ -10,7 +10,7 @@
 
 import os
 
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 class PluginScript(MoinScript):
     def __init__(self, argv, def_values):
--- a/MoinMoin/script/maint/globaledit.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/maint/globaledit.py	Sat May 20 23:40:21 2006 +0200
@@ -12,7 +12,7 @@
 debug = False
 
 from MoinMoin import PageEditor
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 class PluginScript(MoinScript):
     def __init__(self, argv, def_values):
--- a/MoinMoin/script/maint/mkpagepacks.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/maint/mkpagepacks.py	Sat May 20 23:40:21 2006 +0200
@@ -16,7 +16,7 @@
 from MoinMoin.Page import Page
 from MoinMoin.PageEditor import PageEditor
 from MoinMoin.packages import packLine, unpackLine, MOIN_PACKAGE_FILE
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 EXTRA = u'extra'
 NODIST = u'nodist'
--- a/MoinMoin/script/maint/reducewiki.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/maint/reducewiki.py	Sat May 20 23:40:21 2006 +0200
@@ -30,7 +30,7 @@
 from MoinMoin.Page import Page
 from MoinMoin.action import AttachFile
 
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 class PluginScript(MoinScript):
     def __init__(self, argv, def_values):
--- a/MoinMoin/script/migration/data.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/migration/data.py	Sat May 20 23:40:21 2006 +0200
@@ -20,7 +20,7 @@
 import os
 
 from MoinMoin import wikiutil
-from MoinMoin.script._util import MoinScript
+from MoinMoin.script import MoinScript
 
 class PluginScript(MoinScript):
     """ Migration script class """
--- a/MoinMoin/script/moin.py	Thu May 18 20:27:25 2006 +0200
+++ b/MoinMoin/script/moin.py	Sat May 20 23:40:21 2006 +0200
@@ -11,8 +11,8 @@
 """
 
 def run():
-    from MoinMoin.script import _util
-    _util.MoinScript().run(showtime=0)
+    from MoinMoin.script import MoinScript
+    MoinScript().run(showtime=0)
     
 if __name__ == "__main__":
     # Insert the path to MoinMoin in the start of the path