def getConfigEncoding(extention): """ 获取配置中设置的编码 """ ext = CustomConfig().query("dataSource/encodings/%s" % extention, str) if ext == "": return CustomConfig().query("dataSource/encodings" % extention, str) return ext
def _initEnvironment(): """ 初始化环境变量 """ sys.path.append(script2sys(Path.executeDirectory())) # 将程序根目录添加到环境变量 for root in CustomConfig().syscodeTplRoots: # 将配置文件所在路径加入环境变量 sys.path.append(root) if CustomConfig().syscodeSrcPluginRoot != "": sys.path.append(CustomConfig().syscodeSrcPluginRoot) # 数据源插件根目录 if CustomConfig().syscodeDstPluginRoot != "": sys.path.append(CustomConfig().syscodeDstPluginRoot) # 数据导出插件根目录
def onBeginLoadDataSource(loadTracer): """ 开始加载数据源时被调用 """ Printer.dout("theme", "tipsLoadDataSource", src=loadTracer.getSrcText()) if CustomConfig().query("custom/showProgress", bool): global _cappedPrint _cappedPrint = Printer.capPrinter(True) pbarLen = CustomConfig().query("custom/pbarLen", int) _cappedPrint.pbarLen = pbarLen _cappedPrint.pbarChr = CustomConfig().query("custom/pbarChar") _cappedPrint.fmt = Printer.dtext("theme", "tipsLoadDataSourceProgress", pbarLen=pbarLen)
def printBlockHead(title, lines, width=None, blockChr=None): """ 打印一个区块头 """ if width is None: width = CustomConfig().query("custom/blockWidth", int) if blockChr is None: blockChr = CustomConfig().query("custom/blockSplitter") splitter = blockChr * width print "# %s" % splitter if title is not None: print "# %s" % script2sys(title) print "# %s" % ('-' * width) for line in lines: print "# %s" % script2sys(line) print "# %s" % splitter
def dstFile(self): """ 导出文件路径 """ if self.__dstFile is None: return None path = os.path.join(CustomConfig().dstRoot, self.__dstFile) return Path.normalizePath(path)
def _initExportTpl(tplFile): """ 初始化导出模板 """ exists = False for root in CustomConfig().tplRoots: fullName = os.path.join(root, tplFile) if os.path.exists(script2sys(fullName)): exists = True break if not exists: Printer.dout("theme", "errTplFileUnxist", tpl=tplFile) engine.exit(1) else: _printTplMsg(fullName) tplFile = os.path.splitext(tplFile)[0] tplFile = tplFile.replace("\\", ".").replace("/", ".") try: tplFile = script2sys(tplFile) tplModule = __import__(tplFile, globals(), locals(), [tplFile]) # 导出模板 return tplModule except BaseException, err: if not _isDebug(): print err.sysMsg else: sys.excepthook(BaseException, err, sys.exc_traceback) messageBox(err.scriptMsg, "ERROR", MB_OK, MB_ICONSTOP)
class TarsWriter(ExportWriter): __escs = { '<': "<", '>': ">", '\t': "	", '\n': "
", '\r': "
",} chrs = [] for ch in __escs: chrs.append("(%s)" % ch) __reptnESCs = re.compile("|".join(chrs)) del chrs def __init__(self, exportTracer): ExportWriter.__init__(self, exportTracer) fileName = self.outInfo.dstFile path = os.path.split(fileName)[0] if not os.path.exists(script2sys(path)): raise ExportFixException("errSaveFilePath", path=path) try: self.__file = open(script2sys(fileName), "w") except Exception, err: raise ExportFixException("errSaveFileName", file=fileName, msg=sys2script(err.message)) self.__encoding = self.outInfo.encoding self.__nl = CustomConfig().dstNewline
def query(self, attrName, attrType): """ 获取额外属性 """ if attrName not in self.__attrs: self.__attrs[attrName] = CustomConfig().query( "dataSource/%s" % attrName, attrType) return self.__attrs[attrName]
def query(self, attrName, attrType): """ 获取构造函数中传入的额外属性值,如果这些属性值不存在,则会从 outItemInfo 的 query 中取 """ value = self.__extras.get(attrName, self.INNER) if value is not self.INNER: return value value = CustomConfig().query("explainer/%s" % attrName, attrType) self.__extras[attrName] = value return value
def __init__(self, outInfo): self.__outInfo = outInfo if isinstance(outInfo.dstFile, basestring): self.__writer = outInfo.exportWriter if outInfo.isWriteHeader: self.__writer.writeText(outInfo.header) self.__writer.writeText(CustomConfig().dstNewline) outInfo.onBeginWriteOut(self.__writer) else: self.__writer = None
def getSrcFullName(srcFile): """ 获取数据源文件全路径 """ for root in CustomConfig().srcRoots: fullName = os.path.join(root, srcFile) fullName = Path.normalizePath(fullName) if os.path.exists(script2sys(fullName)): return fullName return srcFile
def onBeginWriteConfigItem(writeTracer): """ 写入配置选项 """ Printer.printNewline() name = writeTracer.owner.outItemInfo.name Printer.dout("export", "tipsWriteOut", name=name) if CustomConfig().query("custom/showProgress", bool): global _cappedPrint _cappedPrint = Printer.capPrinter(False)
def __init__(self, dstFile, *outItemInfos, **attrs): """ dstFile 允许为 None,如果为 None,则只解释表格,不将表格导出到配置文件 """ self.__attrs = attrs self.__exportTracer = None # 导出信息追踪器(实现将导出过程反馈给 UI) self.__dstFile = dstFile # 导出文件(可以传入 None,如果传入 None,将不写出文件) self.__outItemInfos = outItemInfos # 导出字典列表 self.__encoding = attrs.get("dstEncoding", \ CustomConfig().query("outInfo/dstEncoding", str)) self.__isWriteHeader = attrs.get("isWriteHeader", True) # 写出配置中,是否有注释头 self.comment = attrs.get("comment", "") # 导出模块的注释
class PyDictWriter(ExportWriter): __typeWriters = {} def __init__(self, exportTracer): ExportWriter.__init__(self, exportTracer) fileName = self.outInfo.dstFile try: self.__file = open(script2sys(fileName), "w") except Exception, err: raise ExportFixException("errSaveFileName", file=fileName, msg=sys2script(err.message)) self.__encoding = self.outInfo.encoding self.__nl = CustomConfig().dstNewline
def onEndScanDataSource(dsrcTracer): """ 结束一个数据源的扫描 """ if CustomConfig().query("custom/showProgress", bool): Printer.uncapPrinter() global _cappedPrint _cappedPrint = None # 打印空行(键) if len(dsrcTracer.emptyRows): Printer.printNewline() Printer.dout("export", "warnEmptyRows", rows=dsrcTracer.emptyRows)
class JsonWriter: __typeWriters = {} def __init__(self, path, encoding="utf-8"): try: self.__file = open(script2sys(path), "w") except Exception, err: raise ExportFixException("errSaveFileName", file=path, msg=sys2script(err.message)) self.__warps = 1 self.__warp = 0 self.__encoding = encoding self.__nl = CustomConfig().dstNewline
def onBeginScanDataSource(dsrcTracer): """ 开始扫描数据源 """ global _firstScanData if _firstScanData: _firstScanData = False else: Printer.printNewline() srcText = dsrcTracer.dsrc.getSrcText() Printer.dout("export", "tipsScanSrcData", path=srcText) if CustomConfig().query("custom/showProgress", bool): global _cappedPrint _cappedPrint = Printer.capPrinter(False)
def run(self): writer = self.__writer exportTracer = self.__outInfo.exportTracer exportTracer.onBeginExportConfig() count = len(exportTracer.exportItemTracers) for exportItemTracer in exportTracer.exportItemTracers: OutItemExporter(exportItemTracer).export(writer) count = count - 1 if count > 0 and writer: writer.writeText(CustomConfig().dstNewline) if writer: self.__outInfo.onWritenOut(writer) writer.close() self.__outInfo.onWriteClosed() exportTracer.onEndExportConfig()
def onBeginExportConfigItem(exportItemTracer): """ 开始导出一个配置选项时调用 """ global _firstScanData _firstScanData = True exportTracer = exportItemTracer.owner if len(exportTracer.exportItemTracers) > 1: outItemInfo = exportItemTracer.outItemInfo Printer.printBlockHead( None, [ Printer.dtext( "theme", "tipsBeginExportItem", dname=outItemInfo.name) ], width=CustomConfig().query("custom/blockWidth", int) - 10, blockChr="-")
def onEndWriteOutItem(writeTracer): """ 结束一个数据源的扫描 """ if CustomConfig().query("custom/showProgress", bool): Printer.uncapPrinter()
def __init__(self, key, ecount=0, defValue=ex_base.INNER, **extras): dds = extras.get("decimalDigits", CustomConfig().query("explainer/decimalDigits", int)) ex_onetype_array_col.__init__(self, key, xfloat[dds], ecount, defValue, **extras)
def __init__(self, key, defValue=0.0, **extras): dds = extras.get("decimalDigits", CustomConfig().query("explainer/decimalDigits", int)) if type(defValue) is float: self.defValue_ = xfloat[dds](defValue) ex_value_col_base.__init__(self, key, xfloat[dds], defValue, **extras)
""" 实现一些给导出模板用的规范化类 writen by hyw -- 2014.04.15 """ import time import socket from TableExporter import OrderDict from config.CustomConfig import CustomConfig from exporter.OutInfo import OutItemInfo from exporter.OutInfo import OutInfo from JsonWriter import JsonWriter from JsonScanner import JsonScanner _nl = CustomConfig().dstNewline # --------------------------------------------------------------------------------------- # 导出选项,每个选项对应一个导出配置中的数据字典。对应多个 DataSourceInfo # attrs: # isWriteTips: 表示是否要写出导出配置数据源提示 # warps: 表示导出的配置字典中,前面几层嵌套会换行(最小值为 1) # 如果不传入,则为 1 # writers: 传入一组类型写出回调:{类型: 写出函数} # 写出函数包含三个参数:writeTracer, value, fnStreamWriter # --------------------------------------------------------------------------------------- class JsonOutItemInfo(OutItemInfo): """ 从数据源中检索数据的导出选项 """
def printBlockSplitter(): """ 打印区块分割线 """ print "# %s" % (CustomConfig().query("custom/blockWidth", int) * \ CustomConfig().query("custom/blockSplitter"))