grailutil.py :  » Network » Grail-Internet-Browser » grail-0.6 » utils » 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 » Network » Grail Internet Browser 
Grail Internet Browser » grail 0.6 » utils » grailutil.py
"""Miscellaneous utilities for Grail."""

__version__ = "$Revision: 2.31 $"

import os

# Utility functions for handling attribute values used to be defined here;
# now get them from sgml.utils since Grail expects them to be here.  One is
# in the printing package.

from grailbase.utils import *
from sgml.utils import *
from printing.utils import conv_fontsize


# This is here for compatibility with pre-1.5.2 Python versions.
try:
    abspath = os.path.abspath
except AttributeError:
    # Copied from posixpath in Python 1.5.2.
    def abspath(path):
        if not os.path.isabs(path):
            path = join(os.getcwd(), path)
        return os.path.normpath(path)


def complete_url(url):
    import urlparse
    scheme, netloc = urlparse.urlparse(url)[:2]
    if not scheme:
        if not netloc:
            # XXX url2pathname/pathname2url???
            if os.path.exists(url):
                import urllib
                url = "file:" + urllib.quote(url)
            else:
                url = "http://" + url
        else:
            url = "http:" + url
    return url


def nicebytes(n):
    """Convert a bytecount to a string like '<number> bytes' or '<number>K'.

    This is intended for inclusion in status messages that display
    things like '<number>% read of <bytecount>' or '<bytecount> read'.
    When the byte count is large, it will be expressed as a small
    floating point number followed by K, M or G, e.g. '3.14K'.

    The word 'bytes' (or singular 'byte') is part of the returned
    string if the byte count is small; when the count is expressed in
    K, M or G, 'bytes' is implied.

    """
    if n < 1000:
        if n == 1: return "1 byte"
        return "%d bytes" % n
    n = n * 0.001
    if n < 1000.0:
        suffix = "K"
    else:
        n = n * 0.001
        if n < 1000.0:
            suffix = "M"
        else:
            n = n * 0.001
            suffix = "G"
    if n < 10.0: r = 2
    elif n < 100.0: r = 1
    else: r = 0
    return "%.*f" % (r, n) + suffix




def pref_or_getenv(name, group='proxies', type_name='string',
                   check_ok=None, user=0, factory=0):
    """Help for integrating environment variables with preferences.

    First check preferences, under 'group', for the component 'name'.
    If 'name' is defined as a 'string' and it's NULL, try to read
    'name' from the environment.  If 'name's defined in the
    environment, migrate the value to preferences.  Return the value
    associated with the name, None if it's not defined in either place
    (env or prefs... and it's a 'string').  If check_ok is not None,
    it is expected to be a tuple of valid names. e.g. ('name1',
    'name2').  If factory is TRUE then the value for name is retrieved
    only from factory defaults and not user preferences and not the
    environment. If it's not found there, return None.

    """
    if check_ok and  name not in check_ok:
            return None

    app = get_grailapp()

    if type_name == 'string':
        component = app.prefs.Get(group, name, factory=factory)
        if len(component) or factory:
            return component
    elif type_name == 'int':
        component = app.prefs.GetInt(group, name, factory=factory)
        return component
    elif type_name == 'Boolean':
        component = app.prefs.GetBoolean(group, name, factory=factory)
        return component
    elif type_name == 'float':
        component = app.prefs.GetFloat(group, name, factory=factory)
        return component
    else:
        raise ValueError, ('%s not supported - must be one of %s'
                      % (`type_name`, ['string', 'int', 'float', 'Boolean']))

    import os
    try:
        component = os.environ[name]
    except:
        return None

    app.prefs.Set(group, name, component)
    return component

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