Skip to content

Commit 7638512

Browse files
authored
Merge pull request #5708: Refactor st2-generate-schemas to be importable
2 parents d1a201f + 5db734b commit 7638512

File tree

4 files changed

+92
-29
lines changed

4 files changed

+92
-29
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ Changed
9494
* Refactor tests to use python imports to identify test fixtures. #5699 #5702 #5703 #5704 #5705 #5706
9595
Contributed by @cognifloyd
9696

97+
* Refactor ``st2-generate-schemas`` so that logic is in an importable module. #5708
98+
Contributed by @cognifloyd
99+
97100
Removed
98101
~~~~~~~
99102

contrib/schemas/sensor.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"uid": {
1212
"type": "string"
1313
},
14-
"name": {
14+
"class_name": {
1515
"type": "string",
1616
"required": true
1717
},

st2common/bin/st2-generate-schemas

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,29 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919

20+
"""
21+
A script that generates st2 metadata (pack, action, rule, ...) schemas.
22+
`st2-generate-schemas` is used to to update contrib/schemas/*.json.
23+
24+
USAGE: st2-generate-schemas <destination directory>
25+
"""
26+
2027
from __future__ import absolute_import
2128

22-
import json
2329
import os
24-
import six
25-
26-
from st2common.models.api import action as action_models
27-
from st2common.models.api import pack as pack_models
28-
from st2common.models.api import policy as policy_models
29-
from st2common.models.api import rule as rule_models
30-
from st2common.models.api import sensor as sensor_models
30+
import sys
3131

32-
content_models = {
33-
"pack": pack_models.PackAPI,
34-
"action": action_models.ActionAPI,
35-
"alias": action_models.ActionAliasAPI,
36-
"policy": policy_models.PolicyAPI,
37-
"rule": rule_models.RuleAPI,
38-
"sensor": sensor_models.SensorTypeAPI,
39-
}
32+
from st2common.cmd import generate_schemas
4033

4134

42-
def main():
35+
def init():
4336
scripts_dir = os.path.dirname(os.path.abspath(__file__))
4437
schemas_dir = os.path.abspath(os.path.join(scripts_dir, "../../contrib/schemas"))
4538

46-
for name, model in six.iteritems(content_models):
47-
schema_text = json.dumps(model.schema, indent=4)
48-
print('Generated schema for the "%s" model.' % name)
49-
50-
schema_file = os.path.join(schemas_dir, name + ".json")
51-
print('Schema will be written to "%s".' % schema_file)
52-
53-
with open(schema_file, "w") as f:
54-
f.write(schema_text)
55-
f.write("\n")
39+
# set the default for backwards compatibility
40+
generate_schemas.default_schemas_dir = schemas_dir
5641

5742

5843
if __name__ == "__main__":
59-
main()
44+
init()
45+
sys.exit(generate_schemas.main())
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2021 The StackStorm Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
A script that generates st2 metadata (pack, action, rule, ...) schemas.
17+
This is used by `st2-generate-schemas` to update contrib/schemas/*.json.
18+
"""
19+
20+
from __future__ import absolute_import
21+
22+
import json
23+
import os
24+
import sys
25+
26+
from st2common.models.api import action as action_models
27+
from st2common.models.api import pack as pack_models
28+
from st2common.models.api import policy as policy_models
29+
from st2common.models.api import rule as rule_models
30+
from st2common.models.api import sensor as sensor_models
31+
32+
__all__ = ["generate_schemas", "write_schemas"]
33+
34+
content_models = {
35+
"pack": pack_models.PackAPI,
36+
"action": action_models.ActionAPI,
37+
"alias": action_models.ActionAliasAPI,
38+
"policy": policy_models.PolicyAPI,
39+
"rule": rule_models.RuleAPI,
40+
"sensor": sensor_models.SensorTypeAPI,
41+
}
42+
43+
44+
default_schemas_dir = "."
45+
46+
47+
def generate_schemas():
48+
for name, model in content_models.items():
49+
schema_text = json.dumps(model.schema, indent=4)
50+
51+
yield name, schema_text
52+
53+
54+
def write_schemas(schemas_dir):
55+
for name, schema_text in generate_schemas():
56+
print('Generated schema for the "%s" model.' % name)
57+
58+
schema_file = os.path.join(schemas_dir, name + ".json")
59+
print('Schema will be written to "%s".' % schema_file)
60+
61+
with open(schema_file, "w") as f:
62+
f.write(schema_text)
63+
f.write("\n")
64+
65+
66+
def main():
67+
argv = sys.argv[1:]
68+
69+
# 1st positional parameter is the destination directory
70+
schemas_dir = argv[0] if argv else default_schemas_dir
71+
72+
write_schemas(schemas_dir)
73+
74+
return 0

0 commit comments

Comments
 (0)