File: __init__.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (56 lines) | stat: -rw-r--r-- 1,782 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Manages Reserved Capacity."""
# :license: MIT, see LICENSE for more details.

import importlib
import os

import click
from SoftLayer.CLI.command import CommandLoader
from SoftLayer.CLI.command import OptionHighlighter

CONTEXT = {'help_option_names': ['-h', '--help'],
           'max_content_width': 999}


class CapacityCommands(CommandLoader):
    """Loads module for capacity related commands.

    Will automatically replace _ with - where appropriate.
    I'm not sure if this is better or worse than using a long list of manual routes, so I'm trying it here.
    CLI/virt/capacity/create_guest.py -> slcli vs capacity create-guest
    """

    def __init__(self, **attrs):
        CommandLoader.__init__(self, **attrs)
        self.path = os.path.dirname(__file__)
        self.highlighter = OptionHighlighter()
        self.env = None
        self.console = None

    def list_commands(self, ctx):
        """List all sub-commands."""
        commands = []
        for filename in os.listdir(self.path):
            if filename == '__init__.py':
                continue
            if filename.endswith('.py'):
                commands.append(filename[:-3].replace("_", "-"))
        commands.sort()
        return commands

    def get_command(self, ctx, cmd_name):
        """Get command for click."""
        path = "%s.%s" % (__name__, cmd_name)
        path = path.replace("-", "_")
        try:
            module = importlib.import_module(path)
            return getattr(module, 'cli')
        except ModuleNotFoundError as ex:
            print(ex.name)
            return None


# Required to get the sub-sub-sub command to work.
@click.group(cls=CapacityCommands, context_settings=CONTEXT)
def cli():
    """Base command for all capacity related concerns"""