Commit 588344d2 authored by Morten Welinder's avatar Morten Welinder

Introspection: better value __str__ function.

parent b860ec23
......@@ -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')
......
......@@ -1058,6 +1058,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
......
......@@ -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);
......
......@@ -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
......@@ -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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment