Ejemplo n.º 1
0
 def test_rio_to_unicode(self):
     uni_data = u'\N{KATAKANA LETTER O}'
     s = Stanza(foo=uni_data)
     unicode_str = s.to_unicode()
     self.assertEqual(u'foo: %s\n' % (uni_data, ), unicode_str)
     new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
     self.assertEqual(uni_data, new_s.get('foo'))
Ejemplo n.º 2
0
    def test_read_several(self):
        """Read several stanzas from file"""
        tmpf = TemporaryFile()
        tmpf.write("""\
version_header: 1

name: foo
val: 123

name: quoted
address:   "Willowglen"
\t  42 Wallaby Way
\t  Sydney

name: bar
val: 129319
""")
        tmpf.seek(0)
        s = read_stanza(tmpf)
        self.assertEqual(s, Stanza(version_header='1'))
        s = read_stanza(tmpf)
        self.assertEqual(s, Stanza(name="foo", val='123'))
        s = read_stanza(tmpf)
        self.assertEqualDiff(s.get('name'), 'quoted')
        self.assertEqualDiff(s.get('address'),
                             '  "Willowglen"\n  42 Wallaby Way\n  Sydney')
        s = read_stanza(tmpf)
        self.assertEqual(s, Stanza(name="bar", val='129319'))
        s = read_stanza(tmpf)
        self.assertEqual(s, None)
        self.check_rio_file(tmpf)
Ejemplo n.º 3
0
 def test_rio_to_unicode(self):
     uni_data = u'\N{KATAKANA LETTER O}'
     s = Stanza(foo=uni_data)
     unicode_str = s.to_unicode()
     self.assertEqual(u'foo: %s\n' % (uni_data,), unicode_str)
     new_s = rio.read_stanza_unicode(unicode_str.splitlines(True))
     self.assertEqual(uni_data, new_s.get('foo'))
Ejemplo n.º 4
0
 def test_rio_unicode(self):
     uni_data = u'\N{KATAKANA LETTER O}'
     s = Stanza(foo=uni_data)
     self.assertEqual(s.get('foo'), uni_data)
     raw_lines = s.to_lines()
     self.assertEqual(raw_lines,
                      ['foo: ' + uni_data.encode('utf-8') + '\n'])
     new_s = read_stanza(raw_lines)
     self.assertEqual(new_s.get('foo'), uni_data)
Ejemplo n.º 5
0
 def test_rio_unicode(self):
     uni_data = u'\N{KATAKANA LETTER O}'
     s = Stanza(foo=uni_data)
     self.assertEquals(s.get('foo'), uni_data)
     raw_lines = s.to_lines()
     self.assertEquals(raw_lines,
             ['foo: ' + uni_data.encode('utf-8') + '\n'])
     new_s = read_stanza(raw_lines)
     self.assertEquals(new_s.get('foo'), uni_data)
Ejemplo n.º 6
0
 def test_patch_rio_linebreaks(self):
     stanza = Stanza(breaktest='linebreak -/' * 30)
     self.assertContainsRe(
         rio.to_patch_lines(stanza, 71)[0], 'linebreak\\\\\n')
     stanza = Stanza(breaktest='linebreak-/' * 30)
     self.assertContainsRe(
         rio.to_patch_lines(stanza, 70)[0], 'linebreak-\\\\\n')
     stanza = Stanza(breaktest='linebreak/' * 30)
     self.assertContainsRe(
         rio.to_patch_lines(stanza, 70)[0], 'linebreak\\\\\n')
Ejemplo n.º 7
0
 def test_repeated_field(self):
     """Repeated field in rio"""
     s = Stanza()
     for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
                  ('a', '1000'), ('b', '2000')]:
         s.add(k, v)
     s2 = read_stanza(s.to_lines())
     self.assertEquals(s, s2)
     self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000]))
     self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))
