browseProjects.py :  » Windows » pyExcelerator » pywin32-214 » pythonwin » pywin » tools » 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 » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » pythonwin » pywin » tools » browseProjects.py
import regutil, os
import hierlist
import win32con, win32ui, win32api
import commctrl
from pywin.mfc import dialog
import glob
import pyclbr
import pywin.framework.scriptutils
import afxres

class HLIErrorItem(hierlist.HierListItem):
  def __init__(self, text):
    self.text = text
    hierlist.HierListItem.__init__(self)
  def GetText(self):
    return self.text

class HLICLBRItem(hierlist.HierListItem):
  def __init__(self, name, file, lineno, suffix = ""):
    # If the 'name' object itself has a .name, use it.  Not sure
    # how this happens, but seems pyclbr related. 
    # See PyWin32 bug 817035
    self.name = getattr(name, "name", name)
    self.file = file
    self.lineno = lineno
    self.suffix = suffix
  def __lt__(self, other):
    return self.name < other.name
  def __eq__(self, other):
    return self.name == other.name
  def GetText(self):
    return self.name + self.suffix
  def TakeDefaultAction(self):
    if self.file:
      pywin.framework.scriptutils.JumpToDocument(self.file, self.lineno, bScrollToTop=1)
    else:
      win32ui.SetStatusText("The source of this object is unknown")
  def PerformItemSelected(self):
    if self.file is None:
      msg = "%s - source can not be located." % (self.name, )
    else:
      msg = "%s defined at line %d of %s" % (self.name, self.lineno, self.file)
    win32ui.SetStatusText(msg)

class HLICLBRClass(HLICLBRItem):
  def __init__(self, clbrclass, suffix = ""):
    try:
      name = clbrclass.name
      file = clbrclass.file
      lineno = clbrclass.lineno
      self.super = clbrclass.super
      self.methods = clbrclass.methods
    except AttributeError:
      name = clbrclass
      file = lineno = None
      self.super = []; self.methods = {}
    HLICLBRItem.__init__(self, name, file, lineno, suffix)
  def GetSubList(self):
     ret = []
     for c in self.super:
       ret.append(HLICLBRClass(c, " (Parent class)"))
     for meth, lineno in self.methods.iteritems():
       ret.append(HLICLBRMethod(meth, self.file, lineno, " (method)"))
     return ret
  def IsExpandable(self):
    return len(self.methods) + len(self.super)
  def GetBitmapColumn(self):
    return 21

class HLICLBRFunction(HLICLBRClass):
  def GetBitmapColumn(self):
    return 22
class HLICLBRMethod(HLICLBRItem):
  def GetBitmapColumn(self):
    return 22

class HLIModuleItem(hierlist.HierListItem):
  def __init__(self, path):
    hierlist.HierListItem.__init__(self)
    self.path = path
  def GetText(self):
    return os.path.split(self.path)[1] + " (module)"
  def IsExpandable(self):
    return 1
  def TakeDefaultAction(self):
    win32ui.GetApp().OpenDocumentFile( self.path )
  def GetBitmapColumn(self):
    col = 4 # Default
    try:
      if win32api.GetFileAttributes(self.path) & win32con.FILE_ATTRIBUTE_READONLY:
        col = 5
    except win32api.error:
      pass
    return col
  def GetSubList(self):
    mod, path = pywin.framework.scriptutils.GetPackageModuleName(self.path)
    win32ui.SetStatusText("Building class list - please wait...", 1)
    win32ui.DoWaitCursor(1)
    try:
      try:
        reader = pyclbr.readmodule_ex # Post 1.5.2 interface.
        extra_msg = " or functions"
      except AttributeError:
        reader = pyclbr.readmodule
        extra_msg = ""
      data = reader(mod, [path])
      if data:
        ret = []
        for item in data.itervalues():
          if item.__class__ != pyclbr.Class: # ie, it is a pyclbr Function instance (only introduced post 1.5.2)
            ret.append(HLICLBRFunction( item, " (function)" ) )
          else:
            ret.append(HLICLBRClass( item, " (class)") )
        ret.sort()
        return ret
      else:
        return [HLIErrorItem("No Python classes%s in module." % (extra_msg,))]
    finally:
      win32ui.DoWaitCursor(0)
      win32ui.SetStatusText(win32ui.LoadString(afxres.AFX_IDS_IDLEMESSAGE))

def MakePathSubList(path):
  ret = []
  for filename in glob.glob(os.path.join(path,'*')):
    if os.path.isdir(filename) and os.path.isfile(os.path.join(filename, "__init__.py")):
      ret.append(HLIDirectoryItem(filename, os.path.split(filename)[1]))
    else:
      if os.path.splitext(filename)[1].lower() in ['.py', '.pyw']:
        ret.append(HLIModuleItem(filename))
  return ret

