home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1755 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.8 KB  |  286 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from IPython import ipapi
  5. from IPython import Release
  6. from types import StringType
  7. import sys
  8. import os
  9. import textwrap
  10. _marker = []
  11.  
  12. def shasattr(obj, attr, acquire = False):
  13.     if not acquire:
  14.         obj = obj.aq_base
  15.     
  16.     return getattr(obj, attr, _marker) is not _marker
  17.  
  18.  
  19. class ZopeDebug(object):
  20.     
  21.     def __init__(self):
  22.         self.instancehome = os.environ.get('INSTANCE_HOME')
  23.         configfile = os.environ.get('CONFIG_FILE')
  24.         if configfile is None and self.instancehome is not None:
  25.             configfile = os.path.join(self.instancehome, 'etc', 'zope.conf')
  26.         
  27.         if configfile is None:
  28.             raise RuntimeError('CONFIG_FILE env not set')
  29.         configfile is None
  30.         print 'CONFIG_FILE=', configfile
  31.         print 'INSTANCE_HOME=', self.instancehome
  32.         self.configfile = configfile
  33.         
  34.         try:
  35.             configure = configure
  36.             import Zope2
  37.         except ImportError:
  38.             configure = configure
  39.             import Zope
  40.  
  41.         configure(configfile)
  42.         
  43.         try:
  44.             import Zope2
  45.             app = Zope2.app()
  46.         except ImportError:
  47.             import Zope
  48.             app = Zope.app()
  49.  
  50.         makerequest = makerequest
  51.         import Testing.makerequest
  52.         self.app = makerequest(app)
  53.         
  54.         try:
  55.             self._make_permissive()
  56.             print 'Permissive security installed'
  57.         except:
  58.             print 'Permissive security NOT installed'
  59.  
  60.         if not self.portal:
  61.             pass
  62.         self._pwd = self.app
  63.         
  64.         try:
  65.             getSiteManager = getSiteManager
  66.             import zope.component
  67.             getGlobalSiteManager = getGlobalSiteManager
  68.             import zope.component
  69.             setSite = setSite
  70.             import zope.app.component.hooks
  71.             if self.portal is not None:
  72.                 setSite(self.portal)
  73.                 gsm = getGlobalSiteManager()
  74.                 sm = getSiteManager()
  75.                 if sm is gsm:
  76.                     print 'ERROR SETTING SITE!'
  77.                 
  78.         except:
  79.             pass
  80.  
  81.  
  82.     
  83.     def utils(self):
  84.         
  85.         class Utils((object,)):
  86.             commit = self.commit
  87.             sync = self.sync
  88.             objectInfo = self.objectInfo
  89.             ls = self.ls
  90.             pwd = self.pwd
  91.             cd = self.cd
  92.             su = self.su
  93.             getCatalogInfo = self.getCatalogInfo
  94.             
  95.             def cwd(self):
  96.                 return self.pwd()
  97.  
  98.             cwd = property(cwd)
  99.  
  100.         return Utils()
  101.  
  102.     utils = property(utils)
  103.     
  104.     def namespace(self):
  105.         return dict(utils = self.utils, app = self.app, portal = self.portal)
  106.  
  107.     namespace = property(namespace)
  108.     
  109.     def portal(self):
  110.         portals = self.app.objectValues('Plone Site')
  111.         if len(portals):
  112.             return portals[0]
  113.         raise KeyError('No Plone Site found.')
  114.  
  115.     portal = property(portal)
  116.     
  117.     def pwd(self):
  118.         return self._pwd
  119.  
  120.     
  121.     def _make_permissive(self):
  122.         PermissiveSecurityPolicy = PermissiveSecurityPolicy
  123.         import Products.CMFCore.tests.base.security
  124.         import AccessControl
  125.         newSecurityManager = newSecurityManager
  126.         import AccessControl.SecurityManagement
  127.         setSecurityPolicy = setSecurityPolicy
  128.         import AccessControl.SecurityManager
  129.         _policy = PermissiveSecurityPolicy()
  130.         self.oldpolicy = setSecurityPolicy(_policy)
  131.         newSecurityManager(None, AccessControl.User.system)
  132.  
  133.     
  134.     def su(self, username):
  135.         user = self.portal.acl_users.getUser(username)
  136.         if not user:
  137.             print "Can't find %s in %s" % (username, self.portal.acl_users)
  138.             return None
  139.         ZopeSecurityPolicy = ZopeSecurityPolicy
  140.         import AccessControl
  141.         import AccessControl
  142.         newSecurityManager = newSecurityManager
  143.         getSecurityManager = getSecurityManager
  144.         import AccessControl.SecurityManagement
  145.         setSecurityPolicy = setSecurityPolicy
  146.         import AccessControl.SecurityManager
  147.         _policy = ZopeSecurityPolicy
  148.         self.oldpolicy = setSecurityPolicy(_policy)
  149.         wrapped_user = user.__of__(self.portal.acl_users)
  150.         newSecurityManager(None, user)
  151.         print 'User changed.'
  152.         return getSecurityManager().getUser()
  153.  
  154.     
  155.     def getCatalogInfo(self, obj = None, catalog = 'portal_catalog', query = None, sort_on = 'created', sort_order = 'reverse'):
  156.         if obj and query:
  157.             print 'Ignoring %s, using query.' % obj
  158.         
  159.         catalog = self.portal.get(catalog)
  160.         if not catalog:
  161.             return 'No catalog'
  162.         indexes = catalog._catalog.indexes
  163.         if not query:
  164.             if type(obj) is StringType:
  165.                 cwd = self.pwd()
  166.                 obj = cwd.unrestrictedTraverse(obj)
  167.             
  168.             query = { }
  169.             if indexes.get('path'):
  170.                 join = join
  171.                 import string
  172.                 path = join(obj.getPhysicalPath(), '/')
  173.                 query.update({
  174.                     'path': path })
  175.             
  176.             if indexes.get('getID'):
  177.                 query.update({
  178.                     'getID': obj.id })
  179.             
  180.             if indexes.get('UID') and shasattr(obj, 'UID'):
  181.                 query.update({
  182.                     'UID': obj.UID() })
  183.             
  184.         
  185.         if indexes.get(sort_on):
  186.             query.update({
  187.                 'sort_on': sort_on,
  188.                 'sort_order': sort_order })
  189.         
  190.         if not query:
  191.             return 'Empty query'
  192.         results = catalog(**query)
  193.         result_info = []
  194.         for r in results:
  195.             rid = r.getRID()
  196.             if rid:
  197.                 result_info.append({
  198.                     'path': catalog.getpath(rid),
  199.                     'metadata': catalog.getMetadataForRID(rid),
  200.                     'indexes': catalog.getIndexDataForRID(rid) })
  201.                 continue
  202.             query
  203.             result_info.append({
  204.                 'missing': rid })
  205.         
  206.         if len(result_info) == 1:
  207.             return result_info[0]
  208.         return result_info
  209.  
  210.     
  211.     def commit(self):
  212.         
  213.         try:
  214.             import transaction
  215.             transaction.get().commit()
  216.         except ImportError:
  217.             get_transaction().commit()
  218.  
  219.  
  220.     
  221.     def sync(self):
  222.         self.app._p_jar.sync()
  223.  
  224.     
  225.     def objectInfo(self, o):
  226.         Title = ''
  227.         t = getattr(o, 'Title', None)
  228.         if t:
  229.             Title = t()
  230.         
  231.         return {
  232.             'id': o.getId(),
  233.             'Title': Title,
  234.             'portal_type': getattr(o, 'portal_type', o.meta_type),
  235.             'folderish': o.isPrincipiaFolderish }
  236.  
  237.     
  238.     def cd(self, path):
  239.         if type(path) is not StringType:
  240.             path = '/'.join(path.getPhysicalPath())
  241.         
  242.         cwd = self.pwd()
  243.         x = cwd.unrestrictedTraverse(path)
  244.         if x is None:
  245.             raise KeyError("Can't cd to %s" % path)
  246.         x is None
  247.         print '%s -> %s' % (self.pwd().getId(), x.getId())
  248.         self._pwd = x
  249.  
  250.     
  251.     def ls(self, x = None):
  252.         if type(x) is StringType:
  253.             cwd = self.pwd()
  254.             x = cwd.unrestrictedTraverse(x)
  255.         
  256.         if x is None:
  257.             x = self.pwd()
  258.         
  259.         if x.isPrincipiaFolderish:
  260.             return [ self.objectInfo(o) for id, o in x.objectItems() ]
  261.         return self.objectInfo(x)
  262.  
  263.  
  264. zope_debug = None
  265.  
  266. def ipy_set_trace():
  267.     import IPython
  268.     IPython.Debugger.Pdb().set_trace()
  269.  
  270.  
  271. def main():
  272.     global zope_debug
  273.     ip = ipapi.get()
  274.     o = ip.options
  275.     SOFTWARE_HOME = os.environ.get('SOFTWARE_HOME')
  276.     sys.path.append(SOFTWARE_HOME)
  277.     print 'SOFTWARE_HOME=%s\n' % SOFTWARE_HOME
  278.     zope_debug = ZopeDebug()
  279.     import pdb
  280.     pdb.set_trace = ipy_set_trace
  281.     o.banner = 'ZOPE Py %s IPy %s\n' % (sys.version.split('\n')[0], Release.version)
  282.     print ','.join([] % [](_[1]))
  283.     ip.user_ns.update(zope_debug.namespace)
  284.  
  285. main()
  286.