Ejemplo n.º 8
0
 def test_stanza(self):
     """Construct rio stanza in memory"""
     s = Stanza(number='42', name="fred")
     self.assertTrue('number' in s)
     self.assertFalse('color' in s)
     self.assertFalse('42' in s)
     self.assertEquals(list(s.iter_pairs()),
             [('name', 'fred'), ('number', '42')])
     self.assertEquals(s.get('number'), '42')
     self.assertEquals(s.get('name'), 'fred')
Ejemplo n.º 9
0
 def test_stanza(self):
     """Construct rio stanza in memory"""
     s = Stanza(number='42', name="fred")
     self.assertTrue('number' in s)
     self.assertFalse('color' in s)
     self.assertFalse('42' in s)
     self.assertEqual(list(s.iter_pairs()), [('name', 'fred'),
                                             ('number', '42')])
     self.assertEqual(s.get('number'), '42')
     self.assertEqual(s.get('name'), 'fred')
Ejemplo n.º 10
0
    def test_to_file(self):
        """Write rio to file"""
        tmpf = TemporaryFile()
        s = Stanza(a_thing='something with "quotes like \\"this\\""', number='42', name='fred')
        s.write(tmpf)
        tmpf.seek(0)
        self.assertEqualDiff(tmpf.read(), r'''
a_thing: something with "quotes like \"this\""
name: fred
number: 42
'''[1:])
Ejemplo n.º 11
0
    def test_blank_line(self):
        s = Stanza(none='', one='\n', two='\n\n')
        self.assertEqualDiff(s.to_string(), """\
none:\x20
one:\x20
\t
two:\x20
\t
\t
""")
        s2 = read_stanza(s.to_lines())
        self.assertEquals(s, s2)
Ejemplo n.º 12
0
    def test_whitespace_value(self):
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
        self.assertEqualDiff(s.to_string(), """\
combo:\x20
\t\t\t
\t
space:\x20\x20
tabs: \t\t\t
""")
        s2 = read_stanza(s.to_lines())
        self.assertEquals(s, s2)
        self.rio_file_stanzas([s])
Ejemplo n.º 13
0
    def test_blank_line(self):
        s = Stanza(none='', one='\n', two='\n\n')
        self.assertEqualDiff(s.to_string(), """\
none:\x20
one:\x20
\t
two:\x20
\t
\t
""")
        s2 = read_stanza(s.to_lines())
        self.assertEqual(s, s2)
Ejemplo n.º 14
0
    def test_whitespace_value(self):
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
        self.assertEqualDiff(s.to_string(), """\
combo: 
\t\t\t
\t
space:  
tabs: \t\t\t
""")
        s2 = read_stanza(s.to_lines())
        self.assertEquals(s, s2)
        self.rio_file_stanzas([s])
Ejemplo n.º 15
0
    def test_multiline_string(self):
        tmpf = TemporaryFile()
        s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength")
        s.write(tmpf)
        tmpf.seek(0)
        self.assertEqualDiff(tmpf.read(), '''\
motto: war is peace
\tfreedom is slavery
\tignorance is strength
''')
        tmpf.seek(0)
        s2 = read_stanza(tmpf)
        self.assertEquals(s, s2)
Ejemplo n.º 16
0
 def test_nested_rio_unicode(self):
     uni_data = u'\N{KATAKANA LETTER O}'
     s = Stanza(foo=uni_data)
     parent_stanza = Stanza(child=s.to_unicode())
     raw_lines = parent_stanza.to_lines()
     self.assertEqual(['child: foo: ' + uni_data.encode('utf-8') + '\n',
                       '\t\n',
                      ], raw_lines)
     new_parent = read_stanza(raw_lines)
     child_text = new_parent.get('child')
     self.assertEqual(u'foo: %s\n' % uni_data, child_text)
     new_child = rio.read_stanza_unicode(child_text.splitlines(True))
     self.assertEqual(uni_data, new_child.get('foo'))
