SiteManagerPlugin.py 2.4 KB

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