CommandLineUtil.py :  » XML » 4Suite » 4Suite-XML-1.0.2 » Ft » Lib » CommandLine » 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 » CommandLine » CommandLineUtil.py
########################################################################
# $Header: /var/local/cvsroot/4Suite/Ft/Lib/CommandLine/CommandLineUtil.py,v 1.12 2004/09/07 20:05:27 mbrown Exp $
"""
Utility functions used by command-line scripts

Copyright 2004 Fourthought, Inc. (USA).
Detailed license and copyright information: http://4suite.org/COPYRIGHT
Project home, documentation, distributions: http://4suite.org/
"""

import os, sys

from Ft.Lib import Uri,Wrap

__all__ = ['ArgumentError', 'GetoptError',
           'wrap_text',
           'SourceArgToUri', 'SourceArgToInputSource']


class ArgumentError(Exception):
    pass


class GetoptError(Exception):
    pass


def wrap_text(text, width):
    """
    Split 'text' into multiple lines of no more than 'width' characters
    each, and return the result as a list of strings.

    This function differs from distutils.fancy_getopt.wrap_text() in
    that the distutils version collapses blank lines; this one doesn't.

    See also: Ft.Lib.Wrap()
    """
    return Wrap(text, width).split('\n')


def SourceArgToUri(arg, resolver=Uri.BASIC_RESOLVER):
    """
    Some command-line scripts take an argument that is supposed to be
    either "-" (denoting standard input) or a URI reference that can be
    resolved against the URI equivalent of the current working
    directory. This function processes such an argument, given as a
    string, and returns an appropriate URI.

    Since users tend to expect local OS paths to work as URIs, this
    function will accept and use an OS path argument if does appear to
    point to an existing local file, even though this could interfere
    with catalog-based resolution.

    Raises a ValueError if arg is neither a local file nor a valid URI
    reference nor "-".

    The resolver object must support a normalize() method that
    can resolve a URI reference against a base URI, returning a URI.
    """
    if not isinstance(resolver, Uri.UriResolverBase):
        msg = 'It appears there is a bug in this command-line' \
              ' script. A %s was passed as URI resolver to a function that' \
              ' requires an instance of Ft.Lib.Uri.UriResolverBase (or' \
              ' a subclass thereof).'
        raise TypeError(msg % type(resolver))
    if not isinstance(arg, str) and not isinstance(arg, unicode):
        msg = 'It appears there is a bug in this command-line' \
              ' script. A %s was passed as an argument needing to be' \
              ' converted to a URI. A string must be provided instead.'
        raise TypeError(msg % type(arg))

    if arg == '-':
        return Uri.OsPathToUri('unknown-STDIN', attemptAbsolute=True)
    elif arg:
        if os.path.isfile(arg):
            return Uri.OsPathToUri(arg, attemptAbsolute=True)
        elif not Uri.MatchesUriRefSyntax(arg):
            raise ValueError("'%s' is not a valid URI reference." % arg)
        elif Uri.IsAbsolute(arg):
            return arg

    base = Uri.OsPathToUri(os.getcwd(), attemptAbsolute=True)
    if base[-1] != '/':
        base += '/'
    return resolver.normalize(arg, base)


def SourceArgToInputSource(arg, factory, *v_args, **kw_args):
    """
    Some command-line scripts take an argument that is supposed to be
    either "-" (denoting standard input) or a URI reference that can be
    resolved against the URI equivalent of the current working
    directory. This function processes such an argument, given as a
    string, and returns an appropriate InputSource object.

    Since users tend to expect local OS paths to work as URIs, this
    function will accept and use an OS path argument if does appear to
    point to an existing local file, even though this could interfere
    with catalog-based resolution.

    Raises a ValueError if arg is neither a local file nor a valid URI
    reference nor "-". Raises a UriException if a stream for the
    InputSource could not be opened (e.g., when the URI refers to a
    directory or unreadable file).

    Extra arguments given to this function are passed to the
    InputSourceFactory method that creates the InputSource instance.
    The factory must support the methods fromStream() and fromUri(),
    as defined in Ft.Xml.InputSource.InputSourceFactory. The factory
    must also provide a resolver object with a normalize() method that
    can resolve a URI reference against a base URI, returning a URI.
    """
    # It is important to require the caller to supply the factory,
    # as we are in Ft.Lib, not Ft.Xml, so it is not safe to import
    # Ft.Xml.InputSourceFactory. Also, we want to leave open the
    # possibility that other factories can be used, perhaps to
    # generate an InputSource that is not XML oriented or that is
    # a subclass of Ft.Xml.InputSource.
    if factory is None:
        msg = 'It appears there is a bug in this command-line' \
              ' script. Python\'s "None" type was passed to a' \
              ' function that requires an instance of' \
              ' Ft.Xml.InputSource.InputSourceFactory.'
        raise TypeError(msg)

    sourceUri = SourceArgToUri(arg, factory.resolver)

    if arg == '-':
        v_args = (sys.stdin,) + v_args
        kw_args['uri'] = sourceUri
        isrc = factory.fromStream(*v_args, **kw_args)
    else:
        v_args = (sourceUri,) + v_args
        isrc = factory.fromUri(*v_args, **kw_args)
    return isrc

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.