Ejemplo n.º 17
0
 def test_quoted(self):
     """rio quoted string cases"""
     s = Stanza(q1='"hello"',
                q2=' "for',
                q3='\n\n"for"\n',
                q4='for\n"\nfor',
                q5='\n',
                q6='"',
                q7='""',
                q8='\\',
                q9='\\"\\"',
                )
     s2 = read_stanza(s.to_lines())
     self.assertEquals(s, s2)
Ejemplo n.º 18
0
    def test_to_file(self):
        """Write rio to file"""
        tmpf = TemporaryFile()
        s = Stanza(a_thing='something with "quotes like \\"this\\""',
                   number='42',
                   name='fred')
        s.write(tmpf)
        tmpf.seek(0)
        self.assertEqualDiff(
            tmpf.read(), r'''
a_thing: something with "quotes like \"this\""
name: fred
number: 42
'''[1:])
Ejemplo n.º 19
0
    def test_multiline_string(self):
        tmpf = TemporaryFile()
        s = Stanza(
            motto="war is peace\nfreedom is slavery\nignorance is strength")
        s.write(tmpf)
        tmpf.seek(0)
        self.assertEqualDiff(
            tmpf.read(), '''\
motto: war is peace
\tfreedom is slavery
\tignorance is strength
''')
        tmpf.seek(0)
        s2 = read_stanza(tmpf)
        self.assertEqual(s, s2)
Ejemplo n.º 20
0
 def test_quoted(self):
     """rio quoted string cases"""
     s = Stanza(
         q1='"hello"',
         q2=' "for',
         q3='\n\n"for"\n',
         q4='for\n"\nfor',
         q5='\n',
         q6='"',
         q7='""',
         q8='\\',
         q9='\\"\\"',
     )
     s2 = read_stanza(s.to_lines())
     self.assertEqual(s, s2)
Ejemplo n.º 21
0
 def _prepare_info(self):
     """Write information about a pending lock to a temporary file.
     """
     import socket
     # XXX: is creating this here inefficient?
     config = bzrlib.config.GlobalConfig()
     try:
         user = config.user_email()
     except errors.NoEmailInUsername:
         user = config.username()
     s = Stanza(hostname=socket.gethostname(),
                pid=str(os.getpid()),
                start_time=str(int(time.time())),
                nonce=self.nonce,
                user=user,
                )
     return s.to_string()
Ejemplo n.º 22
0
 def _prepare_info(self):
     """Write information about a pending lock to a temporary file.
     """
     import socket
     # XXX: is creating this here inefficient?
     config = bzrlib.config.GlobalConfig()
     try:
         user = config.user_email()
     except errors.NoEmailInUsername:
         user = config.username()
     s = Stanza(
         hostname=socket.gethostname(),
         pid=str(os.getpid()),
         start_time=str(int(time.time())),
         nonce=self.nonce,
         user=user,
     )
     return s.to_string()
Ejemplo n.º 23
0
    def test_read_iter(self):
        """Read several stanzas from file"""
        tmpf = TemporaryFile()
        tmpf.write("""\
version_header: 1

name: foo
val: 123

name: bar
val: 129319
""")
        tmpf.seek(0)
        reader = read_stanzas(tmpf)
        read_iter = iter(reader)
        stuff = list(reader)
        self.assertEqual(stuff, [
            Stanza(version_header='1'),
            Stanza(name="foo", val='123'),
            Stanza(name="bar", val='129319'),
        ])
