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

Basic directory nodes #2695

Merged
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
1 change: 1 addition & 0 deletions backend/src/packages/chaiNNer_standard/utility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
text_group = utility_category.add_node_group("Text")
color_group = utility_category.add_node_group("Color")
random_group = utility_category.add_node_group("Random")
directory_group = utility_category.add_node_group("Directory")
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import annotations

from pathlib import Path

from nodes.properties.inputs import DirectoryInput, NumberInput
from nodes.properties.outputs import DirectoryOutput

from .. import directory_group


@directory_group.register(
schema_id="chainner:utility:back_directory",
name="Directory Go Up",
description="Traverse up from a directory the specified number of times.",
icon="BsFolder",
inputs=[
DirectoryInput(must_exist=False, label_style="hidden"),
NumberInput("Times", minimum=0, default=1, label_style="inline").with_docs(
"How many times to go up.",
"- 0 will return the given directory as is.",
"- 1 will return the parent directory.",
"- 2 will return the grandparent directory.",
"- etc.",
hint=True,
),
],
outputs=[
DirectoryOutput(
output_type="Directory { path: getParentDirectory(Input0.path, Input1) }",
),
],
)
def directory_go_up_node(directory: Path, amt: int) -> Path:
result = directory
for _ in range(amt):
result = result.parent
return result
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from __future__ import annotations

from pathlib import Path

from nodes.properties.inputs import DirectoryInput
from nodes.properties.outputs import TextOutput

from .. import directory_group


@directory_group.register(
schema_id="chainner:utility:directory_to_text",
name="Directory to Text",
description="Converts a directory path into text.",
icon="BsFolder",
inputs=[
DirectoryInput(must_exist=False, label_style="hidden"),
],
outputs=[
TextOutput(
"Dir Text",
output_type="Input0.path",
),
],
)
def directory_to_text_node(directory: Path) -> str:
return str(directory)
52 changes: 52 additions & 0 deletions src/common/types/chainner-builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
handleNumberLiterals,
intersect,
literal,
union,
wrapBinary,
wrapQuaternary,
wrapScopedUnary,
wrapTernary,
Expand Down Expand Up @@ -360,3 +362,53 @@ export const parseColorJson = wrapScopedUnary(
return createInstance(colorDesc);
}
);

const getParentDirectoryStr = (pathStr: string): string => {
return path.dirname(pathStr);
};
export const getParentDirectory = wrapBinary<StringPrimitive, Int, StringPrimitive>(
(pathStr, times) => {
if (times.type === 'literal' && times.value === 0) {
return pathStr;
}
if (pathStr.type === 'literal') {
let min;
let max;
if (times.type === 'literal') {
min = times.value;
max = times.value;
} else {
min = times.min;
max = times.max;
}

// the basic idea here is that repeatedly getting the parent directory of a path
// will eventually converge to the root directory, so we can stop when that happens
let p = pathStr.value;
for (let i = 0; i < min; i += 1) {
const next = getParentDirectoryStr(p);
if (next === p) {
return literal(p);
}
p = next;
}

if (min === max) {
return literal(p);
}

const values = new Set<string>();
values.add(p);
for (let i = min; i < max; i += 1) {
p = getParentDirectoryStr(p);
if (values.has(p)) {
break;
}
values.add(p);
}

return union(...[...values].map(literal));
}
return StringType.instance;
}
);
3 changes: 3 additions & 0 deletions src/common/types/chainner-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { lazy } from '../util';
import {
formatTextPattern,
getParentDirectory,
padCenter,
padEnd,
padStart,
Expand Down Expand Up @@ -126,6 +127,7 @@ intrinsic def padEnd(text: string, width: uint, padding: string): string;
intrinsic def padCenter(text: string, width: uint, padding: string): string;
intrinsic def splitFilePath(path: string): SplitFilePath;
intrinsic def parseColorJson(json: string): Color;
intrinsic def getParentDirectory(path: string, times: uint): string;
`;

export const getChainnerScope = lazy((): Scope => {
Expand All @@ -139,6 +141,7 @@ export const getChainnerScope = lazy((): Scope => {
padCenter: makeScoped(padCenter),
splitFilePath,
parseColorJson,
getParentDirectory: makeScoped(getParentDirectory),
};

const definitions = parseDefinitions(new SourceDocument(code, 'chainner-internal'));
Expand Down
Loading