Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
tg
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
tg
Commits
5994d34e
Commit
5994d34e
authored
May 03, 2015
by
Vincent Castellano
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement calling from python into C. Need to implement command queue to execute during netloop.
parent
3a13f800
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
127 additions
and
104 deletions
+127
-104
pytest.py
pytest.py
+10
-2
python-tg.c
python-tg.c
+117
-102
No files found.
pytest.py
View file @
5994d34e
import
tgl
import
pprint
our_id
=
0
pp
=
pprint
.
PrettyPrinter
(
indent
=
4
)
def
on_binlog_replay_end
():
pass
def
on_get_difference_end
():
pass
def
on_our_id
():
pass
def
on_our_id
(
id
):
our_id
=
id
return
"Set ID: "
+
str
(
our_id
)
def
on_msg_receive
(
msg
):
pp
.
pprint
(
msg
)
tgl
.
send_msg
(
msg
[
"from"
][
"type"
],
msg
[
"from"
][
"id"
],
"PONG!"
)
return
"Got msg from "
+
msg
[
"from"
][
"peer"
][
"first_name"
]
def
on_secret_chat_update
(
peer
,
types
):
...
...
python-tg.c
View file @
5994d34e
...
...
@@ -57,19 +57,6 @@ PyObject *_py_secret_chat_update;
PyObject
*
_py_user_update
;
PyObject
*
_py_chat_update
;
// Python callback callables
PyObject
*
_py_empty_cb
;
PyObject
*
_py_contact_list_cb
;
PyObject
*
_py_dialog_list_cb
;
PyObject
*
_py_msg_cb
;
PyObject
*
_py_msg_list_cb
;
PyObject
*
_py_file_cb
;
PyObject
*
_py_chat_cb
;
PyObject
*
_py_secret_chat_cb
;
PyObject
*
_py_user_cb
;
PyObject
*
_py_str_cb
;
PyObject
*
get_user
(
tgl_peer_t
*
P
);
PyObject
*
get_peer
(
tgl_peer_id_t
id
,
tgl_peer_t
*
P
);
...
...
@@ -238,7 +225,9 @@ PyObject* get_peer (tgl_peer_id_t id, tgl_peer_t *P) {
if
(
peer
==
NULL
)
assert
(
0
);
// TODO handle python exception;
PyDict_SetItemString
(
peer
,
"type"
,
get_tgl_peer_type
(
tgl_get_peer_type
(
id
)));
PyDict_SetItemString
(
peer
,
"type_str"
,
get_tgl_peer_type
(
tgl_get_peer_type
(
id
)));
PyDict_SetItemString
(
peer
,
"type"
,
PyInt_FromLong
(
tgl_get_peer_type
(
id
)));
PyDict_SetItemString
(
peer
,
"id"
,
PyInt_FromLong
(
tgl_get_peer_id
(
id
)));
if
(
!
P
||
!
(
P
->
flags
&
FLAG_CREATED
))
{
PyObject
*
name
;
...
...
@@ -372,47 +361,44 @@ PyObject* get_message (struct tgl_message *M) {
return
msg
;
}
//void lua_binlog_end (void) {
// if (!have_file) { return; }
// lua_settop (luaState, 0);
// //lua_checkstack (luaState, 20);
// my_lua_checkstack (luaState, 20);
// lua_getglobal (luaState, "on_binlog_replay_end");
// assert (lua_gettop (luaState) == 1);
//
// int r = lua_pcall (luaState, 0, 0, 0);
// if (r) {
// logprintf ("lua: %s\n", lua_tostring (luaState, -1));
// }
//}
//
//void lua_diff_end (void) {
// if (!have_file) { return; }
// lua_settop (luaState, 0);
// //lua_checkstack (luaState, 20);
// my_lua_checkstack (luaState, 20);
// lua_getglobal (luaState, "on_get_difference_end");
// assert (lua_gettop (luaState) == 1);
//
// int r = lua_pcall (luaState, 0, 0, 0);
// if (r) {
// logprintf ("lua: %s\n", lua_tostring (luaState, -1));
// }
//}
//
void
py_binlog_end
(
void
)
{
if
(
!
have_file
)
{
return
;
}
PyObject
*
arglist
,
*
result
;
arglist
=
Py_BuildValue
(
"()"
);
result
=
PyEval_CallObject
(
_py_binlog_end
,
arglist
);
Py_DECREF
(
arglist
);
if
(
result
==
NULL
)
PyErr_Print
();
else
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
}
void
py_diff_end
(
void
)
{
if
(
!
have_file
)
{
return
;
}
PyObject
*
arglist
,
*
result
;
arglist
=
Py_BuildValue
(
"()"
);
result
=
PyEval_CallObject
(
_py_diff_end
,
arglist
);
Py_DECREF
(
arglist
);
if
(
result
==
NULL
)
PyErr_Print
();
else
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
}
void
py_our_id
(
int
id
)
{
// if (!have_file) { return; }
// lua_settop (luaState, 0);
// //lua_checkstack (luaState, 20);
// my_lua_checkstack (luaState, 20);
// lua_getglobal (luaState, "on_our_id");
// lua_pushnumber (luaState, id);
// assert (lua_gettop (luaState) == 2);
//
// int r = lua_pcall (luaState, 1, 0, 0);
// if (r) {
// logprintf ("lua: %s\n", lua_tostring (luaState, -1));
// }
if
(
!
have_file
)
{
return
;
}
PyObject
*
arglist
,
*
result
;
arglist
=
Py_BuildValue
(
"(i)"
,
id
);
result
=
PyEval_CallObject
(
_py_our_id
,
arglist
);
Py_DECREF
(
arglist
);
if
(
result
==
NULL
)
PyErr_Print
();
else
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
}
void
py_new_msg
(
struct
tgl_message
*
M
)
{
...
...
@@ -426,7 +412,9 @@ void py_new_msg (struct tgl_message *M) {
result
=
PyEval_CallObject
(
_py_new_msg
,
arglist
);
Py_DECREF
(
arglist
);
assert
(
result
&&
PyString_Check
(
result
));
// TODO handle python exception
if
(
result
==
NULL
)
PyErr_Print
();
else
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
}
...
...
@@ -442,43 +430,50 @@ void py_secret_chat_update (struct tgl_secret_chat *C, unsigned flags) {
result
=
PyEval_CallObject
(
_py_secret_chat_update
,
arglist
);
Py_DECREF
(
arglist
);
assert
(
result
&&
PyString_Check
(
result
));
// TODO handle python exception
if
(
result
==
NULL
)
PyErr_Print
();
else
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
}
void
py_user_update
(
struct
tgl_user
*
U
,
unsigned
flags
)
{
// if (!have_file) { return; }
// lua_settop (luaState, 0);
// //lua_checkstack (luaState, 20);
// my_lua_checkstack (luaState, 20);
// lua_getglobal (luaState, "on_user_update");
// push_peer (U->id, (void *)U);
// push_update_types (flags);
// assert (lua_gettop (luaState) == 3);
//
// int r = lua_pcall (luaState, 2, 0, 0);
// if (r) {
// logprintf ("lua: %s\n", lua_tostring (luaState, -1));
// }
if
(
!
have_file
)
{
return
;
}
PyObject
*
peer
,
*
types
;
PyObject
*
arglist
,
*
result
;
peer
=
get_peer
(
U
->
id
,
(
void
*
)
U
);
types
=
get_update_types
(
flags
);
arglist
=
Py_BuildValue
(
"(OO)"
,
peer
,
types
);
result
=
PyEval_CallObject
(
_py_user_update
,
arglist
);
Py_DECREF
(
arglist
);
if
(
result
==
NULL
)
PyErr_Print
();
else
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
}
void
py_chat_update
(
struct
tgl_chat
*
C
,
unsigned
flags
)
{
// if (!have_file) { return; }
// lua_settop (luaState, 0);
// //lua_checkstack (luaState, 20);
// my_lua_checkstack (luaState, 20);
// lua_getglobal (luaState, "on_chat_update");
// push_peer (C->id, (void *)C);
// push_update_types (flags);
// assert (lua_gettop (luaState) == 3);
//
// int r = lua_pcall (luaState, 2, 0, 0);
// if (r) {
// logprintf ("lua: %s\n", lua_tostring (luaState, -1));
// }
if
(
!
have_file
)
{
return
;
}
PyObject
*
peer
,
*
types
;
PyObject
*
arglist
,
*
result
;
peer
=
get_peer
(
C
->
id
,
(
void
*
)
C
);
types
=
get_update_types
(
flags
);
arglist
=
Py_BuildValue
(
"(OO)"
,
peer
,
types
);
result
=
PyEval_CallObject
(
_py_chat_update
,
arglist
);
Py_DECREF
(
arglist
);
if
(
result
==
NULL
)
PyErr_Print
();
else
logprintf
(
"python: %s
\n
"
,
PyString_AsString
(
result
));
}
//
////extern tgl_peer_t *Peers[];
////extern int peer_num;
//
...
...
@@ -537,12 +532,12 @@ void py_chat_update (struct tgl_chat *C, unsigned flags) {
// lq_extf
//};
//
//struct
lua
_query_extra {
//struct
py
_query_extra {
// int func;
// int param;
//};
//
//void
lua
_empty_cb (struct tgl_state *TLSR, void *cb_extra, int success) {
//void
py
_empty_cb (struct tgl_state *TLSR, void *cb_extra, int success) {
// assert (TLSR == TLS);
// struct lua_query_extra *cb = cb_extra;
// lua_settop (luaState, 0);
...
...
@@ -660,8 +655,8 @@ void py_chat_update (struct tgl_chat *C, unsigned flags) {
// free (cb);
//}
//
//void lua
_msg_cb (struct tgl_state *TLSR, void *cb_extra, int success, struct tgl_message *M) {
//
assert (TLSR == TLS);
void
py
_msg_cb
(
struct
tgl_state
*
TLSR
,
void
*
cb_extra
,
int
success
,
struct
tgl_message
*
M
)
{
assert
(
TLSR
==
TLS
);
// struct lua_query_extra *cb = cb_extra;
// lua_settop (luaState, 0);
// //lua_checkstack (luaState, 20);
...
...
@@ -690,8 +685,8 @@ void py_chat_update (struct tgl_chat *C, unsigned flags) {
// }
//
// free (cb);
//
}
//
}
//void lua_msg_list_cb (struct tgl_state *TLSR, void *cb_extra, int success, int num, struct tgl_message *M[]) {
// assert (TLSR == TLS);
// struct lua_query_extra *cb = cb_extra;
...
...
@@ -1166,13 +1161,27 @@ void py_chat_update (struct tgl_chat *C, unsigned flags) {
// enum lua_function_param params[10];
//};
//
//struct lua_function functions[] = {
// {"get_contact_list", lq_contact_list, { lfp_none }},
// {"get_dialog_list", lq_dialog_list, { lfp_none }},
// {"rename_chat", lq_rename_chat, { lfp_chat, lfp_string, lfp_none }},
// {"send_msg", lq_msg, { lfp_peer, lfp_string, lfp_none }},
// {"send_typing", lq_send_typing, { lfp_peer, lfp_none }},
// {"send_typing_abort", lq_send_typing_abort, { lfp_peer, lfp_none }},
PyObject
*
py_send_msg
(
PyObject
*
self
,
PyObject
*
args
)
{
const
char
*
msg
;
tgl_peer_id_t
id
;
if
(
!
PyArg_ParseTuple
(
args
,
"iis"
,
&
id
.
type
,
&
id
.
id
,
&
msg
))
return
NULL
;
tgl_do_send_message
(
TLS
,
(
tgl_peer_id_t
)
id
,
msg
,
strlen
(
msg
),
py_msg_cb
,
args
);
return
PyString_FromString
(
"sent!"
);
}
static
PyMethodDef
py_tgl_methods
[]
=
{
// {"get_contact_list", lq_contact_list, METH_VARARGS},
// {"get_dialog_list", lq_dialog_list, METH_VARARGS},
// {"rename_chat", lq_rename_chat, METH_VARARGS},
{
"send_msg"
,
py_send_msg
,
METH_VARARGS
,
"send message to user or chat"
},
// {"send_typing", lq_send_typing, METH_VARARGS},
// {"send_typing_abort", lq_send_typing_abort, METH_VARARGS},
// {"send_photo", lq_send_photo, { lfp_peer, lfp_string, lfp_none }},
// {"send_video", lq_send_video, { lfp_peer, lfp_string, lfp_none }},
// {"send_audio", lq_send_audio, { lfp_peer, lfp_string, lfp_none }},
...
...
@@ -1211,9 +1220,9 @@ void py_chat_update (struct tgl_chat *C, unsigned flags) {
// {"status_offline", lq_status_offline, { lfp_none }},
// {"send_location", lq_send_location, { lfp_peer, lfp_double, lfp_double, lfp_none }},
// {"ext_function", lq_extf, { lfp_string, lfp_none }},
// { 0, 0, { lfp_none}
}
//
};
//
{
NULL
,
NULL
,
0
,
NULL
}
};
//static int parse_lua_function (lua_State *L, struct lua_function *F) {
// int p = 0;
// while (F->params[p] != lfp_none) { p ++; }
...
...
@@ -1466,7 +1475,10 @@ void py_chat_update (struct tgl_chat *C, unsigned flags) {
f = PyDict_GetItemString(dict, name);
void
inittgl
()
{
(
void
)
Py_InitModule
(
"tgl"
,
py_tgl_methods
);
}
void
py_init
(
const
char
*
file
)
{
if
(
!
file
)
{
return
;
}
...
...
@@ -1475,6 +1487,8 @@ void py_init (const char *file) {
PyObject
*
pName
,
*
pModule
,
*
pDict
;
Py_Initialize
();
inittgl
();
PyObject
*
sysPath
=
PySys_GetObject
((
char
*
)
"path"
);
PyList_Append
(
sysPath
,
PyString_FromString
(
"."
));
...
...
@@ -1484,6 +1498,7 @@ void py_init (const char *file) {
pModule
=
PyImport_Import
(
pName
);
pDict
=
PyModule_GetDict
(
pModule
);
// Store callables for python functions
my_python_register
(
pDict
,
"on_binlog_replay_end"
,
_py_binlog_end
);
my_python_register
(
pDict
,
"on_get_difference_end"
,
_py_diff_end
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment