/usr/lib/python3/dist-packages/tables/path.py is in python3-tables 3.3.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | # -*- coding: utf-8 -*-
########################################################################
#
# License: BSD
# Created: January 15, 2007
# Author: Ivan Vilata i Balaguer - ivan at selidor dot net
#
# $Id$
#
########################################################################
"""Functionality related with node paths in a PyTables file.
Variables
=========
`__docformat`__
The format of documentation strings in this module.
"""
from __future__ import absolute_import
# Imports
# =======
import re
import warnings
import keyword
from .exceptions import NaturalNameWarning
import six
# Public variables
# ================
__docformat__ = 'reStructuredText'
"""The format of documentation strings in this module."""
# Private variables
# =================
_python_id_re = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')
"""Python identifier regular expression."""
_reserved_id_re = re.compile('^_[cfgv]_')
"""PyTables reserved identifier regular expression.
- c: class variables
- f: class public methods
- g: class private methods
- v: instance variables
"""
_hidden_name_re = re.compile('^_[pi]_')
"""Nodes with a name *matching* this expression are considered hidden.
For instance, ``name`` whould be visible while ``_i_name`` would not.
"""
_hidden_path_re = 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.
"""
_warnInfo = (
"you will not be able to use natural naming to access this object; "
"using ``getattr()`` will still work, though")
"""Warning printed when a name will not be reachable through natural naming"""
# Public functions
# ================
def check_attribute_name(name):
"""Check the validity of the `name` of an attribute in AttributeSet.
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.
>>> check_attribute_name('a')
>>> check_attribute_name('a_b')
>>> check_attribute_name('a:b')
>>> check_attribute_name('/a/b')
>>> check_attribute_name('/')
>>> check_attribute_name('.')
>>> check_attribute_name('__members__')
Traceback (most recent call last):
...
ValueError: ``__members__`` is not allowed as an object name
>>> check_attribute_name(1)
Traceback (most recent call last):
...
TypeError: object name is not a string: 1
>>> check_attribute_name('')
Traceback (most recent call last):
...
ValueError: the empty string is not allowed as an object name
"""
if not isinstance(name, six.string_types): # Python >= 2.3
raise TypeError("object name is not a string: %r" % (name,))
if name == '':
raise ValueError("the empty string is not allowed as an object name")
# Check whether `name` is a valid Python identifier.
if not _python_id_re.match(name):
warnings.warn("object name is not a valid Python identifier: %r; "
"it does not match the pattern ``%s``; %s"
% (name, _python_id_re.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 _reserved_id_re.match(name):
raise ValueError("object name starts with a reserved prefix: %r; "
"it matches the pattern ``%s``"
% (name, _reserved_id_re.pattern))
# ``__members__`` is the only exception to that rule.
if name == '__members__':
raise ValueError("``__members__`` is not allowed as an object name")
def check_name_validity(name):
"""Check the validity of the `name` of a Node object, which more limited
than attribute names.
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.
>>> check_name_validity('a')
>>> check_name_validity('a_b')
>>> check_name_validity('a:b')
>>> check_name_validity('/a/b')
Traceback (most recent call last):
...
ValueError: the ``/`` character is not allowed in object names: '/a/b'
>>> check_name_validity('.')
Traceback (most recent call last):
...
ValueError: ``.`` is not allowed as an object name
>>> check_name_validity('')
Traceback (most recent call last):
...
ValueError: the empty string is not allowed as an object name
"""
check_attribute_name(name)
# Check whether `name` is a valid HDF5 name.
# http://hdfgroup.org/HDF5/doc/UG/03_Model.html#Structure
if name == '.':
raise ValueError("``.`` is not allowed as an object name")
elif '/' in name:
raise ValueError("the ``/`` character is not allowed "
"in object names: %r" % name)
def join_path(parentpath, name):
"""Join a *canonical* `parentpath` with a *non-empty* `name`.
.. versionchanged:: 3.0
The *parentPath* parameter has been renamed into *parentpath*.
>>> join_path('/', 'foo')
'/foo'
>>> join_path('/foo', 'bar')
'/foo/bar'
>>> join_path('/foo', '/foo2/bar')
'/foo/foo2/bar'
>>> join_path('/foo', '/')
'/foo'
"""
if name.startswith('./'): # Support relative paths (mainly for links)
name = name[2:]
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 split_path(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.
>>> split_path('/')
('/', '')
>>> split_path('/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 _hidden_name_re.match(name) is None
def isvisiblepath(path):
"""Does this `path` make the named node a visible one?"""
return _hidden_path_re.search(path) is None
# Main part
# =========
def _test():
"""Run ``doctest`` on this module."""
import doctest
doctest.testmod()
if __name__ == '__main__':
_test()
|