from pathlib import Path
from html.parser import HTMLParser
import json,re,runpy
WORK=Path(__file__).resolve().parent
context=runpy.run_path(str(WORK/'audit_menu_skill_sources.py'))
ROOT=context['ROOT'];classes=context['classes']
audits=json.loads((WORK/'menu-skill-source-audit.json').read_text(encoding='utf-8'))
class Forms(HTMLParser):
def __init__(self):super().__init__();self.fields={};self.current=None;self.option=None;self.hidden=[]
def handle_starttag(self,tag,attrs):
attrs=dict(attrs)
if tag=='select' and attrs.get('name'):
self.current=attrs['name'];self.fields.setdefault(self.current,{'values':{},'dictionary':''})
if tag=='option' and self.current:self.option=[attrs.get('value'),[]]
if tag=='input' and attrs.get('type')=='hidden' and attrs.get('name') and attrs.get('value') not in {None,''}:
self.hidden.append({'name':attrs['name'],'value':attrs['value']})
if self.current:
for value in attrs.values():
match=re.search(r"@dict\.getType\('([^']+)'\)",value or '')
if match:self.fields[self.current]['dictionary']=match.group(1)
def handle_data(self,text):
if self.option:self.option[1].append(text)
def handle_endtag(self,tag):
if tag=='option' and self.option and self.current:
value,text=self.option;label=''.join(text).strip()
if value is not None and value!='' and label:self.fields[self.current]['values'][value]=label
self.option=None
if tag=='select':self.current=None
result={}
for audit in audits:
url=audit['menu']['url'] or ''
path=('/'+url.lstrip('/')).split('?',1)[0]
candidates=[]
for controller in audit['controllers']:
code=classes[controller['class']]['text'];prefix=re.search(r'\bprefix\s*=\s*"([^"]+)"',code)
for method in controller['endpoints']:
if method['return_type']!='String' or not any(route['verb']!='POST' and route['url']==path for route in method['routes']):continue
for tail in re.findall(r'return\s+prefix\s*\+\s*"([^"]+)"',method['body']):
if prefix:candidates.append((prefix.group(1)+tail,method))
for view in re.findall(r'return\s+"([^"]+)"\s*;',method['body']):candidates.append((view,method))
pages=[]
for view,method in candidates:
file=ROOT/'ruoyi-admin/src/main/resources/templates'/(view.lstrip('/')+'.html')
if not file.is_file():continue
text=file.read_text(encoding='utf-8-sig',errors='replace');forms=Forms();forms.feed(text)
columns=[]
for match in re.finditer(r'\bfield\s*:\s*[\'"]([\w]+)[\'"]([\s\S]{0,800}?)\}\s*,',text):
body=match.group(2);title=re.search(r'\btitle\s*:\s*[\'"]([^\'"\n]+)',body)
if title:columns.append({'property':match.group(1),'label':title.group(1),'divisor':10000 if re.search(r'/\s*10000\b',body) else None})
pages.append({'path':str(file.relative_to(ROOT)).replace('\\','/'),'view_method_line':method['line'],
'form_fields':forms.fields,'hidden_inputs':forms.hidden,'columns':columns})
result[str(audit['menu']['menu_id'])]=pages
(WORK/'menu-template-audit.json').write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8')
print(json.dumps({'menus_with_templates':sum(bool(v) for v in result.values()),'pages':sum(len(v) for v in result.values())},ensure_ascii=False))