Prevent python 1.5 from overflowing here
[polysh.git] / gsh / host_syntax.py
blob72e46f34a367d1b53a27531fd62ad6fcc606cef0
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006, 2007, 2008 Guillaume Chazarain <guichaz@gmail.com>
19 import re
21 # Currently the only expansion is <START_NUMBER-END_NUMBER>
22 # <1-10> => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
23 # <10-1> => 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
24 # <01-10> => 01, 02, 03, 04, 05, 06, 07, 08, 09, 10
25 # <1-4,6-10> => 1, 2, 3, 4, 6, 7, 8, 9, 10
27 syntax_pattern = re.compile('<([0-9,-]+)>')
28 interval_pattern = re.compile('([0-9]+)-([0-9]+)')
30 def _iter_numbers(start, end):
31 if int(start) < int(end):
32 increment = 1
33 else:
34 increment = -1
35 zero_pad = start.startswith('0') or end.startswith('0')
36 if zero_pad:
37 length = max(len(start), len(end))
38 for i in xrange(int(start), int(end) + increment, increment):
39 s = str(i)
40 if zero_pad:
41 s = '0' * (length - len(s)) + s
42 yield s
44 def expand_syntax(string):
45 """Iterator over all the strings in the expansion of the argument"""
46 match = syntax_pattern.search(string)
47 if match:
48 prefix = string[:match.start()]
49 suffix = string[match.end():]
50 intervals = match.group(1).split(',')
51 for interval in intervals:
52 interval_match = interval_pattern.match(interval)
53 if interval_match:
54 start = interval_match.group(1)
55 end = interval_match.group(2)
56 for i in _iter_numbers(start, end):
57 for expanded in expand_syntax(prefix + i + suffix):
58 yield expanded
59 else:
60 yield string