Ejemplo n.º 1
0
 def merge(clazz, lines):
     'Merge a sequence of lines into one.  Continuation flags are cleared'
     buf = StringIO()
     for line in lines:
         text = string_util.remove_tail(line.text, clazz.CONTINUATION_CHAR)
         buf.write(text)
     return text_line(lines[0].line_number, buf.getvalue())
Ejemplo n.º 2
0
 def _spacify(clazz, s):
     buf = StringIO()
     for c in s:
         if c == '\n':
             buf.write(c)
         else:
             buf.write(' ')
     return buf.getvalue()
Ejemplo n.º 3
0
 def to_string(self, delimiter = ' '):
   buf = StringIO()
   first = True
   for req in iter(self):
     if not first:
       buf.write(delimiter)
     first = False
     buf.write(str(req))
   return buf.getvalue()
Ejemplo n.º 4
0
 def __str__(self):
     buf = StringIO()
     buf.write(self.action.value.rjust(2))
     buf.write(' ')
     buf.write(self.filename)
     if self.renamed_filename:
         buf.write(' -> ')
         buf.write(self.renamed_filename)
     return buf.getvalue()
Ejemplo n.º 5
0
 def _buf_to_str(clazz, buf, col_width):
     col_buf = StringIO()
     for i in range(0, col_width):
         c = buf.read(1)
         if c:
             col_buf.write(c)
         else:
             break
     return col_buf.getvalue().strip() or None
Ejemplo n.º 6
0
 def to_string(self, delimiter='=', value_delimiter=';', quote=False):
     buf = StringIO()
     first = True
     for kv in iter(self):
         if not first:
             buf.write(value_delimiter)
         first = False
         buf.write(kv.to_string(delimiter=delimiter, quote_value=quote))
     return buf.getvalue()
Ejemplo n.º 7
0
 def __str__(self):
     buf = StringIO()
     buf.write(self.key_type)
     buf.write(' ')
     buf.write(self.key)
     if self.annotation:
         buf.write(' ')
         buf.write(self.annotation)
     return buf.getvalue()
Ejemplo n.º 8
0
 def to_string(self):
     buf = StringIO()
     first = True
     for finfo in iter(self):
         if not first:
             buf.write('\n')
         first = False
         buf.write(str(finfo))
     return buf.getvalue()
Ejemplo n.º 9
0
 def to_string(self, delimiter='\n'):
     buf = StringIO()
     first = True
     for vfs_file_info in iter(self):
         if not first:
             buf.write(delimiter)
         first = False
         buf.write(str(vfs_file_info))
     return buf.getvalue()
Ejemplo n.º 10
0
 def replace_punctuation(clazz, s, replacement):
     'Replace punctuation in s with replacement.'
     buf = StringIO()
     for c in s:
         if c in string.punctuation:
             if replacement:
                 buf.write(replacement)
         else:
             buf.write(c)
     return buf.getvalue()
Ejemplo n.º 11
0
 def escape_white_space(clazz, text):
     last_char = None
     buf = StringIO()
     for c in text:
         is_escaping = last_char == '\\'
         if c.isspace() and not is_escaping:
             buf.write('\\')
         buf.write(c)
         last_char = c
     return buf.getvalue()
Ejemplo n.º 12
0
 def __str__(self):
     buf = StringIO()
     first = True
     for key, value in sorted(self.__dict__['_credentials'].items()):
         if not first:
             buf.write('; ')
         first = False
         buf.write('{}=**********'.format(key))
     return buf.getvalue()
     return '{}:{}'.format(self.username, '**********')
Ejemplo n.º 13
0
 def __str__(self):
   buf = StringIO()
   if self.epoch != 0:
     buf.write(str(self.epoch))
     buf.write(':')
   buf.write(str(self.upstream_version))
   if self.revision != 0:
     buf.write('-')
     buf.write(str(self.revision))
   return buf.getvalue()
Ejemplo n.º 14
0
 def dumps(d, delimiter='\n'):
     if not d:
         return ''
     buf = StringIO()
     longest_key = max([len(key) for key in d.keys()])
     fmt = '%%%ds: %%s' % (longest_key)
     for k, v in sorted(d.items()):
         buf.write(fmt % (k, v))
         buf.write(delimiter)
     return buf.getvalue()
