示例#1
0
 def to_string(self, sort=False, key_column_width=0):
     buf_left = StringIO()
     buf_left.write(self.value.key)
     if self.annotations:
         buf_left.write('[')
         for i, annotation in enumerate(self.annotations):
             if i != 0:
                 buf_left.write(',')
             buf_left.write(annotation.key)
             if annotation.value:
                 buf_left.write('=')
                 buf_left.write(annotation.value)
         buf_left.write(']')
     buf_left.write(': ')
     left_side = buf_left.getvalue()
     buf_right = StringIO()
     buf_right.write(left_side)
     value_lines = self.value.value.split(line_break.DEFAULT_LINE_BREAK)
     value_lines = value_lines if not sort else sorted(value_lines)
     indent = ' ' * (len(left_side) + 2)
     for i, line in enumerate(value_lines):
         if i > 0:
             buf_right.write(line_break.DEFAULT_LINE_BREAK)
             buf_right.write(indent)
         key_column_indent = ' ' * (2 + key_column_width - len(left_side))
         buf_right.write(key_column_indent + line)
     return buf_right.getvalue()
示例#2
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()
示例#3
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())
示例#4
0
文件: config.py 项目: reconstruir/bes
 def __str__(self):
     buf = StringIO()
     #    for section in self._parser.sections():
     #      for x in self._parser.items(section):
     #        print('HI: {} x={}'.format(section, x))
     #    assert False
     self._parser.write(buf)
     return buf.getvalue().strip() + '\n'
示例#5
0
 def _spacify(clazz, s):
     buf = StringIO()
     for c in s:
         if c == '\n':
             buf.write(c)
         else:
             buf.write(' ')
     return buf.getvalue()
示例#6
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()
示例#7
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()
示例#8
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
示例#9
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()
示例#10
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()
示例#11
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()
示例#12
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()
示例#13
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()
示例#14
0
 def to_csv(self, delimiter=',', quotechar='|'):
     'Return the table as Filter rows with filter_func.'
     buf = StringIO()
     writer = csv.writer(buf,
                         delimiter=delimiter,
                         quotechar=quotechar,
                         quoting=csv.QUOTE_MINIMAL)
     for row in self._rows:
         writer.writerow(list(row))
     return buf.getvalue()
示例#15
0
 def to_string(self):
     buf = StringIO()
     buf.write('# %s\n' % (self.name))
     buf.write('name: %s\n' % (self.name))
     buf.write('variables: %s\n' % (' '.join(self.variables)))
     buf.write('unixpath: %s\n' % (os.pathsep.join(self.unixpath)))
     buf.write('pythonpath: %s\n' % (os.pathsep.join(self.pythonpath)))
     buf.write('requires: %s\n' %
               (' '.join(sorted([r for r in self.requires]))))
     return buf.getvalue()
示例#16
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()
示例#17
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()
示例#18
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()
示例#19
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()
示例#20
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()
示例#21
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, '**********')
示例#22
0
 def __str__(self):
     buf = StringIO()
     buf.write(self.HEADER)
     buf.write('BES_VERSION = u\'%s\'\n' % (self.version))
     buf.write('BES_AUTHOR_NAME = u\'%s\'\n' % (self.author_name))
     buf.write('BES_AUTHOR_EMAIL = u\'%s\'\n' % (self.author_email))
     buf.write('BES_ADDRESS = u\'%s\'\n' % (self.address))
     buf.write('BES_TAG = u\'%s\'\n' % (self.tag))
     buf.write('BES_TIMESTAMP = u\'%s\'\n' % (self.timestamp))
     return buf.getvalue()
示例#23
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
示例#24
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()
示例#25
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()
示例#26
0
 def __str__(self):
   buf = StringIO()
   for i, item in enumerate(self._stack):
     if i != 0:
       buf.write('/')
     buf.write(str(item.depth or 0))
     buf.write(':')
     buf.write(item.data.text)
     buf.write(':')
     buf.write(item.data.line_number)
   return buf.getvalue()
示例#27
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()
示例#28
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
示例#29
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()
示例#30
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