File: lilylib.py

package info (click to toggle)
lilypond 2.24.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 70,356 kB
  • sloc: cpp: 87,962; lisp: 43,344; xml: 31,269; python: 22,974; sh: 4,090; yacc: 4,080; perl: 2,873; lex: 1,387; makefile: 76
file content (141 lines) | stat: -rw-r--r-- 3,962 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# This file is part of LilyPond, the GNU music typesetter.
#
# Copyright (C) 1998--2022 Han-Wen Nienhuys <hanwen@xs4all.nl>
#                Jan Nieuwenhuizen <janneke@gnu.org>
#
# LilyPond is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LilyPond is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.

import __main__
import codecs
import gettext
import optparse
import os
import sys

sys.stdin = codecs.getreader('utf-8')(sys.stdin.detach())
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())
sys.stderr = codecs.getwriter('utf-8')(sys.stderr.detach())

# Lilylib globals.
program_name = os.path.basename(sys.argv[0])

# Logging framework: We have the following output functions:
#    error
#    warning
#    progress
#    debug

# TODO: use the standard logging module
_loglevels = {"NONE": 0, "ERROR": 1, "WARN": 2,
              "BASIC": 3, "PROGRESS": 4, "INFO": 5, "DEBUG": 6}

_loglevel = _loglevels["PROGRESS"]


def set_loglevel(l):
    global _loglevel
    newlevel = _loglevels.get(l, -1)
    if newlevel >= 0:
        debug_output(_("Setting loglevel to %s") % l)
        _loglevel = newlevel
    else:
        error(_("Unknown or invalid loglevel '%s'") % l)


def handle_loglevel_option(option, opt_str, value, parser, *args):
    if value:
        set_loglevel(value)
    elif args:
        set_loglevel(args[0])


def _is_loglevel(l):
    global _loglevel
    return _loglevel >= _loglevels[l]


def is_verbose():
    return _is_loglevel("DEBUG")


def _print_logmessage(level, s, fullmessage=True, newline=True):
    if _is_loglevel(level):
        if fullmessage:
            s = program_name + ": " + s + "\n"
        elif newline:
            s += '\n'
        sys.stderr.write(s)
        sys.stderr.flush()


def error(s):
    _print_logmessage("ERROR", _("error: %s") % s)


def warning(s):
    _print_logmessage("WARN", _("warning: %s") % s)


def progress(s, fullmessage=False, newline=True):
    _print_logmessage("PROGRESS", s, fullmessage, newline)


def debug_output(s, fullmessage=False, newline=True):
    _print_logmessage("DEBUG", s, fullmessage, newline)


class _NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
    def format_heading(self, heading):
        if heading:
            return heading[0].upper() + heading[1:] + ':\n'
        return ''

    def format_option_strings(self, option):
        sep = ' '
        if option._short_opts and option._long_opts:
            sep = ','

        metavar = ''
        if option.takes_value():
            metavar = '=%s' % option.metavar or option.dest.upper()

        return "%3s%s %s%s" % (" ".join(option._short_opts),
                               sep,
                               " ".join(option._long_opts),
                               metavar)

    # Only use one level of indentation (even for groups and nested groups),
    # since we don't indent the headings, either
    def indent(self):
        self.current_indent = self.indent_increment
        self.level += 1

    def dedent(self):
        self.level -= 1
        if self.level <= 0:
            self.current_indent = ''
            self.level = 0

    def format_usage(self, usage):
        return _("Usage: %s") % usage + '\n'

    def format_description(self, description):
        return description


def get_option_parser(*args, **kwargs):
    p = optparse.OptionParser(*args, **kwargs)
    p.formatter = _NonDentedHeadingFormatter()
    p.formatter.set_parser(p)
    return p