__init__.py :  » Network » Chestnut-Dialer » chestnut-dialer-0.3.3 » chestnut_dialer » dobj » 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 » Network » Chestnut Dialer 
Chestnut Dialer » chestnut dialer 0.3.3 » chestnut_dialer » dobj » __init__.py
import socket
import time
try:
  import cPickle
  pickle = cPickle
except:
  import pickle

AF_UNIX = socket.AF_UNIX
AF_INET = socket.AF_INET

_load = pickle.load
def _dump(o, f):
  pickle.dump(o, f, -1)

class GetEventsRequest:
  def __init__(self, after_time):
    self.after_time = after_time

class GetRootObjectRequest:
  pass

class UnrefRequest:
  def __init__(self, obj_id):
    self.obj_id = obj_id

class ReprRequest:
  def __init__(self, obj_id):
    self.obj_id = obj_id

class StrRequest:
  def __init__(self, obj_id):
    self.obj_id = obj_id

class UnicodeRequest:
  def __init__(self, obj_id):
    self.obj_id = obj_id

class GetAttrRequest:
  def __init__(self, obj_id, name):
    self.obj_id = obj_id
    self.name = name

class SetAttrRequest:
  def __init__(self, obj_id, name, value):
    self.obj_id = obj_id
    self.name = name
    self.value = value

class DelAttrRequest:
  def __init__(self, obj_id, name):
    self.obj_id = obj_id
    self.name = name

class CallRequest:
  def __init__(self, obj_id, args):
    self.obj_id = obj_id
    self.args = args

class LenRequest:
  def __init__(self, obj_id):
    self.obj_id = obj_id

class GetItemRequest:
  def __init__(self, obj_id, key):
    self.obj_id = obj_id
    self.key = key

class SetItemRequest:
  def __init__(self, obj_id, key, value):
    self.obj_id = obj_id
    self.key = key
    self.value = value

class DelItemRequest:
  def __init__(self, obj_id, key):
    self.obj_id = obj_id
    self.key = key

class ErrorResponse:
  def __init__(self, exception):
    self.exception = exception

class UnknownRequestError(Exception):
  pass

class UnknownObjectError(Exception):
  def __init__(self, obj_id):
    self.obj_id = obj_id

class DObjClient:
  def __init__(self, address_family, server_address, timeout=None):
    self._address_family = address_family
    self._server_address = server_address
    self._timeout = timeout
  def _call_server(self, request):
    for i in range(3):
      sock = socket.socket(self._address_family, socket.SOCK_STREAM)
      sock.connect(self._server_address)
      sock.settimeout(self._timeout)
      _dump(request, sock.makefile('w'))
      value = _load(sock.makefile('r'))
      sock.close()
      if not isinstance(value, ErrorResponse): return value
      if value.exception != UnknownRequestError: break
    raise value.exception

class DObject(DObjClient):
  _id = None
  _unref_on_del = 1
  def __init__(self, address_family, server_address, id):
    DObjClient.__init__(self, address_family, server_address)
    self._id = id
  def __del__(self):
    if self._unref_on_del:
      self._call_server(UnrefRequest(self._id))
  def __repr__(self):
    return self._call_server(ReprRequest(self._id))
  def __str__(self):
    return self._call_server(StrRequest(self._id))
  def __unicode__(self):
    return self._call_server(UnicodeRequest(self._id))
  def __getattr__(self, name):
    if name[0:1] == "_": raise AttributeError(name)
    return self._call_server(GetAttrRequest(self._id, name))
  def __setattr__(self, name, value):
    if name[0:1] == "_":
      self.__dict__[name] = value
      return
    self._call_server(SetAttrRequest(self._id, name, value))
  def __delattr__(self, name):
    if name[0:1] == "_":
      del self.__dict__[name]
      return
    self._call_server(DelAttrRequest(self._id, name))
  def __call__(self, *args):
    return self._call_server(CallRequest(self._id, args))
  def __len__(self):
    return self._call_server(LenRequest(self._id))
  def __getitem__(self, key):
    return self._call_server(GetItemRequest(self._id, key))
  def __setitem__(self, key, value):
    self._call_server(SetItemRequest(self._id, key, value))
  def __delitem__(self, key):
    self._call_server(DelItemRequest(self._id, key))

class BaseEvent:
  def __init__(self):
    self.time = time.time()

class SimpleEvent(BaseEvent):
  def __init__(self, data):
    BaseEvent.__init__(self)
    self.data = data

class DObjConnection(DObjClient):
  def __init__(self, address_family, server_address, timeout=None):
    DObjClient.__init__(self, address_family, server_address, timeout)
    self.last_event_time = time.time()
  def get_object(self):
    return self._call_server(GetRootObjectRequest())
  def get_events(self, after_time = 0):
    if after_time == 0: after_time = self.last_event_time
    events = self._call_server(GetEventsRequest(after_time))
    if events:
      self.last_event_time = max(self.last_event_time,
          events[-1].time)
    return events
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.