[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [augeas-devel] More generic constructions in util.aug





2009/3/28 David Lutterkort <lutter redhat com>
On Fri, 2009-03-27 at 22:51 +0100, Raphaël Pinson wrote:
> As shared with David on IRC, I think we should go even more towards
> sharing generic constructions in lenses to make writing lenses easier,
> and adopt generic ways of implementing common patterns.
>
> Here are some propositions already, taken from monit.aug and
> exports.aug and how these two lenses can be simplified by using the
> newly defined types :

I like that a lot, and it's a much needed cleanup. As we discussed on
IRC, we should split Util into several modules, like Rx for commonly
used regexps etc.

I also want to get 0.5.0 out before we do this, simply since these
library additions should not change too much once they've been released,
and I feel we should be a little careful in figuring out what to factor
where.


Here is a first shot, applied to the previously changed files :


diff --git a/lenses/exports.aug b/lenses/exports.aug
index 499a3af..856e365 100644
--- a/lenses/exports.aug
+++ b/lenses/exports.aug
@@ -78,19 +78,20 @@ module Exports =
 
   let client_re = /[a-zA-Z0-9\-\ \*\?\/]+/
 
-  let eol = del /[ \t]*\n/ "\n"
+  let eol = Util.eol
+  let lbracket  = Util.del_str "("
+  let rbracket  = Util.del_str ")"
+  let sep_com   = Sep.comma
+  let sep_spc   = Sep.space
  
   let option = [ label "option" . store /[^,)]+/ ]
 
-  let client = [ label "client" . store client_re .
-                    ( Util.del_str "(" .
-                        option .
-                        ( Util.del_str "," . option ) * .
-                      Util.del_str ")" )? ]
+  let client    = [ label "client" . store client_re .
+                    ( Build.brackets lbracket rbracket
+                         ( Build.list option sep_com ) )? ]
 
-  let entry = [ label "dir" . store /\/[^ \t]*/ .
-                Util.del_ws_spc .
-                client . (Util.del_ws_spc . client)* . eol ]
+  let entry = [ label "dir" . store /\/[^ \t]*/
+                . sep_spc . Build.list client sep_spc . eol ]
 
   let lns = (Hosts.empty | Hosts.comment | entry)*
 
diff --git a/lenses/fstab.aug b/lenses/fstab.aug
index 2246026..0a990db 100644
--- a/lenses/fstab.aug
+++ b/lenses/fstab.aug
@@ -3,17 +3,20 @@
 module Fstab =
   autoload xfm
 
-  let sep_tab = Util.del_ws_tab
-  let sep_spc = Util.del_ws_spc
-  let comma = Util.del_str ","
-  let eol = del /[ \t]*\n/ "\n"
+  let sep_tab = Sep.tab
+  let sep_spc = Sep.space
+  let comma   = Sep.comma
+  let eol     = Util.eol
 
   let comment = Util.comment
   let empty   = Util.empty
 
-  let word = /[^,# \n\t]+/
+  let word    = Rx.word
+
   let comma_sep_list (l:string) =
-    [ label l . store word ] . ([comma . label l . store word])*
+    let lns = [ label l . store word ] in
+       Build.list lns comma
+
   let record = [ seq "mntent" .
                    [ label "spec" . store  word ] . sep_tab .
                    [ label "file" . store word ] . sep_tab .
diff --git a/lenses/group.aug b/lenses/group.aug
index 26bfc61..288c2e2 100644
--- a/lenses/group.aug
+++ b/lenses/group.aug
@@ -17,25 +17,24 @@ let eol        = Util.eol
 let comment    = Util.comment
 let empty      = Util.empty
 
-let colon      = del ":" ":"
-let comma      = del "," ","
+let colon      = Sep.colon
+let comma      = Sep.comma
 
-let sto_to_eol = store /([^ \t\n].*[^ \t\n]|[^ \t\n])/
+let sto_to_spc = store Rx.space_in
 
-let word       = /[A-Za-z0-9_.-]+/
-let integer    = /[0-9]+/
+let word    = Rx.word
+let integer = Rx.integer
 
 (************************************************************************
  *                               ENTRIES
  *************************************************************************)
 
-let user      = [ label "user"     . store word ]
-let entry     = [ key word
-                . colon
-                . [ label "password" . store word    . colon ]
+let user      = [ label "user" . store word ]
+let user_list = Build.list user comma
+let params    = [ label "password" . store word    . colon ]
                 . [ label "gid"      . store integer . colon ]
-                . (user . (comma . user)*)?
-                . eol ]
+                . user_list?
+let entry     = Build.key_value_line word colon params
 
 (************************************************************************
  *                                LENS
diff --git a/lenses/monit.aug b/lenses/monit.aug
index bd4e63e..dde4573 100644
--- a/lenses/monit.aug
+++ b/lenses/monit.aug
@@ -33,29 +33,28 @@ module Monit =
  *                           USEFUL PRIMITIVES
  *************************************************************************)
 
-let eol        = Util.eol
-let spc        = Util.del_ws_spc
+let spc        = Sep.space
 let comment    = Util.comment
 let empty      = Util.empty
 
-let sto_to_eol = store /([^ \t\n].*[^ \t\n]|[^ \t\n])/
-let sto_to_spc = store /[^ \t\n]+/
+let sto_to_spc = store Rx.space_in
+let sto_no_spc = store Rx.no_spaces
 
-let word       = /[A-Za-z0-9_.-]+/
-let value      = [ key word . spc . sto_to_eol . eol ]
+let word       = Rx.word
+let value      = Build.key_value_line word spc sto_to_spc
 
 (************************************************************************
  *                               ENTRIES
  *************************************************************************)
 
 (* set statement *)
-let set        = [ key "set" . spc . value  ]
+let set        = Build.key_value "set" spc value
 
 (* include statement *)
-let include    = [ key "include" . spc . sto_to_eol . eol ]
+let include    = Build.key_value_line "include" spc sto_to_spc
 
 (* service statement *)
-let service    = [ key "check" . spc . value . (spc . value)+ ]
+let service    = Build.key_value "check" spc (Build.list value spc)
 
 let entry      = (set|include|service)
 


Raphaël



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]