[KSS-checkins] r51979 - kukit/kss.base/trunk/src/kss/base

jvloothuis at codespeak.net jvloothuis at codespeak.net
Sat Mar 1 11:35:25 CET 2008


Author: jvloothuis
Date: Sat Mar  1 11:35:25 2008
New Revision: 51979

Added:
   kukit/kss.base/trunk/src/kss/base/utils.txt
Modified:
   kukit/kss.base/trunk/src/kss/base/tests.py
   kukit/kss.base/trunk/src/kss/base/utils.py
Log:

Readded the code from r51399 which got lost for some reason::

  Improved the command lined utility by making it dump all the
  required . Also made some of the unused options work. The script now
  has tests as well.


Modified: kukit/kss.base/trunk/src/kss/base/tests.py
==============================================================================
--- kukit/kss.base/trunk/src/kss/base/tests.py	(original)
+++ kukit/kss.base/trunk/src/kss/base/tests.py	Sat Mar  1 11:35:25 2008
@@ -7,7 +7,7 @@
             'selectors.txt',
             'registry.txt', 'plugin.txt',
             'commands.txt', 'corecommands.txt',
-            'javascript.txt',
+            'javascript.txt', 'utils.txt',
             package='kss.base',
             optionflags=doctest.ELLIPSIS|doctest.REPORT_ONLY_FIRST_FAILURE,
         ),

Modified: kukit/kss.base/trunk/src/kss/base/utils.py
==============================================================================
--- kukit/kss.base/trunk/src/kss/base/utils.py	(original)
+++ kukit/kss.base/trunk/src/kss/base/utils.py	Sat Mar  1 11:35:25 2008
@@ -1,160 +1,58 @@
+import os
+import sys
 import optparse
-import ConfigParser
-
-from paste.script.templates import Template
-
-from kss.base.javascript import packed
-from kss.base.plugin import load_plugins, activated_plugins
-
-
-class KSSConcatJs(object):
-    '''Prints out or saves a packed javascript of loaded plugins'''
-
-    configSection = "KSSConcatJs"
-
-    def __init__(self):
-        self.getOptions()
-        if self.options.configFile:
-            self.getConfig(self.options.configFile)
-        self.handleCreation()
-
-
-    def getOptions(self):
-        '''Gets the arguments passed to the script (override config)'''
-        parser = optparse.OptionParser()
-
-        parser.add_option('-m', '--message',
-            action='store',
-            dest='message',
-            help="message (comment) that will be added to the generated javascript")
-        parser.add_option('--list',
-            action='store_true',
-            dest='listPlugins',
-            help="lists activated plugins")
-        parser.add_option('--compression-level',
-            action='store',
-            dest='compressionLevel',
-            help="specifies the compression level (devel / stripped / safe / full / safe-devel / full-devel)")
-        parser.add_option('--plugin',
-            action='append',
-            dest='pluginsToHandle',
-            help="specifies one additional plugin to load; if you want to specify multiple plugins you can do it by calling --plugin multiple times")
-        parser.add_option('--no-display',
-            action='store_true',
-            dest='noDisplayJavascript',
-            help="avoids the display of resulting javascript in console")
-        parser.add_option('--output-file',
-            action='store',
-            dest='outputFile',
-            help="outputs the resulting javascript to the specified file")
-        parser.add_option('--include-extras',
-            action='store',
-            dest='includeExtras',
-            help="specifies if 3rd party javascripts have to be packed too")
-        parser.add_option('-v', '--verbose',
-            action='store_true',
-            dest='verboseMode',
-            help="verbose mode")
-        parser.add_option('-F',
-            action='store',
-            dest='configFile',
-            help="loads options from the specified config file")
-
-        options, args = parser.parse_args()
-        self.options = options
-
-
-    def getConfig(self, fileName):
-        '''Gets the values in config file only if not overrided by argument'''
-        config = ConfigParser.ConfigParser()
-        config.read(fileName)
-        listParameters = ['pluginsToHandle']
-        strParameters = ['compressionLevel', \
-                         'outputFile']
-        boolParameters = ['listPlugins', \
-                          'noDisplayJavascript', \
-                          'includeExtras', \
-                          'verboseMode']
-        for param in strParameters:
-            if not hasattr(self.options, param):
-                try:
-                    setattr(self.options, \
-                            param, \
-                            config.get(self.configSection, param))
-                except ConfigParser.NoOptionError:
-                    pass
-        for param in boolParameters:
-            if not hasattr(self.options, param):
-                try:
-                    setattr(self.options, \
-                            param, \
-                            config.getboolean(self.configSection, param))
-                except ConfigParser.NoOptionError:
-                    pass
-        for param in listParameters:
-            #this is a list so the condition doesn't work with hasattr
-            if not bool(self.options.pluginsToHandle):
-                try:
-                    paramList = config.get(self.configSection, param).split(' ')
-                    setattr(self.options, \
-                            param, \
-                            paramList)
-                except ConfigParser.NoOptionError:
-                    pass
-
-
-    def makeJS(self, compressionLevel=None, includeExtras=False):
-        '''Returns a compacted javascript generated from loaded javascripts'''
-        return packed(compressionLevel, include_extras=False)
-
-
-    def setComment(self, javascript, message):
-        '''Add a comment to the generated javascript'''
-        return "/* %s */\n\n%s" % (message, javascript)
-
-    def writeToFile(self, fileName, fileContent):
-        '''Writes the packed javascript to file'''
-        file = open(fileName, 'w')
-        file.write(fileContent)
-        file.close()
-
-
-    def listPlugins(self):
-        '''Displays activated plugins'''
-        if self.options.verboseMode:
-            print "Activated plugins :"
-            for id, plugin in activated_plugins():
-                print " * %s" % id
-        else:
-            for id, plugin in activated_plugins():
-                print id
-
-
-    def handleCreation(self):
-        '''Handles the JS creation regarding options and/or config'''
-        corePlugin = ('kss-core', )
-        if self.options.pluginsToHandle:
-            plugins = tuple(self.options.pluginsToHandle) + corePlugin
-        else:
-            plugins = corePlugin
-        self.loadedPlugins = load_plugins(*plugins)
-
-        if self.options.listPlugins:
-            self.listPlugins()
-
-        javascript = self.makeJS(self.options.compressionLevel, \
-                                 self.options.includeExtras)
-
-        if self.options.message:
-            javascript = self.setComment(javascript, self.options.message)
-
-        if self.options.outputFile:
-            self.writeToFile(self.options.outputFile, javascript)
-        elif not self.options.noDisplayJavascript:
-            print javascript
-
-
-class KSSPluginTemplate(Template):
-    _template_dir = 'templates/plugin'
-    summary = 'KSS plugin template'
-    egg_plugins = ['kss.base']
+ 
+from kss.base import javascript
+from kss.base.plugin import activate, active_plugins
+ 
+def ksspackage():
+    parser = optparse.OptionParser()
+    parser.add_option('--list',
+                      action='store_true',
+                      dest='list_plugins',
+                      help="lists available plugins")
+    parser.add_option('--compression',
+                      action='store',
+                      dest='compression',
+                      default='safe',
+                      help="specifies the compression level (devel / stripped / safe / full / safe-devel / full-devel)")
+    parser.add_option('--plugin',
+                      action='append',
+                      dest='plugins',
+                      default=[],
+                      help="specifies one additional plugin to load; if you want to specify multiple plugins you can do it by calling --plugin multiple times")
+
+
+    options, args = parser.parse_args()
+
+    def make_package():
+        output_dir = args[0]
+        if not os.path.exists(output_dir):
+            os.makedirs(output_dir)
+
+        activate(*options.plugins)
+
+        print "Creating `kss.js` script:",
+        f = open(os.path.join(output_dir, 'kss.js'), 'w')
+        f.write(javascript.packed(options.compression))
+        f.close()
+        print "DONE"
+    
+        for name, script in javascript.extra_scripts().items():
+            print "Creating `%s` script:" % name,
+            f = open(os.path.join(output_dir, name), 'w')
+            f.write(script)
+            f.close()
+            print "DONE"
+
+    if args:
+        make_package()
+
+    elif options.list_plugins:
+        print "Available plugins:"
+        for id, factory in active_plugins():
+            print "  %s" % id
+            
+    else:
+        print 'Usage: %s OUTPUTDIR [OPTIONS]' % sys.argv[0]
+        print 'Try `%s --help` for more information.' % sys.argv[0]

