def main(logger=mylog.default_logger()): # load arguments and logger arguments = docopt(__doc__, version='0.0') # script self name self_name = os.path.basename(sys.argv[0]) # log logfile = self_name.replace('py', 'log') logger = set_mylogger(arguments, logfile) # load config # main_config=load_config('.ll') # set filename varibles # main_title=os.path.basename(os.getcwd()) # markdown_file_name=os.path.join(os.getcwd(),'build',main_title+'.markdown') # docx_file_name=main_title+'.docx' # rtf_file_name=main_title+'.rtf' latexmk_arg = [ '-pdf', '-f', '-quiet', '-e', r'$pdflatex=q/xelatex %O -interaction=nonstopmode %S/' ] if arguments['--quiet']: latexmk_arg.append('-quiet') if arguments['<tex_file_name>']: latexmk_arg.append(arguments['<tex_file_name>']) try: sh.latexmk(*latexmk_arg) except Exception, e: pass pdf_file_name = arguments['<tex_file_name>'].replace('tex', 'pdf') if arguments['--view'] and os.path.exists(pdf_file_name): sh.open(pdf_file_name)
def do_edit(self, arg, arguments): """Ussage: edir FILENAME Arguments: FILENAME the file to edit Edits a file.""" filename = arg if platform.system() == 'Darwin': # touch filename if not os.path.exists(filename): file(filename, 'w+').close() editors = [ "/Applications/Aquamacs.app", "/Applications/Emacs.app", "/usr/bin/emacs" ] for editor in editors: if os.path.exists(editor): open("-a", editor, filename) return print "ERROR: Could not find working editor in", editors
def main(logger=mylog.default_logger()): # load arguments and logger arguments = docopt(__doc__, version='0.0') # script self name self_name=os.path.basename(sys.argv[0]) # log logfile=self_name.replace('py','log') logger=set_mylogger(arguments,logfile) # load config # main_config=load_config('.ll') # set filename varibles # main_title=os.path.basename(os.getcwd()) # markdown_file_name=os.path.join(os.getcwd(),'build',main_title+'.markdown') # docx_file_name=main_title+'.docx' # rtf_file_name=main_title+'.rtf' latexmk_arg=['-pdf','-f','-quiet','-e',r'$pdflatex=q/xelatex %O -interaction=nonstopmode %S/'] if arguments['--quiet']: latexmk_arg.append('-quiet') if arguments['<tex_file_name>']: latexmk_arg.append(arguments['<tex_file_name>']) try: sh.latexmk(*latexmk_arg) except Exception, e: pass pdf_file_name=arguments['<tex_file_name>'].replace('tex','pdf') if arguments['--view'] and os.path.exists(pdf_file_name): sh.open(pdf_file_name)
def open_default(path): if platform.system() == 'Darwin': sh.open(path) elif os.name == 'nt': os.startfile(path) elif os.name == 'posix': run = sh.Command('xdg-open') run(path)
def show_latex(doctext, cleanup=False): """ Show a pdf of a LaTeX document with doctext. Only really expected to work on OS X. """ import sh with tempfile.NamedTemporaryFile(mode='w+t', encoding='utf-8') as docout: print(doctext, file=docout) docout.flush() sh.pdflatex(docout.name) output_filename = os.path.basename(docout.name) + ".pdf" sh.open(output_filename) if cleanup: sh.rm(output_filename)
def xcode(self): parser = argparse.ArgumentParser(description="Open the xcode project") parser.add_argument("filename", help="Path to your project or xcodeproj") args = parser.parse_args(sys.argv[2:]) filename = args.filename if not filename.endswith(".xcodeproj"): # try to find the xcodeproj from glob import glob xcodeproj = glob(join(filename, "*.xcodeproj")) if not xcodeproj: print("ERROR: Unable to find a xcodeproj in {}".format(filename)) sys.exit(1) filename = xcodeproj[0] sh.open(filename)
def genPhantom(url, imgName, config): # generate the specific headless browser screenshot res = config["resolution"] currOS = platform.system() + ' ' + platform.release() # get current os # add info about the metadata of the screenshot itself in the iamge name. # Use splittext to take out the extension. imgName = '{0}_PhantomJS_{1}_{2}_{3}.png'.format( os.path.splitext(imgName)[0], currOS, res[0], res[1]) # take the screenshot and save png file to a directory sh.phantomjs( 'screenshotScript/capture.js', # where the capture.js script is url, # url for screenshot '{0}/{1}'.format(imgName), # img name res[0], # width res[1]) # height print('Generated image: {0}/{1}'.format(imgName)) sh.open('{0}/{1}'.format(imgName)) return
def main(): arguments = docopt(__doc__, version='makemd 1.0') # print arguments # up to root if not up_to_main(): print( ".main file not exist, bort. Please creat a .main file in the main folder." ) return main_config = yaml.load(open(".main", 'r')) if not main_config: main_config = {} if arguments.get('-l') or arguments.get('--log'): logger = mylog.set_logger(filename='makemd.log', level=mylog.logging.INFO) elif arguments.get('-q') or arguments.get('--quiet'): logger = mylog.set_logger(filename='makemd.log', level=mylog.logging.ERROR) elif arguments.get('-d') or arguments.get('--debug'): logger = mylog.set_logger(filename='makemd.log', level=mylog.logging.DEBUG) else: logger = mylog.set_logger(level=mylog.logging.INFO) logger.debug(arguments) # load main_config if main_config.has_key('output_type_list'): logger.info('output_type_list are %s' % main_config['output_type_list']) ## A .main config file sample. using yaml. ## output_type_list: ## - latex_article ## #- latex_report ## #- rtf ## #- docx # set filename varibles main_title = os.path.basename(os.getcwd()) if not os.path.exists('.build'): sh.mkdir('.build') sh.cd('.build') if not os.path.exists('images') and os.path.exists('../images'): sh.ln('-s', '../images', './') markdown_file_name = os.path.join(os.getcwd(), main_title + '.markdown') # markdown_file_name=os.path.join(os.getcwd(),main_title+'.markdown') docx_file_name = main_title + '.docx' rtf_file_name = main_title + '.rtf' # generate main_title.markdown file markdown_file = open(markdown_file_name, 'w') # markdown_file.write('#'+os.path.basename(os.getcwd())+'\n') # sh.cd('..') dfs_dir_byplist(os.pardir, markdown_file, 0, logger) markdown_file.close() sh.open(markdown_file_name) markdown_file = open(markdown_file_name, 'r') if main_config.has_key('output_type_list'): # generate latex file if main_config['output_type_list'] and ( 'latex_report' in main_config['output_type_list'] or 'latex_article' in main_config['output_type_list']): content = markdown_file.read() encoding = chardet.detect(content)['encoding'] if encoding == 'utf-8' and 'latex_report' in main_config[ 'output_type_list']: # generate latex & pdf file by article mmd2tex(markdown_file_name, main_title, 'report', 'cn') if encoding == 'utf-8' and 'latex_article' in main_config[ 'output_type_list']: # generate latex & pdf file by article mmd2tex(markdown_file_name, main_title, 'article', 'cn') if encoding != 'utf-8' and 'latex_report' in main_config[ 'output_type_list']: mmd2tex(markdown_file_name, main_title, 'report', 'en') if encoding != 'utf-8' and 'latex_article' in main_config[ 'output_type_list']: # generate latex & pdf file by article mmd2tex(markdown_file_name, main_title, 'article', 'en') logger.info("tex & pdf file generated") # generate rtf file if main_config['output_type_list'] and 'rtf' in main_config[ 'output_type_list']: mmd2rtf(markdown_file_name, rtf_file_name) sh.open(rtf_file_name) logger.info("rtf file generated") # generate docx file if main_config['output_type_list'] and 'docx' in main_config[ 'output_type_list']: mmd2docx(markdown_file_name, docx_file_name) sh.open(docx_file_name) logger.info("docx file generated")
tex_file.close() _s = open(tex_file_name, 'r').read().replace('includegraphics{', 'includegraphics[width=\\textwidth]{') open(tex_file_name, 'w').write(_s) try: run_mytex = sh.Command("mytex.py") run_mytex(tex_file_name) except Exception, e: pass pdf_file_name = tex_file_name.replace('tex', 'pdf') if os.path.exists(pdf_file_name): sh.cd('..') if not os.path.exists(pdf_file_name): sh.ln('-s', os.path.join('latex', pdf_file_name)) sh.open(pdf_file_name) def mmd2rtf(markdown_file_name, rtf_file_name): sh.pandoc(markdown_file_name, f="markdown_mmd", t="rtf", o=rtf_file_name) def mmd2docx(markdown_file_name, docx_file_name): sh.pandoc(markdown_file_name, f="markdown_mmd", t="docx", o=docx_file_name) def add_quotation(s): return '\"' + s + '\"' def main():
def main(): arguments = docopt(__doc__, version='makemd 1.0') # print arguments # up to root if not up_to_main(): print(".main file not exist, bort. Please creat a .main file in the main folder.") return main_config=yaml.load(open(".main",'r')) if not main_config: main_config={} if arguments.get('-l') or arguments.get('--log'): logger=mylog.set_logger(filename='makemd.log', level=mylog.logging.INFO) elif arguments.get('-q') or arguments.get('--quiet'): logger=mylog.set_logger(filename='makemd.log', level=mylog.logging.ERROR) elif arguments.get('-d') or arguments.get('--debug'): logger=mylog.set_logger(filename='makemd.log', level=mylog.logging.DEBUG) else: logger=mylog.set_logger(level=mylog.logging.INFO) logger.debug(arguments) # load main_config if main_config.has_key('output_type_list'): logger.info('output_type_list are %s' % main_config['output_type_list']) ## A .main config file sample. using yaml. ## output_type_list: ## - latex_article ## #- latex_report ## #- rtf ## #- docx # set filename varibles main_title=os.path.basename(os.getcwd()) if not os.path.exists('.build'): sh.mkdir('.build') sh.cd('.build') if not os.path.exists('images') and os.path.exists('../images'): sh.ln('-s','../images','./') markdown_file_name=os.path.join(os.getcwd(),main_title+'.markdown') # markdown_file_name=os.path.join(os.getcwd(),main_title+'.markdown') docx_file_name=main_title+'.docx' rtf_file_name=main_title+'.rtf' # generate main_title.markdown file markdown_file=open(markdown_file_name, 'w') # markdown_file.write('#'+os.path.basename(os.getcwd())+'\n') # sh.cd('..') dfs_dir_byplist(os.pardir,markdown_file,0,logger) markdown_file.close() sh.open(markdown_file_name) markdown_file=open(markdown_file_name, 'r') if main_config.has_key('output_type_list'): # generate latex file if main_config['output_type_list'] and ('latex_report' in main_config['output_type_list'] or 'latex_article' in main_config['output_type_list']): content=markdown_file.read() encoding=chardet.detect(content)['encoding'] if encoding == 'utf-8' and 'latex_report' in main_config['output_type_list']: # generate latex & pdf file by article mmd2tex(markdown_file_name,main_title,'report','cn') if encoding == 'utf-8' and 'latex_article' in main_config['output_type_list']: # generate latex & pdf file by article mmd2tex(markdown_file_name,main_title,'article','cn') if encoding != 'utf-8' and 'latex_report' in main_config['output_type_list']: mmd2tex(markdown_file_name,main_title,'report','en') if encoding != 'utf-8' and 'latex_article' in main_config['output_type_list']: # generate latex & pdf file by article mmd2tex(markdown_file_name,main_title,'article','en') logger.info("tex & pdf file generated") # generate rtf file if main_config['output_type_list'] and 'rtf' in main_config['output_type_list']: mmd2rtf(markdown_file_name,rtf_file_name) sh.open(rtf_file_name) logger.info("rtf file generated") # generate docx file if main_config['output_type_list'] and 'docx' in main_config['output_type_list']: mmd2docx(markdown_file_name,docx_file_name) sh.open(docx_file_name) logger.info("docx file generated")
tex_file=open(tex_file_name,'w') tex_file.writelines(content_list) tex_file.close() _s=open(tex_file_name,'r').read().replace('includegraphics{','includegraphics[width=\\textwidth]{') open(tex_file_name,'w').write(_s) try: run_mytex=sh.Command("mytex.py") run_mytex(tex_file_name) except Exception, e: pass pdf_file_name=tex_file_name.replace('tex','pdf') if os.path.exists(pdf_file_name): sh.cd('..') if not os.path.exists(pdf_file_name): sh.ln('-s',os.path.join('latex',pdf_file_name)) sh.open(pdf_file_name) def mmd2rtf(markdown_file_name,rtf_file_name): sh.pandoc(markdown_file_name,f="markdown_mmd",t="rtf",o=rtf_file_name) def mmd2docx(markdown_file_name,docx_file_name): sh.pandoc(markdown_file_name,f="markdown_mmd",t="docx",o=docx_file_name) def add_quotation(s): return '\"'+s+'\"' def main(): arguments = docopt(__doc__, version='makemd 1.0') # print arguments
def main(image, name, local_ip_num, explicit_ip, clean, port): if not CONFIG_DIR.exists(): CONFIG_DIR.mkdir(parents=True) if not which('mitmproxy'): logger.critical("mitmproxy not in path. Aborting...") return -1 contents = CONFIG_DIR.glob("**/*") if clean: logger.info(f"Cleaning {CONFIG_DIR}") for f in contents: f.unlink() return logger.info(f"Config dir: {CONFIG_DIR}:") for f in CONFIG_DIR.glob("**/*"): logger.info(f"\t{f.name}") pem: Path = get_certs() try: container_id = sh.docker("ps", "--quiet", "--all", f"--filter=name={name}") except sh.CommandError: pass except sh.ErrorReturnCode as error: click.echo( "Docker returned an error while running. Is the service running?" f" {error}" ) sys.exit(1) container_id = container_id.strip() if container_id: logger.info(f"Stopping previous container {container_id}") sh.docker("rm", "-f", container_id) if not explicit_ip: local_ips = list(get_ips()) if not 0 <= local_ip_num <= len(local_ips): click.echo( message="-i/--local-ip-num must be between 0 and {len(local_ips)}" ) return 1 ip = local_ips[local_ip_num] else: ip = explicit_ip click.echo("Using IP: {}".format(ip)) if not xquartz_running(): sh.open("-a", "XQuartz") print(sh.Command("/opt/X11/bin/xhost")("+", ip)) generate_command() container_id = sh.docker( "run", "--rm", "-u", "root", # Transient, user "-v", f"{CONFIG_DIR.absolute()}:/config", # Certs, and command file volume "--entrypoint", "bash", # Change chromium to bash "-e", "QT_X11_NO_MITSHM=1", # Some options "-e", f"DISPLAY={ip}:0", # Display "-d", # Launch in the bg image, "/config/command.sh", # Last line image and command ).strip() sh.mitmproxy("--cert", CONFIG_DIR / "cert.pem", _fg=True) logger.info("Killing browser container") sh.docker('kill', container_id)