[gnumeric] Introspection: better value __str__ function.



commit 588344d280c6410d05f32300050aef15ae48cae0
Author: Morten Welinder <terra gnome org>
Date:   Fri Apr 27 11:47:35 2018 -0400

    Introspection: better value __str__ function.

 introspection/gi/overrides/Gnm.py     |   12 +----
 src/value.c                           |   67 +++++++++++++++++++++++++++++++++
 src/value.h                           |    1 +
 test/t3004-introspection-overrides.pl |   18 ++++++++-
 test/t3004-introspection-overrides.py |   27 ++++++++++++-
 5 files changed, 112 insertions(+), 13 deletions(-)
---
diff --git a/introspection/gi/overrides/Gnm.py b/introspection/gi/overrides/Gnm.py
index 926d1a5..7e6c0b6 100644
--- a/introspection/gi/overrides/Gnm.py
+++ b/introspection/gi/overrides/Gnm.py
@@ -18,8 +18,7 @@ class Range(Gnm.Range):
     def __init__(cls,*argv):
         pass
 
-    def __str__(self):
-        return self.as_string()
+    __str__ = Gnm.Range.as_string
     
 Range = override(Range)
 __all__.append('Range')
@@ -27,17 +26,12 @@ __all__.append('Range')
 # ----------------------------------------------------------------------------
 
 def _valuetype_str(vt):
-    return vt.value_name
+    return vt.value_name[6:]
 
 Gnm.ValueType.__str__ = _valuetype_str
 
 class Value(Gnm.Value):
-    def __str__(self):
-        fmt = self.v_any.fmt
-        main = self.type_of().value_name + "," + self.get_as_string()
-        if fmt is not None:
-            main += "," + str(fmt)
-        return "{" + main + "}";
+    __repr__ = Gnm.Value.stringify
 
 Value = override(Value)
 __all__.append('Value')
diff --git a/src/value.c b/src/value.c
index b2c6e2e..21358e0 100644
--- a/src/value.c
+++ b/src/value.c
@@ -1059,6 +1059,73 @@ value_peek_string (GnmValue const *v)
 }
 
 /**
+ * value_stringify:
+ * @v: a #GnmValue
+ *
+ * Returns: (transfer full): A string representation of the value suitable
+ * for use in a Python __repr__ function.
+ */
+char *
+value_stringify (GnmValue const *v)
+{
+       GString *res = g_string_sized_new (30);
+
+       g_string_append_c (res, '{');
+
+       switch (v->v_any.type) {
+       case VALUE_EMPTY:
+               g_string_append (res, "EMPTY,");
+               g_string_append (res, "None");
+               break;
+
+       case VALUE_STRING:
+               g_string_append (res, "STRING,");
+               go_strescape (res, value_peek_string (v));
+               break;
+
+       case VALUE_CELLRANGE:
+               g_string_append (res, "CELLRANGE,");
+               g_string_append (res, value_peek_string (v));
+               return 0;
+
+       case VALUE_ARRAY:
+               g_string_append (res, "ARRAY,");
+               g_string_append (res, value_peek_string (v));
+               break;
+
+       case VALUE_FLOAT:
+               g_string_append (res, "FLOAT,");
+               g_string_append (res, value_peek_string (v));
+               break;
+
+       case VALUE_BOOLEAN:
+               g_string_append (res, "BOOLEAN,");
+               g_string_append_c (res, v->v_bool.val ? '1' : '0');
+               break;
+
+       case VALUE_ERROR:
+               g_string_append (res, "ERROR,");
+               go_strescape (res, value_peek_string (v));
+               break;
+
+       default:
+               g_string_append (res, "?,?");
+               break;
+       }
+
+       if (VALUE_FMT (v) != NULL) {
+               g_string_append_c (res, ',');
+               go_strescape (res, go_format_as_XL (VALUE_FMT (v)));
+       }
+
+       g_string_append_c (res, '}');
+
+       return g_string_free (res, FALSE);
+}
+
+
+
+/**
  * value_get_as_int:
  * @v: (nullable): a #GnmValue
  *
diff --git a/src/value.h b/src/value.h
index 1475572..344ed4a 100644
--- a/src/value.h
+++ b/src/value.h
@@ -149,6 +149,7 @@ char const *value_peek_string          (GnmValue const *v);
 char       *value_get_as_string           (GnmValue const *v);
 void        value_get_as_gstring   (GnmValue const *v, GString *target,
                                    GnmConventions const *conv);
+char       *value_stringify        (GnmValue const *v);
 
 GnmValueType value_type_of         (GnmValue const *v);
 int         value_get_as_int      (GnmValue const *v);
diff --git a/test/t3004-introspection-overrides.pl b/test/t3004-introspection-overrides.pl
index fdaf1de..ccb6e2b 100755
--- a/test/t3004-introspection-overrides.pl
+++ b/test/t3004-introspection-overrides.pl
@@ -9,5 +9,21 @@ use GnumericTest;
 
 my $python_script = $0;
 $python_script =~ s/\.pl$/.py/;
+my $ref = join("",<DATA>);
 &test_command ($PYTHON . ' ' . &GnumericTest::quotearg ($python_script),
-              sub { /^Using in-tree / });
+              sub { $_ eq $ref });
+
+__DATA__
+Using in-tree gi.overrides.Gnm
+
+Testing GnmValue overrides:
+{EMPTY,None}
+{BOOLEAN,0}
+{BOOLEAN,1}
+{FLOAT,12}
+{FLOAT,12.5}
+{STRING,"howdy"}
+{FLOAT,12.5,"0.00"}
+
+Testing GnmRange overrides:
+B3:D5
diff --git a/test/t3004-introspection-overrides.py b/test/t3004-introspection-overrides.py
index 477d69d..eb2eeee 100755
--- a/test/t3004-introspection-overrides.py
+++ b/test/t3004-introspection-overrides.py
@@ -4,9 +4,10 @@
 import gi
 gi.require_version('Gnm', '1.12') 
 from gi.repository import Gnm
+from gi.repository import GOffice as Go
 Gnm.init()
 
-import os.path;
+import os.path
 
 def atomize_path(p):
     res=[]
@@ -18,7 +19,7 @@ def atomize_path(p):
             break
         if h == p:
             res.append(h)
-            break;
+            break
         p = h
     res.reverse()
     return res
@@ -30,4 +31,24 @@ else:
     print("Using installed gi.overrides.Gnm at {}"
           .format (gi.overrides.Gnm.__file__))
 
-print Gnm.Value.new_int(12)
+# -----------------------------------------------------------------------------
+
+print("\nTesting GnmValue overrides:")
+# __str__
+print(Gnm.Value.new_empty())
+print(Gnm.Value.new_bool(0))
+print(Gnm.Value.new_bool(1))
+print(Gnm.Value.new_int(12))
+print(Gnm.Value.new_float(12.5))
+print(Gnm.Value.new_string("howdy"))
+v=Gnm.Value.new_float(12.5)
+v.set_fmt(Go.Format.new_from_XL("0.00"))
+print(v)
+
+# -----------------------------------------------------------------------------
+
+print("\nTesting GnmRange overrides:")
+# __new__
+r=Gnm.Range(1,2,3,4)
+# __str__
+print(r)


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