Advertisement
Guest User

Untitled

a guest
Jul 25th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. christiaan@alucard:~/devel/clash-compiler$ cat /nix/store/sg57qn9zn81h413nmmbkr5zjf3d1x4hy-source/parse-git-config.nix
  2. # Basic git INI-like file format parser
  3. #
  4. # Probably not feature complete anytime soon...
  5. #
  6. # Notable omissions:
  7. # - multiline values (if supported??)
  8. # - proper subsections
  9. # - includes
  10. # - conditional includes
  11. # - keys with embedded whitespace
  12. #
  13. # Low hanging fruit:
  14. # - group by section if you need to query the file often
  15. #
  16. # Unknowns:
  17. # - whitespace before section header?
  18. # - what if no section is specified before first item?
  19. #
  20. { lib ? import <nixpkgs/lib>, ... }:
  21. let
  22. inherit (lib.strings) splitString hasPrefix removePrefix removeSuffix hasInfix replaceStrings;
  23. inherit (lib.lists) foldl' head tail;
  24.  
  25. parseIniText = text:
  26. let
  27. rawLines = splitString "\n" text;
  28. folded = foldl' step zero rawLines;
  29. zero = { section = "";
  30. items = [];
  31. };
  32. step = r@{ section, items }: line:
  33. if hasPrefix "[" line
  34. then r // {
  35. section = removePrefix "[" (removeSuffix "]" line);
  36. }
  37. else if hasInfix "=" line then
  38. let
  39. s = splitString "=" line;
  40. s0 = head s;
  41. key = replaceStrings [" " "\t"] ["" ""] s0;
  42. v = removePrefix "${s0}=" line;
  43. value = lstrip v;
  44. in
  45. r // {
  46. items = items ++ [{ inherit section key value; }];
  47. }
  48. else
  49. r
  50. ;
  51. in
  52. folded.items
  53. ;
  54. lstrip = s: if hasPrefix " " s then lstrip (removePrefix " " s)
  55. else if hasPrefix "\t" s then lstrip (removePrefix "\t" s)
  56. else s;
  57. parseIniFile = p:
  58. builtins.addErrorContext ("while parsing INI file " + toString p) (
  59. parseIniText (builtins.readFile p)
  60. )
  61. ;
  62. in {
  63. inherit parseIniText parseIniFile;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement