Skip to content

Commit a958e3b

Browse files
committed
filemanager retrieve
1 parent 7ec90b0 commit a958e3b

23 files changed

+97
-1399
lines changed

bdd.nim

+7-6
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,21 @@ proc bdd*(caseName: string): (What, string, () -> bool) -> void =
5151
of should: itShould msg, caseName, condition()
5252
of shouldNot: itShouldNot msg, caseName, condition()
5353
of shouldRaise, shouldNotRaise:
54-
var didError = false
54+
var didRaise = false
5555
try: discard condition()
56-
except CatchableError, OSError: didError = true
56+
except CatchableError, OSError: didRaise = true
5757
finally:
58-
if what.ord == shouldRaise.ord: itShould msg, caseName, didError
59-
else: itShouldNot msg, caseName, didError
58+
if what.ord == shouldRaise.ord: itShould msg, caseName, didRaise
59+
else: itShouldNot msg, caseName, didRaise
6060
)
6161

6262

6363
when isMainModule:
64-
proc catchme: bool = raise newException(CatchableError, "if you can")
65-
let it = bdd "bdd tests"
64+
proc catchme: bool = raise TddError(msg: "if you can")
6665

66+
let it = bdd "bdd tests"
6767
it should, "be true", () => true
6868
it shouldNot, "be true", () => false
6969
it shouldRaise, "error", () => catchme()
7070
it shouldNotRaise, "error", () => true
71+
it shouldNotRaise, "or care about result", () => false

src/boat.dot

-62
Original file line numberDiff line numberDiff line change
@@ -26,66 +26,4 @@ digraph boat {
2626
"system/io" -> "std/private/since";
2727
"system/io" -> "system/formatfloat";
2828
"system" -> "system/io";
29-
"std/os" -> "std/private/since";
30-
"std/strutils" -> "std/parseutils";
31-
"std/math" -> "std/private/since";
32-
"std/macros" -> "std/private/since";
33-
"std/bitops" -> "std/macros";
34-
"std/bitops" -> "std/private/since";
35-
"std/bitops" -> "std/private/bitops_utils";
36-
"std/bitops" -> "system/countbits_impl";
37-
"std/math" -> "std/bitops";
38-
"std/math" -> "std/fenv";
39-
"std/strutils" -> "std/math";
40-
"std/algorithm" -> "std/private/since";
41-
"std/strutils" -> "std/algorithm";
42-
"std/enumutils" -> "std/macros";
43-
"std/typetraits" -> "std/private/since";
44-
"std/typetraits" -> "std/macros";
45-
"std/enumutils" -> "std/typetraits";
46-
"std/strutils" -> "std/enumutils";
47-
"std/strutils" -> "std/unicode";
48-
"std/strutils" -> "std/private/since";
49-
"std/strutils" -> "std/private/strimpl";
50-
"std/pathnorm" -> "std/osseps";
51-
"std/os" -> "std/strutils";
52-
"std/os" -> "std/pathnorm";
53-
"std/options" -> "std/typetraits";
54-
"std/times" -> "std/strutils";
55-
"std/times" -> "std/math";
56-
"std/times" -> "std/options";
57-
"std/times" -> "std/private/since";
58-
"std/times" -> "std/posix";
59-
"std/os" -> "std/posix";
60-
"std/os" -> "std/times";
61-
"boat/private/BoatConstants" -> "std/os";
62-
"boat/private/BoatConstants" -> "boat/private/BoatErrors";
63-
"std/sugar" -> "std/private/since";
64-
"std/sugar" -> "std/macros";
65-
"std/private/underscored_calls" -> "std/macros";
66-
"std/sugar" -> "std/private/underscored_calls";
67-
"../bdd" -> "std/sugar";
68-
"boat/private/Config" -> "../bdd";
69-
"std/streams" -> "std/private/since";
70-
"std/lexbase" -> "std/strutils";
71-
"std/lexbase" -> "std/streams";
72-
"std/tables" -> "std/private/since";
73-
"std/hashes" -> "std/private/since";
74-
"std/tables" -> "std/hashes";
75-
"std/tables" -> "std/math";
76-
"std/tables" -> "std/algorithm";
77-
"std/parsecfg" -> "std/strutils";
78-
"std/parsecfg" -> "std/lexbase";
79-
"std/parsecfg" -> "std/streams";
80-
"std/parsecfg" -> "std/tables";
81-
"std/parsecfg" -> "std/private/decode_helpers";
82-
"std/parsecfg" -> "std/private/since";
83-
"boat/private/Config" -> "std/os";
84-
"boat/private/Config" -> "std/parsecfg";
85-
"boat/private/Config" -> "std/strutils";
86-
"boat/private/Config" -> "boat/private/BoatErrors";
87-
"boat/private/Config" -> "boat/private/BoatConstants";
88-
"boat" -> "boat/private/BoatConstants";
89-
"boat" -> "boat/private/BoatErrors";
90-
"boat" -> "boat/private/Config";
9129
}

