path.py :  » Database » PyTables » tables-2.1.2 » tables » 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 » Database » PyTables 
PyTables » tables 2.1.2 » tables » path.py
"""
Functionality related with node paths in a PyTables file.

:Author: Ivan Vilata i Balaguer
:Contact: ivan at selidor dot net
:License: BSD
:Created: January 15, 2007
:Revision: $Id: path.py 3782 2008-10-03 12:49:15Z faltet $

Variables
=========

`__docformat`__
    The format of documentation strings in this module.
`__version__`
    Repository version of this file.
"""

# Imports
# =======
import re
import warnings
import keyword

from tables.exceptions import NaturalNameWarning


# Public variables
# ================
__docformat__ = 'reStructuredText'
"""The format of documentation strings in this module."""

__version__ = '$Revision: 3782 $'
"""Repository version of this file."""


# Private variables
# =================
_pythonIdRE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')
"""Python identifier regular expression."""

_reservedIdRE = re.compile('^_[cfgv]_')
"""
PyTables reserved identifier regular expression.

- c: class variables
- f: class public methods
- g: class private methods
- v: instance variables
"""

_hiddenNameRE = re.compile('^_[pi]_')
"""
Nodes with a name *matching* this expression are considered hidden.

For instance, ``name`` whould be visible while ``_i_name`` would not.
"""

_hiddenPathRE = re.compile('/_[pi]_')
"""
Nodes with a path *containing* this expression are considered hidden.

For instance, a node with a pathname like ``/a/b/c`` would be visible
while nodes with pathnames like ``/a/c/_i_x`` or ``/a/_p_x/y`` would
not.
"""


# Public functions
# ================
def checkNameValidity(name):
    """
    Check the validity of the `name` of an object.

    If the name is not valid, a ``ValueError`` is raised.  If it is
    valid but it can not be used with natural naming, a
    `NaturalNameWarning` is issued.
    """

    warnInfo = (
        "you will not be able to use natural naming to access this object; "
        "using ``getattr()`` will still work, though" )

    if not isinstance(name, basestring):  # Python >= 2.3
        raise TypeError("object name is not a string: %r" % (name,))

    # Check whether `name` is a valid HDF5 name.
    # http://hdfgroup.org/HDF5/doc/UG/03_Model.html#Structure
    if name == '':
        raise ValueError("the empty string is not allowed as an object name")
    if name == '.':
        raise ValueError("``.`` is not allowed as an object name")
    if '/' in name:
        raise ValueError( "the ``/`` character is not allowed "
                          "in object names: %r" % name )

    # Check whether `name` is a valid Python identifier.
    if not _pythonIdRE.match(name):
        warnings.warn( "object name is not a valid Python identifier: %r; "
                       "it does not match the pattern ``%s``; %s"
                       % (name, _pythonIdRE.pattern, warnInfo),
                       NaturalNameWarning )
        return

    # However, Python identifiers and keywords have the same form.
    if keyword.iskeyword(name):
        warnings.warn( "object name is a Python keyword: %r; %s"
                       % (name, warnInfo), NaturalNameWarning )
        return

    # Still, names starting with reserved prefixes are not allowed.
    if _reservedIdRE.match(name):
        raise ValueError( "object name starts with a reserved prefix: %r; "
                          "it matches the pattern ``%s``"
                          % (name, _reservedIdRE.pattern) )

    # ``__members__`` is the only exception to that rule.
    if name == '__members__':
        raise ValueError("``__members__`` is not allowed as an object name")


def joinPath(parentPath, name):
    """
    Join a *canonical* `parentPath` with a *non-empty* `name`.

    >>> joinPath('/', 'foo')
    '/foo'
    >>> joinPath('/foo', 'bar')
    '/foo/bar'
    >>> joinPath('/foo', '/foo2/bar')
    '/foo/foo2/bar'
    >>> joinPath('/foo', '/')
    '/foo'
    """

    if parentPath == '/' and name.startswith('/'):
        pstr = '%s' % name
    elif parentPath == '/' or name.startswith('/'):
        pstr = '%s%s' % (parentPath, name)
    else:
        pstr = '%s/%s' % (parentPath, name)
    if pstr.endswith('/'):
        pstr = pstr[:-1]
    return pstr


def splitPath(path):
    """
    Split a *canonical* `path` into a parent path and a node name.

    The result is returned as a tuple.  The parent path does not
    include a trailing slash.

    >>> splitPath('/')
    ('/', '')
    >>> splitPath('/foo/bar')
    ('/foo', 'bar')
    """

    lastSlash = path.rfind('/')
    ppath = path[:lastSlash]
    name = path[lastSlash+1:]

    if ppath == '':
        ppath = '/'

    return (ppath, name)


def isVisibleName(name):
    """Does this `name` make the named node a visible one?"""
    return _hiddenNameRE.match(name) is None


def isVisiblePath(path):
    """Does this `path` make the named node a visible one?"""
    return _hiddenPathRE.search(path) is None


# Main part
# =========
def _test():
    """Run ``doctest`` on this module."""
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    _test()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.