Skip to content

Commit 4a23ea6

Browse files
tarkatronicrobshakir
authored andcommitted
Switch to using regex instead of re (#170)
* Use regex instead of re as regex implements \p{*} py3 re stdlib fails if the regular expression contains things it can't understand like \p. However, regex actually understands those constructs * Update a couple more references to use regex
1 parent c0aa7d6 commit 4a23ea6

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

pyangbind/lib/xpathhelper.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from collections import OrderedDict
2727

2828
from lxml import etree
29-
import re
29+
import regex
3030
import uuid
3131
from .yangtypes import safe_name
3232
from .base import PybindBase
@@ -104,11 +104,11 @@ def __init__(self):
104104

105105

106106
class YANGPathHelper(PybindXpathHelper):
107-
_attr_re = re.compile("^(?P<tagname>[^\[]+)(?P<args>(\[[^\]]+\])+)$")
108-
_arg_re = re.compile("^((and|or) )?[@]?(?P<cmd>[a-zA-Z0-9\-\_:]+)([ ]+)?" +
107+
_attr_re = regex.compile("^(?P<tagname>[^\[]+)(?P<args>(\[[^\]]+\])+)$")
108+
_arg_re = regex.compile("^((and|or) )?[@]?(?P<cmd>[a-zA-Z0-9\-\_:]+)([ ]+)?" +
109109
"=([ ]+)?[\'\"]?(?P<arg>[^ ^\'^\"]+)([\'\"])?([ ]+)?" +
110110
"(?P<remainder>.*)")
111-
_relative_path_re = re.compile("^(\.|\.\.)")
111+
_relative_path_re = regex.compile("^(\.|\.\.)")
112112

113113
def __init__(self):
114114
# Initialise an empty library and a new FakeRoot class to act as the
@@ -174,7 +174,7 @@ def _encode_path(self, path, mode="search", find_parent=False,
174174
for k, v in attributes.iteritems():
175175
# handling for rfc6020 current() specification
176176
if "current()" in v:
177-
remaining_path = re.sub("current\(\)(?P<remaining>.*)",
177+
remaining_path = regex.sub("current\(\)(?P<remaining>.*)",
178178
'\g<remaining>', v).split("/")
179179
# since the calling leaf may not exist, we need to do a
180180
# lookup on a path that will do, which is the parent
@@ -227,7 +227,7 @@ def register(self, object_path, object_ptr, caller=False):
227227
if isinstance(object_path, str):
228228
raise XPathError("not meant to receive strings as input to register()")
229229

230-
if re.match('^\.\.', object_path[0]):
230+
if regex.match('^\.\.', object_path[0]):
231231
raise XPathError("unhandled relative path in register()")
232232

233233
# This is a hack to register anything that is a top-level object,
@@ -279,7 +279,7 @@ def register(self, object_path, object_ptr, caller=False):
279279
def unregister(self, object_path, caller=False):
280280
if isinstance(object_path, str):
281281
raise XPathError("should not receive paths as a str in unregister()")
282-
if re.match("^(\.|\.\.|\/)", object_path[0]):
282+
if regex.match("^(\.|\.\.|\/)", object_path[0]):
283283
raise XPathError("unhandled relative path in unregister()")
284284

285285
existing_objs = self._get_etree(object_path)

pyangbind/lib/yangtypes.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
from decimal import Decimal
2525
from bitarray import bitarray
2626
import uuid
27-
import re
27+
import regex
2828
import collections
2929
import copy
3030
import six
3131

3232
# For Python3
3333
if six.PY3:
3434
unicode = str
35-
35+
basestring = str
3636
# Words that could turn up in YANG definition files that are actually
3737
# reserved names in Python, such as being builtin types. This list is
3838
# not complete, but will probably continue to grow.
@@ -139,7 +139,7 @@ def RestrictedClassType(*args, **kwargs):
139139
# this gives deserialisers some hints as to how to encode/decode this value
140140
# it must be a list since a restricted class can encapsulate a restricted
141141
# class
142-
current_restricted_class_type = re.sub("<(type|class) '(?P<class>.*)'>",
142+
current_restricted_class_type = regex.sub("<(type|class) '(?P<class>.*)'>",
143143
"\g<class>", str(base_type))
144144
if hasattr(base_type, "_restricted_class_base"):
145145
restricted_class_hint = getattr(base_type, "_restricted_class_base")
@@ -179,9 +179,9 @@ def __new__(self, *args, **kwargs):
179179
_restriction_test method so that it can be called by other functions.
180180
"""
181181

182-
range_regex = re.compile("(?P<low>\-?[0-9\.]+|min)([ ]+)?\.\.([ ]+)?" +
182+
range_regex = regex.compile("(?P<low>\-?[0-9\.]+|min)([ ]+)?\.\.([ ]+)?" +
183183
"(?P<high>(\-?[0-9\.]+|max))")
184-
range_single_value_regex = re.compile("(?P<value>\-?[0-9\.]+)")
184+
range_single_value_regex = regex.compile("(?P<value>\-?[0-9\.]+)")
185185

186186
def convert_regexp(pattern):
187187

@@ -203,6 +203,7 @@ def convert_regexp(pattern):
203203
pattern = "^%s" % pattern
204204
if not pattern[len(pattern) - 1] == "$":
205205
pattern = "%s$" % pattern
206+
206207
return pattern
207208

208209
def build_length_range_tuples(range, length=False, multiplier=1):
@@ -253,7 +254,7 @@ def match_pattern_check(regexp):
253254
def mp_check(value):
254255
if not isinstance(value, basestring):
255256
return False
256-
if re.match(convert_regexp(regexp), value):
257+
if regex.match(convert_regexp(regexp), value):
257258
return True
258259
return False
259260
return mp_check
@@ -947,7 +948,7 @@ class YANGBaseClass(base_type):
947948
if yang_type in ["container", "list"] or is_container == "container":
948949
__slots__ = tuple(clsslots)
949950

950-
_pybind_base_class = re.sub("<(type|class) '(?P<class>.*)'>", "\g<class>",
951+
_pybind_base_class = regex.sub("<(type|class) '(?P<class>.*)'>", "\g<class>",
951952
str(base_type))
952953

953954
def __new__(self, *args, **kwargs):
@@ -1211,7 +1212,7 @@ def __init__(self, *args, **kwargs):
12111212

12121213
if value is not None:
12131214
set_method(value)
1214-
self._type = re.sub("<(type|class) '(?P<class>.*)'>", "\g<class>",
1215+
self._type = regex.sub("<(type|class) '(?P<class>.*)'>", "\g<class>",
12151216
str(get_method()._base_type))
12161217

12171218
self._utype = get_method()._base_type

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pyang
22
bitarray
33
lxml
4+
regex
45
six
56
enum34

tests/serialise/openconfig-serialise/run.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
import requests
88
import time
9-
import re
9+
import regex
1010
import difflib
1111

1212
from pyangbind.lib.xpathhelper import YANGPathHelper
@@ -94,7 +94,7 @@ def main():
9494

9595
for fn in os.listdir(os.path.join(this_dir, "json")):
9696
jobj = json.load(open(os.path.join(this_dir, "json", fn), 'r'))
97-
parameters = re.sub(
97+
parameters = regex.sub(
9898
'interfaces\_ph:(?P<pathhelper>[a-zA-Z]+)\-flt:(?P<filter>[a-zA-Z]+)\-m:(?P<mode>[a-zA-Z]+)\.json',
9999
'\g<pathhelper>||\g<filter>||\g<mode>', fn).split("||")
100100
path_helper, config_filter, mode = YANGBool(parameters[0]), YANGBool(parameters[1]), parameters[2]

0 commit comments

Comments
 (0)