Skip to content

Commit ccc3098

Browse files
xml_reflection: First step to enable deprecation of public access
1 parent 54e4155 commit ccc3098

File tree

5 files changed

+292
-128
lines changed

5 files changed

+292
-128
lines changed

src/urdf_parser_py/__init__.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Python implementation of the URDF parser.
3+
"""
4+
5+
import functools
6+
import warnings
7+
8+
9+
class _DeprecatedDescriptor(object):
10+
def __init__(self, attr):
11+
self._attr = attr
12+
13+
def _warn(self):
14+
raise NotImplemented
15+
16+
def __get__(self, obj, objtype):
17+
self._warn()
18+
if obj is None:
19+
return getattr(objtype, self._attr)
20+
else:
21+
return getattr(obj, self._attr)
22+
23+
def __set__(self, obj, value):
24+
self._warn()
25+
setattr(obj, self._attr, value)
26+
27+
def __del__(self, obj):
28+
self._warn()
29+
delattr(obj, self._attr)
30+
31+
32+
class _NowPrivateDescriptor(_DeprecatedDescriptor):
33+
# Implements the descriptor interface to warn about deprecated access.
34+
def __init__(self, private):
35+
_DeprecatedDescriptor.__init__(self, private)
36+
self._private = private
37+
self._old_public = self._private.lstrip('_')
38+
self.__doc__ = "Deprecated propery '{}'".format(self._old_public)
39+
40+
def _warn(self):
41+
warnings.warn(
42+
"'{}' is deprecated, and will be removed in future releases."
43+
.format(self._old_public),
44+
category=DeprecationWarning, stacklevel=1)
45+
46+
47+
def _now_private_property(private):
48+
# Indicates that a property (or method) is now private.
49+
return _NowPrivateDescriptor(private)
50+
51+
52+
class _RenamedDescriptor(_DeprecatedDescriptor):
53+
# Implements the descriptor interface to warn about deprecated access.
54+
def __init__(self, old, new):
55+
_DeprecatedDescriptor.__init__(self, new)
56+
self._old = old
57+
self._new = new
58+
self.__doc__ = "Deprecated propery '{}'".format(self._old)
59+
60+
def _warn(self):
61+
warnings.warn(
62+
"'{}' is deprecated, please use '{}' instead.".format(
63+
self._old, self._new),
64+
category=DeprecationWarning, stacklevel=1)
65+
66+
67+
def _renamed_property(old, new):
68+
return _RenamedDescriptor(old, new)

src/urdf_parser_py/_xml_reflection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TODO(eacousineau): Move all symbols from `.xml_reflection` into here.
2+
from urdf_parser_py.xml_reflection.basics import *
3+
# Import full module so that tests can easily monkey patch `on_error`.
4+
from urdf_parser_py.xml_reflection import core
5+
from urdf_parser_py.xml_reflection.core import *
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# TODO(eacousineau): Deprecate public access.
12
from urdf_parser_py.xml_reflection.core import *

src/urdf_parser_py/xml_reflection/basics.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
import string
2-
import yaml
31
import collections
2+
import string
3+
# TODO(eacousineau): Leverage tfoote's PR.
4+
from xml.etree.ElementTree import ElementTree
5+
46
from lxml import etree
7+
import yaml
8+
9+
# TODO(eacousineau): Deprecate public access.
10+
from urdf_parser_py import _now_private_property
11+
12+
__all__ = [
13+
"xml_string",
14+
"dict_sub",
15+
"node_add",
16+
"pfloat",
17+
"xml_children",
18+
"isstring",
19+
"to_yaml",
20+
"SelectiveReflection",
21+
"YamlReflection",
22+
# Backwards compatibility.
23+
"etree",
24+
]
25+
526

627
def xml_string(rootXml, addHeader=True):
728
# Meh
@@ -74,13 +95,15 @@ def to_yaml(obj):
7495

7596

7697
class SelectiveReflection(object):
77-
def get_refl_vars(self):
98+
def _get_refl_vars(self):
7899
return list(vars(self).keys())
79100

101+
get_refl_vars = _now_private_property('_get_refl_vars')
102+
80103

81104
class YamlReflection(SelectiveReflection):
82105
def to_yaml(self):
83-
raw = dict((var, getattr(self, var)) for var in self.get_refl_vars())
106+
raw = dict((var, getattr(self, var)) for var in self._get_refl_vars())
84107
return to_yaml(raw)
85108

86109
def __str__(self):

0 commit comments

Comments
 (0)