[Pydev-cvs] awb/src/Python .cvsignore,NONE,1.1 AWB.bat,NONE,1.1 AWB.log,NONE,1.1 AWB.py,NONE,1.1 AWB
Brought to you by:
fabioz
Update of /cvsroot/pydev/awb/src/Python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30772/src/Python Added Files: .cvsignore AWB.bat AWB.log AWB.py AWBcontroller.bat About.py AgentDataCollector.py ArtHNA.xml CS03.py CSMARTer.bat CSMARTer.log Cecom.bat __init__.py agentCanvas.py agentCanvas.pyc agentController.py agentController.pyc agentLaydown.py agentLaydown.pyc agentsRoot.html awb.list.p cecomDLG.py cougaar_DragAndDrop.py cougaar_DragAndDrop.pyc csmarter_events.py csmarter_events.pyc debug.py editorTextControl.py editorTextControl.pyc encode_bitmaps.py eventFactory.py eventFactory.pyc facetProperties.py facetProperties.pyc gizmo.py gizmo.pyc gizmoImages.py gizmoImages.pyc globalConstants.py globalConstants.pyc hierarchy-small.xml hierarchy.xml images.py images.pyc informationPanel.py informationPanel.pyc insertion_dialog.py insertion_dialog.pyc mySociety.py probeDLG.py probeDLG.pyc run.py run.pyc screenScraper.py screenScraper.pyc screenScraperTest.py servletProperties.py servletProperties.pyc simple.rb simpleCanvas.py societyBuilder.py societyBuilder.pyc societyController.py societyController.pyc societyEditor.py societyEditor.pyc societyFactoryServer.py societyFactoryServer.pyc societyReader.py societyReader.pyc societyViewer.py societyViewer.pyc societyVisualModel.py societyVisualModel.pyc urlDlg.py urlDlg.pyc zoomer.py zoomer.pyc Log Message: wholesale commit --- NEW FILE: societyController.pyc --- (This appears to be a binary file; contents omitted.) --- NEW FILE: societyFactoryServer.pyc --- (This appears to be a binary file; contents omitted.) --- NEW FILE: urlDlg.py --- import sys import re import os from wxPython.wx import * from wxPython.help import * import images import pickle #--------------------------------------------------------------------------- URLSEQ = ['LEADER', 'HOST', 'COLON_SEP','PORT','HACK','DOLLAR','AGENT', 'REMAINDER'] URLITEMS = {'LEADER':'http://', 'HOST': None, 'COLON_SEP':':', 'PORT': None, 'HACK':'/', 'DOLLAR':'$', 'AGENT':None, 'REMAINDER':'/hierarchy?recurse=true&allRelationships=true&format=xml&Display=', } HOST_COMBOBOX_ID = 601 PORT_COMBOBOX_ID = 602 AGENT_COMBOBOX_ID = 603 URL_COMBOBOX_ID = 604 OBJECT_STORE = 'awb.list.p' class URLDlg(wxDialog): def OnSetFocus(self, evt): print "OnSetFocus" evt.Skip() def OnKillFocus(self, evt): print "OnKillFocus" evt.Skip() def GetBookmarkedData(self): try: self.URLcomponents = pickle.load(open(OBJECT_STORE)) print self.URLcomponents except IOError, why: self.log.WriteText("IO Error %s" % why) self.URLcomponents = URLcomponents() def __init__(self, parent, ID, log, title, pos=wxDefaultPosition, size=wxDefaultSize, style=wxDEFAULT_DIALOG_STYLE): pre = wxPreDialog() pre.Create(parent, ID, title, pos, size, style) self.this = pre.this self.parent = parent self.initParticles() self.log = log self.URLcomponents = URLcomponents() self.GetBookmarkedData() #~ ------------------------------ urlLbl = wxStaticText(self, -1, "Host") default = "enter/select host" if len(self.URLcomponents.hosts) > 0 : default = self.URLcomponents.hosts[0] URLITEMS['HOST'] = default self.cbHosts = wxComboBox(self, HOST_COMBOBOX_ID, default, wxPoint(-1,-1), wxSize(90, -1), # wxPoint(90, 50), wxSize(95, -1), self.URLcomponents.hosts, wxCB_DROPDOWN) EVT_TEXT(self, self.cbHosts.GetId(), self.EvtText) EVT_TEXT_ENTER(self, self.cbHosts.GetId(), self.EvtTextEnter) #~ EVT_CHAR(t1, self.EvtChar) #~ ------------------------------ portLbl = wxStaticText(self, -1, "Port") default = "enter/select port" if len(self.URLcomponents.ports) > 0 : default = self.URLcomponents.ports[0] URLITEMS['PORT'] = default self.cbPorts = wxComboBox(self, PORT_COMBOBOX_ID, default, wxPoint(-1,-1), wxSize(90, -1), # wxPoint(90, 50), wxSize(95, -1), self.URLcomponents.ports, wxCB_DROPDOWN) EVT_TEXT(self, self.cbPorts.GetId(), self.EvtText) EVT_TEXT_ENTER(self, self.cbPorts.GetId(), self.EvtTextEnter) #~ ------------------------------ agentLbl = wxStaticText(self, -1, "Agent") default = "enter/select agent" if len(self.URLcomponents.agents) > 0 : default = self.URLcomponents.agents[0] URLITEMS['AGENT'] = default self.cbAgents = wxComboBox(self, AGENT_COMBOBOX_ID, default, wxPoint(-1,-1), wxSize(90, -1), # wxPoint(90, 50), wxSize(95, -1), self.URLcomponents.agents, wxCB_DROPDOWN) EVT_TEXT(self, self.cbAgents .GetId(), self.EvtText) EVT_TEXT_ENTER(self, self.cbAgents .GetId(), self.EvtTextEnter) #~ ------------------------------ cbLbl = wxStaticText(self, -1, "UL Heirarchy Servlet", wxPoint(8, 10)) default = "" if len(self.URLcomponents.urls) is 0: default = "No URLs Bookmarked" else: default = self.urlcomponents.urls[0] cb = wxComboBox(self, URL_COMBOBOX_ID , default, wxPoint(10, 50), wxSize(300, -1), self.URLcomponents.urls, wxCB_DROPDOWN) EVT_COMBOBOX(self,URL_COMBOBOX_ID , self.EvtComboBox) EVT_TEXT(self, URL_COMBOBOX_ID , self.EvtText) EVT_TEXT_ENTER(self, URL_COMBOBOX_ID , self.EvtTextEnter) EVT_SET_FOCUS(cb, self.OnSetFocus) EVT_KILL_FOCUS(cb, self.OnKillFocus) #~ ------------------------------ self.bg_bmp = images.getGridBGBitmap() EVT_ERASE_BACKGROUND(self, self.OnEraseBackground) bOk = wxButton(self, wxID_OK, "OK") bOk.SetDefault() EVT_BUTTON(self, bOk.GetId(), self.OnOk) bCan = wxButton(self, wxID_CANCEL, "Cancel") EVT_BUTTON(self, bCan.GetId(), self.OnCancel) bsizer = wxBoxSizer(wxHORIZONTAL) bsizer.Add(bOk, 0, wxGROW|wxALL, 4) bsizer.Add(bCan, 0, wxGROW|wxALL, 4) sizer = wxFlexGridSizer(cols=3, hgap=6, vgap=6) sizer.AddMany([urlLbl, self.cbHosts, (0,0), portLbl, self.cbPorts, (0,0), agentLbl, self.cbAgents, (0,0), cbLbl, cb, (0,0), (0,0),bsizer,(0,0), ]) border = wxBoxSizer(wxVERTICAL) border.Add(sizer, 0, wxALL, 25) self.SetSizer(border) self.SetAutoLayout(True) # ---------------------------------------------- def initParticles(self): self.url = "" self.port = "" self.agent = "" def OnOk(self, evt): rtn = self.formURLStrings() if rtn is True: self.SetReturnCode(wxID_OK) self.Destroy() def OnCancel(self, evt): self.SetReturnCode(wxID_CANCEL) self.Destroy() def OnEraseBackground(self, evt): dc = evt.GetDC() if not dc: dc = wxClientDC(self.GetClientWindow()) # tile the background bitmap sz = self.GetClientSize() w = self.bg_bmp.GetWidth() h = self.bg_bmp.GetHeight() x = 0 while x < sz.width: y = 0 while y < sz.height: dc.DrawBitmap(self.bg_bmp, x, y) y = y + h x = x + w def EvtComboBox(self, evt): cb = evt.GetEventObject() #~ data = cb.GetClientData(cb.GetSelection()) #~ self.log.WriteText('EvtComboBox: %s\nClientData: %s\n' % (evt.GetString(), data)) def EvtText(self, evt): #~ self.log.WriteText('EvtText: %s\n' % evt.GetString()) if evt.GetId() == HOST_COMBOBOX_ID:URLITEMS['HOST'] = evt.GetString() if evt.GetId() == PORT_COMBOBOX_ID:URLITEMS['PORT'] = evt.GetString() if evt.GetId() == AGENT_COMBOBOX_ID:URLITEMS['AGENT'] = evt.GetString() def EvtTextEnter(self, evt): self.log.WriteText('EvtTextEnter: %s, %s, %s\n' % (evt.GetString(), evt.GetEventObject(), evt.GetId())) obj = evt.GetEventObject() self.log.WriteText("EvtTextEnter:%s\n %s, %s" % (dir(obj),obj.GetLabel(), obj.GetName())) def EvtChar(self, event): self.log.WriteText('EvtChar: %d\n' % event.GetKeyCode()) event.Skip() def formURLStrings(self): done = True for i in URLSEQ: if URLITEMS[i] is None or URLITEMS[i] is "": dlg = wxMessageDialog(self.parent, 'Need a value for '+str(i), 'No!', wxOK | wxICON_INFORMATION) #wxYES_NO | wxNO_DEFAULT | wxCANCEL | wxICON_INFORMATION) dlg.ShowModal() done = False if done: s = "" for i in URLSEQ: s += URLITEMS[i] self.log.WriteText("formURLStrings:%s" % (s)) if URLITEMS['HOST'] not in self.URLcomponents.hosts: self.URLcomponents.hosts.append( str(URLITEMS['HOST']) ) if URLITEMS['PORT'] not in self.URLcomponents.ports: self.URLcomponents.ports.append( str(URLITEMS['PORT']) ) if URLITEMS['AGENT'] not in self.URLcomponents.agents: self.URLcomponents.agents.append( str(URLITEMS['AGENT']) ) pickle.dump(self.URLcomponents,open(OBJECT_STORE,'w')) self.parent.URL = s return done class URLcomponents: def __init__(self): self.hosts = [] self.ports = [] self.agents = [] self.urls = [] def __str__(self): s0 = "URL COMPONENTS\n" s1 = "Hosts\n" for i in self.hosts: s1 += str(i)+"\n" s1 += '\n' s2 = "Ports\n" for i in self.ports: s2 += str(i)+"\n" s2 += '\n' s3 = "Agents\n" for i in self.agents: s3 += str(i)+"\n" s3 += '\n' return s0+s1 + s2 + s3 #--------------------------------------------------------------------------- def runTest(frame, nb, log): win = URLDlg(nb, log) return win if __name__ == '__main__': import sys,os import run run.main(['', os.path.basename(sys.argv[0])]) --- NEW FILE: gizmo.py --- import threading, os ##os.putenv('LANG', 'C') # for running on GTK2 from wxPython.wx import * # ------------------------------------------------------------------------------ wxEVT_UPDATE_GIZMO = wxNewEventType() def EVT_UPDATE_GIZMO(win, func): win.Connect(-1, -1, wxEVT_UPDATE_GIZMO, func) class UpdateGizmoEvent(wxPyEvent): def __init__(self): wxPyEvent.__init__(self) self.SetEventType(wxEVT_UPDATE_GIZMO) # ------------------------------------------------------------------------------ class Gizmo(wxPanel): """ The first argument is either the name of a file that will be split into frames (a composite image) or a list of strings of image names that will be treated as individual frames. If a single (composite) image is given, then additional information must be provided: the number of frames in the image and the width of each frame. The first frame is treated as the "at rest" frame (it is not shown during animation, but only when GIZMO.Rest() is called. A second, single image may be optionally specified to overlay on top of the animation. A label may also be specified to show on top of the animation. """ def __init__(self, parent, id, bitmap, # single (composite) bitmap or list of bitmaps pos = (554,316), #wxDefaultPosition, size = wxDefaultSize, frameDelay = 0.1,# time between frames frames = 0, # number of frames (only necessary for composite image) frameWidth = 0, # width of each frame (only necessary for composite image) label = None, # optional text to be displayed overlay = None, # optional image to overlay on animation reverse = 0, # reverse direction at end of animation style = 0, # window style name = "gizmo"): wxPanel.__init__(self, parent, id, pos, size, style, name) self.name = name self.label = label _seqTypes = (type([]), type(())) # set size, guessing if necessary width, height = size if width == -1: if type(bitmap) in _seqTypes: width = bitmap[0].GetWidth() else: if frameWidth: width = frameWidth if height == -1: if type(bitmap) in _seqTypes: height = bitmap[0].GetHeight() else: height = bitmap.GetHeight() self.width, self.height = width, height # double check it assert width != -1 and height != -1, "Unable to guess size" if label: extentX, extentY = self.GetTextExtent(label) self.labelX = (width - extentX)/2 self.labelY = (height - extentY)/2 self.frameDelay = frameDelay self.current = 0 self.direction = 1 self.autoReverse = reverse self.overlay = overlay if overlay is not None: self.overlay = overlay self.overlayX = (width - self.overlay.GetWidth()) / 2 self.overlayY = (height - self.overlay.GetHeight()) / 2 self.showOverlay = overlay is not None self.showLabel = label is not None # do we have a sequence of images? if type(bitmap) in _seqTypes: self.submaps = bitmap self.frames = len(self.submaps) # or a composite image that needs to be split? else: self.frames = frames self.submaps = [] for chunk in range(frames): rect = (chunk * frameWidth, 0, width, height) self.submaps.append(bitmap.GetSubBitmap(rect)) # self.sequence can be changed, but it's not recommended doing it # while the gizmo is running. self.sequence[0] should always # refer to whatever frame is to be shown when 'resting' and be sure # that no item in self.sequence >= self.frames or < 0!!! self.sequence = range(self.frames) self.SetClientSize((width, height)) EVT_PAINT(self, self.OnPaint) EVT_UPDATE_GIZMO(self, self.Rotate) EVT_WINDOW_DESTROY(self, self.OnDestroyWindow) self.event = threading.Event() self.event.set() # we start out in the "resting" state def __del__(self): # make sure it's stopped, since EVT_WINDOW_DESTROY may not be sent # on all platforms self.Stop() def OnDestroyWindow(self, event): # this is currently broken due to a bug in wxWindows... hopefully # it'll be fixed soon. Meanwhile be sure to explicitly call Stop() # before the gizmo is destroyed. self.Stop() event.Skip() def Draw(self, dc): dc.DrawBitmap(self.submaps[self.sequence[self.current]], 0, 0, true) if self.overlay and self.showOverlay: dc.DrawBitmap(self.overlay, self.overlayX, self.overlayY, true) if self.label and self.showLabel: dc.DrawText(self.label, self.labelX, self.labelY) dc.SetTextForeground(wxWHITE) dc.DrawText(self.label, self.labelX-1, self.labelY-1) def OnPaint(self, event): self.Draw(wxPaintDC(self)) event.Skip() def UpdateThread(self): try: while hasattr(self, 'event') and not self.event.isSet(): wxPostEvent(self, UpdateGizmoEvent()) self.event.wait(self.frameDelay) except wxPyDeadObjectError: # BUG: we were destroyed print "Got wxPyDeadObjectError" # prg debug self.Rest() #~ return except Exception, args: import traceback traceback.print_exc() self.Rest() def Rotate(self, event): if self.event.isSet(): return self.current += self.direction if self.current >= len(self.sequence): if self.autoReverse: self.Reverse() self.current = len(self.sequence) - 1 else: self.current = 1 if self.current < 1: if self.autoReverse: self.Reverse() self.current = 1 else: self.current = len(self.sequence) - 1 self.Draw(wxClientDC(self)) # --------- public methods --------- def SetFont(self, font): """Set the font for the label""" wxPanel.SetFont(self, font) self.SetLabel(self.label) self.Draw(wxClientDC(self)) def Rest(self): """Stop the animation and return to frame 0""" self.Stop() self.current = 0 self.Draw(wxClientDC(self)) def Reverse(self): """Change the direction of the animation""" self.direction = -self.direction def Running(self): """Returns true if the animation is running""" return not self.event.isSet() def Start(self): """Start the animation""" if not self.Running(): self.event.clear() thread = threading.Thread(target = self.UpdateThread, name = "%s-thread" % self.name) thread.start() def Stop(self): """Stop the animation""" if self.event.isSet(): return self.event.set() def SetFrameDelay(self, frameDelay = 0.05): """Delay between each frame""" self.frameDelay = frameDelay def ToggleOverlay(self, state = None): """Toggle the overlay image""" if state is None: self.showOverlay = not self.showOverlay else: self.showOverlay = state self.Draw(wxClientDC(self)) def ToggleLabel(self, state = None): """Toggle the label""" if state is None: self.showLabel = not self.showLabel else: self.showLabel = state self.Draw(wxClientDC(self)) def SetLabel(self, label): """Change the text of the label""" self.label = label if label: extentX, extentY = self.GetTextExtent(label) self.labelX = (self.width - extentX)/2 self.labelY = (self.height - extentY)/2 self.Draw(wxClientDC(self)) # ------------------------------------------------------------------------------ --- NEW FILE: csmarter_events.py --- #!/bin/env python #---------------------------------------------------------------------------- # Name: # Purpose: # # Author: ISAT (D. Moore/P. Gardella) # # RCS-ID: $Id: csmarter_events.py,v 1.1 2004/08/25 21:03:17 dana_virtual Exp $ # <copyright> # Copyright 2002 BBN Technologies, LLC # under sponsorship of the Defense Advanced Research Projects Agency (DARPA). # # This program is free software; you can redistribute it and/or modify # it under the terms of the Cougaar Open Source License as published by # DARPA on the Cougaar Open Source Website (www.cougaar.org). # # THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS # PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR # IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT # ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT # HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS, # TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR # PERFORMANCE OF THE COUGAAR SOFTWARE. # </copyright> # from wxPython.wx import * from wxPython import events wxEVT_UPDATE_SOCIETY = wxNewEventType() def EVT_UPDATE_SOCIETY(win, func): win.Connect(-1, -1, wxEVT_UPDATE_SOCIETY, func) #---------------------------------------------------------------------- class UpdateSocietyEvent(wxPyEvent): def __init__(self, msg): wxPyEvent.__init__(self) self.SetEventType(wxEVT_UPDATE_SOCIETY) self.msg = msg #---------------------------------------------------------------------- wxEVT_SOCIETYCONTROLLER_TEST = wxNewEventType() def EVT_SOCIETYCONTROLLER_TEST(win, func): win.Connect(-1, -1, wxEVT_SOCIETYCONTROLLER_TEST, func) #---------------------------------------------------------------------- class SocietyControllerEvent(wxPyEvent): def __init__(self, msg): wxPyEvent.__init__(self) self.SetEventType(wxEVT_SOCIETYCONTROLLER_TEST) self.msg = msg #---------------------------------------------------------------------- #---------------------------------------------------------------------- wxEVT_AGENT_TASK_COUNT = wxNewEventType() def EVT_AGENT_TASK_COUNT(win, func): win.Connect(-1, -1, wxEVT_AGENT_TASK_COUNT, func) #---------------------------------------------------------------------- class AgentTaskCountEvent(wxPyEvent): def __init__(self, msg): wxPyEvent.__init__(self) self.SetEventType(wxEVT_AGENT_TASK_COUNT) self.msg = msg --- NEW FILE: AWB.bat --- @echo off REM CSMARTer.bat REM Set the initial tabbed pane to be displayed REM 0 is the Overview pane REM 1 is the Rule Editor REM 2 is the Society Editor REM 3 is the Agent Laydown set INITIAL_PANE="3" python AWB.py %INITIAL_PANE% --- NEW FILE: urlDlg.pyc --- (This appears to be a binary file; contents omitted.) --- NEW FILE: societyViewer.pyc --- (This appears to be a binary file; contents omitted.) --- NEW FILE: cougaar_DragAndDrop.pyc --- (This appears to be a binary file; contents omitted.) --- NEW FILE: hierarchy.xml --- <?xml version='1.0' ?> <Hierarchy RootID="OSD.GOV"> <Org> <OrgID>OSD.GOV</OrgID> <Name>OSD.GOV</Name> <Rel OrgID="DLAHQ.MIL" Rel="Subordinate"/> <Rel OrgID="TRANSCOM-20.TRANSCOM.MIL" Rel="SupportSubordinate"/> <Rel OrgID="FORSCOM.MIL" Rel="Subordinate"/> <Rel OrgID="TRANSCOM-20.TRANSCOM.MIL" Rel="Subordinate"/> <Rel OrgID="DLAHQ.MIL" Rel="SupportSubordinate"/> <Rel OrgID="OSC.MIL" Rel="SupportSubordinate"/> <Rel OrgID="AWR-2.21-TSC.ARMY.MIL" Rel="SupportSubordinate"/> <Rel OrgID="HNS.MIL" Rel="Subordinate"/> <Rel OrgID="USEUCOM.MIL" Rel="Subordinate"/> <Rel OrgID="HNS.MIL" Rel="SupportSubordinate"/> <Rel OrgID="OSC.MIL" Rel="Subordinate"/> <Rel OrgID="NATO.GOV" Rel="Subordinate"/> </Org> <Org> [...11872 lines suppressed...] <OrgID>AmmoShipPacker.TRANSCOM.MIL</OrgID> <Name>AmmoShipPacker.TRANSCOM.MIL</Name> <Rel OrgID="AmmoSea.TRANSCOM.MIL" Rel="ConverseOfShipPackingTransportationProvider"/> <Rel OrgID="AmmoSea.TRANSCOM.MIL" Rel="Superior"/> </Org> <Org> <OrgID>AmmoConusGround.TRANSCOM.MIL</OrgID> <Name>AmmoConusGround.TRANSCOM.MIL</Name> <Rel OrgID="AmmoSea.TRANSCOM.MIL" Rel="ConverseOfToPOEGroundTransportationProvider"/> <Rel OrgID="AmmoSea.TRANSCOM.MIL" Rel="Superior"/> </Org> <Org> <OrgID>DLAHQ.MIL</OrgID> <Name>DLAHQ.MIL</Name> <Rel OrgID="OSD.GOV" Rel="SupportSuperior"/> <Rel OrgID="343-SUPPLYCO.29-SPTGP.21-TSC.ARMY.MIL" Rel="SubsistenceSupplyCustomer"/> <Rel OrgID="200-MMC.21-TSC.ARMY.MIL" Rel="SparePartsCustomer"/> <Rel OrgID="OSD.GOV" Rel="Superior"/> </Org> </Hierarchy> --- NEW FILE: insertion_dialog.pyc --- (This appears to be a binary file; contents omitted.) --- NEW FILE: societyEditor.pyc --- (This appears to be a binary file; contents omitted.) --- NEW FILE: mySociety.py --- import sys import os from wxPython.wx import * from wxPython.ogl import * #--------------------------------------------------------------------------- # The class which is a representation of the Facets in the society class myFacet: # Init for a Facet myFacetName = "noname" myFacetParent = "noparent" myFacetType = "SOURCEFACET" myFacetLevel = "0" myFacetPen = wxBLACK_PEN myFacetBrush = '#800080' myFacetText = "Source" myFacetTextColour = "LIGHT GREY" myFacetShow = "true" myFacetViewDepth = "-1" children = [] # Set up the Facet type which initilizes all the properties of a given Facet def setFacetType(self, facetname, facettype, facetparent): if (facettype == "SOURCEFACET"): self.myFacetName = facetname self.myFacetParent = facetparent self.myFacetType = "SOURCEFACET" self.myFacetLevel = "0" self.myFacetBrush = wxBrush("#800080", wxSOLID) self.myFacetText = "Source" self.myFacetTextColour = "LIGHT GREY" elif (facettype == "HOSTFACET"): self.myFacetName = facetname self.myFacetParent = facetparent self.myFacetType = "HOSTFACET" self.myFacetLevel = "1" self.myFacetBrush = wxBrush("#0000FF", wxSOLID) self.myFacetText = "Host" self.myFacetTextColour = "YELLOW" elif (facettype == "NODEFACET"): self.myFacetName = facetname self.myFacetParent = facetparent self.myFacetType = "NODEFACET" self.myFacetLevel = "2" self.myFacetBrush = wxBrush("#FF0000", wxSOLID) self.myFacetText = "Node" self.myFacetTextColour = "YELLOW" elif (facettype == "AGENTFACET"): self.myFacetName = facetname self.myFacetParent = facetparent self.myFacetType = "AGENTFACET" self.myFacetLevel = "3" self.myFacetBrush = wxBrush("#008000", wxSOLID) self.myFacetText = "Agent" self.myFacetTextColour = "YELLOW" elif(facettype == "COMPONENTFACET"): self.myFacetName = facetname self.myFacetParent = facetparent self.myFacetType = "COMPONENTFACET" self.myFacetLevel = "4" self.myFacetBrush = wxBrush("#FFFF00", wxSOLID) self.myFacetText = "Component" self.myFacetTextColour = "DARK GREEN" elif(facettype == "Level6"): self.myFacetName = facetname self.myFacetParent = facetparent self.myFacetType = "Level6" self.myFacetLevel = "5" self.myFacetBrush = wxBrush("#FFFF11", wxSOLID) self.myFacetText = "Component" self.myFacetTextColour = "DARK GREEN" elif(facettype == "Level7"): self.myFacetName = facetname self.myFacetParent = facetparent self.myFacetType = "Level7" self.myFacetLevel = "5" self.myFacetBrush = wxBrush("#11FF11", wxSOLID) self.myFacetText = "Component" self.myFacetTextColour = "DARK GREEN" else: self.myFacetName = "noname" self.myFacetParent = "noparent" self.myFacetType = "UNKNOWN" self.myFacetLevel = "-1" self.myFacetBrush = "WHITE" self.myFacetText = "UNK" self.myFacetTextColour = "BLACK" def getFacetType(self): return self.myFacetType def getFacetBrush(self): return self.myFacetBrush def getFacetText(self): return self.myFacetTextColour #--------------------------------------------------------------------------- # This is the society class which cotains the connections within the society # The is used to remember what levels each facet is on and what each facet is connected to class mySociety: def __init__(self): self.maxdepth = 0 self.facetLevelsList = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] self.levelIndex = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] self.facetList = [] self.tempList = [] self.removeList = [] self.addList = [] self.connectionsDictionary = {} self.hiddenConnectionsDictionary = {} print "New Society Created" self.connectionsDictionary.clear() def __del__(self): del self.facetLevelsList[0:] del self.levelIndex[0:] del self.tempList[0:] del self.removeList[0:] del self.addList[0:] self.connectionsDictionary.clear() self.hiddenConnectionsDictionary.clear() def ClearFacetList(self): self.facetList = [] #~ self.tempList = [] #~ self.removeList = [] self.addList = [] def AddNewFacet(self, name = "noname", type = "SOURCEFACET", level = 0, parent = "noparent"): newFacet = myFacet() newFacet.setFacetType(name, type,parent) self.facetList.append(newFacet) if (parent == "noparent"): self.connectionsDictionary[name] = [] self.facetLevelsList[level] += 1 else: self.facetLevelsList[level] += 1 # if there is no key add it initilizes if (parent in self.connectionsDictionary.keys()): self.connectionsDictionary[parent] += [name] # else if there is a key else: self.connectionsDictionary[parent] = [name] def getFacetList(self): del self.tempList[0:] self.makeLevelIndex() for k in self.facetList: if k.myFacetShow == "true": self.tempList.append(-1) self.sortFacets("noparent", len(self.facetList),self.facetList,0) #~ del self.facetList[0:] #~ self.facetList = self.facetList + self.tempList #~ if self.facetList: #~ self.getDepth(self.facetList[0]) return self.tempList def hideAll(self): self.organizeConnections(self.facetList[0].myFacetName, 0) self.facetList[0].myFacetShow = "false" self.facetLevelsList[0] = 0 def getConnectionsDictionary(self): return self.connectionsDictionary def getFacetLevelList(self): return self.facetLevelsList def getFacetDepth(self, fdSearchString): idx = 0 while idx < len(self.facetList): if self.facetList[idx].myFacetName == fdSearchString: idx+=1 def makeLevelIndex(self): prevtotal = 0 idx = 0 for i in self.facetLevelsList: if i==0: continue self.levelIndex[idx] = prevtotal prevtotal+=i idx+=1 def sortFacets(self,searchString, theFacetListLength, theFacetList, depth): if theFacetListLength == 0: return count = 0 searhStringList = [] delFacetsList = [] while (count < theFacetListLength): if (searchString == theFacetList[count].myFacetParent and theFacetList[count].myFacetShow == "true"): self.tempList[self.levelIndex[depth]] = theFacetList[count] self.levelIndex[depth]+=1 searhStringList.append(theFacetList[count].myFacetName) delFacetsList.append(count) count+=1 depth+=1 for i in theFacetList: del i for i in searhStringList: self.sortFacets(i, len(theFacetList), theFacetList,depth) def getDepth(self,currentFacet): self.maxdepth = 0 self.countChildren(currentFacet, 0) return self.maxdepth def countChildren(self, currNode,depth=0): if depth > self.maxdepth: self.maxdepth = depth if self.connectionsDictionary.has_key(currNode) == 1: depth+=1 for i in self.connectionsDictionary[currNode]: self.countChildren(i, depth) self.myFacetViewDepth(i,depth) elif self.hiddenConnectionsDictionary.has_key(currNode) == 1: depth+=1 for j in self.hiddenConnectionsDictionary[currNode]: self.countChildren(j, depth) self.myFacetViewDepth(j,depth) def myFacetViewDepth(self, currNode, depth): for i in self.facetList: if i.myFacetName == currNode: i.myFacetViewDepth = abs(self.maxdepth-depth) def organizeConnections(self, cN, vD, cD=0): #cN: Current Node / vD: View Depth / cD: Current Depth if vD == -1: vD = self.maxdepth del self.removeList[0:] for j in self.facetList: if j.myFacetName == cN: prevvD = j.myFacetViewDepth vD+=int(j.myFacetLevel) cD+=int(j.myFacetLevel) j.myFacetViewDepth = vD continue #~ print "Current Depth: ", cD, "\nPrev View Depth: ", prevvD , "\nView Depth: ", vD if prevvD < vD: self.addConn(cN, vD, cD) for k in self.facetList: if k.myFacetName in self.addList: k.myFacetShow = "true" #~ print self.facetLevelsList else: self.removeConn(cN, vD, cD) for i in self.facetList: if i.myFacetName in self.removeList: i.myFacetShow = "false" #~ print self.facetLevelsList def removeConn(self, currNode, viewdepth, currdepth=0): if self.connectionsDictionary.has_key(currNode) == 1: currdepth+=1 for i in self.connectionsDictionary[currNode]: self.removeConn(i,viewdepth, currdepth) if viewdepth <= currdepth-1: self.hiddenConnectionsDictionary[currNode] = self.connectionsDictionary[currNode] self.removeList += self.connectionsDictionary[currNode] #~ print str(currdepth) + " " +str(len(self.connectionsDictionary[currNode])) self.facetLevelsList[currdepth]-=len(self.connectionsDictionary[currNode]) del self.connectionsDictionary[currNode] def addConn(self,currNode, viewdepth, currdepth=0): if self.connectionsDictionary.has_key(currNode) == 1: currdepth+=1 for i in self.connectionsDictionary[currNode]: self.addConn(i,viewdepth,currdepth) if self.hiddenConnectionsDictionary.has_key(currNode) == 1: currdepth+=1 for j in self.hiddenConnectionsDictionary[currNode]: self.addConn(j,viewdepth,currdepth) if viewdepth > currdepth-1: self.connectionsDictionary[currNode] = self.hiddenConnectionsDictionary[currNode] self.addList += self.hiddenConnectionsDictionary[currNode] self.facetLevelsList[currdepth]+=len(self.hiddenConnectionsDictionary[currNode]) del self.hiddenConnectionsDictionary[currNode] #--------------------------------------------------------------------------- #~ TheSociety = mySociety() # Create the society #TheSociety.AddNewFacet("Root", "SOURCEFACET", 0, "noparent") #TheSociety.AddNewFacet("Child1", "HOSTFACET", 1, "Root") #TheSociety.AddNewFacet("Child2", "HOSTFACET", 1, "Root") #TheSociety.AddNewFacet("Child3", "HOSTFACET", 1, "Root") #TheSociety.AddNewFacet("SubChild1", "NODEFACET", 2, "Child3") #TheSociety.AddNewFacet("SubChild2", "NODEFACET", 2, "Child1") #TheSociety.AddNewFacet("SubSubChild1", "AGENTFACET", 3, "SubChild1") #TheSociety.getFacetList() #print TheSociety.getFacetList() --- NEW FILE: gizmoImages.py --- #---------------------------------------------------------------------- # This file was generated by encode_bitmaps.py # from wxPython.wx import wxImageFromStream, wxBitmapFromImage import cStringIO catalog = {} index = [] class ImageClass: pass def get001Data(): return \ '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00"\x00\x00\x00"\x08\x06\x00\ \x00\x00:G\x0b\xc2\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\x07\ \x9fIDATx\x9c\xcd\x98_o\xdbF\x16\xc5\x7f\xf7\xce\x0c\xa5\xc8\xb2];N\xea4M\ \x8a\xc2@\xb6[\xa0E\xd1\xa2o]\xf4#\xef\x17\xd8\xa7}\xeck\x8b\x16}\t\x908\x9b\ \x7fnc\xc9\x96H\x0e\x87\x9c\xd9\x87\x19QR\xd2\xf7]\x02\xf4P\xb2L\x1e\x9d{\ [...3038 lines suppressed...] f\xb3\xcf9<<\xa2\xaa\xd6\xac\x16s\xbc_\xb3Z\xafX\xae\x16\x18\x9d\xe7R\xeb\ \xd2\x9e\xd9\xbad\x82\xb6\xe8K\x95g\xca\x9c\xfb\xfd\xd7\x02{>"\x92v\xe51\xa6\ \xbd\xa8\xd2\x01\x1d4\xa2@b@b$F\xa1(\xc6\x1c\xdf{\x80\xc4\xfbD\x89\xe9&\x92_\ P\x88\xf47\xbd\xbd\xaa\xac\x0e\xa5\xd8\x9b\xf4D\x04bO\x8a\x7f\xc7\x8b\x9a\ \xbf\x01\xfc\t\xfa\\\xad\xe0v?\x00\x00\x00\x00IEND\xaeB`\x82' def getrestBitmap(): return wxBitmapFromImage(getrestImage()) def getrestImage(): stream = cStringIO.StringIO(getrestData()) return wxImageFromStream(stream) index.append('rest') catalog['rest'] = ImageClass() catalog['rest'].getData = getrestData catalog['rest'].getImage = getrestImage catalog['rest'].getBitmap = getrestBitmap --- NEW FILE: AWB.py --- #!/bin/env python #---------------------------------------------------------------------------- # Name: CS03.py # Purpose: AWB umbrella # # Author: ISAT (D. Moore # # RCS-ID: $Id: AWB.py,v 1.1 2004/08/25 21:03:17 dana_virtual Exp $ # <copyright> # Copyright 2002 BBN Technologies, LLC # under sponsorship of the Defense Advanced Research Projects Agency (DARPA). # # This program is free software; you can redistribute it and/or modify # it under the terms of the Cougaar Open Source License as published by # DARPA on the Cougaar Open Source Website (www.cougaar.org). # # THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS # PROVIDED 'AS IS' WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR # IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF [...1157 lines suppressed...] Use the Society Editor to view or manually edit a society. When viewing a society, changes just made by applying a rule (from the Rule Editor) are highlighted in cyan. To manually edit the society, select an item by left clicking on it, then left click again to change its name and/or value, or right click to view a menu from which adds, deletes, and changes can be made. <p> <h2>Agent Laydown</h2> Use the Agent Laydown to allocate agents (from a list of agents) to hosts (from a list of hosts). The allocation can be done automatically by distributing the agents evenly among the hosts or by specifying the number of agents to put on each host, or it can be done manually by dragging and dropping agents and/or nodes from the agent list to hosts on the host list. There are capabilities for quickly creating societies and for adding/removing/editing facets. When society manipulation is complete, the new or revised society can be saved to an XML file. Agent Laydown does not permit working with society entities below the Agent level, but for operations at or above that level, Agent Laydown is more versatile than Society Editor. </body></html> """ #---------------------------------------------------------------------------- if __name__ == '__main__': main() --- NEW FILE: AgentDataCollector.py --- from threading import Thread, Semaphore from __future__ import generators import re import string import urllib import time concurrentProcesses = 10 mutex = Semaphore(value=concurrentProcesses) class AgentHTMLParser(Thread): def __init__(self, url=0, aname="No Name"): Thread.__init__(self) self.name = aname self.ratio = -1 self.totalTasks= -1 self.unplannedTasks = -1 self.unestimatedTasks = -1 self.failedTasks = -1 self.unconfidentTasks = -1 self.siteurl = url self.returnlist = [] def run(self): self.getPage() def getPage(self): if self.siteurl == 0: return else: mutex.acquire() self.file = urllib.urlopen(self.siteurl) mutex.release() # Parse the website and collect the data though regular expressions for self.i in self.file.readlines(): totalRe = re.compile('Number of Tasks: <b>(.*)') ratioRe = re.compile('GLMCompletion ratio:.*<b>(.?[0-9]+)') unplannedRe = re.compile('<.*Unplanned Tasks\[(.*)\]') unestimatedRe = re.compile('<.*Unestimated Tasks\[(.*)\]') failRe= re.compile('<.*Failed Tasks\[(.*)\]') unconfidentRe = re.compile('<.*Unconfident Tasks\[(.*)\]') ans = totalRe.match(self.i) if ans: self.totalTasks = ans.group(1) ans = ratioRe.match(self.i) if ans: self.ratio = ans.group(1) ans = unplannedRe.match(self.i) if ans: self.unplannedTasks = ans.group(1) ans = unestimatedRe.match(self.i) if ans: self.unestimatedTasks = ans.group(1) ans = failRe.match(self.i) if ans: self.failedTasks = ans.group(1) ans = unconfidentRe.match(self.i) if ans: self.unconfidentTasks = ans.group(1) # Add all the elemets to a list for returning self.returnlist.append(str(self.totalTasks)) self.returnlist.append(str(self.ratio)) self.returnlist.append(str(self.unplannedTasks)) self.returnlist.append(str(self.unestimatedTasks)) self.returnlist.append(str(self.failedTasks)) self.returnlist.append(str(self.unconfidentTasks)) self.returnlist.append(self.name) def getInfo(self): return self.returnlist def SocietyQuery(societyAddress): # Path Style # http://sm056:8801/$1-1-CAVSQDN.AVNBDE.1-AD.ARMY.MIL/completion?showTables=true&viewType=viewAgentBig # A = AgentHTMLParser("completionbft.htm") Agent_Names = [] agentfile = urllib.urlopen(societyAddress) ThreadList = [] MainOut = [] QueryDictionary = {} start = time.clock() # Read in the names of all the agents and store them to an agent list [^.comm] for agentline in agentfile: AgentNameRE = re.compile('^<.*\/\$(.*.[^com])\/list') AgentHostRE = re.compile('^<.*Agents on host \((.*)\)') agentString = AgentNameRE.match(agentline) hostString = AgentHostRE.match(agentline) if hostString: agentHost = hostString.group(1) if agentString: Agent_Names.append(agentString.group(1)) # Loop though all the angets and get the information from the server #~ print Agent_Names for i in Agent_Names: agentInfoURL = "http://" + agentHost + "/$" + i + "/completion?showTables=true&viewType=viewAgentBig" AHP = AgentHTMLParser(agentInfoURL, i) #~ AHP.getPage() #~ Out += AHP.getInfo() AHP.start() ThreadList.append(AHP) for retriever in ThreadList: retriever.join() agentInformation = retriever.getInfo() agentName = agentInformation.pop() QueryDictionary[agentName] = agentInformation looptime = time.clock()-start print "\nNumber of Agnets: \t" + str(len(Agent_Names)) + "\nProcessing Time: \t" + str(looptime) + " seconds\n" + "Society Server: \t" return QueryDictionary #~ dict = SocietyQuery() concurrentProcesses = 1 mutex = Semaphore(value=concurrentProcesses) dict = SocietyQuery('http://u142:8800/agents?suffix=.') print "Number of Threads: \t" + str(concurrentProcesses) print str(dict) --- NEW FILE: .cvsignore --- insertion_dialog.pyc societyViewer.pyc agentLaydown.pyc cougaar_DragAndDrop.pyc CS03.pyc CSMARTer.log csmarter_events.pyc editorTextControl.pyc gizmo.pyc gizmoImages.pyc images.pyc insertion_dialog.pyc run.pyc simpleCanvas.pyc societyBuilder.pyc societyController.pyc societyEditor.pyc societyFactoryServer.pyc debug.pyc eventFactory.pyc urlDlg.pyc hierarchyJD.html societyController.py~ societyVisualModel.pyc zoomer.pyc awb.list.p awb.urls.p facetProperties.pyc globalConstants.pyc PollingServices --- NEW FILE: societyReader.py --- import sys import re import urllib2 from urlparse import urlparse import random as r import time import os, string import thread import httplib from screenScraper import * #~ htmlText = ''' <html><head><title>Agents at the Root ("<a href="/agents?suffix=.">.</a>")</title></head> #~ <body><p><h1>Agents at the Root ("<a href="/agents?suffix=.">.</a>")</h1> #~ <table border="0"> #~ <tr><td align="right"> 1. </td><td align="right"><a href="/agents?suffix=.comm">.comm</a></td></tr> #~ <tr><td align="right"> 2. </td><td align="right"><a href="/$PlannerAgent/list">PlannerAgent</a></td></tr> #~ <tr><td align="right"> 3. </td><td align="right"><a href="/$PlannerAgent2/list">PlannerAgent2</a></td></tr> #~ </table> #~ <p> #~ <a href="/agents">Agents on host (fpga:8800)</a><br> #~ <a href="/agents?suffix=.">Agents at the root (.)</a><br></body></html> #~ ''' class SocietyReader: def __init__(self, url): self.url = url def readAgents(self): f = urllib2.urlopen(self.url) htmlText= f.read() # reads the whole page as a big glob f.close() return self.scrapeHTML(htmlText) def scrapeHTML(self, htmlText): self.agentList = [] scraper = BeautifulSoup() scraper.feed(htmlText) print ">>>\n", list = scraper.fetch('a', {'href':'/$%'}) #~ for l in list: #~ print "AREF >>>", l #~ print "\n\n Second tests..." #~ print 'Fetch List...' for s in list: s = str(s) print 's >>>', s start = string.index(s, ">") end = string.rindex(s, "<") t = s[start+1:end] print "AREF...", t self.agentList.append(str(t)) #~ alphabet = [ #~ 'Alpha','Beta','Gamma','Delta','Epsilon', #~ 'Zeta','Eta','Theta','Iota','Kappa', #~ 'Lambda','Mu','Nu','Xi','Omicron', #~ 'Pi','Rho','Sigma','Tau','Upsilon', #~ 'Phi','Chi','Psi','Omega' #~ ] #~ for item in alphabet: #~ self.agentList.append(item) return self.agentList fyi = ''' http://192.233.51.210:8800/$PlanAnalyzerAgent/tasks?mode=12&limit=true&formType=3&uid=formSubmit=Search ''' def readUniqueObjects(self, host,port): self.uniqueObjects = {} uniqueObjectsQuery = '/tasks?mode=12&limit=true&formType=3&uid=formSubmit=Search' if len(self.agentList) == None: return for agent in self.agentList: wellformedURL = 'http://'+host+':'+port+ '/$' + agent +uniqueObjectsQuery print 'wellformedURL', wellformedURL f = urllib2.urlopen(wellformedURL) htmlText= f.read() # reads the whole page as a big glob f.close() scraper = BeautifulSoup() scraper.feed(htmlText) elements = scraper.fetch('center') #~ print "elements>>>", elements elt = str(elements[0]) elt = elt[elt.index('<b>')+3:elt.index('</b>')] self.uniqueObjects[str(agent)] = elt # add dummy: self.uniqueObjects['---'] = '---' # not sure why you need to do - bug in DividedShape?? #~ print 'uniqueObjects', self.uniqueObjects return self.uniqueObjects def __str__(self): return "Agents >>>" + self.url --- NEW FILE: zoomer.pyc --- (This appears to be a binary file; contents omitted.) --- NEW FILE: screenScraper.py --- from sgmllib import SGMLParser import string import types class PageElement: """Contains the navigational information for some part of the page (either a tag or a piece of text)""" def __init__(self, parent=None, previous=None): self.parent = parent self.previous = previous self.next = None class NavigableText(PageElement): """A simple wrapper around a string that keeps track of where in the document the string was found. Doesn't implement all the string methods because I'm lazy. You could have this extend UserString if you were using 2.2.""" def __init__(self, string, parent=None, previous=None): PageElement.__init__(self, parent, previous) self.string = string def __eq__(self, other): return self.string == str(other) def __str__(self): return self.string def strip(self): return self.string.strip() class Tag(PageElement): """Represents a found HTML tag with its attributes and contents.""" def __init__(self, name, attrs={}, parent=None, previous=None): PageElement.__init__(self, parent, previous) self.name = name self.attrs = attrs self.contents = [] self.foundClose = 0 def get(self, key, default=None): return self._getAttrMap().get(key, default) def __call__(self, *args): return apply(self.fetch, args) def __getitem__(self, key): return self._getAttrMap()[key] def __setitem__(self, key, value): self._getAttrMap() self.attrMap[key] = value for i in range(0, len(self.attrs)): if self.attrs[i][0] == key: self.attrs[i] = (key, value) def _getAttrMap(self): if not hasattr(self, 'attrMap'): self.attrMap = {} for (key, value) in self.attrs: self.attrMap[key] = value return self.attrMap def __repr__(self): return str(self) def __ne__(self, other): return not self == other def __eq__(self, other): if not isinstance(other, Tag) or self.name != other.name or self.attrs != other.attrs or len(self.contents) != len(other.contents): return 0 for i in range(0, len(self.contents)): if self.contents[i] != other.contents[i]: return 0 return 1 def __str__(self): attrs = '' if self.attrs: for key, val in self.attrs: attrs = attrs + ' %s="%s"' % (key, val) close = '' closeTag = '' if self.isSelfClosing(): close = ' /' elif self.foundClose: closeTag = '</%s>' % self.name s = self.renderContents() if not hasattr(self, 'hideTag'): s = '<%s%s%s>' % (self.name, attrs, close) + s + closeTag return s def renderContents(self): s='' #non-Unicode for c in self.contents: try: s = s + str(c) except UnicodeEncodeError: if type(s) <> types.UnicodeType: s = s.decode('utf8') #convert ascii to Unicode #str() should, strictly speaking, not return a Unicode #string, but NavigableText never checks and will return #Unicode data if it was initialised with it. s = s + str(c) return s def isSelfClosing(self): return self.name in BeautifulSoup.SELF_CLOSING_TAGS def append(self, tag): self.contents.append(tag) def first(self, name=None, attrs={}, contents=None, recursive=1): r = None l = self.fetch(name, attrs, contents, recursive) if l: r = l[0] return r def fetch(self, name=None, attrs={}, contents=None, recursive=1): """Extracts Tag objects that match the given criteria. You can specify the name of the Tag, any attributes you want the Tag to have, and what text and Tags you want to see inside the Tag.""" if contents and type(contents) != type([]): contents = [contents] results = [] for i in self.contents: if isinstance(i, Tag): if not name or i.name == name: match = 1 for attr, value in attrs.items(): check = i.get(attr) #By default, find the specific value called for. #Use SQL-style wildcards to find substrings, prefix, #suffix, etc. result = (check == value) if check and value: if len(value) > 1 and value[0] == '%' and value[-1] == '%' and value[-2] != '\\': result = (check.find(value[1:-1]) != -1) elif value[0] == '%': result = check.rfind(value[1:]) == len(check)-len(value)+1 elif value[-1] == '%': result = check.find(value[:-1]) == 0 if not result: match = 0 break match = match and (not contents or i.contents == contents) if match: results.append(i) if recursive: results.extend(i.fetch(name, attrs, contents, recursive)) return results class BeautifulSoup(SGMLParser, Tag): """The actual parser. It knows the following facts about HTML, and not much else: * Some tags have no closing tag and should be interpreted as being closed as soon as they are encountered. * Most tags can't be nested; encountering an open tag when there's already an open tag of that type in the stack means that the previous tag of that type should be implicitly closed. However, some tags can be nested. When a nestable tag is encountered, it's okay to close all unclosed tags up to the last nestable tag. It might not be safe to close any more, so that's all it closes. * The text inside some tags (ie. 'script') may contain tags which are not really part of the document and which should be parsed as text, not tags. If you want to parse the text as tags, you can always get it and parse it explicitly.""" SELF_CLOSING_TAGS = ['br', 'hr', 'input', 'img', 'meta', 'spacer'] NESTABLE_TAGS = ['font', 'table', 'tr', 'td', 'th', 'tbody', 'p'] QUOTE_TAGS = ['script'] IMPLICITLY_CLOSE_TAGS = 1 def __init__(self, text=None): Tag.__init__(self, '[document]') SGMLParser.__init__(self) self.quoteStack = [] self.hideTag = 1 self.reset() if text: self.feed(text) def feed(self, text): SGMLParser.feed(self, text) self.endData() def reset(self): SGMLParser.reset(self) self.currentData = '' self.currentTag = None self.tagStack = [] self.pushTag(self) def popTag(self, closedTagName=None): tag = self.tagStack.pop() if closedTagName == tag.name: tag.foundClose = 1 #print "Pop", tag.name self.currentTag = self.tagStack[-1] return self.currentTag def pushTag(self, tag): #print "Push", tag.name if self.currentTag: self.currentTag.append(tag) self.tagStack.append(tag) self.currentTag = self.tagStack[-1] def endData(self): if self.currentData: if not string.strip(self.currentData): if '\n' in self.currentData: self.currentData = '\n' else: self.currentData = ' ' o = NavigableText(self.currentData, self.currentTag, self.previous) if self.previous: self.previous.next = o self.previous = o self.currentTag.contents.append(o) self.currentData = '' def _popToTag(self, name, closedTag=0): """Pops the tag stack up to and including the most recent instance of the given tag. If a list of tags is given, will accept any of those tags as an excuse to stop popping, and will *not* pop the tag that caused it to stop popping.""" if self.IMPLICITLY_CLOSE_TAGS: closedTag = 1 numPops = 0 mostRecentTag = None oneTag = (type(name) == types.StringType) for i in range(len(self.tagStack)-1, 0, -1): thisTag = self.tagStack[i].name if (oneTag and thisTag == name) \ or (not oneTag and thisTag in name): numPops = len(self.tagStack)-i break if not oneTag: numPops = numPops - 1 closedTagName = None if closedTag: closedTagName = name for i in range(0, numPops): mostRecentTag = self.popTag(closedTagName) return mostRecentTag def unknown_starttag(self, name, attrs): if self.quoteStack: #This is not a real tag. #print "<%s> is not real!" % name attrs = map(lambda(x, y): '%s="%s"' % (x, y), attrs) self.handle_data('<%s %s>' % (name, attrs)) return self.endData() tag = Tag(name, attrs, self.currentTag, self.previous) if self.previous: self.previous.next = tag self.previous = tag if not name in self.SELF_CLOSING_TAGS: if name in self.NESTABLE_TAGS: self._popToTag(self.NESTABLE_TAGS) else: self._popToTag(name) self.pushTag(tag) if name in self.SELF_CLOSING_TAGS: self.popTag() if name in self.QUOTE_TAGS: #print "Beginning quote (%s)" % name self.quoteStack.append(name) def unknown_endtag(self, name): if self.quoteStack and self.quoteStack[-1] != name: #This is not a real end tag. #print "</%s> is not real!" % name self.handle_data('</%s>' % name) return self.endData() self._popToTag(name, 1) if self.quoteStack and self.quoteStack[-1] == name: #print "That's the end of %s!" % self.quoteStack[-1] self.quoteStack.pop() def handle_data(self, data): self.currentData = self.currentData + data def handle_comment(self, text): "Propagate comments right through." self.handle_data("<!--%s-->" % text) def handle_charref(self, ref): "Propagate char refs right through." self.handle_data('&#%s;' % ref) def handle_entityref(self, ref): "Propagate entity refs right through." self.handle_data('&%s;' % ref) def handle_decl(self, data): "Propagate DOCTYPEs right through." self.handle_data('<!%s>' % data) class BeautifulStoneSoup(BeautifulSoup): """A version of BeautifulSoup that doesn't know anything at all about what HTML tags have special behavior. Useful for parsing things that aren't HTML, or when BeautifulSoup makes an assumption counter to what you were expecting.""" IMPLICITLY_CLOSE_TAGS = 0 SELF_CLOSING_TAGS = [] NESTABLE_TAGS = [] QUOTE_TAGS = [] htmlText = ''' <html><head><title>Agents at the Root ("<a href="/agents?suffix=.">.</a>")</title></head> <body><p><h1>Agents at the Root ("<a href="/agents?suffix=.">.</a>")</h1> <table border="0"> <tr><td align="right"> 1. </td><td align="right"><a href="/agents?suffix=.comm">.comm</a></td></tr> <tr><td align="right"> 2. </td><td align="right"><a href="/$PlannerAgent/list">PlannerAgent</a></td></tr> <tr><td align="right"> 3. </td><td align="right"><a href="/$PlannerAgent2/list">PlannerAgent2</a></td></tr> </table> <p> <a href="/agents">Agents on host (fpga:8800)</a><br> <a href="/agents?suffix=.">Agents at the root (.)</a><br></body></html> ''' # ~~~~~~ # quickie tester: #~ text = ''' <html><head><title>Agents at the Root ("<a href="/agents?suffix=.">.</a>")</title></head> #~ <body><p><h1>Agents at the Root ("<a href="/agents?suffix=.">.</a>")</h1> #~ <table border="0"> #~ <tr><td align="right"> 1. </td><td align="right"><a href="/agents?suffix=.comm">.comm</a></td></tr> #~ <tr><td align="right"> 2. </td><td align="right"><a href="/$PlannerAgent/list">PlannerAgent</a></td></tr> #~ <tr><td align="right"> 3. </... [truncated message content] |