# See the License for the specific language governing permissions and
# limitations under the License.
-from copy import deepcopy
-
-from ncclient.rpc import RPC
-
-def build_filter(spec, type, criteria):
- filter = {
- 'tag': 'filter',
- 'attributes': {'type': type}
- }
- if type=='subtree':
- if isinstance(criteria, dict):
- filter['children'] = [criteria]
- else:
- filter['text'] = criteria
- elif type=='xpath':
- filter['attributes']['select'] = criteria
+from rpc import RPC, RPCReply
-class Get(RPC):
+from ncclient.xml_ import *
+
+import util
+
+class GetReply(RPCReply):
+
+ """Adds attributes for the *data* element to `RPCReply`."""
+
+ def _parsing_hook(self, root):
+ self._data = None
+ if not self._errors:
+ self._data = root.find(qualify("data"))
- SPEC = {
- 'tag': 'get',
- 'children': []
- }
+ @property
+ def data_ele(self):
+ "*data* element as an :class:`~xml.etree.ElementTree.Element`"
+ if not self._parsed:
+ self.parse()
+ return self._data
+
+ @property
+ def data_xml(self):
+ "*data* element as an XML string"
+ if not self._parsed:
+ self.parse()
+ return to_xml(self._data)
+ data = data_ele
+ "Same as :attr:`data_ele`"
+
+
+class Get(RPC):
+
+ "The *get* RPC."
+
+ REPLY_CLS = GetReply
+ "See :class:`GetReply`."
+
def request(self, filter=None):
- spec = deepcopy(SPEC)
+ """Retrieve running configuration and device state information.
+
+ *filter* specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
+
+ :seealso: :ref:`filter_params`
+ """
+ node = new_ele("get")
if filter is not None:
- spec['children'].append(build_filter(*filter))
- return self._request(spec)
+ node.append(util.build_filter(filter))
+ return self._request(node)
class GetConfig(RPC):
-
- SPEC = {
- 'tag': 'get-config',
- 'children': [ { 'tag': 'source', 'children': {'tag': None } } ]
- }
-
- def request(self, source='running', filter=None):
- spec = deepcopy(SPEC)
- spec['children'][0]['children']['tag'] = source
+
+ "The *get-config* RPC."
+
+ REPLY_CLS = GetReply
+ "See :class:`GetReply`."
+
+ def request(self, source, filter=None):
+ """Retrieve all or part of a specified configuration.
+
+ *source* name of the configuration datastore being queried
+
+ *filter* specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
+
+ :seealso: :ref:`filter_params`"""
+ node = new_ele("get-config")
+ node.append(util.datastore_or_url("source", source, self._assert))
if filter is not None:
- spec['children'].append(build_filter(*filter))
- return self._request(spec)
+ node.append(util.build_filter(filter))
+ return self._request(node)
\ No newline at end of file