1# This file is part of python-ly, https://pypi.python.org/pypi/python-ly
2#
3# Copyright (c) 2015 - 2015 by Wilbert Berendsen
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License
7# as published by the Free Software Foundation; either version 2
8# of the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18# See http://www.gnu.org/licenses/ for more information.
19
20"""
21Variables, checking and defaults.
22"""
23
24from __future__ import unicode_literals
25
26
27def _check_bool(name, value):
28    """Check for boolean value."""
29    if value.lower() in ('yes', 'on', 'true'):
30        return True
31    elif value.lower() in ('no', 'off', 'false'):
32        return False
33    elif value.isdigit():
34        return bool(int(value))
35    raise ValueError(
36        "{name}: ambiguous boolean value: {value}".format(
37            name=name, value=value))
38
39
40def _check_int(name, value):
41    """Check for integer value."""
42    if value.isdigit():
43        return int(value)
44    raise ValueError("{name}: not an integer value: {value}".format(
45            name=name, value=value))
46
47
48def mode(arg):
49    import ly.lex
50    if arg is not None and arg not in ly.lex.modes:
51        raise ValueError("unknown mode: {mode}".format(mode=arg))
52    return mode
53
54
55def in_place(arg):
56    return _check_bool("in-place", arg)
57
58
59def encoding(arg):
60    import codecs
61    try:
62        codecs.lookup(arg)
63    except LookupError:
64        raise ValueError("encoding: unknown encoding: {encoding}".format(
65            encoding=encoding))
66    return arg
67
68
69def output_encoding(arg):
70    if arg:
71        import codecs
72        try:
73            codecs.lookup(arg)
74        except LookupError:
75            raise ValueError("output-encoding: unknown encoding: {encoding}".format(
76                encoding=encoding))
77        return arg
78    return None
79
80
81def output(arg):
82    return arg or None
83
84
85def replace_pattern(arg):
86    return _check_bool("replace-pattern", arg)
87
88
89def backup_suffix(arg):
90    if "/" in arg:
91        raise ValueError("/ not allowed in backup-suffix")
92
93
94def with_filename(arg):
95    if arg:
96        return _check_bool("with-filename", arg)
97    return None
98
99
100def default_language(arg):
101    import ly.pitch
102    if arg:
103        if arg not in ly.pitch.pitchInfo:
104            raise ValueError("unknown pitch language: {language}".format(
105                language=arg))
106        return arg
107    return "nederlands"
108
109
110def rel_startpitch(arg):
111    return _check_bool("rel-startpitch", arg)
112
113
114def rel_absolute(arg):
115    return _check_bool("rel-absolute", arg)
116
117
118def indent_width(arg):
119    return _check_int("indent-width", arg)
120
121
122def indent_tabs(arg):
123    return _check_bool("indent-tabs", arg)
124
125
126def tab_width(arg):
127    return _check_int("tab-width", arg)
128
129
130def inline_style(arg):
131    return _check_bool("inline-style", arg)
132
133
134def full_html(arg):
135    return _check_bool("full_html", arg)
136
137
138def stylesheet(arg):
139    return arg or None
140
141
142def number_lines(arg):
143    return _check_bool("number-lines", arg)
144
145
146def wrapper_tag(arg):
147    if not arg in ['div', 'pre', 'code', 'id']:
148        raise ValueError("unknown wrapper tag: {tag}".format(
149            tag=arg))
150    return arg
151
152
153def wrapper_attribute(arg):
154    if not arg in ['id', 'class']:
155        raise ValueError("wrapper attribute must be 'id' or 'class', found {attr}".format(
156            attr=arg))
157    return arg
158
159
160def document_id(arg):
161    return arg or None
162
163
164def linenumbers_id(arg):
165    return arg or None
166