"Fossies" - the Fresh Open Source Software Archive

Member "polysh-polysh-0.13/polysh/host_syntax.py" (11 May 2020, 2619 Bytes) of package /linux/privat/polysh-polysh-0.13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "host_syntax.py" see the Fossies "Dox" file reference documentation.

    1 """Polysh - Hostname Expansion
    2 
    3 Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
    4 Copyright (c) 2018 InnoGames GmbH
    5 """
    6 # This program is free software: you can redistribute it and/or modify
    7 # it under the terms of the GNU General Public License as published by
    8 # the Free Software Foundation, either version 2 of the License, or
    9 # (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 
   19 import re
   20 from typing import Iterator
   21 from typing import Tuple
   22 
   23 # Currently the only expansion is <START_NUMBER-END_NUMBER>
   24 # <1-10> => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
   25 # <10-1> => 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
   26 # <01-10> => 01, 02, 03, 04, 05, 06, 07, 08, 09, 10
   27 # <1-4,6-10> => 1, 2, 3, 4, 6, 7, 8, 9, 10
   28 # <1,3-6> => 1, 3, 4, 5, 6
   29 # <1> => 1
   30 
   31 syntax_pattern = re.compile('<([0-9,-]+)>')
   32 interval_pattern = re.compile('([0-9]+)(-[0-9]+)?')
   33 
   34 
   35 def _split_port(hostname: str) -> Tuple[str, str]:
   36     s = hostname.split(':', 1)
   37     if len(s) > 1:
   38         return s[0], s[1]
   39     else:
   40         return s[0], '22'
   41 
   42 
   43 def _iter_numbers(start: str, end: str) -> Iterator[str]:
   44     int_start = int(start)
   45     int_end = int(end)
   46     if int_start < int_end:
   47         increment = 1
   48     else:
   49         increment = -1
   50     zero_pad = len(start) > 1 and start.startswith('0') or \
   51         len(end) > 1 and end.startswith('0')
   52     if zero_pad:
   53         length = max(len(start), len(end))
   54     for i in range(int_start, int_end + increment, increment):
   55         s = str(i)
   56         if zero_pad:
   57             s = s.zfill(length)
   58         yield s
   59 
   60 
   61 def expand_syntax(string: str) -> Iterator[str]:
   62     """Iterator over all the strings in the expansion of the argument"""
   63     match = syntax_pattern.search(string)
   64     if match:
   65         prefix = string[:match.start()]
   66         suffix = string[match.end():]
   67         intervals = match.group(1).split(',')
   68         for interval in intervals:
   69             interval_match = interval_pattern.match(interval)
   70             if interval_match:
   71                 start = interval_match.group(1)
   72                 end = (interval_match.group(2) or start).strip('-')
   73                 for i in _iter_numbers(start, end):
   74                     for expanded in expand_syntax(prefix + i + suffix):
   75                         yield expanded
   76     else:
   77         yield string