Ejemplo n.º 15
0
 def __str__(self):
     buf = StringIO()
     buf.write(self.name)
     if self.extends:
         buf.write(' extends ')
         buf.write(self.extends)
     if self.extra_text:
         buf.write(' ')
         buf.write(self.extra_text)
     return buf.getvalue()
Ejemplo n.º 16
0
 def to_text(self, style):
     self._check_style(style)
     buf = StringIO()
     for key, value in sorted(self._properties.items()):
         buf.write(self._key_value_to_str(style, key, value))
         buf.write('\n')
     value = buf.getvalue().strip() + '\n'
     if value == '\n':
         value = ''
     return value
Ejemplo n.º 17
0
 def to_string(self, delimiter=';', quote=False):
     buf = StringIO()
     first = True
     for s in self._values:
         if not first:
             buf.write(delimiter)
         first = False
         if quote:
             s = string_util.quote_if_needed(s)
         buf.write(s)
     return buf.getvalue()
Ejemplo n.º 18
0
 def to_string(self, sort=False, fixed_key_column_width=False):
     buf = StringIO()
     sections = self._sections if not sort else sorted(self._sections)
     for i, section in enumerate(sections):
         if i != 0:
             buf.write(line_break.DEFAULT_LINE_BREAK)
         buf.write(
             section.to_string(
                 entry_formatter=self._entry_formatter,
                 sort=sort,
                 fixed_key_column_width=fixed_key_column_width))
     return buf.getvalue()
Ejemplo n.º 19
0
 def __str__(self):
   buf = StringIO()
   buf.write(self.pattern)
   for attr in self.attributes:
     buf.write(' ')
     if isinstance(attr.value, bool):
       if not attr.value:
         buf.write('-')
       buf.write(attr.key)
     else:
       buf.write(str(attr))
   return buf.getvalue()
Ejemplo n.º 20
0
 def to_text(self, formatter):
     buf = StringIO()
     for key, value in sorted(self.values().items()):
         formatted_value = formatter.value_to_text(key, value)
         formatted_key_value = formatter.key_value_to_text(
             key, formatted_value)
         buf.write(formatted_key_value)
         buf.write('\n')
     result = buf.getvalue().strip()
     result = result + '\n'
     if result == '\n':
         result = ''
     return result
Ejemplo n.º 21
0
 def __str__(self):
     buf = StringIO()
     buf.write('[submodule "{}"]\n'.format(self.name))
     buf.write('\tpath = {}\n'.format(self.path))
     buf.write('\turl = {}\n'.format(self.url))
     if self.branch:
         buf.write('\tbranch = {}\n'.format(self.branch))
     return buf.getvalue()
Ejemplo n.º 22
0
 def _strip_line_allow_quoted(clazz,
                              text,
                              strip_head=False,
                              strip_tail=False):
     'Strip comments from one line allowing for # to appear in quoted strings .'
     buf = StringIO()
     for token in string_lexer.tokenize(
             text,
             'comments_strip_line',
             options=string_lexer_options.KEEP_QUOTES):
         if token.token_type not in [
                 string_lexer.TOKEN_DONE, string_lexer.TOKEN_COMMENT
         ]:
             buf.write(token.value)
     return string_util.strip_ends(buf.getvalue(),
                                   strip_head=strip_head,
                                   strip_tail=strip_tail)
Ejemplo n.º 23
0
 def _strip_line_disallow_quoted(clazz,
                                 text,
                                 strip_head=False,
                                 strip_tail=False):
     'Strip comments from one line disallowing # to appear in quoted strings but much faster.'
     last_char = None
     buf = StringIO()
     found = False
     for c in text:
         is_escaping = last_char == '\\'
         if c == '#' and not is_escaping:
             found = True
             break
         if c != '\\':
             buf.write(c)
         last_char = c
     text = buf.getvalue()
     return string_util.strip_ends(text,
                                   strip_head=strip_head,
                                   strip_tail=strip_tail)
