Add header Examples

develop
Самоукин Алексей 14 years ago
parent d966c5d518
commit 82fbe6b9e1

@ -17,6 +17,7 @@
import optparse
import sys
import re
import textwrap
from cl_lang import lang
tr = lang()
@ -80,6 +81,95 @@ def make_option(shortOption=None,longOption=None,
help=help, default=default,
choices=choices)
class ShareHelpFormatter:
"""Общие методы форматирования help-а"""
def format_description(self, description):
if description:
lines = map(lambda x: self._format_text(x), description.split("\n"))
return "\n".join(lines)
else:
return ""
def get_example_header(self):
return _("Examples") + ":\n"
def format_examples(self, examples, comment=""):
if examples:
# "Examples:"
header = self.get_example_header()
if comment:
comment = "%s" % comment
backup_width = self.width
self.width = self.width - self.indent_increment - 2
lines = []
for line in comment.split("\n"):
lines.append(self._format_text(line))
self.width = backup_width
lines = ("\n".join(lines)).split("\n")
out_lines = []
for line in lines:
if line.strip():
out_lines.append(" "*self.indent_increment+"# "+line)
else:
out_lines.append(line)
comment = "\n".join(out_lines)
text_examples = "%s\n%s%s" \
%(comment," "*self.indent_increment, examples)
else:
text_examples = "%s%s" %(" "*self.indent_increment,examples)
return header + text_examples + "\n"
else:
return ""
class TitledHelpFormatter(ShareHelpFormatter, optparse.TitledHelpFormatter):
"""Форматирование вывода помощи с заголовками"""
def __init__(self,
indent_increment=2,
max_help_position=24,
width=None,
short_first=1):
optparse.TitledHelpFormatter.__init__(self, indent_increment,
max_help_position, width,
short_first)
def format_usage(self, usage):
if usage:
return "%s%s%s" % (self.format_heading(_("Usage")),
" "*self.indent_increment,
usage)
else:
return ""
def get_example_header(self):
return self.format_heading(_("Examples"))
def format_description(self, description):
description = ShareHelpFormatter.format_description(self, description)
if description:
return " "*self.indent_increment + description
else:
return ""
class IndentedHelpFormatter(ShareHelpFormatter, optparse.IndentedHelpFormatter):
"""Форматирование вывода стандартный класс"""
def __init__(self,
indent_increment=2,
max_help_position=24,
width=None,
short_first=1):
optparse.IndentedHelpFormatter.__init__(self, indent_increment,
max_help_position, width,
short_first)
def format_usage(self, usage):
if usage:
return _("Usage") + ": %s" % usage
else:
return ""
class opt(optparse.OptionParser):
"""Class of options for calculate utilities
@ -143,29 +233,25 @@ class opt(optparse.OptionParser):
" 'never', 'always', "+ _("or") + " 'auto'"
}]
def __init__(self,
usage=None,
option_list=None,
option_class=optparse.Option,
version=None,
conflict_handler="error",
description=None,
formatter=None,
add_help_option=True,
prog=None,
epilog=None,
package=None):
def __init__(self, package=None, version=None,
usage=None, examples=None, description=None,
option_list=[],
epilog=None,
comment_examples=None,
formatter=IndentedHelpFormatter()):
"""Установка опций командной строки"""
self.package = package
self.examples = examples
self.comment_examples = comment_examples
optparse.OptionParser.__init__(self,
usage=usage,
option_list=[make_option(**i) for i in option_list],
option_class=option_class,
option_class=optparse.Option,
version=version,
conflict_handler=conflict_handler,
conflict_handler="error",
description=description,
formatter=formatter,
add_help_option=add_help_option,
prog=prog,
#formatter=TitledHelpFormatter(),
epilog=epilog)
self.formatter._long_opt_fmt = "%s %s"
@ -173,10 +259,22 @@ class opt(optparse.OptionParser):
"""Replace standard help header"""
if self.usage:
return "%s %s\n\n%s"%(self.package,self.version,
self.formatter.format_usage(self.expand_prog_name(self.usage)))
self.formatter.format_usage(self.expand_prog_name(self.usage)))
else:
return ""
def get_description(self):
"""Replace standard help header"""
if self.examples:
if self.comment_examples:
text_examples = self.formatter.format_examples(self.examples,
self.comment_examples)
else:
text_examples = self.formatter.format_examples(self.examples)
return self.description + "\n\n" +\
self.expand_prog_name(text_examples)
return self.description + "\n"
def format_help(self, formatter=None):
"""Decode format help from utf-8 to unicode"""
return \

@ -200,7 +200,7 @@ in a sambaDomainName',
# Если No
elif resRestore[0] == "No":
try:
removeDir(resRestore[1]):
removeDir(resRestore[1])
except:
flagError = True
if not flagError:

Loading…
Cancel
Save