zephir.monitor.agentmanager.clientmanager

Package zephir :: Package monitor :: Package agentmanager :: Module clientmanager |
[frames] | no frames]

# Source Code for Module zephir.monitor.agentmanager.clientmanager

      1  # -*- coding: UTF-8 -*-
      2  ###########################################################################
      3  # Eole NG - 2007
      4  # Copyright Pole de Competence Eole  (Ministere Education - Academie Dijon)
      5  # Licence CeCill  cf /root/LicenceEole.txt
      6  # eole@ac-dijon.fr
      7  ###########################################################################
      8
      9  """
     10
     11  """
     12
     13  try: _ # localized string fetch function
     14  except NameError: _ = str
     15
     16  import os, dircache
     17
     18  from zephir.monitor.agentmanager import config as cfg
     19  from zephir.monitor.agentmanager.agentdatamanager import AgentDataManager, LiveAgentsManager
     20
     21
     22
     23 -class ClientManager:
     24      """Structure d'accès aux postes clients.
     25
     26      Se comporte comme un dictionnaire C{{'nom client': AgentManager}}.
     27
     28      TODO: utiliser UserDict.DictMixin
     29      """
     30
     31 -    def __init__(self, config, live_agents=None):
     32          """
     33          @param live_agents: dictionnaire C{{client_name:
     34            L{LiveAgentsManager}}} pour les éventuels agents chargés en
     35            local.
     36          """
     37          self.config = config
     38          self.live_agents = {}
     39          if live_agents is not None:
     40              for client_name, agents in live_agents.items():
     41                  self.live_agents[client_name] = LiveAgentsManager(self.config, client_name, agents)
     42          self.cache = {}
     43
     44 -    def __getitem__(self, client_name):
     45          if self.has_live_key(client_name):
     46              return self.live_agents[client_name]
     47          elif self.has_archive_key(client_name):
     48              try:
     49                  agents = self.cache[client_name]
     50              except KeyError:
     51                  agents = AgentDataManager(self.config, client_name)
     52                  self.cache[client_name] = agents
     53              return agents
     54          else:
     55              # data no more on disk, so cleanup the cache if it still
     56              # has an entry
     57              if self.cache.has_key(client_name):
     58                  del self.cache[client_name]
     59              return None
     60
     61 -    def has_live_key(self, client_name):
     62          """
     63          @return: C{True} si C{client_name} est le nom d'un agent local
     64          """
     65          return self.live_agents.has_key(client_name)
     66
     67 -    def has_archive_key(self, client_name):
     68          """
     69          @return: C{True} si C{client_name} est le nom d'un agent
     70            archivé
     71          """
     72          d = cfg.client_data_dir(self.config, client_name)
     73          client_data_dir_exists = os.path.isdir(d)
     74          client_structure_exists = os.path.isfile(os.path.join(d, 'site.cfg'))
     75          return client_data_dir_exists and client_structure_exists
     76
     77 -    def has_key(self, client_name):
     78          return (self.has_live_key(client_name)
     79                  or self.has_archive_key(client_name))
     80
     81 -    def keys(self):
     82          d = self.config['state_dir']
     83          archives = []
     84          for k in dircache.listdir(d):
     85              if self.has_archive_key(k) and not self.has_live_key(k):
     86                  archives.append(k) # maybe add a link to the archived data in the web page?
     87          return self.live_agents.keys() + archives
     88
     89 -    def items(self): #FIXME duplicated with AgentManager.items
     90          result = []
     91          for k in self.keys():
     92              result.append([k, self[k]])
     93          return result
     94
     95  # def test_main():
     96  #     test_support.run_unittest(UserStringTest)
     97
     98  # if __name__ == "__main__":
     99  #     test_main()
    100