utils.py :  » Network » Grail-Internet-Browser » grail-0.6 » grailbase » 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 » grailbase » utils.py
"""Several useful routines that isolate some of the weirdness of Grail-based
applications.
"""
__version__ = '$Revision: 1.3 $'

import os
import string

# TBD: hack!  grail.py calculates grail_root, which would be
# convenient to export to extensions, but you can't `import grail' or
# `import __main__'.  grail.py isn't designed for that.  You could
# `from grail import grail_root' but that's kind of gross.  This
# global holds the value of grail_root which can be had with
# grailutil.get_grailroot()
_grail_root = None
_grail_app = None


# XXX Unix specific stuff
# XXX (Actually it limps along just fine for Macintosh, too)

def getgraildir():
    return getenv("GRAILDIR") or os.path.join(gethome(), ".grail")


def get_grailroot():
    return _grail_root


def get_grailapp():
    return _grail_app


def gethome():
    try:
        home = getenv("HOME")
        if not home:
            import pwd
            user = getenv("USER") or getenv("LOGNAME")
            if not user:
                pwent = pwd.getpwuid(os.getuid())
            else:
                pwent = pwd.getpwnam(user)
            home = pwent[6]
        return home
    except (KeyError, ImportError):
        return os.curdir


def getenv(s):
    if os.environ.has_key(s): return os.environ[s]
    return None


def which(filename, searchlist=None):
    if searchlist is None:
        import sys
        searchlist = sys.path
    for dir in searchlist:
        found = os.path.join(dir, filename)
        if os.path.exists(found):
            return found
    return None


def establish_dir(dir):
    """Ensure existence of DIR, creating it if necessary.

    Returns 1 if successful, 0 otherwise."""
    if os.path.isdir(dir):
        return 1
    head, tail = os.path.split(dir)
    if not establish_dir(head):
        return 0
    try:
        os.mkdir(dir, 0777)
        return 1
    except os.error:
        return 0


def conv_mimetype(type):
    """Convert MIME media type specifications to tuples of
    ('type/subtype', {'option': 'value'}).
    """
    if not type:
        return None, {}
    if ';' in type:
        i = string.index(type, ';')
        opts = _parse_mimetypeoptions(type[i + 1:])
        type = type[:i]
    else:
        opts = {}
    fields = string.split(string.lower(type), '/')
    if len(fields) != 2:
        raise ValueError, "Illegal media type specification."
    type = string.join(fields, '/')
    return type, opts


def _parse_mimetypeoptions(options):
    opts = {}
    options = string.strip(options)
    while options:
        if '=' in options:
            pos = string.find(options, '=')
            name = string.lower(string.strip(options[:pos]))
            value = string.strip(options[pos + 1:])
            options = ''
            if ';' in value:
                pos = string.find(value, ';')
                options = string.strip(value[pos + 1:])
                value = string.strip(value[:pos])
            if name:
                opts[name] = value
        else:
            options = None
    return opts
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.