|
@@ -70,108 +70,7 @@ M.idgen = function(len)
|
|
|
end
|
|
|
|
|
|
|
|
|
---[=[
|
|
|
-M.generate_sip_v1 = function(path)
|
|
|
- --[[
|
|
|
- Version 1 CSV parsing.
|
|
|
-
|
|
|
- Each row has 3 cells: reference path (ref), metadata key (k), value (v).
|
|
|
- Each v cell contains one value.
|
|
|
-
|
|
|
- Properties for the same ref can be added in successive rows without
|
|
|
- repeating the ref cell.
|
|
|
-
|
|
|
- Multiple values can be added in successive rows without repeating the ref
|
|
|
- and v cells.
|
|
|
- --]]
|
|
|
-
|
|
|
- local sub_data = assert(csv.open(path))
|
|
|
- local md = {}
|
|
|
- local prev_ref, prev_k
|
|
|
-
|
|
|
- -- Collate metadata.
|
|
|
- local i = 1
|
|
|
- for row in sub_data:lines() do
|
|
|
- ref, k, v = table.unpack(row)
|
|
|
- -- nil-out empty cells (they come through as "")
|
|
|
- if ref == "" then ref = nil end
|
|
|
- if k == "" then k = nil end
|
|
|
- if v == "" then v = nil end
|
|
|
-
|
|
|
- print("Parsing row:", ref, k, v)
|
|
|
- -- v can be a legit false value.
|
|
|
- if ref and not k and v == nil then
|
|
|
- -- This can be a placeholder for ordering purposes.
|
|
|
- md[ref] = md_ref or {}
|
|
|
- goto continue
|
|
|
- elseif v == nil then
|
|
|
- goto continue
|
|
|
- else
|
|
|
- -- If ref or k are missing, reuse the previous one.
|
|
|
- if ref then prev_ref = ref
|
|
|
- else
|
|
|
- if not prev_ref then
|
|
|
- -- If column 1 is empty, it must have been set in a
|
|
|
- -- previous row.
|
|
|
- error(string.format(
|
|
|
- "Reference in column 1, row %d not found!", i), 2)
|
|
|
- end
|
|
|
- ref = prev_ref
|
|
|
- end
|
|
|
-
|
|
|
- if k then prev_k = k
|
|
|
- else
|
|
|
- if not prev_k then
|
|
|
- -- If column 2 is empty, it must have been set in a
|
|
|
- -- previous row.
|
|
|
- error(string.format(
|
|
|
- "Property key in column 2, row %d not found!", i), 2)
|
|
|
- end
|
|
|
- k = prev_k
|
|
|
- end
|
|
|
- end
|
|
|
-
|
|
|
- md[ref] = md[ref] or {id = M.idgen(), path = ref, _sort = i}
|
|
|
- md[ref][k] = md[ref][k] or {}
|
|
|
- if k == "type" then
|
|
|
- md[ref][k] = v
|
|
|
- else
|
|
|
- table.insert(md[ref][k], v)
|
|
|
- end
|
|
|
-
|
|
|
- ::continue::
|
|
|
- i = i + 1
|
|
|
- end
|
|
|
-
|
|
|
- -- Move md to an ordered list.
|
|
|
- mdlist = {root_path = path:match("(.*/)")}
|
|
|
- for _, v in pairs(md) do table.insert(mdlist, v) end
|
|
|
- table.sort(mdlist, function (a, b) return (a._sort < b._sort) end)
|
|
|
-
|
|
|
- -- Infer structure from paths and row ordering.
|
|
|
- for i, v in ipairs(mdlist) do
|
|
|
- for j = i + 1, #mdlist do
|
|
|
- --print(string.format("comparing %s : %s", v["pas:sourcePath"], mdlist[j]["pas:sourcePath"]))
|
|
|
- if not v["next"] and
|
|
|
- mdlist[j]["pas:sourcePath"]:match("(.*/)") == v["pas:sourcePath"]:match("(.*/)") then
|
|
|
- --print("next match.")
|
|
|
- v["next"] = mdlist[j]["pas:sourcePath"]
|
|
|
- end
|
|
|
- if not v.first and
|
|
|
- mdlist[j]["pas:sourcePath"]:match("^" .. escape_pattern(v["pas:sourcePath"])) then
|
|
|
- --print("First child match.")
|
|
|
- v.first = mdlist[j]["pas:sourcePath"]
|
|
|
- end
|
|
|
- end
|
|
|
- v._sort = nil
|
|
|
- end
|
|
|
-
|
|
|
- return mdlist
|
|
|
-end
|
|
|
---]=]
|
|
|
-
|
|
|
-
|
|
|
-M.generate_sip_v2 = function(path)
|
|
|
+M.generate_sip = function(path)
|
|
|
local sub_data = assert(csv.open(path, {header = true}))
|
|
|
local sip = {root_path = path:match("(.*/)")}
|
|
|
|