def get_streams(orig): """Get audio stream type""" streams = {} cmd = "ffprobe '{0}'".format(orig) cache = tools.Cache() try: output = subprocess.check_output( cmd, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as error: logger.error(error) logger.error(tools.to_unicode(error.output)) return None output = tools.to_unicode(output) for line in output.split("\n"): if cache(re.search(r".*Stream #0.(\d+).*: (.*?): (.+?)[, ].*", line)): stream, stype, encoding = cache.output.groups() if stype != "Audio": continue if encoding not in ENCODINGS: logger.error("Unknown encoding: {0}".format(encoding)) else: streams[int(stream)] = encoding return streams
def get_colname(colname): """convert column name from psql to mysql""" cache = tools.Cache() if cache(re.match("\"(.*)\"", colname)): return cache.output.group(1) else: return colname
def psql_mysql(orig, dest, **_kwargs): """Convert based on fobj""" tools.create_dir(dest, is_dir=False, remove=True) mysql = io.open(dest, "w") psql = io.open(orig, "r") cache = tools.Cache() for line in psql: line = strip(line) if cache(re.match(r"CREATE TABLE (.*) \(", line)): # table_rename table = cache.output.group(1) table_def(mysql, table, line, psql) elif line == "" or line.startswith("--"): pass elif (re.match("ALTER TABLE .*", line) or re.match("CREATE SEQUENCE .*", line) or re.match("DROP SEQUENCE .*", line) or re.match("DROP TABLE .*", line) or re.match("ALTER SEQUENCE .*", line) or re.match("SELECT .*", line) or re.match("SET .*", line)): while not line.endswith(";"): line = next(psql) line = strip(line) elif cache(re.match(r"COPY (.*) \((.*)\) FROM stdin;", line)): # table_rename table = cache.output.group(1) data_def(mysql, table, cache.output.group(2), psql) else: logger.error("Unknown line: {0}".format(repr(line))) mysql.close() return dest
def get_coldef(coldef): """convert column def from psql to mysql""" cache = tools.Cache() if coldef.endswith(","): coldef = coldef[:-1] if cache(re.match(r"character varying\((\d+)\)", coldef)): return "VARCHAR({0})".format(cache.output.group(1)) elif re.match("timestamp with time zone", coldef): return "TEXT" elif re.match("unknown", coldef): return "TEXT" else: return coldef
def get_params(orig): """Get params for this specific latex file""" cache = tools.Cache() options = [] params = [] with open(orig, "r") as fobj: for line in fobj: if cache(re.match("(%)?.*% +option: *(.*?) *$", line)): comment = cache.output.group(1) option = cache.output.group(2) if option not in options: params.append([option, "", comment is None]) options.append(option) return params
def mktex(orig, dest, options): """Write the latex doc based on the options""" orig_obj = io.open(orig, "r") cache = tools.Cache() dest_tex = os.path.join( os.path.dirname(dest), os.path.splitext(os.path.basename(dest))[0] + ".tex") dest_tex_obj = io.open(dest_tex, "w") for line in orig_obj: if cache(re.match("(%)? *(.*) +% +option: (.*)$", line)): _comment, cmd, option = cache.output.groups() if option.strip() in options: dest_tex_obj.write("{0} % option: {1}\n".format(cmd, option)) else: dest_tex_obj.write(line) orig_obj.close() dest_tex_obj.close() return dest_tex
def table_def(mysql, table, line, psql): """Convert table definition""" cache = tools.Cache() mysql.write("DROP TABLE IF EXISTS `{0}`;\n".format(table)) mysql.write("CREATE TABLE `{0}` (\n".format(table)) columns = [] for line in psql: line = strip(line) if line == ");": mysql.write(",\n".join(columns) + "\n);\n") break elif re.match("CONSTRAINT.*", line): pass elif cache(re.match("(.+?) (.*)$", line)): columns.append(" {0} {1}".format( get_colname(cache.output.group(1)), get_coldef(cache.output.group(2)))) else: sys.exit("Unknown column definition " + repr(line))
def ly_pdf(orig, dest, *_args, **_kwargs): """Convert lilypond to pdf""" cmd = "lilypond --ps --output={destbase} {orig}".format( orig=orig, destbase=os.path.splitext(dest)[0]) try: logger.debug("Running: {0}".format(cmd)) output = subprocess.check_output( cmd, stderr=subprocess.STDOUT, shell=True) output = tools.to_unicode(output) except subprocess.CalledProcessError as error: logger.error(error) logger.error(tools.to_unicode(error.output)) return None cache = tools.Cache() if cache(re.search("Layout output to `(.*)'", output, re.MULTILINE)): return os.path.join(os.path.dirname(dest), cache.output.group(1)) else: logger.error("ERROR: {0}".format(output)) return None
def extract(msg, fname, settings, dest_tex): """Extract info dict from raw latex output""" cache = tools.Cache() if cache(re.search(r"Output written on (.*) \((\d+) page.*", msg)): yield "fname", cache.output.group(1) yield "pages", int(cache.output.group(2)) msg = (msg[0:cache.output.start()] + msg[cache.output.end():]) while cache(re.search("No file (.*.bbl).*", msg)): yield "bibtex", cache.output.group(1) msg = (msg[0:cache.output.start()] + msg[cache.output.end():]) while (cache(re.search(r"No file .*\.(nav|aux).*", msg)) or cache(re.search(r"Label\(s\) may have changed.*", msg)) or cache(re.search(r"Rerun to get citations correct\.", msg))): yield "rerun", True msg = (msg[0:cache.output.start()] + msg[cache.output.end():]) for regex, debug in settings.items(): while cache(re.search(regex, msg)): index = cache.output.lastindex if cache.output.lastindex else 0 yield (debug, cache.output.group(index)) msg = (msg[0:cache.output.start(index)] + msg[cache.output.end(index):]) if msg.strip() != "": yield ( # main level "debug" if fname == "" else # (local) style files "debug" if fname.endswith("sty") else # local latex file "warning" if os.path.commonprefix([dest_tex, fname]) != "/" else # package/style file "debug", msg)
def ftype_mp4(fname, file_options): """File type for mp4 files is it video or audio""" cache = tools.Cache() cmd = "ffprobe {0}".format(pipes.quote(fname)) try: output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as error: logger.error(error) logger.error(tools.to_unicode(error)) return output = tools.to_unicode(output) streams = set() video = False for line in output.split("\n"): if cache(re.search(r".*Stream #0.\d+.*: (.*?): ([a-zA-Z0-9]*).*", line)): stype, encoding = cache.output.groups() if stype == "Video": video = True break if encoding not in ENCODINGS: logger.error("Unknown encoding: {0}".format(encoding)) streams.add(encoding) else: streams.add(ENCODINGS[encoding]) if video and not streams: return {file_options["mime_type"]} else: if video: print("TODO: has video, but also audio") return streams
import io import re import sys # pylint: disable=W0611 import configobj import os import logging from mconvert import tools ORIG = "text/lilypond-template" DEST = "text/lilypond-score", "text/lilypond-midi" CONVERT = "lyt_ly" ROOT = os.path.dirname(__file__) logger = logging.getLogger(__name__) cache = tools.Cache() def lyt_ly(orig, dest, *_args, **kwargs): """Convert lilypond template to lilypond""" ftypes = kwargs.get("tree").items[kwargs.get("step")]["ftypes"] midi = True if "text/lilypond-midi" in ftypes else False values = configobj.ConfigObj(os.path.join(ROOT, "lily.ini")) values.update(configobj.ConfigObj(orig)) for key in values.keys(): values[key] = (update_dynamics(values[key], values, midi) if "dynamics" in key else update_score(values[key])) parts = tools.SetList([
def parse_output(output): """Parse the latex output""" cache = tools.Cache() msg = "" # quote = False pos = 0 line = "" vals = [] start = "\000" for char in output + "\000": if pos == 79 and char == "\n": pos = 0 continue if char == "\n": pos = 0 line = "" else: pos += 1 line += char if (line.startswith(" ") or line.startswith("l.") or line.startswith("\\")): msg += char continue # if char == "`": # quote = True # elif char == "'": # quote = False # # if quote: # msg += char # continue if char in ["(", ")", "[", "]", "\000"]: key = None if (start == "(" and cache( re.match(r"([\/\.].*?)([\n ]|$)(.*)", msg, re.DOTALL))): key = cache.output.group(1) msg = cache.output.group(2) + cache.output.group(3) elif (start == "[" and cache(re.match(r"(\d+)(.*)", msg))): key = int(cache.output.group(1)) msg = cache.output.group(2) # elif start == "\000": # key = "START" # if key is None and start in ("(", "["): # vals[-1]["msg"] += vals[-1]["end"] + msg # vals[-1]["end"] = char # else: vals.append({"start": start, "key": key, "msg": msg, "end": char}) start = char msg = "" else: msg += char change = True while change: change = False for index, values in enumerate(vals): if ((values["start"] == "(" and values["end"] == ")" and values["key"] is None) or (values["start"] == "[" and values["end"] == "]" and values["key"] is None)): vals[index - 1]["msg"] += (values["start"] + values["msg"] + values["end"] + vals[index + 1]["msg"]) vals[index - 1]["end"] = vals[index + 1]["end"] del vals[index] del vals[index] change = True break return vals