CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
alias.py78 linesDownload Raw Back to command
1from setuptools.command.setopt import config_file, edit_config, option_base2 3from distutils.errors import DistutilsOptionError4 5 6def shquote(arg):7    """Quote an argument for later parsing by shlex.split()"""8    for c in '"', "'", "\\", "#":9        if c in arg:10            return repr(arg)11    if arg.split() != [arg]:12        return repr(arg)13    return arg14 15 16class alias(option_base):17    """Define a shortcut that invokes one or more commands"""18 19    description = "define a shortcut to invoke one or more commands"20    command_consumes_arguments = True21 22    user_options = [23        ('remove', 'r', 'remove (unset) the alias'),24    ] + option_base.user_options25 26    boolean_options = option_base.boolean_options + ['remove']27 28    def initialize_options(self):29        option_base.initialize_options(self)30        self.args = None31        self.remove = None32 33    def finalize_options(self) -> None:34        option_base.finalize_options(self)35        if self.remove and len(self.args) != 1:36            raise DistutilsOptionError(37                "Must specify exactly one argument (the alias name) when using --remove"38            )39 40    def run(self) -> None:41        aliases = self.distribution.get_option_dict('aliases')42 43        if not self.args:44            print("Command Aliases")45            print("---------------")46            for alias in aliases:47                print("setup.py alias", format_alias(alias, aliases))48            return49 50        elif len(self.args) == 1:51            (alias,) = self.args52            if self.remove:53                command = None54            elif alias in aliases:55                print("setup.py alias", format_alias(alias, aliases))56                return57            else:58                print(f"No alias definition found for {alias!r}")59                return60        else:61            alias = self.args[0]62            command = ' '.join(map(shquote, self.args[1:]))63 64        edit_config(self.filename, {'aliases': {alias: command}}, self.dry_run)65 66 67def format_alias(name, aliases):68    source, command = aliases[name]69    if source == config_file('global'):70        source = '--global-config '71    elif source == config_file('user'):72        source = '--user-config '73    elif source == config_file('local'):74        source = ''75    else:76        source = f'--filename={source!r}'77    return source + name + ' ' + command78 
Aluode/PerceptionLabPortable · CoolFace