python-igraph manual

For using igraph from Python

   Home       Trees       Indices       Help   
Package igraph :: Package drawing :: Module baseclasses
[hide private]

Source Code for Module igraph.drawing.baseclasses

  1  """ 
  2  Abstract base classes for the drawing routines. 
  3  """ 
  4   
  5  from igraph.compat import property 
  6  from igraph.drawing.utils import BoundingBox 
  7  from math import pi 
8 9 ##################################################################### 10 11 # pylint: disable-msg=R0903 12 # R0903: too few public methods 13 -class AbstractDrawer(object):
14 """Abstract class that serves as a base class for anything that 15 draws an igraph object.""" 16
17 - def draw(self, *args, **kwds):
18 """Abstract method, must be implemented in derived classes.""" 19 raise NotImplementedError("abstract class")
20
21 ##################################################################### 22 23 # pylint: disable-msg=R0903 24 # R0903: too few public methods 25 -class AbstractCairoDrawer(AbstractDrawer):
26 """Abstract class that serves as a base class for anything that 27 draws on a Cairo context within a given bounding box. 28 29 A subclass of L{AbstractCairoDrawer} is guaranteed to have an 30 attribute named C{context} that represents the Cairo context 31 to draw on, and an attribute named C{bbox} for the L{BoundingBox} 32 of the drawing area. 33 """ 34
35 - def __init__(self, context, bbox):
36 """Constructs the drawer and associates it to the given 37 Cairo context and the given L{BoundingBox}. 38 39 @param context: the context on which we will draw 40 @param bbox: the bounding box within which we will draw. 41 Can be anything accepted by the constructor 42 of L{BoundingBox} (i.e., a 2-tuple, a 4-tuple 43 or a L{BoundingBox} object). 44 """ 45 self.context = context 46 self._bbox = None 47 self.bbox = bbox 48 49 @property
50 - def bbox(self):
51 """The bounding box of the drawing area where this drawer will 52 draw.""" 53 return self._bbox 54 55 @bbox.setter
56 - def bbox(self, bbox):
57 """Sets the bounding box of the drawing area where this drawer 58 will draw.""" 59 if not isinstance(bbox, BoundingBox): 60 self._bbox = BoundingBox(bbox) 61 else: 62 self._bbox = bbox 63
64 - def draw(self, *args, **kwds):
65 """Abstract method, must be implemented in derived classes.""" 66 raise NotImplementedError("abstract class") 67
68 - def _mark_point(self, x, y, color=0, size=4):
69 """Marks the given point with a small circle on the canvas. 70 Used primarily for debugging purposes. 71 72 @param x: the X coordinate of the point to mark 73 @param y: the Y coordinate of the point to mark 74 @param color: the color of the marker. It can be a 75 3-tuple (RGB components, alpha=0.5), a 4-tuple 76 (RGBA components) or an index where zero means red, 1 means 77 green, 2 means blue and so on. 78 @param size: the diameter of the marker. 79 """ 80 if isinstance(color, int): 81 colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), 82 (0, 1, 1), (1, 0, 1)] 83 color = colors[color % len(colors)] 84 if len(color) == 3: 85 color += (0.5, ) 86 87 ctx = self.context 88 ctx.save() 89 ctx.set_source_rgba(*color) 90 ctx.arc(x, y, size / 2.0, 0, 2*pi) 91 ctx.fill() 92 ctx.restore()
93
94 ##################################################################### 95 96 -class AbstractXMLRPCDrawer(AbstractDrawer):
97 """Abstract drawer that uses a remote service via XML-RPC 98 to draw something on a remote display. 99 """ 100
101 - def __init__(self, url, service=None):
102 """Constructs an abstract drawer using the XML-RPC service 103 at the given URL. 104 105 @param url: the URL where the XML-RPC calls for the service should 106 be addressed to. 107 @param service: the name of the service at the XML-RPC address. If 108 C{None}, requests will be directed to the server proxy object 109 constructed by C{xmlrpclib.ServerProxy}; if not C{None}, the 110 given attribute will be looked up in the server proxy object. 111 """ 112 import xmlrpclib 113 url = self._resolve_hostname(url) 114 self.server = xmlrpclib.ServerProxy(url) 115 if service is None: 116 self.service = self.server 117 else: 118 self.service = getattr(self.server, service) 119 120 @staticmethod
121 - def _resolve_hostname(url):
122 """Parses the given URL, resolves the hostname to an IP address 123 and returns a new URL with the resolved IP address. This speeds 124 up things big time on Mac OS X where an IP lookup would be 125 performed for every XML-RPC call otherwise.""" 126 from urlparse import urlparse, urlunparse 127 import re 128 129 url_parts = urlparse(url) 130 hostname = url_parts.netloc 131 if re.match("[0-9.:]+$", hostname): 132 # the hostname is already an IP address, possibly with a port 133 return url 134 135 from socket import gethostbyname 136 if ":" in hostname: 137 hostname = hostname[0:hostname.index(":")] 138 hostname = gethostbyname(hostname) 139 if url_parts.port is not None: 140 hostname = "%s:%d" % (hostname, url_parts.port) 141 url_parts = list(url_parts) 142 url_parts[1] = hostname 143 return urlunparse(url_parts)
144

   Home       Trees       Indices       Help