public
Last active

A data type to simplify in-memory operations with project's files (and generally files in a directory)

  • Download Gist
FileInfo.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
(* Mathematica Package *)
 
BeginPackage["FileInfo`", {"OO`","OO`Methods`","OO`Errors`", "RuleTreeInfo`"}]
(* Exported symbols added here with SymbolName::usage *)
 
FileInfo;
 
Begin["`Private`"] (* Begin Private Context *)
 
join = Function[{dir, file}, FileNameJoin[{dir, file}]];
 
ClearAll[getFile];
getFile[fullName_String?FileExistsQ] :=
getFile[FileNameTake[fullName, -1], DirectoryName[fullName]];
getFile[file_String, prdir_String] :=
With[{fcont = Quiet@Import[prdir~join~file, "String"]},
(file -> fcont) /; fcont =!= $Failed
];
getFile[file_String, _] :=
ThrowError[getFile, file];
 
 
 
ClearAll[getFiles];
getFiles[files : {__String}, prdir_String?DirectoryQ] :=
getFile @@@ Thread[{files, prdir}];
getFiles[___] := ThrowError[getFiles];
 
 
 
ClearAll[getAllFiles];
getAllFiles[prdir_String?DirectoryQ] :=
getFile /@ FileNames["*", {prdir}];
getAllFiles[___] := ThrowError[getAllFiles];
 
 
 
DeclareType[FileInfo ~ Extends ~ RuleTreeInfo][
bindToFile[file_String] :=
ThrowError[FileInfo, bindToFile, "unsupported_super_method"]
,
OO`Methods`bindToDir[dir_String?DirectoryQ] :=
$self@AddMethods[
OO`Methods`getDir[] := dir
,
read[files : {__String?FileExistsQ} | All : All] :=
(
$content =
If[files === All, getAllFiles[dir], getFiles[files, dir]];
$self@normal[]
)
,
OO`Methods`getFileNames[] := $content[[All, 1]]
,
save[] :=
Map[$self@save[#] &, $self@OO`Methods`getFileNames[]]
,
save[file_String] :=
With[{result =
Quiet@Export[
FileNameJoin[{dir, file}],
$self@get[file],
"String"
]},
result /; result =!= $Failed]
,
save[file_String] := ThrowError[save]
](* AddMethods *)
,
OO`Methods`new[dir_String?DirectoryQ] :=
Module[{},
$self@OO`Methods`bindToDir[dir];
$self@read[];
$self
]
];
 
End[] (* End Private Context *)
 
EndPackage[]
README.md
Markdown

FileInfo

A data type to simplify in-memory operations with project's files (and generally files in a directory)

project.m
Matlab
1 2 3 4 5 6 7 8 9 10 11 12
{
"author"->
{
"name" -> "lshifr",
"email" -> "lshifr@gmail.com",
"url" -> "http://www.mathprogramming-intro.org"
},
"name" -> "FileInfo",
"mathematica_version" -> "8.0+",
"description" -> "A data type to simplify in-memory operations with project's files (and generally files in a directory)",
"url" -> "https://gist.github.com/4298795"
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.