1. Gregory Petukhov
  2. grab

Commits

Gregory Petukhov  committed 4ec8367

Generate warning if Selector class is used

  • Participants
  • Parent commits cfa0f39
  • Branches default

Comments (0)

Files changed (4)

File grab/error.py

View file
  • Ignore whitespace
     |-> Grab*Error
 
 """
+import warnings
 
 class GrabError(Exception):
     """
     Raised when Grab reached max. allowd number of redirects for
     one request.
     """
+
+
+class GrabDeprecationWarning(Warning):
+    """
+    Raised when some deprecated feature is used.
+    """
+
+
+def warn(msg):
+    warnings.warn(msg, category=GrabDeprecationWarning, stacklevel=3)

File grab/item.py

View file
  • Ignore whitespace
 
 from .tools.lxml_tools import get_node_text
 from .error import DataNotFound
-from .selector import Selector
+from .selector import XpathSelector
 
 NULL = object()
 
         self._tree = tree
         self._cache = {}
         self._grab = grab
-        self._selector = Selector(self._tree)
+        self._selector = XpathSelector(self._tree)
 
     @classmethod
     def find(cls, root, **kwargs):

File grab/selector/selector.py

View file
  • Ignore whitespace
 """
 Selector module provides high usability interface to lxml tree
-
-Example of usage::
-
-    >>> from lxml.html import fromstring
-    >>> from grab.selector import Selector
-    >>> HTML = '<body><h1>test</h1><ul><li>one: 1</li><li>two: 2</li></ul><span id="foo">FOO</span>'
-    >>> sel = Selector(fromstring(HTML))
-    >>> sel.select('//li')
-    <grab.selector.selector.SelectorList object at 0x1aeaa90>
-    >>> sel.select('//li')[0].node
-    <Element li at 0x1adad10>
-    >>> sel.select('//li')[0].node.text
-    'one: 1'
-    >>> sel.select('//li')[0].html()
-    '<li>one: 1</li>'
-    >>> sel.select('//span')[0].attr('id')
-    'foo'
-    >>> sel.select('//span/@id')[0]
-    <grab.selector.selector.Selector object at 0x1aeaa50>
-    >>> sel.select('//span').one().html()
-    '<span id="foo">FOO</span>'
-    >>> sel.select('//li').one().html()
-    '<li>one: 1</li>'
-    >>> sel.select('//li').text()
-    'one: 1'
-    >>> sel.select('//li').number()
-    1
-    >>> sel.select('//li').exists()
-    True
 """
 from __future__ import absolute_import
 import logging
 
 from ..tools.lxml_tools import get_node_text, render_html
 from ..tools.text import find_number, normalize_space as normalize_space_func
-from ..error import GrabMisuseError, DataNotFound
+from ..error import GrabMisuseError, DataNotFound, warn
 from ..tools import rex as rex_tools
 from ..tools.text import normalize_space
 from ..tools.html import decode_entities
 class Selector(XpathSelector):
     def __init__(self, *args, **kwargs):
         super(Selector, self).__init__(*args, **kwargs)
-        logger.debug('Selector class is deprecated. Please use XpathSelector class instead.')
+        warn('Selector class is deprecated. Please use XpathSelector class instead.')

File test/item.py

View file
  • Ignore whitespace
                        FuncField)
 from test.util import GRAB_TRANSPORT
 from grab.tools.lxml_tools import get_node_text, parse_html
-from grab.selector import Selector
+from grab.selector import XpathSelector
 
 XML = """<?xml version='1.0' encoding='utf-8'?>
 <bbapi version='1'>
     def test_get_function(self):
         func = Player.get_function('height1')
         html = '<html><body><height>3'
-        self.assertEquals(3, func(Selector(parse_html(html))))
+        self.assertEquals(3, func(XpathSelector(parse_html(html))))
 
         func = Player.get_function('height2')
         html = '<html><body><height>3'
-        self.assertEquals(3, func(Selector(parse_html(html))))
+        self.assertEquals(3, func(XpathSelector(parse_html(html))))