StandAlone Structure Example¶
StandAlone project example.
Let’s create the structure¶
$ python -margparseinator -d ~/src/python -D"Greets people" greets
~/src/python/greets
├── greets
│ ├── commands.py
│ └── __init__.py
└── greets.py
The running script¶
As you can see the script loads the submodules/package dinamically at runtime.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Standard launcher
"""
import sys
from os.path import basename, splitext
from importlib import import_module
sys.modules[__name__] = import_module(splitext(basename(sys.argv[0]))[0])
if __name__ == "__main__":
__argpi__.check_command()
The __init__ module¶
As you can see we can define here everything we need to run the main script.
# -*- coding: utf-8 -*-
"""
======
Greets
======
Greets people
"""
from argparseinator import ArgParseInator
# we import the get_compiled function in case of sphinx.
from argparseinator import get_compiled
import yaml
import greets.commands
__version__ = "0.0.1"
# We don't really need a configuration but it's for example propouse.
def cfg_factory(filename):
"""Config Factory"""
try:
# try to load config as yaml file.
with open(filename, 'rb') as stream:
return yaml.load(stream)
except StandardError as error:
# In case of error we use the **__argpi__** builtin to exit from
# script
__argpi__.exit(1, str(error))
ArgParseInator(
# Add the possibility of writing the output to file.
add_output=True,
# we will append to existing file.
write_mode='ab',
# Add the cfg_factory with the default config name to None.
config=(None, cfg_factory,)
)
The command module¶
As you can see
# -*- coding: utf-8 -*-
"""
===============================
Commands :mod:`greets.commands`
===============================
"""
from argparseinator import ArgParseInated
from argparseinator import arg, ap_arg
from argparseinator import class_args
@class_args
class Commands(ArgParseInated):
"""Commands for greets"""
# we have the same params for all the commands so we can share them
__shared_arguments__ = [
ap_arg("who", help="The who", nargs="?", default="World!"),
ap_arg("-l", "--lang", default="en", help="Language")
]
def __preinator__(self):
# Actually we are not sure we have a configuration so is better
# add some default.
cfg = {
'lang': 'en',
'words_en': {
'greet': 'Greetings',
'hello': 'Hello',
'bye': 'Goodbye',
}
}
cfg.update(self.cfg)
self.cfg = cfg
def get_language(self, word, lang=None):
"""Get right word for configured language"""
lang = lang or self.cfg.get('lang', 'en')
# let's retrieve the word from configuration dict.
try:
return self.cfg['words_' + lang][word]
except StandardError:
return 'Do not know how to "{}" in "{}"'.format(word, lang)
@arg()
def greet(self, who, lang):
"""Greets"""
# we added the add_output param so we use the writeln to greet on the
# right output.
writeln(self.get_language('greet', lang), who)
@arg()
def hallo(self, who, lang):
"""Hallo"""
writeln(self.get_language('hello', lang), who)
@arg()
def bye(self, who, lang):
"""Bye"""
writeln(self.get_language('bye', lang), who)