| 90 | |
| 91 | |
| 92 | class MyApp(Application): |
| 93 | name = Unicode("myapp") |
| 94 | running = Bool(False, help="Is the app running?").tag(config=True) |
| 95 | classes = List([Bar, Foo]) # type:ignore[assignment] |
| 96 | config_file = Unicode("", help="Load this config file").tag(config=True) |
| 97 | |
| 98 | aliases = Dict( # type:ignore[assignment] |
| 99 | dict( # noqa: C408 |
| 100 | i="Foo.i", |
| 101 | j="Foo.j", |
| 102 | name="Foo.name", |
| 103 | mode="Foo.mode", |
| 104 | running="MyApp.running", |
| 105 | enabled="Bar.enabled", |
| 106 | log_level="MyApp.log_level", |
| 107 | ) |
| 108 | ) |
| 109 | |
| 110 | flags = Dict( # type:ignore[assignment] |
| 111 | dict( # noqa: C408 |
| 112 | enable=({"Bar": {"enabled": True}}, "Enable Bar"), |
| 113 | disable=({"Bar": {"enabled": False}}, "Disable Bar"), |
| 114 | debug=({"MyApp": {"log_level": 10}}, "Set loglevel to DEBUG"), |
| 115 | ) |
| 116 | ) |
| 117 | |
| 118 | def init_foo(self): |
| 119 | # You can pass self as parent to automatically propagate config. |
| 120 | self.foo = Foo(parent=self) |
| 121 | |
| 122 | def init_bar(self): |
| 123 | # Pass config to other classes for them to inherit the config. |
| 124 | self.bar = Bar(config=self.config) |
| 125 | |
| 126 | def initialize(self, argv=None): |
| 127 | self.parse_command_line(argv) |
| 128 | if self.config_file: |
| 129 | self.load_config_file(self.config_file) |
| 130 | self.load_config_environ() |
| 131 | self.init_foo() |
| 132 | self.init_bar() |
| 133 | |
| 134 | def start(self): |
| 135 | print("app.config:") |
| 136 | print(self.config) |
| 137 | self.describe() |
| 138 | print("try running with --help-all to see all available flags") |
| 139 | assert self.log is not None |
| 140 | self.log.debug("Debug Message") |
| 141 | self.log.info("Info Message") |
| 142 | self.log.warning("Warning Message") |
| 143 | self.log.critical("Critical Message") |
| 144 | |
| 145 | def describe(self): |
| 146 | print("I am MyApp with", self.name, self.running, "and 2 sub configurables Foo and bar:") |
| 147 | self.foo.describe() |
| 148 | self.bar.describe() |
| 149 | |