Beispiel #1
0
 def test_no_replaces_no_kills(self):
     segment = Segment("name", "filename")
     segment.text = [
         ''' JUST NOTHING HERE \n''', 'this is the question\n',
         "END SEGMENT\n"
     ]
     handler = RegexHandler()
     handler.handle(3, segment)
     self.assertEqual(
         ''' JUST NOTHING HERE \n'''
         'this is the question\n'
         """END SEGMENT\n""", "".join(segment.text))
Beispiel #2
0
 def test_replaces_no_kills(self):
     segment = Segment("name", "filename")
     segment.text = [
         '''        REPLACE "(t)his" -> "\\1hat" 'que(s)tion\\n' -> 'an\\1wer'\n''',
         'this is the question\n', "END SEGMENT\n"
     ]
     handler = RegexHandler()
     handler.handle(3, segment)
     self.assertEqual(
         """        REPLACE "(t)his" -> "\\1hat" 'que(s)tion\\n' -> 'an\\1wer'\n"""
         """that is the answer\n"""
         """END SEGMENT\n""", "".join(segment.text))
Beispiel #3
0
 def handle(self, pass_nr, segment: Segment):
     startline = segment.text[0]
     match = re.search(SnippetWriter.start_line, startline)
     if not match:
         return
     text = self.get_modified_text(match.group(2), segment)
     if not text:
         return
     if len(segment.text) < 2:
         logger.warning("segment %s/%s is too short, cannot be processed" % (segment.filename, segment.name))
     else:
         text = self.chomp(text, False)
         segment.text = [segment.text[0], segment.text[1]] + \
                        text[1:-1] + \
                        [segment.text[-1]]
         segment.modified = True
Beispiel #4
0
 def test_skip_after(self):
     segment = Segment("name", "filename")
     segment.text = [
         ''' SKIPPER \n''', 'this stays\n', 'this stays\n',
         ' SNIPPET SKIP AFTER "this stays"\n', 'this goes away\n',
         'this goes away\n', 'this goes away\n', 'this stays\n',
         'this stays\n', 'this stays\n', 'this stays\n', "END SEGMENT\n"
     ]
     handler = LineSkipper()
     handler.handle(3, segment)
     self.assertEqual(
         ''' SKIPPER \n'''
         'this stays\n'
         'this stays\n'
         'this stays\n'
         'this stays\n'
         'this stays\n'
         "END SEGMENT\n", "".join(segment.text))
Beispiel #5
0
    def read(self):
        """
        Read the file and split up into segments
        :return: the File object that contains the segments of the file
        """
        segments = []
        segment = None
        end_regex = None
        with open(self.filename, 'r') as f:
            for line in f:
                is_start, name, end_regex_ = self._startsegment(line)
                if is_start:
                    segment = Segment(name, self.filename)
                    segments.append(segment)
                    end_regex = end_regex_
                    segment.add(line)
                    self.analyze_parameters(line, segment)
                    continue

                if end_regex and re_search(end_regex, line):
                    logger.debug("line '%s' matches '%s' segment ending" %
                                 (line, end_regex))
                    segment.add(line)
                    segment = None
                    end_regex = None
                    continue

                if segment is None:
                    name = self.next_segment()
                    logger.debug("Creating new unnamed segment '%s'" % name)
                    segment = Segment(name, self.filename)
                    segments.append(segment)

                segment.add(line)
        self.chain_segments(segments)
        return File(self.filename, segments)