CommandLineFormatter.py :  » XML » 4Suite » 4Suite-XML-1.0.2 » Ft » Lib » DistExt » Formatters » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » XML » 4Suite 
4Suite » 4Suite XML 1.0.2 » Ft » Lib » DistExt » Formatters » CommandLineFormatter.py
from Ft.Lib.CommandLine import CommandLineApp
from Ft.Lib.DistExt.Formatters import XmlFormatter

class CommandLineFormatter(XmlFormatter.XmlFormatter):

    document_type = CommandLineApp.CommandLineApp

    def document(self, application):
        attributes = {'name' : application.name}
        self.start_element('application', attributes)
        self.doc_command(application)
        self.end_element('application')
        return

    def doc_command(self, command):
        if command.description:
            self.write_element('description',
                               content=self.escape(command.description))

        if command.verbose_description:
            self.write_element('verbose-description',
                               content=self.escape(command.verbose_description))

        if command.example:
            self.write_element('example',
                               content=self.escape(command.example))

        if command.options:
            options = map(lambda opt: (None, opt), command.options)
            self.section('options', options, self.doc_option)

        if command.arguments:
            arguments = map(lambda arg: (arg.name, arg), command.arguments)
            self.section('arguments', arguments, self.doc_argument)

        if command.subCommands:
            subcommands = command.subCommands.items()
            self.section('subcommands', subcommands, self.doc_subcommand)
        return

    def doc_subcommand(self, command, name):
        assert name == command.name
        self.start_element('command', {'name' : name})
        self.doc_command(command)
        self.end_element('command')
        return

    def doc_option(self, option, name):
        if hasattr(option, 'choices'):
            options = map(lambda opt: (None, opt), option.choices)
            self.section('exclusive-options', options, self.doc_option)
        else:
            attributes = {'long-name' : option.longName}

            if option.shortName:
                attributes['short-name'] = option.shortName

            self.start_element('option', attributes)

            desc = self.escape(option.description)
            self.write_element('description', content=desc)

            if option.takesArg or hasattr(option, 'allowed'):
                self.start_element('argument', {'name' : option.argName})
                for name, description in getattr(option, 'allowed', []):
                    self.start_element('value', {'name' : name})
                    desc = self.escape(description)
                    self.write_element('description', content=desc)
                    self.end_element('value')
                self.end_element('argument')

            if option.subOptions:
                options = map(lambda opt: (None, opt), option.subOptions)
                self.section('suboptions', options, self.doc_option)

            self.end_element('option')
        return

    def doc_argument(self, argument, name):
        attributes = {
            'name' : name,
            'required' : argument.requirements in [1, 4] and 'yes' or 'no',
            'multiple' : argument.requirements in [3, 4] and 'yes' or 'no',
            }
        self.start_element('argument', attributes)
        desc = self.escape(argument.description)
        self.write_element('description', content=desc)

        self.end_element('argument')
        return
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.