Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(python): invalid reference to variable handler #268

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dynamodb/dynamodb-types.w
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub inflight interface IClient {
inflight transactWrite(options: TransactWriteOptions): TransactWriteOutput;
}

pub interface ITable extends IClient {
pub interface ITable extends IClient, std.IResource {
setStreamConsumer(handler: inflight (StreamRecord): void, options: StreamConsumerOptions?): void;
inflight readWriteConnection(): Connection;
}
4 changes: 4 additions & 0 deletions dynamodb/dynamodb.w
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,9 @@ pub class Table impl dynamodb_types.ITable {
pub inflight readWriteConnection(): dynamodb_types.Connection {
return this.implementation.readWriteConnection();
}

pub onLift(host: std.IInflightHost, ops: Array<str>) {
this.implementation.onLift(host, ops);
}
}

4 changes: 2 additions & 2 deletions dynamodb/package-lock.json

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

2 changes: 1 addition & 1 deletion dynamodb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@winglibs/dynamodb",
"version": "0.1.11",
"version": "0.1.12",
"description": "DynamoDB library for Wing",
"author": {
"name": "Cristian Pallarés",
Expand Down
15 changes: 15 additions & 0 deletions python/inflight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const tryGetPythonInflight = (inflight) => {
if (inflight._inflightType === "_inflightPython") {
return inflight;
} else {
// inflight was lifted to another inflight
for (let l of inflight._liftMap?.handle || []) {
const lifted = tryGetPythonInflight(l[0]);
if (lifted) {
return lifted;
}
}
}
};

exports.tryGetPythonInflight = tryGetPythonInflight;
4 changes: 2 additions & 2 deletions python/package-lock.json

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

2 changes: 1 addition & 1 deletion python/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@winglibs/python",
"description": "python library for Wing",
"version": "0.0.10",
"version": "0.0.11",
"repository": {
"type": "git",
"url": "https://github.com/winglang/winglibs.git",
Expand Down
4 changes: 2 additions & 2 deletions python/test-assets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ def handler(event, context):
foo_env = os.getenv("FOO")

email_client = lifted("email")
email_client.send_email(Source="bot@wing.cloud", Destination={'ToAddresses': ['bot@monada.co',],},Message={'Subject': {'Data': 'Winglang Test Email!',},'Body': {'Text': {'Data': 'Hello from Python!',},}},)
email_client.send_email(Source="eladc@wing.cloud", Destination={'ToAddresses': ['eladc@monada.co',],},Message={'Subject': {'Data': 'Winglang Test Email!',},'Body': {'Text': {'Data': 'Hello from Python!',},}},)

mobile_client = lifted("sms")
mobile_client.publish(
Message="Hello from Python!",
Subject="Test Subject",
PhoneNumber="1234567890",
PhoneNumber="+972503292946",
)

table = lifted("table")
Expand Down
5 changes: 4 additions & 1 deletion python/tfaws/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const { Api: TfAwsApi } = require("@winglang/sdk/lib/target-tf-aws/api.js");
const { App } = require("@winglang/sdk/lib/target-tf-aws/app.js");
const { Node } = require("@winglang/sdk/lib/std/node.js");
const { Function } = require("./function.js");
const { tryGetPythonInflight } = require("../inflight.js");

module.exports.Api = class Api extends TfAwsApi {
addHandler(inflight, method, path, props) {
if (inflight._inflightType !== "_inflightPython") {
const pythonInflight = tryGetPythonInflight(inflight);
if (!pythonInflight) {
return super.addHandler(inflight, method, path);
}

Expand All @@ -16,6 +18,7 @@ module.exports.Api = class Api extends TfAwsApi {
App.of(this).makeId(this, `${this.node.id}-OnMessage`),
inflight,
props,
pythonInflight,
);
Node.of(handler).hidden = true;
this.handlers[inflight._id] = handler;
Expand Down
13 changes: 9 additions & 4 deletions python/tfaws/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Bucket: TfAwsBucket } = require("@winglang/sdk/lib/target-tf-aws/bucket.
const { BucketEventType } = require("@winglang/sdk/lib/cloud/bucket.js");
const { Node } = require("@winglang/sdk/lib/std/node.js");
const { Topic } = require("./topic.js");
const { tryGetPythonInflight } = require("../inflight.js");

const EVENTS = {
[BucketEventType.DELETE]: ["s3:ObjectRemoved:*"],
Expand All @@ -20,7 +21,8 @@ module.exports.Bucket = class Bucket extends TfAwsBucket {
}

onCreate(inflight, opts) {
if (inflight._inflightType !== "_inflightPython") {
const pythonInflight = tryGetPythonInflight(inflight);
if (!pythonInflight) {
return super.onCreate(inflight, props);
}

Expand All @@ -35,7 +37,8 @@ module.exports.Bucket = class Bucket extends TfAwsBucket {
}

onUpdate(inflight, opts) {
if (inflight._inflightType !== "_inflightPython") {
const pythonInflight = tryGetPythonInflight(inflight);
if (!pythonInflight) {
return super.onUpdate(inflight, props);
}

Expand All @@ -50,7 +53,8 @@ module.exports.Bucket = class Bucket extends TfAwsBucket {
}

onDelete(inflight, opts) {
if (inflight._inflightType !== "_inflightPython") {
const pythonInflight = tryGetPythonInflight(inflight);
if (!pythonInflight) {
return super.onDelete(inflight, props);
}

Expand All @@ -65,7 +69,8 @@ module.exports.Bucket = class Bucket extends TfAwsBucket {
}

onEvent(inflight, opts) {
if (inflight._inflightType !== "_inflightPython") {
const pythonInflight = tryGetPythonInflight(inflight);
if (!pythonInflight) {
return super.onEvent(inflight, props);
}

Expand Down
9 changes: 6 additions & 3 deletions python/tfaws/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports.Function = class Function extends Construct {
const homeEnv = process.env["HOME"] || "";

const outdir = buildAws({
nodePath: Node.of(handler).path,
nodePath: Node.of(this).path,
path: pythonInflight.inner.props.path,
handler: pythonInflight.inner.props.handler,
homeEnv: homeEnv,
Expand Down Expand Up @@ -76,18 +76,21 @@ module.exports.Function = class Function extends Construct {
// Custom resources
if (typeof client.tableName === "string" &&
typeof client.connection === "object" ) {
client.onLift(this.dummy, allow);
clients[clientId] = {
type: "@winglibs.dyanmodb.Table",
target: "aws",
props: { connection: client.connection },
}
} else if (client.constructor?.name === "MobileClient") {
} else if (client.constructor?.name === "MobileNotifications") {
client.onLift(this.dummy, allow);
clients[clientId] = {
type: "@winglibs.sns.MobileClient",
type: "@winglibs.sns.MobileNotifications",
target: "aws",
props: {},
}
} else if (client.constructor?.name === "EmailService") {
client.onLift(this.dummy, allow);
clients[clientId] = {
type: "@winglibs.ses.EmailService",
target: "aws",
Expand Down
6 changes: 4 additions & 2 deletions python/tfaws/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { Node } = require("@winglang/sdk/lib/std/node.js");
const { Duration } = require("@winglang/sdk/lib/std/duration.js");
const awsProvider = require("@cdktf/provider-aws");
const { Function } = require("./function.js");
const { tryGetPythonInflight } = require("../inflight.js");

module.exports.Queue = class Queue extends TfAwsQueue {
constructor(
Expand All @@ -17,7 +18,8 @@ module.exports.Queue = class Queue extends TfAwsQueue {
inflight,
props = {},
) {
if (inflight._inflightType !== "_inflightPython") {
const pythonInflight = tryGetPythonInflight(inflight);
if (!pythonInflight) {
return super.setConsumer(inflight, props);
}

Expand All @@ -26,7 +28,7 @@ module.exports.Queue = class Queue extends TfAwsQueue {
timeout: Duration.fromSeconds(
this.queue.visibilityTimeoutSeconds ?? 30
),
});
}, pythonInflight);

consumer.dummy.addPolicyStatements({
actions: [
Expand Down
5 changes: 4 additions & 1 deletion python/tfaws/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { App } = require("@winglang/sdk/lib/target-tf-aws/app.js");
const { Node } = require("@winglang/sdk/lib/std/node.js");
const awsProvider = require("@cdktf/provider-aws");
const { Function } = require("./function.js");
const { tryGetPythonInflight } = require("../inflight.js");

module.exports.Topic = class Topic extends TfAwsTopic {
constructor(
Expand All @@ -17,7 +18,8 @@ module.exports.Topic = class Topic extends TfAwsTopic {
inflight,
props = {}
) {
if (inflight._inflightType !== "_inflightPython") {
const pythonInflight = tryGetPythonInflight(inflight);
if (!pythonInflight) {
return super.setConsumer(inflight, props);
}

Expand All @@ -31,6 +33,7 @@ module.exports.Topic = class Topic extends TfAwsTopic {
App.of(this).makeId(this, `${this.node.id}-OnMessage`),
inflight,
props,
pythonInflight,
);
this.handlers[inflight._id] = consumer;

Expand Down
8 changes: 5 additions & 3 deletions python/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const createMD5ForProject = (requirementsFile, nodePath = "", path = "", handler

const tryInspect = (imageName) => {
try {
execSync(`docker inspect ${imageName}`);
console.log("Checking for image", imageName);
execSync(`docker inspect ${imageName}`, { stdio: "pipe" });
return true;
} catch {}
};
Expand Down Expand Up @@ -67,7 +68,8 @@ RUN pip install -r /app/requirements.txt`
writeFileSync(dockerfile, dockerfileContent);
}

execSync(`docker build -t ${imageName} -f ${dockerfile} ${path}`,
console.log("Building image", imageName, dockerfile, path);
execSync(`docker build -t ${imageName} -f "${dockerfile}" "${path}"`,
{
cwd: __dirname,
env: { HOME: homeEnv, PATH: pathEnv }
Expand Down Expand Up @@ -101,7 +103,7 @@ exports.buildAws = (options) => {
if (!existsSync(outdir)) {
mkdirSync(outdir, { recursive: true });
cpSync(requirementsPath, join(outdir, "requirements.txt"));
execSync(`docker run --rm -v ${outdir}:/var/task:rw --entrypoint python python:3.12 -m pip install -r /var/task/requirements.txt -t /var/task/python`,
execSync(`docker run --rm -v "${outdir}":/var/task:rw --entrypoint python python:3.12 -m pip install -r /var/task/requirements.txt -t /var/task/python`,
{
cwd: outdir,
env: { HOME: homeEnv, PATH: pathEnv }
Expand Down
18 changes: 2 additions & 16 deletions python/wplatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,18 @@ const { Queue: TfAwsQueue } = require("./tfaws/queue.js");
const { Topic: TfAwsTopic } = require("./tfaws/topic.js");
const { Bucket: TfAwsBucket } = require("./tfaws/bucket.js");
const { Api: TfAwsApi } = require("./tfaws/api.js");

const { tryGetPythonInflight } = require("./inflight.js");
const FUNCTION_FQN = "@winglang/sdk.cloud.Function";
const QUEUE_FQN = "@winglang/sdk.cloud.Queue";
const TOPIC_FQN = "@winglang/sdk.cloud.Topic";
const BUCKET_FQN = "@winglang/sdk.cloud.Bucket";
const API_FQN = "@winglang/sdk.cloud.Api";

const tryGetPythonInflight = (inflight) => {
if (inflight._inflightType === "_inflightPython") {
return inflight;
} else {
// inflight was lifted to another inflight
for (let l of inflight._liftMap?.handle || []) {
const lifted = tryGetPythonInflight(l[0]);
if (lifted) {
return lifted;
}
}
}
};

const createFunction = (target, scope, id, inflight, props) => {
const pythonInflight = tryGetPythonInflight(inflight);
if (pythonInflight) {
if (target === "tf-aws") {
return new TfAwsFunction(scope, id, inflight, props);
return new TfAwsFunction(scope, id, inflight, props, pythonInflight);
} else if (target === "sim") {
return new SimFunction(scope, id, inflight, props, pythonInflight);
}
Expand Down
4 changes: 4 additions & 0 deletions ses/lib.w
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ pub class EmailService impl types.IEmailService {
pub inflight sendRawEmail(options: types.SendRawEmailOptions): str? {
return this.inner.sendRawEmail(options);
}

pub onLift(host: std.IInflightHost, ops: Array<str>) {
this.inner.onLift(host, ops);
}
}
4 changes: 2 additions & 2 deletions ses/package-lock.json

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

2 changes: 1 addition & 1 deletion ses/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@winglibs/ses",
"description": "Wing library for interacting with AWS SES",
"version": "0.0.2",
"version": "0.0.3",
"repository": {
"type": "git",
"url": "https://github.com/winglang/winglibs.git",
Expand Down
2 changes: 1 addition & 1 deletion ses/types.w
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct SendRawEmailOptions {
RawMessage: RawMessage;
}

pub interface IEmailService {
pub interface IEmailService extends std.IResource {
inflight sendEmail(options: SendEmailOptions): str?;
inflight sendRawEmail(options: SendRawEmailOptions): str?;
}
4 changes: 4 additions & 0 deletions sns/lib.w
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ pub class MobileNotifications impl types.IMobileNotifications {
pub inflight publish(options: types.PublishOptions): types.PublishResult {
return this.inner.publish(options);
}

pub onLift(host: std.IInflightHost, ops: Array<str>) {
this.inner.onLift(host, ops);
}
}
4 changes: 2 additions & 2 deletions sns/package-lock.json

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

2 changes: 1 addition & 1 deletion sns/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@winglibs/sns",
"description": "Wing library for interacting with AWS SNS",
"version": "0.1.0",
"version": "0.1.1",
"repository": {
"type": "git",
"url": "https://github.com/winglang/winglibs.git",
Expand Down
Loading
Loading