Skip to content

Commit

Permalink
refactoring, configure linters (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasgerstmayr authored Mar 17, 2024
1 parent 3fff6ad commit 4ddeb03
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 161 deletions.
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ deps-js:
cd frontend; npm install && npx puppeteer browsers install chrome

deps-py:
cd example; pipenv install -d
pipenv install -d

deps: deps-js deps-py

Expand All @@ -24,13 +24,18 @@ run:
run-debug:
cd example; pipenv run fava --debug example.beancount

lint:
pipenv run mypy src/fava_dashboards/__init__.py scripts/format_js_in_dashboard.py
pipenv run pylint src/fava_dashboards/__init__.py scripts/format_js_in_dashboard.py

format:
cd frontend; npx prettier -w . ../src/fava_dashboards/templates/*.css
cd example; pipenv run black ../src/fava_dashboards/__init__.py ../scripts/format_js_in_dashboard.py
cd example; find . -name '*.beancount' -exec pipenv run bean-format -c 59 -o "{}" "{}" \;
pipenv run black src/fava_dashboards/__init__.py scripts/format_js_in_dashboard.py
find example -name '*.beancount' -exec pipenv run bean-format -c 59 -o "{}" "{}" \;
./scripts/format_js_in_dashboard.py example/dashboards.yaml

ci:
make lint
make build-js
make run &
make test-js
Expand Down
5 changes: 4 additions & 1 deletion example/Pipfile → Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ verify_ssl = true
name = "pypi"

[packages]
fava-dashboards = {editable = true, path = "./.."}
fava-dashboards = {editable = true, path = "."}

[dev-packages]
black = "*"
mypy = "*"
pylint = "*"
types-PyYAML = "*"

[requires]
python_version = "3.11"
101 changes: 99 additions & 2 deletions example/Pipfile.lock → Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The following variables and functions are available:
* `ledger.dateLast`: last date in the current date filter
* `ledger.operatingCurrencies`: configured operating currencies of the ledger
* `ledger.ccy`: shortcut for the first configured operating currency of the ledger
* `ledger.accounts`: declared accounts of the ledger
* `ledger.commodities`: declared commodities of the ledger
* `utils`: the return value of the `utils` code of the dashboard configuration

Expand Down
75 changes: 10 additions & 65 deletions frontend/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,16 @@
import * as echarts from "echarts";
import * as helpers from "./helpers";
import { render_d3sankey } from "./sankey";
import { Dashboard, Ledger, Panel as PanelType, Utils } from "./types";
import { Bootstrap, Dashboard, Ledger, Utils } from "./types";
import * as Panels from "./panels";

class Panel {
static runScript(ledger: Ledger, utils: Utils, panel: PanelType) {
// pass 'fava' and 'helpers' for backwards compatibility
const scriptFn = new Function("panel", "ledger", "fava", "helpers", "utils", panel.script!);
return scriptFn(panel, ledger, ledger, helpers, utils);
}

static html(ledger: Ledger, utils: Utils, panel: PanelType, elem: HTMLDivElement) {
try {
elem.innerHTML = Panel.runScript(ledger, utils, panel);
} catch (e) {
elem.innerHTML = e;
}
}

static echarts(ledger: Ledger, utils: Utils, panel: PanelType, elem: HTMLDivElement) {
let options;
try {
options = Panel.runScript(ledger, utils, panel);
} catch (e) {
elem.innerHTML = e;
return;
}

const renderer = window.navigator.userAgent === "puppeteer" ? "svg" : undefined;
const chart = echarts.init(elem, undefined, { renderer });
if (options.onClick) {
chart.on("click", options.onClick);
delete options.onClick;
}
if (options.onDblClick) {
chart.on("dblclick", options.onDblClick);
delete options.onDblClick;
}
chart.setOption(options);
}

static d3_sankey(ledger: Ledger, utils: Utils, panel: PanelType, elem: HTMLDivElement) {
let options;
try {
options = Panel.runScript(ledger, utils, panel);
} catch (e) {
elem.innerHTML = e;
return;
}

render_d3sankey(elem, options);
}

static jinja2(ledger: Ledger, utils: Utils, panel: PanelType, elem: HTMLDivElement) {
elem.innerHTML = panel.template!;
}
}

function renderDashboard(ledger: Ledger, dashboard: Dashboard, utils: Utils) {
function renderDashboard(dashboard: Dashboard, ledger: Ledger, utils: Utils) {
for (let i = 0; i < dashboard.panels.length; i++) {
const panel = dashboard.panels[i];
if (!panel.type || !(panel.type in Panel)) {
if (!panel.type || !(panel.type in Panels)) {
continue;
}

const elem = document.getElementById(`panel${i}`);
Panel[panel.type](ledger, utils, panel, elem as HTMLDivElement);
const elem = document.getElementById(`panel${i}`) as HTMLDivElement;
const ctx = { ledger, utils, panel };
Panels[panel.type](ctx, elem);
}
}

Expand All @@ -74,8 +19,8 @@ export default {
const boostrapJSON = (document.querySelector("#favaDashboardsBootstrap") as HTMLScriptElement)?.text;
if (!boostrapJSON) return;

const bootstrap = JSON.parse(boostrapJSON);
const utils = new Function(bootstrap.utils)();
renderDashboard(bootstrap.ledger, bootstrap.dashboard, utils);
const bootstrap: Bootstrap = JSON.parse(boostrapJSON);
const utils: Utils = new Function(bootstrap.utils)();
renderDashboard(bootstrap.dashboard, bootstrap.ledger, utils);
},
};
69 changes: 69 additions & 0 deletions frontend/src/panels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as echartslib from "echarts";
import * as helpers from "./helpers";
import { render_d3sankey } from "./sankey";
import { PanelCtx } from "./types";

function runFunction(src: string, args: Record<string, any>): Promise<any> {
const AsyncFunction = async function () {}.constructor;
const params = Object.entries(args);
const fn = AsyncFunction(
params.map(([k, _]) => k),
src,
);
return fn(...params.map(([_, v]) => v));
}

function runScript(ctx: PanelCtx) {
return runFunction(ctx.panel.script!, {
...ctx,
// pass 'fava' and 'helpers' for backwards compatibility
fava: ctx.ledger,
helpers,
});
}

export async function html(ctx: PanelCtx, elem: HTMLDivElement) {
try {
elem.innerHTML = await runScript(ctx);
} catch (e) {
elem.innerHTML = e;
}
}

export async function echarts(ctx: PanelCtx, elem: HTMLDivElement) {
let options: echartslib.EChartsOption;
try {
options = await runScript(ctx);
} catch (e) {
elem.innerHTML = e;
return;
}

const renderer = window.navigator.userAgent === "puppeteer" ? "svg" : undefined;
const chart = echartslib.init(elem, undefined, { renderer });
if (options.onClick) {
chart.on("click", (options as any).onClick);
delete options.onClick;
}
if (options.onDblClick) {
chart.on("dblclick", (options as any).onDblClick);
delete options.onDblClick;
}
chart.setOption(options);
}

export async function d3_sankey(ctx: PanelCtx, elem: HTMLDivElement) {
let options: any;
try {
options = await runScript(ctx);
} catch (e) {
elem.innerHTML = e;
return;
}

render_d3sankey(elem, options);
}

export async function jinja2(ctx: PanelCtx, elem: HTMLDivElement) {
elem.innerHTML = ctx.panel.template ?? "";
}
Loading

0 comments on commit 4ddeb03

Please sign in to comment.