def _get_container() -> Mapping: 'get the sigil containers' from bookcontainer import BookContainer # type: ignore from inputcontainer import InputContainer # type: ignore from outputcontainer import OutputContainer # type: ignore from validationcontainer import ValidationContainer # type: ignore global wrapper wrapper = load_wrapper() # collect the containers return DictAttr( wrapper=wrapper, edit=BookContainer(wrapper), input=InputContainer(wrapper), validation=ValidationContainer(wrapper), output=OutputContainer(wrapper), )
def main(argv=unicode_argv()): if len(argv) != 5: failed( None, msg="Launcher: improper number of arguments passed to launcher.py") return -1 ebook_root = argv[1] outdir = argv[2] script_type = argv[3] target_file = argv[4] script_home = os.path.dirname(target_file) plugin_name = os.path.split(script_home)[-1] plugin_dir = os.path.dirname(script_home) script_module = os.path.splitext(os.path.basename(target_file))[0] # do basic sanity checking anyway if script_type not in SUPPORTED_SCRIPT_TYPES: failed(None, msg="Launcher: script type %s is not supported" % script_type) return -1 ok = unipath.exists(ebook_root) and unipath.isdir(ebook_root) ok = ok and unipath.exists(outdir) and unipath.isdir(outdir) ok = ok and unipath.exists(script_home) and unipath.isdir(script_home) ok = ok and unipath.exists(target_file) and unipath.isfile(target_file) if not ok: failed(None, msg="Launcher: missing or incorrect paths passed in") return -1 # update sys with path to target module home directory sys.path.append(script_home) # load and parse opf if present op = None opf_path = os.path.join(ebook_root, 'OEBPS', 'content.opf') if unipath.exists(opf_path) and unipath.isfile(opf_path): op = Opf_Parser(opf_path) # create a wrapper for record keeping and safety rk = Wrapper(ebook_root, outdir, op, plugin_dir, plugin_name) # get the correct container if script_type == 'edit': bc = BookContainer(rk) elif script_type == 'input': bc = InputContainer(rk) elif script_type == 'validation': bc = ValidationContainer(rk) else: bc = OutputContainer(rk) # start the target script ps = ProcessScript(script_type, script_module, bc) ps.launch() # get standard error and standard out from the target script successmsg = '' for data in ps.stdouttext: successmsg += unicode_str(data) successmsg = escapeit(successmsg) errorlog = '' for data in ps.stderrtext: errorlog += unicode_str(data) errorlog = escapeit(errorlog) # get the target's script wrapper xml resultxml = "".join(ps.wrapout) resultxml += "<msg>\n" if ps.exitcode == 0: resultxml += successmsg if _DEBUG: resultxml += errorlog else: if _DEBUG: resultxml += successmsg resultxml += errorlog resultxml += '</msg>\n</wrapper>\n' # write it to stdout and exit if PY3: sys.stdout.buffer.write(utf8_str(resultxml)) else: sys.stdout.write(utf8_str(resultxml)) return 0
def main(argv=sys.argv): if len(argv) != 5: failed(None, msg="Launcher: improper number of arguments passed to launcher.py") return -1 ebook_root = argv[1] outdir = argv[2] script_type = argv[3] target_file = argv[4] script_home = os.path.dirname(target_file) plugin_name = os.path.split(script_home)[-1] plugin_dir = os.path.dirname(script_home) script_module = os.path.splitext(os.path.basename(target_file))[0] # remap cssutils to css_parser try: import css_parser sys.modules['cssutils'] = css_parser except ImportError: pass # do basic sanity checking anyway if script_type not in SUPPORTED_SCRIPT_TYPES: failed(None, msg="Launcher: script type %s is not supported" % script_type) return -1 ok = os.path.exists(ebook_root) and os.path.isdir(ebook_root) ok = ok and os.path.exists(outdir) and os.path.isdir(outdir) ok = ok and os.path.exists(script_home) and os.path.isdir(script_home) ok = ok and os.path.exists(target_file) and os.path.isfile(target_file) if not ok: failed(None, msg="Launcher: missing or incorrect paths passed in") return -1 # update sys with path to target module home directory sys.path.append(script_home) # load and parse opf if present op = None cfg = '' with open(os.path.join(outdir, 'sigil.cfg'), 'rb') as f: cfg = f.read().decode('utf-8') cfg = cfg.replace("\r", "") cfg_lst = cfg.split("\n") opfbookpath = cfg_lst[0] opf_path = os.path.join(ebook_root, opfbookpath.replace("/", os.sep)) if os.path.exists(opf_path) and os.path.isfile(opf_path): op = Opf_Parser(opf_path, opfbookpath) # create a wrapper for record keeping and safety rk = Wrapper(ebook_root, outdir, op, plugin_dir, plugin_name) # get the correct container if script_type == 'edit': bc = BookContainer(rk) elif script_type == 'input': bc = InputContainer(rk) elif script_type == 'validation': bc = ValidationContainer(rk) else: bc = OutputContainer(rk) # start the target script ps = ProcessScript(script_type, script_module, bc) ps.launch() # get standard error and standard out from the target script successmsg = '' for data in ps.stdouttext: successmsg += _unicodestr(data) successmsg = escapeit(successmsg) errorlog = '' for data in ps.stderrtext: errorlog += _unicodestr(data) errorlog = escapeit(errorlog) # get the target's script wrapper xml resultxml = "".join(ps.wrapout) resultxml += "<msg>\n" if ps.exitcode == 0: resultxml += successmsg if _DEBUG: resultxml += errorlog else: resultxml += successmsg resultxml += errorlog resultxml += '</msg>\n</wrapper>\n' # write it to stdout and exit sys.stdout.flush sys.stdout.buffer.write(_utf8str(resultxml)) return 0