new files
authorShikhar Bhushan <shikhar@schmizz.net>
Mon, 20 Apr 2009 04:24:20 +0000 (04:24 +0000)
committerShikhar Bhushan <shikhar@schmizz.net>
Mon, 20 Apr 2009 04:24:20 +0000 (04:24 +0000)
git-svn-id: http://ncclient.googlecode.com/svn/trunk@28 6dbcf712-26ac-11de-a2f3-1373824ab735

ncclient/capability.py [new file with mode: 0644]
ncclient/error.py [new file with mode: 0644]
ncclient/listener.py [new file with mode: 0644]
ncclient/operations.py [new file with mode: 0644]

diff --git a/ncclient/capability.py b/ncclient/capability.py
new file mode 100644 (file)
index 0000000..7d4c03f
--- /dev/null
@@ -0,0 +1,67 @@
+# Copyright 2009 Shikhar Bhushan
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+class Capabilities:
+    
+    def __init__(self, capabilities=None):
+        self._dict = {}
+        if isinstance(capabilities, dict):
+            self._dict = capabilities
+        elif isinstance(capabilities, list):
+            for uri in capabilities:
+                self._dict[uri] = Capabilities.guess_shorthand(uri)
+    
+    def __contains__(self, key):
+        return ( key in self._dict ) or ( key in self._dict.values() )
+    
+    def __repr__(self):
+        elems = ['<capability>%s</capability>' % uri for uri in self._dict]
+        return ('<capabilities>%s</capabilities>' % ''.join(elems))
+    
+    def add(self, uri, shorthand=None):
+        if shorthand is None:
+            shorthand = Capabilities.guess_shorthand(uri)
+        self._dict[uri] = shorthand
+    
+    def remove(self, key):
+        if key in self._dict:
+            del self._dict[key]
+        else:
+            for uri in self._dict:
+                if self._dict[uri] == key:
+                    del self._dict[uri]
+                    break
+    
+    @staticmethod
+    def guess_shorthand(uri):
+        if uri.startswith('urn:ietf:params:netconf:capability:'):
+            return (':' + uri.split(':')[5])
+    
+CAPABILITIES = Capabilities([
+    'urn:ietf:params:netconf:base:1.0',
+    'urn:ietf:params:netconf:capability:writable-running:1.0',
+    'urn:ietf:params:netconf:capability:candidate:1.0',
+    'urn:ietf:params:netconf:capability:confirmed-commit:1.0',
+    'urn:ietf:params:netconf:capability:rollback-on-error:1.0',
+    'urn:ietf:params:netconf:capability:startup:1.0',
+    'urn:ietf:params:netconf:capability:url:1.0',
+    'urn:ietf:params:netconf:capability:validate:1.0',
+    'urn:ietf:params:netconf:capability:xpath:1.0',
+    'urn:ietf:params:netconf:capability:notification:1.0',
+    'urn:ietf:params:netconf:capability:interleave:1.0'
+    ])
+
+if __name__ == "__main__":
+    assert(':validate' in CAPABILITIES) # test __contains__
+    print CAPABILITIES # test __repr__
\ No newline at end of file
diff --git a/ncclient/error.py b/ncclient/error.py
new file mode 100644 (file)
index 0000000..4ac088e
--- /dev/null
@@ -0,0 +1,21 @@
+# Copyright 2009 Shikhar Bhushan
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+class ClientError(Exception):
+    
+    pass
+
+class NETCONFError(ClientError):
+    
+    pass
\ No newline at end of file
diff --git a/ncclient/listener.py b/ncclient/listener.py
new file mode 100644 (file)
index 0000000..14e1c16
--- /dev/null
@@ -0,0 +1,50 @@
+# Copyright 2009 Shikhar Bhushan
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from threading import Lock
+
+import logging
+
+logger = logging.getLogger('ncclient.listener')
+
+class Subject:
+        
+    def __init__(self, listeners=[]):
+        self._listeners = listeners
+        self._lock = Lock()
+    
+    def has_listener(self, listener):
+        with self._lock:
+            return (listener in self._listeners)
+    
+    def add_listener(self, listener):
+        with self._lock:
+            self._listeners.append(listener)
+    
+    def remove_listener(self, listener):
+        with self._lock:
+            try:
+                self._listeners.remove(listener)
+            except ValueError:
+                pass
+    
+    def dispatch(self, event, *args, **kwds):
+        with self._lock:
+            listeners = list(self._listeners)
+        for l in listeners:
+            logger.debug('dispatching [%s] to [%s]' % (event, l.__class__))
+            try:
+                getattr(l, event)(*args, **kwds)
+            except Exception as e:
+                logger.warning(e)
diff --git a/ncclient/operations.py b/ncclient/operations.py
new file mode 100644 (file)
index 0000000..3c6a4ca
--- /dev/null
@@ -0,0 +1,24 @@
+# Copyright 2009 Shikhar Bhushan
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from threading import Event
+
+from rpc import RPC
+
+class OperationError(NETCONFError): pass
+
+class Operation(RPC):
+    
+    def __init__(self, *args, **kwds):
+        RPC.__init__(self, *args, **kwds)