Package: beancount-black
beancount-blackOpinionated code formatter, just like Python'sblackcode formatter but for BeancountTry it out onlinehereFeaturesMIT licensed- based onbeancount-parser, aLarkbased LALR(1) Beancount syntax parserExtremely fast- 5K+ lines file generated bybean-examplecan be formatted in around 1 secondSection awareness- entries separated by Emac org symbol mark*will be formatted in groups without changing the overall structureComment preserving- comments are preserved and will be formatted as wellAuto column width- calculate maximum column width and adjust accordinglyValid beancount file assumed- please notice that the formatter assumes the given beacnount file is valid, it doesn't not perform any kind of validationSponsorThe original project beancount-black was meant to be an internal tool built byLaunch Platform LLCforA modern accounting book service based on the most popular open source version control systemGitand text-based double entry accounting book softwareBeancount.
We realized adding new entries with BeanHub automatically over time makes beancount file a mess.
So obviously, a strong code formatter is needed.
While SaaS businesses won't be required to open source an internal tool like this, we still love that the service is only possible because of the open-source tool we are using.
We think it would be greatly beneficial for the community to access a tool like this, so we've decided to open source it under MIT license, hope you find this tool useful 😄InstallTo install the formatter, simply runpipinstallbeancount-blackUsageRunbean-black/path/to/file.beanThen the file will be formatted.
Since this tool is still in its early stage, a backup file at<filepath>.backupwill be created automatically by default just in case.
The creation of backup files can be disabled by passing-nor--no-backuplike thisbean-black-n/path/to/file.beanIt's highly recommended to useBeanHub, Git or other version control system to track your Beancount book files before running the formatter against them without a backup.If you want to run the formatter programmatically, you can do thisimportiofrombeancount_parser.parserimportmake_parserfrombeancount_black.formatterimportFormatterparser=make_parser()formatter=Formatter()tree=parser.parse(beancount_content)output_file=io.StringIO()formatter.format(tree,output_file)Stdin modeYou can run the formatter in STDIN mode by passing-sor--stdin-modeargument like this:bean-black-sDebugSometimes you might encounter problems when formatting some beancount files.
To debug and better understand which line is causing the problem, you can change the log level by passing-largument withdebugorverbose.
For example:bean-black-n/path/to/file.bean-lverboseWithverbose, details of the parsed object will be printed.
Withdebug, only the line number and type of entry will be printed.
You can also useLOG_LEVELenvironment variable as well.
The available log level options are:verbosedebuginfo (default)errorwarningfatalFuture featuresAdd argument for renaming account and commodityAdd argument for following other files fromincludestatements and also format those filesFeedbacks, bugs reporting or feature requests are welcome 🙌, just please open an issue.
No guarantee we have time to deal with them, but will see what we can do.