本文整理汇总了Python中augeas.Augeas.add_transform方法的典型用法代码示例。如果您正苦于以下问题:Python Augeas.add_transform方法的具体用法?Python Augeas.add_transform怎么用?Python Augeas.add_transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类augeas.Augeas
的用法示例。
在下文中一共展示了Augeas.add_transform方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_configured_ifaces
# 需要导入模块: from augeas import Augeas [as 别名]
# 或者: from augeas.Augeas import add_transform [as 别名]
def get_configured_ifaces():
aug = Augeas(flags=Augeas.NO_MODL_AUTOLOAD)
aug.add_transform('interfaces', '/etc/network/interfaces')
aug.load()
base = '/files/etc/network/interfaces'
for m in aug.match('%s/iface' % base):
yield aug.get(m)
aug.close()
示例2: LVMConfig
# 需要导入模块: from augeas import Augeas [as 别名]
# 或者: from augeas.Augeas import add_transform [as 别名]
class LVMConfig(object):
def __init__(self, path="/etc/lvm/lvm.conf"):
self.path = path
# Augeas loads by default tons of unneeded lenses and configuration
# files. On my test host, it fails to load, trying to read my 500 MiB
# /etc/lvm/archive/.
#
# These are the standard LVM lens includes:
# /augeas/load/LVM/incl[1] /etc/lvm/lvm.conf
# /augeas/load/LVM/incl[2] /etc/lvm/backup/*
# /augeas/load/LVM/incl[3] /etc/lvm/archive/*.vg
#
# We need only the first entry to work with lvm.conf. Using customized
# load setup, as explained in
# https://github.com/hercules-team/augeas/wiki/Loading-specific-files
#
# Removing the archive and backup entries, we can load augeas in 0.7
# seconds on my test vm. Removing all other lenses shorten the time to
# 0.04 seconds.
log.debug("Loading LVM configuration from %r", path)
self.aug = Augeas(flags=Augeas.NO_MODL_AUTOLOAD | Augeas.SAVE_BACKUP)
self.aug.add_transform("lvm.lns", [path])
self.aug.load()
# Context manager interface
def __enter__(self):
return self
def __exit__(self, t, v, tb):
try:
self.close()
except Exception as e:
# Caller succeeded, raise the close error.
if t is None:
raise
# Caller has failed, do not hide the original error.
log.exception("Error closing %s: %s" % (self, e))
# Accessing list of strings
def getlist(self, section, option):
pat = "/files%s/%s/dict/%s/list/*/str" % (self.path, section, option)
matches = self.aug.match(pat)
if not matches:
return None # Cannot store/read empty list
return [self.aug.get(m) for m in matches]
def setlist(self, section, option, value):
log.debug("Setting %s/%s to %s", section, option, value)
opt_path = "/files%s/%s/dict/%s" % (self.path, section, option)
self.aug.remove(opt_path)
item_path = opt_path + "/list/%d/str"
for i, item in enumerate(value, 1):
self.aug.set(item_path % i, item)
# Accessing flat values (int, string)
def getint(self, section, option):
val = self._get_flat(section, option, "int")
return int(val) if val is not None else None
def setint(self, section, option, value):
self._set_flat(section, option, "int", str(value))
def getstr(self, section, option):
return self._get_flat(section, option, "str")
def setstr(self, section, option, value):
self._set_flat(section, option, "str", value)
def _get_flat(self, section, option, opt_type):
path = self._flat_path(section, option, opt_type)
return self.aug.get(path)
def _set_flat(self, section, option, opt_type, value):
log.debug("Setting %s/%s to %r", section, option, value)
path = self._flat_path(section, option, opt_type)
return self.aug.set(path, value)
def _flat_path(self, section, option, opt_type):
return "/files%s/%s/dict/%s/%s" % (
self.path, section, option, opt_type)
# Removing options
def remove(self, section, option):
log.debug("Removing %s/%s", section, option)
path = "/files%s/%s/dict/%s" % (self.path, section, option)
self.aug.remove(path)
# File operations
def save(self):
log.info("Saving new LVM configuration to %r, previous configuration "
"saved to %r",
self.path, self.path + ".augsave")
#.........这里部分代码省略.........
示例3: AugeasWrap
# 需要导入模块: from augeas import Augeas [as 别名]
# 或者: from augeas.Augeas import add_transform [as 别名]
class AugeasWrap(object):
_transform = 'interfaces'
_file = None
_attrs = []
_map = {}
_match = None
__aug = None
@property
def _aug(self):
if not self.__aug:
self.__aug = Augeas(flags=Augeas.NO_MODL_AUTOLOAD)
self.__aug.add_transform(self._transform, self._file)
self.__aug.load()
return self.__aug
_debug = False
def exists(self):
return bool(self.get())
def _abspath(self, path):
if not path or not (path.startswith('/augeas') or path.startswith('/files') or path.startswith('$')):
path = '%s%s' % (self._match, path or '')
return path or ''
def get(self, path=None):
ret = self._aug.get(self._abspath(path))
if self._debug:
logger.debug('get path=%s value=%s', self._abspath(path), ret)
return ret
def set(self, value, path=None):
value = str(value)
if self._debug:
logger.debug('set path=%s value=%s', self._abspath(path), value)
return self._aug.set(self._abspath(path), value)
def match(self, path=None):
if self._debug:
logger.debug('match path=%s', self._abspath(path))
return self._aug.match(self._abspath(path))
def remove(self, path=None):
if self._debug:
logger.debug('remove path=%s', self._abspath(path))
return self._aug.remove(self._abspath(path))
def insert(self, value, path=None, before=True):
value = str(value)
if self._debug:
logger.debug('insert path=%s value=%s', self._abspath(path), value)
return self._aug.insert(self._abspath(path), value, before=before)
def _print(self, path=None):
path = self._abspath(path)
get = self.get(path)
logger.info("[%s] = '%s'", path, get)
try:
for match in self.match('%s//*' % path):
logger.info("[%s] = '%s'", match, self._aug.get(match))
except RuntimeError:
pass
def _all_attrs(self):
return self._attrs + self._map.keys()
示例4: TestNode
# 需要导入模块: from augeas import Augeas [as 别名]
# 或者: from augeas.Augeas import add_transform [as 别名]
class TestNode(unittest.TestCase):
"""Tests the Node class"""
def setUp(self):
self.tmp = tempfile.mkdtemp()
self.root = "%s/root" % self.tmp
shutil.copytree("%s/fakeroot" % os.path.dirname(__file__), self.root)
self.aug = Augeas(root=self.root, flags=Augeas.NO_MODL_AUTOLOAD)
self.aug.add_transform("Nsswitch.lns", "/etc/nsswitch.conf")
self.aug.load()
class FakeParent:
def setpath(self):
return "/files/etc/nsswitch.conf"
self.parent = FakeParent()
def tearDown(self):
shutil.rmtree(self.tmp)
self.aug.close()
def test_setpath_self(self):
"""Test setpath generates path for '.' (itself)"""
n = aug2cmds.Node(self.aug, self.parent, uniqpaths=["."],
path="/files/etc/nsswitch.conf/database[1]")
self.assertEqual(n.setpath(),
"/files/etc/nsswitch.conf/database[.='passwd']")
def test_setpath_subnode(self):
"""Test setpath generates path for a subnode"""
n = aug2cmds.Node(self.aug, self.parent, uniqpaths=["service"],
path="/files/etc/nsswitch.conf/database[1]")
self.assertEqual(n.setpath(),
"/files/etc/nsswitch.conf/database[service='files']")
def test_setpath_and(self):
"""Test setpath generates path for multiple subpaths"""
n = aug2cmds.Node(self.aug, self.parent, uniqpaths=[".", "service"],
path="/files/etc/nsswitch.conf/database[1]")
self.assertEqual(n.setpath(),
"/files/etc/nsswitch.conf/database[.='passwd' and service='files']")
def test_basename(self):
"""Test basename understands Augeas paths"""
self.assertEqual(aug2cmds.Node.basename("/files"), "files")
self.assertEqual(aug2cmds.Node.basename("/files/test"), "test")
self.assertEqual(aug2cmds.Node.basename("/files/test[foo]"), "test")
self.assertEqual(aug2cmds.Node.basename(
"/files/test[foo/bar]"), "test")
def test_dirname(self):
"""Test dirname understands Augeas paths"""
self.assertEqual(aug2cmds.Node.dirname("/files"), "/")
self.assertEqual(aug2cmds.Node.dirname("/files/test"), "/files")
self.assertEqual(aug2cmds.Node.dirname("/files/test[foo]"), "/files")
self.assertEqual(aug2cmds.Node.dirname(
"/files/test[foo/bar]"), "/files")
def test_children_none(self):
"""Test when there are no children"""
n = aug2cmds.Node(self.aug, self.parent,
"/files/etc/nsswitch.conf/#comment[1]", ["."])
self.assertRaises(StopIteration, next, n.children())
def test_children(self):
"""Test new Node objects are created for children"""
n = aug2cmds.Node(self.aug, self.parent,
"/files/etc/nsswitch.conf/database[15]", ["."])
c = n.children()
sn = c.next()
self.assertEqual(sn.path,
"/files/etc/nsswitch.conf/database[15]/service[1]")
self.assertEqual(sn.setpath(),
"/files/etc/nsswitch.conf/database[.='aliases']/service[.='files']")
sn = c.next()
self.assertEqual(sn.path,
"/files/etc/nsswitch.conf/database[15]/service[2]")
self.assertEqual(sn.setpath(),
"/files/etc/nsswitch.conf/database[.='aliases']/service[.='nisplus']")
self.assertRaises(StopIteration, next, c)
def test_value(self):
"""Test value is retrieved"""
n = aug2cmds.Node(self.aug, self.parent, uniqpaths=["."],
path="/files/etc/nsswitch.conf/database[1]")
self.assertEqual(n.value(), "passwd")
def test_value_none(self):
"""Test when no value is set"""
n = aug2cmds.Node(self.aug, self.parent, uniqpaths=["."],
path="/files/etc/nsswitch.conf")
self.assertEqual(n.value(), None)