/usr/lib/python2.7/dist-packages/dhm/nestdict.py is in python-dhm 0.6-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | # nestdict.py
#
# Copyright 2002 Wichert Akkerman <wichert@deephackmode.org>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Calculate shared library dependencies
""" Nested dictionaries
This module implements the NestedDict class, which is a simple
tool to make it simpler to deal with nested dictionaries.
Here is a simple example showing how to use it::
dict=NestedDict()
dict["sql/username"]="guest"
dict["sql/password"]="secret"
print "Username: " + dict["sql"]["username"]
print "Password: " + dict["sql/password"]
"""
import types, UserDict
class NestedDict(UserDict.UserDict):
"""Nested dictionary class
@ivar seperator: seperator string
@type seperator: string
"""
def __init__(self, dict=None):
self.seperator="/"
UserDict.UserDict.__init__(self, dict)
def __getitem__(self, key):
keys=key.split(self.seperator)
top=self.data
while len(keys)>1:
top=top[keys[0]]
keys=keys[1:]
if type(top[keys[0]])==types.DictType:
return NestedDict(top[keys[0]])
else:
return top[keys[0]]
def __setitem__(self, key, item):
keys=key.split(self.seperator)
top=self.data
while len(keys)>1:
if not top.has_key(keys[0]):
top[keys[0]]={}
top=top[keys[0]]
keys=keys[1:]
top[keys[0]]=item
def __delitem__(self, key):
top=self.data
path=key.split(self.seperator)
(path, leaf)=(path[:-1], path[-1])
try:
for subkey in path:
top=top[subkey]
except KeyError:
raise KeyError, key
del top[leaf]
def has_key(self, key):
top=self.data
try:
for subkey in key.split(self.seperator):
top=top[subkey]
except KeyError:
return 0
return 1
if __name__=="__main__":
d=NestedDict()
d["sql/hostname"]="sql.your.domain"
d["sql/table"]="users"
d["radius/hostname"]="radius.your.domain"
d["pants"]="off"
print d
|