I'm using ReadList
to pull the declarations off of a package like so:
PackagePullDeclarationsAction // Clear
PackagePullDeclarationsAction[
Hold[
_Begin | _BeginPackage |
CompoundExpression[_Begin | _BeginPackage, ___]
]
] :=
Throw[Begin];
PackagePullDeclarationsAction[e : Except[Hold[Expression]]] :=
Sow@e;
PackagePullHeader[pkgFile_] :=
Reap[
Catch@
ReadList[pkgFile,
PackagePullDeclarationsAction@Hold[Expression]
]
][[2, 1]]
The PackagePullDeclarationsAction
either uses Sow
to sow an expression or Throw
to abort the ReadList
call. And this works as intended:
$pkgFile =
URLDownload[
"https://raw.githubusercontent.com/b3m2a1/mathematica-BTools/\
master/Packages/AppBuilder.m",
FileNameJoin@{$TemporaryDirectory, "AppBuilder.m"}
];
Begin["test`"];
Take[
PackagePullHeader[$pkgFile] // DeleteCases[Hold[Null]],
5
]
End[];
{Hold[$AppDirectoryRoot::usage =
"The directory root for finding apps";],
Hold[$AppDirectoryName::usage =
"The basic extension to a directory for locating apps";],
Hold[$AppDirectory::usage = "Joins the root and name";],
Hold[AppPath::usage = "A path parser for a given app name";],
Hold[AppDirectory::usage =
"Used by AppPath find appropriate directories";]}
The problem is that it also loaded all of my "`Private`"
symbols into the primary Context
:
Names["test`*"]~Take~5
{"test`a", "test`add", "test`app", "test`AppAddContent", \
"test`AppAddDocPage"}
What's going on here? ReadList
clearly didn't ignore the Throw
so did it load everything first then apply PackagePullDeclarationsAction
?
Is there a good work-around for this? Am I just missing something trivial?
Begin
. Hence the context issues. My old version used a loop until it hit that. I wanted something faster. $\endgroup$ – b3m2a1 Aug 18 '17 at 7:03ReadList
reads all first. $\endgroup$ – Kuba♦ Aug 18 '17 at 7:31Context[s]=...
but that was slower than the loop. $\endgroup$ – b3m2a1 Aug 18 '17 at 7:32