How to use parse_conf method in autotest

Best Python code snippet using autotest_python

lalr_parser.py

Source:lalr_parser.py Github

copy

Full Screen

1"""This module implements a LALR(1) Parser2"""3# Author: Erez Shinan (2017)4# Email : erezshin@gmail.com5from copy import deepcopy, copy6from ..exceptions import UnexpectedInput, UnexpectedToken7from ..lexer import Token8from ..utils import Serialize9from .lalr_analysis import LALR_Analyzer, Shift, Reduce, IntParseTable10from .lalr_interactive_parser import InteractiveParser11from lark.exceptions import UnexpectedCharacters, UnexpectedInput, UnexpectedToken12###{standalone13class LALR_Parser(Serialize):14 def __init__(self, parser_conf, debug=False):15 analysis = LALR_Analyzer(parser_conf, debug=debug)16 analysis.compute_lalr()17 callbacks = parser_conf.callbacks18 self._parse_table = analysis.parse_table19 self.parser_conf = parser_conf20 self.parser = _Parser(analysis.parse_table, callbacks, debug)21 @classmethod22 def deserialize(cls, data, memo, callbacks, debug=False):23 inst = cls.__new__(cls)24 inst._parse_table = IntParseTable.deserialize(data, memo)25 inst.parser = _Parser(inst._parse_table, callbacks, debug)26 return inst27 def serialize(self, memo):28 return self._parse_table.serialize(memo)29 30 def parse_interactive(self, lexer, start):31 return self.parser.parse(lexer, start, start_interactive=True)32 def parse(self, lexer, start, on_error=None):33 try:34 return self.parser.parse(lexer, start)35 except UnexpectedInput as e:36 if on_error is None:37 raise38 while True:39 if isinstance(e, UnexpectedCharacters):40 s = e.interactive_parser.lexer_state.state41 p = s.line_ctr.char_pos42 if not on_error(e):43 raise e44 if isinstance(e, UnexpectedCharacters):45 # If user didn't change the character position, then we should46 if p == s.line_ctr.char_pos:47 s.line_ctr.feed(s.text[p:p+1])48 try:49 return e.interactive_parser.resume_parse()50 except UnexpectedToken as e2:51 if (isinstance(e, UnexpectedToken)52 and e.token.type == e2.token.type == '$END'53 and e.interactive_parser == e2.interactive_parser):54 # Prevent infinite loop55 raise e256 e = e257 except UnexpectedCharacters as e2:58 e = e259class ParseConf(object):60 __slots__ = 'parse_table', 'callbacks', 'start', 'start_state', 'end_state', 'states'61 def __init__(self, parse_table, callbacks, start):62 self.parse_table = parse_table63 self.start_state = self.parse_table.start_states[start]64 self.end_state = self.parse_table.end_states[start]65 self.states = self.parse_table.states66 self.callbacks = callbacks67 self.start = start68class ParserState(object):69 __slots__ = 'parse_conf', 'lexer', 'state_stack', 'value_stack'70 def __init__(self, parse_conf, lexer, state_stack=None, value_stack=None):71 self.parse_conf = parse_conf72 self.lexer = lexer73 self.state_stack = state_stack or [self.parse_conf.start_state]74 self.value_stack = value_stack or []75 @property76 def position(self):77 return self.state_stack[-1]78 # Necessary for match_examples() to work79 def __eq__(self, other):80 if not isinstance(other, ParserState):81 return NotImplemented82 return len(self.state_stack) == len(other.state_stack) and self.position == other.position83 def __copy__(self):84 return type(self)(85 self.parse_conf,86 self.lexer, # XXX copy87 copy(self.state_stack),88 deepcopy(self.value_stack),89 )90 def copy(self):91 return copy(self)92 def feed_token(self, token, is_end=False):93 state_stack = self.state_stack94 value_stack = self.value_stack95 states = self.parse_conf.states96 end_state = self.parse_conf.end_state97 callbacks = self.parse_conf.callbacks98 while True:99 state = state_stack[-1]100 try:101 action, arg = states[state][token.type]102 except KeyError:103 expected = {s for s in states[state].keys() if s.isupper()}104 raise UnexpectedToken(token, expected, state=self, interactive_parser=None)105 assert arg != end_state106 if action is Shift:107 # shift once and return108 assert not is_end109 state_stack.append(arg)110 value_stack.append(token if token.type not in callbacks else callbacks[token.type](token))111 return112 else:113 # reduce+shift as many times as necessary114 rule = arg115 size = len(rule.expansion)116 if size:117 s = value_stack[-size:]118 del state_stack[-size:]119 del value_stack[-size:]120 else:121 s = []122 value = callbacks[rule](s)123 _action, new_state = states[state_stack[-1]][rule.origin.name]124 assert _action is Shift125 state_stack.append(new_state)126 value_stack.append(value)127 if is_end and state_stack[-1] == end_state:128 return value_stack[-1]129class _Parser(object):130 def __init__(self, parse_table, callbacks, debug=False):131 self.parse_table = parse_table132 self.callbacks = callbacks133 self.debug = debug134 def parse(self, lexer, start, value_stack=None, state_stack=None, start_interactive=False):135 parse_conf = ParseConf(self.parse_table, self.callbacks, start)136 parser_state = ParserState(parse_conf, lexer, state_stack, value_stack)137 if start_interactive:138 return InteractiveParser(self, parser_state, parser_state.lexer)139 return self.parse_from_state(parser_state)140 141 def parse_from_state(self, state):142 # Main LALR-parser loop143 try:144 token = None145 for token in state.lexer.lex(state):146 state.feed_token(token)147 token = Token.new_borrow_pos('$END', '', token) if token else Token('$END', '', 0, 1, 1)148 return state.feed_token(token, True)149 except UnexpectedInput as e:150 try:151 e.interactive_parser = InteractiveParser(self, state, state.lexer)152 except NameError:153 pass154 raise e155 except Exception as e:156 if self.debug:157 print("")158 print("STATE STACK DUMP")159 print("----------------")160 for i, s in enumerate(state.state_stack):161 print('%d)' % i , s)162 print("")163 raise...