src/boat.nim

-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
## .. include:: ./readme.rst
22

3-
import boat/private/[
4-
boatConfig,
5-
boatConstants,
6-
boatErrors,
7-
]
8-
93
proc boat*: void = echo "All HANDS! cat o'nine tails! blue peter! OMG... landlubber"
104

115
when isMainModule:

src/boat.png

-370 KB
Loading

src/boat/private/boatConfig.nim

+21-37
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
## TLDR
88
- come back later
99
10-
todos
11-
-----
12-
- cmd to parse arbitrarily, e.g. boat conf load ./some/dir
13-
- cmd to see current captains: e.g. boat conf list -> X, Y, Z
1410
]##
1511

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

34-
proc parseLocalManifest*(self: BoatConfig, path: string = ""): bool =
35-
## parse self.use to self.parsed
36-
## prefer calling self.load or self.reload for validation
37-
let usePath = self.usePath path
38-
39-
# self.parsed = self.parsed.retrieve usePath
30+
proc parseManifest*(self: BoatConfig, path: string = "", ft: FileType): bool =
31+
## sets self.parsed to the parsed manifest
32+
self.parsed = ft.retrieve self.usePath path
4033
result = true
4134

42-
proc localManifestIsValid*(self: BoatConfig, path: string = ""): bool =
35+
proc manifestIsValid*(self: BoatConfig, path: string = ""): bool =
4336
## throws if manifest not found, cant be read, or errors during parsing
4437
let usePath = self.usePath path
4538
let pathInfo = usePath.getFileInfo
@@ -48,62 +41,53 @@ proc localManifestIsValid*(self: BoatConfig, path: string = ""): bool =
4841
of pcFile, pcLinkToFile:
4942
if fpUserRead notin pathInfo.permissions: raise filePermissionError
5043
elif not usePath.endsWith manifestName: raise manifestNameError
51-
elif not self.parseLocalManifest usePath: raise configParseError
44+
elif not self.parseManifest(usePath, localManifest): raise configParseError
5245
else: true
5346
of pcDir, pcLinkToDir:
5447
# force directories to use their manifest
5548
self.use = self.use / manifestName
56-
self.localManifestIsValid()
49+
self.manifestIsValid
5750

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

64-
proc init*(self: BoatConfig, path: string = ""): bool =
65-
let usePath = self.usePath path
57+
proc init*(self: BoatConfig): bool =
6658
# starts with https?
6759
# ends with manifestName?
68-
# save to boatConstants.tempDir / self.use
69-
# recurse self.reload path = temp location
60+
# check FileManagerUtils.retrieve
61+
# it should contain logic for loading remote manifests
7062
# throw: urls must point to a manifest.nim.ini
71-
case usePath.startsWith "https"
63+
case self.use.startsWith "https"
7264
of true: raise tddError
7365
else:
74-
try: doAssert self.localManifestIsValid(path) == true
66+
try: doAssert self.manifestIsValid == true
7567
except CatchableError:
7668
debugEcho repr getCurrentException()
7769
raise fileLoadError
78-
if not self.save usePath: raise fileSaveDefect
70+
if not self.save: raise fileSaveDefect
7971
else: result = true
8072

8173
proc reload*(self: BoatConfig): bool =
82-
## reloads a configuration from disk
83-
# (fpath, T) = FileMananger.fromDisk(...)
84-
# self.parsedPath = fpath, self.parsed = T
74+
## reloads a configuration from captainsLog
8575
raise tddError
8676

8777
proc load*(self: BoatConfig): bool =
88-
## (re)load a Configuration; safer than calling reload specifically
78+
## (re)load a Configuration
8979
result =
90-
if captainsLogLoaded and self.parsedPath.len is Positive: self.reload()
91-
else: self.init()
80+
# if self.use in captainsLog ? reload from captainslog
81+
if 1 > 2: raise tddError
82+
else: self.init
9283

9384
proc loadCaptainsLog(): void =
9485
## loads the previous or initializes a new captains log
95-
if not captainsLogLoaded: echo "loading captains log"
96-
# captainsLogLoaded = true
97-
# let (fpath, prevCaptainsLog) = captainsLog.fromDisk(cacheDir / manifestName, JsonNode, false)
98-
# captainsLog = prevCaptainsLog
86+
captainsLogLoaded = true
87+
# try to retrieve the prev captainslog from cachDir
88+
# else initialize an empty captainslog
9989

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

103-
104-
# consumers can retrieve the parsed Config and path on disk
105-
# but only internal functions should be able to set it
106-
export
107-
boatConfigType.BoatConfig,
108-
parsed,
109-
parsedPath
93+
export boatConfigType

src/boat/private/boatConfigType.nim

+4-15
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,10 @@ type BoatConfig*[T: BoatConfigKind = Config] = ref object of RootObj
66
## base type for all boat configs
77
use*: string ## \
88
## filepath, dir containing a file or remote uri
9-
10-
parsed: T ## \
11-
## the parsed config after loading
12-
parsedPath: string ## \
9+
parsed*: T ## \
10+
## the parsed config if the config has been parsed
11+
parsedPath*: string ## \
1312
## path on disk the parsed config was saved to
1413