Ejemplo n.º 24
0
def _read_stanza_unicode(unicode_iter):
    stanza = Stanza()
    tag = None
    accum_value = None

    # TODO: jam 20060922 This code should raise real errors rather than
    #       using 'assert' to process user input, or raising ValueError
    #       rather than a more specific error.

    for line in unicode_iter:
        if line is None or line == u"":
            break  # end of file
        if line == u"\n":
            break  # end of stanza
        real_l = line
        if line[0] == u"\t":  # continues previous value
            if tag is None:
                raise ValueError("invalid continuation line %r" % real_l)
            accum_value.append(u"\n" + line[1:-1])
        else:  # new tag:value line
            if tag is not None:
                stanza.add(tag, u"".join(accum_value))
            try:
                colon_index = line.index(u": ")
            except ValueError:
                raise ValueError("tag/value separator not found in line %r" % real_l)
            tag = str(line[:colon_index])
            if not _valid_tag(tag):
                raise ValueError("invalid rio tag %r" % (tag,))
            accum_value = [line[colon_index + 2 : -1]]

    if tag is not None:  # add last tag-value
        stanza.add(tag, u"".join(accum_value))
        return stanza
    else:  # didn't see any content
        return None
Ejemplo n.º 25
0
 def test_patch_rio(self):
     stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
     lines = rio.to_patch_lines(stanza)
     for line in lines:
         self.assertContainsRe(line, '^# ')
         self.assertTrue(72 >= len(line))
     for line in rio.to_patch_lines(stanza, max_width=12):
         self.assertTrue(12 >= len(line))
     new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
                                                        dos_nl=False))
     lines = self.mail_munge(lines)
     new_stanza = rio.read_patch_stanza(lines)
     self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
     self.assertEqual(' ' * 255, new_stanza.get('space'))
     self.assertEqual('#' * 255, new_stanza.get('hash'))
Ejemplo n.º 26
0
 def test_repeated_field(self):
     """Repeated field in rio"""
     s = Stanza()
     for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
                  ('a', '1000'), ('b', '2000')]:
         s.add(k, v)
     s2 = read_stanza(s.to_lines())
     self.assertEqual(s, s2)
     self.assertEqual(s.get_all('a'), map(str, [10, 100, 1000]))
     self.assertEqual(s.get_all('b'), map(str, [20, 200, 2000]))
Ejemplo n.º 27
0
 def test_nested_rio_unicode(self):
     uni_data = u'\N{KATAKANA LETTER O}'
     s = Stanza(foo=uni_data)
     parent_stanza = Stanza(child=s.to_unicode())
     raw_lines = parent_stanza.to_lines()
     self.assertEqual([
         'child: foo: ' + uni_data.encode('utf-8') + '\n',
         '\t\n',
     ], raw_lines)
     new_parent = read_stanza(raw_lines)
     child_text = new_parent.get('child')
     self.assertEqual(u'foo: %s\n' % uni_data, child_text)
     new_child = rio.read_stanza_unicode(child_text.splitlines(True))
     self.assertEqual(uni_data, new_child.get('foo'))
Ejemplo n.º 28
0
 def test_rio_raises_type_error_key(self):
     """TypeError on adding invalid type to Stanza"""
     s = Stanza()
     self.assertRaises(TypeError, s.add, 10, {})
Ejemplo n.º 29
0
 def test_as_dict(self):
     """Convert rio Stanza to dictionary"""
     s = Stanza(number='42', name='fred')
     sd = s.as_dict()
     self.assertEquals(sd, dict(number='42', name='fred'))
Ejemplo n.º 30
0
 def test_to_lines(self):
     """Write simple rio stanza to string"""
     s = Stanza(number='42', name='fred')
     self.assertEquals(list(s.to_lines()),
             ['name: fred\n',
              'number: 42\n'])
Ejemplo n.º 31
0
 def test_empty_value(self):
     """Serialize stanza with empty field"""
     s = Stanza(empty='')
     self.assertEqualDiff(s.to_string(),
             "empty: \n")
Ejemplo n.º 32
0
 def test_backslash(self):
     s = Stanza(q='\\')
     t = s.to_string()
     self.assertEqualDiff(t, 'q: \\\n')
     s2 = read_stanza(s.to_lines())
     self.assertEqual(s, s2)
