Changeset 0147ca9


Ignore:
Timestamp:
2013-03-17T15:06:34Z (8 years ago)
Author:
Nick Murdoch <nick@…>
Children:
e38c6d8
Parents:
0075527
Message:

Scan through plugindir and find python files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/python/src/bpython.c

    r0075527 r0147ca9  
    2828void init_plugin() {
    2929    struct prpl *ret = g_new0(struct prpl, 1);
     30    GDir *dir;
     31    GError *error = NULL;
    3032
    3133    printf("Initialising Python... ");
    3234    Py_Initialize();
    3335    PyRun_SimpleString("print 'Python initialised!'\n");
    34     // Py_Finalize(); // TODO - there's no plugin_unload for us to do this in.
     36   
     37    /* Get a reference to the main module. */
     38    PyObject* main_module = PyImport_AddModule("__main__");
     39
     40    /* Get the main module's dictionary */
     41    PyObject* main_dict = PyModule_GetDict(main_module);
     42
     43    dir = g_dir_open(global.conf->plugindir, 0, &error);
     44    if (dir) {
     45        const gchar *entry;
     46        char * path;
     47        while ((entry = g_dir_read_name(dir))) {
     48            path = g_build_filename(global.conf->plugindir, entry, NULL);
     49            if (!path) {
     50                log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry);
     51                continue;
     52            }
     53           
     54            if (g_str_has_suffix(path, ".py")) {
     55                printf("Loading python file %s\n", path);
     56                FILE* pyfile = fopen(path, "r");
     57                /* Copy main dict to make sure that separate plugins
     58                   run in separate environments */
     59                PyObject* main_dict_copy = PyDict_Copy(main_dict);
     60                PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
     61               
     62            }
     63
     64            g_free(path);
     65        }
     66    }
     67
    3568
    3669    ret->name = "bpython";
    3770
    3871    register_protocol(ret);
     72
     73    // Py_Finalize(); // TODO - there's no plugin_unload for us to do this in.
    3974}
    4075
Note: See TracChangeset for help on using the changeset viewer.