@primate/python 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,8 @@ import TAG from "@primate/core/backend/TAG";
3
3
  import fail from "@primate/core/fail";
4
4
  import log from "@primate/core/log";
5
5
  import assert from "@rcompat/assert";
6
- import FileRef from "@rcompat/fs/FileRef";
7
- import execute from "@rcompat/stdio/execute";
6
+ import fs from "@rcompat/fs";
7
+ import io from "@rcompat/io";
8
8
  const PACKAGE = "primate-run";
9
9
  const [MAJOR, MINOR] = TAG.split(".").map(Number);
10
10
  function package_not_found() {
@@ -15,13 +15,13 @@ function pkg_mismatch(major, minor) {
15
15
  }
16
16
  async function show_package() {
17
17
  const str0 = () => "";
18
- let out = await execute(`pip show ${PACKAGE} 2>/dev/null`).catch(str0);
18
+ let out = await io.run(`pip show ${PACKAGE} 2>/dev/null`).catch(str0);
19
19
  if (out.trim())
20
20
  return out;
21
- out = await execute(`uv pip show ${PACKAGE} 2>/dev/null`).catch(str0);
21
+ out = await io.run(`uv pip show ${PACKAGE} 2>/dev/null`).catch(str0);
22
22
  if (out.trim())
23
23
  return out;
24
- out = await execute(`python -m pip show ${PACKAGE} 2>/dev/null`).catch(str0);
24
+ out = await io.run(`python -m pip show ${PACKAGE} 2>/dev/null`).catch(str0);
25
25
  if (out.trim())
26
26
  return out;
27
27
  return null;
@@ -44,7 +44,7 @@ export default class Default extends Runtime {
44
44
  const requirements_txt = app.root.join("requirements.txt");
45
45
  let packages = [];
46
46
  if (await requirements_txt.exists()) {
47
- const requirements = await FileRef.text(requirements_txt);
47
+ const requirements = await fs.text(requirements_txt);
48
48
  packages = requirements
49
49
  .split("\n")
50
50
  .filter(line => line.trim() && !line.startsWith("#"))
@@ -52,16 +52,19 @@ export default class Default extends Runtime {
52
52
  }
53
53
  const packages_str = JSON.stringify(packages);
54
54
  app.bind(this.fileExtension, async (file, { context }) => {
55
- assert(context === "routes", "python: only route files are supported");
55
+ assert.true(context === "routes", "python: only route files are supported");
56
56
  const relative = file.debase(app.path.routes).path.replace(/^\//, "");
57
57
  const source = await file.text();
58
58
  return `
59
59
  import wrapper from "@primate/python/wrapper";
60
+ import i18n from "app:config:i18n";
61
+ import session from "app:config:session";
60
62
  await wrapper(
61
63
  ${JSON.stringify(source)},
62
64
  ${packages_str},
63
65
  "${PACKAGE}~=${TAG}.0",
64
- "${relative}"
66
+ "${relative}",
67
+ { i18n, session }
65
68
  );
66
69
  `;
67
70
  });
@@ -1,5 +1,5 @@
1
- import type RequestFacade from "@primate/core/request/RequestFacade";
2
- import type Dict from "@rcompat/type/Dict";
1
+ import type { RequestFacade } from "@primate/core/request";
2
+ import type { Dict } from "@rcompat/type";
3
3
  type FileEntry = {
4
4
  bytes: Uint8Array;
5
5
  field: string;
@@ -1,5 +1,5 @@
1
- import type ResponseLike from "@primate/core/response/ResponseLike";
2
- import type Dict from "@rcompat/type/Dict";
1
+ import { type ResponseLike } from "@primate/core/response";
2
+ import type { Dict } from "@rcompat/type";
3
3
  import type { PyProxy } from "pyodide/ffi";
4
4
  declare const _default: (response: Dict | PyProxy) => ResponseLike;
5
5
  export default _default;
@@ -1,8 +1,7 @@
1
1
  import HANDLER_PROPERTY from "#handler-property";
2
2
  import unwrap from "#unwrap";
3
- import error from "@primate/core/response/error";
4
- import redirect from "@primate/core/response/redirect";
5
- import view from "@primate/core/response/view";
3
+ import response from "@primate/core/response";
4
+ const { error, redirect, view } = response;
6
5
  const handlers = { error, redirect, view };
7
6
  const handle_handler = (handler, response) => {
8
7
  if (handler === "view") {
@@ -1,4 +1,4 @@
1
- import type Dict from "@rcompat/type/Dict";
1
+ import type { Dict } from "@rcompat/type";
2
2
  import type { PyProxy } from "pyodide/ffi";
3
3
  export default function unwrap(response: PyProxy): Dict;
4
4
  //# sourceMappingURL=unwrap.d.ts.map
@@ -1,2 +1,5 @@
1
- export default function wrapper(source: string, packages: string[], primate_run: string, id: string): Promise<void>;
1
+ export default function wrapper(source: string, packages: string[], primate_run: string, id: string, context?: {
2
+ i18n?: any;
3
+ session?: any;
4
+ }): Promise<void>;
2
5
  //# sourceMappingURL=wrapper.d.ts.map
@@ -3,7 +3,6 @@ import helpers from "@primate/python/helpers";
3
3
  import pyodide from "@primate/python/pyodide";
4
4
  import to_request from "@primate/python/to-request";
5
5
  import to_response from "@primate/python/to-response";
6
- import session from "primate/config/session";
7
6
  import route from "primate/route";
8
7
  const messageCallback = () => { };
9
8
  let _micropip = null;
@@ -23,7 +22,33 @@ async function load_package(micropip, pkg) {
23
22
  await micropip.install(pkg, { messageCallback });
24
23
  installed.add(pkg);
25
24
  }
26
- export default async function wrapper(source, packages, primate_run, id) {
25
+ function wrap_session(session) {
26
+ if (!session)
27
+ return null;
28
+ return {
29
+ get id() { return session.id; },
30
+ get exists() { return session.exists; },
31
+ create(initial) { session.create(borrow(initial)); },
32
+ get() { return session.get(); },
33
+ try_get() { return session.try(); },
34
+ set(data) { session.set(borrow(data)); },
35
+ destroy() { session.destroy(); },
36
+ };
37
+ }
38
+ function wrap_i18n(i18n) {
39
+ if (!i18n)
40
+ return null;
41
+ return {
42
+ get locale() { return i18n.locale.get(); },
43
+ t(key, params) {
44
+ if (!params)
45
+ return i18n(key);
46
+ return i18n(key, JSON.parse(params));
47
+ },
48
+ set(locale) { i18n.locale.set(locale); },
49
+ };
50
+ }
51
+ export default async function wrapper(source, packages, primate_run, id, context = {}) {
27
52
  const python = await pyodide();
28
53
  const micropip = await load_micropip(python);
29
54
  const route_id = JSON.stringify(id);
@@ -41,39 +66,19 @@ export default async function wrapper(source, packages, primate_run, id) {
41
66
  from primate import Route
42
67
  list(Route.registry(${route_id}).keys())
43
68
  `).toJs();
44
- const wrapped_session = {
45
- get id() {
46
- return session().id;
47
- },
48
- get exists() {
49
- return session().exists;
50
- },
51
- create(initial) {
52
- session().create(borrow(initial));
53
- },
54
- get() {
55
- return session().get();
56
- },
57
- try() {
58
- return session().get();
59
- },
60
- set(data) {
61
- session().set(borrow(data));
62
- },
63
- destroy() {
64
- session().destroy();
65
- },
66
- };
69
+ const wrapped_session = wrap_session(context.session);
70
+ const wrapped_i18n = wrap_i18n(context.i18n);
67
71
  for (const verb of verbs) {
68
72
  route[verb.toLowerCase()](async (request) => {
69
73
  python.globals.set("js_req", await to_request(request));
70
74
  python.globals.set("helpers_obj", helpers);
71
75
  python.globals.set("session_obj", wrapped_session);
76
+ python.globals.set("i18n_obj", wrapped_i18n);
72
77
  const verb_str = JSON.stringify(verb);
73
78
  try {
74
79
  const result = await python.runPythonAsync(`
75
80
  from primate import Route
76
- await Route.call_js(${route_id}, ${verb_str}, js_req, helpers_obj, session_obj)
81
+ await Route.call_js(${route_id}, ${verb_str}, js_req, helpers_obj, session_obj, i18n_obj)
77
82
  `);
78
83
  return to_response(result);
79
84
  }
@@ -85,6 +90,7 @@ export default async function wrapper(source, packages, primate_run, id) {
85
90
  python.globals.delete("js_req");
86
91
  python.globals.delete("helpers_obj");
87
92
  python.globals.delete("session_obj");
93
+ python.globals.delete("i18n_obj");
88
94
  }
89
95
  });
90
96
  }
package/package.json CHANGED
@@ -1,32 +1,35 @@
1
1
  {
2
2
  "name": "@primate/python",
3
- "version": "0.4.0",
4
- "description": "Primate Python backend",
3
+ "version": "0.5.0",
4
+ "description": "Python backend for Primate",
5
5
  "homepage": "https://primate.run/docs/backend/python",
6
6
  "bugs": "https://github.com/primate-run/primate/issues",
7
+ "type": "module",
7
8
  "license": "MIT",
8
- "files": [
9
- "/lib/**/*.js",
10
- "/lib/**/*.d.ts",
11
- "!/**/*.spec.*"
12
- ],
13
9
  "repository": {
14
10
  "type": "git",
15
11
  "url": "https://github.com/primate-run/primate",
16
12
  "directory": "packages/python"
17
13
  },
14
+ "files": [
15
+ "/lib/**/*.js",
16
+ "/lib/**/*.d.ts",
17
+ "!/**/*.spec.*"
18
+ ],
18
19
  "dependencies": {
19
- "@rcompat/assert": "^0.3.1",
20
- "@rcompat/fs": "^0.22.3",
21
- "@rcompat/stdio": "^0.12.2",
22
- "pyodide": "^0.29.0",
23
- "@primate/core": "^0.4.0",
24
- "pema": "^0.4.0"
20
+ "@rcompat/assert": "^0.6.0",
21
+ "@rcompat/fs": "^0.25.2",
22
+ "@rcompat/io": "^0.3.0",
23
+ "pyodide": "^0.29.3",
24
+ "@primate/core": "^0.5.0",
25
+ "pema": "^0.5.0"
26
+ },
27
+ "devDependencies": {
28
+ "@rcompat/type": "^0.9.0"
25
29
  },
26
30
  "peerDependencies": {
27
- "primate": "^0.35.0"
31
+ "primate": "^0.36.0"
28
32
  },
29
- "type": "module",
30
33
  "imports": {
31
34
  "#*": {
32
35
  "apekit": "./src/private/*.ts",