Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ The output will be a dictionary with the following structure:
"removed": []
}

In alternative, you can use the subtraction operator:

.. code-block:: python

from netdiff import OlsrParser
from netdiff import diff

stored = OlsrParser('./stored-olsr.json')
latest = OlsrParser('telnet://127.0.0.1:9090')
latest - stored

Parsers
-------

Expand Down
4 changes: 4 additions & 0 deletions netdiff/parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import urllib.parse as urlparse

from ..exceptions import NetParserException, NetParserJsonException, NetJsonException
from ..utils import diff


class BaseParser(object):
Expand Down Expand Up @@ -44,6 +45,9 @@ def __init__(self, data, version=None, revision=None, metric=None):
if self.__class__ is not BaseParser:
self.parse(self.original_data)

def __sub__(self, other):
return diff(other, self)

def _to_python(self, data):
"""
Private method which parses the input data and converts it into a Python data structure
Expand Down
11 changes: 11 additions & 0 deletions tests/olsr/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ def test_added_1_link(self):
self.assertIn('10.150.0.5', result['added'][0])
self.assertIn('10.150.0.4', result['added'][0])

def test_added_1_link_sub(self):
old = OlsrParser(links2)
new = OlsrParser(links3)
result = new - old
# ensure there are differences
self.assertEqual(len(result['added']), 1)
self.assertEqual(len(result['removed']), 0)
# ensure 1 link added
self.assertIn('10.150.0.5', result['added'][0])
self.assertIn('10.150.0.4', result['added'][0])

def test_removed_1_link(self):
old = OlsrParser(links3)
new = OlsrParser(links2)
Expand Down