def _get_key_value(self, key, value): """Process a key/value pair from an INI section. :param key: The key to be processed. :type key: str :param value: The value to be processed. :rtype: tuple :returns: The key and value, both of which may be modified from the originals. """ if key in ("environments", "environs", "envs", "env"): _key = "environments" _value = split_csv(value) elif key in ("func", "function"): _key = "function" _value = value elif key == "items": _key = "items" _value = split_csv(value) elif key == "tags": _key = "tags" _value = split_csv(value) else: _key = key _value = smart_cast(value) return _key, _value
def __init__(self, name, back_title="Input", choices=None, default=None, fancy=False, help_text=None, label=None, **kwargs): """Initialize a prompt for user input. :param name: The variable name. :type name: str :param back_title: The back title of the input. Used only when ``dialog`` is enabled. :type back_title: str :param choices: Valid choices for the variable. May be given as a list of strings or a comma separated string. :type choices: list[str] | str :param default: The default value of the variable. :param fancy: Indicates the dialog command should be used. :type fancy: bool :param help_text: Additional text to display. Only use when ``fancy`` is ``True``. :type help_text: str :param label: The label of the prompt. """ self.back_title = back_title self.default = default self.dialog_enabled = fancy self.help_text = help_text self.label = label or name.replace("_", " ").title() self.variable_name = name if type(choices) in (list, tuple): self.choices = choices elif type(choices) is str: self.choices = split_csv(choices, smart=False) # for i in choices.split(","): # self.choices.append(i.strip()) else: self.choices = None kwargs.setdefault("comment", "prompt user for %s input" % name) super().__init__(name, **kwargs)
def user(name, groups=None, home=None, op="add", password=None, **kwargs): """Create or remove a user. - name (str): The user name. - groups (str | list): A list of groups to which the user should belong. - home (str): The path to the user's home directory. - op (str); The operation to perform; ``add`` or ``remove``. - password (str): The user's password. (NOT IMPLEMENTED) """ if op == "add": kwargs.setdefault("comment", "create a user named %s" % name) commands = list() # The gecos switch eliminates the prompts. a = list() a.append('adduser %s --disabled-password --gecos ""' % name) if home is not None: a.append("--home %s" % home) commands.append(Command(" ".join(a), **kwargs)) if type(groups) is str: groups = split_csv(groups, smart=False) if type(groups) in [list, tuple]: for group in groups: commands.append( Command("adduser %s %s" % (name, group), **kwargs)) a = list() for c in commands: a.append(c.get_statement(suppress_comment=True)) return Command("\n".join(a), **kwargs) elif op == "remove": kwargs.setdefault("comment", "remove a user named %s" % name) return Command("deluser %s" % name, **kwargs) else: raise NameError("Unsupported or unrecognized operation: %s" % op)
def _load_variables_ini(path, environment=None): """Load variables from an INI file. See ``load_variables()``.""" ini = RawConfigParser() ini.read(path) a = list() for section in ini.sections(): if ":" in section: variable_name, _environment = section.split(":") else: _environment = None variable_name = section _kwargs = { 'environment': _environment, } for key, value in ini.items(section): if key == "tags": value = split_csv(value) else: value = smart_cast(value) _kwargs[key] = value a.append(Variable(variable_name, **_kwargs)) if environment is not None: b = list() for var in a: if var.environment and var.environment == environment or var.environment is None: b.append(var) return b return a