Skip to content

Commit

Permalink
Rename to getattr_
Browse files Browse the repository at this point in the history
[ci skip-build-wheels]
  • Loading branch information
gshuflin committed Oct 13, 2020
1 parent 7a1260b commit 093deb3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/rust/engine/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ impl Function {
pub fn name(&self) -> String {
let Function(key) = self;
let val = externs::val_for(&key);
let module = externs::project_str(&val, "__module__");
let name = externs::project_str(&val, "__name__");
let module = externs::getattr_as_string(&val, "__module__");
let name = externs::getattr_as_string(&val, "__name__");
// NB: this is a custom dunder method that Python code should populate before sending the
// function (e.g. an `@rule`) through FFI.
let line_number: u64 = externs::getattr(&val, "__line_number__").unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/rust/engine/src/externs/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ fn capture_snapshots(
let path_globs_and_roots = values
.iter()
.map(|value| {
let root = PathBuf::from(externs::project_str(&value, "root"));
let root = PathBuf::from(externs::getattr_as_string(&value, "root"));
let path_globs = nodes::Snapshot::lift_prepared_path_globs(
&externs::getattr(&value, "path_globs").unwrap(),
);
Expand Down Expand Up @@ -1566,7 +1566,7 @@ fn run_local_interactive_process(
if hermetic_env {
command.env_clear();
}
let env = externs::collect_frozendict(&value, "env");
let env = externs::getattr_from_frozendict(&value, "env");
command.envs(env);

let mut subprocess = command.spawn().map_err(|e| format!("Error executing interactive process: {}", e.to_string()))?;
Expand Down
6 changes: 3 additions & 3 deletions src/rust/engine/src/externs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub fn collect_iterable(value: &PyObject) -> Result<Vec<PyObject>, String> {
}
}

pub fn collect_frozendict(value: &PyObject, field: &str) -> BTreeMap<String, String> {
pub fn getattr_from_frozendict(value: &PyObject, field: &str) -> BTreeMap<String, String> {
let frozendict = getattr(value, field).unwrap();
let pydict: PyDict = getattr(&frozendict, "_data").unwrap();
let gil = Python::acquire_gil();
Expand All @@ -236,7 +236,7 @@ pub fn collect_frozendict(value: &PyObject, field: &str) -> BTreeMap<String, Str
.collect()
}

pub fn project_str(value: &PyObject, field: &str) -> String {
pub fn getattr_as_string(value: &PyObject, field: &str) -> String {
// TODO: It's possible to view a python string as a `Cow<str>`, so we could avoid actually
// cloning in some cases.
// TODO: We can't directly extract as a string here, because val_to_str defaults to empty string
Expand All @@ -249,7 +249,7 @@ pub fn key_to_str(key: &Key) -> String {
}

pub fn type_to_str(type_id: TypeId) -> String {
project_str(&type_for_type_id(type_id).into_object(), "__name__")
getattr_as_string(&type_for_type_id(type_id).into_object(), "__name__")
}

pub fn val_to_str(obj: &PyObject) -> String {
Expand Down
6 changes: 3 additions & 3 deletions src/rust/engine/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn remove_prefix_request_to_digest(
let input_digest =
lift_directory_digest(&core.types, &externs::getattr(&args[0], "digest").unwrap())
.map_err(|e| throw(&e))?;
let prefix = externs::project_str(&args[0], "prefix");
let prefix = externs::getattr_as_string(&args[0], "prefix");
let prefix = RelativePath::new(PathBuf::from(prefix))
.map_err(|e| throw(&format!("The `prefix` must be relative: {:?}", e)))?;
let digest = store
Expand All @@ -252,7 +252,7 @@ fn add_prefix_request_to_digest(
let input_digest =
lift_directory_digest(&core.types, &externs::getattr(&args[0], "digest").unwrap())
.map_err(|e| throw(&e))?;
let prefix = externs::project_str(&args[0], "prefix");
let prefix = externs::getattr_as_string(&args[0], "prefix");
let prefix = RelativePath::new(PathBuf::from(prefix))
.map_err(|e| throw(&format!("The `prefix` must be relative: {:?}", e)))?;
let digest = store
Expand Down Expand Up @@ -347,7 +347,7 @@ fn create_digest_to_digest(
let digests: Vec<_> = file_contents_and_directories
.into_iter()
.map(|file_content_or_directory| {
let path = externs::project_str(&file_content_or_directory, "path");
let path = externs::getattr_as_string(&file_content_or_directory, "path");
let store = context.core.store();
async move {
let path = RelativePath::new(PathBuf::from(path))
Expand Down
24 changes: 12 additions & 12 deletions src/rust/engine/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub fn lift_directory_digest(types: &Types, digest: &Value) -> Result<hashing::D
digest, types.directory_digest
));
}
let fingerprint = externs::project_str(&digest, "fingerprint");
let fingerprint = externs::getattr_as_string(&digest, "fingerprint");
let digest_length: usize = externs::getattr(&digest, "serialized_bytes_length").unwrap();
Ok(hashing::Digest(
hashing::Fingerprint::from_hex_string(&fingerprint)?,
Expand All @@ -225,7 +225,7 @@ pub fn lift_file_digest(types: &Types, digest: &Value) -> Result<hashing::Digest
if types.file_digest != externs::get_type_for(digest) {
return Err(format!("{} is not of type {}.", digest, types.file_digest));
}
let fingerprint = externs::project_str(&digest, "fingerprint");
let fingerprint = externs::getattr_as_string(&digest, "fingerprint");
let digest_length: usize = externs::getattr(&digest, "serialized_bytes_length").unwrap();
Ok(hashing::Digest(
hashing::Fingerprint::from_hex_string(&fingerprint)?,
Expand All @@ -247,10 +247,10 @@ impl MultiPlatformExecuteProcess {
value: &Value,
target_platform: PlatformConstraint,
) -> Result<Process, String> {
let env = externs::collect_frozendict(&value, "env");
let env = externs::getattr_from_frozendict(&value, "env");

let working_directory = {
let val = externs::project_str(&value, "working_directory");
let val = externs::getattr_as_string(&value, "working_directory");
if val.is_empty() {
None
} else {
Expand Down Expand Up @@ -282,17 +282,17 @@ impl MultiPlatformExecuteProcess {
Some(Duration::from_millis((timeout_in_seconds * 1000.0) as u64))
};

let description = externs::project_str(&value, "description");
let description = externs::getattr_as_string(&value, "description");
let py_level: PyObject = externs::getattr(&value, "level").unwrap();
let level = externs::val_to_log_level(&py_level)?;

let append_only_caches = externs::collect_frozendict(&value, "append_only_caches")
let append_only_caches = externs::getattr_from_frozendict(&value, "append_only_caches")
.into_iter()
.map(|(name, dest)| Ok((CacheName::new(name)?, CacheDest::new(dest)?)))
.collect::<Result<_, String>>()?;

let jdk_home = {
let val = externs::project_str(&value, "jdk_home");
let val = externs::getattr_as_string(&value, "jdk_home");
if val.is_empty() {
None
} else {
Expand All @@ -303,7 +303,7 @@ impl MultiPlatformExecuteProcess {
let is_nailgunnable: bool = externs::getattr(&value, "is_nailgunnable").unwrap();

let execution_slot_variable = {
let s = externs::project_str(&value, "execution_slot_variable");
let s = externs::getattr_as_string(&value, "execution_slot_variable");
if s.is_empty() {
None
} else {
Expand Down Expand Up @@ -615,7 +615,7 @@ impl Snapshot {
pub fn lift_path_globs(item: &Value) -> Result<PathGlobs, String> {
let globs: Vec<String> = externs::getattr(item, "globs").unwrap();

let description_of_origin_field = externs::project_str(item, "description_of_origin");
let description_of_origin_field = externs::getattr_as_string(item, "description_of_origin");
let description_of_origin = if description_of_origin_field.is_empty() {
None
} else {
Expand All @@ -624,12 +624,12 @@ impl Snapshot {

let glob_match_error_behavior: PyObject =
externs::getattr(item, "glob_match_error_behavior").unwrap();
let failure_behavior = externs::project_str(&glob_match_error_behavior, "value");
let failure_behavior = externs::getattr_as_string(&glob_match_error_behavior, "value");
let strict_glob_matching =
StrictGlobMatching::create(failure_behavior.as_str(), description_of_origin)?;

let conjunction_obj: PyObject = externs::getattr(item, "conjunction").unwrap();
let conjunction_string = externs::project_str(&conjunction_obj, "value");
let conjunction_string = externs::getattr_as_string(&conjunction_obj, "value");
let conjunction = GlobExpansionConjunction::create(&conjunction_string)?;
Ok(PathGlobs::new(globs, strict_glob_matching, conjunction))
}
Expand Down Expand Up @@ -850,7 +850,7 @@ impl WrappedNode for DownloadedFile {

async fn run_wrapped_node(self, context: Context) -> NodeResult<Digest> {
let value = externs::val_for(&self.0);
let url_str = externs::project_str(&value, "url");
let url_str = externs::getattr_as_string(&value, "url");

let url = Url::parse(&url_str)
.map_err(|err| throw(&format!("Error parsing URL {}: {}", url_str, err)))?;
Expand Down

0 comments on commit 093deb3

Please sign in to comment.