1## This file is part of Scapy
2## See http://www.secdev.org/projects/scapy for more informations
3## Copyright (C) Philippe Biondi <phil@secdev.org>
4## This program is published under a GPLv2 license
5
6"""
7Direct Access dictionary.
8"""
9
10from __future__ import absolute_import
11from __future__ import print_function
12from scapy.error import Scapy_Exception
13import scapy.modules.six as six
14from scapy.compat import *
15
16###############################
17## Direct Access dictionary  ##
18###############################
19
20def fixname(x):
21    if x and str(x[0]) in "0123456789":
22        x = "n_"+x
23    return x.translate("________________________________________________0123456789_______ABCDEFGHIJKLMNOPQRSTUVWXYZ______abcdefghijklmnopqrstuvwxyz_____________________________________________________________________________________________________________________________________")
24
25
26class DADict_Exception(Scapy_Exception):
27    pass
28
29class DADict:
30    def __init__(self, _name="DADict", **kargs):
31        self._name=_name
32        self.update(kargs)
33    def fixname(self,val):
34        return fixname(plain_str(val))
35    def __contains__(self, val):
36        return val in self.__dict__
37    def __getitem__(self, attr):
38        return getattr(self, attr)
39    def __setitem__(self, attr, val):
40        return setattr(self, self.fixname(attr), val)
41    def __iter__(self):
42        return (value for key, value in six.iteritems(self.__dict__)
43                if key and key[0] != '_')
44    def _show(self):
45        for k in self.__dict__:
46            if k and k[0] != "_":
47                print("%10s = %r" % (k,getattr(self,k)))
48    def __repr__(self):
49        return "<%s/ %s>" % (self._name," ".join(x for x in self.__dict__ if x and x[0]!="_"))
50
51    def _branch(self, br, uniq=0):
52        if uniq and br._name in self:
53            raise DADict_Exception("DADict: [%s] already branched in [%s]" % (br._name, self._name))
54        self[br._name] = br
55
56    def _my_find(self, *args, **kargs):
57        if args and self._name not in args:
58            return False
59        for k in kargs:
60            if k not in self or self[k] != kargs[k]:
61                return False
62        return True
63
64    def update(self, *args, **kwargs):
65        for k, v in six.iteritems(dict(*args, **kwargs)):
66            self[k] = v
67
68    def _find(self, *args, **kargs):
69         return self._recurs_find((), *args, **kargs)
70    def _recurs_find(self, path, *args, **kargs):
71        if self in path:
72            return None
73        if self._my_find(*args, **kargs):
74            return self
75        for o in self:
76            if isinstance(o, DADict):
77                p = o._recurs_find(path+(self,), *args, **kargs)
78                if p is not None:
79                    return p
80        return None
81    def _find_all(self, *args, **kargs):
82        return self._recurs_find_all((), *args, **kargs)
83    def _recurs_find_all(self, path, *args, **kargs):
84        r = []
85        if self in path:
86            return r
87        if self._my_find(*args, **kargs):
88            r.append(self)
89        for o in self:
90            if isinstance(o, DADict):
91                p = o._recurs_find_all(path+(self,), *args, **kargs)
92                r += p
93        return r
94    def keys(self):
95        return list(self.iterkeys())
96    def iterkeys(self):
97        return (x for x in self.__dict__ if x and x[0] != "_")
98    def __len__(self):
99        return len(self.__dict__)
100