Exemple #1
0
def context_(ehash=None):
    "Render the before & after context around a transaction entry."

    matching_entries = [
        entry for entry in app.entries if ehash == compare.hash_entry(entry)
    ]

    oss = io.StringIO()
    if len(matching_entries) == 0:
        print("ERROR: Could not find matching entry for '{}'".format(ehash),
              file=oss)

    elif len(matching_entries) > 1:
        print("ERROR: Ambiguous entries for '{}'".format(ehash), file=oss)
        print(file=oss)
        dcontext = app.options['dcontext']
        printer.print_entries(matching_entries, dcontext, file=oss)

    else:
        dcontext = app.options['dcontext']
        oss.write("<pre>\n")
        for entry in matching_entries:
            oss.write(
                context.render_entry_context(app.entries, app.options,
                                             dcontext, entry.meta["filename"],
                                             entry.meta["lineno"]))
        oss.write("</pre>\n")

    return render_global(pagetitle="Context: {}".format(ehash),
                         contents=oss.getvalue())
Exemple #2
0
def do_context(filename, args):
    """Describe the context that a particular transaction is applied to.

    Args:
      filename: A string, which consists in the filename.
      args: A tuple of the rest of arguments. We're expecting the first argument
        to be an integer as a string.
    """
    from beancount.reports import context
    from beancount import loader

    # Parse the arguments, get the line number.
    if len(args) != 1:
        raise SystemExit("Missing line number argument.")
    lineno = int(args[0])

    # Load the input file.
    entries, errors, options_map = loader.load_file(filename)

    dcontext = options_map['dcontext']

    # Note: Make sure to use the absolute filename used by the parser to resolve
    # the file.
    str_context = context.render_entry_context(entries, options_map, dcontext,
                                               options_map['filename'], lineno)
    sys.stdout.write(str_context)
Exemple #3
0
    def context(self, ehash):
        try:
            entry = next(entry for entry in self.all_entries if ehash == compare.hash_entry(entry))
        except StopIteration:
            return

        context_str = context.render_entry_context(self.all_entries, self.options, entry)
        return {"context": context_str.split("\n", 2)[2], "entry": entry}
Exemple #4
0
    def context(self, ehash):
        try:
            entry = next(entry for entry in self.all_entries
                         if ehash == compare.hash_entry(entry))
        except StopIteration:
            return

        context_str = context.render_entry_context(self.all_entries,
                                                   self.options, entry)
        return {
            'context': context_str.split("\n", 2)[2],
            'entry': entry,
        }
Exemple #5
0
    def context(self, entry_hash):
        """Context for an entry.

        Arguments:
            entry_hash: Hash of entry.

        Returns:
            A tuple ``(entry, context)`` of the (unique) entry with the given
            ``entry_hash`` and its context.

        """
        entry = self.get_entry(entry_hash)
        ctx = render_entry_context(self.all_entries, self.options, entry)
        return entry, ctx.split("\n", 2)[2]
Exemple #6
0
    def context(self, ehash):
        matching_entries = [entry for entry in self.all_entries if ehash == compare.hash_entry(entry)]

        if not matching_entries:
            return

        # the hash should uniquely identify the entry
        assert len(matching_entries) == 1
        entry = matching_entries[0]
        context_str = context.render_entry_context(self.all_entries, self.options, entry)
        return {
            "hash": ehash,
            "context": context_str.split("\n", 2)[2],
            "filename": entry.meta["filename"],
            "lineno": entry.meta["lineno"],
            "journal": matching_entries,
        }
Exemple #7
0
    def context(self, ehash=None):
        matching_entries = [
            entry for entry in self.entries
            if ehash == compare.hash_entry(entry)
        ]

        contexts = []
        dcontext = self.options['dcontext']

        for entry in matching_entries:
            context_str = context.render_entry_context(self.entries,
                                                       self.options, entry)

            hash_ = context_str.split("\n", 2)[0].split(':')[1].strip()
            filenamelineno = context_str.split("\n", 2)[1]
            filename = filenamelineno.split(":")[1].strip()
            lineno = int(filenamelineno.split(":")[2].strip())

            contexts.append({
                'hash': hash_,
                'context': context_str.split("\n", 2)[2],
                'filename': filename,
                'line': lineno
            })

        # TODO
        #        if len(matching_entries) == 0:
        #            print("ERROR: Could not find matching entry for '{}'".format(ehash),
        #                  file=oss)
        #
        #        elif len(matching_entries) > 1:
        #            print("ERROR: Ambiguous entries for '{}'".format(ehash),
        #                  file=oss)
        #            print(file=oss)
        #            dcontext = app.options['dcontext']
        #            printer.print_entries(matching_entries, dcontext, file=oss)
        #
        #        else:

        return {
            'hash': ehash,
            'contexts': contexts,
            'journal': self._journal_for_postings(matching_entries)
        }
