import varnish
import logging
from django.conf import settings

#STUB config options here
VARNISH_HOST = settings.VARNISH_HOST
VARNISH_PORT = settings.VARNISH_PORT

VARNISH_DEBUG = settings.DEBUG

VARNISH_SITE_DOMAIN = ".*"

class VarnishManager(object):
	def __init__(self):
		self.handler = varnish.VarnishHandler([VARNISH_HOST, VARNISH_PORT])

	def __send_command(self, command):
		if VARNISH_DEBUG:
			logging.info("unrun cache command (debug on): {0}".format(command))
		else:
			self.handler.fetch(command.encode('utf-8'))

	def close(self):
		self.handler.close()

	def purge(self, command):
		cmd = r'ban req.http.host ~ "{host}" && req.url ~ "{url}"'.format(
			host = VARNISH_SITE_DOMAIN.encode('ascii'),
			url = command.encode('ascii'),
		)
		self.__send_command(cmd)
		
	def purge_all(self):
		return self.expire('.*')

@@ -157,17 +157,16 @@
 SH_PORT%0A
-%0A
 VARNISH_
@@ -192,35 +192,115 @@
 BUG%0A
-%0AVARNISH_SITE_DOMAIN = %22.*%22
+VARNISH_SECRET = settings.VARNISH_SECRET or None%0AVARNISH_SITE_DOMAIN = settings.VARNISH_SITE_DOMAIN or '.*'
 %0A%0Acl
@@ -340,32 +340,109 @@
 __init__(self):%0A
+%09%09varnish_url = %22%7Bhost%7D:%7Bport%7D%22.format(host=VARNISH_HOST, port=VARNISH_PORT)%0A
 %09%09self.handler =
@@ -469,31 +469,36 @@
 ler(
-%5BVARNISH_HOST, 
+varnish_url, secret=
 VARNISH_
 PORT
@@ -497,13 +497,14 @@
 ISH_
-PORT%5D
+SECRET
 )%0A%0A%09
@@ -987,8 +987,9 @@
 e('.*')%0A
+%0A

Add support for varnish backend secret and pass a domain when it's defined in settings