[Libguestfs] [PATCH 1/2] builder: move some language-related code into a Languages module

Pino Toscano ptoscano at redhat.com
Tue Mar 11 13:27:08 UTC 2014


Mostly code motion, no behaviour changes.
---
 builder/Makefile.am     |  3 +++
 builder/languages.ml    | 57 +++++++++++++++++++++++++++++++++++++++++++++++++
 builder/languages.mli   | 21 ++++++++++++++++++
 builder/list_entries.ml | 36 ++-----------------------------
 4 files changed, 83 insertions(+), 34 deletions(-)
 create mode 100644 builder/languages.ml
 create mode 100644 builder/languages.mli

diff --git a/builder/Makefile.am b/builder/Makefile.am
index 387913c..110b146 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -49,6 +49,8 @@ SOURCES = \
 	index-parser-c.c \
 	ini_reader.mli \
 	ini_reader.ml \
+	languages.mli \
+	languages.ml \
 	list_entries.mli \
 	list_entries.ml \
 	paths.ml \
@@ -101,6 +103,7 @@ deps = \
 	setlocale.cmx \
 	ini_reader.cmx \
 	paths.cmx \
+	languages.cmx \
 	get_kernel.cmx \
 	downloader.cmx \
 	sigchecker.cmx \
diff --git a/builder/languages.ml b/builder/languages.ml
new file mode 100644
index 0000000..876b23a
--- /dev/null
+++ b/builder/languages.ml
@@ -0,0 +1,57 @@
+(* virt-builder
+ * Copyright (C) 2013-2014 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+open Common_utils
+
+let split_locale loc =
+  let regex = Str.regexp "^\\([A-Za-z]+\\)\\(_\\([A-Za-z]+\\)\\)?\\(\\.\\([A-Za-z0-9-]+\\)\\)?\\(@\\([A-Za-z]+\\)\\)?$" in
+  let l = ref [] in
+  if Str.string_match regex loc 0 then (
+    let match_or_empty n =
+      try Str.matched_group n loc with
+      | Not_found -> ""
+    in
+    let lang = Str.matched_group 1 loc in
+    let territory = match_or_empty 3 in
+    (match territory with
+    | "" -> ()
+    | territory -> l := (lang ^ "_" ^ territory) :: !l);
+    l := lang :: !l;
+  );
+  l := "" :: !l;
+  List.rev !l
+
+let languages () =
+  match Setlocale.setlocale Setlocale.LC_MESSAGES None with
+  | None -> [""]
+  | Some locale -> split_locale locale
+
+let find_notes languages notes =
+  let notes = List.fold_left (
+    fun acc lang ->
+      let res = List.filter (
+        fun (langkey, _) ->
+          match langkey with
+          | "C" -> lang = ""
+          | langkey -> langkey = lang
+      ) notes in
+      match res with
+      | (_, noteskey) :: _ -> noteskey :: acc
+      | [] -> acc
+  ) [] languages in
+  List.rev notes
diff --git a/builder/languages.mli b/builder/languages.mli
new file mode 100644
index 0000000..f096e9a
--- /dev/null
+++ b/builder/languages.mli
@@ -0,0 +1,21 @@
+(* virt-builder
+ * Copyright (C) 2014 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+val languages : unit -> string list
+
+val find_notes : string list -> (string * string) list -> string list
diff --git a/builder/list_entries.ml b/builder/list_entries.ml
index 476bf14..623040a 100644
--- a/builder/list_entries.ml
+++ b/builder/list_entries.ml
@@ -21,24 +21,6 @@ open Common_utils
 
 open Printf
 
-let split_locale loc =
-  let regex = Str.regexp "^\\([A-Za-z]+\\)\\(_\\([A-Za-z]+\\)\\)?\\(\\.\\([A-Za-z0-9-]+\\)\\)?\\(@\\([A-Za-z]+\\)\\)?$" in
-  let l = ref [] in
-  if Str.string_match regex loc 0 then (
-    let match_or_empty n =
-      try Str.matched_group n loc with
-      | Not_found -> ""
-    in
-    let lang = Str.matched_group 1 loc in
-    let territory = match_or_empty 3 in
-    (match territory with
-    | "" -> ()
-    | territory -> l := (lang ^ "_" ^ territory) :: !l);
-    l := lang :: !l;
-  );
-  l := "" :: !l;
-  List.rev !l
-
 let rec list_entries ~list_format ~sources index =
   match list_format with
   | `Short -> list_entries_short index
@@ -60,9 +42,7 @@ and list_entries_short index =
   ) index
 
 and list_entries_long ~sources index =
-  let langs = match Setlocale.setlocale Setlocale.LC_MESSAGES None with
-  | None -> [""]
-  | Some locale -> split_locale locale in
+  let langs = Languages.languages () in
 
   List.iter (
     fun (source, key) ->
@@ -97,19 +77,7 @@ and list_entries_long ~sources index =
         | Some size ->
           printf "%-24s %s\n" (s_"Download size:") (human_size size);
         );
-        let notes = List.fold_left (
-          fun acc lang ->
-            let res = List.filter (
-              fun (langkey, _) ->
-                match langkey with
-                | "C" -> lang = ""
-                | langkey -> langkey = lang
-            ) notes in
-            match res with
-            | (_, noteskey) :: _ -> noteskey :: acc
-            | [] -> acc
-        ) [] langs in
-        let notes = List.rev notes in
+        let notes = Languages.find_notes langs notes in
         (match notes with
         | notes :: _ ->
           printf "\n";
-- 
1.8.3.1




More information about the Libguestfs mailing list