def build_callable_view(self, dotted_name): """ build the html for a class method """ # XXX we may want to have seperate func = get_obj(self.dsa, self.pkg, dotted_name) docstring = func.__doc__ if docstring: docstring = deindent(docstring) localname = func.__name__ argdesc = get_param_htmldesc(self.linker, func) excdesc = self.build_exception_description(dotted_name) valuedesc = self.build_callable_signature_description(dotted_name) sourcefile = inspect.getsourcefile(func) callable_source = self.dsa.get_function_source(dotted_name) # i assume they're both either available or unavailable(XXX ?) is_in_pkg = self.is_in_pkg(sourcefile) href = None text = 'could not get to source file' colored = [] if sourcefile and callable_source: enc = source_html.get_module_encoding(sourcefile) sep = get_linesep(callable_source) colored = [enumerate_and_color(callable_source.split(sep), func.func_code.co_firstlineno, enc)] relpath = get_rel_sourcepath(self.projroot, sourcefile, sourcefile) text = 'source: %s' % (relpath,) if is_in_pkg: href = self.linker.get_lazyhref(sourcefile, self.get_anchor(func)) csource = H.SourceSnippet(text, href, colored) cslinks = self.build_callsites(dotted_name) snippet = H.FunctionDescription(localname, argdesc, docstring, valuedesc, excdesc, csource, cslinks) return snippet
def test_get_encoding_ascii(): temp = py.test.ensuretemp('test_get_encoding') fpath = temp.join('ascii.py') fpath.write(str(py.code.Source("""\ def foo(): return 'foo' """))) # XXX I think the specs say we have to assume latin-1 here... assert get_module_encoding(fpath.strpath) == 'ISO-8859-1'
def test_get_encoding_matching_pattern_elsewhere(): temp = py.test.ensuretemp('test_get_encoding') fpath = temp.join('matching_pattern.py') fpath.write(str(py.code.Source("""\ #!/usr/bin/env python def foo(coding=None): pass """))) assert get_module_encoding(fpath.strpath) == 'ISO-8859-1'
def build_python_page(self, fspath): # XXX two reads of the same file here... not very bad (disk caches # and such) but also not very nice... enc = source_html.get_module_encoding(fspath.strpath) source = fspath.read() sep = get_linesep(source) colored = [enumerate_and_color(source.split(sep), 0, enc)] tag = H.PythonSource(colored) nav = self.build_navigation(fspath) return tag, nav
def test_get_encoding_for_real(): temp = py.test.ensuretemp('test_get_encoding') fpath = temp.join('utf-8.py') fpath.write(str(py.code.Source("""\ #!/usr/bin/env python # -*- coding: UTF-8 -*- def foo(): return 'föö' """))) assert get_module_encoding(fpath.strpath) == 'UTF-8'
def build_python_page(self, fspath): # XXX two reads of the same file here... not very bad (disk caches # and such) but also not very nice... enc = source_html.get_module_encoding(fspath.strpath) try: colored = [enumerate_and_color_module(fspath, enc)] except (KeyboardInterrupt, SystemExit): raise except Exception, e: #self.capture.err.writeorg('\ncompilation exception: %s\n' % (e,)) # problem building HTML with anchors; let's try without... source = fspath.read() sep = get_linesep(source) colored = [enumerate_and_color(source.split(sep), 0, enc)]
def gen_traceback(self, dotted_name, call_site): tbtag = H.CallStackDescription() obj = self.dsa.get_obj(dotted_name) for frame in call_site: lineno = frame.lineno - frame.firstlineno source = frame.source sourcefile = frame.filename tokenizer = source_color.Tokenizer(source_color.PythonSchema) mangled = [] source = str(source) sep = get_linesep(source) for i, sline in enumerate(source.split(sep)): if i == lineno: l = '-> %s' % (sline,) else: l = ' %s' % (sline,) mangled.append(l) if sourcefile: relpath = get_rel_sourcepath(self.projpath, sourcefile, sourcefile) linktext = '%s - line %s' % (relpath, frame.lineno + 1) # skip py.code.Source objects and source files outside of the # package is_code_source = self._reg_source.match(sourcefile) if (not is_code_source and self.is_in_pkg(sourcefile) and py.path.local(sourcefile).check()): enc = source_html.get_module_encoding(sourcefile) href = self.linker.get_lazyhref(sourcefile) sourcelink = H.a(linktext, href=href) else: enc = 'latin-1' sourcelink = H.div(linktext) colored = [enumerate_and_color(mangled, frame.firstlineno, enc)] else: sourcelink = H.div('source unknown (%s)' % (sourcefile,)) colored = mangled[:] tbtag.append(sourcelink) tbtag.append(H.div(*colored)) return tbtag