"Fossies" - the Fresh Open Source Software Archive

Member "js-beautify-1.14.0/python/jsbeautifier/__init__.py" (14 Jun 2021, 11340 Bytes) of package /linux/www/js-beautify-1.14.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "__init__.py": 1.13.13_vs_1.14.0.

    1 from __future__ import print_function
    2 import sys
    3 import os
    4 import platform
    5 import io
    6 import getopt
    7 import re
    8 import string
    9 import errno
   10 import copy
   11 import glob
   12 from jsbeautifier.__version__ import __version__
   13 from jsbeautifier.cli import *
   14 from jsbeautifier.javascript.options import BeautifierOptions
   15 from jsbeautifier.javascript.beautifier import Beautifier
   16 
   17 #
   18 # The MIT License (MIT)
   19 
   20 # Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
   21 
   22 # Permission is hereby granted, free of charge, to any person
   23 # obtaining a copy of this software and associated documentation files
   24 # (the "Software"), to deal in the Software without restriction,
   25 # including without limitation the rights to use, copy, modify, merge,
   26 # publish, distribute, sublicense, and/or sell copies of the Software,
   27 # and to permit persons to whom the Software is furnished to do so,
   28 # subject to the following conditions:
   29 
   30 # The above copyright notice and this permission notice shall be
   31 # included in all copies or substantial portions of the Software.
   32 
   33 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   34 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   35 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   36 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   37 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   38 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   39 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   40 # SOFTWARE.
   41 #
   42 # Originally written by Einar Lielmanis et al.,
   43 # Conversion to python by Einar Lielmanis, einar@beautifier.io,
   44 # Parsing improvement for brace-less and semicolon-less statements
   45 #    by Liam Newman <bitwiseman@beautifier.io>
   46 # Python is not my native language, feel free to push things around.
   47 #
   48 # Use either from command line (script displays its usage when run
   49 # without any parameters),
   50 #
   51 #
   52 # or, alternatively, use it as a module:
   53 #
   54 #   import jsbeautifier
   55 #   res = jsbeautifier.beautify('your javascript string')
   56 #   res = jsbeautifier.beautify_file('some_file.js')
   57 #
   58 #  you may specify some options:
   59 #
   60 #   opts = jsbeautifier.default_options()
   61 #   opts.indent_size = 2
   62 #   res = jsbeautifier.beautify('some javascript', opts)
   63 #
   64 #
   65 # Here are the available options: (read source)
   66 
   67 __all__ = [
   68     "default_options",
   69     "beautify",
   70     "beautify_file",
   71     "usage",
   72     "main",
   73 ]
   74 
   75 
   76 def default_options():
   77     return BeautifierOptions()
   78 
   79 
   80 def beautify(string, opts=default_options()):
   81     b = Beautifier()
   82     return b.beautify(string, opts)
   83 
   84 
   85 def beautify_file(file_name, opts=default_options()):
   86     return process_file(file_name, opts, beautify)
   87 
   88 
   89 def usage(stream=sys.stdout):
   90 
   91     print(
   92         "jsbeautifier.py@"
   93         + __version__
   94         + """
   95 
   96 Javascript beautifier (https://beautifier.io/)
   97 
   98 Usage: jsbeautifier.py [options] <infile>
   99 
  100     <infile> can be "-", which means stdin.
  101 
  102 Input options:
  103 
  104  -i,  --stdin                      Read input from stdin
  105 
  106 Output options:
  107 
  108  -s,  --indent-size=NUMBER         Indentation size. (default 4).
  109  -c,  --indent-char=CHAR           Character to indent with. (default space).
  110  -e,  --eol=STRING                 Character(s) to use as line terminators.
  111                                    (default first newline in file, otherwise "\\n")
  112  -t,  --indent-with-tabs           Indent with tabs, overrides -s and -c
  113  -d,  --disable-preserve-newlines  Do not preserve existing line breaks.
  114  -P,  --space-in-paren             Add padding spaces within paren, ie. f( a, b )
  115  -E,  --space-in-empty-paren       Add a single space inside empty paren, ie. f( )
  116  -j,  --jslint-happy               More jslint-compatible output
  117  -a,  --space-after-anon-function  Add a space before an anonymous function's parens, ie. function ()
  118  --space-after-named-function      Add a space before a named function's parens, i.e. function example ()
  119  -b,  --brace-style=collapse       Brace style (collapse, expand, end-expand, none)(,preserve-inline)
  120  -k,  --keep-array-indentation     Keep array indentation.
  121  --quiet                           Suppress info about a file if nothing was changed.
  122  -r,  --replace                    Write output in-place, replacing input
  123  -o,  --outfile=FILE               Specify a file to output to (default stdout)
  124  -f,  --keep-function-indentation  Do not re-indent function bodies defined in var lines.
  125  -x,  --unescape-strings           Decode printable chars encoded in \\xNN notation.
  126  -X,  --e4x                        Pass E4X xml literals through untouched
  127  -C,  --comma-first                Put commas at the beginning of new line instead of end.
  128  -m,
  129  --max-preserve-newlines=NUMBER    Number of line-breaks to be preserved in one chunk (default 10)
  130  -O,  --operator-position=STRING   Set operator position (before-newline, after-newline, preserve-newline)
  131  -w,  --wrap-line-length           Attempt to wrap line when it exceeds this length.
  132                                    NOTE: Line continues until next wrap point is found.
  133  -n,  --end-with-newline           End output with newline
  134  --indent-empty-lines              Keep indentation on empty lines
  135  --templating                      List of templating languages (auto,none,django,erb,handlebars,php,smarty) ["auto"] auto = none in JavaScript, all in html
  136  --editorconfig                    Enable setting configuration from EditorConfig
  137 
  138 Rarely needed options:
  139 
  140  --eval-code                       evaluate code if a JS interpreter is
  141                                    installed. May be useful with some obfuscated
  142                                    script but poses a potential security issue.
  143 
  144  -l,  --indent-level=NUMBER        Initial indentation level. (default 0).
  145 
  146  -h,  --help, --usage              Prints this help statement.
  147  -v,  --version                    Show the version
  148 
  149 """,
  150         file=stream,
  151     )
  152     if stream == sys.stderr:
  153         return 1
  154     else:
  155         return 0
  156 
  157 
  158 def main():
  159 
  160     argv = sys.argv[1:]
  161 
  162     try:
  163         opts, args = getopt.getopt(
  164             argv,
  165             "f:s:c:e:o:rdEPjab:kil:xhtvXnCO:w:m:",
  166             [
  167                 "brace-style=",
  168                 "comma-first",
  169                 "disable-preserve-newlines",
  170                 "e4x",
  171                 "editorconfig",
  172                 "end-with-newline",
  173                 "eol=",
  174                 "eval-code",
  175                 "file=",
  176                 "help",
  177                 "indent-char=",
  178                 "indent-empty-lines",
  179                 "indent-level=",
  180                 "indent-size=",
  181                 "indent-with-tabs",
  182                 "jslint-happy",
  183                 "keep-array-indentation",
  184                 "keep-function-indentation",
  185                 "max-preserve-newlines=",
  186                 "operator-position=",
  187                 "outfile=",
  188                 "quiet",
  189                 "replace",
  190                 "space-after-anon-function",
  191                 "space-after-named-function",
  192                 "space-in-empty-paren",
  193                 "space-in-paren",
  194                 "stdin",
  195                 "templating",
  196                 "unescape-strings",
  197                 "usage",
  198                 "version",
  199                 "wrap-line-length",
  200             ],
  201         )
  202     except getopt.GetoptError as ex:
  203         print(ex, file=sys.stderr)
  204         return usage(sys.stderr)
  205 
  206     js_options = default_options()
  207 
  208     filepath_params = []
  209     filepath_params.extend(args)
  210 
  211     outfile_param = "stdout"
  212     replace = False
  213 
  214     for opt, arg in opts:
  215         if opt in ("--file", "-f"):
  216             filepath_params.append(arg)
  217         elif opt in ("--keep-array-indentation", "-k"):
  218             js_options.keep_array_indentation = True
  219         elif opt in ("--keep-function-indentation",):
  220             js_options.keep_function_indentation = True
  221         elif opt in ("--outfile", "-o"):
  222             outfile_param = arg
  223         elif opt in ("--replace", "-r"):
  224             replace = True
  225         elif opt in ("--indent-size", "-s"):
  226             js_options.indent_size = int(arg)
  227         elif opt in ("--indent-char", "-c"):
  228             js_options.indent_char = arg
  229         elif opt in ("--eol", "-e"):
  230             js_options.eol = arg
  231         elif opt in ("--indent-with-tabs", "-t"):
  232             js_options.indent_with_tabs = True
  233         elif opt in ("--disable-preserve-newlines", "-d"):
  234             js_options.preserve_newlines = False
  235         elif opt in ("--max-preserve-newlines", "-m"):
  236             js_options.max_preserve_newlines = int(arg)
  237         elif opt in ("--space-in-paren", "-P"):
  238             js_options.space_in_paren = True
  239         elif opt in ("--space-in-empty-paren", "-E"):
  240             js_options.space_in_empty_paren = True
  241         elif opt in ("--jslint-happy", "-j"):
  242             js_options.jslint_happy = True
  243         elif opt in ("--space-after-anon-function", "-a"):
  244             js_options.space_after_anon_function = True
  245         elif opt in ("--space-after-named-function",):
  246             js_options.space_after_named_function = True
  247         elif opt in ("--eval-code",):
  248             js_options.eval_code = True
  249         elif opt in ("--quiet",):
  250             js_options.keep_quiet = True
  251         elif opt in ("--brace-style", "-b"):
  252             js_options.brace_style = arg
  253         elif opt in ("--unescape-strings", "-x"):
  254             js_options.unescape_strings = True
  255         elif opt in ("--e4x", "-X"):
  256             js_options.e4x = True
  257         elif opt in ("--end-with-newline", "-n"):
  258             js_options.end_with_newline = True
  259         elif opt in ("--comma-first", "-C"):
  260             js_options.comma_first = True
  261         elif opt in ("--operator-position", "-O"):
  262             js_options.operator_position = arg
  263         elif opt in ("--wrap-line-length ", "-w"):
  264             js_options.wrap_line_length = int(arg)
  265         elif opt in ("--indent-empty-lines",):
  266             js_options.indent_empty_lines = True
  267         elif opt in ("--templating",):
  268             js_options.templating = arg.split(",")
  269         elif opt in ("--stdin", "-i"):
  270             # stdin is the default if no files are passed
  271             filepath_params = []
  272         elif opt in ("--editorconfig",):
  273             js_options.editorconfig = True
  274         elif opt in ("--version", "-v"):
  275             return print(__version__)
  276         elif opt in ("--help", "--usage", "-h"):
  277             return usage()
  278 
  279     try:
  280         filepaths, replace = get_filepaths_from_params(filepath_params, replace)
  281         for filepath in filepaths:
  282             if not replace:
  283                 outfile = outfile_param
  284             else:
  285                 outfile = filepath
  286 
  287             js_options = integrate_editorconfig_options(
  288                 filepath, js_options, outfile, "js"
  289             )
  290 
  291             pretty = beautify_file(filepath, js_options)
  292 
  293             write_beautified_output(pretty, js_options, outfile)
  294 
  295     except MissingInputStreamError:
  296         print("Must pipe input or define at least one file.\n", file=sys.stderr)
  297         usage(sys.stderr)
  298         return 1
  299 
  300     except UnicodeError as ex:
  301         print("Error while decoding input or encoding output:", file=sys.stderr)
  302         print(ex, file=sys.stderr)
  303         return 1
  304 
  305     except Exception as ex:
  306         print(ex, file=sys.stderr)
  307         return 1
  308 
  309     # Success
  310     return 0
  311 
  312 
  313 if __name__ == "__main__":
  314     main()