def test___init__with_lines_and_starting_line_number(self): l1 = text_line_parser('apple\nkiwi\npear\nmelon', starting_line_number = 5) self.assertEqual( 4, len(l1) ) l2 = text_line_parser(l1) self.assertEqual( 4, len(l2) ) self.assertEqual( ( 5, 'apple' ), l2[0] ) self.assertEqual( ( 6, 'kiwi' ), l2[1] ) self.assertEqual( ( 7, 'pear' ), l2[2] ) self.assertEqual( ( 8, 'melon' ), l2[3] )
def test___init__with_lines(self): l1 = text_line_parser('apple\nkiwi\npear\nmelon') self.assertEqual( 4, len(l1) ) l2 = text_line_parser(l1) self.assertEqual( 4, len(l2) ) self.assertEqual( ( 1, 'apple' ), l2[0] ) self.assertEqual( ( 2, 'kiwi' ), l2[1] ) self.assertEqual( ( 3, 'pear' ), l2[2] ) self.assertEqual( ( 4, 'melon' ), l2[3] )
def test_add_line_numbers(self): l = text_line_parser('foo\nbar\n') l.add_line_numbers() self.assertMultiLineEqual( '''1|foo 2|bar ''', str(l) ) l = text_line_parser('foo\nbar') l.add_line_numbers() self.assertMultiLineEqual( '''1|foo 2|bar''', str(l) )
def test_continuation(self): text = r'''foo bar kiwi \ apple pear \ orange almond \ peanut \ walnut \ rum coke''' l = text_line_parser(text) l.merge_continuations() l.add_line_numbers() self.assertMultiLineEqual( ''' 1|foo bar 2|kiwi apple 3| 4|pear orange 5| 6|almond peanut walnut rum 7| 8| 9| 10|coke''', str(l) )
def test_prepend_with_negative_index(self): l = text_line_parser('1234\n5678') l.prepend('_', index = -2) self.assertMultiLineEqual( '''12_34 56_78''', str(l) )
def test_prepend_with_index(self): l = text_line_parser('1234\n5678') l.prepend('_', index = 1) self.assertMultiLineEqual( '''1_234 5_678''', str(l) )
def test_cut_sections(self): text = '''\ section:1 a b c section:2 d e f section:3 g h i ''' l = text_line_parser(text) sections = l.cut_sections(r'^section\:.*$', r'^\s*$', include_pattern = True) self.assertEqual( 3, len(sections) ) self.assertEqual( ( 1, 'section:1' ), sections[0][0] ) self.assertEqual( ( 2, 'a' ), sections[0][1] ) self.assertEqual( ( 3, 'b' ), sections[0][2] ) self.assertEqual( ( 4, 'c' ), sections[0][3] ) self.assertEqual( ( 6, 'section:2' ), sections[1][0] ) self.assertEqual( ( 7, 'd' ), sections[1][1] ) self.assertEqual( ( 8, 'e' ), sections[1][2] ) self.assertEqual( ( 9, 'f' ), sections[1][3] ) self.assertEqual( ( 11, 'section:3' ), sections[2][0] ) self.assertEqual( ( 12, 'g' ), sections[2][1] ) self.assertEqual( ( 13, 'h' ), sections[2][2] ) self.assertEqual( ( 14, 'i' ), sections[2][3] )
def parse(clazz, text, filename='<unknown>'): name = None unixpath = None pythonpath = None requires = None variables = [] for line in text_line_parser(text): text = line.text_no_comments.strip() if text: key, sep, value = text.partition(':') if sep != ':': raise ValueError('Invalid config line \"%s\" at %s:%s' % (line.text, filename, line.line_number)) key = key.strip() value = value.strip() if key == 'name': name = value elif key in ['unixpath']: unixpath = [path.expanduser(p) for p in value.split(':')] elif key in ['pythonpath']: pythonpath = [path.expanduser(p) for p in value.split(':')] elif key == 'requires': requires = set(string_util.split_by_white_space(value)) elif key == 'variables': variables = string_util.split_by_white_space(value) else: raise ValueError('Invalid config value \"%s\" at %s:%s' % (line.text, filename, line.line_number)) return clazz(name, unixpath, pythonpath, requires, variables)
def test_replace_line_with_lines(self): text = '''\ kiwi apple melon cheese''' l = text_line_parser(text) self.assertEqual( [ 1, 2, 3, 4 ], l.line_numbers() ) l.replace_line_with_lines(2, string_list(['wine', 'pepper'])) self.assertMultiLineEqual( '''\ kiwi wine pepper melon cheese''', str(l) ) self.assertEqual( [ 1, 2, 3, 4, 5 ], l.line_numbers() ) return text = '''\ kiwi apple melon cheese''' l = text_line_parser(text) l.replace_line_with_lines(1, string_list(['wine', 'pepper'])) self.assertMultiLineEqual( '''\ wine pepper apple pepper melon cheese''', str(l) ) text = '''\ kiwi apple melon cheese''' l = text_line_parser(text) l.replace_line_with_lines(4, string_list(['wine', 'pepper'])) self.assertMultiLineEqual( '''\ kiwi apple pepper melon wine pepper''', str(l) )
def test_prepend(self): l = text_line_parser('foo\nbar\n') l.prepend('ABC: ') self.assertMultiLineEqual( '''ABC: foo ABC: bar ''', str(l) )
def test___init__with_tuple_seq(self): lines = [ ( 1, 'apple' ), ( 2, 'kiwi' ), ( 3, 'pear' ), ( 4, 'melon' ) ] l = text_line_parser(lines) self.assertEqual( 4, len(l) ) self.assertEqual( ( 1, 'apple' ), l[0] ) self.assertEqual( ( 2, 'kiwi' ), l[1] ) self.assertEqual( ( 3, 'pear' ), l[2] ) self.assertEqual( ( 4, 'melon' ), l[3] )
def test_1_empty_line(self): l = text_line_parser('\n') self.assertEqual( 1, len(l) ) self.assertEqual( '', l[0].text ) self.assertMultiLineEqual( ''' ''', str(l) )
def test_append(self): l = text_line_parser('foo\nbar\n') l.append(':ABC') self.assertMultiLineEqual( '''foo:ABC bar:ABC ''', str(l) )
def test_1_line_with_newline(self): l = text_line_parser('foo\n') self.assertEqual( 1, len(l) ) self.assertEqual( 'foo', l[0].text ) self.assertMultiLineEqual( '''foo ''', str(l) )
def test__fold_literals(self): text = '''\ child1 child2 sub2a sub2a1 sub2b > this is a multi line literal that includes \'\'\'whatever\'\'\' sub2c foo sub2d >this is a another multi # line literal sub2e > this is yet another multi # line literal foo sub3d''' from bes.text.text_line_parser import text_line_parser parser = text_line_parser(text) literals = P._fold_literals(parser) expected = '''\ child1 child2 sub2a sub2a1 sub2b @@tree_text_literal:0@@ sub2c foo sub2d @@tree_text_literal:1@@ sub2e @@tree_text_literal:2@@ sub3d''' self.assertMultiLineEqual(expected, str(parser)) self.assertEqual([ '@@tree_text_literal:0@@', '@@tree_text_literal:1@@', '@@tree_text_literal:2@@' ], sorted(literals.keys())) self.assertEqual( "this is a multi\nline literal\nthat includes '''whatever'''\n", literals['@@tree_text_literal:0@@'].text) self.assertEqual("this is a another multi\n#\nline literal\n\n", literals['@@tree_text_literal:1@@'].text) self.assertEqual("this is yet another multi\n\n#\nline literal\nfoo", literals['@@tree_text_literal:2@@'].text)
def test_strip(self): text = ''' apple kiwi orange ''' l = text_line_parser(text) l.strip() self.assertEqual( [ '', 'apple', 'kiwi', 'orange', '' ], l.to_string_list() )
def caca_dependencies(clazz, filename, python_exe): 'Return list of files filename depends on or None if snakefood is not found.' rv = execute.execute('%s -v %s' % (python_exe, filename), raise_error=False) parser = text_line_parser(rv.stderr) parser.remove_empties() for line in parser: f = clazz._IMPORT_PATTERN.findall(line.text) if f: print(f[0][2]) return None
def test_windows_line_break(self): 'Test that windows line breaks are interpreted and preserved correctly.' text = '''kiwi\r\napple\r\nmelon\r\ncheese\r\n''' l = text_line_parser(text) self.assertEqual( [ ( 1, 'kiwi' ), ( 2, 'apple' ), ( 3, 'melon' ), ( 4, 'cheese' ), ], l.lines ) self.assertMultiLineEqual( text, str(l) )
def test_expand_continuations_with_indent(self): text = '''foo\\bar\\baz''' l = text_line_parser(text) self.assertEqual( [ ( 1, 'foo\\bar\\baz' ), ], l.lines ) l.expand_continuations(indent = 2) self.assertEqual( [ ( 1, 'foo' ), ( 2, ' bar' ), ( 3, ' baz' ), ], l.lines )
def test_renumber(self): text = '''\ kiwi apple melon cheese''' l = text_line_parser(text) self.assertEqual( [ 1, 2, 3, 4 ], l.line_numbers() ) l.remove_line_number(2) self.assertEqual( [ 1, 3, 4 ], l.line_numbers() ) l.renumber() self.assertEqual( [ 1, 2, 3 ], l.line_numbers() )
def test_match_all(self): text = '''apple kiwi orange apricot banana watermelon''' l = text_line_parser(text) l.strip() self.assertEqual( [ ( 1, 'apple' ), ( 4, 'apricot' ) ], l.match_all('^a.*$') ) self.assertEqual( [], l.match_all('^nothere.*$') ) self.assertEqual( [ ( 1, 'apple' ) ], l.match_all('^app.*$') )
def test_match_first(self): text = ''' Health ID: 8573008129436468 Test Name Results Reference Range Lab CHLORIDE 101 98-110 mmol/L ''' l = text_line_parser(text) patterns = [ r'^\s*Test\s+Name\s\s+Result\s\s+Flag\s\s+Reference\s+Range\s\s+Lab\s*$', r'^\s*Test\s+Name\s\s+Results\s\s+Reference\s+Range\s\s+Lab\s*$', ] self.assertEqual( 3, l.match_first(patterns).line.line_number )
def test_re_findall(self): text = '''\ cheese = brie; fruit = kiwi; wine = barolo; cheese = cheddar; ''' l = text_line_parser(text) pattern = r'cheese\s+=\s+(.*);' self.assertEqual( [ ( 0, ( 1, 'cheese = brie;' ), ['brie'] ), ( 3, ( 4, 'cheese = cheddar;' ), ['cheddar'] ), ], l.re_findall(r'cheese\s+=\s+(.*);') )
def test_find_by_line_number(self): text = '''apple kiwi orange apricot banana watermelon''' l = text_line_parser(text) l.strip() self.assertEqual( 0, l.find_by_line_number(1) ) self.assertEqual( 3, l.find_by_line_number(4) ) self.assertEqual( 5, l.find_by_line_number(6) ) self.assertEqual( -1, l.find_by_line_number(666) )
def test_replace_line_text(self): text = '''\ kiwi apple melon eggs''' l = text_line_parser(text) l.replace_line_text(3, 'whiskey') self.assertMultiLineEqual( '''\ kiwi apple whiskey eggs''', str(l) )
def test_append_line(self): text = '''\ kiwi apple melon eggs''' l = text_line_parser(text) l.append_line('wine') self.assertMultiLineEqual( '''\ kiwi apple melon eggs wine''', str(l) )
def test_cut_lines(self): text = ''' apple kiwi orange apricot banana watermelon''' l = text_line_parser(text) l.remove_empties() l.strip() self.assertEqual( [ 'kiwi', 'orange', 'apricot' ], l.cut_lines('^ap.*$', '^ba.*$').to_string_list() ) self.assertEqual( [ 'banana', 'watermelon' ], l.cut_lines('^apr.*$', None).to_string_list() ) self.assertEqual( [ 'apple', 'kiwi' ], l.cut_lines(None, '^or.*$').to_string_list() )
def test_remove_empties(self): text = ''' foo bar baz ''' l = text_line_parser(text) l.remove_empties() self.assertEqual( [ 'foo', 'bar', 'baz' ], l.to_string_list(strip_text = True) )
def instructions(self, env): buf = StringIO() buf.write('#!/bin/bash\n') buf.write('echo "----1----"\n') buf.write('declare -px\n') buf.write('echo "----2----"\n') for f in self.files_abs: buf.write('source \"%s\"\n' % (f)) buf.write('echo "----3----"\n') buf.write('declare -px\n') buf.write('echo "----4----"\n') script = temp_file.make_temp_file(content=buf.getvalue(), delete=not self._debug) if self._debug: sys.stdout.write('env_dir: script=%s\n' % (script)) sys.stdout.flush() os.chmod(script, 0o755) try: rv = execute.execute(script, raise_error=True, shell=True, env=env) finally: if not self._debug: file_util.remove(script) parser = text_line_parser(rv.stdout) if self._debug: sys.stdout.write('env_dir: stdout=%s\n' % (rv.stdout)) sys.stdout.write('env_dir: stderr=%s\n' % (rv.stderr)) sys.stdout.flush() if rv.stderr: raise RuntimeError(rv.stderr) env1 = self._parse_env_lines(parser.cut_lines('----1----', '----2----')) env2 = self._parse_env_lines(parser.cut_lines('----3----', '----4----')) delta = self._env_delta(env1, env2) instructions = [] for key in delta.added: instructions.append(instruction(key, env2[key], action.SET)) for key in delta.removed: instructions.append(instruction(key, None, action.UNSET)) for key in delta.changed: value1 = env1[key] value2 = env2[key] for inst in self._determine_change_instructions( key, value1, value2): instructions.append(inst) return sorted(instructions, key=lambda x: (x.key, x.value))
def test_match_first_with_line_number(self): text = '''\ apple1 kiwi apple2 strawberry banana kiwi2 ''' l = text_line_parser(text) self.assertEqual( 1, l.match_first([ '^apple.*$' ]).line.line_number ) self.assertEqual( 'apple1', l.match_first([ '^apple.*$' ]).line.text ) self.assertEqual( 3, l.match_first([ '^apple.*$' ], line_number = 2).line.line_number ) self.assertEqual( 'apple2', l.match_first([ '^apple.*$' ], line_number = 2).line.text )