15-
proc `parsed=`*(self: BoatConfig, path: string): void =
16-
self.parsed = self.parsed.retrieve path
17-
18-
proc `parsed`*(self: BoatConfig): BoatConfigKind = self.parsed
19-
20-
proc `parsedPath=`*(self: BoatConfig, path: string): void =
21-
self.parsedPath = path
22-
23-
proc `parsedPath`*(self: BoatConfig): string = self.parsedPath
24-
25-
proc usePath*(self: BoatConfig, path: string): string =
14+
proc usePath*(self: BoatConfig, path: string = ""): string =
2615
if path.len is Positive: path else: self.use

src/boat/private/boatConstants.nim

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ from parsecfg import Config
1010

1111
import boatErrors
1212

13-
type BoatConfigKind* = Config | JsonNode
13+
type BoatConfigKind* = Config | JsonNode ## \
14+
## a Config generally means a manifest
15+
## while JsonNode indicates a captainslog
1416

1517
var captainsLogLoaded* {.global.} = false ## \
1618
## true if we've loaded the captains log from disk into ram

src/boat/private/boatErrors.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ type BoatDefect = ref object of Defect ## \
2020
var dirCreateDefect* = BoatDefect(msg: "Dir Create Failed")
2121
var fileLoadDefect* = BoatDefect(msg: "Cant Load File")
2222
var fileSaveDefect* = BoatDefect(msg: "Cant Save File")
23-
var boatConfigKindError* = BoatDefect(msg: fmt"Expected BoatConfigKind")
23+
var boatConfigKindError* = BoatDefect(msg: fmt"Expected typeof BoatConfigKind")

src/boat/private/fileManager.nim

+11-15
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,25 @@
55

66
from ../../../bdd import tddError
77

8-
import std/[
9-
locks,
10-
threadpool,
11-
]
8+
# import std/[
9+
# locks,
10+
# threadpool,
11+
# ]
1212

1313
import
1414
boatConstants,
1515
boatErrors,
1616
fileManagerUtils
1717

18-
type SaveType* = enum
19-
parsedConfig,
20-
captainsLog,
21-
remoteManifest,
2218

23-
proc dir*(self: SaveType): string =
24-
## returns the directory where different SaveTypes are persisted
19+
proc dir*(self: FileType): string =
20+
## returns the directory where different FileTypes are persisted
2521
result = case self
26-
of parsedConfig, captainsLog: cacheDir
22+
of localManifest, captainsLog: cacheDir
2723
else: tempDir
2824

29-
proc path*(self: SaveType, fname: string): string =
30-
## computes the filpath for a SaveType
25+
proc path*(self: FileType, fname: string): string =
26+
## computes the filpath for a FileType
3127
raise tddError
3228
# result = self.dir / hash(fname)
3329

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

4036

4137
proc toDisk*[T: JsonNode | string | Config](
42-
self: SaveType,
38+
self: FileType,
4339
fname: string,
4440
data: T,
4541
captainsLog: JsonNode
@@ -63,7 +59,7 @@ proc toDisk*[T: JsonNode | string | Config](
6359

6460

6561
proc fromDisk*[T](
66-
self: SaveType,
62+
self: FileType,
6763
fname: string,
6864
to: T,
6965
errorNotFound = false

src/boat/private/fileManagerUtils.nim

+12-8
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,27 @@ import std/[
88

99
import boatErrors, boatConstants
1010

11+
type FileType* = enum
12+
captainsLog,
13+
localManifest,
14+
remoteManifest,
1115

12-
proc persist*[T: string | Config](self: T, path: string): Future[void] {.async.} =
13-
## writes strings to path
14-
## calls parsecfg.writeConfig for configs
16+
proc persist*[T: FileType](self: T, path: string): Future[void] {.async.} =
17+
## persists a FileType to path
1518
raise tddError
1619
# lock
1720
# of string -> data.write path
1821
# of Config -> parsecfg.writeConfig path
1922
# unlock
2023
# throw if any errors occur
2124

22-
proc retrieve*[T: BoatConfigKind](self: T, path: string): T =
23-
# retrieves a Config or Json from a path
25+
proc retrieve*[T: FileType](self: T, path: string): BoatConfigKind =
26+
## retrieves a FileType from path and parses to BoatConfigKind
2427
try:
25-
if self is Config: result = loadConfig path
26-
elif self is JsonNode: raise tddError # should load json from path
27-
else: raise boatConfigKindError
28+
result = case self
29+
of captainsLog: raise tddError # parse to json
30+
of localManifest: loadConfig path
31+
of remoteManifest: raise tddError # download, then loadConfig path
2832
except CatchableError:
2933
debugEcho repr getCurrentException()
3034
raise fileLoadDefect

0 commit comments

Comments
 (0)