collect_api_endpoints.py: move collect_api_modules into its own module, for https://github.com/opnsense/docs/pull/712

This commit is contained in:
Ad Schellevis 2025-05-03 18:08:54 +02:00
parent f44b9b32ea
commit db14cb93d6
3 changed files with 28 additions and 21 deletions

View File

@ -27,9 +27,7 @@
import os
import argparse
from jinja2 import Template
from lib import ApiParser
EXCLUDE_CONTROLLERS = ['Core/Api/FirmwareController.php']
from lib.utils import collect_api_modules
def source_url(repo, src_filename):
@ -46,25 +44,9 @@ if __name__ == '__main__':
parser.add_argument('--debug', help='enable debug mode', default=False, action='store_true')
cmd_args = parser.parse_args()
# collect all endpoints
all_modules = dict()
for root, dirs, files in os.walk(cmd_args.source):
for fname in sorted(files):
filename = os.path.join(root, fname)
skip = False
for to_exclude in EXCLUDE_CONTROLLERS:
if filename.endswith(to_exclude):
skip = True
break
if not skip and filename.lower().endswith('controller.php') and filename.find('mvc/app/controllers') > -1 \
and root.endswith('Api'):
payload = ApiParser(filename, cmd_args.debug).parse_api_php()
if len(payload) > 0:
if payload['module'] not in all_modules:
all_modules[payload['module']] = list()
all_modules[payload['module']].append(payload)
# writeout .rst files
all_modules = collect_api_modules(cmd_args.source, cmd_args.debug)
for module_name in all_modules:
target_filename = "%s/source/development/api/%s/%s.rst" % (
os.path.dirname(__file__), cmd_args.repo, module_name

View File

@ -40,7 +40,7 @@ DEFAULT_BASE_METHODS = {
class ApiParser:
def __init__(self, filename, debug=False):
def __init__(self,filename: str,debug: bool=False):
self._debug = debug
self._filename = filename
self.base_filename = os.path.basename(filename)

25
lib/utils.py Normal file
View File

@ -0,0 +1,25 @@
import os
from . import ApiParser
EXCLUDE_CONTROLLERS = ['Core/Api/FirmwareController.php']
def collect_api_modules(source: str, debug: bool = False) -> dict[str, list[dict]]:
# collect all endpoints
all_modules = dict()
for root, dirs, files in os.walk(source):
for fname in sorted(files):
filename = os.path.join(root, fname)
skip = False
for to_exclude in EXCLUDE_CONTROLLERS:
if filename.endswith(to_exclude):
skip = True
break
if not skip and filename.lower().endswith('controller.php') and filename.find('mvc/app/controllers') > -1 \
and root.endswith('Api'):
payload = ApiParser(filename, debug).parse_api_php()
if len(payload) > 0:
if payload['module'] not in all_modules:
all_modules[payload['module']] = list()
all_modules[payload['module']].append(payload)
return all_modules