1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
|
/*
* This file is part of Python FAM.
* Copyright (C) 2002 Robert Sander <robert.sander@epigenomics.com>
* Copyright (C) 2005 Martin v. Loewis
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: _fam.c,v 1.14 2005/04/10 07:48:45 loewis Exp $
*
*/
#include <Python.h>
#include <structmember.h>
#include <fam.h>
#include "_fam.h"
#define PY22 0x020200F0
// #define _DEBUG 1
#define add_int(d, name) \
{ \
PyObject *i = PyInt_FromLong(FAM##name); \
PyDict_SetItemString( d, #name, i ); \
Py_DECREF(i); \
}
char* code2str(int code) {
char* rv;
switch (code) {
case FAMExists:
rv = "exists";
break;
case FAMEndExist:
rv = "endExist";
break;
case FAMChanged:
rv = "changed";
break;
case FAMDeleted:
rv = "deleted";
break;
case FAMStartExecuting:
rv = "started executing";
break;
case FAMStopExecuting:
rv = "stopped executing";
break;
case FAMCreated:
rv = "created";
break;
case FAMMoved:
rv = "moved";
break;
case FAMAcknowledge:
rv = "acknowleged";
break;
default:
rv = NULL;
}
return rv;
}
/*
* FAMConnection object definitions
*
* The Connection object is returned by the open() method of the module.
* It implements FAMClose(), FAMMonitorDirectory(), FAMMonitorFile(),
* FAMNextEvent() and FAMPending().
*
*/
typedef struct {
PyObject_HEAD
FAMConnection *fc;
} _fam_connection_object;
static PyMethodDef _fam_connection_methods[] = {
{ "close", _fam_close, 1, _fam_close__doc__},
{ "monitorDirectory", _fam_monitor_directory, 1, _fam_monitor_directory__doc__},
{ "monitorFile", _fam_monitor_file, 1, _fam_monitor_file__doc__},
{ "nextEvent", _fam_next_event, 1, _fam_next_event__doc__},
{ "pending", _fam_pending, 1, _fam_pending__doc__},
{ "fileno", _fam_fileno, 1, _fam_fileno__doc__},
{ NULL, NULL, 0, NULL}
};
#if PY_VERSION_HEX < PY22
static PyObject* _fam_connection_getattr(PyObject* self, char* name) {
#ifdef _DEBUG
fprintf(stderr, "_fam: getting attribute %s from connection object.\n", name);
#endif
return Py_FindMethod(_fam_connection_methods, self, name);
}
#endif
void _fam_connection_del(PyObject* self) {
#ifdef _DEBUG
fprintf(stderr, "_fam: deleting connection object.\n");
#endif
_fam_close(self, NULL);
PyMem_DEL(self);
}
static PyTypeObject _fam_connection_type = {
#if defined(WIN32) || defined(__CYGWIN__)
PyObject_HEAD_INIT(NULL)
#else
PyObject_HEAD_INIT(&PyType_Type)
#endif
0, /* ob_size */
"_fam.FAMConnection", /* tp_name */
sizeof(_fam_connection_object), /* tp_basicsize */
0, /* tp_itemsize */
_fam_connection_del, /* tp_dealloc */
0, /* tp_print */
#if PY_VERSION_HEX < PY22
_fam_connection_getattr, /* tp_getattr */
#else
0, /* tp_getattr */
#endif
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* call */
0, /* str */
#if PY_VERSION_HEX >= PY22
PyObject_GenericGetAttr, /* tp_getattro */
#else
0, /* tp_getattro */
#endif
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
_fam_connection_type__doc__, /* doc string */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
#if PY_VERSION_HEX >= PY22
0, /* tp_iter */
0, /* tp_iternext */
_fam_connection_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
#endif
};
/*
* FAMRequest object definitions
*
* A Request object is returned by one of the two monitor methods.
* It implements FAMSuspendMonitor(), FAMResumeMonitor() and
* FAMCancelMonitor()
*
*/
typedef struct {
PyObject_HEAD
_fam_connection_object *fc_obj;
FAMRequest *fr;
} _fam_request_object;
static PyMethodDef _fam_request_methods[] = {
{ "suspendMonitor", _fam_suspend_monitor, 1, _fam_suspend_monitor__doc__},
{ "resumeMonitor", _fam_resume_monitor, 1, _fam_resume_monitor__doc__},
{ "cancelMonitor", _fam_cancel_monitor, 1, _fam_cancel_monitor__doc__},
{ "requestID", _fam_request_id, 1, _fam_request_id__doc__},
{ NULL, NULL, 0, NULL}
};
#if PY_VERSION_HEX < PY22
static PyObject* _fam_request_getattr(PyObject* self, char* name) {
#ifdef _DEBUG
fprintf(stderr, "_fam: getting attribute %s from request object.\n", name);
#endif
return Py_FindMethod(_fam_request_methods, self, name);
}
#endif
void _fam_request_del(PyObject* self) {
#ifdef _DEBUG
fprintf(stderr, "_fam: deleting request object.\n");
#endif
_fam_cancel_monitor(self, NULL);
PyMem_DEL(self);
}
static PyTypeObject _fam_request_type = {
#if defined(WIN32) || defined(__CYGWIN__)
PyObject_HEAD_INIT(NULL)
#else
PyObject_HEAD_INIT(&PyType_Type)
#endif
0, /* ob_size */
"_fam.FAMRequest", /* tp_name */
sizeof(_fam_request_object), /* tp_basicsize */
0, /* tp_itemsize */
_fam_request_del, /* tp_dealloc */
0, /* tp_print */
#if PY_VERSION_HEX < PY22
_fam_request_getattr, /* tp_getattr */
#else
0, /* tp_getattr */
#endif
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* call */
0, /* str */
#if PY_VERSION_HEX >= PY22
PyObject_GenericGetAttr, /* tp_getattro */
#else
0, /* tp_getattro */
#endif
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
_fam_request_type__doc__, /* doc string */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
#if PY_VERSION_HEX >= PY22
0, /* tp_iter */
0, /* tp_iternext */
_fam_request_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
#endif
};
/*
* FAMEvent object definitions
*
* An Event object is returned by the nextEvent method.
* It implements the code2str() method and has
* the attributes connection, requestID, hostname,
* filename, userData and code.
*
*/
typedef struct {
PyObject_HEAD
PyObject *attr;
_fam_connection_object *fc_obj;
enum FAMCodes code;
} _fam_event_object;
static PyMethodDef _fam_event_methods[] = {
{ "code2str", _fam_code2str, 1, _fam_code2str__doc__},
{ NULL, NULL, 0, NULL}
};
#if PY_VERSION_HEX >= PY22
static PyMemberDef _fam_event_members[] = {
{ "__dict__", T_OBJECT, offsetof(_fam_event_object, attr), READONLY, 0},
{ NULL, 0, 0, 0, NULL}
};
#endif
#if PY_VERSION_HEX < PY22
static PyObject* _fam_event_getattr(PyObject* self, char* name) {
_fam_event_object *fe_obj;
PyObject *v;
fe_obj = (_fam_event_object *)self;
if (fe_obj->attr) {
v = PyDict_GetItemString(fe_obj->attr, name);
if (v) {
Py_INCREF(v);
return v;
}
}
return Py_FindMethod(_fam_event_methods, self, name);
}
static int _fam_event_setattr(PyObject* self, char* name, PyObject* v) {
_fam_event_object *fe_obj = (_fam_event_object *)self;
if (! fe_obj->attr) {
fe_obj->attr = PyDict_New();
if (! fe_obj->attr)
return -1;
}
if (! v) {
int rv = PyDict_DelItemString(fe_obj->attr, name);
if (rv < 0)
PyErr_SetString(PyExc_AttributeError, "delete non-existing FAMEvent attribute");
return rv;
} else
return PyDict_SetItemString(fe_obj->attr, name, v);
}
#endif
void _fam_event_del(PyObject* self) {
_fam_event_object *fe_obj = (_fam_event_object *)self;
#ifdef _DEBUG
fprintf(stderr, "_fam: deleting event object.\n");
#endif
if (fe_obj) {
if (fe_obj->fc_obj) {
Py_DECREF(fe_obj->fc_obj);
fe_obj->fc_obj = NULL;
}
Py_XDECREF(fe_obj->attr);
PyMem_DEL(fe_obj);
}
}
static PyTypeObject _fam_event_type = {
#if defined(WIN32) || defined(__CYGWIN__)
PyObject_HEAD_INIT(NULL)
#else
PyObject_HEAD_INIT(&PyType_Type)
#endif
0, /* ob_size */
"_fam.FAMEvent", /* tp_name */
sizeof(_fam_event_object), /* tp_basicsize */
0, /* tp_itemsize */
_fam_event_del, /* tp_dealloc */
0, /* tp_print */
#if PY_VERSION_HEX < PY22
_fam_event_getattr, /* tp_getattr */
_fam_event_setattr, /* tp_setattr */
#else
0, /* tp_getattr */
0, /* tp_setattr */
#endif
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* call */
0, /* str */
#if PY_VERSION_HEX >= PY22
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
#else
0, /* tp_getattro */
0, /* tp_setattro */
#endif
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
_fam_event_type__doc__, /* doc string */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
#if PY_VERSION_HEX >= PY22
0, /* tp_iter */
0, /* tp_iternext */
_fam_event_methods, /* tp_methods */
_fam_event_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
offsetof(_fam_event_object, attr), /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
#endif
};
/*
* The method definitions.
*
*/
static PyObject* _fam_open(PyObject* self, PyObject* args) {
int rc;
_fam_connection_object *fc_obj = PyObject_NEW(_fam_connection_object, &_fam_connection_type);
if (!fc_obj)
return NULL;
fc_obj->fc = (FAMConnection *)malloc(sizeof(FAMConnection));
if (!fc_obj->fc) {
PyErr_SetString(PyExc_MemoryError, "_fam: unable to malloc for connection");
return NULL;
}
rc = FAMOpen(fc_obj->fc);
if (!rc) {
return (PyObject*)fc_obj;
} else {
PyErr_SetString(PyExc_IOError, "_fam: unable to open connection");
return NULL;
}
};
static PyObject* _fam_close(PyObject* self, PyObject* args) {
int rc;
_fam_connection_object *fc_obj = (_fam_connection_object *)self;
if (fc_obj->fc) {
rc = FAMClose(fc_obj->fc);
free(fc_obj->fc);
fc_obj->fc = NULL;
if (!rc) {
#ifdef _DEBUG
fprintf(stderr, "_fam: closed connection.\n");
#endif
} else {
PyErr_SetString(PyExc_IOError, "_fam: unable to close connection");
return NULL;
}
} else {
#ifdef _DEBUG
fprintf(stderr, "_fam: no connection to close.\n");
#endif
}
Py_INCREF(Py_None);
return Py_None;
};
static PyObject* _fam_monitor_directory(PyObject* self, PyObject* args) {
int rc;
char *filename;
PyObject *userData;
_fam_connection_object *fc_obj;
_fam_request_object *fr_obj;
fc_obj = (_fam_connection_object *)self;
if (fc_obj->fc) {
if (!PyArg_ParseTuple(args, "sO", &filename, &userData))
return NULL;
fr_obj = PyObject_NEW(_fam_request_object, &_fam_request_type);
if (!fr_obj)
return NULL;
fr_obj->fc_obj = fc_obj;
Py_INCREF(fc_obj);
fr_obj->fr = (FAMRequest *)malloc(sizeof(FAMRequest));
if (!fr_obj->fr) {
PyErr_SetString(PyExc_MemoryError, "_fam: unable to malloc for request");
return NULL;
}
rc = FAMMonitorDirectory(fc_obj->fc,
filename,
fr_obj->fr,
(void *)userData);
if (!rc) {
Py_INCREF(userData);
return (PyObject*)fr_obj;
} else {
PyErr_SetString(PyExc_IOError, "_fam: unable to monitor directory");
return NULL;
}
} else {
PyErr_SetString(PyExc_RuntimeError, "_fam: no connection to monitor");
return NULL;
}
};
static PyObject* _fam_monitor_file(PyObject* self, PyObject* args) {
int rc;
char *filename;
PyObject *userData;
_fam_connection_object *fc_obj;
_fam_request_object *fr_obj;
fc_obj = (_fam_connection_object *)self;
if (fc_obj->fc) {
if (!PyArg_ParseTuple(args, "sO", &filename, &userData))
return NULL;
fr_obj = PyObject_NEW(_fam_request_object, &_fam_request_type);
if (!fr_obj)
return NULL;
fr_obj->fc_obj = fc_obj;
Py_INCREF(fc_obj);
fr_obj->fr = (FAMRequest *)malloc(sizeof(FAMRequest));
if (!fr_obj->fr) {
PyErr_SetString(PyExc_MemoryError, "_fam: unable to malloc for request");
return NULL;
}
rc = FAMMonitorFile(fc_obj->fc, filename, fr_obj->fr, (void *)userData);
if (!rc) {
Py_INCREF(userData);
return (PyObject*)fr_obj;
} else {
PyErr_SetString(PyExc_IOError, "_fam: unable to monitor file");
return NULL;
}
} else {
PyErr_SetString(PyExc_RuntimeError, "_fam: no connection to monitor");
return NULL;
}
};
static PyObject* _fam_suspend_monitor(PyObject* self, PyObject* args) {
int rc;
_fam_request_object *fr_obj;
fr_obj = (_fam_request_object *)self;
if (fr_obj->fc_obj && fr_obj->fc_obj->fc && fr_obj->fr) {
rc = FAMSuspendMonitor(fr_obj->fc_obj->fc, fr_obj->fr);
if (rc) {
PyErr_SetString(PyExc_RuntimeError, "_fam: unable to suspend monitor");
return NULL;
}
}
Py_INCREF(Py_None);
return Py_None;
};
static PyObject* _fam_resume_monitor(PyObject* self, PyObject* args) {
int rc;
_fam_request_object *fr_obj;
fr_obj = (_fam_request_object *)self;
if (fr_obj->fc_obj && fr_obj->fc_obj->fc && fr_obj->fr) {
rc = FAMResumeMonitor(fr_obj->fc_obj->fc, fr_obj->fr);
if (rc) {
PyErr_SetString(PyExc_RuntimeError, "_fam: unable to resume monitor");
return NULL;
}
}
Py_INCREF(Py_None);
return Py_None;
};
static PyObject* _fam_cancel_monitor(PyObject* self, PyObject* args) {
int rc;
_fam_request_object *fr_obj;
fr_obj = (_fam_request_object *)self;
if (fr_obj->fc_obj && fr_obj->fc_obj->fc && fr_obj->fr) {
rc = FAMCancelMonitor(fr_obj->fc_obj->fc, fr_obj->fr);
free(fr_obj->fr);
fr_obj->fr = NULL;
Py_DECREF(fr_obj->fc_obj);
fr_obj->fc_obj = NULL;
} else {
#ifdef _DEBUG
fprintf(stderr, "_fam: no monitor to cancel.\n");
#endif
}
Py_INCREF(Py_None);
return Py_None;
};
static PyObject* _fam_next_event(PyObject* self, PyObject* args) {
int rc;
_fam_connection_object *fc_obj;
_fam_event_object *fe_obj;
FAMEvent fe;
PyObject *reqid, *hostname, *filename, *userData, *code;
fe.fc = NULL;
fe.hostname = NULL;
fe.userdata = NULL;
fc_obj = (_fam_connection_object *)self;
if (fc_obj->fc) {
fe_obj = PyObject_NEW(_fam_event_object, &_fam_event_type);
if (!fe_obj)
return NULL;
#ifdef _DEBUG
fprintf(stderr, "_fam: got fe_obj.\n");
#endif
fe_obj->attr = NULL;
fe_obj->fc_obj = fc_obj;
Py_INCREF(fc_obj);
rc = FAMNextEvent(fc_obj->fc, &fe);
if (rc == 1) {
if (PyObject_SetAttrString((PyObject*)fe_obj,
"connection",
(PyObject*)fc_obj) < 0)
return NULL;
#ifdef _DEBUG
fprintf(stderr, "_fam: got next event from queue.\n");
#endif
reqid = PyInt_FromLong((long)fe.fr.reqnum);
if (PyObject_SetAttrString((PyObject*)fe_obj, "requestID", reqid) < 0)
return NULL;
#ifdef _DEBUG
fprintf(stderr, "_fam: got request ID.\n");
#endif
filename = PyString_FromString(fe.filename);
if (PyObject_SetAttrString((PyObject*)fe_obj, "filename", filename) < 0)
return NULL;
#ifdef _DEBUG
fprintf(stderr, "_fam: got filename %s.\n", fe.filename);
#endif
userData = (PyObject*)fe.userdata;
if (PyObject_SetAttrString((PyObject*)fe_obj, "userData", userData) < 0)
return NULL;
#ifdef _DEBUG
fprintf(stderr, "_fam: got userdata.\n");
#endif
code = PyInt_FromLong(fe.code);
if (PyObject_SetAttrString((PyObject*)fe_obj, "code", code) < 0)
return NULL;
fe_obj->code = fe.code;
#ifdef _DEBUG
fprintf(stderr, "_fam: got code %d: '%s'.\n", fe.code, code2str(fe.code));
#endif
if (! fe.hostname) {
#ifdef _DEBUG
fprintf(stderr, "_fam: null pointer to hostname.\n");
#endif
fe.hostname = (char*)malloc(255*sizeof(char));
bzero(fe.hostname,255);
#ifdef _DEBUG
} else {
fprintf(stderr, "_fam: valid pointer to hostname %s.\n", fe.hostname);
#endif
}
#ifdef _DEBUG
fprintf(stderr, "_fam: casting hostname to PyString.\n");
#endif
hostname = PyString_FromString(fe.hostname);
#ifdef _DEBUG
fprintf(stderr, "_fam: casted hostname to PyString.\n");
#endif
if (PyObject_SetAttrString((PyObject*)fe_obj, "hostname", hostname) < 0)
return NULL;
#ifdef _DEBUG
fprintf(stderr, "_fam: got hostname.\n");
#endif
return (PyObject*)fe_obj;
} else {
PyErr_SetString(PyExc_IOError, "_fam: unable to get next event");
return NULL;
}
} else {
PyErr_SetString(PyExc_RuntimeError, "_fam: no connection for next event");
return NULL;
}
};
static PyObject* _fam_pending(PyObject* self, PyObject* args) {
PyObject *pyval = Py_False;
_fam_connection_object *fc_obj;
fc_obj = (_fam_connection_object *)self;
if (fc_obj->fc)
pyval = FAMPending(fc_obj->fc) ? Py_True : Py_False;
Py_INCREF(pyval);
return pyval;
};
static PyObject* _fam_code2str(PyObject* self, PyObject* args) {
_fam_event_object *fe_obj;
PyObject *rv;
char *str;
fe_obj = (_fam_event_object *)self;
str = code2str(fe_obj->code);
if (str) {
rv = PyString_FromString(str);
} else {
PyErr_SetString(PyExc_RuntimeError, "_fam: unknown event code");
rv = NULL;
}
return rv;
}
static PyObject* _fam_fileno(PyObject* self, PyObject* args) {
_fam_connection_object *fc_obj;
PyObject *rv;
fc_obj = (_fam_connection_object *)self;
rv = PyInt_FromLong(FAMCONNECTION_GETFD(fc_obj->fc));
if (!rv) {
PyErr_SetString(PyExc_RuntimeError,
"_fam: unable to get file descriptor from connection");
}
return rv;
}
static PyObject* _fam_request_id(PyObject* self, PyObject* args) {
_fam_request_object *fr_obj;
PyObject *rv;
fr_obj = (_fam_request_object *)self;
rv = PyInt_FromLong(FAMREQUEST_GETREQNUM(fr_obj->fr));
if (!rv) {
PyErr_SetString(PyExc_RuntimeError, "_fam: unable to get ID from request");
}
return rv;
}
void _fam_cleanup(void) {
#ifdef _DEBUG
fprintf(stderr, "cleaning up _fam.\n");
#endif
}
static PyMethodDef moduleMethods[] = {
{ "open", _fam_open, 1, _fam_open__doc__},
{ NULL, NULL, 0, NULL}
};
static char _fam__doc__[] = "File Alteration Monitor library routines";
void init_fam(void) {
PyObject *m, *d;
m = Py_InitModule3("_fam",
moduleMethods,
_fam__doc__);
d = PyModule_GetDict(m);
add_int(d, Changed);
add_int(d, Deleted);
add_int(d, StartExecuting);
add_int(d, StopExecuting);
add_int(d, Created);
add_int(d, Moved);
add_int(d, Acknowledge);
add_int(d, Exists);
add_int(d, EndExist);
Py_INCREF(&_fam_connection_type);
PyModule_AddObject(m, "FAMConnection", (PyObject*)&_fam_connection_type);
Py_INCREF(&_fam_request_type);
PyModule_AddObject(m, "FAMRequest", (PyObject*)&_fam_request_type);
Py_INCREF(&_fam_event_type);
PyModule_AddObject(m, "FAMEvent", (PyObject*)&_fam_event_type);
#if defined(WIN32) || defined(__CYGWIN__)
_fam_connection_type.ob_type = &PyType_Type;
_fam_request_type.ob_type = &PyType_Type;
#endif
if (Py_AtExit(_fam_cleanup)) {
fprintf(stderr, "_fam: warining: cleanup procedure not registered.\n");
}
};
|