def test_strip_line(self): self.assertEqual('foo ', comments.strip_line('foo #comment', )) self.assertEqual('foo', comments.strip_line('foo#comment')) self.assertEqual('foo', comments.strip_line('foo #comment', strip_tail=True)) self.assertEqual(' foo', comments.strip_line(' foo #comment', strip_tail=True)) self.assertEqual('foo ', comments.strip_line(' foo #comment', strip_head=True))
def test_strip_line_disallow_quoted(self): self.assertEqual( 'ab "cd ', comments.strip_line('ab "cd # ef"', allow_quoted=False)) self.assertEqual( 'ab "cd ', comments.strip_line('ab "cd # ef"#comment', allow_quoted=False)) self.assertEqual( '', comments.strip_line('#ab "cd # ef"', allow_quoted=False)) self.assertEqual('"', comments.strip_line('"#"', allow_quoted=False))
def _parse_text(clazz, text, filename, formatter): parser = text_line_parser(text) result = {} for line in parser: text = comments.strip_line(line.text, allow_quoted=False).strip() if text: clazz.logger.log_d('_parse_text: line=|{}|'.format(text)) left, delimiter, right = text.partition(formatter.delimiter()) if delimiter != formatter.delimiter(): raise ValueError( 'invalid property file syntax at {}:{} - "{}"'.format( filename, line.line_number, text)) key = left.strip() value = formatter.parse_value(key, right.strip()) clazz.logger.log_d('_parse_text: key={} value=|{}|'.format( key, value)) result[key] = value return result
def parse(clazz, text, default_system_mask=None): text = comments.strip_line(text, allow_quoted=False) STATE_NAME = 'name' STATE_SYSTEM_MASK = 'system_mask' STATE_OPERATOR = 'operator' state = STATE_NAME reqs = [] class state_data_t(object): def __init__(self): self.hardness = None self.name = None self.operator = None self.system_mask = None self.version = None self.expression = None @classmethod def make_requirement(clazz, state_data): if state_data.system_mask: system_mask = state_data.system_mask else: system_mask = default_system_mask return requirement(state_data.name, state_data.operator, state_data.version, system_mask, state_data.hardness, state_data.expression) state_data = state_data_t() tokens = clazz._tokenize(text) for token in tokens: if state == STATE_NAME: if token.type == clazz._TOKEN_END: if state_data.name: yield state_data_t.make_requirement(state_data) elif token.type == clazz._TOKEN_NAME: if state_data.name: yield state_data_t.make_requirement(state_data) state_data = state_data_t() system_mask, expression = clazz._parse_expression( token.system_mask) state_data.name = token.text state_data.expression = expression state_data.system_mask = system_mask elif token.type == clazz._TOKEN_OPERATOR: state_data.operator = token.text state = STATE_OPERATOR elif token.type == clazz._TOKEN_HARDNESS: state_data.hardness = token.text state = STATE_NAME else: raise RuntimeError('Unexpected token %s in state %s' % (token, state)) elif state == STATE_OPERATOR: if token.type != clazz._TOKEN_VERSION: raise RuntimeError( 'Expected version instead got end of line for %s in state %s' % (token, state)) assert state_data.name state_data.version = token.text yield state_data_t.make_requirement(state_data) state_data = state_data_t() state = STATE_NAME
def test_strip_line_with_strip(self): self.assertEqual('foo', comments.strip_line('foo #comment', strip_tail=True))