diff options
-rw-r--r-- | Lib/lib-old/ni.py | 372 | ||||
-rw-r--r-- | Lib/test/test_MimeWriter.py | 88 |
2 files changed, 230 insertions, 230 deletions
diff --git a/Lib/lib-old/ni.py b/Lib/lib-old/ni.py index 95c9b59..13bcfac 100644 --- a/Lib/lib-old/ni.py +++ b/Lib/lib-old/ni.py @@ -16,7 +16,7 @@ Quick Reference - To import module ham from package spam and use function hamneggs() from that module, you can either do - import spam.ham # *not* "import spam" !!! + import spam.ham # *not* "import spam" !!! spam.ham.hamneggs() or @@ -36,7 +36,7 @@ Quick Reference spam.__init__ is a submodule of package spam. It can reference to spam's namespace via the '__.' prefix, for instance - __.spam_inited = 1 # Set a package-level variable + __.spam_inited = 1 # Set a package-level variable @@ -182,84 +182,84 @@ class PackageLoader(ModuleLoader): """ def find_module_in_dir(self, name, dir): - if dir is not None: - dirname = self.hooks.path_join(dir, name) - if self.hooks.path_isdir(dirname): - return None, dirname, ('', '', 'PACKAGE') - return ModuleLoader.find_module_in_dir(self, name, dir) + if dir is not None: + dirname = self.hooks.path_join(dir, name) + if self.hooks.path_isdir(dirname): + return None, dirname, ('', '', 'PACKAGE') + return ModuleLoader.find_module_in_dir(self, name, dir) def load_module(self, name, stuff): - file, filename, info = stuff - suff, mode, type = info - if type == 'PACKAGE': - return self.load_package(name, stuff) - if sys.modules.has_key(name): - m = sys.modules[name] - else: - sys.modules[name] = m = imp.new_module(name) - self.set_parent(m) - if type == imp.C_EXTENSION and '.' in name: - return self.load_dynamic(name, stuff) - else: - return ModuleLoader.load_module(self, name, stuff) + file, filename, info = stuff + suff, mode, type = info + if type == 'PACKAGE': + return self.load_package(name, stuff) + if sys.modules.has_key(name): + m = sys.modules[name] + else: + sys.modules[name] = m = imp.new_module(name) + self.set_parent(m) + if type == imp.C_EXTENSION and '.' in name: + return self.load_dynamic(name, stuff) + else: + return ModuleLoader.load_module(self, name, stuff) def load_dynamic(self, name, stuff): - file, filename, (suff, mode, type) = stuff - # Hack around restriction in imp.load_dynamic() - i = string.rfind(name, '.') - tail = name[i+1:] - if sys.modules.has_key(tail): - save = sys.modules[tail] - else: - save = None - sys.modules[tail] = imp.new_module(name) - try: - m = imp.load_dynamic(tail, filename, file) - finally: - if save: - sys.modules[tail] = save - else: - del sys.modules[tail] - sys.modules[name] = m - return m + file, filename, (suff, mode, type) = stuff + # Hack around restriction in imp.load_dynamic() + i = string.rfind(name, '.') + tail = name[i+1:] + if sys.modules.has_key(tail): + save = sys.modules[tail] + else: + save = None + sys.modules[tail] = imp.new_module(name) + try: + m = imp.load_dynamic(tail, filename, file) + finally: + if save: + sys.modules[tail] = save + else: + del sys.modules[tail] + sys.modules[name] = m + return m def load_package(self, name, stuff): - file, filename, info = stuff - if sys.modules.has_key(name): - package = sys.modules[name] - else: - sys.modules[name] = package = imp.new_module(name) - package.__path__ = [filename] - self.init_package(package) - return package + file, filename, info = stuff + if sys.modules.has_key(name): + package = sys.modules[name] + else: + sys.modules[name] = package = imp.new_module(name) + package.__path__ = [filename] + self.init_package(package) + return package def init_package(self, package): - self.set_parent(package) - self.set_domain(package) - self.call_init_module(package) + self.set_parent(package) + self.set_domain(package) + self.call_init_module(package) def set_parent(self, m): - name = m.__name__ - if '.' in name: - name = name[:string.rfind(name, '.')] - else: - name = '' - m.__ = sys.modules[name] + name = m.__name__ + if '.' in name: + name = name[:string.rfind(name, '.')] + else: + name = '' + m.__ = sys.modules[name] def set_domain(self, package): - name = package.__name__ - package.__domain__ = domain = [name] - while '.' in name: - name = name[:string.rfind(name, '.')] - domain.append(name) - if name: - domain.append('') + name = package.__name__ + package.__domain__ = domain = [name] + while '.' in name: + name = name[:string.rfind(name, '.')] + domain.append(name) + if name: + domain.append('') def call_init_module(self, package): - stuff = self.find_module('__init__', package.__path__) - if stuff: - m = self.load_module(package.__name__ + '.__init__', stuff) - package.__init__ = m + stuff = self.find_module('__init__', package.__path__) + if stuff: + m = self.load_module(package.__name__ + '.__init__', stuff) + package.__init__ = m class PackageImporter(ModuleImporter): @@ -267,132 +267,132 @@ class PackageImporter(ModuleImporter): """Importer that understands packages and '__'.""" def __init__(self, loader = None, verbose = 0): - ModuleImporter.__init__(self, - loader or PackageLoader(None, verbose), verbose) + ModuleImporter.__init__(self, + loader or PackageLoader(None, verbose), verbose) def import_module(self, name, globals={}, locals={}, fromlist=[]): - if globals.has_key('__'): - package = globals['__'] - else: - # No calling context, assume in root package - package = sys.modules[''] - if name[:3] in ('__.', '__'): - p = package - name = name[3:] - while name[:3] in ('__.', '__'): - p = p.__ - name = name[3:] - if not name: - return self.finish(package, p, '', fromlist) - if '.' in name: - i = string.find(name, '.') - name, tail = name[:i], name[i:] - else: - tail = '' - mname = p.__name__ and p.__name__+'.'+name or name - m = self.get1(mname) - return self.finish(package, m, tail, fromlist) - if '.' in name: - i = string.find(name, '.') - name, tail = name[:i], name[i:] - else: - tail = '' - for pname in package.__domain__: - mname = pname and pname+'.'+name or name - m = self.get0(mname) - if m: break - else: - raise ImportError, "No such module %s" % name - return self.finish(m, m, tail, fromlist) + if globals.has_key('__'): + package = globals['__'] + else: + # No calling context, assume in root package + package = sys.modules[''] + if name[:3] in ('__.', '__'): + p = package + name = name[3:] + while name[:3] in ('__.', '__'): + p = p.__ + name = name[3:] + if not name: + return self.finish(package, p, '', fromlist) + if '.' in name: + i = string.find(name, '.') + name, tail = name[:i], name[i:] + else: + tail = '' + mname = p.__name__ and p.__name__+'.'+name or name + m = self.get1(mname) + return self.finish(package, m, tail, fromlist) + if '.' in name: + i = string.find(name, '.') + name, tail = name[:i], name[i:] + else: + tail = '' + for pname in package.__domain__: + mname = pname and pname+'.'+name or name + m = self.get0(mname) + if m: break + else: + raise ImportError, "No such module %s" % name + return self.finish(m, m, tail, fromlist) def finish(self, module, m, tail, fromlist): - # Got ....A; now get ....A.B.C.D - yname = m.__name__ - if tail and sys.modules.has_key(yname + tail): # Fast path - yname, tail = yname + tail, '' - m = self.get1(yname) - while tail: - i = string.find(tail, '.', 1) - if i > 0: - head, tail = tail[:i], tail[i:] - else: - head, tail = tail, '' - yname = yname + head - m = self.get1(yname) - - # Got ....A.B.C.D; now finalize things depending on fromlist - if not fromlist: - return module - if '__' in fromlist: - raise ImportError, "Can't import __ from anywhere" - if not hasattr(m, '__path__'): return m - if '*' in fromlist: - raise ImportError, "Can't import * from a package" - for f in fromlist: - if hasattr(m, f): continue - fname = yname + '.' + f - self.get1(fname) - return m + # Got ....A; now get ....A.B.C.D + yname = m.__name__ + if tail and sys.modules.has_key(yname + tail): # Fast path + yname, tail = yname + tail, '' + m = self.get1(yname) + while tail: + i = string.find(tail, '.', 1) + if i > 0: + head, tail = tail[:i], tail[i:] + else: + head, tail = tail, '' + yname = yname + head + m = self.get1(yname) + + # Got ....A.B.C.D; now finalize things depending on fromlist + if not fromlist: + return module + if '__' in fromlist: + raise ImportError, "Can't import __ from anywhere" + if not hasattr(m, '__path__'): return m + if '*' in fromlist: + raise ImportError, "Can't import * from a package" + for f in fromlist: + if hasattr(m, f): continue + fname = yname + '.' + f + self.get1(fname) + return m def get1(self, name): - m = self.get(name) - if not m: - raise ImportError, "No module named %s" % name - return m + m = self.get(name) + if not m: + raise ImportError, "No module named %s" % name + return m def get0(self, name): - m = self.get(name) - if not m: - sys.modules[name] = None - return m + m = self.get(name) + if not m: + sys.modules[name] = None + return m def get(self, name): - # Internal routine to get or load a module when its parent exists - if sys.modules.has_key(name): - return sys.modules[name] - if '.' in name: - i = string.rfind(name, '.') - head, tail = name[:i], name[i+1:] - else: - head, tail = '', name - path = sys.modules[head].__path__ - stuff = self.loader.find_module(tail, path) - if not stuff: - return None - sys.modules[name] = m = self.loader.load_module(name, stuff) - if head: - setattr(sys.modules[head], tail, m) - return m + # Internal routine to get or load a module when its parent exists + if sys.modules.has_key(name): + return sys.modules[name] + if '.' in name: + i = string.rfind(name, '.') + head, tail = name[:i], name[i+1:] + else: + head, tail = '', name + path = sys.modules[head].__path__ + stuff = self.loader.find_module(tail, path) + if not stuff: + return None + sys.modules[name] = m = self.loader.load_module(name, stuff) + if head: + setattr(sys.modules[head], tail, m) + return m def reload(self, module): - name = module.__name__ - if '.' in name: - i = string.rfind(name, '.') - head, tail = name[:i], name[i+1:] - path = sys.modules[head].__path__ - else: - tail = name - path = sys.modules[''].__path__ - stuff = self.loader.find_module(tail, path) - if not stuff: - raise ImportError, "No module named %s" % name - return self.loader.load_module(name, stuff) + name = module.__name__ + if '.' in name: + i = string.rfind(name, '.') + head, tail = name[:i], name[i+1:] + path = sys.modules[head].__path__ + else: + tail = name + path = sys.modules[''].__path__ + stuff = self.loader.find_module(tail, path) + if not stuff: + raise ImportError, "No module named %s" % name + return self.loader.load_module(name, stuff) def unload(self, module): - if hasattr(module, '__path__'): - raise ImportError, "don't know how to unload packages yet" - PackageImporter.unload(self, module) + if hasattr(module, '__path__'): + raise ImportError, "don't know how to unload packages yet" + PackageImporter.unload(self, module) def install(self): - if not sys.modules.has_key(''): - sys.modules[''] = package = imp.new_module('') - package.__path__ = None - self.loader.init_package(package) - for m in sys.modules.values(): - if not m: continue - if not hasattr(m, '__'): - self.loader.set_parent(m) - ModuleImporter.install(self) + if not sys.modules.has_key(''): + sys.modules[''] = package = imp.new_module('') + package.__path__ = None + self.loader.init_package(package) + for m in sys.modules.values(): + if not m: continue + if not hasattr(m, '__'): + self.loader.set_parent(m) + ModuleImporter.install(self) def install(v = 0): @@ -410,22 +410,22 @@ def no(): def test(): import pdb try: - testproper() + testproper() except: - sys.last_type, sys.last_value, sys.last_traceback = sys.exc_info() - print - print sys.last_type, ':', sys.last_value - print - pdb.pm() + sys.last_type, sys.last_value, sys.last_traceback = sys.exc_info() + print + print sys.last_type, ':', sys.last_value + print + pdb.pm() def testproper(): install(1) try: - import mactest - print dir(mactest) - raw_input('OK?') + import mactest + print dir(mactest) + raw_input('OK?') finally: - uninstall() + uninstall() if __name__ == '__main__': diff --git a/Lib/test/test_MimeWriter.py b/Lib/test/test_MimeWriter.py index 35e5162..8b74421 100644 --- a/Lib/test/test_MimeWriter.py +++ b/Lib/test/test_MimeWriter.py @@ -16,30 +16,30 @@ INTERFACE Seller-1; TYPE Seller = OBJECT DOCUMENTATION "A simple Seller interface to test ILU" METHODS - price():INTEGER, + price():INTEGER, END; ''' BUYER = '''\ class Buyer: def __setup__(self, maxprice): - self._maxprice = maxprice + self._maxprice = maxprice def __main__(self, kos): - """Entry point upon arrival at a new KOS.""" - broker = kos.broker() - # B4 == Barry's Big Bass Business :-) - seller = broker.lookup('Seller_1.Seller', 'B4') - if seller: - price = seller.price() - print 'Seller wants $', price, '... ' - if price > self._maxprice: - print 'too much!' - else: - print "I'll take it!" - else: - print 'no seller found here' -''' # Don't ask why this comment is here + """Entry point upon arrival at a new KOS.""" + broker = kos.broker() + # B4 == Barry's Big Bass Business :-) + seller = broker.lookup('Seller_1.Seller', 'B4') + if seller: + price = seller.price() + print 'Seller wants $', price, '... ' + if price > self._maxprice: + print 'too much!' + else: + print "I'll take it!" + else: + print 'no seller found here' +''' # Don't ask why this comment is here STATE = '''\ # instantiate a buyer instance and put it in a magic place for the KOS @@ -49,32 +49,32 @@ __kp__.__setup__(500) ''' SIMPLE_METADATA = [ - ("Interpreter", "python"), - ("Interpreter-Version", "1.3"), - ("Owner-Name", "Barry Warsaw"), - ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"), - ("Home-KSS", "kss.cnri.reston.va.us"), - ("Identifier", "hdl://cnri.kss/my_first_knowbot"), - ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"), - ] + ("Interpreter", "python"), + ("Interpreter-Version", "1.3"), + ("Owner-Name", "Barry Warsaw"), + ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"), + ("Home-KSS", "kss.cnri.reston.va.us"), + ("Identifier", "hdl://cnri.kss/my_first_knowbot"), + ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"), + ] COMPLEX_METADATA = [ - ("Metadata-Type", "complex"), - ("Metadata-Key", "connection"), - ("Access", "read-only"), - ("Connection-Description", "Barry's Big Bass Business"), - ("Connection-Id", "B4"), - ("Connection-Direction", "client"), - ] + ("Metadata-Type", "complex"), + ("Metadata-Key", "connection"), + ("Access", "read-only"), + ("Connection-Description", "Barry's Big Bass Business"), + ("Connection-Id", "B4"), + ("Connection-Direction", "client"), + ] EXTERNAL_METADATA = [ - ("Metadata-Type", "complex"), - ("Metadata-Key", "generic-interface"), - ("Access", "read-only"), - ("Connection-Description", "Generic Interface for All Knowbots"), - ("Connection-Id", "generic-kp"), - ("Connection-Direction", "client"), - ] + ("Metadata-Type", "complex"), + ("Metadata-Key", "generic-interface"), + ("Access", "read-only"), + ("Connection-Description", "Generic Interface for All Knowbots"), + ("Connection-Id", "generic-kp"), + ("Connection-Direction", "client"), + ] def main(): @@ -91,7 +91,7 @@ def main(): # Toplevel body parts f = toplevel.startmultipartbody("knowbot", "801spam999", - [("version", "0.1")], prefix=0) + [("version", "0.1")], prefix=0) f.write("This is a multi-part message in MIME format.\n") # First toplevel body part: metadata @@ -106,7 +106,7 @@ def main(): md1.addheader("KP-Access", "read-only") m = MimeWriter(md1.startbody("message/rfc822")) for key, value in SIMPLE_METADATA: - m.addheader("KPMD-" + key, value) + m.addheader("KPMD-" + key, value) m.flushheaders() del md1 @@ -114,7 +114,7 @@ def main(): md2 = md.nextpart() for key, value in COMPLEX_METADATA: - md2.addheader("KP-" + key, value) + md2.addheader("KP-" + key, value) f = md2.startbody("text/isl") f.write(SELLER) del md2 @@ -123,11 +123,11 @@ def main(): md3 = md.nextpart() f = md3.startbody("message/external-body", - [("access-type", "URL"), - ("URL", "hdl://cnri.kss/generic-knowbot")]) + [("access-type", "URL"), + ("URL", "hdl://cnri.kss/generic-knowbot")]) m = MimeWriter(f) for key, value in EXTERNAL_METADATA: - md3.addheader("KP-" + key, value) + md3.addheader("KP-" + key, value) md3.startbody("text/isl") # Phantom body doesn't need to be written |