Exemplo n.º 1
0
    def test_load_cache_moved_file(self):
        # Create an initial set of files and load file, thus creating a cache.
        with test_utils.tempdir() as tmp:
            test_utils.create_temporary_files(
                tmp, {
                    'apples.beancount':
                    """
                  include "oranges.beancount"
                  2014-01-01 open Assets:Apples
                """,
                    'oranges.beancount':
                    """
                  2014-01-02 open Assets:Oranges
                """
                })
            top_filename = path.join(tmp, 'apples.beancount')
            entries, errors, options_map = loader.load_file(top_filename)
            self.assertFalse(errors)
            self.assertEqual(2, len(entries))
            self.assertEqual(1, self.num_calls)

            # Make sure the cache was created.
            self.assertTrue(
                path.exists(path.join(tmp, '.apples.beancount.picklecache')))

            # CHeck that it doesn't need refresh
            self.assertFalse(loader.needs_refresh(options_map))

            # Move the input file.
            new_top_filename = path.join(tmp, 'bigapples.beancount')
            os.rename(top_filename, new_top_filename)

            # Check that it needs refresh.
            self.assertTrue(loader.needs_refresh(options_map))

            # Load the root file again, make sure the cache is being hit.
            entries, errors, options_map = loader.load_file(top_filename)
            self.assertEqual(2, self.num_calls)
Exemplo n.º 2
0
    def wrapper(*posargs, **kwargs):
        filename = app.args.filename

        if loader.needs_refresh(app.options):
            logging.info('Reloading...')

            # Save the source for later, to render.
            with open(filename, encoding='utf8') as f:
                app.source = f.read()

            # Parse the beancount file.
            entries, errors, options_map = loader.load_file(filename)

            # Print out the list of errors.
            if errors:
                # pylint: disable=unsupported-assignment-operation
                request.params['render_overlay'] = True
                print(
                    ',----------------------------------------------------------------'
                )
                printer.print_errors(errors, file=sys.stdout)
                print(
                    '`----------------------------------------------------------------'
                )

            # Save globals in the global app.
            app.entries = entries
            app.errors = errors
            app.options = options_map
            app.account_types = options.get_account_types(options_map)

            # Pre-compute the price database.
            app.price_map = prices.build_price_map(entries)

            # Pre-compute the list of active years.
            app.active_years = list(getters.get_active_years(entries))

            # Reset the view cache.
            app.views.clear()

        else:
            # For now, the overlay is a link to the errors page. Always render
            # it on the right when there are errors.
            if app.errors:
                # pylint: disable=unsupported-assignment-operation
                request.params['render_overlay'] = True

        return callback(*posargs, **kwargs)
                index['meta.narration.beancount'].add(entry.narration)
            if entry.links:
                index['meta.link.beancount'].update(entry.links)
            if entry.tags:
                index['meta.tag.beancount'].update(entry.tags)

        meta_keys = entry.meta.keys() - ignored_keys
        if meta_keys:
            index['meta.metadata.key.beancount'].update(meta_keys)
            string_keys = set(
                filter(lambda k: isinstance(entry.meta[k], str), meta_keys))
            meta_values = {entry.meta[k] for k in string_keys}
            index['meta.metadata.value.beancount'].update(meta_values)
    return index, options


if __name__ == '__main__':
    output = None
    input = pickle.load(stdin.buffer)
    options_map = None

    if input['options_map_dump']:
        options_map = pickle.loads(input['options_map_dump'])

    if not options_map or loader.needs_refresh(options_map):
        index, options = build_index(input['beanfile'])
        output = {'index': index, 'options_map_dump': pickle.dumps(options)}

    pickle.dump(output, stdout.buffer, protocol=3)
    stdout.buffer.flush()