1313from pathlib import Path
1414from typing import TYPE_CHECKING
1515
16+ import uromyces
1617from beancount .utils .encryption import is_encrypted_file
18+ from uromyces ._convert import beancount_entries
19+ from uromyces ._convert import convert_options
1720
1821from fava .beans .abc import Balance
1922from fava .beans .abc import Price
5962 from decimal import Decimal
6063 from typing import Literal
6164
65+ from beancount .core .data import BeancountError
66+ from uromyces ._uromyces import UromycesOptions
67+
6268 from fava .beans .abc import Directive
6369 from fava .beans .types import BeancountOptions
6470 from fava .core .conversion import Conversion
6571 from fava .core .fava_options import FavaOptions
6672 from fava .core .group_entries import EntriesByType
6773 from fava .core .inventory import SimpleCounterInventory
68- from fava .helpers import BeancountError
6974 from fava .util .date import DateRange
7075 from fava .util .date import Interval
7176
@@ -149,7 +154,12 @@ def __init__(
149154 if filter and filter .strip ():
150155 entries = AdvancedFilter (filter .strip ()).apply (entries )
151156 if time :
152- time_filter = TimeFilter (ledger .options , ledger .fava_options , time )
157+ time_filter = TimeFilter (
158+ ledger .options ,
159+ ledger .fava_options ,
160+ time ,
161+ uro_options = ledger .uro_options ,
162+ )
153163 entries = time_filter .apply (entries )
154164 self .date_range = time_filter .date_range
155165 self .entries = entries
@@ -183,7 +193,10 @@ def entries_with_all_prices(self) -> Sequence[Directive]:
183193 """The filtered entries, with all prices added back in for queries."""
184194 entries = [* self .entries , * self .ledger .all_entries_by_type .Price ]
185195 entries .sort (key = _incomplete_sortkey )
186- return entries
196+
197+ if self .ledger .use_uromyces :
198+ entries = beancount_entries (entries ) # type: ignore[assignment, arg-type] # ty:ignore[invalid-argument-type]
199+ return entries # ty:ignore[invalid-return-type]
187200
188201 @cached_property
189202 def entries_without_prices (self ) -> Sequence [Directive ]:
@@ -318,6 +331,8 @@ class FavaLedger:
318331 "options" ,
319332 "prices" ,
320333 "query_shell" ,
334+ "uro_options" ,
335+ "use_uromyces" ,
321336 "watcher" ,
322337 )
323338
@@ -330,6 +345,9 @@ class FavaLedger:
330345 #: The Beancount options map.
331346 options : BeancountOptions
332347
348+ #: The Beancount options map.
349+ uro_options : UromycesOptions | None
350+
333351 #: A dict with all of Fava's option values.
334352 fava_options : FavaOptions
335353
@@ -375,15 +393,23 @@ class FavaLedger:
375393 #: A :class:`.QueryShell` instance.
376394 query_shell : QueryShell
377395
378- def __init__ (self , path : str , * , poll_watcher : bool = False ) -> None :
396+ def __init__ (
397+ self ,
398+ path : str ,
399+ * ,
400+ poll_watcher : bool = False ,
401+ use_uromyces : bool = True ,
402+ ) -> None :
379403 """Create an interface for a Beancount ledger.
380404
381405 Arguments:
382406 path: Path to the main Beancount file.
383407 poll_watcher: Whether to use the polling file watcher.
408+ use_uromyces: Whether to use uromyces to load the file.
384409 """
385410 #: The path to the main Beancount file.
386411 self .beancount_file_path = path
412+ self .use_uromyces = use_uromyces
387413 self ._is_encrypted = is_encrypted_file (path )
388414 self .get_filtered = lru_cache (maxsize = 16 )(self ._get_filtered )
389415 self .get_entry = lru_cache (maxsize = 16 )(self ._get_entry )
@@ -406,17 +432,25 @@ def __init__(self, path: str, *, poll_watcher: bool = False) -> None:
406432
407433 def load_file (self ) -> None :
408434 """Load the main file and all included files and set attributes."""
409- self .all_entries , self .load_errors , self .options = load_uncached (
410- self .beancount_file_path ,
411- is_encrypted = self ._is_encrypted ,
412- )
435+ if self .use_uromyces :
436+ ledger = uromyces .load_file (self .beancount_file_path )
437+ self .all_entries = ledger .entries
438+ self .load_errors = ledger .errors # type: ignore[assignment]
439+ self .options = convert_options (ledger )
440+ self .uro_options = ledger .options
441+ else :
442+ self .all_entries , self .load_errors , self .options = load_uncached (
443+ self .beancount_file_path ,
444+ is_encrypted = self ._is_encrypted ,
445+ use_uromyces = self .use_uromyces ,
446+ )
413447 self .get_filtered .cache_clear ()
414448 self .get_entry .cache_clear ()
415449
416450 self .all_entries_by_type = group_entries_by_type (self .all_entries )
417451 self .prices = FavaPriceMap (self .all_entries_by_type .Price )
418452
419- self .fava_options , self .fava_options_errors = parse_options (
453+ self .fava_options , self .fava_options_errors = parse_options ( # type: ignore[assignment]
420454 self .all_entries_by_type .Custom ,
421455 )
422456
@@ -468,10 +502,10 @@ def errors(self) -> Sequence[BeancountError]:
468502 return [
469503 * self .load_errors ,
470504 * self .fava_options_errors ,
471- * self .budgets .errors ,
472- * self .extensions .errors ,
473- * self .misc .errors ,
474- * self .ingest .errors ,
505+ * self .budgets .errors , # type: ignore[list-item]
506+ * self .extensions .errors , # type: ignore[list-item]
507+ * self .misc .errors , # type: ignore[list-item]
508+ * self .ingest .errors , # type: ignore[list-item]
475509 ]
476510
477511 @property
0 commit comments