Ejemplo n.º 24
0
 def fit_line(clazz, text, width):
   assert '\n' not in text
   lines = []
   buf = StringIO()
   options = string_lexer_options.KEEP_QUOTES | string_lexer_options.IGNORE_COMMENTS
   for token in lexer.tokenize(text, 'text_fit', options = options):
     if token.token_type == lexer.TOKEN_SPACE:
       if (buf.tell() + len(token.value)) > width:
         lines.append(buf.getvalue().strip())
         buf = StringIO()
       else:
         buf.write(token.value)
     if token.token_type == lexer.TOKEN_STRING:
       if (buf.tell() + len(token.value)) > width:
         lines.append(buf.getvalue().strip())
         buf = StringIO()
       buf.write(token.value)
     elif token.token_type == lexer.TOKEN_DONE:
       if buf.tell() > 0:
         lines.append(buf.getvalue().strip())
   return lines
Ejemplo n.º 25
0
    def replace_all(clazz,
                    text,
                    src_string,
                    dst_string,
                    word_boundary=False,
                    word_boundary_chars=None):
        'Replace src_string with dst_string optionally respecting word boundaries.'
        check.check_string(text)
        check.check_string(src_string)
        check.check_string(dst_string)
        check.check_bool(word_boundary)
        check.check_set(word_boundary_chars, allow_none=True)

        spans = clazz.find_all(text,
                               src_string,
                               word_boundary=word_boundary,
                               word_boundary_chars=word_boundary_chars)
        if not spans:
            return text
        last_start = 0
        buf = StringIO()
        last_span = None
        for span in spans:
            left = text[last_start:span.start]
            if left:
                buf.write(left)
            buf.write(dst_string)
            last_start = span.end + 1
            last_span = span
        if last_span:
            right = text[last_span.end + 1:]
            buf.write(right)
        return buf.getvalue()
Ejemplo n.º 26
0
 def to_string(self, depth = 0, indent = 2, data_func = None, rstrip = True):
   buf = StringIO()
   buf.write(' ' * depth)
   if data_func:
     data_str = data_func(self.data)
   else:
     data_str = str(self.data)
   buf.write(data_str)
   buf.write('\n')
   for child in self.children:
     buf.write(child.to_string(depth + indent, data_func = data_func, rstrip = False))
   result = buf.getvalue()
   if rstrip:
     result = result.rstrip()
   return result
Ejemplo n.º 27
0
 def add_line_numbers(clazz, text, delimiter='|'):
     lines = text.split('\n')
     width = math.trunc(math.log10(len(lines)) + 1)
     fmt = '%%%dd' % (width)
     buf = StringIO()
     for line_number, line in zip(range(1, 1 + len(lines)), lines):
         buf.write(fmt % (line_number))
         buf.write(delimiter)
         buf.write(str(line))
         buf.write('\n')
     return buf.getvalue()
Ejemplo n.º 28
0
    def insert(clazz, text, position, insert_text):
        'Insert insert_text into text at position.'
        check.check_string(text)
        check.check_int(position)
        check.check_string(insert_text)

        buf = StringIO()
        left = text[0:position]
        right = text[position:]
        buf.write(left)
        buf.write(insert_text)
        buf.write(right)
        return buf.getvalue()
Ejemplo n.º 29
0
 def to_string(self, strip_comments=False):
     buf = StringIO()
     for line in self._lines:
         buf.write(line.get_text(strip_comments=strip_comments))
         buf.write(self._line_break)
     v = buf.getvalue()
     if self._ends_with_line_break:
         if v and v[-1] != self._line_break:
             buf.write(self._line_break)
     else:
         if v and v[-1] == self._line_break:
             v = v[0:-1]
     return v
Ejemplo n.º 30
0
 def __str__(self):
     max_len = 0
     for y in range(0, self.height):
         for x in range(0, self.width):
             max_len = max(len(str(self._rows[y][x])), max_len)
     buf = StringIO()
     for y in range(0, self.height):
         for x in range(0, self.width):
             buf.write(
                 string_util.right_justify(str(self._rows[y][x]), max_len))
             buf.write(' ')
         buf.write('\n')
     return buf.getvalue()