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())
def _spacify(clazz, s): buf = StringIO() for c in s: if c == '\n': buf.write(c) else: buf.write(' ') return buf.getvalue()
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()
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()
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
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()
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()
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()
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()
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()
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()
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, '**********')
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()
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()
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()
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
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()
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()
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()
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
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()
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)
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)
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
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()
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
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()
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()
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
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()