Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
rename: jsonpath->jmespath to  disambiguation
  • Loading branch information
EchoShoot committed Jan 3, 2020
1 parent 85c7b58 commit 3846ea7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
22 changes: 11 additions & 11 deletions parsel/selector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Jsonpath/XPath selectors based on lxml and jmespath
Jmespath/XPath selectors based on lxml and jmespath
"""
import json
import sys
Expand Down Expand Up @@ -73,14 +73,14 @@ def __getitem__(self, pos):
def __getstate__(self):
raise TypeError("can't pickle SelectorList objects")

def jsonpath(self, jsonpath, **kwargs):
def jmespath(self, jmespath, **kwargs):
"""
Call the ``.jsonpath()`` method for each element in this list and return
Call the ``.jmespath()`` method for each element in this list and return
their results flattened as another :class:`SelectorList`.
``jsonpath`` is the same argument as the one in :meth:`Selector.jsonpath`
``jmespath`` is the same argument as the one in :meth:`Selector.jmespath`
"""
return self.__class__(flatten([x.jsonpath(jsonpath, **kwargs) for x in self]))
return self.__class__(flatten([x.jmespath(jmespath, **kwargs) for x in self]))

def xpath(self, xpath, namespaces=None, **kwargs):
"""
Expand Down Expand Up @@ -213,7 +213,7 @@ def __init__(self, text=None, type=None, namespaces=None, root=None,
self._parser = _ctgroup[st]['_parser']
self._csstranslator = _ctgroup[st]['_csstranslator']
self._tostring_method = _ctgroup[st]['_tostring_method']
self._text = text # save source text is friendly to jsonpath
self._text = text # save source text is friendly to jmespath

if text is not None:
if not isinstance(text, six.text_type):
Expand All @@ -237,12 +237,12 @@ def __getstate__(self):
def _get_root(self, text, base_url=None):
return create_root_node(text, self._parser, base_url=base_url)

def jsonpath(self, query, **kwargs):
def jmespath(self, query, **kwargs):
"""
Call the ``.jsonpath()`` method for each element in this list and return
Call the ``.jmespath()`` method for each element in this list and return
their results flattened as another :class:`SelectorList`.
``query`` is the same argument as the one in :meth:`Selector.jsonpath`
``query`` is the same argument as the one in :meth:`Selector.jmespath`
Requires : jmespath(https://github.com/jmespath/jmespath)
"""
Expand All @@ -257,7 +257,7 @@ def jsonpath(self, query, **kwargs):
else:
datas = json.loads(self.root.text)
except jmespath.exceptions.JMESPathError as exc:
msg = u"Jsonpath error: %s in %s" % (exc, query)
msg = u"jmespath error: %s in %s" % (exc, query)
msg = msg if six.PY3 else msg.encode('unicode_escape')
six.reraise(ValueError, ValueError(msg), sys.exc_info()[2])
else:
Expand Down Expand Up @@ -457,7 +457,7 @@ def __bool__(self):

def __str__(self):
if self.json is not None:
return "<%s jsonpath=%r data=%s>" % (type(self).__name__, self._expr, repr(self.json))
return "<%s jmespath=%r data=%s>" % (type(self).__name__, self._expr, repr(self.json))
else:
data = repr(shorten(self.get(), width=40))
return "<%s xpath=%r data=%s>" % (type(self).__name__, self._expr, data)
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ flake8-ignore =
parsel/xpathfuncs.py E501
tests/test_selector.py E124 E127 E128 E231 E265 E303 E501 F811 W293
tests/test_selector_csstranslator.py E501
tests/test_selector_jsonpath.py E501
tests/test_selector_jmespath.py E501
tests/test_utils.py E501
tests/test_xpathfuncs.py E501
22 changes: 11 additions & 11 deletions tests/test_selector_jsonpath.py → tests/test_selector_jmespath.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
# @Author : EchoShoot
# @Email : [email protected]
# @URL : https://github.com/EchoShoot
# @File : test_selector_jsonpath.py
# @File : test_selector_jmespath.py
# @Explain :
import unittest

from parsel import Selector


class jsonpathTestCase(unittest.TestCase):
class jmespathTestCase(unittest.TestCase):
sscls = Selector

def test_jsonpath_with_json_contains_html(self):
def test_jmespath_with_json_contains_html(self):
""" Sometimes the information is returned in a json wrapper """
datas = u"""{
"content": [
Expand Down Expand Up @@ -42,17 +42,17 @@ def test_jsonpath_with_json_contains_html(self):
}
"""
sel = Selector(text=datas)
self.assertEqual(sel.jsonpath(u'html').get(),
self.assertEqual(sel.jmespath(u'html').get(),
u'<div><a>AAA<br>Test</a>aaa</div>'
u'<div><a>BBB</a>bbb<b>BbB</b><div/>')
self.assertEqual(sel.jsonpath(u'html').xpath(u'//div/a/text()').getall(),
self.assertEqual(sel.jmespath(u'html').xpath(u'//div/a/text()').getall(),
[u'AAA', u'Test', u'BBB'])
self.assertEqual(sel.jsonpath(u'html').xpath(u'//div/b').getall(),
self.assertEqual(sel.jmespath(u'html').xpath(u'//div/b').getall(),
[u'<b>BbB</b>'])
self.assertEqual(sel.jsonpath(u'content').jsonpath(u'name.age').get(),
self.assertEqual(sel.jmespath(u'content').jmespath(u'name.age').get(),
18)

def test_jsonpath_with_html_contains_json(self):
def test_jmespath_with_html_contains_json(self):
html_text = u"""
<div>
<h1>Information</h1>
Expand Down Expand Up @@ -84,10 +84,10 @@ def test_jsonpath_with_html_contains_json(self):
"""
sel = Selector(text=html_text)
self.assertEqual(
sel.xpath(u'//div/content/text()').jsonpath(u'user[*].name').getall(),
sel.xpath(u'//div/content/text()').jmespath(u'user[*].name').getall(),
[u'A', u'B', u'C', u'D'])
self.assertEqual(
sel.xpath(u'//div/content').jsonpath(u'user[*].name').getall(),
sel.xpath(u'//div/content').jmespath(u'user[*].name').getall(),
[u'A', u'B', u'C', u'D'])
self.assertEqual(
sel.xpath(u'//div/content').jsonpath(u'total').get(), 4)
sel.xpath(u'//div/content').jmespath(u'total').get(), 4)

0 comments on commit 3846ea7

Please sign in to comment.