KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyModule


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 public class PyModule extends PyObject
5 {
6     public PyObject __dict__;
7
8     public PyModule(String JavaDoc name, PyObject dict) {
9         if (dict == null)
10             __dict__ = new PyStringMap();
11         else
12             __dict__ = dict;
13         __dict__.__setitem__("__name__", new PyString(name));
14         __dict__.__setitem__("__doc__", Py.None);
15     }
16
17     protected PyObject impAttr(String JavaDoc attr) {
18         PyObject path = __dict__.__finditem__("__path__");
19         PyObject pyname = __dict__.__finditem__("__name__");
20
21         if (path == null || pyname == null) return null;
22
23         String JavaDoc name = pyname.__str__().toString();
24         String JavaDoc fullName = (name+'.'+attr).intern();
25
26         PyObject ret = null;
27
28         //System.err.println("PyModule.impAttr " + attr + " " + name + " " + fullName);
29
if (path == Py.None) {
30             /* disabled:
31             ret = imp.loadFromClassLoader(
32             (name+'.'+attr).intern(),
33             Py.getSystemState().getClassLoader());
34              */

35         }
36         else if (path instanceof PyList) {
37             ret = imp.find_module(attr, fullName, (PyList)path);
38         }
39         else {
40             throw Py.TypeError("__path__ must be list or None");
41         }
42
43         if (ret == null) {
44             ret = PySystemState.packageManager.lookupName(fullName);
45         }
46
47         if (ret != null) {
48             // Allow a package component to change its own meaning
49
PyObject tmp = Py.getSystemState().modules.__finditem__(fullName);
50             if (tmp != null) ret = tmp;
51             __dict__.__setitem__(attr, ret);
52             return ret;
53         }
54
55         return null;
56
57     }
58
59     public PyObject __findattr__(String JavaDoc attr) {
60         PyObject ret;
61
62         ret = __dict__.__finditem__(attr);
63         if (ret != null) return ret;
64
65         ret = super.__findattr__(attr);
66         if (ret != null) return ret;
67
68         PyObject pyname = __dict__.__finditem__("__name__");
69         if (pyname == null) return null;
70
71         return impHook(pyname.__str__().toString()+'.'+attr);
72     }
73
74     public void __setattr__(String JavaDoc attr, PyObject value) {
75         __dict__.__setitem__(attr, value);
76     }
77
78     public void __delattr__(String JavaDoc attr) {
79         __dict__.__delitem__(attr);
80     }
81
82     public PyObject __dir__() {
83         return __dict__.invoke("keys");
84     }
85
86     public String JavaDoc toString() {
87         PyObject name = __dict__.__finditem__("__name__");
88         PyObject file = __dict__.__finditem__("__file__");
89         return "<module '"+ name + "' from '" + file + "'>";
90     }
91
92     static private PyObject silly_list = null;
93
94     private static PyObject impHook(String JavaDoc name) {
95         if (silly_list == null) {
96             silly_list = new PyTuple(new PyString[] {
97                                      Py.newString("__doc__"),});
98         }
99         try {
100             return __builtin__.__import__(name, null, null, silly_list);
101         } catch(PyException e) {
102             if (Py.matchException(e, Py.ImportError)) {
103                 return null;
104             }
105             throw e;
106         }
107     }
108
109     /**
110      * @see org.python.core.PyObject#safeRepr()
111      */

112     public String JavaDoc safeRepr() throws PyIgnoreMethodTag {
113         PyObject name = __dict__.__finditem__("__name__");
114         if (name == null) return "unnamed module";
115         return "module '"+name+"'";
116     }
117
118 }
119
Popular Tags