1515# See the License for the specific language governing permissions and
1616# limitations under the License.
1717
18- """Generates a JSON schema from the service's Config class as well as a corresponding
19- example config yaml ( or check whether these files are up to date) .
18+ """Generate a JSON Schema from the service's Config class and an example
19+ config YAML file, or check whether the existing files are up to date.
2020"""
2121
2222import importlib
@@ -46,7 +46,7 @@ class ValidationError(RuntimeError):
4646def get_config_class ():
4747 """
4848 Dynamically imports and returns the Config class from the current service.
49- This makes the script service repo agnostic .
49+ This makes the script agnostic to the service repository .
5050 """
5151 # get the name of the microservice package
5252 with subprocess .Popen (
@@ -82,28 +82,22 @@ def get_schema() -> str:
8282
8383def get_example () -> str :
8484 """Returns an example config YAML."""
85-
8685 config = get_dev_config ()
8786 normalized_config_dict = config .model_dump (mode = "json" , by_alias = True )
8887 return yaml .dump (normalized_config_dict , indent = 2 , sort_keys = True )
8988
9089
9190def update_docs ():
92- """Update the example config and config schema files documenting the config
93- options."""
94-
95- example = get_example ()
96- with open (EXAMPLE_CONFIG_YAML , "w" , encoding = "utf-8" ) as example_file :
97- example_file .write (example )
98-
99- schema = get_schema ()
100- with open (CONFIG_SCHEMA_JSON , "w" , encoding = "utf-8" ) as schema_file :
101- schema_file .write (schema )
91+ """Update the example config YAML and JSON Schema files documenting the
92+ config options.
93+ """
94+ EXAMPLE_CONFIG_YAML .write_text (get_example (), encoding = "utf-8" )
95+ CONFIG_SCHEMA_JSON .write_text (get_schema (), encoding = "utf-8" )
10296
10397
10498def print_diff (expected : str , observed : str ):
105- """Print differences between expected and observed files ."""
106- echo_failure ("Differences in Config YAML:" )
99+ """Print differences between expected and observed example config YAML ."""
100+ echo_failure ("Differences in config YAML file :" )
107101 for line in unified_diff (
108102 expected .splitlines (keepends = True ),
109103 observed .splitlines (keepends = True ),
@@ -114,28 +108,25 @@ def print_diff(expected: str, observed: str):
114108
115109
116110def check_docs ():
117- """Check whether the example config and config schema files documenting the config
118- options are up to date.
111+ """Check whether the example config YAML and JSON Schema files are up to date.
119112
120113 Raises:
121- ValidationError: if not up to date.
114+ ValidationError: If not up to date.
122115 """
123116
124117 example_expected = get_example ()
125- with open (EXAMPLE_CONFIG_YAML , encoding = "utf-8" ) as example_file :
126- example_observed = example_file .read ()
118+ example_observed = EXAMPLE_CONFIG_YAML .read_text (encoding = "utf-8" )
127119 if example_expected != example_observed :
128120 print_diff (example_expected , example_observed )
129121 raise ValidationError (
130- f"Example config YAML at '{ EXAMPLE_CONFIG_YAML } ' is not up to date."
122+ f"Example config YAML file at '{ EXAMPLE_CONFIG_YAML } ' is not up to date."
131123 )
132124
133125 schema_expected = get_schema ()
134- with open (CONFIG_SCHEMA_JSON , encoding = "utf-8" ) as schema_file :
135- schema_observed = schema_file .read ()
126+ schema_observed = CONFIG_SCHEMA_JSON .read_text (encoding = "utf-8" )
136127 if schema_expected != schema_observed :
137128 raise ValidationError (
138- f"Config schema JSON at '{ CONFIG_SCHEMA_JSON } ' is not up to date."
129+ f"Config schema JSON file at '{ CONFIG_SCHEMA_JSON } ' is not up to date."
139130 )
140131
141132
0 commit comments