Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Dec 25, 2024
1 parent a8439f5 commit bf696e2
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 28 deletions.
63 changes: 63 additions & 0 deletions bindings/python/python/opendal/__base.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
this file is generated by opendal/scripts/gen-pyi/main.rs and opendal.__base doesn't exists.
DO NOT EDIT IT Manually
"""

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from typing import overload, Literal, TypeAlias

# `true`/`false`` in any case, for example, `true`/`True`/`TRUE` `false`/`False`/`FALSE`
_bool: TypeAlias = str

class _Base:
@overload
def __init__(
self,
scheme: Literal["s3"],
*,
root: str = ...,
bucket: str = ...,
enable_versioning: str = ...,
endpoint: str = ...,
region: str = ...,
access_key_id: str = ...,
secret_access_key: str = ...,
session_token: str = ...,
role_arn: str = ...,
external_id: str = ...,
role_session_name: str = ...,
disable_config_load: str = ...,
disable_ec2_metadata: str = ...,
allow_anonymous: str = ...,
server_side_encryption: str = ...,
server_side_encryption_aws_kms_key_id: str = ...,
server_side_encryption_customer_algorithm: str = ...,
server_side_encryption_customer_key: str = ...,
server_side_encryption_customer_key_md5: str = ...,
default_storage_class: str = ...,
enable_virtual_host_style: str = ...,
batch_max_operations: str = ...,
delete_max_size: str = ...,
disable_stat_with_override: str = ...,
checksum_algorithm: str = ...,
disable_write_with_if_match: str = ...,
) -> None: ...
@overload
def __init__(self, scheme, **kwargs: str) -> None: ...
29 changes: 1 addition & 28 deletions bindings/python/python/opendal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,7 @@ from types import TracebackType
from opendal import exceptions as exceptions
from opendal import layers as layers
from opendal.layers import Layer

# `true`/`false`` in any case, for example, `true`/`True`/`TRUE` `false`/`False`/`FALSE`
_bool: TypeAlias = str

class _Base:
@overload
def __init__(
self,
scheme: Literal["s3"],
*,
bucket: str,
region: str,
endpoint: str = ...,
root: str = ...,
access_key_id: str = ...,
secret_access_key: str = ...,
default_storage_class: str = ...,
server_side_encryption: str = ...,
server_side_encryption_aws_kms_key_id: str = ...,
server_side_encryption_customer_algorithm: str = ...,
server_side_encryption_customer_key: str = ...,
server_side_encryption_customer_key_md5: str = ...,
disable_config_load: _bool = ...,
enable_virtual_host_style: _bool = ...,
disable_write_with_if_match: _bool = ...,
) -> None: ...
@overload
def __init__(self, scheme: str, **kwargs: str) -> None: ...
from opendal.__base import _Base

@final
class Operator(_Base):
Expand Down
46 changes: 46 additions & 0 deletions scripts/gen-pyi/Cargo.lock

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

11 changes: 11 additions & 0 deletions scripts/gen-pyi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
edition = "2021"
name = "gen-pyi"
version = "0.1.0"

[dependencies]
syn = { version = "2.0.91", features = ['parsing', 'full', 'derive', 'printing', 'visit', 'visit-mut', 'extra-traits', 'proc-macro'] }
proc-macro2 = { version = "1.0.91", features = ["span-locations"] }
[[bin]]
name = "gen-pyi"
path = "main.rs"
85 changes: 85 additions & 0 deletions scripts/gen-pyi/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::fs;

use syn::Ident;

fn main() {
let services = vec![("s3", "S3Config", "../../core/src/services/s3/config.rs")];
let mut s = fs::read_to_string("python.tmpl").expect("failed to open python template file");

for (service, struct_name, filename) in services {
let src = fs::read_to_string(&filename).expect("unable to read file");
let syntax = syn::parse_file(&src).expect("unable to parse file");

// Debug impl is available if Syn is built with "extra-traits" feature.
for item in syntax.items.iter() {
let item = match item {
syn::Item::Struct(item_type) => item_type,
_ => {
continue;
}
};

match item.ident.span().source_text() {
None => {
continue;
}
Some(s) => {
if s != struct_name {
println!("{} {}", s, s == struct_name);
continue;
}
}
}

s.push_str(format!(" @overload\n").as_str());
s.push_str(format!(" def __init__(self,\n").as_str());
s.push_str(format!(r#"scheme: Literal["{service}"],"#).as_str());
s.push_str(format!("\n*,\n").as_str());

for f in item.fields.iter() {
s.push_str(
f.ident
.clone()
.unwrap()
.span()
.source_text()
.unwrap()
.as_ref(),
);
s.push_str(": ");
let pyi_t = py_type(f.ty.clone());
s.push_str(pyi_t.as_ref());
s.push_str(" = ...,\n");
}

s.push_str(")->None:...\n");
}
}

s.push_str(" @overload\n def __init__(self, scheme, **kwargs: str) -> None: ...\n");

fs::write(r"../../bindings/python/python/opendal/__base.pyi", s)
.expect("failed to write result to file");
}

fn py_type(t: syn::Type) -> String {
let p = match t {
_ => {
return "str".into();
}
syn::Type::Path(p) => p,
};

match p.path.get_ident() {
Some(idnt) => {
if idnt.span().unwrap().source_text().unwrap() == "bool" {
return "_bool".into();
}

return "str".into();
}
_ => {
return "str".into();
}
};
}
29 changes: 29 additions & 0 deletions scripts/gen-pyi/python.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
this file is generated by opendal/scripts/gen-pyi/main.rs and opendal.__base doesn't exists.

DO NOT EDIT IT Manually
"""

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from typing import overload,Literal,TypeAlias

# `true`/`false`` in any case, for example, `true`/`True`/`TRUE` `false`/`False`/`FALSE`
_bool: TypeAlias = str

class _Base:

0 comments on commit bf696e2

Please sign in to comment.