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

remove callable param from folder method #1101

Merged
merged 1 commit into from
Aug 4, 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
13 changes: 3 additions & 10 deletions runhouse/resources/folders/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import shutil
import subprocess
from pathlib import Path
from typing import Callable, List, Optional, Union
from typing import List, Optional, Union

from runhouse.globals import rns_client

Expand Down Expand Up @@ -713,9 +713,7 @@ def rm(self, contents: list = None, recursive: bool = True):
else:
folder_path.unlink()

def put(
self, contents, overwrite=False, mode: str = "wb", write_fn: Callable = None
):
def put(self, contents, overwrite=False, mode: str = "wb"):
"""Put given contents in folder.

Args:
Expand All @@ -725,8 +723,6 @@ def put(
overwrite (bool): Whether to dump the file contents as json. By default expects data to be encoded.
Defaults to ``False``.
mode (Optional(str)): Write mode to use. Defaults to ``wb``.
write_fn (Optional(Callable)): Function to use for writing file contents.
Example: ``write_fn = lambda f, data: json.dump(data, f)``

Example:
>>> my_folder.put(contents={"filename.txt": data})
Expand Down Expand Up @@ -768,10 +764,7 @@ def put(

try:
with open(file_path, mode) as f:
if write_fn:
write_fn(f, file_obj)
else:
f.write(file_obj)
f.write(file_obj)

except Exception as e:
raise RuntimeError(f"Failed to write {filename} to {file_path}: {e}")
Expand Down
14 changes: 4 additions & 10 deletions runhouse/resources/folders/gcs_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shutil
import subprocess
from pathlib import Path
from typing import Callable, List, Optional
from typing import List, Optional

from runhouse.logger import logger

Expand Down Expand Up @@ -96,9 +96,7 @@ def _gcs_copy(self, new_path):
new_blob = self.bucket.blob(new_name)
new_blob.rewrite(blob)

def put(
self, contents, overwrite=False, mode: str = "wb", write_fn: Callable = None
):
def put(self, contents, overwrite=False, mode: str = "wb"):
"""Put given contents in folder."""
self.mkdir()
if isinstance(contents, list):
Expand Down Expand Up @@ -134,12 +132,8 @@ def put(
file_key = key + filename
try:
blob = self.bucket.blob(file_key)
if write_fn:
with open(file_obj, "rb") as f:
write_fn(f, blob.upload_from_file(f))
else:
file_obj = self._serialize_file_obj(file_obj)
blob.upload_from_file(file_obj)
file_obj = self._serialize_file_obj(file_obj)
blob.upload_from_file(file_obj)

except Exception as e:
raise RuntimeError(f"Failed to upload {filename} to GCS: {e}")
Expand Down
19 changes: 4 additions & 15 deletions runhouse/resources/folders/s3_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess
import time
from pathlib import Path
from typing import Callable, List, Optional
from typing import List, Optional

from runhouse.logger import logger

Expand Down Expand Up @@ -113,9 +113,7 @@ def _s3_copy(self, new_path):
Key=new_key,
)

def put(
self, contents, overwrite=False, mode: str = "wb", write_fn: Callable = None
):
def put(self, contents, overwrite=False, mode: str = "wb"):
"""Put given contents in folder."""
self.mkdir()
if isinstance(contents, list):
Expand Down Expand Up @@ -154,17 +152,8 @@ def put(
for filename, file_obj in contents.items():
file_key = key + filename
try:
if write_fn:
with open(file_obj, "rb") as f:
write_fn(
f,
self.client.put_object(
Bucket=bucket_name, Key=file_key, Body=f.read()
),
)
else:
body = self._serialize_file_obj(file_obj)
self.client.put_object(Bucket=bucket_name, Key=file_key, Body=body)
body = self._serialize_file_obj(file_obj)
self.client.put_object(Bucket=bucket_name, Key=file_key, Body=body)

except Exception as e:
raise RuntimeError(f"Failed to upload {filename} to S3: {e}")
Expand Down
9 changes: 1 addition & 8 deletions runhouse/resources/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,15 +1153,8 @@ def openapi_spec(self, spec_name: Optional[str] = None):
param.default if param.default != inspect.Parameter.empty else ...,
)

# Filter out any callables which are not compatible for generating the schema
filtered_params = {
k: (v[0], v[1])
for k, v in params.items()
if not isinstance(v[0], Callable)
}

module_method_params = create_model(
f"{method_name}_schema", **filtered_params
f"{method_name}_schema", **params
).schema()
module_method_params["title"] = "kwargs"

Expand Down
3 changes: 1 addition & 2 deletions runhouse/resources/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,9 @@ def _write_config(self, config: dict = None, overwrite: bool = True):
config_to_write = config or self.config()
logger.debug(f"Config to save on system: {config_to_write}")
self.folder.put(
{self.RUN_CONFIG_FILE: config_to_write},
{self.RUN_CONFIG_FILE: json.dumps(config)},
overwrite=overwrite,
mode="w",
write_fn=lambda data, f: json.dump(data, f, indent=4),
)

def _detect_run_type(self):
Expand Down
Loading