File indexing completed on 2024-12-08 13:31:59

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Standard command line options.
0005 
0006 This module defines command lines options frequently used by various scripts,
0007 in a form suitable for adding to their option lists.
0008 
0009 All functions in this module take an C{optparse.OptionParser} instance,
0010 possibly followed by some optional parameters, and return C{None}.
0011 
0012 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0013 @license: GPLv3
0014 """
0015 
0016 from pology import _, n_
0017 from pology.colors import get_coloring_types
0018 from pology.report import format_item_list
0019 
0020 
0021 def add_cmdopt_incexc (opars, ormatch=False):
0022     """
0023     Regular expressions to include and exclude files and directories
0024     by matching names and paths.
0025 
0026     @param ormatch: whether multiple expressions are linked by OR operation
0027     @type ormatch: bool
0028 
0029     @see: L{build_path_selector()<fsops.build_path_selector>}
0030     """
0031 
0032     if not ormatch:
0033         inclink = _("@info command line option description (partial: incexc)",
0034                     "The option can be repeated, in which case a file "
0035                     "is included only if all expressions match it.")
0036         exclink = _("@info command line option description (partial: incexc)",
0037                     "The option can be repeated, in which case a file "
0038                     "is excluded only if all expressions match it.")
0039     else:
0040         inclink =  _("@info command line option description (partial: incexc)",
0041                      "The option can be repeated, in which case a file "
0042                      "is included if at least one expression matches it.")
0043         exclink =  _("@info command line option description (partial: incexc)",
0044                      "The option can be repeated, in which case a file "
0045                      "is excluded if at least one expression matches it.")
0046 
0047     opars.add_option(
0048         "-e", "--exclude-name",
0049         metavar=_("@info command line value placeholder", "REGEX"),
0050         dest="exclude_names", action="append",
0051         help=_("@info command line option description. "
0052                "%(incexc)s is one of the above partial descriptions.",
0053                "Exclude from processing files with names "
0054                "(base name without extension) "
0055                "matching given regular expression. %(incexc)s",
0056                incexc=exclink))
0057     opars.add_option(
0058         "-E", "--exclude-path",
0059         metavar=_("@info command line value placeholder", "REGEX"),
0060         dest="exclude_paths", action="append",
0061         help=_("@info command line option description. "
0062                "%(incexc)s is one of the partial descriptions above.",
0063                "Exclude from processing files with paths "
0064                "matching given regular expression. %(incexc)s",
0065                incexc=exclink))
0066     opars.add_option(
0067         "-i", "--include-name",
0068         metavar=_("@info command line value placeholder", "REGEX"),
0069         dest="include_names", action="append",
0070         help=_("@info command line option description. "
0071                "%(incexc)s is one of the above partial descriptions.",
0072                "Include into processing only files with names "
0073                "(base name without extension) "
0074                "matching given regular expression. %(incexc)s",
0075                incexc=inclink))
0076     opars.add_option(
0077         "-I", "--include-path",
0078         metavar=_("@info command line value placeholder", "REGEX"),
0079         dest="include_paths", action="append",
0080         help=_("@info command line option description. "
0081                "%(incexc)s is one of the above partial descriptions.",
0082                "Include into processing only files with paths "
0083                "matching given regular expression. %(incexc)s",
0084                incexc=inclink))
0085 
0086 
0087 def add_cmdopt_filesfrom (opars, cmnts=True, incexc=True):
0088     """
0089     File paths from which to collect list of file and directory paths.
0090 
0091     If C{cmnts} is set to C{True}, lines can be comments.
0092     If C{incexc} is set to C{True}, lines can be inclusion and exclusion
0093     directives.
0094 
0095     @param cmnts: whether to enable comments
0096     @type cmnts: bool
0097     @param incexc: whether to enable inclusion/exclusion regexes
0098     @type incexc: bool
0099 
0100     @see: L{collect_paths_from_file()<fsops.collect_paths_from_file>}
0101     """
0102 
0103     shead = _("@info command line option description (partial: head)",
0104               "Collect paths of files and directories from given file, "
0105               "which contains one path per line. "
0106               "If a path is not absolute, it is considered relative "
0107               "to current working directory.")
0108     scmnts = _("@info command line option description (partial: cmnts)",
0109                "Lines starting with '#' are treated as comments "
0110                "and skipped.")
0111     sincexc = _("@info command line option description (partial: incexc)",
0112                 "Lines starting with ':' are treated as directives "
0113                 "to include or exclude files/directories from processing, "
0114                 "as follows: "
0115                 ":-REGEX excludes by base name without extension; "
0116                 ":/-REGEX excludes by full path; "
0117                 ":+REGEX includes by base name without extension; "
0118                 ":/+REGEX excludes by full path. "
0119                 "If read directories are expanded into subpaths, "
0120                 "these directives apply to those paths too.")
0121     stail = _("@info command line option description (partial: tail)",
0122               "The option can be repeated to collect paths "
0123               "from several files.")
0124 
0125     vd = dict(head=shead, cmnts=scmnts, incexc=sincexc, tail=stail)
0126     if cmnts and incexc:
0127         help = _("@info command line option description; "
0128                  "combined from the partial descriptions above",
0129                  "%(head)s %(cmnts)s %(incexc)s %(tail)s", **vd)
0130     elif incexc:
0131         help = _("@info command line option description; "
0132                  "combined from the partial descriptions above",
0133                  "%(head)s %(incexc)s %(tail)s", ** vd)
0134     elif cmnts:
0135         help = _("@info command line option description; "
0136                  "combined from the partial descriptions above",
0137                  "%(head)s %(cmnts)s %(tail)s", **vd)
0138     else:
0139         help = _("@info command line option description; "
0140                  "combined from the partial descriptions above",
0141                  "%(head)s %(tail)s", **vd)
0142 
0143     opars.add_option(
0144         "-f", "--files-from",
0145         metavar=_("@info command line value placeholder", "FILE"),
0146         dest="files_from", action="append",
0147         help=help)
0148 
0149 
0150 def add_cmdopt_colors (opars):
0151     """
0152     Options for syntax coloring in output.
0153     """
0154 
0155     opars.add_option(
0156         "-R", "--raw-colors",
0157         action="store_true", dest="raw_colors", default=False,
0158         help=_("@info command line option description",
0159                "Syntax coloring in output independent of destination "
0160                "(whether terminal or file)."))
0161     defctype = "term"
0162     opars.add_option(
0163         "--coloring-type",
0164         metavar=_("@info command line value placeholder", "TYPE"),
0165         action="store", dest="coloring_type", default=defctype,
0166         help=_("@info command line option description",
0167                "Type of syntax coloring in output. "
0168                "Available types: %(typelist)s; default: %(type)s.",
0169                typelist=format_item_list(get_coloring_types()), type=defctype))
0170 
0171 
0172 def add_cmdopt_wrapping (opars):
0173     """
0174     Options for wrapping in catalogs when writing them out.
0175     """
0176 
0177     opars.add_option(
0178         "--wrap",
0179         action="store_true", dest="do_wrap", default=None,
0180         help=_("@info command line option description",
0181                "Basic wrapping: on colum count."))
0182     opars.add_option(
0183         "--no-wrap",
0184         action="store_false", dest="do_wrap", default=None,
0185         help=_("@info command line option description",
0186                "No basic wrapping."))
0187     opars.add_option(
0188         "--fine-wrap",
0189         action="store_true", dest="do_fine_wrap", default=None,
0190         help=_("@info command line option description",
0191                "Fine wrapping: on logical breaks (e.g. some markup tags)."))
0192     opars.add_option(
0193         "--no-fine-wrap",
0194         action="store_false", dest="do_fine_wrap", default=None,
0195         help=_("@info command line option description",
0196                "No fine wrapping."))
0197