Revision a6c00291

b/ncclient/__init__.py
17 17
=====
18 18

  
19 19
- operations complete
20
- make operational again
21
- LockContext
22
- op specfic reply objects
23
- manager testing and augmenting
24
parse into dicts??
25

  
20
- parse into dicts??
21
- code freeze and reST doc
26 22
'''
27 23

  
28 24
import sys
b/ncclient/glue.py
41 41
    def __init__(self):
42 42
        "TODO: docstring"
43 43
        Thread.__init__(self)
44
        self.setDaemon(True)
44 45
        self._listeners = set() # TODO(?) weakref
45 46
        self._lock = Lock()
46 47
    
b/ncclient/manager.py
54 54
        else:
55 55
            return reply.data
56 56

  
57
    def request(op, *args, **kwds):
57
    def do(self, op, *args, **kwds):
58 58
        op = OPERATIONS[op](self._session)
59 59
        reply = op.request(*args, **kwds)
60 60
        if not reply.ok:
......
62 62
        return reply
63 63

  
64 64
    def locked(self, target='running'):
65
        return LockContext(self._session, target)
65
        return operations.LockContext(self._session, target)
66 66
    
67 67
    get = lambda self, *args, **kwds: self._get('get')
68 68
    
69 69
    get_config = lambda self, *args, **kwds: self._get('get-config')
70 70
    
71
    edit_config = lambda self, *args, **kwds: self.request('edit-config', *args, **kwds)
71
    edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
72 72
    
73
    copy_config = lambda self, *args, **kwds: self.request('copy-config', *args, **kwds)
73
    copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
74 74
    
75
    validate = lambda self, *args, **kwds: self.request('validate', *args, **kwds)
75
    validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
76 76
    
77
    commit = lambda self, *args, **kwds: self.request('commit', *args, **kwds)
77
    commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
78 78
    
79
    discard_changes = lambda self, *args, **kwds: self.request('discard-changes', *args, **kwds)
79
    discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
80 80
    
81
    delete_config = lambda self, *args, **kwds: self.request('delete-config', *args, **kwds)
81
    delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
82 82
    
83
    lock = lambda self, *args, **kwds: self.request('lock', *args, **kwds)
83
    lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
84 84
    
85
    unlock = lambda self, *args, **kwds: self.request('unlock', *args, **kwds)
85
    unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
86 86
    
87
    close_session = lambda self, *args, **kwds: self.request('close-session', *args, **kwds)
87
    close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
88 88
    
89
    kill_session = lambda self, *args, **kwds: self.request('kill-session', *args, **kwds)
89
    kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
90 90
    
91 91
    def close(self):
92 92
        try:
b/ncclient/operations/__init__.py
25 25
from retrieve import Get, GetConfig
26 26
from edit import EditConfig, CopyConfig, DeleteConfig, Validate, Commit, DiscardChanges
27 27
from session import CloseSession, KillSession
28
from lock import Lock, Unlock
28
from lock import Lock, Unlock, LockContext
29 29
from subscribe import CreateSubscription
30 30

  
31 31

  
......
42 42
    'DeleteConfig',
43 43
    'Lock',
44 44
    'Unlock',
45
    'LockContext',
45 46
    'CloseSession',
46 47
    'KillSession',
47 48
    'CreateSubscription',
b/ncclient/operations/edit.py
120 120
    
121 121
    SPEC = {'tag': 'discard-changes'}
122 122
    
123
    def request(self):
124
        return self._request(DiscardChanges.SPEC)
123
    request = lambda self: self._request(DiscardChanges.SPEC)
b/ncclient/operations/lock.py
14 14

  
15 15
'Locking-related NETCONF operations'
16 16

  
17
from copy import deepcopy
18

  
19 17
from ncclient.rpc import RPC
20 18

  
21
# TODO - a context manager around some <target> would be real neat
19
import util
22 20

  
23
class Lock(RPC): # x
21
class Lock(RPC):
24 22
    
25 23
    SPEC = {
26 24
        'tag': 'lock',
......
33 31
    def request(self, target='running'):
34 32
        if target=='candidate':
35 33
            self._assert(':candidate')
36
        spec = deepcopy(Lock.SPEC)
34
        spec = Lock.SPEC.copy()
37 35
        spec['children']['children']['tag'] = target
38 36
        return self._request(spec)
39 37

  
40 38

  
41
class Unlock(RPC): # x
39
class Unlock(RPC):
42 40
    
43 41
    SPEC = {
44 42
        'tag': 'unlock',
......
51 49
    def request(self, target='running'):
52 50
        if target=='candidate':
53 51
            self._assert(':candidate')
54
        spec = deepcopy(Unlock.SPEC)
52
        spec = Unlock.SPEC.copy()
55 53
        spec['children']['children']['tag'] = target
56
        return self._request(self.spec)
54
        return self._request(spec)
57 55

  
58 56

  
59 57
class LockContext:
b/ncclient/operations/retrieve.py
14 14

  
15 15
from ncclient.rpc import RPC, RPCReply
16 16

  
17
def build_filter(spec, type, criteria):
18
    filter = {
19
        'tag': 'filter',
20
        'attributes': {'type': type}
21
    }
22
    if type == 'subtree':
23
        filter['children'] = [criteria]
24
    elif type == 'xpath':
25
        filter['attributes']['select'] = criteria
26
    return filter
17
import util
27 18

  
28 19
class GetReply(RPCReply):
29 20
    
30 21
    def parse(self):
31 22
        RPCReply.parse(self)
23
    
24
    @property
25
    def data(self):
26
        return None
32 27

  
33 28
class Get(RPC):
34 29
    
......
42 37
    def request(self, filter=None):
43 38
        spec = Get.SPEC.copy()
44 39
        if filter is not None:
45
            #if filter[0] == 'xpath':
46
            #    self._assert(':xpath')
47
            spec['children'].append(build_filter(*filter))
40
            spec['children'].append(util.build_filter(*filter))
48 41
        return self._request(spec)
49 42

  
50
class GetReply(RPCReply):
51
    
52
    def parse(self):
53
        RPCReply.parse(self)
54

  
55
class GetConfig(RPC): # xx
43
class GetConfig(RPC):
56 44
    
57 45
    SPEC = {
58 46
        'tag': 'get-config',
59
        'children': [ { 'tag': 'source', 'children': {'tag': None } } ]
47
        'children': []
60 48
    }
61 49
    
62 50
    REPLY_CLS = GetReply
63 51
    
64 52
    def request(self, source=None, source_url=None, filter=None):
65
        self._one_of(source, source_url)
53
        util.one_of(source, source_url)
66 54
        spec = GetConfig.SPEC.copy()
67
        if source is not None:
68
            spec['children'][0]['children']['tag'] = source
69
        if source_url is not None:
70
            #self._assert(':url')
71
            spec['children'][0]['children']['tag'] = 'url'
72
            spec['children'][0]['children']['text'] = source_url        
55
        children = spec['children']
56
        children.append({'tag': 'source', 'children': util.store_or_url(source, source_url)})
73 57
        if filter is not None:
74
            #if filter[0] == 'xpath':
75
            #    self._assert(':xpath')
76
            spec['children'].append(build_filter(*filter))
58
            children.append(util.build_filter(*filter))
77 59
        return self._request(spec)
b/ncclient/operations/util.py
31 31
        node['tag'] = 'url'
32 32
        node['text'] = url
33 33
    return node
34

  
35
def build_filter(spec, type, criteria):
36
    filter = {
37
        'tag': 'filter',
38
        'attributes': {'type': type}
39
    }
40
    if type == 'subtree':
41
        filter['children'] = [criteria]
42
    elif type == 'xpath':
43
        filter['attributes']['select'] = criteria
44
    return filter
b/ncclient/rpc/rpc.py
38 38
        self._session = session
39 39
        try:
40 40
            for cap in self.DEPENDS:
41
                self.assert_capability(cap)
41
                self._assert(cap)
42 42
        except AttributeError:
43 43
            pass        
44 44
        self._async = async

Also available in: Unified diff