1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
import sys
from jschema_to_python.python_file_generator import PythonFileGenerator
import jschema_to_python.utilities as util
class InitFileGenerator(PythonFileGenerator):
def __init__(self, module_name, root_schema, root_class_name, output_directory):
super(InitFileGenerator, self).__init__(output_directory)
self.module_name = module_name
self.root_schema = root_schema
self.root_class_name = root_class_name
def __del__(self):
sys.stdout = sys.__stdout__
def generate(self):
file_path = self.make_output_file_path("__init__.py")
with open(file_path, "w") as sys.stdout:
self.write_generation_comment()
self.write_import_statements()
def write_import_statements(self):
self.write_import_statement(self.root_class_name)
definition_schemas = self.root_schema.get("definitions")
if definition_schemas:
definition_keys = sorted(definition_schemas.keys())
for definition_key in definition_keys:
class_name = util.capitalize_first_letter(definition_key)
self.write_import_statement(class_name)
def write_import_statement(self, class_name):
class_module_name = util.class_name_to_private_module_name(class_name)
print(
"from "
+ self.module_name
+ "."
+ class_module_name
+ " import "
+ class_name
)
|