Added: kukit/kss.base/trunk/src/kss/base/utils.txt
==============================================================================
--- (empty file)
+++ kukit/kss.base/trunk/src/kss/base/utils.txt	Sat Mar  1 11:35:25 2008
@@ -0,0 +1,75 @@
+=========
+Utilities
+=========
+
+KSS ships with various utilties. When you install `kss.base` these get
+installed as executables.
+
+
+Create a standalone package
+===========================
+
+Since the whole KSS framework is pluggable the amount of Javascript
+that ends up being used depends a lot on the plugins. To make it easy
+to create static files from a know plugin configuration we have the
+`ksspackage`. This creates a directory with all the Javascripts needed
+to run the set of plugins.
+
+  >>> from pkg_resources import load_entry_point
+  >>> ksspackage = load_entry_point(
+  ...     'kss.base', 'console_scripts', 'ksspackage')
+
+We will first setup a temp directory to contain our stuff.
+
+  >>> import os
+  >>> from tempfile import mktemp
+  >>> temp = mktemp()
+  >>> static_dump = os.path.join(temp, 'static-dump')
+
+Executing the script without any arguments will raise an error.
+
+  >>> import sys
+  >>> old_argv = sys.argv
+  >>> sys.argv = ['ksspackage']
+
+  >>> ksspackage()
+  Usage: ksspackage OUTPUTDIR [OPTIONS]
+  Try `ksspackage --help` for more information.
+
+It needs at least one argument, the path for the output.
+
+  >>> sys.argv = ['ksspackage', static_dump]
+  >>> ksspackage()
+  Creating `kss.js` script: DONE
+
+If we look into the export we will see only the `kss.js` file.
+
+  >>> os.listdir(static_dump)
+  ['kss.js']
+
+In this case it is empty since we did not give it any plugins to
+extract the Javascript from.
+
+  >>> open(os.path.join(static_dump, 'kss.js')).read()
+  ''
+
+Now if we give it a plugin it will put the files into the dir.
+
+  >>> sys.argv = ['ksspackage', static_dump, '--plugin=kss-core']
+  >>> ksspackage()
+  Creating `kss.js` script: DONE
+  Creating `base2-dom-fp.js` script: DONE
+  Creating `sarissa.js` script: DONE
+
+  >>> list(sorted(os.listdir(static_dump)))
+  ['base2-dom-fp.js', 'kss.js', 'sarissa.js']
+
+  >>> sys.argv = old_argv
+
+Besides creating the scripts the utility can also list the available
+plugins.
+
+  >>> sys.argv = ['ksspackage', '--list']
+  >>> ksspackage()
+  Available plugins:
+    kss-core
\ No newline at end of file


More information about the Kukit-checkins mailing list