Improved main.py templates for new command to handle local imports

This commit is contained in:
Sven Heidemann 2021-04-10 12:04:47 +02:00
parent 5712a9ad67
commit 113d3199b1
2 changed files with 46 additions and 6 deletions

View File

@ -13,11 +13,16 @@ class MainWithApplicationHostAndStartupTemplate(TemplateFileABC):
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = os.path.join(path, name) self._path = os.path.join(path, name)
import_pkg = f'{name}.'
if name == '':
import_pkg = ''
self._value = textwrap.dedent(f"""\ self._value = textwrap.dedent(f"""\
from cpl.application import ApplicationBuilder from cpl.application import ApplicationBuilder
from {name}.application import Application from {import_pkg}application import Application
from {name}.startup import Startup from {import_pkg}startup import Startup
def main(): def main():
@ -51,10 +56,15 @@ class MainWithApplicationBaseTemplate(TemplateFileABC):
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = os.path.join(path, name) self._path = os.path.join(path, name)
import_pkg = f'{name}.'
if name == '':
import_pkg = ''
self._value = textwrap.dedent(f"""\ self._value = textwrap.dedent(f"""\
from cpl.application import ApplicationBuilder from cpl.application import ApplicationBuilder
from {name}.application import Application from {import_pkg}application import Application
def main(): def main():
@ -87,6 +97,11 @@ class MainWithoutApplicationBaseTemplate(TemplateFileABC):
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = os.path.join(path, name) self._path = os.path.join(path, name)
import_pkg = f'{name}.'
if name == '':
import_pkg = ''
self._value = textwrap.dedent("""\ self._value = textwrap.dedent("""\
from cpl.console import Console from cpl.console import Console
@ -120,6 +135,11 @@ class MainWithDependencyInjection(TemplateFileABC):
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = os.path.join(path, name) self._path = os.path.join(path, name)
import_pkg = f'{name}.'
if name == '':
import_pkg = ''
self._value = textwrap.dedent("""\ self._value = textwrap.dedent("""\
from cpl.configuration import Configuration, ConfigurationABC from cpl.configuration import Configuration, ConfigurationABC
from cpl.console import Console from cpl.console import Console

View File

@ -13,11 +13,16 @@ class MainWithApplicationHostAndStartupTemplate(TemplateFileABC):
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = os.path.join(path, name) self._path = os.path.join(path, name)
import_pkg = f'{name}.'
if name == '':
import_pkg = ''
self._value = textwrap.dedent(f"""\ self._value = textwrap.dedent(f"""\
from cpl.application import ApplicationBuilder from cpl.application import ApplicationBuilder
from {name}.application import Application from {import_pkg}application import Application
from {name}.startup import Startup from {import_pkg}startup import Startup
def main(): def main():
@ -51,10 +56,15 @@ class MainWithApplicationBaseTemplate(TemplateFileABC):
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = os.path.join(path, name) self._path = os.path.join(path, name)
import_pkg = f'{name}.'
if name == '':
import_pkg = ''
self._value = textwrap.dedent(f"""\ self._value = textwrap.dedent(f"""\
from cpl.application import ApplicationBuilder from cpl.application import ApplicationBuilder
from {name}.application import Application from {import_pkg}application import Application
def main(): def main():
@ -86,6 +96,11 @@ class MainWithoutApplicationBaseTemplate(TemplateFileABC):
self._name = 'main.py' self._name = 'main.py'
self._path = os.path.join(path, name) self._path = os.path.join(path, name)
import_pkg = f'{name}.'
if name == '':
import_pkg = ''
self._value = textwrap.dedent("""\ self._value = textwrap.dedent("""\
from cpl.console import Console from cpl.console import Console
@ -118,6 +133,11 @@ class MainWithDependencyInjection(TemplateFileABC):
self._name = 'main.py' self._name = 'main.py'
self._path = os.path.join(path, name) self._path = os.path.join(path, name)
import_pkg = f'{name}.'
if name == '':
import_pkg = ''
self._value = textwrap.dedent("""\ self._value = textwrap.dedent("""\
from cpl.configuration import Configuration, ConfigurationABC from cpl.configuration import Configuration, ConfigurationABC
from cpl.console import Console from cpl.console import Console