新增工程数据
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
from typing import Dict,List,Any
|
||||
import json,os
|
||||
|
||||
class Record:
|
||||
def __init__(self,datas:Dict[str,Any]) -> None:
|
||||
self._datas:Dict[str,Any] = datas
|
||||
|
||||
def value(self,key:str):
|
||||
if key in self._datas:
|
||||
return self._datas.get(key)
|
||||
return ''
|
||||
|
||||
class Field:
|
||||
def __init__(self,datas:Dict[str,Any]) -> None:
|
||||
self._datas:Dict[str,Any] = datas
|
||||
|
||||
def value(self,key:str):
|
||||
if key in self._datas:
|
||||
return self._datas.get(key)
|
||||
return ''
|
||||
|
||||
class JsonTable:
|
||||
def __init__(self,filePth:str) -> None:
|
||||
self._filePth = filePth
|
||||
self._fields:Dict[str,Field] = {}
|
||||
self._records:List[Record] = []
|
||||
self._name = ''
|
||||
|
||||
def parse(self):
|
||||
with open(self._filePth, 'r',encoding='utf-8') as file:
|
||||
jsObj = json.load(file)
|
||||
data:dict = jsObj.get('table')
|
||||
self._name = data.get('name')
|
||||
Jsfields = data.get('fields')
|
||||
for jsfiled in Jsfields:
|
||||
field = Field(jsfiled)
|
||||
self._fields[field.value('name')] =field
|
||||
|
||||
JsRecords = data.get('records')
|
||||
for jsRecord in JsRecords:
|
||||
self._records.append(Record(jsRecord))
|
||||
|
||||
def records(self):
|
||||
return self._records
|
||||
|
||||
class ProjectJson:
|
||||
def __init__(self,dir:str) -> None:
|
||||
self._dir = dir
|
||||
self._tables:Dict[str,JsonTable] = {}
|
||||
|
||||
def parse(self):
|
||||
json_files = [f for f in os.listdir(self._dir) if f.endswith('.json')]
|
||||
for json_file in json_files:
|
||||
prjPath = os.path.join(self._dir, json_file)
|
||||
tb = JsonTable(prjPath)
|
||||
tb.parse()
|
||||
basename = os.path.splitext(json_file)[0]
|
||||
self._tables[basename] = tb
|
||||
|
||||
def table(self,tableName:str):
|
||||
return self._tables[tableName]
|
||||
|
||||
def getProjectName(dir:str):
|
||||
prjJson = ProjectJson(dir)
|
||||
prjJson.parse()
|
||||
tb:JsonTable = prjJson.table('工程属性')
|
||||
records = tb.records()
|
||||
for record in records:
|
||||
name = record.value('名称')
|
||||
if name == '工程名称':
|
||||
return record.value('值')
|
||||
return ''
|
||||
|
||||
Reference in New Issue
Block a user