1.修复了一个生成备份文件时间的bug

2.修复了写入S文件的bug。
This commit is contained in:
facat 2019-12-29 20:31:24 +08:00
parent 3a19a81224
commit e28328eaf7
2 changed files with 60 additions and 27 deletions

View File

@ -4,9 +4,10 @@ import shutil
import os.path import os.path
import re import re
def file_backup_time(): def file_backup_time():
now = datetime.datetime.now() now = datetime.datetime.now()
return now.strftime("%Y%I%d%H%M%S") return now.strftime("%Y%m%d%H%M%S")
def get_directory(file_path): def get_directory(file_path):
@ -23,7 +24,7 @@ def get_file_name_with_extention(file_path):
class SFileObject: class SFileObject:
def __init__(self, file_path): def __init__(self, file_path):
self._content = [] self._content = []
self._file_path = file_path self.file_path = file_path
self._read(file_path) self._read(file_path)
pass pass
@ -38,11 +39,11 @@ class SFileObject:
self._content.append(sep) self._content.append(sep)
def _save_backup(self, back_file_path): def _save_backup(self, back_file_path):
shutil.copy(self._file_path, back_file_path) shutil.copy(self.file_path, back_file_path)
def write(self, target_file_path): def write(self, target_file_path):
(file_name, ext) = get_file_name_with_extention(self._file_path) (file_name, ext) = get_file_name_with_extention(self.file_path)
src_file_dir = get_directory(self._file_path) src_file_dir = get_directory(self.file_path)
self._save_backup( self._save_backup(
"{dir}/{file_name}-{time}.{ext}".format( "{dir}/{file_name}-{time}.{ext}".format(
dir=src_file_dir, file_name=file_name, time=file_backup_time(), ext=ext dir=src_file_dir, file_name=file_name, time=file_backup_time(), ext=ext
@ -50,7 +51,8 @@ class SFileObject:
) )
with open(target_file_path, "wt") as file: with open(target_file_path, "wt") as file:
for content in self._content: for content in self._content:
file.write("{content}\n".format(content=" ".join(content))) _content=[str(x) for x in content]
file.write("{content}\n".format(content=" ".join(_content)))
def has(self, tower_number): # 塔位号是否存在S文件中。 def has(self, tower_number): # 塔位号是否存在S文件中。
for index, content in enumerate(self._content): for index, content in enumerate(self._content):
@ -66,6 +68,33 @@ class SFileObject:
content[index][7] = new_height content[index][7] = new_height
# 把所有S文件当做一个来操作
class SFileAsSingle:
def __init__(self, s_files):
self._s_files = s_files
# 考虑不同位置处出现塔号的情况
def has(self, tower_number) -> [(SFileObject, int)]:
s_file = self._s_files
ret = []
for s in s_file:
d_file_obj = SFileObject(s)
index = d_file_obj.has(tower_number)
if index > 0:
ret.append((d_file_obj, index))
return ret
def update_height(self, result: [(SFileObject, int)], new_height):
s_changed_record = {}
for s_file_obj, index in result:
s_file_obj.update_height(index, new_height)
s_changed_record[s_file_obj] = 1
for s_file_obj in s_changed_record:
s_output_file_path = s_file_obj.file_path
s_file_obj.write(s_output_file_path)
print("update S file {Ss}".format(Ss=s_output_file_path))
class TaFileObject: class TaFileObject:
def __init__(self, file_path, SFile): def __init__(self, file_path, SFile):
# self._content = [] # self._content = []
@ -97,7 +126,7 @@ class TaFileObject:
tower_height_record[tower_number] = tower_height tower_height_record[tower_number] = tower_height
return tower_height_record return tower_height_record
def sync_tower_height(self, new_contents, old_tower_height_record): def sync_tower_height_to_S(self, new_contents, old_tower_height_record):
updated = False updated = False
for content in new_contents: for content in new_contents:
sep = content sep = content
@ -112,31 +141,34 @@ class TaFileObject:
) )
) )
SFile = self._SFile SFile = self._SFile
norm_tower_number = tower_number.replace("G", "") s_as_single = SFileAsSingle(SFile)
for Ss in SFile: result = s_as_single.has(tower_number)
d_file_obj = SFileObject(Ss) if len(result) > 0:
index = d_file_obj.has(norm_tower_number) s_as_single.update_height(result, new_tower_height)
if index > 0:
d_file_obj.update_height(index, str(new_tower_height))
d_file_obj.write(Ss)
print("update S file {Ss}".format(Ss=Ss))
break
updated = True updated = True
return updated return updated
# 将S文件中的塔高同步到TA文件中。
def sync_tower_height_from_S(self):
pass
def start(self): def start(self):
file_path = self._file_path file_path = self._file_path
while True: while True: # 检测文件是否有变化的循环
new_contents = self._read(file_path) # 每一次循环都会重新读一遍TA文件 new_contents = self._read(file_path) # 每一次循环都会重新读一遍TA文件
if self._old_tower_height_record is None: if self._old_tower_height_record is None:
self._old_tower_height_record = self._make_tower_height_record( self._old_tower_height_record = self._make_tower_height_record(
new_contents new_contents
) )
if self.sync_tower_height(new_contents, self._old_tower_height_record): if self.sync_tower_height_to_S(new_contents, self._old_tower_height_record):
self._old_tower_height_record = self._make_tower_height_record( self._old_tower_height_record = self._make_tower_height_record(
new_contents new_contents
) )
input("press enter to continue\n") while True: # 接受输入的循环
key = input("press Y to synchronize TA to S\n")
if key == "y" or key == "Y":
break
print("key invalid.")
class CordinationObject: class CordinationObject:
@ -148,23 +180,24 @@ class CordinationObject:
if line.strip() == "": if line.strip() == "":
continue continue
sep = line.split(",") sep = line.split(",")
J_or_Z=sep[0][0] J_or_Z = sep[0][0]
pile=re.findall(r'[JZ]0*([0-9]+)',sep[0])[0] pile = re.findall(r"[JZ]0*([0-9]+)", sep[0])[0]
JZ_pile=J_or_Z+pile JZ_pile = J_or_Z + pile
# pile = sep[0][1:len(sep[0])] # pile = sep[0][1:len(sep[0])]
altitude = sep[1] altitude = sep[1]
mileage = sep[2] mileage = sep[2]
content.append((JZ_pile, altitude, mileage)) content.append((JZ_pile, altitude, mileage))
index = len(content) - 1 index = len(content) - 1
cord_dic[pile] = index#字典是不带J或者Z的 cord_dic[pile] = index # 字典是不带J或者Z的
self._content = content self._content = content
self._cord_dic = cord_dic self._cord_dic = cord_dic
def get_altitude(self,pile): def get_altitude(self, pile):
cord_dic=self._cord_dic cord_dic = self._cord_dic
if pile in cord_dic: if pile in cord_dic:
index=cord_dic[pile] index = cord_dic[pile]
return self._content[index][1] return self._content[index][1]
return None return None
def content(self): def content(self):
return self._content return self._content

View File

@ -9,7 +9,7 @@ if __name__ == "__main__":
r"d:\工程\灵州-青山\排位\68-72\S068.DAT", r"d:\工程\灵州-青山\排位\68-72\S068.DAT",
r"d:\工程\灵州-青山\排位\72-169\S072.DAT", r"d:\工程\灵州-青山\排位\72-169\S072.DAT",
] ]
ta_object = TaFileObject(r"d:\工程\灵州-青山\排位\道亨\现场排位\现场排位.TA", SFile) ta_object = TaFileObject(r"d:\工程\灵州-青山\排位\道亨\最终排位\最终排位.TA", SFile)
ta_object.start() ta_object.start()
# generate_ta_from_csv.generate_ta(r"d:\工程\灵州-青山\排位\道亨\最终排位\最终排位.TA",SFile,r'd:\工程\灵州-青山\排位\道亨\成果表.csv') # generate_ta_from_csv.generate_ta(r"d:\工程\灵州-青山\排位\道亨\最终排位\最终排位.TA",SFile,r'd:\工程\灵州-青山\排位\道亨\成果表.csv')
print("Finished.") print("Finished.")