// // CharacterAutoTest.swift // KdanAuto // // Created by 朱东勇 on 2022/11/22. // import Foundation import Cocoa class StringAutoCompareTest : StringAutoTest { override class func shared() -> AutoTest? { return StringAutoCompareTest() } // Auto Test refrence Check File override func autoTest(_ complention:@escaping (_ object:AutoTest, _ report:NSAttributedString?) -> ()) { self.compareFinishedFiles.removeAllObjects(); self.convertFiles.removeAllObjects() // clearCacheFiles() let needCheckString = self.selectedKeys().contains("字符") if !needCheckString { _status = .Finished complention(self, self.reportString) return } _status = .Process reportString = NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】字符比对开始!\n", attributes:[.foregroundColor : NSColor.blue]) let files = DataModel.shared.originFilesFor(_fileType, type: _type) self.testFiles = NSArray(array: files); let checkDirectory = self.checkFileDirectory() let originDirectory = self.originFileDirectory() let resultDirectory = self.resultFileDirectory() if (files.count > 0) { try? FileManager.default.createDirectory(atPath: checkDirectory, withIntermediateDirectories: true); try? FileManager.default.createDirectory(atPath: resultDirectory, withIntermediateDirectories: true); } var tDegree = Double(0); var tCount = Int(0) var fileIndex = 0; var convertFileBlock = { (files:[String]) in } convertFileBlock = { (files:[String]) in if (fileIndex >= files.count) { TestDegreeManager.shared().set(((tCount != 0) ? tDegree/Double(tCount) : 0.0), fileType: self.fileType(), type: self.type()) self._status = .Finished DispatchQueue.main.async { autoreleasepool { complention(self, self.reportString); } } return } let fileName = files[fileIndex] let fName = NSString(string: fileName).deletingPathExtension let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf") let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+self.extention()) let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+self.extention()) self.reportString?.append(NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】开始对照文件 \"\(fName)\"\n", attributes:[.foregroundColor : NSColor.black])) // ... // 执行转换过程 let index = self.testFiles.index(of: fileName); if (index != NSNotFound) { self.convertProgress = Double(index) / Double(self.testFiles.count) } self.convertFiles.add(fileName); self.testlog("开始对照:"+fileName, (self.compareProgress + self.convertProgress)/2.0) // self.process(originPath, resultPath: resultPath) { status in let status = 1 if FileManager.default.fileExists(atPath: resultPath) && status == 1 { if needCheckString && FileManager.default.fileExists(atPath: checkPath) { DispatchQueue.global().async { let checkString = self.readTextFile(checkPath as NSString) let resultStr = self.readTextFile(resultPath as NSString) if (checkString != nil && resultStr != nil) { let maxSize = checkString!.count let report = NSMutableAttributedString(string: "") let degree = self.compareString(checkString as! NSString, result: resultStr as! NSString) { appAttr in report.append(appAttr) } // let degree = self.compareString(checkString as! NSString, result: resultStr as! NSString) { skipString, failString in // self.appendErrorInfo(skipString, failString: failString) // } var color = NSColor.black if fabs(degree-100.0) >= 0.01 { color = NSColor.red } tDegree += degree; tCount += 1 TestDegreeManager.shared().set(degree, fileType: self.fileType(), type: self.type(), fileName: fileName) let successCount = Int(maxSize * Int(degree)/100) report.append(NSAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"比对完成,准确率\(degree)%(\(successCount)/\(maxSize))\n", attributes:[.foregroundColor : color])) if (report != nil) { do { let rtfData = try? report.data(from: .init(location: 0, length: report.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf]) let path = NSString(string: resultPath).appendingPathExtension("rtf") try? FileManager.default.removeItem(atPath: path!); try? rtfData?.write(to: NSURL.fileURL(withPath: path!)) } catch { } self.reportString?.append(report) } } let index = self.testFiles.index(of: fileName); if (index != NSNotFound) { self.compareProgress = Double(index) / Double(self.testFiles.count) } self.compareFinishedFiles.add(fileName); self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0) fileIndex += 1 convertFileBlock(files); } }else { self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】对照文件 \"\(fName)\"不存在!\n", attributes:[.foregroundColor : NSColor.red])) let index = self.testFiles.index(of: fileName); if (index != NSNotFound) { self.compareProgress = Double(index) / Double(self.testFiles.count) } self.compareFinishedFiles.add(fileName); self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0) fileIndex += 1 convertFileBlock(files); } }else { if (status == 0) { self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档失败!\n", attributes:[.foregroundColor : NSColor.red])) }else if (status == -1 || status == -2) { self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"文档无法打开!\n", attributes:[.foregroundColor : NSColor.red])) }else if (status == -3) { self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档中 Crash!\n", attributes:[.foregroundColor : NSColor.red])) } let index = self.testFiles.index(of: fileName); if (index != NSNotFound) { self.compareProgress = Double(index) / Double(self.testFiles.count) } self.compareFinishedFiles.add(fileName); self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0) fileIndex += 1 convertFileBlock(files); } // } } convertFileBlock(files); } } // Dispared class StringAutoCompare : StringAutoCompareTest { override class func shared() -> AutoTest? { return StringAutoCompareTest() } }