Ubuntu's usage of dash for sh broke :upload, use bash instead
[gsh.git] / gsh / host_syntax.py
blob6cebcfa9282bc911dffed882c6175b975b2318f1
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
26 # <1,3-6> => 1, 3, 4, 5, 6
27 # <1> => 1
29 syntax_pattern = re.compile('<([0-9,-]+)>')
30 interval_pattern = re.compile('([0-9]+)(-[0-9]+)?')
32 def _iter_numbers(start, end):
33 int_start = int(start)
34 int_end = int(end)
35 if int_start < int_end:
36 increment = 1
37 else:
38 increment = -1
39 zero_pad = len(start) > 1 and start.startswith('0') or \
40 len(end) > 1 and end.startswith('0')
41 if zero_pad:
42 length = max(len(start), len(end))
43 for i in xrange(int_start, int_end + increment, increment):
44 s = str(i)
45 if zero_pad:
46 s = s.zfill(length)
47 yield s
49 def expand_syntax(string):
50 """Iterator over all the strings in the expansion of the argument"""
51 match = syntax_pattern.search(string)
52 if match:
53 prefix = string[:match.start()]
54 suffix = string[match.end():]
55 intervals = match.group(1).split(',')
56 for interval in intervals:
57 interval_match = interval_pattern.match(interval)
58 if interval_match:
59 start = interval_match.group(1)
60 end = (interval_match.group(2) or start).strip('-')
61 for i in _iter_numbers(start, end):
62 for expanded in expand_syntax(prefix + i + suffix):
63 yield expanded
64 else:
65 yield string