本文整理汇总了Python中karesansui.lib.dict_op.DictOp.comment方法的典型用法代码示例。如果您正苦于以下问题:Python DictOp.comment方法的具体用法?Python DictOp.comment怎么用?Python DictOp.comment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类karesansui.lib.dict_op.DictOp
的用法示例。
在下文中一共展示了DictOp.comment方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _new_lines
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
def _new_lines(self,search_key,new_key):
try:
attrs = self.dop.get(self._module,search_key,with_attr=True)
action = attrs['action']
iscomment = attrs['comment']
val = attrs['value']
except:
action = self.dop.action(self._module,search_key)
iscomment = self.dop.iscomment(self._module,search_key)
val = self.dop.get(self._module,search_key)
pass
#print val
dop = DictOp()
dop.addconf('__',{})
if action == "delete":
dop.add('__',new_key,val)
dop.delete('__',new_key)
elif action == "set":
dop.set('__',new_key,val)
else:
dop.add('__',new_key,val)
if iscomment is True:
dop.comment('__',new_key)
#preprint_r(dop.getconf('__'))
new_lines = self._value_to_lines(dop.getconf('__'))
#print "\n".join(new_lines)
return new_lines
示例2: __init__
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
class shConfParser:
_delim = "="
_new_delim = "="
_comment = "#"
_multidefine = False
_reserved_key_prefix = "@"
_module = "sh_conf_parser"
def __init__(self,paths=[]):
self.dop = DictOp()
self.dop.addconf(self._module,{})
self.set_source_file(paths)
def set_delim(self, delim="="):
self._delim = delim
def set_new_delim(self, delim="="):
self._new_delim = delim
def set_comment(self, comment="#"):
self._comment = comment
def set_multidefine(self, multidefine=False):
self._multidefine = multidefine
def set_reserved_key_prefix(self, prefix="@"):
self._reserved_key_prefix = prefix
def set_source_file(self,paths=[]):
if type(paths) == str:
paths = [paths]
self.paths = paths
return True
def get_source_file(self):
return self.paths
def source_file(self):
return self.get_source_file()
def read_conf(self):
retval = {}
for _afile in self.source_file():
res = ConfigFile(_afile).read()
orders = []
for _aline in res:
_aline = _aline.rstrip('\r\n')
regex_str = "^(?P<comment>%s)?[ \t]*(?P<key>[^%s ]+)[ \t]*%s *(?P<value>.*)$" % (self._comment,self._delim,self._delim,)
regex = re.compile(r"%s" % regex_str)
m = regex.match(_aline)
if m:
comment = m.group('comment')
key = m.group('key')
value = m.group('value')
if not value.rfind(self._comment) == -1:
value = value[:value.rfind(self._comment)]
if self._multidefine and self.dop.isset(self._module,[_afile,key]) is True:
new_value = self.dop.get(self._module,[_afile,key])
if type(new_value) == str:
new_value = [new_value]
if not value in new_value:
new_value.append(value)
else:
new_value = value
self.dop.set(self._module,[_afile,key],new_value)
if not key in orders:
orders.append(key)
if comment is not None:
self.dop.comment(self._module,[_afile,key])
orders_key = "%sORDERS" % (self._reserved_key_prefix,)
self.dop.set(self._module,[_afile,orders_key],orders)
#self.dop.preprint_r(self._module)
return self.dop.getconf(self._module)
def _value_to_lines(self,value):
lines = []
for _k,_v in value.iteritems():
try:
if _v['action'] == "delete":
continue
except:
pass
try:
val = _v['value']
comment = False
#.........这里部分代码省略.........
示例3: write_conf
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
#self.dop.preprint_r(self._module)
return self.dop.getconf(self._module)
def write_conf(self,conf_arr={},extra_args=None,dryrun=False):
retval = True
try:
self.dop.addconf("parser",{})
self.dop.set("parser",[PARSER_RESOLV_CONF],conf_arr)
#self.dop.preprint_r("parser")
arr = self.dop.getconf("parser")
self.parser.write_conf(arr,dryrun=dryrun)
except:
pass
return retval
"""
"""
if __name__ == '__main__':
"""Testing
"""
parser = resolvParser()
dop = DictOp()
dop.addconf("dum",parser.read_conf())
dop.comment("dum","search")
dop.comment("dum","nameserver")
dop.add("dum","domain","example.com localdomain")
conf = dop.getconf("dum")
parser.write_conf(conf,dryrun=True)
示例4: process
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
def process(self):
(opts, args) = getopts()
chkopts(opts)
self.up_progress(10)
original_parser = iscsidParser()
new_parser = iscsidParser()
dop = DictOp()
dop.addconf("original", original_parser.read_conf())
dop.addconf("new", new_parser.read_conf())
self.up_progress(10)
dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_METHOD, opts.auth)
if opts.auth == ISCSI_CONFIG_VALUE_AUTH_METHOD_CHAP:
password = ""
if opts.password is not None:
password = opts.password
elif opts.password_file is not None and is_readable(opts.password_file):
try:
fp = open(opts.password_file, "r")
try:
fcntl.lockf(fp.fileno(), fcntl.LOCK_SH)
try:
password = fp.readline().strip("\n")
finally:
fcntl.lockf(fp.fileno(), fcntl.LOCK_UN)
self.up_progress(10)
finally:
fp.close()
except:
raise KssCommandException('Failed to read file. - target host=%s password_file=%s' \
% (opts.host,opts.password_file))
try:
os.remove(opts.password_file)
except:
raise KssCommandException('Failed to remove file. - target host=%s password_file=%s' \
% (opts.host,opts.password_file))
dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_METHOD, opts.auth)
dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_USER, opts.user)
dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD, password)
else:
dop.comment("new", ISCSI_CONFIG_KEY_AUTH_USER)
dop.comment("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD)
self.up_progress(10)
if opts.autostart:
dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_ON)
else:
dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_OFF)
new_parser.write_conf(dop.getconf("new"))
self.up_progress(10)
discovery_command_args = (ISCSI_CMD,
ISCSI_CMD_OPTION_MODE,
ISCSI_CMD_OPTION_MODE_DISCOVERY,
ISCSI_CMD_OPTION_TYPE,
ISCSI_CMD_OPTION_TYPE_SENDTARGETS,
ISCSI_CMD_OPTION_PORTAL,
opts.host
)
(discovery_rc,discovery_res) = execute_command(discovery_command_args)
self.up_progress(10)
original_parser.write_conf(dop.getconf("original"))
self.up_progress(10)
if discovery_rc != 0:
raise KssCommandException('Failed to add iSCSI. - host=%s message=%s' % (opts.host, discovery_res))
if discovery_res == []:
raise KssCommandException('Failed to add iSCSI. - host=%s message=No exist permit iSCSI disk for target.' % (opts.host))
for node_line in discovery_res:
if not node_line:
continue
try:
node = iscsi_parse_node(node_line)
except:
self.logger.warn('Failed to parse iSCSI discovery command response. message="%s"' % (node_line))
continue
self.logger.info("%s" % (iscsi_print_format_node(node)))
print >>sys.stdout, _("%s") % (iscsi_print_format_node(node))
return True
示例5: __init__
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
class xmlLikeConfParser:
_delim = "[ \t]+"
_new_delim = " "
_comment = "#"
_reserved_key_prefix = "@"
_indent = " "
_module = "xml_like_conf_parser"
_footer = "-- Generated by karesansui"
def __init__(self,paths=[]):
self.dop = DictOp()
self.dop.addconf(self._module,{})
self.set_source_file(paths)
self.opt_uni = ['PIDFile']
self.opt_multi = ['LoadPlugin','Include']
self.opt_sect = ['Directory','VirtualHost','View']
def set_opt_uni(self, opts):
self.opt_uni = opts
def set_opt_multi(self, opts):
self.opt_multi = opts
def set_opt_sect(self, opts):
self.opt_sect = opts
def set_delim(self, delim=" "):
self._delim = delim
def set_new_delim(self, delim=" "):
self._new_delim = delim
def set_comment(self, comment="#"):
self._comment = comment
def set_reserved_key_prefix(self, prefix="@"):
self._reserved_key_prefix = prefix
def set_footer(self, footer=""):
self._footer = footer
def set_source_file(self,paths=[]):
if type(paths) == str:
paths = [paths]
self.paths = paths
return True
def get_source_file(self):
return self.paths
def source_file(self):
return self.get_source_file()
def build_value(self, value=None, precomment=[], postcomment=None):
if type(precomment) == str:
precomment = [precomment]
return [value, [precomment, postcomment], ]
def read_conf(self):
retval = {}
orders_key = "%sORDERS" % (self._reserved_key_prefix,)
for _afile in self.source_file():
res = ConfigFile(_afile).read()
self.orders = []
self.dop.set(self._module,[_afile],self._read_conf(res))
self.dop.set(self._module,[_afile,orders_key],self.orders)
return self.dop.getconf(self._module)
def _read_conf(self,lines,level=0):
dop = DictOp()
dop.addconf("__",{})
pre_comment = [] # 設定の前のコメント リスト配列
post_comment = None # 設定行のコメント 文字列
_in_section = False
_res = []
for _aline in lines:
if _in_section is True:
regex = "[ \t]*(?P<comment>#*)[ \t]*</(?P<key>%s)>" % _section_key #'|'.join(self.opt_sect)
_regex = re.compile(r"%s" % regex)
m = _regex.match(_aline)
if m:
_comment = m.group('comment')
_key = m.group('key').strip()
values = self.build_value(self._read_conf(_res,level),pre_comment,post_comment)
dop.set("__",[_key,_section_val],values)
if _comment != "":
dop.comment("__",[_key,_section_val])
if level == 1:
self.orders.append([_key,_section_val])
pre_comment = []
post_comment = None
_in_section = False
#.........这里部分代码省略.........
示例6: _read_conf
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
def _read_conf(self,lines,level=0):
dop = DictOp()
dop.addconf("__",{})
pre_comment = [] # 設定の前のコメント リスト配列
post_comment = None # 設定行のコメント 文字列
_in_section = False
_res = []
for _aline in lines:
if _in_section is True:
regex = "[ \t]*(?P<comment>#*)[ \t]*</(?P<key>%s)>" % _section_key #'|'.join(self.opt_sect)
_regex = re.compile(r"%s" % regex)
m = _regex.match(_aline)
if m:
_comment = m.group('comment')
_key = m.group('key').strip()
values = self.build_value(self._read_conf(_res,level),pre_comment,post_comment)
dop.set("__",[_key,_section_val],values)
if _comment != "":
dop.comment("__",[_key,_section_val])
if level == 1:
self.orders.append([_key,_section_val])
pre_comment = []
post_comment = None
_in_section = False
_res = []
level = level - 1
else:
_res.append(_aline)
else:
_aline = _aline.rstrip('\r\n')
if _aline.strip() == "":
pre_comment.append(_aline)
continue
match = False
for _type in ['uni','multi','sect']:
exec("regex = '|'.join(self.opt_%s)" % _type)
if _type == "sect":
regex = "[ \t]*(?P<comment>#*)[ \t]*<(?P<key>%s)(?P<section>.*)>" % regex
elif _type == "multi":
regex = "[ \t]*(?P<comment>#*)[ \t]*(?P<key>%s)[ \t]+(?P<value>.+)" % regex
elif _type == "uni":
regex = "[ \t]*(?P<comment>#*)[ \t]*(?P<key>%s)[ \t]+(?P<value>.+)" % regex
_regex = re.compile(r"%s" % regex)
m = _regex.match(_aline)
if m:
match = True
_comment = m.group('comment')
_key = m.group('key').strip()
if _type == "sect":
_section_key = _key
_section_val = re.sub(r"[\"']","",m.group('section').strip())
_in_section = True
level = level + 1
elif _type == "multi":
_value = m.group('value').strip()
if _value.find(self._comment) > 0:
post_comment = _value[_value.find(self._comment):]
_value = re.sub("%s$" % post_comment, "", _value).rstrip()
values = self.build_value(_value,pre_comment,post_comment)
dop.set("__",[_key,_value],values)
if _comment != "":
dop.comment("__",[_key,_value])
if level == 0:
self.orders.append([_key,_value])
pre_comment = []
post_comment = None
elif _type == "uni":
_value = m.group('value').strip()
if _value.find(self._comment) > 0:
post_comment = _value[_value.find(self._comment):]
_value = re.sub("%s$" % post_comment, "", _value).rstrip()
values = self.build_value(_value,pre_comment,post_comment)
dop.set("__",[_key],values)
if _comment != "":
dop.comment("__",[_key])
if level == 0:
self.orders.append([_key])
pre_comment = []
post_comment = None
break
if match is False:
# ブラケットディレクティブのパラメータは除外する (よって、ブラケットディレクティブは全ての定義が必要!)
# example: "<undefined_directive 'foobar'>"
regex_exclude1 = "[ \t]*(?P<comment>#*)[ \t]*(?P<key>%s)[ \t]" % '|'.join(self.opt_sect)
_regex_exclude1 = re.compile(r"%s" % regex_exclude1)
# 未定義のパラメータの値がクオートせずにスペース区切りで3つ以上の値を指定している場合はコメント行とみなす
# example: "# Read this configuration file"
regex_exclude2 = "[ \t]*#+[ \t]*[^ \t]+([ \t]+[^ \t]+){3,}"
_regex_exclude2 = re.compile(r"%s" % regex_exclude2)
#.........这里部分代码省略.........
示例7: iscsidParser
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
retval = True
if conf_path is None:
conf_path = ISCSI_DEFAULT_CONFIG_PATH
try:
self.dop.addconf("parser",{})
self.dop.set("parser",[conf_path],conf_arr)
#self.dop.preprint_r("parser")
arr = self.dop.getconf("parser")
self.parser.write_conf(arr,dryrun=dryrun)
except:
pass
return retval
"""
"""
if __name__ == '__main__':
"""Testing
"""
parser = iscsidParser()
dop = DictOp()
dop.addconf("dum",parser.read_conf())
dop.add("dum",['key'],['value',[['','comment mae1','comment mae2'],'comment ato']])
dop.comment("dum",['node.session.iscsi.FastAbort'])
dop.uncomment("dum",['node.session.iscsi.FastAbort'])
conf = dop.getconf("dum")
#preprint_r(conf)
parser.write_conf(conf,dryrun=True)
示例8: process
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
def process(self):
(opts, args) = getopts()
chkopts(opts)
self.up_progress(10)
config_path = iscsi_get_config_path(opts.host, opts.iqn, opts.port, opts.tpgt)
parser = iscsidParser()
dop = DictOp()
dop.addconf("new", parser.read_conf(config_path))
self.up_progress(10)
dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_METHOD, opts.auth)
if opts.auth == ISCSI_CONFIG_VALUE_AUTH_METHOD_CHAP:
password = ""
if opts.password is not None:
password = opts.password
elif opts.password_file is not None and is_readable(opts.password_file):
try:
fp = open(opts.password_file, "r")
try:
fcntl.lockf(fp.fileno(), fcntl.LOCK_SH)
try:
password = fp.readline().strip("\n")
finally:
fcntl.lockf(fp.fileno(), fcntl.LOCK_UN)
self.up_progress(10)
finally:
fp.close()
except:
raise KssCommandException('Failed to read file. - target host=%s password_file=%s' \
% (opts.host,opts.password_file))
try:
os.remove(opts.password_file)
except:
raise KssCommandException('Failed to remove file. - target host=%s password_file=%s' \
% (opts.host,opts.password_file))
dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_USER, opts.user)
dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD, password)
else:
dop.comment("new", ISCSI_CONFIG_KEY_AUTH_USER)
dop.comment("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD)
self.up_progress(10)
if opts.autostart:
dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_ON)
else:
dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_OFF)
self.up_progress(10)
parser.write_conf(dop.getconf("new"), config_path)
self.up_progress(30)
self.logger.info("Updated iSCSI node. - host=%s iqn=%s" % (opts.host, opts.iqn))
print >>sys.stdout, _("Updated iSCSI node. - host=%s iqn=%s") % (opts.host, opts.iqn)
return True
示例9: modprobe_confParser
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
parser = modprobe_confParser()
# 読み込み
dop = DictOp()
dop.addconf("dum",parser.read_conf())
#########################################
# include と blacklist パラメータの場合
# 1、パラメータを追加する
new_key = '/path/to/include/file1'
new_value = '' # valueを空にセットする
dop.add("dum",["include",new_key],new_value)
# コメントにするなら
dop.comment("dum",["include",new_key])
new_key = '/path/to/include/file2'
new_value = '' # valueを空にセットする
dop.add("dum",["include",new_key],new_value)
# 2、パラメータを削除する
delete_key = '/path/to/include/file2'
dop.delete("dum",["include",delete_key])
"""
# こっちの方式は、_multi_paramをTrueにしたときだけ
# 1、パラメータを追加する
new_value = '/path/to/include/file'
if dop.isset("dum",["include"]):
示例10: __init__
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import comment [as 别名]
class commentDealParser:
_delim = "[ \t]+"
_new_delim = " "
_comment = "#"
_reserved_key_prefix = "@"
_module = "comment_deal_parser"
_footer = "-- Generated by karesansui"
def __init__(self,paths=[]):
self.dop = DictOp()
self.dop.addconf(self._module,{})
self.set_source_file(paths)
def set_delim(self, delim=" "):
self._delim = delim
def set_new_delim(self, delim=" "):
self._new_delim = delim
def set_comment(self, comment="#"):
self._comment = comment
def set_reserved_key_prefix(self, prefix="@"):
self._reserved_key_prefix = prefix
def set_footer(self, footer=""):
self._footer = footer
def set_source_file(self,paths=[]):
if type(paths) == str:
paths = [paths]
self.paths = paths
return True
def get_source_file(self):
return self.paths
def source_file(self):
return self.get_source_file()
def build_value(self, string, precomment=[], postcomment=None):
if type(precomment) == str:
precomment = [precomment]
return [string, [precomment, postcomment], ]
def read_conf(self):
retval = {}
for _afile in self.source_file():
res = ConfigFile(_afile).read()
orders = []
comment_1 = [] # 設定の前のコメント リスト配列
comment_2 = None # 設定行のコメント 文字列
for _aline in res:
_aline = _aline.rstrip('\r\n')
if _aline.strip() == "":
comment_1.append(_aline)
continue
if _aline.lstrip()[0:1] == self._comment:
footer_regex = re.compile(self._footer)
m = footer_regex.search(_aline)
if not m:
comment = _aline[_aline.rfind(self._comment):]
comment_1.append(comment)
continue
regex_str = "^(?P<key>[^ \t]+)%s(?P<value>.*)$" % (self._delim,)
regex = re.compile(r"%s" % regex_str)
m = regex.match(_aline)
if m:
key = m.group('key')
value = m.group('value')
if not value.rfind(self._comment) == -1:
comment_2 = value[value.rfind(self._comment):]
value = value[:value.rfind(self._comment)]
new_value = self.build_value(value,comment_1,comment_2)
if new_value is not False:
self.dop.set(self._module,[_afile,key],new_value)
orders.append(key)
comment_1 = []
comment_2 = None
if len(comment_1) > 0:
eof_key = "%sEOF" % (self._reserved_key_prefix,)
new_value = self.build_value("",comment_1,comment_2)
self.dop.set(self._module,[_afile,eof_key],new_value)
orders_key = "%sORDERS" % (self._reserved_key_prefix,)
self.dop.set(self._module,[_afile,orders_key],orders)
#self.dop.preprint_r(self._module)
return self.dop.getconf(self._module)
#.........这里部分代码省略.........