def extract_tex(self, target, source, env): """ Сканирование TEX-файла, в поиске зависимостей и всяких полезностей. """ deps = [] node = source[0] def relativizefile(relfile): """ return absolute path to file """ newrelfile = relfile.replace(r"\finkdir", path) newrelfile = newrelfile.replace(r"\projectpath", env.project_path) if os.path.commonprefix([newrelfile, env.project_path]) == "": newrelfile = os.path.join(path, newrelfile) return newrelfile contents = node.get_contents() path, filename = os.path.split(node.abspath) for rule in env.project_db["include_commands"]: formatrule = rule["file"] wantedext = "" if "ext" in rule: wantedext = rule["ext"] re_ = rule["re"] if formatrule == "": formatrule = r"%(relfile)s" for include in re_.finditer(contents): g = { 'projectpath': env.project_path, 'currentfilename': filename, 'currentpath': path, 'currentfile': filename, } idx = re_.groupindex.keys() for symname in idx: g[symname] = include.group(symname) relfile = formatrule % g if os.path.splitext(relfile)[1] != wantedext: relfile += wantedext relfile = relativizefile(relfile) relfile = os.path.realpath(relfile) deps.append(relfile) strdeps = [] for dep in deps: if type(dep) == type(""): strdeps.append(dep) else: for node in dep: strdeps.append(node.abspath) mystr = '\n'.join(strdeps) ut.string2file(mystr, target[0].abspath)
def extract_algorithms(ps_infile, env): """ Вытаскиваем части кода-алгоритмы """ import pygments from pygments.lexers import get_lexer_by_name from pygments.formatters import LatexFormatter algorithm_regexp = re.compile( r"(?ms)\#ALGORITHM\s+(?P<name>[a-zA-z-0-9]+)\s*(?P<code>.+?)\s*\#ENDALGORITHM") hideline_regexps = [re.compile(r"(?m)^.*\#hide *\n"), re.compile(r"(?m)\n.*\#hide *") ] ls = ut.file2string(ps_infile) for algorithm in algorithm_regexp.finditer(ls): algfilename = lib.get_target(ps_infile, algorithm.group('name')+".py") texfilename = lib.get_target(ps_infile, algorithm.group('name')+".tex") #lf = open(algfilename, 'w') code = algorithm.group('code') for r in hideline_regexps: code = re.sub(r, "", code) #code = lib.utf8anyway(code) #lf.write(code) #lf.close() #tempblock = os.path.join(tempfile.gettempdir(), tempfile.gettempprefix()) #ls = ''.join([env.project_db['paths']['python'], # r'\scripts\pygmentize -f latex -l python ', # ' -o "%(tempblock)s" "%(algfilename)s" ' % vars() ]) #os.system(ls) lexer = get_lexer_by_name('python') code = ut.unicodeanyway(code) latex_tokens = pygments.lex(code, lexer) # sblock = ut.file2string(tempblock) # from pygments.formatters import LatexFormatter latex_formatter = LatexFormatter(texcomments = True) latex = pygments.format(latex_tokens, latex_formatter) stexblock = r""" \documentclass{minimal} \usepackage{xecyr} \XeTeXdefaultencoding "utf-8" \XeTeXinputencoding "utf-8" \defaultfontfeatures{Mapping=tex-text} \setmonofont{Consolas} \usepackage{color} \usepackage{fancyvrb} \usepackage[russian,english]{babel} """ + latex_formatter.get_style_defs() + r""" \begin{document} """ + latex + r""" \end{document} """ ut.string2file(stexblock, texfilename, encoding='utf-8')
def meta2deps(self, target, source, env): strs = [] for src in source: srcstr = src.get_contents().replace('\r', '') strs.append(srcstr.strip()) srcstrs = srcstr.split('\n') for str in srcstrs: depsfile = lib.get_target(str, self.DEPS_FILE) if os.path.exists(depsfile): depsfilestr = ut.file2string(depsfile).strip() if depsfilestr: strs.append(depsfilestr) mystr = '\n'.join(strs) ut.string2file(mystr, target[0].abspath)
def pdfbeamlatex(target, source, env): """ PDFLatex-обработка для слайдов. """ filename = str(source[0].abspath) (path, nameext) = os.path.split(filename) (name, ext) = os.path.splitext(nameext) outpdf = os.path.join(path, "--obj", name) + ".pdf" outpdfsync = os.path.join(path, "--obj", name) + ".synctex" outlog = os.path.join(path, "--obj", name) + ".log" pdfsync = os.path.join(path, name) + ".synctex" texlog = os.path.join(path, name) + ".log" curdir = os.getcwd() beamdir = path os.chdir(path) ut.createdir("--obj") command = ''.join([ r'xelatex -synctex=-1 -file-line-error-style -output-directory="--obj" ', ' -interaction nonstopmode "', nameext, '"']) print os.environ["PATH"] print command #texfilter = tx.TeXFilter(path) cutefilter = mf.CuteFilter(path) out= "Mock:" + command out = ut.get_prog_output(command) out = cutefilter(out) print out.encode("utf8") env.warnings += cutefilter.warnings ut.string2file("\n".join(env.warnings), filename + ".warnings") outbcffile = os.path.join(path, "--obj", name) + ".bcf" if os.path.exists(outbcffile): os.chdir('--obj') command = os.path.join(env.project_db['paths']['tex'], 'biber "%(outbcffile)s"' % vars()) os.system(command) os.chdir('..') mkidx.makeindex(env, filename) shutil.copyfile(outpdf, target[0].abspath) if os.path.exists(outpdfsync): shutil.copy(outpdfsync, pdfsync) if os.path.exists(outlog): shutil.copy(outlog, texlog) os.chdir(curdir)
def tex2pdf(target, source, env): """ Translate SVG files to EPS """ (pathname, ext) = os.path.splitext(target[0].abspath) assert (ext in [".pdf"]) tmpname = os.path.join(tempfile.gettempdir(), tempfile.gettempprefix()) tmptexname = tmpname + ".tex" tmppdfname = tmpname + ".pdf" tmppcropname = tmpname + ".crop.pdf" shutil.copy(source[0].abspath, tmptexname) command = os.path.join( env.project_db['paths']['tex'], 'xelatex -interaction nonstopmode "%(tmptexname)s"' % vars()) curdir = os.getcwd() os.chdir(tempfile.gettempdir()) cutefilter = mf.CuteFilter(tempfile.gettempdir()) out = "Mock:" + command out = ut.get_prog_output(command) out = cutefilter(out) #print out.encode("utf8") os.system(command) print os.environ['PATH'] print "!!", command command = os.path.join( env.project_db['paths']['tex'], "".join(['pdfcrop "', tmppdfname, '" "', target[0].abspath, '"'])) os.system(command) os.chdir(curdir)
def __init__(self): self.start_time = time.time() self.time_limit = 36 * 2 self.debug_mode = True self.deadline = self.start_time + self.time_limit self.tex_template = r""" \documentclass{minimal} \usepackage{amssymb} \setlength\arraycolsep{10pt} \setlength\tabcolsep{16pt} \setlength\arrayrulewidth{.3pt} \usepackage{xecyr} \XeTeXdefaultencoding "utf-8" \XeTeXinputencoding "utf-8" \defaultfontfeatures{Mapping=tex-text} \setmonofont{Consolas} \usepackage{color} \usepackage{fancyvrb} \usepackage[russian,english]{babel} \begin{document} %(tex)s \end{document} """ self.lines = [] myfilename = sys._getframe(2).f_code.co_filename #pylint: disable=W0212 mypath, mynameext = os.path.split(myfilename) self.basename = os.path.splitext(mynameext)[0] self.objdir = os.path.join(mypath, "--obj", mynameext + ".obj") if not os.path.exists(self.objdir): ut.createdir(self.objdir) self.prefix = os.path.join(self.objdir, "") self.dotprefix = os.path.join(self.objdir, "dot") self.svgprefix = os.path.join(self.objdir, "svg") self.texprefix = os.path.join(self.objdir, "tex") self.options, args, parser = process_cmd_line() #pylint: disable=W0612 if self.options.outputfile: sys.stdout = open(self.options.outputfile, "w") self.experiment_scale = 10 self.experiment_trycount = 2 self.debug_mode = True self.sprite_count = 1 self.random_source = random.Random(time.time())
def main(): """ Call SumatraPDF View, try to synchronize line number """ filename = str(os.path.abspath(sys.argv[1])) (path, nameext) = os.path.split(filename) (name, ext) = os.path.splitext(nameext) respdf = os.path.join(path, name) + ".pdf" outpdf = os.path.join(path, "--obj", name) + ".pdf" outpdfsync = os.path.join(path, "--obj", name) + ".pdfsync" pdfsync = os.path.join(path, name) + ".pdfsync" curdir = os.getcwd() os.chdir(path) ut.createdir("--obj") command = ''.join([ r'xelatex -synctex=1 -file-line-error-style -output-directory="--obj" ', ' -interaction nonstopmode "', nameext, '"']) os.environ['PATH'] = ';'.join([r'c:\app\docstruct\xetex\bin\win32', os.environ['PATH'] ]) print os.environ["PATH"] print command texfilter = tx.TeXFilter(path) out = ut.get_prog_output(command) out = texfilter(out) print out.encode("utf8") shutil.copyfile(outpdf, respdf ) if os.path.exists(outpdfsync): foutpdfsync = open(outpdfsync, "r") fpdfsync = open(pdfsync, "w") for line in foutpdfsync.readlines(): if line.startswith("("): realpath = os.path.realpath(line[1:-1]) if not realpath[-4:-3] == ".": realpath = realpath + ".tex" newline = "(" + realpath.replace('\\','/') + "\n" else: newline = line fpdfsync.write(newline) fpdfsync.close() foutpdfsync.close() os.chdir(curdir)
def register_pdf(self, filename): if os.path.sep not in filename: filename = os.path.join(self.env.GetLaunchDir(), filename) pathname = os.path.splitext(os.path.abspath(filename))[0] path, name = os.path.split(pathname) target = os.path.realpath(pathname + ".pdf") source = os.path.realpath(pathname + ".tex") metafile = lib.get_target(source, self.META_FILE) depsfile = lib.get_target(source, self.DEPS_FILE) cmd = self.env.Command(depsfile, metafile, self.meta2deps) cmd = self.env.Command(metafile, source, self.extract_meta) cmd = self.env.Command(target, [source, depsfile], actions.pdfbeamlatex) auxfile = os.path.join(path, '--obj', name + '.aux') if not os.path.exists(auxfile): ut.createdir(os.path.split(auxfile)[0]) ut.string2file("", auxfile) self.env.Depends(cmd, auxfile) self.env.Precious(target)
def print_statements(self, statements, frametitle, prefix="", graph="digraph", ext="dot", reset=True, beforelaststatement=""): graph_body = "" graph_str = "" frame = 0 if reset: self.reset_tex() for s in statements: if s.strip().startswith("\\"): self.print_tex(s.encode("windows-utf8")) else: frame += 1 if frame == len(statements): graph_body += beforelaststatement graph_body_print = graph_body + beforelaststatement + s graph_body += s graph_str = """ %(graph)s G{ %(graph_body_print)s } """ % vars() graphname = self.dotprefix + prefix + '-%02d.%s' % (frame, ext) ut.string2file(graph_str, graphname) tex_str = ur""" \begin{frame} \frametitle{%(frametitle)s} \begin{center} \localPDF[height=0.9\paperheight, width=.95\paperwidth, keepaspectratio]{--obj/dot%(prefix)s-%(frame)02d.%(ext)s.obj/--obj/obj.svg.obj/--obj/obj} \end{center} \end{frame} """ % vars() self.print_tex(tex_str.encode("utf-8")) graphname = self.dotprefix + '-last.%s' % ext ut.string2file(graph_str, graphname)
def __call__(self, text_output): reg = r''.join([ r'(?P<block>(?P<prefix>[:/\\A-Z\w\d\.-]*?)[/\\]--obj[/\\]', r'(?P<mastername>[A-Z\d\w\.-]+)\.obj)' ]) chainre = re.compile(reg) def formatwarning(filename, line, message): marker = u"--->!!---> " warning = marker + ':'.join([filename, line, message]) prefix = "" for m in chainre.finditer(filename): f = os.path.join(prefix, m.group("prefix"), m.group("mastername")) warning += '\n' + marker + f + ':0: source file' prefix += os.path.join(prefix, m.group("block")) return warning def formatcommonwarning(message): marker = u"--->##---> " warning = marker + message return warning outputlines = [] for match in self.output_re.finditer(text_output): groups = match.groupdict() if groups['dummy']: dummy = ut.unicodeanyway(groups['dummy']) outputlines.append(u"" + dummy) else: if groups['file']: file_ = groups['file'] line_ = groups['line'] message_ = groups['message'] realpath = os.path.abspath(os.path.join(self.path, file_)) warning = formatwarning(realpath, line_, message_) if groups['pyfile']: realpath = groups['pyfile'] line_ = groups['pyline'] message_ = groups['pymessage'] warning = formatwarning(realpath, line_, message_) if groups['commonerror']: commonerror = groups['commonerror'] warning = None if self.suppress_warnings_re.match(commonerror): outputlines.append(u"" + commonerror) else: warning = formatcommonwarning(commonerror) if warning: outputlines.append(warning) self.warnings.append(warning) return '\n'.join(outputlines)
def __call__(self, tex_output): outputlines = [] for match in self.output_re.finditer(tex_output): groups = match.groupdict() if groups['dummy']: dummy = ut.unicodeanyway(groups['dummy']) outputlines.append(u"~~ " + dummy) else: realpath = os.path.abspath( os.path.join(self.path, groups['file'])) outputlines += [ u"--->!!---> " + ':'.join([realpath, groups['line'], groups['message']]) ] return '\n'.join(outputlines)
def python_run(target, source, env): """ Запуск Python-файла """ filename = str(source[0].abspath) (path, nameext) = os.path.split(filename) (name, ext) = os.path.splitext(nameext) extract_algorithms(filename, env) curdir = os.getcwd() os.chdir(path) scmd = ''.join([env.project_db['paths']['python'], '\\python "', filename, '" ', '--output="', path, '/--obj/', nameext, '.obj/log.out"', ]) #command = ('python "%s" --quiet --batchmode --output="%s/--obj/%s.obj/log.out"' # % (filename, path, nameext) ) cutefilter = mf.CuteFilter(path) out = ut.get_prog_output(scmd) out = cutefilter(out) print out.encode("utf8") env.warnings += cutefilter.warnings #pattern = "%s/*" % (os.path.split(target[0].abspath)[0]) #for f in glob.glob(pattern): # if os.path.splitext(f)[1] == ".svg": # svgtarget = lib.get_target(f,"svg.eps") # env.Command(svgtarget, f, [inkscape]) # if os.path.splitext(f)[1] in [".dot", ".neato", ".fdp"]: # svgfilename = lib.get_target(f,"obj.svg") # env.Command(svgfilename, f, [transformation.dot2svg]) # epsfilename = lib.get_target(f,"obj.eps") # env.Command(epsfilename, svgfilename, [inkscape]) # pdffilename = lib.get_target(f,"obj.pdf") # env.Command(pdffilename, svgfilename, [inkscape]) os.chdir(curdir)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Friendly version of Pylint Utility """ import sys import os import getpass import belonesox_tools.MiscUtils as ut if len(sys.argv) < 2: raise Exception("Задайте имя файла для проверки") ut.install_if_asked() class MyPyLint: """ Инкапсуляция работы с Pylintом """ def __init__(self, filepath): """ """ self.filepath = filepath self.pylintfilename = None self.path, self.nameext = os.path.split(filepath) curpath = self.path while True: filename = os.path.join(curpath, "pylint.rc")
def dummy(self, target, source, env): ut.string2file("", target[0].abspath)
def print_lines_to_tex(self): tex = "\n".join(self.lines) texdoc = self.tex_template % {'tex': tex} filename = self.texprefix + ".tex" ut.string2file(texdoc, filename)