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
b547ca71
Commit
b547ca71
authored
May 19, 2015
by
Vincent Castellano
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add custom exceptions
parent
7f2fc476
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
43 deletions
+54
-43
python-tg.c
python-tg.c
+18
-1
python-types.c
python-types.c
+25
-19
tg-test.py
tg-test.py
+11
-23
No files found.
python-tg.c
View file @
b547ca71
...
...
@@ -80,7 +80,6 @@
extern
PyTypeObject
tgl_PeerType
;
extern
PyTypeObject
tgl_MsgType
;
//#include "interface.h"
//#include "auto/constants.h"
#include <tgl/tgl.h>
...
...
@@ -93,6 +92,12 @@ extern struct tgl_state *TLS;
static
int
python_loaded
;
// TGL Python Exceptions
PyObject
*
TglError
;
PyObject
*
PeerError
;
PyObject
*
MsgError
;
// Python update function callables
PyObject
*
_py_binlog_end
;
PyObject
*
_py_diff_end
;
...
...
@@ -1182,6 +1187,18 @@ MOD_INIT(tgl)
Py_INCREF
(
&
tgl_MsgType
);
PyModule_AddObject
(
m
,
"Msg"
,
(
PyObject
*
)
&
tgl_MsgType
);
TglError
=
PyErr_NewException
(
"tgl.Error"
,
NULL
,
NULL
);
Py_INCREF
(
TglError
);
PyModule_AddObject
(
m
,
"TglError"
,
TglError
);
PeerError
=
PyErr_NewException
(
"tgl.PeerError"
,
NULL
,
NULL
);
Py_INCREF
(
PeerError
);
PyModule_AddObject
(
m
,
"PeerError"
,
PeerError
);
MsgError
=
PyErr_NewException
(
"tgl.MsgError"
,
NULL
,
NULL
);
Py_INCREF
(
MsgError
);
PyModule_AddObject
(
m
,
"MsgError"
,
MsgError
);
return
MOD_SUCCESS_VAL
(
m
);
}
...
...
python-types.c
View file @
b547ca71
...
...
@@ -15,6 +15,11 @@
extern
struct
tgl_state
*
TLS
;
// TGL Python Exceptions
extern
PyObject
*
TglError
;
extern
PyObject
*
PeerError
;
extern
PyObject
*
MsgError
;
//
// tgl_peer_t wrapper
//
...
...
@@ -42,7 +47,7 @@ tgl_Peer_init(tgl_Peer *self, PyObject *args, PyObject *kwds)
&
peer_id
.
type
,
&
peer_id
.
id
))
{
PyErr_Format
(
P
yErr_NewException
(
"tgl.PeerInvalid"
,
NULL
,
NULL
)
,
"Peer must specify type and id"
);
PyErr_Format
(
P
eerError
,
"Peer must specify type and id"
);
return
-
1
;
}
self
->
peer
=
tgl_peer_get
(
TLS
,
peer_id
);
...
...
@@ -68,7 +73,7 @@ tgl_Peer_getname (tgl_Peer *self, void *closure)
ret
=
PyUnicode_FromString
(
self
->
peer
->
encr_chat
.
print_name
);
break
;
default:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name not supported!"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name not supported!"
);
Py_RETURN_NONE
;
}
...
...
@@ -87,7 +92,7 @@ tgl_Peer_getuser_id (tgl_Peer *self, void *closure)
ret
=
PyLong_FromLong
(
self
->
peer
->
id
.
id
);
break
;
case
TGL_PEER_CHAT
:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name == 'chat' has no user_id"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name == 'chat' has no user_id"
);
Py_RETURN_NONE
;
break
;
...
...
@@ -95,7 +100,7 @@ tgl_Peer_getuser_id (tgl_Peer *self, void *closure)
ret
=
PyLong_FromLong
(
self
->
peer
->
encr_chat
.
user_id
);
break
;
default:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name not supported!"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name not supported!"
);
Py_RETURN_NONE
;
}
...
...
@@ -122,11 +127,11 @@ tgl_Peer_getuser_list (tgl_Peer *self, void *closure)
break
;
case
TGL_PEER_ENCR_CHAT
:
case
TGL_PEER_USER
:
PyErr_SetString
(
P
yExc_Type
Error
,
"Only peer.type_name == 'chat' has user_list"
);
PyErr_SetString
(
P
eer
Error
,
"Only peer.type_name == 'chat' has user_list"
);
Py_RETURN_NONE
;
break
;
default:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name not supported!"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name not supported!"
);
Py_RETURN_NONE
;
}
...
...
@@ -149,11 +154,11 @@ tgl_Peer_getuser_status(tgl_Peer *self, void *closure)
break
;
case
TGL_PEER_CHAT
:
case
TGL_PEER_ENCR_CHAT
:
PyErr_SetString
(
P
yExc_Type
Error
,
"Only peer.type_name == 'user' has user_status"
);
PyErr_SetString
(
P
eer
Error
,
"Only peer.type_name == 'user' has user_status"
);
Py_RETURN_NONE
;
break
;
default:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name not supported!"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name not supported!"
);
Py_RETURN_NONE
;
}
...
...
@@ -172,11 +177,11 @@ tgl_Peer_getphone (tgl_Peer *self, void *closure)
break
;
case
TGL_PEER_CHAT
:
case
TGL_PEER_ENCR_CHAT
:
PyErr_SetString
(
P
yExc_Type
Error
,
"Only peer.type_name == 'user' has phone"
);
PyErr_SetString
(
P
eer
Error
,
"Only peer.type_name == 'user' has phone"
);
Py_RETURN_NONE
;
break
;
default:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name not supported!"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name not supported!"
);
Py_RETURN_NONE
;
}
...
...
@@ -195,11 +200,11 @@ tgl_Peer_getusername (tgl_Peer *self, void *closure)
break
;
case
TGL_PEER_CHAT
:
case
TGL_PEER_ENCR_CHAT
:
PyErr_SetString
(
P
yExc_Type
Error
,
"Only peer.type_name == 'user' has username"
);
PyErr_SetString
(
P
eer
Error
,
"Only peer.type_name == 'user' has username"
);
Py_RETURN_NONE
;
break
;
default:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name not supported!"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name not supported!"
);
Py_RETURN_NONE
;
}
...
...
@@ -218,11 +223,11 @@ tgl_Peer_getfirst_name (tgl_Peer *self, void *closure)
break
;
case
TGL_PEER_CHAT
:
case
TGL_PEER_ENCR_CHAT
:
PyErr_SetString
(
P
yExc_Type
Error
,
"Only peer.type_name == 'user' has first_name"
);
PyErr_SetString
(
P
eer
Error
,
"Only peer.type_name == 'user' has first_name"
);
Py_RETURN_NONE
;
break
;
default:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name not supported!"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name not supported!"
);
Py_RETURN_NONE
;
}
...
...
@@ -241,11 +246,11 @@ tgl_Peer_getlast_name (tgl_Peer *self, void *closure)
break
;
case
TGL_PEER_CHAT
:
case
TGL_PEER_ENCR_CHAT
:
PyErr_SetString
(
P
yExc_Type
Error
,
"Only peer.type_name == 'user' has last_name"
);
PyErr_SetString
(
P
eer
Error
,
"Only peer.type_name == 'user' has last_name"
);
Py_RETURN_NONE
;
break
;
default:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name not supported!"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name not supported!"
);
Py_RETURN_NONE
;
}
...
...
@@ -266,11 +271,11 @@ tgl_Peer_getuser (tgl_Peer *self, void *closure)
ret
=
(
PyObject
*
)
self
;
break
;
case
TGL_PEER_CHAT
:
PyErr_SetString
(
P
yExc_Type
Error
,
"Only peer.type_name == 'chat' does not have user"
);
PyErr_SetString
(
P
eer
Error
,
"Only peer.type_name == 'chat' does not have user"
);
Py_RETURN_NONE
;
break
;
default:
PyErr_SetString
(
P
yExc_Type
Error
,
"peer.type_name not supported!"
);
PyErr_SetString
(
P
eer
Error
,
"peer.type_name not supported!"
);
Py_RETURN_NONE
;
}
...
...
@@ -448,7 +453,8 @@ tgl_Msg_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static
int
tgl_Msg_init
(
tgl_Msg
*
self
,
PyObject
*
args
,
PyObject
*
kwds
)
{
return
0
;
PyErr_SetString
(
MsgError
,
"You cannot instantiate a tgl.Msg object, only the API can send them."
);
return
-
1
;
}
static
PyObject
*
...
...
tg-test.py
View file @
b547ca71
...
...
@@ -24,45 +24,33 @@ def msg_cb(success, msg):
HISTORY_QUERY_SIZE
=
100
def
history_cb
(
msg_list
,
p
type
,
pid
,
success
,
msgs
):
def
history_cb
(
msg_list
,
p
eer
,
success
,
msgs
):
print
(
len
(
msgs
))
msg_list
.
extend
(
msgs
)
print
(
len
(
msg_list
))
if
len
(
msgs
)
==
HISTORY_QUERY_SIZE
:
tgl
.
get_history
(
p
type
,
pid
,
len
(
msg_list
),
HISTORY_QUERY_SIZE
,
partial
(
history_cb
,
msg_list
,
ptype
,
pid
));
tgl
.
get_history
(
p
eer
,
len
(
msg_list
),
HISTORY_QUERY_SIZE
,
partial
(
history_cb
,
msg_list
,
peer
));
def
on_msg_receive
(
msg
):
if
msg
.
out
and
not
binlog_done
:
return
;
tgl
.
send_msg
(
tgl
.
Peer
(
97704886
),
"Test"
)
print
(
"Peers {0}"
.
format
(
msg
.
src
.
id
))
"""
def on_msg_receive(msg):
if msg["out"] and not binlog_done:
return
;
if msg["to"]["id"] == our_id: # direct message
ptype = msg["from"]["type"]
pid = msg["from"]["id"]
if
msg
.
dest
.
id
==
our_id
:
# direct message
peer
=
msg
.
src
else
:
# chatroom
ptype = msg["to"]["type"]
pid = msg["to"]["id"]
pp.pprint(msg)
peer
=
msg
.
dest
text = msg["text"]
if text.startswith("!ping"):
if
msg
.
text
.
startswith
(
"!ping"
):
print
(
"SENDING PONG"
)
tgl.send_msg(p
type, pid
, "PONG!", msg_cb)
tgl
.
send_msg
(
p
eer
,
"PONG!"
,
msg_cb
)
if text.startswith("!loadhistory"):
if
msg
.
text
.
startswith
(
"!loadhistory"
):
msg_list
=
[]
tgl.get_history_ext(ptype, pid, 0, HISTORY_QUERY_SIZE, partial(history_cb, msg_list, ptype, pid));
"""
tgl
.
get_history
(
peer
,
0
,
HISTORY_QUERY_SIZE
,
partial
(
history_cb
,
msg_list
,
peer
));
def
on_secret_chat_update
(
peer
,
types
):
return
"on_secret_chat_update"
...
...
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