text
stringlengths
0
828
r = __recursive_import(module)
member_dict = dict(inspect.getmembers(r))
sprinter_class = parent_class
for v in member_dict.values():
if inspect.isclass(v) and issubclass(v, parent_class) and v != parent_class:
if sprinter_class is parent_class:
sprinter_class = v
if sprinter_class is None:
raise SprinterException(""No subclass %s that extends %s exists in classpath!"" % (module, str(parent_class)))
return sprinter_class
except ImportError:
e = sys.exc_info()[1]
raise e"
1413,"def __recursive_import(module_name):
""""""
Recursively looks for and imports the names, returning the
module desired
>>> __recursive_import(""sprinter.formula.unpack"") # doctest: +ELLIPSIS
<module 'unpack' from '...'>
currently module with relative imports don't work.
""""""
names = module_name.split(""."")
path = None
module = None
while len(names) > 0:
if module:
path = module.__path__
name = names.pop(0)
(module_file, pathname, description) = imp.find_module(name, path)
module = imp.load_module(name, module_file, pathname, description)
return module"
1414,"def err_exit(msg, rc=1):
""""""Print msg to stderr and exit with rc.
""""""
print(msg, file=sys.stderr)
sys.exit(rc)"
1415,"def popen(self, cmd):
""""""Execute an external command and return (rc, output).
""""""
process = Popen(cmd, shell=True, stdout=PIPE, env=self.env)
stdoutdata, stderrdata = process.communicate()
return process.returncode, stdoutdata"
1416,"def read_file(self, infile):
""""""Read a reST file into a string.
""""""
try:
with open(infile, 'rt') as file:
return file.read()
except UnicodeDecodeError as e:
err_exit('Error reading %s: %s' % (infile, e))
except (IOError, OSError) as e:
err_exit('Error reading %s: %s' % (infile, e.strerror or e))"
1417,"def write_file(self, html, outfile):
""""""Write an HTML string to a file.
""""""
try:
with open(outfile, 'wt') as file:
file.write(html)
except (IOError, OSError) as e:
err_exit('Error writing %s: %s' % (outfile, e.strerror or e))"
1418,"def convert_string(self, rest):
""""""Convert a reST string to an HTML string.
""""""
try:
html = publish_string(rest, writer_name='html')
except SystemExit as e:
err_exit('HTML conversion failed with error: %s' % e.code)
else:
if sys.version_info[0] >= 3:
return html.decode('utf-8')
return html"
1419,"def apply_styles(self, html, styles):
""""""Insert style information into the HTML string.
""""""
index = html.find('</head>')
if index >= 0:
return ''.join((html[:index], styles, html[index:]))
return html"
1420,"def publish_string(self, rest, outfile, styles=''):
""""""Render a reST string as HTML.
""""""
html = self.convert_string(rest)
html = self.strip_xml_header(html)
html = self.apply_styles(html, styles)
self.write_file(html, outfile)
return outfile"
1421,"def publish_file(self, infile, outfile, styles=''):
""""""Render a reST file as HTML.
""""""
rest = self.read_file(infile)
return self.publish_string(rest, outfile, styles)"
1422,"def upgrade(self):
""""""Upgrade the config file.
""""""
warn('Upgrading ' + self.filename)
if self.backup_config(self.filename):
return self.write_default_config(self.filename)
return False"