More scalable display_name handling (actually the length).
[gsh.git] / gsh / host_syntax.py
blob0a6c329c29c7e2c44d274af109a693d48a706705
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 int_start = int(start)
32 int_end = int(end)
33 if int_start < int_end:
34 increment = 1
35 else:
36 increment = -1
37 zero_pad = len(start) > 1 and start.startswith('0') or \
38 len(end) > 1 and end.startswith('0')
39 if zero_pad:
40 length = max(len(start), len(end))
41 for i in xrange(int_start, int_end + increment, increment):
42 s = str(i)
43 if zero_pad:
44 s = s.zfill(length)
45 yield s
47 def expand_syntax(string):
48 """Iterator over all the strings in the expansion of the argument"""
49 match = syntax_pattern.search(string)
50 if match:
51 prefix = string[:match.start()]
52 suffix = string[match.end():]
53 intervals = match.group(1).split(',')
54 for interval in intervals:
55 interval_match = interval_pattern.match(interval)
56 if interval_match:
57 start = interval_match.group(1)
58 end = interval_match.group(2)
59 for i in _iter_numbers(start, end):
60 for expanded in expand_syntax(prefix + i + suffix):
61 yield expanded
62 else:
63 yield string