Beispiel #1
0
    def title(self):
        arguments = ' '.join(self.args) + ' ' if len(self.args) > 0 else ''
        whole_text = ' {} {}'.format(self.name, arguments)
        border = '-' * len(whole_text)

        Output.coloredln(border, Color.DarkGray)
        Output.colored(' a', Color.LightBlue)
        Output.colored('2', Color.LightGreen)
        Output.colored('x', Color.Yellow)
        Output.colored('{} '.format(self.name[3 : ]), Color.White)
        print(arguments)
        Output.coloredln(border, Color.DarkGray)
Beispiel #2
0
class Tool:
    def __init__(self, arg_names):
        quiet = False

        args = [a.strip() for a in sys.argv[1:]]
        args = [a for a in args if len(a) > 0]

        if len(args) > 0 and args[0] == '-q':
            quiet = True
            args = args[1:]

        self.name = os.path.basename(sys.argv[0])
        self.output = Output(quiet)
        self.arg_names = arg_names.split()
        self.arg_values = args
        self.arg_db = {}
        self.args_tail = self.arg_values[len(self.arg_names):]

        required_num = 0
        optional_num = 0

        for name in self.arg_names:
            if name[0] == '[' and name[-1] == ']':
                optional_num += 1
            else:
                required_num += 1

                # Optional args are only allowed after all required args
                if optional_num > 0:
                    self.usage()

        if len(self.arg_values) < required_num:
            self.usage()

        for name, value in zip(self.arg_names, self.arg_values):
            self.arg_db[name] = value

        current_dir = os.path.dirname(__file__)
        self.dir_a2x = os.path.abspath(os.path.join(current_dir, '..', '..'))
        self.dir_bin = os.path.join(self.dir_a2x, 'bin')
        self.dir_cfg = os.path.join(os.environ['HOME'], '.config', 'a2x')
        self.dir_make = os.path.join(self.dir_a2x, 'make')
        self.dir_src = os.path.join(self.dir_a2x, 'src')

        if not os.path.exists(self.dir_cfg):
            os.makedirs(self.dir_cfg)
        elif not os.path.isdir(self.dir_cfg):
            self.output.error('{} is not a dir'.format(self.dir_cfg))

    def title(self):
        self.output.colored('a', Color.LightBlue)
        self.output.colored('2', Color.LightGreen)
        self.output.colored('x', Color.Yellow)
        self.output.colored('{} '.format(self.name[3:]), Color.White)
        self.output.coloredln(' '.join(self.arg_values), Color.LightGray)

    def done(self):
        self.output.coloredln('[ Done ]', Color.LightGreen)

    def usage(self):
        message = 'Usage: {}'.format(self.name)

        for arg in self.arg_names:
            message += ' {}'.format(arg)

        self.output.error(message)

    def get_arg(self, name):
        if name in self.arg_db:
            return self.arg_db[name]
        else:
            return ''

    def get_arg_tail(self):
        return self.args_tail

    def main(self):
        self.output.error('{} does not implement main'.format(self.name))

    def run(self):
        self.title()
        self.main()
        self.done()

    def writefile(self, name, contents):
        self.output.info('Writing file {}'.format(name))

        with open(name, 'w') as f:
            f.write(contents)

    def readbytes(self, name):
        with open(name, 'rb') as f:
            return f.read()

    def readtext(self, name):
        with open(name, 'rU') as f:
            return f.read()

    def readtextlines(self, name):
        with open(name, 'rU') as f:
            return f.readlines()

    def listdir(self, path):
        if not os.path.isdir(path):
            self.output.error('{} is not a dir'.format(path))

        return sorted(os.listdir(path))

    def shell(self, cmd):
        self.output.shell(cmd)
        status, output = subprocess.getstatusoutput(cmd)

        if not self.output.quiet:
            for line in output.splitlines():
                print('    {}'.format(line))

        if status != 0:
            sys.exit(status)

    def sanitizeFileNameForCVar(self, FileName):
        return FileName.replace('.', '_').replace('-', '_').replace('/', '_')
Beispiel #3
0
 def run(self):
     self.title()
     self.validate()
     self.main()
     Output.coloredln('[ Done ]', Color.LightGreen)
Beispiel #4
0
Datei: tool.py Projekt: alxm/a2x
class Tool:
    def __init__(self, arg_names):
        quiet = False

        args = [a.strip() for a in sys.argv[1 : ]]
        args = [a for a in args if len(a) > 0]

        if len(args) > 0 and args[0] == '-q':
            quiet = True
            args = args[1 : ]

        self.name = os.path.basename(sys.argv[0])
        self.output = Output(quiet)
        self.arg_names = arg_names.split()
        self.arg_values = args
        self.arg_db = {}
        self.args_tail = self.arg_values[len(self.arg_names) :]

        required_num = 0
        optional_num = 0

        for name in self.arg_names:
            if name[0] == '[' and name[-1] == ']':
                optional_num += 1
            else:
                required_num += 1

                # Optional args are only allowed after all required args
                if optional_num > 0:
                    self.usage()

        if len(self.arg_values) < required_num:
            self.usage()

        for name, value in zip(self.arg_names, self.arg_values):
            self.arg_db[name] = value

        current_dir = os.path.dirname(__file__)
        self.dir_a2x = os.path.abspath(os.path.join(current_dir, '..', '..'))
        self.dir_bin = os.path.join(self.dir_a2x, 'bin')
        self.dir_cfg = os.path.join(os.environ['HOME'], '.config', 'a2x')
        self.dir_make = os.path.join(self.dir_a2x, 'make')
        self.dir_src = os.path.join(self.dir_a2x, 'src')

        if not os.path.exists(self.dir_cfg):
            os.makedirs(self.dir_cfg)
        elif not os.path.isdir(self.dir_cfg):
            self.output.error('{} is not a dir'.format(self.dir_cfg))

    def title(self):
        self.output.colored('a', Color.LightBlue)
        self.output.colored('2', Color.LightGreen)
        self.output.colored('x', Color.Yellow)
        self.output.colored('{} '.format(self.name[3 : ]), Color.White)
        self.output.coloredln(' '.join(self.arg_values), Color.LightGray)

    def done(self):
        self.output.coloredln('[ Done ]', Color.LightGreen)

    def usage(self):
        message = 'Usage: {}'.format(self.name)

        for arg in self.arg_names:
            message += ' {}'.format(arg)

        self.output.error(message)

    def get_arg(self, name):
        if name in self.arg_db:
            return self.arg_db[name]
        else:
            return ''

    def get_arg_tail(self):
        return self.args_tail

    def main(self):
        self.output.error('{} does not implement main'.format(self.name))

    def run(self):
        self.title()
        self.main()
        self.done()

    def writefile(self, name, contents):
        self.output.info('Writing file {}'.format(name))

        with open(name, 'w') as f:
            f.write(contents)

    def readbytes(self, name):
        with open(name, 'rb') as f:
            return f.read()

    def readtext(self, name):
        with open(name, 'rU') as f:
            return f.read()

    def readtextlines(self, name):
        with open(name, 'rU') as f:
            return f.readlines()

    def listdir(self, path):
        if not os.path.isdir(path):
            self.output.error('{} is not a dir'.format(path))

        return sorted(os.listdir(path))

    def shell(self, cmd):
        self.output.shell(cmd)
        status, output = subprocess.getstatusoutput(cmd)

        if not self.output.quiet:
            for line in output.splitlines():
                print('    {}'.format(line))

        if status != 0:
            sys.exit(status)

    def sanitizeFileNameForCVar(self, FileName):
        return FileName.replace('.', '_').replace('-', '_').replace('/', '_')