Ejemplo n.º 33
0
 def test_to_lines(self):
     """Write simple rio stanza to string"""
     s = Stanza(number='42', name='fred')
     self.assertEqual(list(s.to_lines()), ['name: fred\n', 'number: 42\n'])
Ejemplo n.º 34
0
 def test_as_dict(self):
     """Convert rio Stanza to dictionary"""
     s = Stanza(number='42', name='fred')
     sd = s.as_dict()
     self.assertEqual(sd, dict(number='42', name='fred'))
Ejemplo n.º 35
0
    def generate(self, to_file):
        info = Stanza()
        revision_id = self._get_revision_id()
        if revision_id is not None:
            info.add('revision-id', revision_id)
            rev = self._branch.repository.get_revision(revision_id)
            info.add('date', create_date_str(rev.timestamp, rev.timezone))
            revno = str(self._branch.revision_id_to_revno(revision_id))
        else:
            revno = '0'

        info.add('build-date', create_date_str())
        info.add('revno', revno)

        if self._branch.nick is not None:
            info.add('branch-nick', self._branch.nick)

        if self._check or self._include_file_revs:
            self._extract_file_revisions()

        if self._check:
            if self._clean:
                info.add('clean', 'True')
            else:
                info.add('clean', 'False')

        if self._include_history:
            self._extract_revision_history()
            log = Stanza()
            for (revision_id, message, timestamp,
                 timezone) in self._revision_history_info:
                log.add('id', revision_id)
                log.add('message', message)
                log.add('date', create_date_str(timestamp, timezone))
            info.add('revisions', log.to_unicode())

        if self._include_file_revs:
            files = Stanza()
            for path in sorted(self._file_revisions.keys()):
                files.add('path', path)
                files.add('revision', self._file_revisions[path])
            info.add('file-revisions', files.to_unicode())

        writer = RioWriter(to_file=to_file)
        writer.write_stanza(info)
Ejemplo n.º 36
0
    def generate(self, to_file):
        info = Stanza()
        revision_id = self._get_revision_id()
        if revision_id != NULL_REVISION:
            info.add('revision-id', revision_id)
            rev = self._branch.repository.get_revision(revision_id)
            info.add('date', create_date_str(rev.timestamp, rev.timezone))
            revno = self._get_revno_str(revision_id)
            for hook in RioVersionInfoBuilder.hooks['revision']:
                hook(rev, info)
        else:
            revno = '0'

        info.add('build-date', create_date_str())
        info.add('revno', revno)

        if self._branch.nick is not None:
            info.add('branch-nick', self._branch.nick)

        if self._check or self._include_file_revs:
            self._extract_file_revisions()

        if self._check:
            if self._clean:
                info.add('clean', 'True')
            else:
                info.add('clean', 'False')

        if self._include_history:
            log = Stanza()
            for (revision_id, message,
                 timestamp, timezone) in self._iter_revision_history():
                log.add('id', revision_id)
                log.add('message', message)
                log.add('date', create_date_str(timestamp, timezone))
            info.add('revisions', log.to_unicode())

        if self._include_file_revs:
            files = Stanza()
            for path in sorted(self._file_revisions.keys()):
                files.add('path', path)
                files.add('revision', self._file_revisions[path])
            info.add('file-revisions', files.to_unicode())

        writer = RioWriter(to_file=to_file)
        writer.write_stanza(info)
Ejemplo n.º 37
0
 def test_write_empty_stanza(self):
     """Write empty stanza"""
     l = list(Stanza().to_lines())
     self.assertEqual(l, [])
Ejemplo n.º 38
0
 def test_empty_value(self):
     """Serialize stanza with empty field"""
     s = Stanza(empty='')
     self.assertEqualDiff(s.to_string(), "empty: \n")
Ejemplo n.º 39
0
 def test_backslash(self):
     s = Stanza(q='\\')
     t = s.to_string()
     self.assertEqualDiff(t, 'q: \\\n')
     s2 = read_stanza(s.to_lines())
     self.assertEquals(s, s2)