Full Screen

Full Screen

test_logrotate.py

Source:test_logrotate.py Github

copy

Full Screen

1"""2 :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>3"""4import salt.modules.logrotate as logrotate5from salt.exceptions import SaltInvocationError6from tests.support.mixins import LoaderModuleMockMixin7from tests.support.mock import MagicMock, patch8from tests.support.unit import TestCase9PARSE_CONF = {10 "include files": {"rsyslog": ["/var/log/syslog"]},11 "rotate": 1,12 "/var/log/wtmp": {"rotate": 1},13}14class LogrotateTestCase(TestCase, LoaderModuleMockMixin):15 """16 Test cases for salt.modules.logrotate17 """18 def setup_loader_modules(self):19 return {logrotate: {}}20 # 'show_conf' function tests: 121 def test_show_conf(self):22 """23 Test if it show parsed configuration24 """25 with patch("salt.modules.logrotate._parse_conf", MagicMock(return_value=True)):26 self.assertTrue(logrotate.show_conf())27 # 'set_' function tests: 428 def test_set(self):29 """30 Test if it set a new value for a specific configuration line31 """32 with patch(33 "salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)34 ), patch.dict(35 logrotate.__salt__, {"file.replace": MagicMock(return_value=True)}36 ):37 self.assertTrue(logrotate.set_("rotate", "2"))38 def test_set_failed(self):39 """40 Test if it fails to set a new value for a specific configuration line41 """42 with patch(43 "salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)44 ):45 kwargs = {"key": "/var/log/wtmp", "value": 2}46 self.assertRaises(SaltInvocationError, logrotate.set_, **kwargs)47 def test_set_setting(self):48 """49 Test if it set a new value for a specific configuration line50 """51 with patch.dict(52 logrotate.__salt__, {"file.replace": MagicMock(return_value=True)}53 ), patch(54 "salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)55 ):56 self.assertTrue(logrotate.set_("/var/log/wtmp", "rotate", "2"))57 def test_set_setting_failed(self):58 """59 Test if it fails to set a new value for a specific configuration line60 """61 with patch(62 "salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)63 ):64 kwargs = {"key": "rotate", "value": "/var/log/wtmp", "setting": "2"}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful