"""Offline Classic ASP File object verification lab (IIS ASP absent)."""
from datetime import datetime, timezone
import json
from pathlib import Path
import shutil
import tempfile
def derive_short_name(name: str) -> str:
stem, dot, ext = name.partition(".")
clean_stem = "".join(ch for ch in stem.upper() if ch.isalnum())
clean_ext = "".join(ch for ch in ext.upper() if ch.isalnum())[:3]
short_stem = clean_stem if len(clean_stem) <= 8 else clean_stem[:6] + "~1"
return f"{short_stem}.{clean_ext}" if dot and clean_ext else short_stem
class MockFile:
"""Offline model of Scripting.File backed by a real temporary file."""
def __init__(self, path: Path, attributes: int = 32) -> None:
self._path = path
self._attributes = attributes
@property
def Attributes(self) -> int:
return self._attributes
@Attributes.setter
def Attributes(self, value: int) -> None:
self._attributes = value
@property
def DateCreated(self) -> str:
st = self._path.stat()
return datetime.fromtimestamp(st.st_ctime, tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
@property
def DateLastAccessed(self) -> str:
st = self._path.stat()
return datetime.fromtimestamp(st.st_atime, tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
@property
def DateLastModified(self) -> str:
st = self._path.stat()
return datetime.fromtimestamp(st.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 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_name(self._path.name)
@property
def ShortPath(self) -> str:
return str(self._path.parent / self.ShortName)
@property
def Size(self) -> int:
return self._path.stat().st_size
@property
def Type(self) -> str:
ext = self._path.suffix.lower()
return {".txt": "Text Document", ".asp": "Active Server Document"}.get(ext, "File")
def Copy(self, destination: Path) -> "MockFile":
shutil.copy2(self._path, destination)
return MockFile(destination, self._attributes)
def Move(self, destination: Path) -> None:
shutil.move(str(self._path), str(destination))
self._path = destination
def Delete(self) -> None:
self._path.unlink()
def OpenAsTextStream(self, iomode: int = 1):
mode = {1: "r", 2: "w", 8: "a"}[iomode]
return self._path.open(mode, encoding="utf-8")
def main() -> None:
cases = []
with tempfile.TemporaryDirectory(prefix="asp_file_") as tmp:
root = Path(tmp)
p = root / "test.txt"
p.write_bytes(b"Hello ASP File!")
f = MockFile(p)
# Case 1: Metadata & timestamps (DateCreated, DateLastAccessed, DateLastModified, Size, Type, Drive, ParentFolder)
expected1 = {
"name": "test.txt",
"size": 15,
"type": "Text Document",
"has_timestamps": True,
"parent_matches": True,
"drive_has_colon": True,
}
actual1 = {
"name": f.Name,
"size": f.Size,
"type": f.Type,
"has_timestamps": all(len(ts) == 19 for ts in (f.DateCreated, f.DateLastAccessed, f.DateLastModified)),
"parent_matches": f.ParentFolder == str(root),
"drive_has_colon": f.Drive.endswith(":"),
}
cases.append({
"name": "file_metadata_and_timestamps",
"summary": "Verify File DateCreated/Accessed/Modified, Size, Type, Drive, Path, and ParentFolder.",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: Attributes bitmask & 8.3 ShortName / ShortPath
long_p = root / "LongDocumentName.txt"
long_p.write_text("8.3 test", encoding="utf-8")
lf = MockFile(long_p, attributes=32)
lf.Attributes = lf.Attributes | 1 # Archive + ReadOnly = 33
expected2 = {
"attributes": 33,
"short_name": "LONGDO~1.TXT",
"short_path_ends_short": True,
}
actual2 = {
"attributes": lf.Attributes,
"short_name": lf.ShortName,
"short_path_ends_short": lf.ShortPath.endswith("LONGDO~1.TXT"),
}
cases.append({
"name": "file_attributes_and_short_names",
"summary": "Verify File Attributes bitmask read/write and 8.3 ShortName/ShortPath convention.",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: Copy, Move, Name rename, Delete
copy_dst = root / "copy.txt"
copied = f.Copy(copy_dst)
copied.Name = "renamed.txt"
moved_dst = root / "sub" / "final.txt"
moved_dst.parent.mkdir()
copied.Move(moved_dst)
moved_exists = moved_dst.is_file()
copied.Delete()
after_delete = moved_dst.exists()
expected3 = {
"moved_exists": True,
"after_delete": False,
"original_intact": True,
}
actual3 = {
"moved_exists": moved_exists,
"after_delete": after_delete,
"original_intact": p.is_file(),
}
cases.append({
"name": "file_copy_move_rename_delete",
"summary": "Verify File.Copy, File.Name rename, File.Move, and File.Delete operations.",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: OpenAsTextStream (append + read)
with f.OpenAsTextStream(8) as fh:
fh.write("\nAppended")
with f.OpenAsTextStream(1) as fh:
lines = fh.read().splitlines()
expected4 = ["Hello ASP File!", "Appended"]
cases.append({
"name": "file_open_as_textstream",
"summary": "Verify File.OpenAsTextStream appends and reads back updated content.",
"expected": expected4,
"actual": lines,
"passed": lines == 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-file-lab/explore-ref-file.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-file.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1442.zip
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a1425.zip