ecr.commands.cmd_template

src/ecr/commands/cmd_template.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from typing import cast
import os
import shutil

from .. import shared, ui, template
from ..core import path as ecrpath
from ..core import WorkManager
from ..ui import SwitchState
from ..ui.command import Command, Namespace, ReturnCode
from .helper import assertInited, printFileCreate, printFileDelete


class TemplateCommand(Command):
    @staticmethod
    def default(args: Namespace) -> ReturnCode:  # pylint: disable=W0613
        if not assertInited():
            return ReturnCode.UNLOADED
        return ReturnCode.OK

    @staticmethod
    def new(args: Namespace) -> ReturnCode:
        if not assertInited():
            return ReturnCode.UNLOADED
        tman: WorkManager = cast(WorkManager, shared.getManager())
        template.initialize(os.path.join(
            ecrpath.getTemplatePath(tman.workingDirectory), args.name))
        printFileCreate(args.name)
        return ReturnCode.OK

    @staticmethod
    def clear(args: Namespace) -> ReturnCode:
        if not assertInited():
            return ReturnCode.UNLOADED
        tman: WorkManager = cast(WorkManager, shared.getManager())
        console = ui.getConsole()
        if console.confirm(f"Do you want to clear template config of `{args.name}`?",
                           [SwitchState.Yes, SwitchState.No]) == SwitchState.Yes:
            template.clear(os.path.join(ecrpath.getTemplatePath(
                tman.workingDirectory), args.name))
        return ReturnCode.OK

    @staticmethod
    def remove(args: Namespace) -> ReturnCode:
        if not assertInited():
            return ReturnCode.UNLOADED
        tman: WorkManager = cast(WorkManager, shared.getManager())
        console = ui.getConsole()
        if console.confirm(f"Do you want to remove template `{args.name}`?",
                           [SwitchState.Yes, SwitchState.No]) == SwitchState.Yes:
            shutil.rmtree(os.path.join(ecrpath.getTemplatePath(
                tman.workingDirectory), args.name))
            printFileDelete(args.name)
        return ReturnCode.OK

    @staticmethod
    def showTemplate(basepath)->None:
        tem, exp = template.load(basepath)
        console = ui.getConsole()
        if exp:
            console.error(f"Loading template failed: {exp}")
        else:
            import json
            if tem:
                console.write(json.dumps(
                    tem.__dict__, default=str, indent=4))
            else:
                console.write("None")

    @staticmethod
    def show(args: Namespace) -> ReturnCode:
        if not assertInited():
            return ReturnCode.UNLOADED
        tman: WorkManager = cast(WorkManager, shared.getManager())
        console = ui.getConsole()
        if args.name:
            tpath = os.path.join(
                ecrpath.getTemplatePath(tman.workingDirectory), args.name)
            if os.path.isdir(tpath):
                TemplateCommand.showTemplate(tpath)
                return ReturnCode.OK
            else:
                console.write("No this template.")
                return ReturnCode.OK
        else:
            basepath = ecrpath.getTemplatePath(tman.workingDirectory)
            for item in os.listdir(basepath):
                itempath = os.path.join(basepath, item)
                if os.path.isfile(itempath):
                    continue
                console.info(item)
                TemplateCommand.showTemplate(itempath)
            return ReturnCode.OK

    def __init__(self):
        super().__init__("template", help="Tools for templates", func=TemplateCommand.default)

    def createParser(self, parsers):
        cmd = super().createParser(parsers)
        subpars = cmd.add_subparsers()

        cmd_new = subpars.add_parser("new", help="Create new template")
        cmd_new.add_argument("name", type=str, help="Template name")
        cmd_new.set_defaults(func=TemplateCommand.new)

        cmd_clear = subpars.add_parser("clear", help="Clear template config")
        cmd_clear.add_argument("name", type=str, help="Template name")
        cmd_clear.set_defaults(func=TemplateCommand.clear)

        cmd_remove = subpars.add_parser("remove", help="Remove template")
        cmd_remove.add_argument("name", type=str, help="Template name")
        cmd_remove.set_defaults(func=TemplateCommand.remove)

        cmd_show = subpars.add_parser("show", help="Show template config")
        cmd_show.add_argument("name", nargs="?", default=None,
                              type=str, help="Template name (empty to show all)")
        cmd_show.set_defaults(func=TemplateCommand.show)
        return cmd