feat:
This commit is contained in:
46
nc_http/core/documents/__init__.py
Normal file
46
nc_http/core/documents/__init__.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
# 重构内容:
|
||||||
|
# 1.文档文件通过 docxtpl 模版文件生成
|
||||||
|
# 2.数据库文档数据来源为数据库系统信息查询
|
||||||
|
# Oracle
|
||||||
|
# Mysql
|
||||||
|
# 3.API 文档数据来源 swagger json 接口
|
||||||
|
|
||||||
|
|
||||||
|
class DocumentCreator:
|
||||||
|
has_catalog = True
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def create_cover(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def init_catalog(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def create_catalog(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def create_content(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def create(self):
|
||||||
|
self.create_cover()
|
||||||
|
self.init_catalog()
|
||||||
|
self.create_content()
|
||||||
|
self.create_catalog()
|
||||||
|
|
||||||
|
|
||||||
|
class DocDataBuilder:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self._data = None
|
||||||
|
|
||||||
|
def generate(self):
|
||||||
|
yield
|
||||||
|
|
||||||
|
def clean(self, item):
|
||||||
|
return item
|
||||||
|
|
||||||
|
@property
|
||||||
|
def data(self):
|
||||||
|
return self._data
|
9
nc_http/core/documents/api/__init__.py
Normal file
9
nc_http/core/documents/api/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from nc_http.core.documents.api.builder import ApiDocDataBuilder
|
||||||
|
from nc_http.core.documents.api.creator import ApiDocumentCreator
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
_builder = ApiDocDataBuilder(swagger_json_url='http://localhost:7027/apispec_1.json')
|
||||||
|
creator = ApiDocumentCreator(
|
||||||
|
builder=_builder, filename='text.docx', meta={'number_prefix': '3.1.'}, template='./template.docx'
|
||||||
|
)
|
||||||
|
creator.create()
|
18
nc_http/core/documents/api/builder.py
Normal file
18
nc_http/core/documents/api/builder.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from nc_http.core.documents import DocDataBuilder
|
||||||
|
|
||||||
|
|
||||||
|
class ApiDocDataBuilder(DocDataBuilder):
|
||||||
|
def __init__(self, swagger_json_url, *args, **kwargs):
|
||||||
|
self.swagger_json_url = swagger_json_url
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def data(self):
|
||||||
|
if self._data is None:
|
||||||
|
data = json.loads(requests.get(self.swagger_json_url).content)
|
||||||
|
self._data = data
|
||||||
|
return self._data
|
76
nc_http/core/documents/api/creator.py
Normal file
76
nc_http/core/documents/api/creator.py
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
from docxtpl import DocxTemplate
|
||||||
|
|
||||||
|
from nc_http.core.documents import DocumentCreator
|
||||||
|
|
||||||
|
|
||||||
|
class ApiDocumentCreator(DocumentCreator):
|
||||||
|
definition_map = {}
|
||||||
|
|
||||||
|
def __init__(self, builder, filename, meta, template=None, **kwargs):
|
||||||
|
self.builder = builder
|
||||||
|
self.filename = filename
|
||||||
|
self.meta = meta
|
||||||
|
self.template = template or r'./trash/documents/api/template.docx'
|
||||||
|
self.api_description = None
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
def parse_api(self):
|
||||||
|
api = self.builder.data
|
||||||
|
|
||||||
|
for definition_name, definition in api['definitions'].items():
|
||||||
|
# print(definition)
|
||||||
|
definition_content = {}
|
||||||
|
if definition.get('type') == 'object':
|
||||||
|
definition_content = definition['properties']
|
||||||
|
elif definition.get('type') == 'array':
|
||||||
|
definition_content = definition['items'].get('properties', {})
|
||||||
|
|
||||||
|
# print(definition_content)
|
||||||
|
self.definition_map[definition_name] = definition_content
|
||||||
|
|
||||||
|
api_description = {}
|
||||||
|
for path, methods in api['paths'].items():
|
||||||
|
'''
|
||||||
|
path_content: {
|
||||||
|
'get': {
|
||||||
|
'parameters': [
|
||||||
|
{
|
||||||
|
'default': '/u255.jpeg',
|
||||||
|
'description': '文件 key',
|
||||||
|
'in': 'path',
|
||||||
|
'name': 'key',
|
||||||
|
'required': True,
|
||||||
|
'type': 'string'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'responses': {
|
||||||
|
'200': {'description': '文件','schema': {'type': 'file'}}
|
||||||
|
},
|
||||||
|
'security': [{'Bearer': []}],
|
||||||
|
'summary': '获取文件',
|
||||||
|
'tags': ['文件']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
for method, content in methods.items():
|
||||||
|
tag = content['tags'][0]
|
||||||
|
content['path'] = path
|
||||||
|
content['method'] = method
|
||||||
|
api_description.setdefault(tag, [])
|
||||||
|
api_description[tag].append(content)
|
||||||
|
|
||||||
|
self.api_description = api_description
|
||||||
|
|
||||||
|
def create_content(self):
|
||||||
|
self.parse_api()
|
||||||
|
|
||||||
|
tpl = DocxTemplate(self.template)
|
||||||
|
|
||||||
|
context = {
|
||||||
|
'api_description': self.api_description,
|
||||||
|
'definition_map': self.definition_map,
|
||||||
|
'table_fields': ['参数名', '必选', '类型', '位置', '说明'],
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl.render(context)
|
||||||
|
tpl.save('text.docx')
|
BIN
nc_http/core/documents/api/template.docx
Normal file
BIN
nc_http/core/documents/api/template.docx
Normal file
Binary file not shown.
Reference in New Issue
Block a user