def LoadCategory(self): # {id: {id, name, parent_id, sub_sections}} # 尝试去读目录结构,如果没有,则新建一个目录结构json file root_path = file_util.getStoryFileRoot() cat_file_path = ''.join((root_path, '\\', self.story_id, '\\category.json')) print 'category_file_path:', cat_file_path if os.path.exists(cat_file_path): try: f = codecs.open(cat_file_path, 'r', 'utf-8') content = json.loads(f.read().strip('\n')) except IOError: # 重试 print ''.join(('read file error:', cat_file_path)) return else: try: f = codecs.open(cat_file_path, 'w+') content = {"max_idx": 0, "sections": dict()} f.write(json.dumps(content)) except IOError: # 重试 print ''.join(('read file error:', cat_file_path)) return f.close() self.max_idx = content.get('max_idx', 0) self.GenSections(content['sections'])
def GenScript(self): root_path = file_util.getStoryFileRoot() script_path = ''.join((root_path, '\\', self.story_id, '\\', self.section_id, '\\', self.id, '.stln')) template_path = file_util.getTemplatePath(consts.TEMPLATES[self.n_type]) template_file = codecs.open(template_path, 'r', 'utf-8') script_file = codecs.open(script_path, 'w', 'utf-8') script_file.write(template_file.read()) script_file.close() template_file.close()
def SaveStoryLine(self): pack_dict = {} for key, value in self.story_line_dict.iteritems(): pack_dict[key] = value.Pack() root_path = file_util.getStoryFileRoot() sl_file_path = ''.join((root_path, '\\', self.story_id, '\\', self.section_id, '\\', 'storyline.json')) f = codecs.open(sl_file_path, 'w', 'utf-8') f.write(json.dumps(pack_dict)) f.close()
def Export(self): next_ids = [] for sn in self.nexts: next_ids.append(sn.id) root_path = file_util.getStoryFileRoot() script_path = ''.join((root_path, '\\', self.story_id, '\\', self.section_id, '\\', self.id, '.stln')) script_file = codecs.open(script_path, 'r', 'utf-8') sc = script_file.read() script_file.close() return {'id':self.id, 'nexts': next_ids, 'script': sc}
def onOpenStory(self): story_path = QFileDialog.getExistingDirectory( self, "选取文件", file_util.getStoryFileRoot()) story_id = story_path.split('/')[-1] print story_id # 检查是否合法 if data_util.OpenStory(story_id): self.refreshFrames() else: QMessageBox.information(self, "打开失败", "请选择故事文件夹", QMessageBox.Yes | QMessageBox.No)
def LoadSection(self, dic): self.id = dic.get("id", 0) self.name = dic.get("name", '') start_node_id = dic.get("start_node_id", 0) root_path = file_util.getStoryFileRoot() dir_path = ''.join((root_path, '\\', self.story_id, '\\', self.id)) if not os.path.exists(dir_path): os.mkdir(dir_path) self.story_line = StoryLine(self.story_id, self.id, start_node_id) self.story_line.LoadStoryLine()
def SaveCategory(self): root_path = file_util.getStoryFileRoot() cat_file_path = ''.join((root_path, '\\', self.story_id, '\\category.json')) try: f = codecs.open(cat_file_path, 'w', 'utf-8') catetory_string = json.dumps(self.Pack()) f.write(catetory_string) f.close() except IOError: # 重试 print ''.join(('read file error:', cat_file_path)) for section in self.section_dict.itervalues(): section.SaveStoryLine() return
def Init(self): stories_root = file_util.getStoryFileRoot() # 读故事列表json try: meta = codecs.open(''.join((stories_root, "\\stories.json")), "r", 'utf-8') except: print 'read failed: ', ''.join((stories_root, "\\stories.json")) return False content = meta.read().strip('\n') if content: self.stories_dict = json.loads(content) else: self.stories_dict = {} meta.close()
def LoadStoryLine(self): root_path = file_util.getStoryFileRoot() sl_file_path = ''.join((root_path, '\\', self.story_id, '\\', self.section_id, '\\', 'storyline.json')) if os.path.exists(sl_file_path): # {id:{id, name, []}, id:{id, name, []}} f = codecs.open(sl_file_path, 'r', 'utf-8') story_content_dict = json.loads(f.read().strip('\n')) self.GenNodes(story_content_dict) f.close() else: f = codecs.open(sl_file_path, 'w', 'utf-8') # 跟章节的话建一个只显示章节名称的Node f.write(json.dumps(self.story_line_dict)) f.close() story_content_dict = {} if self.section_id == "root": self.AddNode("root", 0)
def onUploadStory(self): fileName_choose, filetype = QFileDialog.getOpenFileName( self, "选取文件", file_util.getStoryFileRoot(), "All Files (*);;Story Export Files (*.stex)") if filename_choose: def startUpload(data_dict): user = data_dict['user']['widget'].text() password = data_dict['password']['widget'].text() ret = net_util.UploadStoryFile(filename_choose, user, password) if ret: QMessageBox.information(self, "上传成功", "上传成功", QMessageBox.Yes | QMessageBox.No) else: QMessageBox.information(self, "上传失败", "上传失败", QMessageBox.Yes | QMessageBox.No) self.input_dlg = InputDlg() self.input_dlg.setGeometry(300, 100, 200, 100) self.input_dlg.buildInput((('user', '用户名'), ('password', '密码')), startUpload) self.input_dlg.show()
def NewStory(self, name): self.SaveStory() story_id = common_util.genId("Story") self.id = story_id self.name = name self.stories_dict[story_id] = name result = json.dumps(self.stories_dict) stories_root = file_util.getStoryFileRoot() cur_path = ''.join((stories_root, "\\", story_id)) os.mkdir(cur_path) # 创建新的目录数据 self.category = Category(self.id) self.category.LoadCategory() try: meta = codecs.open(''.join((stories_root, "\\stories.json")), "w", 'utf-8') meta.write(result) meta.close() except: print 'write failed: ', ''.join((stories_root, "\\stories.json")) return False return True
def onExportStory(self): filename_choose, filetype = QFileDialog.getSaveFileName( self, "文件保存", file_util.getStoryFileRoot(), "All Files (*);;Story Export Files (*.stex)") data_util.ExportStory(filename_choose)
def GetScrptPath(self): root_path = file_util.getStoryFileRoot() return ''.join((root_path, '\\', self.story_id, '\\', self.section_id, '\\', self.id, '.stln'))
def DeleteStory(self, story_id): if story_id in self.stories_dict: self.stories_dict.pop(story_id) stories_root = file_util.getStoryFileRoot() shutil.rmtree(''.join((stories_root, "\\", story_id)))