Пример #1
0
 def get(self, path):
     values = Values()
     for meta_store in self.meta_stores:
         try:
             values.update(meta_store.get(path))
         except NotImplementedError:
             continue
     return values
Пример #2
0
    def get(self, path):
        lines = []
        num_lines = 0
        try:
            f = open(path)
        except IOError:
            pass
        else:
            try:
                content = f.read()
            finally:
                f.close()

            index = content.rfind('\n')
            header = content[:index]

            for line in header.split('\n'):
                try:
                    line = line.decode(encoding)
                except UnicodeDecodeError:
                    line = ''
                lines.append(line)
                num_lines = num_lines + 1
                if num_lines >= self.last_index:
                    break

        keys = [self.index_to_key(index) for index in range(num_lines)]

        d = dict(zip(keys, lines))
        for k in list(iter(d)):
            if not d[k]:
                del d[k]

        return Values.from_flat_dict(d)
Пример #3
0
 def test_combine(self):
     a = Values({'a': [u'foo'], 'b': [u'baz', u'bar']})
     b = Values({'b': [u'qux', u'bar', u'quxx']})
     self.assertEqual(
       Values.combine([a, b]),
       Values({'a': [u'foo'], 'b': [u'baz', u'bar', u'qux', u'quxx']}),
     )
Пример #4
0
    def get(self, path):
        lines = []
        num_lines = 0
        try:
            f = open(path)
        except IOError:
            pass
        else:
            try:
                content = f.read()
            finally:
                f.close()

            index = content.rfind('\n')
            header = content[:index]

            for line in header.split('\n'):
                try:
                    line = line.decode(encoding)
                except UnicodeDecodeError:
                    line = ''
                lines.append(line)
                num_lines = num_lines + 1
                if num_lines >= self.last_index:
                    break

        keys = [self.index_to_key(index) for index in range(num_lines)]

        d = dict(zip(keys, lines))
        for k in list(iter(d)):
            if not d[k]:
                del d[k]

        return Values.from_flat_dict(d)
Пример #5
0
 def test(self):
     filename = os.path.join(self.test_dir, 'foo')
     store = TestLinesMetaStore()
     store.set(filename, Values.from_flat_dict({'a': 'qux'}))
     try:
         self.assertEqual(store.get(filename), Values({'a': ['qux']}))
     finally:
         os.unlink(filename)
Пример #6
0
 def test_diff3_with_partial_value_change(self):
     base = Values({'a': [u'foo', u'bar'], 'b': [u'baz'], 'd': [u'qux']})
     old = Values({'a': [u'foo'], 'b': [u'baz']})
     new = Values({'a': [u'boink'], 'b': [u'baz']})
     self.assertEqual(
       Values.diff3(base, old, new),
       Values({'a': [u'bar', u'boink']}),
     )
Пример #7
0
def apply_substitution_pattern(filename, values, substitution_pattern):
    if substitution_pattern:
        splitter = substitution_pattern.get_splitter(values)
        values.update(Values.from_flat_dict(splitter.split(filename)))
Пример #8
0
            sclapp.printCritical(unicode(e))
            failed_filenames.append(filename)
            continue

        new_values = Values(values)

        try:
            apply_substitution_pattern(filename, new_values, substitution_pattern)
        except PatternError, e:
            sclapp.printCritical(unicode(e))
            failed_filenames.append(filename)
            continue

        apply_operations(new_values, operations)

        changes = Values.diff2(values, new_values)

        try:
            meta_store.set(filename, changes)
        # An error opening the file would likely cause an OSError or IOError,
        # so we catch both here and handle it as such.
        except (OSError, IOError), e:
            sclapp.printCritical(unicode(e))
            failed_filenames.append(filename)
            continue

    if failed_filenames:
        sclapp.printCritical('Failed to process the following files:')
    for filename in failed_filenames:
        sclapp.printCritical(filename)
Пример #9
0
 def test_diff2_with_value_change(self):
     old = Values({'a': [u'foo'], 'b': [u'baz']})
     new = Values({'a': [u'boink'], 'b': [u'baz']})
     self.assertEqual(Values.diff2(old, new), Values({'a': [u'boink']}))