SubModules structure example¶
Let’s create the structure¶
$ python -margparseinator -s -d ~/src/python/pets dog
$ python -margparseinator -s -d ~/src/python/pets cat
$ python -margparseinator -s -d ~/src/python/pets bird
# add a pets submodule for shared things
$ python -margparseinator -s -d ~/src/python/pets pets
~/src/python/pets
├── bird
│ ├── commands.py
│ └── __init__.py
├── cat
│ ├── commands.py
│ └── __init__.py
├── dog
│ ├── commands.py
│ └── __init__.py
├── pets
│ ├── commands.py
│ └── __init__.py
└── pets.py
The pets/main submodule¶
# -*- coding: utf-8 -*-
"""
====
Pets
====
Manages various pets.
"""
from argparseinator import ArgParseInator
import pets.commands
__version__ = "0.0.1"
# We enable the output and set the default write_mode to append binary.
ArgParseInator(add_output=True, write_mode="ab")
The pets/main commands¶
# -*- coding: utf-8 -*-
"""
=============================
Commands :mod:`pets.commands`
=============================
"""
import os
from os.path import dirname
from importlib import import_module
from argparseinator import ArgParseInated
from argparseinator import arg
from argparseinator import class_args
@class_args
class Commands(ArgParseInated):
"""Commands for pets"""
@arg()
def allsays(self):
"""Asks all pets to say something"""
# cycle in the main dir
for name in os.listdir(dirname(dirname(__file__))):
# try to import the module and say something.
try:
mod = import_module(name)
mod.commands.Commands(__argpi__).say()
except (ImportError, AttributeError):
# just pass in case of these errors
pass
@arg()
def allnames(self):
"""Asks all pets their names"""
# cycle in the main dir
for name in os.listdir(dirname(dirname(__file__))):
# try to import the module and say something.
try:
mod = import_module(name)
mod.commands.Commands(__argpi__).name()
except (ImportError, AttributeError):
# just pass in case of these errors
pass
The dog submodule¶
# -*- coding: utf-8 -*-
"""
===
Dog
===
Does the dog's things.
"""
from argparseinator import get_compiled
import dog.commands
__version__ = "0.0.1"
The dog commands¶
# -*- coding: utf-8 -*-
"""
============================
Commands :mod:`dog.commands`
============================
"""
from argparseinator import ArgParseInated
from argparseinator import arg
from argparseinator import class_args
@class_args
class Commands(ArgParseInated):
"""Commands for dog"""
@arg("word", help="The word", nargs="?", default="dog")
def say(self, word="dog"):
"""says the word"""
writeln("I woof", word)
@arg()
def name(self):
"""The pet name"""
writeln("Walle")
The running script¶
As you can see the script loads the submodules/package dinamically at runtime.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
==============================
Generic ArgParseInator Launcer
==============================
Generic ArgParseInator Launcer.
"""
from __future__ import print_function
import sys
from glob import glob
from os.path import basename, splitext, join, dirname
from importlib import import_module
try:
# we try to import the core module (pets/__init__.py in this case)
MOD = import_module(splitext(basename(sys.argv[0]))[0])
except ImportError:
MOD = None
try:
# we try to import the wanted module
sys.modules[__name__] = import_module(splitext(basename(sys.argv[1]))[0])
# modify the sys.argv to set it as starting script or the startin command.
sys.argv[0] = sys.argv.pop(1)
except (ImportError, IndexError):
# if we have a import error will and we have found the core module we
# will set is as the main module.
if MOD:
sys.modules[__name__] = MOD
else:
print("Valid module are:")
print("\n".join(set([
basename(dirname(d))
for d in glob(join(dirname(sys.argv[0]), "*/__init__.py*"))])))
sys.exit(1)
# we use the builtin __argpi__ object to check the command line.
__argpi__.check_command()
We can run the script in various ways
# Using the *core* module name *pets*
$ python pets.py pets allsays
# No module name uses the default pets commands.
$ python pets.py allsays
# both cases the result is
I woof dog
I miaow cat
I tweet bird
# Passing the submodule name
$ python pets.py dog say "I'm a coold dog!"
I woof I'm a cool dog
# submodules preserves the core module commands
$ python pets.py dog allsays
I woof dog
I miaow cat
I tweet bird