in src/aaz_dev/cli/controller/az_module_manager.py [0:0]
def create_new_mod(self, mod_name):
if self.folder_is_module:
raise exceptions.ResourceConflict(
f"Cannot create a new module in cli extension repo('{Config.CLI_EXTENSION_PATH}'), "
f"because the repo is an extension module"
)
mod_path = self.get_mod_path(mod_name)
templates = get_templates()['extension']
new_files = {}
# render HISTORY.rst
file_path = os.path.join(mod_path, 'HISTORY.rst')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = templates['HISTORY.rst']
new_files[file_path] = tmpl.render()
# render README.md
file_path = os.path.join(mod_path, 'README.md')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = templates['README.md']
new_files[file_path] = tmpl.render(
mod_name=mod_name
)
# render setup.cfg
file_path = os.path.join(mod_path, 'setup.cfg')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = templates['setup.cfg']
new_files[file_path] = tmpl.render()
# render setup.py
file_path = os.path.join(mod_path, 'setup.py')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = templates['setup.py']
new_files[file_path] = tmpl.render(
mod_name=mod_name
)
# azext_* folder
ext_path = self.get_mod_ext_path(mod_name)
ext_templates = templates['azext_']
# render __init__.py
file_path = os.path.join(ext_path, '__init__.py')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = ext_templates['__init__.py']
new_files[file_path] = tmpl.render(
mod_name=mod_name
)
# render _help.py
file_path = os.path.join(ext_path, '_help.py')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = ext_templates['_help.py']
new_files[file_path] = tmpl.render()
# render _params.py
file_path = os.path.join(ext_path, '_params.py')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = ext_templates['_params.py']
new_files[file_path] = tmpl.render()
# render commands.py
file_path = os.path.join(ext_path, 'commands.py')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = ext_templates['commands.py']
new_files[file_path] = tmpl.render()
# render custom.py
file_path = os.path.join(ext_path, 'custom.py')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = ext_templates['custom.py']
new_files[file_path] = tmpl.render()
# azext_metadata.json
file_path = os.path.join(ext_path, 'azext_metadata.json')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
file_path, ext_metadata = self.create_or_update_mod_azext_metadata(mod_name)
new_files[file_path] = json.dumps(ext_metadata, indent=4, sort_keys=True)
# test_folder
t_path = os.path.join(ext_path, 'tests')
t_templates = ext_templates['tests']
# render __init__.py
file_path = os.path.join(t_path, '__init__.py')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = t_templates['__init__.py']
new_files[file_path] = tmpl.render()
profile = Config.CLI_DEFAULT_PROFILE
tp_path = os.path.join(t_path, profile)
tp_templates = t_templates['profile']
# render __init__.py
file_path = os.path.join(tp_path, '__init__.py')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = tp_templates['__init__.py']
new_files[file_path] = tmpl.render()
# render test_*.py
file_path = os.path.join(tp_path, f'test_{self.pkg_name(mod_name)}.py')
if os.path.exists(file_path):
raise exceptions.ResourceConflict(f"File already exist: '{file_path}'")
tmpl = tp_templates['test_.py']
new_files[file_path] = tmpl.render(name=mod_name)
# written files
for path, data in new_files.items():
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'w', encoding="utf-8") as f:
f.write(data)
module = CLIModule()
module.name = mod_name
module.folder = self.get_mod_path(mod_name)
module.profiles = {}
for profile_name in Config.CLI_PROFILES:
module.profiles[profile_name] = self._load_view_profile(profile_name, self.get_aaz_path(mod_name))
return module