Skip to content

Commit

Permalink
filemanager retrieve
Browse files Browse the repository at this point in the history
  • Loading branch information
noahehall committed Mar 16, 2023
1 parent 7ec90b0 commit a958e3b
Show file tree
Hide file tree
Showing 23 changed files with 97 additions and 1,399 deletions.
13 changes: 7 additions & 6 deletions bdd.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,21 @@ proc bdd*(caseName: string): (What, string, () -> bool) -> void =
of should: itShould msg, caseName, condition()
of shouldNot: itShouldNot msg, caseName, condition()
of shouldRaise, shouldNotRaise:
var didError = false
var didRaise = false
try: discard condition()
except CatchableError, OSError: didError = true
except CatchableError, OSError: didRaise = true
finally:
if what.ord == shouldRaise.ord: itShould msg, caseName, didError
else: itShouldNot msg, caseName, didError
if what.ord == shouldRaise.ord: itShould msg, caseName, didRaise
else: itShouldNot msg, caseName, didRaise
)


when isMainModule:
proc catchme: bool = raise newException(CatchableError, "if you can")
let it = bdd "bdd tests"
proc catchme: bool = raise TddError(msg: "if you can")

let it = bdd "bdd tests"
it should, "be true", () => true
it shouldNot, "be true", () => false
it shouldRaise, "error", () => catchme()
it shouldNotRaise, "error", () => true
it shouldNotRaise, "or care about result", () => false
62 changes: 0 additions & 62 deletions src/boat.dot
Original file line number Diff line number Diff line change
Expand Up @@ -26,66 +26,4 @@ digraph boat {
"system/io" -> "std/private/since";
"system/io" -> "system/formatfloat";
"system" -> "system/io";
"std/os" -> "std/private/since";
"std/strutils" -> "std/parseutils";
"std/math" -> "std/private/since";
"std/macros" -> "std/private/since";
"std/bitops" -> "std/macros";
"std/bitops" -> "std/private/since";
"std/bitops" -> "std/private/bitops_utils";
"std/bitops" -> "system/countbits_impl";
"std/math" -> "std/bitops";
"std/math" -> "std/fenv";
"std/strutils" -> "std/math";
"std/algorithm" -> "std/private/since";
"std/strutils" -> "std/algorithm";
"std/enumutils" -> "std/macros";
"std/typetraits" -> "std/private/since";
"std/typetraits" -> "std/macros";
"std/enumutils" -> "std/typetraits";
"std/strutils" -> "std/enumutils";
"std/strutils" -> "std/unicode";
"std/strutils" -> "std/private/since";
"std/strutils" -> "std/private/strimpl";
"std/pathnorm" -> "std/osseps";
"std/os" -> "std/strutils";
"std/os" -> "std/pathnorm";
"std/options" -> "std/typetraits";
"std/times" -> "std/strutils";
"std/times" -> "std/math";
"std/times" -> "std/options";
"std/times" -> "std/private/since";
"std/times" -> "std/posix";
"std/os" -> "std/posix";
"std/os" -> "std/times";
"boat/private/BoatConstants" -> "std/os";
"boat/private/BoatConstants" -> "boat/private/BoatErrors";
"std/sugar" -> "std/private/since";
"std/sugar" -> "std/macros";
"std/private/underscored_calls" -> "std/macros";
"std/sugar" -> "std/private/underscored_calls";
"../bdd" -> "std/sugar";
"boat/private/Config" -> "../bdd";
"std/streams" -> "std/private/since";
"std/lexbase" -> "std/strutils";
"std/lexbase" -> "std/streams";
"std/tables" -> "std/private/since";
"std/hashes" -> "std/private/since";
"std/tables" -> "std/hashes";
"std/tables" -> "std/math";
"std/tables" -> "std/algorithm";
"std/parsecfg" -> "std/strutils";
"std/parsecfg" -> "std/lexbase";
"std/parsecfg" -> "std/streams";
"std/parsecfg" -> "std/tables";
"std/parsecfg" -> "std/private/decode_helpers";
"std/parsecfg" -> "std/private/since";
"boat/private/Config" -> "std/os";
"boat/private/Config" -> "std/parsecfg";
"boat/private/Config" -> "std/strutils";
"boat/private/Config" -> "boat/private/BoatErrors";
"boat/private/Config" -> "boat/private/BoatConstants";
"boat" -> "boat/private/BoatConstants";
"boat" -> "boat/private/BoatErrors";
"boat" -> "boat/private/Config";
}
6 changes: 0 additions & 6 deletions src/boat.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
## .. include:: ./readme.rst

