def decompress_mini_js(self, js_file):
        """
        解压被压缩的JS文件
        :param js_file:
        :return:
        """
        js_options = BeautifierOptions()
        replace = True
        outfile = js_file
        try:
            if replace:
                outfile = js_file

            pretty = beautify_file(js_file, js_options)

            if outfile == 'stdout':
                sys.stdout.write(pretty)
            else:
                if isFileDifferent(outfile, pretty):
                    mkdir_p(os.path.dirname(outfile))
                    with open(outfile, 'w') as f:
                        f.write(pretty)

        except Exception as ex:
            raise H5DetectorException(
                'There is an exception when'
                ' decompressing js file: {}'.format(js_file))
    def decompress_mini_js(self, js_file):
        """
        解压被压缩的JS文件
        :param js_file:
        :return:
        """
        js_options = BeautifierOptions()
        replace = True
        outfile = js_file
        try:
            if replace:
                outfile = js_file

            pretty = beautify_file(js_file, js_options)

            if outfile == 'stdout':
                sys.stdout.write(pretty)
            else:
                if isFileDifferent(outfile, pretty):
                    mkdir_p(os.path.dirname(outfile))
                    with open(outfile, 'w') as f:
                        f.write(pretty)

        except Exception as ex:
            raise H5DetectorException('There is an exception when'
                                      ' decompressing js file: {}'.format(js_file))
Exemplo n.º 3
0
def main():

    argv = sys.argv[1:]

    try:
        opts, args = getopt.getopt(argv, "hvio:rs:c:e:tn", [
            'help', 'usage', 'version', 'stdin', 'outfile=', 'replace',
            'indent-size=', 'indent-char=', 'eol=', 'indent-with-tabs',
            'preserve-newlines', 'disable-selector-separator-newline',
            'end-with-newline', 'disable-newline-between-rules',
            'space-around-combinator'
        ])
    except getopt.GetoptError as ex:
        print(ex, file=sys.stderr)
        return usage(sys.stderr)

    css_options = default_options()

    file = None
    outfile = 'stdout'
    replace = False
    if len(args) == 1:
        file = args[0]

    for opt, arg in opts:
        if opt in ('--stdin', '-i'):
            file = '-'
        elif opt in ('--outfile', '-o'):
            outfile = arg
        elif opt in ('--replace', '-r'):
            replace = True
        elif opt in ('--version', '-v'):
            return print(__version__)
        elif opt in ('--help', '--usage', '-h'):
            return usage()

        elif opt in ('--indent-size', '-s'):
            css_options.indent_size = int(arg)
        elif opt in ('--indent-char', '-c'):
            css_options.indent_char = arg
        elif opt in ('--eol', '-e'):
            css_options.eol = arg
        elif opt in ('--indent-with-tabs', '-t'):
            css_options.indent_with_tabs = True
        elif opt in ('--preserve-newlines'):
            css_options.preserve_newlines = True
        elif opt in ('--disable-selector-separator-newline'):
            css_options.selector_separator_newline = False
        elif opt in ('--end-with-newline', '-n'):
            css_options.end_with_newline = True
        elif opt in ('--disable-newline-between-rules'):
            css_options.newline_between_rules = False
        elif opt in ('--space-around-combinator'):
            css_options.space_around_combinator = True

    if not file:
        file = '-'

    try:
        if outfile == 'stdout' and replace and not file == '-':
            outfile = file

        pretty = beautify_file(file, css_options)

        if outfile == 'stdout':
            # python automatically converts newlines in text to "\r\n" when on windows
            # switch to binary to prevent this
            if sys.platform == "win32":
                import msvcrt
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

            sys.stdout.write(pretty)
        else:
            if isFileDifferent(outfile, pretty):
                mkdir_p(os.path.dirname(outfile))

                # python automatically converts newlines in text to "\r\n" when on windows
                # set newline to empty to prevent this
                with io.open(outfile, 'wt', newline='') as f:
                    print('writing ' + outfile, file=sys.stderr)
                    try:
                        f.write(pretty)
                    except TypeError:
                        # This is not pretty, but given how we did the version import
                        # it is the only way to do this without having setup.py
                        # fail on a missing six dependency.
                        six = __import__("six")
                        f.write(six.u(pretty))

    except Exception as ex:
        print(ex, file=sys.stderr)
        return 1

    # Success
    return 0
