Skip to content

Commit

Permalink
Basic directory nodes (#2695)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Mar 21, 2024
1 parent c63b74c commit b0d4249
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
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

0 comments on commit b0d4249

Please sign in to comment.