Package pallavi :: Module pallavi
[hide private]
[frames] | no frames]

Source Code for Module pallavi.pallavi

  1  #!/usr/bin/env python 
  2   
  3  # Copyright (c) 2006-2007 Dusty Phillips 
  4   
  5  # Permission is hereby granted, free of charge, to any person obtaining a 
  6  # copy of this software and associated documentation files (the "Software"), 
  7  # to deal in the Software without restriction, including without limitation 
  8  # the rights to use, copy, modify, merge, publish, distribute, sublicense, 
  9  # and/or sell copies of the Software, and to permit persons to whom the 
 10  # Software is furnished to do so, subject to the following conditions: 
 11   
 12  # The above copyright notice and this permission notice shall be included in 
 13  # all copies or substantial portions of the Software. 
 14   
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 18  # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 20  # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 21  # DEALINGS IN THE SOFTWARE. 
 22   
 23  '''This module provides the RunPallavi function, which can be used to invoke 
 24   Pallavi views from inside the Python interpreter. Typically this is the main 
 25  entry point for the editor, and is run from the command line. It also provides 
 26  the class Pallavi, which holds references to the views, eventbus, etc 
 27   
 28  This module provides the following actions: 
 29  NewView 
 30          Open a new Pallavi edit window 
 31  CloseView 
 32          Close the current Pallavi edit window. If it is the only window open, terminate 
 33  ToggleDockPane 
 34          Toggle whether or not a given dockpane is currently visible. If it is not shown 
 35          it will be shown. If it is visible and attached to the currently focused view, 
 36          it will be closed. If it is visible but attached to a different view, it will 
 37          be attached to the currently focused view. 
 38           
 39  This module issues the following events: 
 40  ViewClosed 
 41          When a view is closed 
 42  ViewCreated 
 43          When a new view is created''' 
 44   
 45  import sys, os, os.path, shutil 
 46  import wx 
 47  import PluginManager, Views, EventActionManager, siteconfig 
 48   
 49   
