DCSearch.py :  » Development » pyDC » pydc » pyDClib » 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 » Development » pyDC 
pyDC » pydc » pyDClib » DCSearch.py
# This file is part of pyDC software
# Copyright 2002 Anakim Border <aborder@users.sourceforge.net>
#
# pyDC is released under the terms of GPL licence.

from EventGenerator import *
import DCWorker
from threading import Lock
from copy import copy
import socket
import string
import os
import time

class DCSearch(object, EventGenerator):
  LOG_STATUS = 1
  LOG_CHAT = 2
  LOG_ERROR = 3

  def __init__(self, pattern = None, private = 0):
    EventGenerator.__init__(self)

    self._pattern = pattern
    self._results = []
    self._private = private
    self._status = 0
    self.opLock = Lock()

  def getStatus(self):
    return self._status

  def setPattern(self, pattern):
    if self._status != 0:
      raise SearchInProgressError()
    self._pattern = pattern

  def getPattern(self):
    return self._pattern
    
  def isPrivate(self):
    return self._private

  def getResultNum(self):
    return len(self._results)

  def getResults(self):
    self.opLock.acquire()
    results = copy(self._results)
    self.opLock.release()

    return results

  def start(self, nodelay=0):
    if self._pattern == None:
      raise PatternNotSetError()

    res = DCWorker.getWorker().getSearchWorker().registerSearch(self, nodelay)
    if res:
      self.lock.acquire()
      for listener in self.listeners:
        listener.onSearchStart(self)
      self.lock.release()
      self._status = 1

    return res

  def stop(self):
    if self._status == 1:
      DCWorker.getWorker().getSearchWorker().deregisterSearch(self)
      self._status = 2
      
      self.lock.acquire()
      for listener in self.listeners:
        listener.onSearchStop(self)
      self.lock.release()

  def isAlive(self):
    return self._status == 1

  def onNewResult(self, result):
    resultPath = result.path.lower()
    try:
      for i in self._pattern.lower().split(' '): resultPath.index(i)
    except ValueError:
      return

    self.opLock.acquire()
    self._results.append(result)
    self.opLock.release()
    
    self.lock.acquire()
    for listener in self.listeners:
      listener.onNewResult(self, result)
    self.lock.release()

  #=================#
  # Private section #
  #=================#

  status = property(getStatus, None, None, None)
  pattern = property(getPattern, setPattern, None, None)
  private = property(isPrivate, None, None, None)
  resultNum = property(getResultNum, None, None, None)
  results = property(getResults, None, None, None)

class PatternNotSetError(Exception): pass
class SearchInProgressError(Exception): pass
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.