qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

table_templater.py (1931B)


      1 # Parser for test templates
      2 #
      3 # Copyright (c) 2021 Virtuozzo International GmbH.
      4 #
      5 # This program is free software; you can redistribute it and/or modify
      6 # it under the terms of the GNU General Public License as published by
      7 # the Free Software Foundation; either version 2 of the License, or
      8 # (at your option) any later version.
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU General Public License
     16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 #
     18 
     19 import itertools
     20 from lark import Lark
     21 
     22 grammar = """
     23 start: ( text | column_switch | row_switch )+
     24 
     25 column_switch: "{" text ["|" text]+ "}"
     26 row_switch: "[" text ["|" text]+ "]"
     27 text: /[^|{}\[\]]+/
     28 """
     29 
     30 parser = Lark(grammar)
     31 
     32 class Templater:
     33     def __init__(self, template):
     34         self.tree = parser.parse(template)
     35 
     36         c_switches = []
     37         r_switches = []
     38         for x in self.tree.children:
     39             if x.data == 'column_switch':
     40                 c_switches.append([el.children[0].value for el in x.children])
     41             elif x.data == 'row_switch':
     42                 r_switches.append([el.children[0].value for el in x.children])
     43 
     44         self.columns = list(itertools.product(*c_switches))
     45         self.rows = list(itertools.product(*r_switches))
     46 
     47     def gen(self, column, row):
     48         i = 0
     49         j = 0
     50         result = []
     51 
     52         for x in self.tree.children:
     53             if x.data == 'text':
     54                 result.append(x.children[0].value)
     55             elif x.data == 'column_switch':
     56                 result.append(column[i])
     57                 i += 1
     58             elif x.data == 'row_switch':
     59                 result.append(row[j])
     60                 j += 1
     61 
     62         return ''.join(result)