summaryrefslogtreecommitdiff
path: root/src/plugins/python
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2012-11-02 09:37:15 +0100
committerSebastien Helleu <flashcode@flashtux.org>2012-11-02 09:37:15 +0100
commit32c93b5c0a3abd2525677e0e2615abde8460d693 (patch)
treebb7377366ff48325d31cc2c3caecc1be3c88400f /src/plugins/python
parent3ec0ad7c62279af13fcc6ee3ef78b11dcc3be382 (diff)
downloadweechat-32c93b5c0a3abd2525677e0e2615abde8460d693.zip
core: add command /eval, use expression in conditions for bars, add function "string_eval_expression" in plugin API
Diffstat (limited to 'src/plugins/python')
-rw-r--r--src/plugins/python/weechat-python-api.c58
-rw-r--r--src/plugins/python/weechat-python.c28
-rw-r--r--src/plugins/python/weechat-python.h4
3 files changed, 76 insertions, 14 deletions
diff --git a/src/plugins/python/weechat-python-api.c b/src/plugins/python/weechat-python-api.c
index cc8f2a4e1..f450f556b 100644
--- a/src/plugins/python/weechat-python-api.c
+++ b/src/plugins/python/weechat-python-api.c
@@ -444,6 +444,43 @@ weechat_python_api_string_input_for_buffer (PyObject *self, PyObject *args)
}
/*
+ * weechat_python_api_string_eval_expression: evaluate expression and return
+ * result
+ */
+
+static PyObject *
+weechat_python_api_string_eval_expression (PyObject *self, PyObject *args)
+{
+ char *expr, *result;
+ struct t_hashtable *pointers, *extra_vars;
+ PyObject *dict, *dict2, *return_value;
+
+ API_FUNC(1, "string_eval_expression", API_RETURN_EMPTY);
+ expr = NULL;
+ pointers = NULL;
+ extra_vars = NULL;
+ if (!PyArg_ParseTuple (args, "sOO", &expr, &dict, &dict2))
+ API_WRONG_ARGS(API_RETURN_EMPTY);
+ pointers = weechat_python_dict_to_hashtable (dict,
+ WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_POINTER);
+ extra_vars = weechat_python_dict_to_hashtable (dict2,
+ WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING);
+
+ result = weechat_string_eval_expression (expr, pointers, extra_vars);
+
+ if (pointers)
+ weechat_hashtable_free (pointers);
+ if (extra_vars)
+ weechat_hashtable_free (extra_vars);
+
+ API_RETURN_STRING_FREE(result);
+}
+
+/*
* weechat_python_api_mkdir_home: create a directory in WeeChat home
*/
@@ -2081,7 +2118,9 @@ weechat_python_api_key_bind (PyObject *self, PyObject *args)
API_WRONG_ARGS(API_RETURN_INT(0));
hashtable = weechat_python_dict_to_hashtable (dict,
- WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
+ WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING);
num_keys = weechat_key_bind (context, hashtable);
@@ -2652,7 +2691,9 @@ weechat_python_api_hook_process_hashtable (PyObject *self, PyObject *args)
&data))
API_WRONG_ARGS(API_RETURN_EMPTY);
options = weechat_python_dict_to_hashtable (dict,
- WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
+ WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING);
result = API_PTR2STR(plugin_script_api_hook_process_hashtable (weechat_python_plugin,
python_current_script,
@@ -3077,7 +3118,9 @@ weechat_python_api_hook_hsignal_send (PyObject *self, PyObject *args)
API_WRONG_ARGS(API_RETURN_ERROR);
hashtable = weechat_python_dict_to_hashtable (dict,
- WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
+ WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING);
weechat_hook_hsignal_send (signal, hashtable);
@@ -4840,7 +4883,9 @@ weechat_python_api_info_get_hashtable (PyObject *self, PyObject *args)
if (!PyArg_ParseTuple (args, "sO", &info_name, &dict))
API_WRONG_ARGS(API_RETURN_EMPTY);
hashtable = weechat_python_dict_to_hashtable (dict,
- WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
+ WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING);
result_hashtable = weechat_info_get_hashtable (info_name, hashtable);
result_dict = weechat_python_hashtable_to_dict (result_hashtable);
@@ -5613,7 +5658,9 @@ weechat_python_api_hdata_update (PyObject *self, PyObject *args)
if (!PyArg_ParseTuple (args, "ssO", &hdata, &pointer, &dict))
API_WRONG_ARGS(API_RETURN_INT(0));
hashtable = weechat_python_dict_to_hashtable (dict,
- WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
+ WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING);
value = weechat_hdata_update (API_STR2PTR(hdata),
API_STR2PTR(pointer),
@@ -5807,6 +5854,7 @@ PyMethodDef weechat_python_funcs[] =
API_DEF_FUNC(string_remove_color),
API_DEF_FUNC(string_is_command_char),
API_DEF_FUNC(string_input_for_buffer),
+ API_DEF_FUNC(string_eval_expression),
API_DEF_FUNC(mkdir_home),
API_DEF_FUNC(mkdir),
API_DEF_FUNC(mkdir_parents),
diff --git a/src/plugins/python/weechat-python.c b/src/plugins/python/weechat-python.c
index 2eba42de4..d1a6d4185 100644
--- a/src/plugins/python/weechat-python.c
+++ b/src/plugins/python/weechat-python.c
@@ -232,23 +232,23 @@ weechat_python_hashtable_to_dict (struct t_hashtable *hashtable)
/*
* weechat_python_dict_to_hashtable: get WeeChat hashtable with python
* dictionary
- * Hashtable returned has type string for
- * both keys and values
* Note: hashtable has to be released after
* use with call to weechat_hashtable_free()
*/
struct t_hashtable *
-weechat_python_dict_to_hashtable (PyObject *dict, int hashtable_size)
+weechat_python_dict_to_hashtable (PyObject *dict, int size,
+ const char *type_keys,
+ const char *type_values)
{
struct t_hashtable *hashtable;
PyObject *key, *value;
Py_ssize_t pos;
char *str_key, *str_value;
- hashtable = weechat_hashtable_new (hashtable_size,
- WEECHAT_HASHTABLE_STRING,
- WEECHAT_HASHTABLE_STRING,
+ hashtable = weechat_hashtable_new (size,
+ type_keys,
+ type_values,
NULL,
NULL);
if (!hashtable)
@@ -275,7 +275,17 @@ weechat_python_dict_to_hashtable (PyObject *dict, int hashtable_size)
str_value = weechat_python_unicode_to_string (value);
if (str_key)
- weechat_hashtable_set (hashtable, str_key, str_value);
+ {
+ if (strcmp (type_values, WEECHAT_HASHTABLE_STRING) == 0)
+ weechat_hashtable_set (hashtable, str_key, str_value);
+ else if (strcmp (type_values, WEECHAT_HASHTABLE_POINTER) == 0)
+ {
+ weechat_hashtable_set (hashtable, str_key,
+ plugin_script_str2ptr (weechat_python_plugin,
+ NULL, NULL,
+ str_value));
+ }
+ }
if (str_key)
free (str_key);
@@ -388,7 +398,9 @@ weechat_python_exec (struct t_plugin_script *script,
else if (ret_type == WEECHAT_SCRIPT_EXEC_HASHTABLE)
{
ret_value = weechat_python_dict_to_hashtable (rc,
- WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE);
+ WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
+ WEECHAT_HASHTABLE_STRING,
+ WEECHAT_HASHTABLE_STRING);
Py_XDECREF(rc);
}
else
diff --git a/src/plugins/python/weechat-python.h b/src/plugins/python/weechat-python.h
index 1a522603a..0a1aa2fe5 100644
--- a/src/plugins/python/weechat-python.h
+++ b/src/plugins/python/weechat-python.h
@@ -45,7 +45,9 @@ extern const char *python_current_script_filename;
extern PyObject *weechat_python_hashtable_to_dict (struct t_hashtable *hashtable);
extern struct t_hashtable *weechat_python_dict_to_hashtable (PyObject *dict,
- int hashtable_size);
+ int size,
+ const char *type_keys,
+ const char *type_values);
extern void *weechat_python_exec (struct t_plugin_script *script,
int ret_type, const char *function,
char *format, void **argv);