Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 12, 2024
1 parent fb909d2 commit 0b59a46
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.
4 changes: 3 additions & 1 deletion pyflask/apis/neuroconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def post(self):
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))


@neuroconv_api.route("/locate/autocomplete")
class Locate(Resource):
@neuroconv_api.doc(responses={200: "Success", 400: "Bad Request", 500: "Internal server error"})
Expand All @@ -86,7 +87,8 @@ def post(self):
except Exception as exception:
if notBadRequestException(exception):
neuroconv_api.abort(500, str(exception))



@neuroconv_api.route("/metadata")
class Metadata(Resource):
@neuroconv_api.doc(responses={200: "Success", 400: "Bad Request", 500: "Internal server error"})
Expand Down
27 changes: 15 additions & 12 deletions pyflask/manageNeuroconv/manage_neuroconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@

announcer = MessageAnnouncer()


def is_path_contained(child, parent):
parent = Path(parent).resolve()
child = Path(child).resolve()

# Attempt to construct a relative path from parent to child
try:
child.relative_to(parent)
return True
except ValueError:
return False


def replace_nan_with_none(data):
if isinstance(data, dict):
# If it's a dictionary, iterate over its items and replace NaN values with None
Expand Down Expand Up @@ -118,6 +120,7 @@ def coerce_schema_compliance_recursive(obj, schema):
copy.deepcopy(json_object), resolve_references(copy.deepcopy(json_schema))
)


def autocomplete_format_string(info: dict) -> str:
from neuroconv.tools.path_expansion import construct_path_template

Expand All @@ -126,30 +129,30 @@ def autocomplete_format_string(info: dict) -> str:

if not is_path_contained(filesystem_entry_path, base_directory):
raise "Path is not contained in the provided base directory."

full_format_string = construct_path_template(filesystem_entry_path, subject_id=info["subject_id"], session_id=info["session_id"], **info["additional_metadata"])


full_format_string = construct_path_template(
filesystem_entry_path,
subject_id=info["subject_id"],
session_id=info["session_id"],
**info["additional_metadata"],
)

parent = Path(base_directory).resolve()
child = Path(full_format_string).resolve()

format_string = str(child.relative_to(parent))

to_locate_info = dict(base_directory=base_directory)

if (Path(filesystem_entry_path).is_dir()):
if Path(filesystem_entry_path).is_dir():
to_locate_info["folder_path"] = format_string
else:
to_locate_info["file_path"] = format_string

all_matched = locate_data(dict(autocomplete=to_locate_info))

all_matched = locate_data(dict(
autocomplete = to_locate_info
))
return dict(matched=all_matched, format_string=format_string)

return dict(
matched=all_matched,
format_string=format_string
)

def locate_data(info: dict) -> dict:
"""Locate data from the specifies directories using fstrings."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ import { Button } from "../../../Button.js";
import { Modal } from "../../../Modal";
import { header } from "../../../forms/utils";

import autocompleteIcon from '../../../assets/inspect.svg?raw'
import autocompleteIcon from "../../../assets/inspect.svg?raw";


export async function autocompleteFormatString( path ) {
export async function autocompleteFormatString(path) {
let notification;

const { base_directory } = path.reduce((acc, key) => acc[key] ?? {}, this.form.resolved)
const { base_directory } = path.reduce((acc, key) => acc[key] ?? {}, this.form.resolved);

const notify = (message, type) => {
if (notification) this.dismiss(notification);
return (notification = this.notify(message, type));
};

if (!base_directory) {
const message = `Please fill out the <b>base directory</b> for ${header(path[0])} before attempting auto-completion.`
notify(message, 'error')
throw new Error(message)
const message = `Please fill out the <b>base directory</b> for ${header(path[0])} before attempting auto-completion.`;
notify(message, "error");
throw new Error(message);
}

const modal = new Modal({
Expand All @@ -47,53 +46,52 @@ export async function autocompleteFormatString( path ) {
paddingBottom: "0px",
});

const propOrder = ["path", "subject_id", "session_id"]
const propOrder = ["path", "subject_id", "session_id"];
const form = new JSONSchemaForm({
schema: {
type: "object",
properties: {
path: {
type: "string",
title: "Example Filesystem Entry",
format: [ "file", "directory" ],
description: "Provide an example filesystem entry for the selected interface"
format: ["file", "directory"],
description: "Provide an example filesystem entry for the selected interface",
},
subject_id: {
type: "string",
description: "The subject ID in the above entry"
description: "The subject ID in the above entry",
},
session_id: {
type: "string",
description: "The session ID in the above entry"
description: "The session ID in the above entry",
},
},
required: propOrder,
order: propOrder
order: propOrder,
},
validateOnChange: async (name, parent) => {
const value = parent[name];

if (name === 'path') {
const errors = []
if (name === "path") {
const errors = [];
for (let key in parent) {
if (key === name) continue;
if (!value.includes(parent[key])) errors.push( {
type: "error",
message: `${header(name)} not found in the updated path.`,
})
if (!value.includes(parent[key]))
errors.push({
type: "error",
message: `${header(name)} not found in the updated path.`,
});
}
} else {
if (!parent.path || !parent.path.includes(value))
return [
{
type: "error",
message: `${header(name)} not found in the provided path.`,
},
];
}

else {
if (!parent.path || !parent.path.includes(value)) return [
{
type: "error",
message: `${header(name)} not found in the provided path.`,
},
];
}

}
},
});

content.append(form);
Expand All @@ -111,8 +109,12 @@ export async function autocompleteFormatString( path ) {
throw e;
});

const results = await run('locate/autocomplete', { base_directory, additional_metadata: {}, ...form.results })
const input = this.form.getFormElement([ ...path, 'format_string_path' ]);
const results = await run("locate/autocomplete", {
base_directory,
additional_metadata: {},
...form.results,
});
const input = this.form.getFormElement([...path, "format_string_path"]);
input.updateData(results.format_string);
this.save();
resolve(results.format_string);
Expand Down Expand Up @@ -385,7 +387,7 @@ export class GuidedPathExpansionPage extends Page {

// Require properties for all sources
const generatedSchema = { type: "object", properties: {}, additionalProperties: false };
const controls = {}
const controls = {};
for (let key in this.info.globalState.interfaces) {
generatedSchema.properties[key] = { type: "object", ...pathExpansionSchema };

Expand All @@ -397,12 +399,10 @@ export class GuidedPathExpansionPage extends Page {
buttonStyles: {
width: "max-content",
},
onClick: async () => autocompleteFormatString.call(this, [key])
onClick: async () => autocompleteFormatString.call(this, [key]),
}),
]
}


],
};
}
structureState.schema = generatedSchema;

Expand Down

0 comments on commit 0b59a46

Please sign in to comment.