SiteManagerPlugin.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import logging, json, os, re, sys, time
  2. import gevent
  3. from Plugin import PluginManager
  4. from Config import config
  5. from Debug import Debug
  6. from domainLookup import lookupDomain
  7. allow_reload = False # No reload supported
  8. log = logging.getLogger("Zeroname-localPlugin")
  9. @PluginManager.registerTo("SiteManager")
  10. class SiteManagerPlugin(object):
  11. def load(self):
  12. super(SiteManagerPlugin, self).load()
  13. # Checks if its a valid address
  14. def isAddress(self, address):
  15. if self.isDomain(address):
  16. return True
  17. else:
  18. return super(SiteManagerPlugin, self).isAddress(address)
  19. # Return: True if the address is domain
  20. def isDomain(self, address):
  21. return re.match(r"(.*?)([A-Za-z0-9_-]+\.[A-Za-z0-9]+)$", address)
  22. # Resolve domain
  23. # Return: The address or None
  24. def resolveDomain(self, domain):
  25. return lookupDomain(domain)
  26. # Return or create site and start download site files
  27. # Return: Site or None if dns resolve failed
  28. def need(self, address, all_file=True):
  29. if self.isDomain(address): # Its looks like a domain
  30. address_resolved = self.resolveDomain(address)
  31. if address_resolved:
  32. address = address_resolved
  33. else:
  34. return None
  35. return super(SiteManagerPlugin, self).need(address, all_file)
  36. # Return: Site object or None if not found
  37. def get(self, address):
  38. if self.sites == None: # Not loaded yet
  39. self.load()
  40. if self.isDomain(address): # Its looks like a domain
  41. address_resolved = self.resolveDomain(address)
  42. if address_resolved: # Domain found
  43. site = self.sites.get(address_resolved)
  44. if site:
  45. site_domain = site.settings.get("domain")
  46. if site_domain != address:
  47. site.settings["domain"] = address
  48. else: # Domain not found
  49. site = self.sites.get(address)
  50. else: # Access by site address
  51. site = self.sites.get(address)
  52. return site