Quickstart¶
Eager to get started? This page gives a good introduction in how to get started with chuda.
First, make sure that:
- chuda is installed
- chuda is up-to-date
Let’s get started with some simple examples.
Hello World¶
Create an app with chuda is very simple
from chuda import App, autorun
@autorun() # Equivalent to if __name__ == "__main__"
class HelloWorldApp(App):
def main(self):
self.logger.info("Hello Word")
Parse a config file¶
Chuda will handle for you the parsing of a configuration file
For example, with this configuration file:
# ./config.ini
[hello]
name = John
This code will parse it automatically:
from chuda import App, autorun
@autorun()
class ConfigApp(App):
config_path = ["./config.ini", "../config.ini"]
def main(self):
self.logger.info("Hello {}".format(self.config["hello"]["name"]))
chuda can handle ini (default), json and yaml files (only if pyyaml is installed).
You must specified which parser will be used with the config_parser
attribute
As you can see, config_path
is a list.
Chuda will try to load each file in the order in which they have been declared until it find one that exists.
Handle arguments¶
Chuda gives you a declarative interface to add options and parameters to your applications
from chuda import App, autorun, Option, Parameter
@autorun()
class ArgumentsApp(App):
arguments = [
Option(["--language"], default="en", choices=["en", "fr"]),
Option(["--polite", "-p"], action="store_true"),
Parameter("name"),
]
def main(self):
salutation = ""
if self.arguments.language == "en":
salutation = "Hello " if self.arguments.polite else "Hi "
elif self.arguments.language == "fr":
salutation = "Bonjour " if self.arguments.polite else "Salut "
salutation += self.arguments.name
self.logger.info(salutation)
An Option
represent an UNIX style option (“e.g: git commit -m stuff”)
A Parameter
represent a simple parameter (“e.g: git checkout stuff”)
Both takes the same parameters as the add_argument()
method.
By default, chuda proposes three basic options :
-q
/--quiet
: The logger stop logging-v
/--verbose
: The logging level is lowered to debug--version
: print the content of theversion
attribute and exit
Create subcommands¶
Chuda gives you a very simple way to create subcommands in your
application by defining Command
subclasses
from chuda import App, autorun, Command
# Should be split in mutiple files
class FirstCommand(Command):
command_name = "first"
description = "this is the first command"
def main(self):
self.logger.info("first from {}".format(self.app.app_name))
class SecondCommand(Command):
command_name = "second"
description = "this is the second command"
def main(self):
self.logger.info("second from {}".format(self.app.app_name))
@autorun()
class SubCommandsApp(App):
app_name = "my_app"
subcommands = [
FirstCommand,
SecondCommand
]
subcommands
is transformed to a dictionnary at runtime. So, if you need to call a subcommand from
an other subcommand :
from chuda import App, autorun, Command
# Should be split in mutiple files
class FirstCommand(Command):
command_name = "first"
description = "this is the first command"
def main(self):
self.logger.info("first from {}".format(self.app.app_name))
class SecondCommand(Command):
command_name = "second"
description = "this is the second command"
def main(self):
self.app.subcommands["first"].run()
@autorun()
class SubCommandsApp(App):
app_name = "my_app"
subcommands = [
FirstCommand,
SecondCommand
]
Add plugins¶
Chuda apps encourage separation of concerns with a system of plugins
A plugin is created by extend the Plugin
class
from chuda import App, autorun, Plugin
import requests
class HttpPlugin(Plugin):
base_url = None
def on_create(self):
self.enrich_app("http", self)
def on_config_read(self):
try:
self.base_url = self.app.config["base_url"]
except KeyError:
self.base_url = "http://www.example.com"
def get_root(self):
return requests.get(self.base_url)
@autorun()
class HttpApp(App):
http = None
plugins = [
HttpPlugin()
]
def main(self):
response = self.http.get_root()
self.logger.info(response.text)
Plugins provides mutliple methods to execute code at key points
in the lifecycle of the application. See Plugin
documentation for more informations