summary refs log tree commit diff
path: root/pkgs/lib
diff options
context:
space:
mode:
authorMarc Weber <marco-oweber@gmx.de>2008-02-13 10:09:29 +0000
committerMarc Weber <marco-oweber@gmx.de>2008-02-13 10:09:29 +0000
commit9c211b094e6800631018a308c8eb251308a265f7 (patch)
treec6ab3f75182283795cf1b501e7dfc3c43f125fc1 /pkgs/lib
parent74adfd62395b4dde3ad04ab17ec6c30d205c5dfb (diff)
added whatis, traceWhatis, subsetmap, escapeShellArg, stringToCharacters, defineShList
stringToCharacters is only used by escapeShellArg and should be implemented as primop

svn path=/nixpkgs/trunk/; revision=10655
Diffstat (limited to 'pkgs/lib')
-rw-r--r--pkgs/lib/default.nix34
1 files changed, 33 insertions, 1 deletions
diff --git a/pkgs/lib/default.nix b/pkgs/lib/default.nix
index 2674c883888b9..a549120b67669 100644
--- a/pkgs/lib/default.nix
+++ b/pkgs/lib/default.nix
@@ -32,6 +32,7 @@ rec {
   pairMap = innerPairMap [];
 
   
+  
   # "Fold" a binary function `op' between successive elements of
   # `list' with `nul' as the starting value, i.e., `fold op nul [x_1
   # x_2 ... x_n] == op x_1 (op x_2 ... (op x_n nul))'.  (This is
@@ -91,6 +92,10 @@ rec {
       then getAttr (tail attrPath) default (builtins.getAttr attr e)
       else default;
 
+  # shortcut for getAttr ["name"] default attrs
+  maybeAttr = name: default: attrs:
+    if (__hasAttr name attrs) then (__getAttr name attrs) else default;
+
 
   # Filter a list using a predicate; that is, return a list containing
   # every element from `list' for which `pred' returns true.
@@ -317,6 +322,18 @@ rec {
   debugVal = if builtins ? trace then x: (builtins.trace x x) else x: x;
   debugXMLVal = if builtins ? trace then x: (builtins.trace (builtins.toXML x) x) else x: x;
 
+  # this can help debug your code as well - designed to not produce thousands of lines
+  traceWhatis = x : __trace (whatis x) x;
+  whatis = x : 
+      if (__isAttrs x) then
+          if (x ? outPath) then "x is a derivation with name ${x.name}"
+          else "x is an attr set with attributes ${builtins.toString (__attrNames x)}"
+      else if (__isFunction x) then "x is a function"
+      else if (__isList x) then "x is a list, first item is : ${whatis (__head x)}"
+      else if (x == true || x == false) then builtins.toString x
+      else "x is propably a string starting, starting characters: ${__substring 0 50 x}..";
+
+
   innerClosePropagation = ready: list: if list == [] then ready else
     if (head list) ? propagatedBuildInputs then 
       innerClosePropagation (ready ++ [(head list)]) 
@@ -325,11 +342,20 @@ rec {
 
   closePropagation = list: (uniqList {inputList = (innerClosePropagation [] list);});
 
+  stringToCharacters = s : let l = __stringLength s; in
+    if (__lessThan l 1) then [""] else  [(__substring 0 1 s)] ++ stringToCharacters (__substring 1 (__sub l 1) s);
+
+  # should this be implemented as primop ? Yes it should..
+  escapeShellArg = s :
+    let escapeChar = x : if ( x == "'" ) then "'\"'\"'" else x;
+    in "'" + concatStrings (map escapeChar (stringToCharacters s) ) +"'";
+
+  defineShList = name : list : "\n${name}=(${concatStringsSep " " (map escapeShellArg list)})\n";
+
   # calls a function (f attr value ) for each record item. returns a list
   mapRecordFlatten = f : r : map (attr: f attr (builtins.getAttr attr r) ) (attrNames r);
 
   # to be used with listToAttrs (_a_ttribute _v_alue)
-  # TODO should be renamed to nv because niksnut has renamed the attribute attr to name
   nv = name : value : { inherit name value; };
   # attribute set containing one attribute
   nvs = name : value : listToAttrs [ (nv name value) ];
@@ -358,6 +384,12 @@ rec {
   flattenAttrs = set : map ( attr : builtins.getAttr attr set) (attrNames set);
   mapIf = cond : f :  fold ( x : l : if (cond x) then [(f x)] ++ l else l) [];
 
+  # pick attrs subset_attr_names and apply f 
+  subsetmap = f : attrs : subset_attr_names : 
+    listToAttrs (fold ( attr : r : if __hasAttr attr attrs
+          then r ++ [ (  nv attr ( f (__getAttr attr attrs) ) ) ] else r ) []
+      subset_attr_names );
+
 # Marc 2nd proposal: (not everything has been tested in detail yet..)
 
   # usage / example