import os

class Browser:
	# registered factories
    	factory = {}

	def __init__(self, host, username, password, name):
		self._provider = self.__class__.provider
		self._host = host
		self._username = username
		self._password = password
		self._name = name

        def install(self):
            import slideshow
            filename = os.path.join(os.path.dirname(slideshow.__file__), 'install', self.__class__._install)
            lines = "\n".join(open(filename, 'r').readlines())
            self.executescript(lines)

	def __str__(self):
		return self.string(password=False)

	def string(self, password=False):
		credentials = ''
		if self._username != '':
			credentials = self._username

			# if plain-text password is enable
			if password and self._password != '':
				credentials += ':' + self._password

			credentials += '@'

		hostname = self._host
		if hostname != '':
			hostname += '/'

		return '{provider}://{credentials}{hostname}{name}'.format(provider=self._provider, credentials=credentials, hostname=hostname, name=self._name)

def register(name):
    def inner(cls):
        Browser.factory[name] = cls
        cls.provider = name
        return cls
    return inner

def on_install(name):
    def inner(cls):
        cls._install = name
        return cls
    return inner

def from_string(string):
    m = re.match('([a-z]+)://(.+)', string)
    if m is None:
        raise RuntimeError, 'string %s is not a valid browser string!' % string

    provider = m.group(1)
    name = m.group(2)
    
    Browser.factory[provider](None, None, None, name)
    
def from_settings(settings):
    provider = settings['Database.Provider']
    username = settings['Database.Username']
    password = settings['Database.Password']
    hostname = settings['Database.Hostname']
    name = settings['Database.Name']
    
    return Browser.factory[provider](hostname, username, password, name)

import slideshow.lib.browser._sqlite3

@@ -86,16 +86,20 @@
 lf, host
+name
 , userna
@@ -112,19 +112,23 @@
 ssword, 
-nam
+databas
 e):%0A%09%09se
@@ -126,25 +126,24 @@
 se):%0A%09%09self.
-_
 provider = s
@@ -176,28 +176,35 @@
 elf.
-_
 host
+name
  = host
+name
 %0A%09%09self.
 _use
@@ -195,25 +195,24 @@
 name%0A%09%09self.
-_
 username = u
@@ -222,25 +222,24 @@
 name%0A%09%09self.
-_
 password = p
@@ -257,19 +257,26 @@
 elf.
-_name = nam
+database = databas
 e%0A%0A 
@@ -659,25 +659,24 @@
 '%0A%09%09if self.
-_
 username != 
@@ -701,17 +701,16 @@
  = self.
-_
 username
@@ -769,25 +769,24 @@
 rd and self.
-_
 password != 
@@ -819,17 +819,16 @@
  + self.
-_
 password
@@ -874,13 +874,16 @@
 elf.
-_
 host
+name
 %0A%09%09i
@@ -995,17 +995,16 @@
 er=self.
-_
 provider
@@ -1063,12 +1063,15 @@
 elf.
-_nam
+databas
 e)%0A%0A
@@ -1807,27 +1807,31 @@
 tname'%5D%0A    
-nam
+databas
 e = settings
@@ -1920,19 +1920,23 @@
 ssword, 
-nam
+databas
 e)%0A%0Aimpo
@@ -1969,8 +1969,44 @@
 sqlite3%0A
+import slideshow.lib.browser._mysql%0A

rename fields to make more sense (also breaks all backens)