import boat/private/[
boatConfig,
boatConstants,
boatErrors,
]

proc boat*: void = echo "All HANDS! cat o'nine tails! blue peter! OMG... landlubber"

when isMainModule:
Expand Down
Binary file modified src/boat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 21 additions & 37 deletions src/boat/private/boatConfig.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
## TLDR
- come back later
todos
-----
- cmd to parse arbitrarily, e.g. boat conf load ./some/dir
- cmd to see current captains: e.g. boat conf list -> X, Y, Z
]##

from ../../../bdd import tddError
Expand All @@ -31,15 +27,12 @@ import
var captainsLog* {.global.} = %* {} ## \
## captains log is the world

proc parseLocalManifest*(self: BoatConfig, path: string = ""): bool =
## parse self.use to self.parsed
## prefer calling self.load or self.reload for validation
let usePath = self.usePath path

# self.parsed = self.parsed.retrieve usePath
proc parseManifest*(self: BoatConfig, path: string = "", ft: FileType): bool =
## sets self.parsed to the parsed manifest
self.parsed = ft.retrieve self.usePath path
result = true

proc localManifestIsValid*(self: BoatConfig, path: string = ""): bool =
proc manifestIsValid*(self: BoatConfig, path: string = ""): bool =
## throws if manifest not found, cant be read, or errors during parsing
let usePath = self.usePath path
let pathInfo = usePath.getFileInfo
Expand All @@ -48,62 +41,53 @@ proc localManifestIsValid*(self: BoatConfig, path: string = ""): bool =
of pcFile, pcLinkToFile:
if fpUserRead notin pathInfo.permissions: raise filePermissionError
elif not usePath.endsWith manifestName: raise manifestNameError
elif not self.parseLocalManifest usePath: raise configParseError
elif not self.parseManifest(usePath, localManifest): raise configParseError
else: true
of pcDir, pcLinkToDir:
# force directories to use their manifest
self.use = self.use / manifestName
self.localManifestIsValid()
self.manifestIsValid

proc save*(self: BoatConfig, path: string = ""): bool =
## serialize Self.parsed to disk @ boatConstants.cacheDir / <SELF.ID>.{manifestName}
## updates captains manifest with stuffWeCached.self.use -> cache location
# should call fileManager.toDisk
result = true

proc init*(self: BoatConfig, path: string = ""): bool =
let usePath = self.usePath path
proc init*(self: BoatConfig): bool =
# starts with https?
# ends with manifestName?
# save to boatConstants.tempDir / self.use
# recurse self.reload path = temp location
# check FileManagerUtils.retrieve
# it should contain logic for loading remote manifests
# throw: urls must point to a manifest.nim.ini
case usePath.startsWith "https"
case self.use.startsWith "https"
of true: raise tddError
else:
try: doAssert self.localManifestIsValid(path) == true
try: doAssert self.manifestIsValid == true
except CatchableError:
debugEcho repr getCurrentException()
raise fileLoadError
if not self.save usePath: raise fileSaveDefect
if not self.save: raise fileSaveDefect
else: result = true

proc reload*(self: BoatConfig): bool =
## reloads a configuration from disk
# (fpath, T) = FileMananger.fromDisk(...)
# self.parsedPath = fpath, self.parsed = T
## reloads a configuration from captainsLog
raise tddError

proc load*(self: BoatConfig): bool =
## (re)load a Configuration; safer than calling reload specifically
## (re)load a Configuration
result =
if captainsLogLoaded and self.parsedPath.len is Positive: self.reload()
else: self.init()
# if self.use in captainsLog ? reload from captainslog
if 1 > 2: raise tddError
else: self.init

proc loadCaptainsLog(): void =
## loads the previous or initializes a new captains log
if not captainsLogLoaded: echo "loading captains log"
# captainsLogLoaded = true
# let (fpath, prevCaptainsLog) = captainsLog.fromDisk(cacheDir / manifestName, JsonNode, false)
# captainsLog = prevCaptainsLog
captainsLogLoaded = true
# try to retrieve the prev captainslog from cachDir
# else initialize an empty captainslog

# always load the captainsLog into ram
if not captainsLogLoaded: loadCaptainsLog()


# consumers can retrieve the parsed Config and path on disk
# but only internal functions should be able to set it
export
boatConfigType.BoatConfig,
parsed,
parsedPath
export boatConfigType
19 changes: 4 additions & 15 deletions src/boat/private/boatConfigType.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,10 @@ type BoatConfig*[T: BoatConfigKind = Config] = ref object of RootObj
## base type for all boat configs
use*: string ## \
## filepath, dir containing a file or remote uri

