"""Offline Classic ASP Folder object verification lab (IIS ASP absent)."""
from datetime import datetime, timezone
import json
from pathlib import Path
import shutil
import tempfile
def derive_short_folder(name: str) -> str:
clean = "".join(ch for ch in name.upper() if ch.isalnum())
return clean if len(clean) <= 8 else clean[:6] + "~1"
class MockFolder:
"""Offline model of Scripting.Folder backed by a real temporary directory."""
def __init__(self, path: Path, attributes: int = 16) -> None:
self._path = path
self._attributes = attributes
@property
def Files(self) -> list[str]:
return sorted(p.name for p in self._path.iterdir() if p.is_file())
@property
def SubFolders(self) -> list[str]:
return sorted(p.name for p in self._path.iterdir() if p.is_dir())
@property
def Attributes(self) -> int:
return self._attributes
@Attributes.setter
def Attributes(self, value: int) -> None:
self._attributes = value
@property
def DateCreated(self) -> str:
return datetime.fromtimestamp(self._path.stat().st_ctime, tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
@property
def DateLastAccessed(self) -> str:
return datetime.fromtimestamp(self._path.stat().st_atime, tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
@property
def DateLastModified(self) -> str:
return datetime.fromtimestamp(self._path.stat().st_mtime, tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
@property
def Drive(self) -> str:
return self._path.drive.upper() or "C:"
@property
def IsRootFolder(self) -> bool:
return self._path == self._path.parent
@property
def Name(self) -> str:
return self._path.name
@Name.setter
def Name(self, new_name: str) -> None:
target = self._path.with_name(new_name)
self._path.rename(target)
self._path = target
@property
def ParentFolder(self) -> str:
return str(self._path.parent)
@property
def Path(self) -> str:
return str(self._path)
@property
def ShortName(self) -> str:
return derive_short_folder(self._path.name)
@property
def ShortPath(self) -> str:
return str(self._path.parent / self.ShortName)
@property
def Size(self) -> int:
return sum(p.stat().st_size for p in self._path.rglob("*") if p.is_file())
@property
def Type(self) -> str:
return "File Folder"
def Copy(self, destination: Path) -> "MockFolder":
shutil.copytree(self._path, destination)
return MockFolder(destination, self._attributes)
def Move(self, destination: Path) -> None:
shutil.move(str(self._path), str(destination))
self._path = destination
def Delete(self) -> None:
shutil.rmtree(self._path)
def CreateTextFile(self, filename: str, overwrite: bool = True):
target = self._path / filename
if target.exists() and not overwrite:
raise FileExistsError(str(target))
return target.open("w", encoding="utf-8", newline="\r\n")
def main() -> None:
cases = []
with tempfile.TemporaryDirectory(prefix="asp_folder_") as tmp:
root = Path(tmp)
work = root / "SampleProjectFolder"
work.mkdir()
(work / "a.txt").write_bytes(b"12345")
(work / "b.txt").write_bytes(b"1234567890")
sub = work / "nested"
sub.mkdir()
(sub / "c.txt").write_bytes(b"123")
fo = MockFolder(work)
# Case 1: Collections (Files, SubFolders), recursive Size, timestamps
expected1 = {
"files": ["a.txt", "b.txt"],
"subfolders": ["nested"],
"recursive_size": 18,
"has_timestamps": True,
}
actual1 = {
"files": fo.Files,
"subfolders": fo.SubFolders,
"recursive_size": fo.Size,
"has_timestamps": all(len(t) == 19 for t in (fo.DateCreated, fo.DateLastAccessed, fo.DateLastModified)),
}
cases.append({
"name": "folder_collections_size_timestamps",
"summary": "Verify Folder.Files, Folder.SubFolders, recursive Folder.Size, and timestamps.",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: Identity, IsRootFolder, Attributes, ShortName/ShortPath, Type
anchor_folder = MockFolder(Path(root.anchor))
expected2 = {
"name": "SampleProjectFolder",
"is_root_subdir": False,
"is_root_anchor": True,
"attributes": 16,
"short_name": "SAMPLE~1",
"type": "File Folder",
"parent_matches": True,
}
actual2 = {
"name": fo.Name,
"is_root_subdir": fo.IsRootFolder,
"is_root_anchor": anchor_folder.IsRootFolder,
"attributes": fo.Attributes,
"short_name": fo.ShortName,
"type": fo.Type,
"parent_matches": fo.ParentFolder == str(root),
}
cases.append({
"name": "folder_identity_and_root_check",
"summary": "Verify Name, IsRootFolder (True for root, False for subdir), Attributes, ShortName, Type.",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: Folder Copy, Move, Rename, Delete
copied = fo.Copy(root / "copied_proj")
copied.Name = "renamed_proj"
moved_target = root / "moved_proj"
copied.Move(moved_target)
moved_files = copied.Files
copied.Delete()
expected3 = {
"moved_files": ["a.txt", "b.txt"],
"deleted_cleanly": True,
"source_preserved": True,
}
actual3 = {
"moved_files": moved_files,
"deleted_cleanly": not moved_target.exists(),
"source_preserved": work.is_dir(),
}
cases.append({
"name": "folder_copy_move_delete",
"summary": "Verify Folder.Copy, Folder.Name rename, Folder.Move, and recursive Folder.Delete.",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: Folder.CreateTextFile
with fo.CreateTextFile("index.asp", overwrite=True) as fh:
fh.write("<% Response.Write(\"OK\") %>")
expected4 = {
"files_after_create": ["a.txt", "b.txt", "index.asp"],
"content": "<% Response.Write(\"OK\") %>",
}
actual4 = {
"files_after_create": fo.Files,
"content": (work / "index.asp").read_text(encoding="utf-8"),
}
cases.append({
"name": "folder_create_textfile",
"summary": "Verify Folder.CreateTextFile creates a file directly inside the folder and updates Files.",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
print(json.dumps({
"version": "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)",
"cases": cases,
}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/asp-ref-folder-lab/explore-ref-folder.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-folder.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a1425.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1444.zip