50 -class Pallavi(object):
51 __nextUntitled = 0 52 views=[] 53 eventBus = EventActionManager.EventBus() 54 actions = EventActionManager.ActionList() 55 dockWindows = {} 56
57 - def RunPallavi(self, args):
58 """Begin running Pallavi. args is a list of command-line arguments 59 to be passed to the programm""" 60 61 self.actions.AddAction(EventActionManager.Action("NewView", self.NewView, "New View", "Open a New Pallavi Window.")) 62 self.actions.AddAction(EventActionManager.Action("CloseView", self.CloseView, "Close View", "Close the focused Window.")) 63 self.actions.AddAction(EventActionManager.Action("ToggleDockPane", self.ToggleDockPane, "Toggle Dock Pane", "Toggle whether a specified dock pane is visible in the current view." )) 64 app = wx.App(False) 65 app.SetAppName("pallavi") 66 app.Bind(wx.EVT_IDLE, self.eventBus.idle) 67 68 if len(args) > 1 and (args[0] == "-c" or args[0] == "--config_file"): 69 config_dir = args[1] 70 else: 71 config_dir = wx.StandardPaths.Get().GetUserDataDir() 72 config_dir = os.path.abspath(config_dir) 73 config_skeleton = os.path.join(wx.StandardPaths.Get().GetDataDir(), "pallavi_config.py") 74 if not os.path.exists(config_skeleton): 75 config_skeleton = os.path.join(sys.prefix, "/share/pallavi/pallavi_config.py") 76 77 78 if not os.path.exists(config_dir): 79 os.makedirs(config_dir + "/plugins") 80 os.makedirs(config_dir + "/lexing_languages") 81 82 if os.path.exists(config_skeleton): 83 shutil.copy(config_skeleton, config_dir) 84 else: 85 print "The file pallavi_config.py does not exist in ", config_dir 86 print "and I am unable to find the default configuration file." 87 print "(I looked in ", config_skeleton, ")" 88 print "Please create a valid pallavi_config.py in ", config_dir 89 sys.exit(1) 90 91 sys.path.append(config_dir) 92 import pallavi_config 93 sys.path.remove(config_dir) 94 sys.path.append(os.path.join(config_dir, 'plugins')) 95 96 # These variables may be needed by external plugins 97 pallavi_config.config_dir = config_dir 98 pallavi_config.share = os.path.dirname(config_skeleton) 99 pallavi_config.sysargs = args 100 self.config = pallavi_config 101 102 self.NewView() 103 self.pluginManager = PluginManager.PluginManager(self) 104 self.pluginManager.LoadDefaultPlugins(pallavi_config.default_plugins) 105 self.pluginManager.LoadPlugins(pallavi_config.plugins) 106 107 app.MainLoop()
108
109 - def ViewFocused(self, event):
110 '''called whenever a view is focused''' 111 if event.Active: 112 self.focusedView = event.EventObject
113
114 - def ViewClosed(self, event):
115 '''callback when a view is closed; cleans up after it and exits if no views remain''' 116 self.views.remove(event.EventObject) 117 if len(self.views) == 0: 118 sys.exit(0) 119 event.Skip()
120
121 - def NewView(self, data=None):
122 '''Opens a brand now edit window and focuses it''' 123 self.focusedView = Views.View(self) 124 self.focusedView.Bind(wx.EVT_ACTIVATE, self.ViewFocused) 125 self.focusedView.Bind(wx.EVT_CLOSE, self.ViewClosed) 126 self.views.append(self.focusedView) 127 self.eventBus.QueueEvent("ViewCreated", self.focusedView)
128
129 - def CloseView(self, data=None):
130 '''Closes the currently focused view''' 131 for textview in self.focusedView.textViews: 132 self.focusView.DeleteTextView(textview) 133 self.focusedView.Close() 134 self.eventBus.QueueEvent("ViewClosed", self.focusedView)
135
136 - def AddDockPane(self, panel, caption):
137 '''Add a panel to the list of dockpanes. Looks up the location 138 for the dockpane in the configuration file and adds it to the 139 currently focused view.''' 140 if caption in self.config.dock_locations and self.config.dock_locations[caption] != None: 141 panetype = self.config.dock_locations[caption].upper() 142 else: panetype = None 143 floatingpaneinfo = wx.aui.AuiPaneInfo().Caption(caption).Float().FloatingPosition((100,100)).FloatingSize((400,400)).BestSize((200,200)) 144 paneinfo = { 145 "LEFT": wx.LEFT, 146 "RIGHT": wx.RIGHT, 147 "TOP": wx.TOP, 148 "BOTTOM": wx.BOTTOM, 149 "FLOAT": floatingpaneinfo, 150 "NONE": floatingpaneinfo.Hide(), 151 None: floatingpaneinfo.Hide() 152 }[panetype] 153 self.focusedView.dockmanager.AddPane(panel, paneinfo, caption) 154 self.dockWindows[caption] = panel 155 self.focusedView.dockmanager.Update()
156
157 - def ToggleDockPane(self, caption=None):
158 '''Action to toggle the visibility of a docking pane by caption. If 159 it is currently visible and attached to the focused view, it will 160 be hidden. Otherwise, it will be attached to the current view and shown.''' 161 dockpane = self.dockWindows[caption] 162 parent = dockpane.Parent 163 paneinfo = parent.dockmanager.GetPane(self.dockWindows[caption]) 164 if parent == self.focusedView and paneinfo.IsShown(): 165 paneinfo.Hide() 166 elif parent != self.focusedView: 167 dockpane.Reparent(self.focusedView) 168 parent.dockmanager.DetachPane(dockpane) 169 if paneinfo.IsFloating(): 170 newinfo = wx.aui.AuiPaneInfo().Caption(caption).Float().FloatingPosition((100,100)).FloatingSize((400,400)).BestSize((200,200)) 171 else: 172 direction = paneinfo.dock_direction 173 newinfo = wx.aui.AuiPaneInfo().Direction(direction) 174 newinfo = newinfo.Caption(caption) 175 self.focusedView.dockmanager.AddPane(dockpane, newinfo, caption) 176 else: 177 paneinfo.Show() 178 self.focusedView.dockmanager.Update() 179 parent.dockmanager.Update()
180
181 - def nextUntitled(self):
182 '''Return an ID for the next untitled filename for this view''' 183 self.__nextUntitled += 1 184 return "Untitled-%d" % self.__nextUntitled
185 186 if __name__ == "__main__": 187 pallavi = Pallavi() 188 pallavi.RunPallavi(sys.argv[1:]) 189