class HLIDirectoryItem(hierlist.HierListItem):
  def __init__(self, path, displayName = None, bSubDirs = 0):
    hierlist.HierListItem.__init__(self)
    self.path = path
    self.bSubDirs = bSubDirs
    if displayName:
      self.displayName = displayName
    else:
      self.displayName = path
  def IsExpandable(self):
    return 1
  def GetText(self):
    return self.displayName

  def GetSubList(self):
    ret = MakePathSubList(self.path)
    if os.path.split(self.path)[1] == "win32com": # Complete and utter hack for win32com.
      try:
        path = win32api.GetFullPathName(os.path.join(self.path, "..\\win32comext"))
        ret = ret + MakePathSubList(path)
      except win32ui.error:
        pass
    return ret

class HLIProjectRoot(hierlist.HierListItem):
  def __init__(self, projectName, displayName = None):
    hierlist.HierListItem.__init__(self)
    self.projectName = projectName
    self.displayName = displayName or projectName
  def GetText(self):
    return self.displayName
  def IsExpandable(self):
    return 1
  def GetSubList(self):
    paths = regutil.GetRegisteredNamedPath(self.projectName)
    pathList = paths.split(";")
    if len(pathList)==1: # Single dir - dont bother putting the dir in
      ret = MakePathSubList(pathList[0])
    else:
      ret = list(map( HLIDirectoryItem, pathList ))
    return ret

class HLIRoot(hierlist.HierListItem):
  def __init__(self):
    hierlist.HierListItem.__init__(self)
  def IsExpandable(self):
    return 1
  def GetSubList(self):
    keyStr = regutil.BuildDefaultPythonKey() + "\\PythonPath"
    hKey = win32api.RegOpenKey(regutil.GetRootKey(), keyStr)
    try:
      ret = []
      ret.append(HLIProjectRoot("", "Standard Python Library")) # The core path.
      index = 0
      while 1:
        try:
          ret.append(HLIProjectRoot(win32api.RegEnumKey(hKey, index)))
          index = index + 1
        except win32api.error:
          break
      return ret
    finally:
      win32api.RegCloseKey(hKey)

class dynamic_browser (dialog.Dialog):
    style = win32con.WS_OVERLAPPEDWINDOW | win32con.WS_VISIBLE
    cs = (
        win32con.WS_CHILD           |
        win32con.WS_VISIBLE         |
        commctrl.TVS_HASLINES       |
        commctrl.TVS_LINESATROOT    |
        commctrl.TVS_HASBUTTONS
        )

    dt = [
        ["Python Projects", (0, 0, 200, 200), style, None, (8, "MS Sans Serif")],
        ["SysTreeView32", None, win32ui.IDC_LIST1, (0, 0, 200, 200), cs]
        ]

    def __init__ (self, hli_root):
        dialog.Dialog.__init__ (self, self.dt)
        self.hier_list = hierlist.HierListWithItems (
            hli_root,
            win32ui.IDB_BROWSER_HIER
            )
        self.HookMessage (self.on_size, win32con.WM_SIZE)

    def OnInitDialog (self):
        self.hier_list.HierInit (self)
        return dialog.Dialog.OnInitDialog (self)

    def on_size (self, params):
        lparam = params[3]
        w = win32api.LOWORD(lparam)
        h = win32api.HIWORD(lparam)
        self.GetDlgItem (win32ui.IDC_LIST1).MoveWindow((0,0,w,h))

def BrowseDialog():
    root = HLIRoot()
    if not root.IsExpandable():
        raise TypeError("Browse() argument must have __dict__ attribute, or be a Browser supported type")
        
    dlg = dynamic_browser (root)
    dlg.CreateWindow()

def DockableBrowserCreator(parent):
  root = HLIRoot()
  hl = hierlist.HierListWithItems (
            root,
            win32ui.IDB_BROWSER_HIER
            )

  style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | commctrl.TVS_HASLINES | commctrl.TVS_LINESATROOT | commctrl.TVS_HASBUTTONS

  control = win32ui.CreateTreeCtrl()
  control.CreateWindow(style, (0, 0, 150, 300), parent, win32ui.IDC_LIST1)
  list = hl.HierInit (parent, control)
  return control

def DockablePathBrowser():
  import pywin.docking.DockingBar
  bar = pywin.docking.DockingBar.DockingBar()
  bar.CreateWindow(win32ui.GetMainFrame(), DockableBrowserCreator, "Path Browser", 0x8e0a)
  bar.SetBarStyle( bar.GetBarStyle()|afxres.CBRS_TOOLTIPS|afxres.CBRS_FLYBY|afxres.CBRS_SIZE_DYNAMIC)
  bar.EnableDocking(afxres.CBRS_ALIGN_ANY)
  win32ui.GetMainFrame().DockControlBar(bar)

# The "default" entry point
Browse = DockablePathBrowser
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.