[Libguestfs] [PATCH 3/3] python: export hive_types constants

Peter Wu peter at lekensteyn.nl
Sat Aug 9 15:33:06 UTC 2014


Instead of using magic numbers or copying the numbers all over place,
make the hive type constants available so you can do:

    import hivex
    from hivex.hive_types import *

    h = hivex.Hivex ("../images/minimal", write = True)
    new_value = { "key": "abc", "t": REG_BINARY, "value": b"xyz" }
    h.node_set_value (h.root (), new_value)
---
 generator/generator.ml | 18 ++++++++++++++++++
 python/Makefile.am     |  3 +++
 2 files changed, 21 insertions(+)

diff --git a/generator/generator.ml b/generator/generator.ml
index 466efcb..349f1d1 100755
--- a/generator/generator.ml
+++ b/generator/generator.ml
@@ -3410,6 +3410,23 @@ class Hivex(object):
       )
   ) functions
 
+and generate_python_hive_types_py () =
+  generate_header HashStyle LGPLv2plus;
+
+  pr "\
+\"\"\"Define integer constants for hive type
+
+The names correspond with the hive_type enum type of the C API, but without
+'hive_t_' prefix.
+\"\"\"
+
+";
+  List.iter (
+    fun (t, _, new_style, description) ->
+      pr "# %s\n" description;
+      pr "REG_%s = %d\n" new_style t
+  ) hive_types;
+
 and generate_ruby_c () =
   generate_header CStyle LGPLv2plus;
 
@@ -3856,6 +3873,7 @@ Run it from the top source directory using the command
 
   (try Unix.mkdir "python/hivex" 0o755 with Unix_error _ -> ());
   output_to "python/hivex/__init__.py" generate_python_py;
+  output_to "python/hivex/hive_types.py" generate_python_hive_types_py;
   output_to "python/hivex-py.c" generate_python_c;
 
   output_to "ruby/ext/hivex/_hivex.c" generate_ruby_c;
diff --git a/python/Makefile.am b/python/Makefile.am
index 6bb8ade..1117ab9 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -21,6 +21,7 @@ builddir ?= $(top_builddir)/python
 EXTRA_DIST = \
 	run-python-tests \
 	hivex/__init__.py \
+	hivex/hive_types.py \
 	hivex-py.c \
 	t/*.py
 
@@ -30,6 +31,7 @@ pythondir = $(PYTHON_INSTALLDIR)
 pythonmoddir = $(PYTHON_INSTALLDIR)/hivex
 
 pythonmod_DATA = \
+	hivex/hive_types.py \
 	hivex/__init__.py
 
 python_LTLIBRARIES = libhivexmod.la
@@ -48,6 +50,7 @@ TESTS = run-python-tests
 CLEANFILES = \
 	hivex.py \
 	hivex.pyc \
+	hivex/hive_types.pyc \
 	hivex/__init__.pyc
 
 # For Python 3
-- 
2.0.2




More information about the Libguestfs mailing list