同步操作将从 飞行器智能感知与控制/python_turtle 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import os, sysimport timeimport multiprocessing, threadingimport loggingimport turtle# current file's pathfpath = os.path.dirname(os.path.abspath(__file__))def extname(fname):fa = os.path.splitext(fname)if len(fa) > 1:return fa[1].lower()else:return ""def filename(fname):return os.path.split(fname)[1]def loadModule(mpath):# get module's namemoduleName = os.path.splitext(os.path.split(mpath)[1])[0]# try load moduletry:# for Python 3.5+import importlib.utilspec = importlib.util.spec_from_file_location(moduleName, mpath)module = importlib.util.module_from_spec(spec)spec.loader.exec_module(module)return moduleexcept Exception as e:logging.error("Can not load module %s, error msg: %s" % (mpath, str(e)))return Nonedef getScriptList(fp):fl = os.listdir(fp)fileList = [f for f in fl if extname(f) == ".py"]fileList.sort()return fileListclass RunScript:def __init__(self, fname, txtfile):self.p = Noneself.fname = fnameself.txtfile = txtfileself.finished = Falsedef runScript_(self):mod = loadModule(self.fname)if mod:# createts = threading.Thread(target=self.saveRes)ts.start()# call `draw` functionprint(">>> load module: %s" % filename(self.fname))f = getattr(mod, "draw")f()# regist timer function, and do mainloopturtle.ontimer(self.drawFinished, 0)turtle.mainloop()def drawFinished(self):print("- draw finished!")self.finished = Truedef saveRes(self):while not self.finished:time.sleep(0.5)time.sleep(5)# do snapshotimgPath = os.path.join(fpath, "images")fbaseName = os.path.splitext(os.path.split(self.fname)[1])[0]imgName = fbaseName + ".png"imgFP = os.path.join(imgPath, imgName)cmd = 'shutter -e --min_at_startup --window="Python Turtle Graphics" -o "%s"' % imgFPprint("cmd: ", cmd)os.system(cmd)# do markdown text outputcfname = "codes/" + os.path.split(self.fname)[1]imgname = "images/" + imgNamemt = """code: [%s](%s)\n\n\n\n\n""" % (fbaseName, cfname, fbaseName, imgname)f = open(self.txtfile, "at")f.write(mt)f.close()# close drawing windowturtle.bye()print("- capture finished!")def runScript(self):self.p = multiprocessing.Process(name="run_script", target=self.runScript_)self.p.start()self.p.join()print("- script run finished!\n\n")if __name__ == "__main__":# code pathcodePath = os.path.join(fpath, "codes")mtfile = os.path.join(fpath, "gallery.md")# get code listif len(sys.argv) > 1:cl = sys.argv[1:]else:cl = getScriptList(codePath)print("code list: ", cl)print("")# run codesfor c in cl:s = RunScript(os.path.join(codePath, c), mtfile)s.runScript()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。