SiteManagerPlugin.py 3.0 KB

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