def updateInfo(info_file, info_resource): if not os.path.isfile(info_file): print("数据库信息文件: '%s' 缺失, 尝试从 '%s' 中导入信息." % (info_file, info_resource)) elif (os.path.getmtime(info_resource)>os.path.getmtime(info_file)): print("数据库信息文件: '%s' 有更新, 尝试从中导入新信息." % info_resource) else: try: from QuantStudio.Tools.DataTypeFun import readNestedDictFromHDF5 return (readNestedDictFromHDF5(info_file, ref="/TableInfo"), readNestedDictFromHDF5(info_file, ref="/FactorInfo")) except: print("数据库信息文件: '%s' 损坏, 尝试从 '%s' 中导入信息." % (info_file, info_resource)) if not os.path.isfile(info_resource): raise __QS_Error__("缺失数据库信息源文件: %s" % info_resource) return importInfo(info_file, info_resource)
def connect(self): if not os.path.isdir(self.MainDir): raise __QS_Error__("不存在主目录: %s!" % self.MainDir) AllTables = listDirDir(self.MainDir) _TableFactorDict = {} if not os.path.isfile(self.MainDir + os.sep + "LockFile"): open(self.MainDir + os.sep + "LockFile", mode="a").close() self._LockFile = self.MainDir + os.sep + "LockFile" self._DataLock = fasteners.InterProcessLock(self._LockFile) with self._DataLock: for iTable in AllTables: iTablePath = self.MainDir + os.sep + iTable iFactors = set(listDirFile(iTablePath, suffix=self._Suffix)) if not iFactors: continue try: iDataType = readNestedDictFromHDF5( iTablePath + os.sep + "_TableInfo.h5", "/DataType") except: iDataType = None if (iDataType is None) or (iFactors != set(iDataType.index)): iDataType = {} for ijFactor in iFactors: with h5py.File(iTablePath + os.sep + ijFactor + "." + self._Suffix, mode="r") as ijDataFile: iDataType[ijFactor] = ijDataFile.attrs["DataType"] iDataType = pd.Series(iDataType) writeNestedDict2HDF5(iDataType, iTablePath + os.sep + "_TableInfo.h5", "/DataType") _TableFactorDict[iTable] = iDataType self._TableFactorDict = _TableFactorDict self._isAvailable = True return 0
def getMetaData(self, key=None): with self._FactorDB._DataLock: return readNestedDictFromHDF5( self._FactorDB.MainDir + os.sep + self.Name + os.sep + "_TableInfo.h5", "/" + ("" if key is None else key))