Exemple #8
0
    def context(self, ehash):
        matching_entries = [entry for entry in self.all_entries
                            if ehash == compare.hash_entry(entry)]

        if not matching_entries:
            return

        # the hash should uniquely identify the entry
        assert len(matching_entries) == 1
        entry = matching_entries[0]
        context_str = context.render_entry_context(self.all_entries,
                                                   self.options, entry)
        return {
            'hash': ehash,
            'context': context_str.split("\n", 2)[2],
            'filename': entry.meta['filename'],
            'lineno': entry.meta['lineno'],
            'journal': self._journal(matching_entries),
        }
Exemple #9
0
    def context(self, ehash):
        matching_entries = [entry for entry in self.all_entries
                            if ehash == compare.hash_entry(entry)]

        if not matching_entries:
            return

        # the hash should uniquely identify the entry
        assert len(matching_entries) == 1
        entry = matching_entries[0]
        context_str = context.render_entry_context(self.all_entries,
                                                   self.options, entry)
        return {
            'hash': ehash,
            'context': context_str.split("\n", 2)[2],
            'filename': entry.meta['filename'],
            'lineno': entry.meta['lineno'],
            'journal': self._journal(matching_entries),
        }
Exemple #10
0
    def context(self, ehash=None):
        matching_entries = [entry
                                for entry in self.entries
                                if ehash == compare.hash_entry(entry)]

        contexts = []
        dcontext = self.options['dcontext']

        for entry in matching_entries:
            context_str = context.render_entry_context(
                self.entries, self.options, entry)

            hash_ = context_str.split("\n",2)[0].split(':')[1].strip()
            filenamelineno = context_str.split("\n",2)[1]
            filename = filenamelineno.split(":")[1].strip()
            lineno = int(filenamelineno.split(":")[2].strip())

            contexts.append({
                'hash': hash_,
                'context': context_str.split("\n",2)[2],
                'filename': filename,
                'line': lineno
            })

        # TODO
        #        if len(matching_entries) == 0:
        #            print("ERROR: Could not find matching entry for '{}'".format(ehash),
        #                  file=oss)
        #
        #        elif len(matching_entries) > 1:
        #            print("ERROR: Ambiguous entries for '{}'".format(ehash),
        #                  file=oss)
        #            print(file=oss)
        #            dcontext = app.options['dcontext']
        #            printer.print_entries(matching_entries, dcontext, file=oss)
        #
        #        else:

        return {
            'hash': ehash,
            'contexts': contexts,
            'journal': self._journal_for_postings(matching_entries)
        }
Exemple #11
0
def context_(ehash=None):
    "Render the before & after context around a transaction entry."

    matching_entries = [
        entry for entry in app.entries if ehash == compare.hash_entry(entry)
    ]

    oss = io.StringIO()
    if len(matching_entries) == 0:
        print("ERROR: Could not find matching entry for '{}'".format(ehash),
              file=oss)

    elif len(matching_entries) > 1:
        print("ERROR: Ambiguous entries for '{}'".format(ehash), file=oss)
        print(file=oss)
        dcontext = app.options['dcontext']
        printer.print_entries(matching_entries, dcontext, file=oss)

    else:
        entry = matching_entries[0]

        # Render the context.
        oss.write("<pre>\n")
        oss.write(context.render_entry_context(app.entries, app.options,
                                               entry))
        oss.write("</pre>\n")

        # Render the filelinks.
        if FILELINK_PROTOCOL:
            meta = entry.meta
            uri = FILELINK_PROTOCOL.format(filename=meta.get('filename'),
                                           lineno=meta.get('lineno'))
            oss.write('<div class="filelink"><a href="{}">{}</a></div>'.format(
                uri, 'Open'))

    return render_global(pagetitle="Context: {}".format(ehash),
                         contents=oss.getvalue())
