def copy_files(self, _pre, _post, _among, _filter, _keepdir, _ext): ''' 拷贝将满足条件的文件 ''' self.files = util.get_files(self._src, _ext) for item in self.files: filename = os.path.basename(item) basename = util.get_basename(filename) if '' != _pre: if basename.find(_pre) != 0: continue if '' != _post: if basename.rfind(_post) != len(basename) - len(_post): continue if '' != _among: if basename.find(_among) == -1: continue if '' != _filter: if item.find(_filter) != -1: continue if int(_keepdir) == 0: outfile = self._dest + '/' + filename else: outfile = item.replace(self._src, self._dest) outfile = os.path.normpath(util.format_path(outfile)) util.mkdir(os.path.dirname(outfile)) util.copy_file(item, outfile) util.log('File successfully copied : ' + outfile, console=self._console) return 'ok'
def open_excel(self, _file): ''' 读取Excel文件 ''' try: data = xlrd.open_workbook(_file) return data except Exception, e: util.log('Error, %s, open excel "%s"' % (e, _file), console=self._console)
def write_data(self, filename, strs, extname, nobom=False): ''' 将数据写入到目标文件 ''' util.mkdir(self._dest) basename = os.path.basename(filename) file_out = os.path.normpath( os.path.join(self._dest, util.get_basename(basename) + extname)) fp = open(file_out, 'w') if not nobom: strs = '\xef\xbb\xbf' + strs fp.write(strs) fp.close() util.log('File successfully converted : ' + file_out, console=self._console)
def merge_json(self, _match, _filter, _id, _outfile): ''' 合并所给的所有json文件为一个json文件 ''' self.files = util.get_files(self._src, '.json') values = [] for item in self.files: if item.find(_match) == -1 or item.find(_filter) != -1: continue try: f = open(item, 'r') data_string = f.read() f.close() decoded = json.loads(data_string) except Exception, e: util.log("Error, %s, read file: %s" % (e, item), console=None) continue for each in decoded: if not each.has_key(_id): continue values.append(each)
def OnTDo(self, event): ''' 执行 ''' last_index = len(self.curr_page) - 1 if 0 > last_index: return logic_type = self.curr_page[last_index] parent_page = self.curr_page[last_index - 1] if 1 <= last_index else '' if 'format' == parent_page: text_input = self.pages[logic_type].get('src') text_output = self.pages[logic_type].get('dest') src = text_input.GetValue() dest = text_output.GetValue() if not util.check_path(src): util.log(u"请选择正确的源文件夹路径!") return if not util.check_path(dest): util.log(u"请选择正确的目标文件夹路径!") return self.pages[logic_type]['console'].SetValue('') formatConvert = FormatConvert( src, dest, self.pages[logic_type].get('console')) result = '' if 'excel_txt' == logic_type: result = formatConvert.excel_to_txt() elif 'excel_txt' == logic_type: result = formatConvert.excel_to_csv() elif 'txt_json' == logic_type: result = formatConvert.txt_to_json() elif 'excel_json' == logic_type: result = formatConvert.excel_to_json() elif 'excel_lua' == logic_type: result = formatConvert.excel_to_lua(False) if 'ok' == result: util.log(u"执行完毕!", flag=wx.OK | wx.ICON_INFORMATION)
if __name__ == '__main__': args = sys.argv if len(args) < 2: config = os.path.abspath('.') + '/conf/config.conf'; else: config = os.path.abspath('.') + '/' + args[1]; try: src = util.parser_config(config, 'path', 'srcFolder'); dest = util.parser_config(config, 'path', 'destFolder'); if len(args) > 2: if args[2] == 'trunk': src = src.replace('{0}', 'trunk'); dest = dest.replace('{0}', 'trunk'); else: src = src.replace('{0}', 'branches/' + args[2]); dest = dest.replace('{0}', 'branches/' + args[2]); ext = util.parser_config(config, 'path', 'extname'); pre = util.parser_config(config, 'path', 'prefix'); post = util.parser_config(config, 'path', 'postfix'); among = util.parser_config(config, 'path', 'among'); filter = util.parser_config(config, 'path', 'filter'); keepdir = util.parser_config(config, 'path', 'keepdir'); except Exception, e: util.log("Error, %s, config: %s" % (e, config), console=None); os._exit(); src = util.format_path(os.path.abspath('.') + '/' + src); dest = util.format_path(os.path.abspath('.') + '/' + dest); formatConvert = FormatConvert(src, dest, None); formatConvert.copy_files(pre, post, among, filter, keepdir, ext);