90 lines
2.5 KiB
Python
90 lines
2.5 KiB
Python
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._fileName = os.path.splitext(os.path.basename(filePth))[0]
|
|
self._name = ''
|
|
self._comment = ''
|
|
|
|
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')
|
|
self._comment = data.get('comment')
|
|
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
|
|
|
|
def fields(self):
|
|
return self._fields
|
|
|
|
def name(self):
|
|
return self._fileName
|
|
|
|
def comment(self):
|
|
return self._comment
|
|
|
|
|
|
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 tables(self):
|
|
return self._tables
|
|
|
|
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 ''
|
|
|