Pino Toscano
2014-Mar-11 13:27 UTC
[Libguestfs] [PATCH 1/2] builder: move some language-related code into a Languages module
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
Pino Toscano
2014-Mar-11 13:27 UTC
[Libguestfs] [PATCH 2/2] builder: output translated notes also in --notes
---
builder/builder.ml | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/builder/builder.ml b/builder/builder.ml
index 1800f2d..274edfe 100644
--- a/builder/builder.ml
+++ b/builder/builder.ml
@@ -215,11 +215,12 @@ let main ()
(match mode with
| `Notes -> (* --notes *)
- (match entry with
- | { Index_parser.notes = ("", notes) :: _ } ->
- print_endline notes;
- | { Index_parser.notes = _ :: _ }
- | { Index_parser.notes = [] } ->
+ let notes + Languages.find_notes (Languages.languages ())
entry.Index_parser.notes in
+ (match notes with
+ | notes :: _ ->
+ print_endline notes
+ | [] ->
printf (f_"There are no notes for %s\n") arg
);
exit 0
--
1.8.3.1
Richard W.M. Jones
2014-Mar-11 13:39 UTC
Re: [Libguestfs] [PATCH 1/2] builder: move some language-related code into a Languages module
On Tue, Mar 11, 2014 at 02:27:08PM +0100, Pino Toscano wrote:> Mostly code motion, no behaviour changes.Yup, looks like a sensible reorganization of the code. ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Richard W.M. Jones
2014-Mar-11 13:40 UTC
Re: [Libguestfs] [PATCH 2/2] builder: output translated notes also in --notes
On Tue, Mar 11, 2014 at 02:27:09PM +0100, Pino Toscano wrote:> --- > builder/builder.ml | 11 ++++++----- > 1 file changed, 6 insertions(+), 5 deletions(-) > > diff --git a/builder/builder.ml b/builder/builder.ml > index 1800f2d..274edfe 100644 > --- a/builder/builder.ml > +++ b/builder/builder.ml > @@ -215,11 +215,12 @@ let main () > > (match mode with > | `Notes -> (* --notes *) > - (match entry with > - | { Index_parser.notes = ("", notes) :: _ } -> > - print_endline notes; > - | { Index_parser.notes = _ :: _ } > - | { Index_parser.notes = [] } -> > + let notes > + Languages.find_notes (Languages.languages ()) entry.Index_parser.notes inAs a matter of style you can write these two lines as: let notes let langs = Languages.languages () in Languages.find_notes langs entry.Index_parser.notes in> + (match notes with > + | notes :: _ -> > + print_endline notes > + | [] -> > printf (f_"There are no notes for %s\n") arg > ); > exit 0 > --ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming blog: http://rwmj.wordpress.com Fedora now supports 80 OCaml packages (the OPEN alternative to F#)
Seemingly Similar Threads
- [PATCH] builder: output translated notes
- Re: [PATCH] builder: output translated notes
- [PATCH] builder: support aliases for images (RHBZ#1098718).
- [PATCH] builder: read all the available notes from the index
- [PATCH 2/2] builder: Replace small usage of Str with new PCRE module.