Pyjoyment.Command - Command base class

# Lowercase command name, in the module Pyjoyment.Command.mycommand
from Pyjoyment.Command import Command

from Pyjo.Util import extract_usage


class Pyjoyment_Command_mycommand(Command):
    # Short description
    description = 'My first Pyjoyment command'

    # Usage message from the synopsis of the docstring of the module
    usage = extract_usage()

    def run(self, *args):
        # Magic here! :)
        ...


object = Pyjoyment_Command_mycommand

# Or import the module, as in Google Python Style Guide

# Lowercase command name, in the module Pyjoyment.Command.mycommand
import Pyjoyment.Command

from Pyjo.Util import extract_usage


class Pyjoyment_Command_mycommand(Pyjoyment.Command.object):
    # Short description
    description = 'My first Pyjoyment command'

    # Usage message from the synopsis of the docstring of the module
    usage = extract_usage()

    def run(self, *args):
        # Magic here! :)
        ...


object = Pyjoyment_Command_mycommand

# The synopsis in the docstring of the module
"""
Pyjoyment.Command.mycommand - My command
========================================
::

    Usage: APPLICATION mycommand [OPTIONS]

    Options:
      -s, --something   Does something
"""

Pyjoyment.Command is an abstract base class for Pyjoyment commands.

See Pyjoyment.Commands for a list of commands that are available by default.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojolicious::Command of Mojolicious 9.49.

Classes

class Pyjoyment.Command.Pyjoyment_Command(**kwargs: Any)[source]

Pyjoyment.Command inherits all methods from Pyjo.Base and implements the following new ones.

property app: Pyjoyment.object[source]
app = command.app
command.app = Pyjoyment.new()

Application for command, defaults to a Pyjoyment.HelloWorld object. Note that this attribute is weakened.

# Introspect
for path in command.app.renderer.paths:
    print(f'Template path: {path}')
chmod_file(path: str | PathLike[str], mode: int) → Self[source]
command = command.chmod_file('/home/sri/foo.txt', 0o644)

Change mode of a file.

chmod_rel_file(path: str, mode: int) → Self[source]
command = command.chmod_rel_file('foo/foo.txt', 0o644)

Portably change mode of a file relative to the current working directory.

create_dir(path: str | PathLike[str]) → Self[source]
command = command.create_dir('/home/sri/foo/bar')

Create a directory if it does not exist already.

create_rel_dir(path: str) → Self[source]
command = command.create_rel_dir('foo/bar/baz')

Portably create a directory relative to the current working directory if it does not exist already.

description: str = 'No description'
description = command.description
command.description = 'Foo'

Short description of command, used for the command list.

extract_usage() → str[source]
usage = command.extract_usage()

Extract usage message from the synopsis of the docstring of the file this method was called from with Pyjo.Util.extract_usage().

help(*args: str) → None[source]
command.help()

Print usage information for command.

quiet: bool | None = None
boolean = command.quiet
command.quiet = boolean

Limited command output.

rel_file(path: str) → Pyjo_File[source]
path = command.rel_file('foo/bar.txt')

Return a Pyjo.File object relative to the current working directory.

render_data(name: str, *args: Any) → str[source]
data = command.render_data('foo_bar')
data = command.render_data('foo_bar', *args)
data = command.render_data('foo_bar', {'foo': 'bar'})

Render a template from the DATA section of the module of the command with Pyjo.Loader and Pyjo.Template. The template can be configured with template.

render_to_file(data: str, path: str | PathLike[str], *args: Any) → Self[source]
command = command.render_to_file('foo_bar', '/home/sri/foo.txt')
command = command.render_to_file('foo_bar', '/home/sri/foo.txt', *args)
command = command.render_to_file('foo_bar', '/home/sri/foo.txt', {'foo': 'bar'})

Render a template with render_data() to a file if it does not exist already, and create the directory if necessary.

render_to_rel_file(data: str, path: str, *args: Any) → Self[source]
command = command.render_to_rel_file('foo_bar', 'foo/bar.txt')
command = command.render_to_rel_file('foo_bar', 'foo/bar.txt', *args)
command = command.render_to_rel_file('foo_bar', 'foo/bar.txt', {'foo': 'bar'})

Portably render a template with render_data() to a file relative to the current working directory if it does not exist already, and create the directory if necessary.

run(*args: str) → Any[source]
command.run()
command.run(*sys.argv[1:])

Run command. Meant to be overloaded in a subclass.

template: dict[str, Any]
template = command.template
command.template = {'vars': True}

Attribute values passed to Pyjo.Template objects used to render templates with render_data(), defaults to activating vars.

usage: str = 'Usage: APPLICATION\n'
usage = command.usage
command.usage = 'Foo'

Usage information for command, used for the help screen.

write_file(path: str | PathLike[str], data: str | bytes) → Self[source]
command = command.write_file('/home/sri/foo.txt', 'Hello World!')

Write text to a file if it does not exist already, and create the directory if necessary.

write_rel_file(path: str, data: str | bytes) → Self[source]
command = command.write_rel_file('foo/bar.txt', 'Hello World!')

Portably write text to a file relative to the current working directory if it does not exist already, and create the directory if necessary.