"Fossies" - the Fresh Open Source Software Archive

Member "js-beautify-1.14.0/python/cssbeautifier/_main.py" (14 Jun 2021, 6931 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 "_main.py": 1.13.13_vs_1.14.0.

    1 #
    2 # The MIT License (MIT)
    3 
    4 # Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
    5 
    6 # Permission is hereby granted, free of charge, to any person
    7 # obtaining a copy of this software and associated documentation files
    8 # (the "Software"), to deal in the Software without restriction,
    9 # including without limitation the rights to use, copy, modify, merge,
   10 # publish, distribute, sublicense, and/or sell copies of the Software,
   11 # and to permit persons to whom the Software is furnished to do so,
   12 # subject to the following conditions:
   13 
   14 # The above copyright notice and this permission notice shall be
   15 # included in all copies or substantial portions of the Software.
   16 
   17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   21 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   22 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   23 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   24 # SOFTWARE.
   25 
   26 from __future__ import print_function
   27 import sys
   28 import os
   29 import io
   30 import re
   31 import copy
   32 import getopt
   33 from cssbeautifier.__version__ import __version__
   34 from jsbeautifier.cli import *
   35 from cssbeautifier.css.options import BeautifierOptions
   36 from cssbeautifier.css.beautifier import Beautifier
   37 
   38 __all__ = ["default_options", "beautify", "beautify_file", "usage", "main"]
   39 
   40 
   41 def default_options():
   42     return BeautifierOptions()
   43 
   44 
   45 def beautify(string, opts=None):
   46     b = Beautifier(string, opts)
   47     return b.beautify()
   48 
   49 
   50 def beautify_file(file_name, opts=None):
   51     return process_file(file_name, opts, beautify)
   52 
   53 
   54 def usage(stream=sys.stdout):
   55 
   56     print(
   57         "cssbeautifier.py@"
   58         + __version__
   59         + """
   60 
   61 CSS beautifier (https://beautifier.io/)
   62 
   63 Usage: cssbeautifier.py [options] <infile>
   64 
   65     <infile> can be "-", which means stdin.
   66 
   67 Input options:
   68 
   69  -i,  --stdin                      Read input from stdin
   70 
   71 Output options:
   72 
   73  -s,  --indent-size=NUMBER         Indentation size. (default 4).
   74  -c,  --indent-char=CHAR           Character to indent with. (default space).
   75  -e,  --eol=STRING                 Character(s) to use as line terminators.
   76                                    (default first newline in file, otherwise "\\n")
   77  -t,  --indent-with-tabs           Indent with tabs, overrides -s and -c
   78       --preserve-newlines          Preserve existing line breaks.
   79       --disable-selector-separator-newline
   80                                    Do not print each selector on a separate line.
   81  -b,  --brace-style=collapse       Brace style (collapse, expand)
   82  -n,  --end-with-newline           End output with newline
   83       --disable-newline-between-rules
   84                                    Do not print empty line between rules.
   85       --space-around-combinator    Print spaces around combinator.
   86       --indent-empty-lines         Keep indentation on empty lines
   87  -r,  --replace                    Write output in-place, replacing input
   88  -o,  --outfile=FILE               Specify a file to output to (default stdout)
   89 
   90 Rarely needed options:
   91 
   92  -h,  --help, --usage              Prints this help statement.
   93  -v,  --version                    Show the version
   94 
   95 """,
   96         file=stream,
   97     )
   98     if stream == sys.stderr:
   99         return 1
  100     else:
  101         return 0
  102 
  103 
  104 def main():
  105 
  106     argv = sys.argv[1:]
  107 
  108     try:
  109         opts, args = getopt.getopt(
  110             argv,
  111             "hvio:rs:c:e:tnb:",
  112             [
  113                 "editorconfig",
  114                 "help",
  115                 "usage",
  116                 "version",
  117                 "stdin",
  118                 "outfile=",
  119                 "replace",
  120                 "indent-size=",
  121                 "indent-char=",
  122                 "eol=",
  123                 "indent-with-tabs",
  124                 "preserve-newlines",
  125                 "brace-style=",
  126                 "disable-selector-separator-newline",
  127                 "end-with-newline",
  128                 "disable-newline-between-rules",
  129                 "space-around-combinator",
  130                 "indent-empty-lines",
  131             ],
  132         )
  133     except getopt.GetoptError as ex:
  134         print(ex, file=sys.stderr)
  135         return usage(sys.stderr)
  136 
  137     css_options = default_options()
  138 
  139     filepath_params = []
  140     filepath_params.extend(args)
  141 
  142     outfile_param = "stdout"
  143     replace = False
  144 
  145     for opt, arg in opts:
  146         if opt in ("--stdin", "-i"):
  147             file = "-"
  148         elif opt in ("--outfile", "-o"):
  149             outfile = arg
  150         elif opt in ("--replace", "-r"):
  151             replace = True
  152         elif opt in ("--version", "-v"):
  153             return print(__version__)
  154         elif opt in ("--help", "--usage", "-h"):
  155             return usage()
  156 
  157         elif opt in ("--indent-size", "-s"):
  158             css_options.indent_size = int(arg)
  159         elif opt in ("--indent-char", "-c"):
  160             css_options.indent_char = arg
  161         elif opt in ("--eol", "-e"):
  162             css_options.eol = arg
  163         elif opt in ("--indent-with-tabs", "-t"):
  164             css_options.indent_with_tabs = True
  165         elif opt in ("--preserve-newlines",):
  166             css_options.preserve_newlines = True
  167         elif opt in ("--disable-selector-separator-newline",):
  168             css_options.selector_separator_newline = False
  169         elif opt in ("--brace-style", "-b"):
  170             css_options.brace_style = arg
  171         elif opt in ("--end-with-newline", "-n"):
  172             css_options.end_with_newline = True
  173         elif opt in ("--disable-newline-between-rules",):
  174             css_options.newline_between_rules = False
  175         elif opt in ("--space-around-combinator",):
  176             css_options.space_around_combinator = True
  177         elif opt in ("--indent-empty-lines",):
  178             css_options.indent_empty_lines = True
  179         elif opt in ("--editorconfig",):
  180             css_options.editorconfig = True
  181 
  182     try:
  183         filepaths, replace = get_filepaths_from_params(filepath_params, replace)
  184         for filepath in filepaths:
  185             if not replace:
  186                 outfile = outfile_param
  187             else:
  188                 outfile = filepath
  189 
  190             css_options = integrate_editorconfig_options(
  191                 filepath, css_options, outfile, "js"
  192             )
  193 
  194             pretty = beautify_file(filepath, css_options)
  195 
  196             write_beautified_output(pretty, css_options, outfile)
  197 
  198     except MissingInputStreamError:
  199         print("Must pipe input or define at least one file.\n", file=sys.stderr)
  200         usage(sys.stderr)
  201         return 1
  202 
  203     except UnicodeError as ex:
  204         print("Error while decoding input or encoding output:", file=sys.stderr)
  205         print(ex, file=sys.stderr)
  206         return 1
  207 
  208     except Exception as ex:
  209         print(ex, file=sys.stderr)
  210         return 1
  211 
  212     # Success
  213     return 0