Skip to content

Commit db14cb9

Browse files
committed
collect_api_endpoints.py: move collect_api_modules into its own module, for #712
1 parent f44b9b3 commit db14cb9

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

collect_api_endpoints.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import os
2828
import argparse
2929
from jinja2 import Template
30-
from lib import ApiParser
31-
32-
EXCLUDE_CONTROLLERS = ['Core/Api/FirmwareController.php']
30+
from lib.utils import collect_api_modules
3331

3432

3533
def source_url(repo, src_filename):
@@ -46,25 +44,9 @@ def source_url(repo, src_filename):
4644
parser.add_argument('--debug', help='enable debug mode', default=False, action='store_true')
4745
cmd_args = parser.parse_args()
4846

49-
# collect all endpoints
50-
all_modules = dict()
51-
for root, dirs, files in os.walk(cmd_args.source):
52-
for fname in sorted(files):
53-
filename = os.path.join(root, fname)
54-
skip = False
55-
for to_exclude in EXCLUDE_CONTROLLERS:
56-
if filename.endswith(to_exclude):
57-
skip = True
58-
break
59-
if not skip and filename.lower().endswith('controller.php') and filename.find('mvc/app/controllers') > -1 \
60-
and root.endswith('Api'):
61-
payload = ApiParser(filename, cmd_args.debug).parse_api_php()
62-
if len(payload) > 0:
63-
if payload['module'] not in all_modules:
64-
all_modules[payload['module']] = list()
65-
all_modules[payload['module']].append(payload)
6647

6748
# writeout .rst files
49+
all_modules = collect_api_modules(cmd_args.source, cmd_args.debug)
6850
for module_name in all_modules:
6951
target_filename = "%s/source/development/api/%s/%s.rst" % (
7052
os.path.dirname(__file__), cmd_args.repo, module_name

lib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141

4242
class ApiParser:
43-
def __init__(self, filename, debug=False):
43+
def __init__(self,filename: str,debug: bool=False):
4444
self._debug = debug
4545
self._filename = filename
4646
self.base_filename = os.path.basename(filename)

lib/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
from . import ApiParser
3+
4+
EXCLUDE_CONTROLLERS = ['Core/Api/FirmwareController.php']
5+
6+
def collect_api_modules(source: str, debug: bool = False) -> dict[str, list[dict]]:
7+
# collect all endpoints
8+
all_modules = dict()
9+
for root, dirs, files in os.walk(source):
10+
for fname in sorted(files):
11+
filename = os.path.join(root, fname)
12+
skip = False
13+
for to_exclude in EXCLUDE_CONTROLLERS:
14+
if filename.endswith(to_exclude):
15+
skip = True
16+
break
17+
if not skip and filename.lower().endswith('controller.php') and filename.find('mvc/app/controllers') > -1 \
18+
and root.endswith('Api'):
19+
payload = ApiParser(filename, debug).parse_api_php()
20+
if len(payload) > 0:
21+
if payload['module'] not in all_modules:
22+
all_modules[payload['module']] = list()
23+
all_modules[payload['module']].append(payload)
24+
25+
return all_modules

0 commit comments

Comments
 (0)