Exemple #1
0
def main(argv):
    argv = argv[1:]
    try:
        opts, extra = getopt.getopt(argv, OPTIONS_ARGS, Commands.keys())
    except getopt.GetoptError as err:
        app_error(err)
        usage()
        return 1

    files = phylib.expand_path(extra)

    encoding = None
    script = None
    func = remove_null
    for opt, arg in opts:
        if opt == "-e":
            encoding = arg
        elif opt == "-f":
            if not os.path.exists(arg):
                app_error("not such script: '{0}'".format(arg))
                return 1
            script = arg
        elif opt == "-h":
            usage()
            sys.exit(0)
        else:
            func = Commands.get(opt.lstrip("-"), None)

    if not files:
        app_error("no input files")
        return 1

    if script:
        path, base = os.path.split(script)
        path = path if path else "."
        sys.path.insert(0, path)
        name = os.path.splitext(base)[0]
        try:
            mod = __import__(name)
        except Exception as err:
            msg = "cannot load script '{0}': {1}"
            app_error(msg.format(script, err))
            return 1
        try:
            func = mod.do
        except AttributeError:
            msg = "not found function 'do(lines)' in script '{0}'"
            app_error(msg.format(script))
            return 1
    elif func is None:
        return 1

    for file in files:
        app_echo(file)
        split_to_lines(file, func, encoding)

    return 0
Exemple #2
0
def split_to_lines(file, func, encoding=None):
    try:
        fp = open(file, "rb")
    except IOError as err:
        app_error(err)
        return

    data = fp.read()
    fp.close()
    if encoding:
        data = phylib.strip_bom(data)
        try:
            text = data.decode(encoding)
        except UnicodeDecodeError:
            app_error("invalid encoding: '{0}'".format(encoding))
            return
    else:
        text, encoding = phylib.decode_text(data)
        if text is None:
            err = "cannot decode text file: '{0}'"
            app_error(err.format(file))
            return

    lines = func(text.splitlines())
    if not isinstance(lines, list):
        app_error("expected 'list' returned value")
        return

    text = phylib.LN.join(lines)
    data = text.encode(encoding)
    try:
        fp = open(file, "wb")
    except IOError as err:
        app_error(err)
        return

    fp.write(data)
    fp.close()