1 # -*- coding: utf-8 -*-
2 # SPDX-FileCopyrightText: 2012-2024 kaliko <kaliko@azylum.org>
3 # SPDX-License-Identifier: LGPL-3.0-or-later
5 from .exceptions import MPDCommandError
9 def __init__(self, tpl):
16 return f'{self.lower}:{self.upper}'
19 return f'Range({self.tpl})'
21 def _check_element(self, item):
22 if item is None or item == '':
26 except (TypeError, ValueError) as err:
27 raise MPDCommandError(f'Not an integer: "{item}"') from err
31 if not isinstance(self.tpl, tuple):
32 raise MPDCommandError('Wrong type, provide a tuple')
33 if len(self.tpl) == 0:
35 if len(self.tpl) == 1:
36 self.lower = self._check_element(self.tpl[0])
38 if len(self.tpl) != 2:
39 raise MPDCommandError('Range wrong size (0, 1 or 2 allowed)')
40 self.lower = self._check_element(self.tpl[0])
41 self.upper = self._check_element(self.tpl[1])
42 if self.lower == '' and self.upper != '':
43 raise MPDCommandError(f'Integer expected to start the range: {self.tpl}')
44 if self.upper.isdigit() and self.lower.isdigit():
45 if int(self.lower) > int(self.upper):
46 raise MPDCommandError(f'Wrong range: {self.lower} > {self.upper}')
49 return text.replace("\\", "\\\\").replace('"', '\\"')