addpack.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Lib » lib-old » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Lib » lib old » addpack.py
# This module provides standard support for "packages".
#
# The idea is that large groups of related modules can be placed in
# their own subdirectory, which can be added to the Python search path
# in a relatively easy way.
#
# The current version takes a package name and searches the Python
# search path for a directory by that name, and if found adds it to
# the module search path (sys.path).  It maintains a list of packages
# that have already been added so adding the same package many times
# is OK.
#
# It is intended to be used in a fairly stylized manner: each module
# that wants to use a particular package, say 'Foo', is supposed to
# contain the following code:
#
#   from addpack import addpack
#   addpack('Foo')
#   <import modules from package Foo>
#
# Additional arguments, when present, provide additional places where
# to look for the package before trying sys.path (these may be either
# strings or lists/tuples of strings).  Also, if the package name is a
# full pathname, first the last component is tried in the usual way,
# then the full pathname is tried last.  If the package name is a
# *relative* pathname (UNIX: contains a slash but doesn't start with
# one), then nothing special is done.  The packages "/foo/bar/bletch"
# and "bletch" are considered the same, but unrelated to "bar/bletch".
#
# If the algorithm finds more than one suitable subdirectory, all are
# added to the search path -- this makes it possible to override part
# of a package.  The same path will not be added more than once.
#
# If no directory is found, ImportError is raised.

_packs = {}        # {pack: [pathname, ...], ...}

def addpack(pack, *locations):
  import os
  if os.path.isabs(pack):
    base = os.path.basename(pack)
  else:
    base = pack
  if _packs.has_key(base):
    return
  import sys
  path = []
  for loc in _flatten(locations) + sys.path:
    fn = os.path.join(loc, base)
    if fn not in path and os.path.isdir(fn):
      path.append(fn)
  if pack != base and pack not in path and os.path.isdir(pack):
    path.append(pack)
  if not path: raise ImportError, 'package ' + pack + ' not found'
  _packs[base] = path
  for fn in path:
    if fn not in sys.path:
      sys.path.append(fn)

def _flatten(locations):
  locs = []
  for loc in locations:
    if type(loc) == type(''):
      locs.append(loc)
    else:
      locs = locs + _flatten(loc)
  return locs
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.