parsed: T ## \
## the parsed config after loading
parsedPath: string ## \
parsed*: T ## \
## the parsed config if the config has been parsed
parsedPath*: string ## \
## path on disk the parsed config was saved to

proc `parsed=`*(self: BoatConfig, path: string): void =
self.parsed = self.parsed.retrieve path

proc `parsed`*(self: BoatConfig): BoatConfigKind = self.parsed

proc `parsedPath=`*(self: BoatConfig, path: string): void =
self.parsedPath = path

proc `parsedPath`*(self: BoatConfig): string = self.parsedPath

proc usePath*(self: BoatConfig, path: string): string =
proc usePath*(self: BoatConfig, path: string = ""): string =
if path.len is Positive: path else: self.use
4 changes: 3 additions & 1 deletion src/boat/private/boatConstants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ from parsecfg import Config

import boatErrors

type BoatConfigKind* = Config | JsonNode
type BoatConfigKind* = Config | JsonNode ## \
## a Config generally means a manifest
## while JsonNode indicates a captainslog

var captainsLogLoaded* {.global.} = false ## \
## true if we've loaded the captains log from disk into ram
Expand Down
2 changes: 1 addition & 1 deletion src/boat/private/boatErrors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ type BoatDefect = ref object of Defect ## \
var dirCreateDefect* = BoatDefect(msg: "Dir Create Failed")
var fileLoadDefect* = BoatDefect(msg: "Cant Load File")
var fileSaveDefect* = BoatDefect(msg: "Cant Save File")
var boatConfigKindError* = BoatDefect(msg: fmt"Expected BoatConfigKind")
var boatConfigKindError* = BoatDefect(msg: fmt"Expected typeof BoatConfigKind")
26 changes: 11 additions & 15 deletions src/boat/private/fileManager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,25 @@

from ../../../bdd import tddError

import std/[
locks,
threadpool,
]
# import std/[
# locks,
# threadpool,
# ]

import
boatConstants,
boatErrors,
fileManagerUtils

type SaveType* = enum
parsedConfig,
captainsLog,
remoteManifest,

proc dir*(self: SaveType): string =
## returns the directory where different SaveTypes are persisted
proc dir*(self: FileType): string =
## returns the directory where different FileTypes are persisted
result = case self
of parsedConfig, captainsLog: cacheDir
of localManifest, captainsLog: cacheDir
else: tempDir

proc path*(self: SaveType, fname: string): string =
## computes the filpath for a SaveType
proc path*(self: FileType, fname: string): string =
## computes the filpath for a FileType
raise tddError
# result = self.dir / hash(fname)

Expand All @@ -39,7 +35,7 @@ proc encode*[T: JsonNode | string](self: T): string =


proc toDisk*[T: JsonNode | string | Config](
self: SaveType,
self: FileType,
fname: string,
data: T,
captainsLog: JsonNode
Expand All @@ -63,7 +59,7 @@ proc toDisk*[T: JsonNode | string | Config](


proc fromDisk*[T](
self: SaveType,
self: FileType,
fname: string,
to: T,
errorNotFound = false
Expand Down
20 changes: 12 additions & 8 deletions src/boat/private/fileManagerUtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ import std/[

import boatErrors, boatConstants

type FileType* = enum
captainsLog,
localManifest,
remoteManifest,

proc persist*[T: string | Config](self: T, path: string): Future[void] {.async.} =
## writes strings to path
## calls parsecfg.writeConfig for configs
proc persist*[T: FileType](self: T, path: string): Future[void] {.async.} =
## persists a FileType to path
raise tddError
# lock
# of string -> data.write path
# of Config -> parsecfg.writeConfig path
# unlock
# throw if any errors occur

proc retrieve*[T: BoatConfigKind](self: T, path: string): T =
# retrieves a Config or Json from a path
proc retrieve*[T: FileType](self: T, path: string): BoatConfigKind =
## retrieves a FileType from path and parses to BoatConfigKind
try:
if self is Config: result = loadConfig path
elif self is JsonNode: raise tddError # should load json from path
else: raise boatConfigKindError
result = case self
of captainsLog: raise tddError # parse to json
of localManifest: loadConfig path
of remoteManifest: raise tddError # download, then loadConfig path
except CatchableError:
debugEcho repr getCurrentException()
raise fileLoadDefect
Expand Down
Loading

0 comments on commit a958e3b

Please sign in to comment.