Exemple #12
0
    def context(self, ehash):
        matching_entries = [entry for entry in self.all_entries
                            if ehash == compare.hash_entry(entry)]

        if not matching_entries:
            return

        # the hash should uniquely identify the entry
        assert len(matching_entries) == 1
        entry = matching_entries[0]
        context_str = context.render_entry_context(self.all_entries,
                                                   self.options, entry)
        ctx = context_str.split("\n", 2)
        filenamelineno = ctx[1]
        filename = filenamelineno.split(":")[1].strip()
        lineno = int(filenamelineno.split(":")[2].strip())

        return {
            'hash': ehash,
            'context': ctx[2],
            'filename': filename,
            'line': lineno,
            'journal': self._journal(matching_entries)
        }
Exemple #13
0
    def test_context(self, entries, errors, options_map):
        """
        plugin "beancount.plugins.implicit_prices"

        2012-01-01 open Assets:US:ETrade:Cash                       USD
        2012-01-01 open Assets:US:ETrade:ITOT                       ITOT
        2012-01-01 open Assets:US:ETrade:GLD                        GLD
        2012-01-01 open Income:US:ETrade:Gains                      USD
        2012-01-01 open Expenses:Financial:Commissions              USD

        2012-08-31 * "Buy shares of GLD"
          Assets:US:ETrade:Cash              -784.06 USD
          Assets:US:ETrade:GLD                  7.00 GLD       {110.73 USD}
          Expenses:Financial:Commissions        8.95 USD

        2012-08-31 * "Buy shares of ITOT"
          Assets:US:ETrade:Cash              -701.39 USD
          Assets:US:ETrade:ITOT                 4.00 ITOT       {173.11 USD}
          Expenses:Financial:Commissions        8.95 USD

        2012-10-13 * "Buy shares of ITOT"
          Assets:US:ETrade:Cash                                 -2,337.77 USD
          Assets:US:ETrade:ITOT                13.00 ITOT       {179.14 USD}
          Expenses:Financial:Commissions        8.95 USD

        2013-02-01 * "Sell shares of ITOT"
          Assets:US:ETrade:ITOT               -13.00 ITOT       {179.14 USD} @ 186.21 USD
          Assets:US:ETrade:Cash                                  2,411.78 USD
          Expenses:Financial:Commissions        8.95 USD
          Income:US:ETrade:Gains              -91.91 USD

        2013-02-07 * "Buy shares of ITOT"
          Assets:US:ETrade:Cash                                 -1,126.21 USD
          Assets:US:ETrade:ITOT                 6.00 ITOT       {186.21 USD}  ;; *
          Expenses:Financial:Commissions        8.95 USD

        2013-02-07 * "Buy shares of GLD"
          Assets:US:ETrade:Cash                                 -1,287.70 USD
          Assets:US:ETrade:GLD                 11.00 GLD       {116.25 USD}
          Expenses:Financial:Commissions        8.95 USD

        """
        self.assertFalse(errors)

        search_filename = entries[0].meta["filename"]
        search_lineno = entries[-3].meta["lineno"] + 2
        dcontext = options_map['dcontext']
        str_context = context.render_entry_context(entries, options_map,
                                                   dcontext, search_filename,
                                                   search_lineno)

        self.assertLines(
            textwrap.dedent("""
        Hash:298dca350249afe0378cf8bac2fb12cf
        Location: <string>:31

        ;   Assets:US:ETrade:Cash                       -1411.44 USD

        ;   Assets:US:ETrade:ITOT             4.00 ITOT {173.11 USD}

        ;   Expenses:Financial:Commissions                 35.80 USD


        2013-02-07 * "Buy shares of ITOT"
          Assets:US:ETrade:Cash           -1126.21 USD                ;  -1126.21 USD
          Assets:US:ETrade:ITOT               6.00 ITOT {186.21 USD}  ; 1117.2600 USD
          Expenses:Financial:Commissions      8.95 USD                ;      8.95 USD


        ;;; Tolerances: ITOT=0.005, USD=0.005

        ; ! Assets:US:ETrade:Cash                      -2537.65 USD

        ;   Assets:US:ETrade:ITOT             4.00 ITOT {173.11 USD}
        ; ! Assets:US:ETrade:ITOT             6.00 ITOT {186.21 USD}

        ; ! Expenses:Financial:Commissions                 44.75 USD
        """), str_context)