輸入信號
導入系統
從隨機導入randint
導入作業系統,筦道
從shuil導入rmtree
從shuil導入copyfile
導入子流程
PdfMaker類:
def命令解析(self,cmd):
fct={
“幫助”:self.helpmenu,
'?':自助選單,
“創建”:self.create,
“顯示”:self.show,
“compile”:self.compilePDF,
“flag”:self.flag
}.get(命令,self.unknown)
返回fct
def控制碼(自身):
自我初始化連接()
列印“歡迎來到p.d.f.maker!發送“?”或者“幫助”得到幫助。鍵入“exit”以斷開連接。“
指令計數器=0
while(指令計數器<77):
嘗試:
cmd=(原始輸入(“>”).strip().split()
如果len(命令)<1:
持續
如果命令[0]=“退出”:
self.endConnection()
返回
列印self.cmdparse(命令[0])(命令)
指令計數器+=1
除例外情况外,e:
列印“發生异常:”,e.args
self.endConnection()
打破
列印“達到最大指令數”
self.endConnection()
def initConnection(自身):
cwd=os.getcwd()
self.directory=cwd+“/tmp/”+str(randint(0,2**60))
當os.path.exists(self.directory)時:
self.directory=cwd+“/tmp/”+str(randint(0,2**60))
os.makedirs(self.directory)
標誌=self.directory+“/”+“33C3”+“%X”%randint(0,2**31)+“%X”%randint(0,2**31)
複製檔案(“標誌”,標誌)
定義端連接(自):
如果os.path.exists(self.directory):
rmtree(self.directory)
未知定義(self,cmd):
return“未知命令!鍵入“help”或“?”去尋求幫助!”
def幫助選單(self,cmd):
如果len(命令)<2:
return“可用命令:?,幫助,創建,顯示,編譯。\n鍵入'help COMMAND'獲取有關特定命令的資訊。”
如果(命令[1]=“創建”):
return(“創建檔案。語法:create TYPE NAME\n“
“TYPE:檔案的類型。可能的類型有log、tex、sty、mp、bib\n“
“NAME:檔案的名稱(不以類型結尾)\n”
“創建的檔名為name.TYPE”)
elif(命令[1]=“顯示”):
return(“顯示檔案的內容。語法:show TYPE NAME\n“
“TYPE:檔案的類型。可能的類型有log、tex、sty、mp、bib\n“
“NAME:檔名(不帶類型結尾)”)
elif(命令[1]=“編譯”):
return(“在pdflatex的幫助下編譯tex檔案。語法:編譯名\n“
“NAME:檔名(不帶類型結尾)”)
顯示定義(self,cmd):
如果長度(命令值)<3:
return“參數個數無效。鍵入“幫助顯示”以獲取更多資訊。“
如果在[“log”,“tex”,“sty”,“mp”,“bib”]中沒有命令[1]:
return“檔案結尾無效。只允許日誌、文字、sty和mp“
filename=cmd[2]+““+cmd[1]
full_filename=os.path.join(self.directory,檔名)
full_filename=os.path.abspath(完整檔名)
如果存在完整的檔名.startswith(self.directory)和os.path
打開(完整檔名,“r”)作為檔案:
content=file.read()
其他:
content=“找不到檔案。”
返回內容
定義標誌(self,cmd):
通過
定義創建(self,cmd):
如果長度(命令值)<3:
return“參數個數無效。鍵入“幫助創建”以獲取更多資訊。“
如果在[“log”,“tex”,“sty”,“mp”,“bib”]中沒有命令[1]:
return“檔案結尾無效。只允許日誌、文字、sty和mp“
filename=cmd[2]+““+cmd[1]
full_filename=os.path.join(self.directory,檔名)
full_filename=os.path.abspath(完整檔名)
如果不是完整的檔名.startswith(self.directory):
return“無法創建檔案。”
打開(完整檔名,“w”)作為檔案:
列印“檔案已創建。請立即鍵入內容,並通過發送僅包含“\q”的行來完成它。”
當1:
文字=原始輸入(“”);
如果text.strip(“\n”)=“\q”:
打破
write_to_file=True;
對於(“…”、“*”、“/”、“\\x”)中的篩選項:
如果在文字中篩選項目:
寫入檔案=False
打破
如果(寫入檔案):
file.write(文字+“\n”)
return“寫入”+filename+
def compilePDF(self,命令列):
如果(len(cmd)<2:
return“參數個數無效。鍵入“help compile”獲取更多資訊。“
filename=cmd[1]+“.tex”
full_filename=os.path.join(self.directory,檔名)
full_filename=os.path.abspath(完整檔名)
列印完整檔名
如果不是full_filename.startswith(self.directory)或不是os.path.exists(full_filename):
return“無法編譯檔案。”
print pipes.quote(完整檔名)
compile_command=“cd”+self.directory+“&&pdflatex”+pipes.quote(完整檔名)
compile_result=subprocess.check_output(compile_命令,shell=True)
返回編譯結果
def signal_handler_sigint(訊號,幀):
列印“退出…”
pdfmaker.endConnection()
系統出口(0)
如果uu name_uuuu==“uuu main_uuu”:
signal.signal(signal.SIGINT,訊號處理器SIGINT)
pdfmaker=pdfmaker()
pdfmaker.handle()