pkar.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/lua
  2. local cli = require "cli"
  3. local plpath = require "pl.path"
  4. local nsm = require "volksdata.namespace"
  5. local term = require "volksdata.term"
  6. local triple = require "volksdata.triple"
  7. local graph = require "volksdata.graph"
  8. local pkar = require "pocket_archive"
  9. local sub = require "pocket_archive.submission"
  10. local repo = require "pocket_archive.repo"
  11. local gen = require "pocket_archive.generator"
  12. cli.locale "en_US" -- TODO set with multilingual support.
  13. init = cli.command {
  14. "Initialize a new Pocket Archive store.",
  15. cli.flag "wipe" {
  16. "Wipe all preexisting data.",
  17. type = cli.boolean,
  18. },
  19. function(args)
  20. if args.wipe then _ = pkar.reset_store
  21. else _ = pkar.store end
  22. end
  23. }
  24. list = cli.command {
  25. "List all resource IDs.",
  26. function()
  27. for _, s in pairs(repo.gr:unique_terms(triple.POS_S)) do
  28. print(nsm.denormalize_uri(s.data))
  29. end
  30. end,
  31. }
  32. deposit = cli.command {
  33. "Deposit a package.",
  34. cli.positional "path" {
  35. [[Path of the package root. It must be a directory containing
  36. all the files and folders to be submitted and a `pkar_submission.csv`
  37. file at the top of the folder.]]
  38. },
  39. function(args)
  40. sip = sub.generate_sip(plpath.join(args.path, "pkar_submission.csv"))
  41. sub.deposit(sip)
  42. end
  43. }
  44. gen_site = cli.command {
  45. "Generate a static site from the archive.",
  46. function(args) gen.generate_site() end
  47. }
  48. gen_rdf = cli.command {
  49. "Generate an RDF representation of a resource.",
  50. cli.positional "id" {
  51. "ID of the resource, prefixed by `par:`",
  52. type = cli.string,
  53. },
  54. cli.flag "f,format" {
  55. "RDF format. `nt` and `ttl` are available.",
  56. type = cli.string,
  57. default = "ttl",
  58. },
  59. cli.flag "o,output" {
  60. "Output file. If not specified, output to stdout.",
  61. type = cli.string,
  62. default = "",
  63. },
  64. function(args)
  65. local s = term.new_iriref_ns(args.id)
  66. local out = repo.serialize_rsrc(s, args.format)
  67. if args.output ~= "" then
  68. local fh = assert(io.open(args.output, "w"))
  69. fh:write(out)
  70. fh:close()
  71. print("File written to ", args.output)
  72. else print(out) end
  73. end,
  74. }
  75. gen_ll = cli.command {
  76. "Generate a laundry list for a stored resource.",
  77. cli.positional "id" {
  78. "ID of the resource, prefixed by `par:`",
  79. type = cli.string,
  80. },
  81. cli.flag "o,output" {
  82. "Output file. If not specified, output to stdout.",
  83. type = cli.string,
  84. default = "",
  85. },
  86. function(args)
  87. local s = term.new_iriref_ns(args.id)
  88. local out = gen.generate_ll(s)
  89. if args.output ~= "" then
  90. local fh = assert(io.open(args.output, "w"))
  91. fh:write(out)
  92. fh:close()
  93. print("File written to ", args.output)
  94. else print(out) end
  95. end
  96. }
  97. cli.program {
  98. "Pocket Archive command line interface.",
  99. }