Перейти до вмісту

Користувач:Pavlo Shevelo/Forge

Матеріал з Вікіджерел

Пайтон

[ред.]

Hello, world

[ред.]

<python> >>> a = range(1,10000000) >>> del a

class c:

  def p(self, x):
     print x

def fun(x):

  print x

>>> fun("Hello, %s" % c().p("world")) </python>

Контролер TurboGears навігатора LDAP каталогів

[ред.]

<python> import logging

import cherrypy

import turbogears from turbogears import controllers, expose, validate, redirect

from ldapnav import json

import ldap

log = logging.getLogger("ldapnav.controllers")

class Root(controllers.RootController):

   """Контролер для навігатора LDAP каталогів
   """
   login_dn = "domain\\user"
   login_pw = 'password'
   host = "192.168.1.6"


   def __connect(self):
       l = ldap.open(self.host)
       l.simple_bind_s(self.login_dn, self.login_pw)
       return l


   def __disconnect(self, conn):
       conn.unbind()
   @expose(template="ldapnav.templates.index")
   def index(self):
       return self.go()
   @expose(template="ldapnav.templates.index")
   def go(self, entry="OU=Unit, DC=company, DC=com"):
       def stackLinks(prevLink, remPath):
           """Builds a list of linkName,linkHref tuples corresponding
           to the elements in the path tree.
           """
           ## Рекурсивно проходить весь список
           if len(remPath)>1:
               item = remPath.pop()
               currentName = (item.split('=',2))[1]
              item = remPath.pop()
               currentName = (item.split('=',2))[1]
               currentLink = item + prevLink
               return (currentName, currentLink
                       + stackLinks(',' + currentLink, remPath))
           else:
               item = remPath.pop()
               currentName = (item.split('=',2))[1]
               currentLink = item + prevLink
               return currentName, currentLink
           pass


       conn = self.__connect()
       res = conn.search_s(entry,
                           ldap.SCOPE_ONELEVEL,
                           "objectclass=*",)
       current = (conn.search_s(entry, ldap.SCOPE_BASE,
                               "objectclass=*",))[0]
       self.__disconnect(conn)
       pathToCurrent = ldap.explode_dn(current[0])
       pathToCurrentLinks = stackLinks(, pathToCurrent)
       return dict(entries=res, currentDN=current, pathDN=pathToCurrentLinks)

</python>