__init__.py :  » XML » 4Suite » 4Suite-XML-1.0.2 » Ft » Lib » 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 » __init__.py
########################################################################
# $Header: /var/local/cvsroot/4Suite/Ft/Lib/__init__.py,v 1.23 2005/03/06 06:39:32 mbrown Exp $
"""
Module providing common utilities for many 4Suite components,
as well as for general use.

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

import os

from Ft import FtException


class UriException(FtException):
    """
    Exceptions used by the Uri module, and possibly others.
    """

    INVALID_BASE_URI = 100

    #RELATIVE_DOCUMENT_URI = 110
    RELATIVE_BASE_URI = 111
    OPAQUE_BASE_URI = 112

    NON_FILE_URI = 120
    UNIX_REMOTE_HOST_FILE_URI = 121

    RESOURCE_ERROR = 130

    SCHEME_REQUIRED = 200 # for SchemeRegistryResolver
    UNSUPPORTED_SCHEME = 201
    IDNA_UNSUPPORTED = 202

    INVALID_PUBLIC_ID_URN = 300

    UNSUPPORTED_PLATFORM = 1000

    def __init__(self, errorCode, *args, **kwargs):
        import MessageSource
        self.params = args or (kwargs,)
        self.errorCode = errorCode
        messages = MessageSource.URI
        msgargs = args or kwargs
        self.message = messages[errorCode] % msgargs
        Exception.__init__(self, self.message, args)
        return


def Truncate(text, length):
    """
    Returns text truncated to length, with "..." appended if truncation
    was necessary.
    """
    if len(text) > length:
        return text[:length] + '...'
    else:
        return text[:length]


def Wrap(text, width):
    r"""
    A word-wrap function that preserves existing line breaks
    and most spaces in the text. Expects that existing line
    breaks are posix newlines (\n).

    See also: Ft.Lib.CommandLine.CommandLineUtil.wrap_text()
    """
    # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061
    return reduce(lambda line, word, width=width: '%s%s%s' %
                  (line,
                   ' \n'[(len(line)-line.rfind('\n')-1
                         + len(word.split('\n',1)[0]
                              ) >= width)],
                   word),
                  text.split(' ')
                 )


def CloseStream(stream, quiet=False):
    """
    Closes a stream, ignoring errors if quiet=True. If the stream is a
    terminal (e.g. sys.stdin, stdout, stderr), does not attempt to close
    the stream.

    Closing terminal streams could interfere with subsequent read or
    write attempts. For example, after calling sys.stdout.close(),
    subsequent writes to stdout will not raise an exception, but may
    also fail to actually write anything to stdout.

    The stream argument can be any Python file-like object with a
    close() method, such as a regular file object or an instance of
    Ft.Xml.InputSource.InputSource (or subclass thereof).
    """
    # get the actual stream
    if hasattr(stream, 'stream'):
        # given stream is probably an InputSource
        raw_stream = stream.stream
    else:
        raw_stream = stream

    # determine if it is a terminal; abort if it is
    if hasattr(raw_stream, 'isatty') and raw_stream.isatty():
        return

    try:
        # if an InputSource, let the InputSource close stream for us
        # since subclasses can override the close method.
        stream.close()
    except:
        if quiet:
            pass
        else:
            raise

    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.