Exemplo n.º 4
0
def main():

    argv = sys.argv[1:]

    try:
        opts, args = getopt.getopt(argv, "hvio:rs:c:e:tn",
                                   ['help', 'usage', 'version', 'stdin', 'outfile=', 'replace',
                                    'indent-size=', 'indent-char=', 'eol=', 'indent-with-tabs',
                                    'preserve-newlines', 'disable-selector-separator-newline',
                                    'end-with-newline', 'disable-newline-between-rules',
                                    'space-around-combinator'])
    except getopt.GetoptError as ex:
        print(ex, file=sys.stderr)
        return usage(sys.stderr)

    css_options = default_options()

    file = None
    outfile = 'stdout'
    replace = False
    if len(args) == 1:
        file = args[0]

    for opt, arg in opts:
        if opt in ('--stdin', '-i'):
            file = '-'
        elif opt in ('--outfile', '-o'):
            outfile = arg
        elif opt in ('--replace', '-r'):
            replace = True
        elif opt in ('--version', '-v'):
            return print(__version__)
        elif opt in ('--help', '--usage', '-h'):
            return usage()

        elif opt in ('--indent-size', '-s'):
            css_options.indent_size = int(arg)
        elif opt in ('--indent-char', '-c'):
            css_options.indent_char = arg
        elif opt in ('--eol', '-e'):
            css_options.eol = arg
        elif opt in ('--indent-with-tabs', '-t'):
            css_options.indent_with_tabs = True
        elif opt in ('--preserve-newlines'):
            css_options.preserve_newlines = True
        elif opt in ('--disable-selector-separator-newline'):
            css_options.selector_separator_newline = False
        elif opt in ('--end-with-newline', '-n'):
            css_options.end_with_newline = True
        elif opt in ('--disable-newline-between-rules'):
            css_options.newline_between_rules = False
        elif opt in ('--space-around-combinator'):
            css_options.space_around_combinator = True

    if not file:
        file = '-'

    try:
        if outfile == 'stdout' and replace and not file == '-':
            outfile = file

        pretty = beautify_file(file, css_options)

        if outfile == 'stdout':
            # python automatically converts newlines in text to "\r\n" when on windows
            # switch to binary to prevent this
            if sys.platform == "win32":
                import msvcrt
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

            sys.stdout.write(pretty)
        else:
            if isFileDifferent(outfile, pretty):
                mkdir_p(os.path.dirname(outfile))

                # python automatically converts newlines in text to "\r\n" when on windows
                # set newline to empty to prevent this
                with io.open(outfile, 'wt', newline='') as f:
                    print('writing ' + outfile, file=sys.stderr)
                    try:
                        f.write(pretty)
                    except TypeError:
                        # This is not pretty, but given how we did the version import
                        # it is the only way to do this without having setup.py
                        # fail on a missing six dependency.
                        six = __import__("six")
                        f.write(six.u(pretty))

    except Exception as ex:
        print(ex, file=sys.stderr)
        return 1

    # Success
    return 0
Exemplo n.º 5
0
def main():

    argv = sys.argv[1:]

    try:
        opts, args = getopt.getopt(
            argv,
            "hvio:rs:c:e:tn",
            [
                "help",
                "usage",
                "version",
                "stdin",
                "outfile=",
                "replace",
                "indent-size=",
                "indent-char=",
                "eol=",
                "indent-with-tabs",
                "preserve-newlines",
                "disable-selector-separator-newline",
                "end-with-newline",
                "disable-newline-between-rules",
                "space-around-combinator",
                "indent-empty-lines",
            ],
        )
    except getopt.GetoptError as ex:
        print(ex, file=sys.stderr)
        return usage(sys.stderr)

    css_options = default_options()

    file = None
    outfile = "stdout"
    replace = False
    if len(args) == 1:
        file = args[0]

    for opt, arg in opts:
        if opt in ("--stdin", "-i"):
            file = "-"
        elif opt in ("--outfile", "-o"):
            outfile = arg
        elif opt in ("--replace", "-r"):
            replace = True
        elif opt in ("--version", "-v"):
            return print(__version__)
        elif opt in ("--help", "--usage", "-h"):
            return usage()

        elif opt in ("--indent-size", "-s"):
            css_options.indent_size = int(arg)
        elif opt in ("--indent-char", "-c"):
            css_options.indent_char = arg
        elif opt in ("--eol", "-e"):
            css_options.eol = arg
        elif opt in ("--indent-with-tabs", "-t"):
            css_options.indent_with_tabs = True
        elif opt in ("--preserve-newlines"):
            css_options.preserve_newlines = True
        elif opt in ("--disable-selector-separator-newline"):
            css_options.selector_separator_newline = False
        elif opt in ("--end-with-newline", "-n"):
            css_options.end_with_newline = True
        elif opt in ("--disable-newline-between-rules"):
            css_options.newline_between_rules = False
        elif opt in ("--space-around-combinator"):
            css_options.space_around_combinator = True
        elif opt in ("--indent-empty-lines"):
            css_options.indent_empty_lines = True

    if not file:
        file = "-"

    try:
        if outfile == "stdout" and replace and not file == "-":
            outfile = file

        pretty = beautify_file(file, css_options)

        if outfile == "stdout":
            # python automatically converts newlines in text to "\r\n" when on windows
            # switch to binary to prevent this
            if sys.platform == "win32":
                import msvcrt

                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

            sys.stdout.write(pretty)
        else:
            if isFileDifferent(outfile, pretty):
                mkdir_p(os.path.dirname(outfile))

                # python automatically converts newlines in text to "\r\n" when on windows
                # set newline to empty to prevent this
                with io.open(outfile, "wt", newline="") as f:
                    print("writing " + outfile, file=sys.stderr)
                    try:
                        f.write(pretty)
                    except TypeError:
                        # This is not pretty, but given how we did the version import
                        # it is the only way to do this without having setup.py
                        # fail on a missing six dependency.
                        six = __import__("six")
                        f.write(six.u(pretty))

    except Exception as ex:
        print(ex, file=sys.stderr)
        return 1

    # Success
    return 0