2013-10-24 4 views
-1

Это может или не может быть легким для кого-то там ...список Сложение каталогов и файлов на сайт

Я пытаюсь создать файл восстановления службы. У меня есть словарь, который выглядит так.

{'full_path': '/home/michael/Data', 
'items': {'UpgradeToCentOS5': {'full_path': '/home/michael/Data/UpgradeToCentOS5', 
           'type': 'f', 
           'versions': ['current', '09/10/12']}, 
      'basic_debian': {'full_path': '/home/michael/Data/basic_debian', 
          'type': 'f', 
          'versions': ['current']}, 
      'deploy.tgz': {'full_path': '/home/michael/Data/deploy.tgz', 
          'type': 'f', 
          'versions': ['current']}, 
      'firewall': {'full_path': '/home/michael/Data/firewall', 
         'type': 'f', 
         'versions': ['current']}, 
      'legalholder.sh': {'full_path': '/home/michael/Data/legalholder.sh', 
           'type': 'f', 
           'versions': ['current']}, 
      'lists': {'full_path': '/home/michael/Data/lists', 
        'items': {'boothosts': {'full_path': '/home/michael/Data/lists/boothosts', 
              'type': 'f', 
              'versions': ['current']}, 
           'checklist': {'full_path': '/home/michael/Data/lists/checklist', 
              'type': 'f', 
              'versions': ['current']}, 
           'ns_ip': {'full_path': '/home/michael/Data/lists/ns_ip', 
             'type': 'f', 
             'versions': ['current']}, 
           'server_info': {'full_path': '/home/michael/Data/lists/server_info', 
               'type': 'f', 
               'versions': ['current']}, 
           'temp': {'full_path': '/home/michael/Data/lists/temp', 
             'type': 'f', 
             'versions': ['current']}, 
           'tsm_clients': {'full_path': '/home/michael/Data/lists/tsm_clients', 
               'type': 'f', 
               'versions': ['current']}}, 
        'type': 'd', 
        'versions': ['current']}, 
      'salt': {'full_path': '/home/michael/Data/salt', 
        'type': 'f', 
        'versions': ['current']}, 
      'std-srv': {'full_path': '/home/michael/Data/std-srv', 
         'type': 'f', 
         'versions': ['current']}, 
      'upgrade_debian_6': {'full_path': '/home/michael/Data/upgrade_debian_6', 
           'type': 'f', 
           'versions': ['current']}, 
      'using-imaps': {'full_path': '/home/michael/Data/using-imaps', 
          'type': 'f', 
          'versions': ['current']}, 
      'xiv_online_resize': {'full_path': '/home/michael/Data/xiv_online_resize', 
           'type': 'f', 
           'versions': ['current']}}, 
'type': 'd', 
'versions': ['current']} 

В основном ... это словарь для данных, которые я нахожу, которые можно восстановить. Все файлы (не уверены в каталогах) будут иметь «версии», которые представляют собой список версий, существующих для этого файла.

Я пытаюсь выяснить, как я собираюсь превратить это в данные HTML, которые пользователь может использовать для выбора версии файлов, которые они хотят восстановить. (Ограничение одна версия восстановления для каждого файла.

Я никогда не строили ничего подобного, и я не подтягивания ничего в поиске.

Все, что я пропускаю в моих деталях?

I/думать/что я ищу что-то вроде этого ...

- home 
-- /michael 
--- /Data 
---- UpgradeToCentOS5 
     □ Version: current 
     □ Version: 09/10/12 
---- basic_debian 
     □ Version: current 
---- deploy.tgz 
     □ Version: current 
---- firewall 
     □ Version: current 
---- legalholder.sh 
     □ Version: current 
---- lists 
----- boothosts 
     □ Version: current 
[...] 

ответ

1

Я использовал это, чтобы произвести что-то вроде вашего примера.

recovery = {'full_path': '/home/michael/Data', 
'items': {'UpgradeToCentOS5': {'full_path': '/home/michael/Data/UpgradeToCentOS5', 
           'type': 'f', 
           'versions': ['current', '09/10/12']}, 
      'basic_debian': {'full_path': '/home/michael/Data/basic_debian', 
          'type': 'f', 
          'versions': ['current']}, 
      'deploy.tgz': {'full_path': '/home/michael/Data/deploy.tgz', 
          'type': 'f', 
          'versions': ['current']}, 
      'firewall': {'full_path': '/home/michael/Data/firewall', 
         'type': 'f', 
         'versions': ['current']}, 
      'legalholder.sh': {'full_path': '/home/michael/Data/legalholder.sh', 
           'type': 'f', 
           'versions': ['current']}, 
      'lists': {'full_path': '/home/michael/Data/lists', 
        'items': {'boothosts': {'full_path': '/home/michael/Data/lists/boothosts', 
              'type': 'f', 
              'versions': ['current']}, 
           'checklist': {'full_path': '/home/michael/Data/lists/checklist', 
              'type': 'f', 
              'versions': ['current']}, 
           'ns_ip': {'full_path': '/home/michael/Data/lists/ns_ip', 
             'type': 'f', 
             'versions': ['current']}, 
           'server_info': {'full_path': '/home/michael/Data/lists/server_info', 
               'type': 'f', 
               'versions': ['current']}, 
           'temp': {'full_path': '/home/michael/Data/lists/temp', 
             'type': 'f', 
             'versions': ['current']}, 
           'tsm_clients': {'full_path': '/home/michael/Data/lists/tsm_clients', 
               'type': 'f', 
               'versions': ['current']}}, 
        'type': 'd', 
        'versions': ['current']}, 
      'salt': {'full_path': '/home/michael/Data/salt', 
        'type': 'f', 
        'versions': ['current']}, 
      'std-srv': {'full_path': '/home/michael/Data/std-srv', 
         'type': 'f', 
         'versions': ['current']}, 
      'upgrade_debian_6': {'full_path': '/home/michael/Data/upgrade_debian_6', 
           'type': 'f', 
           'versions': ['current']}, 
      'using-imaps': {'full_path': '/home/michael/Data/using-imaps', 
          'type': 'f', 
          'versions': ['current']}, 
      'xiv_online_resize': {'full_path': '/home/michael/Data/xiv_online_resize', 
           'type': 'f', 
           'versions': ['current']}}, 
'type': 'd', 
'versions': ['current']} 

import os 

def get_parent_dirs(path): 
    parents = [] 
    x = path.split('/')[1:-1] 
    for i in range(len(x)): 
     parents.append('/'.join(x[0:i+1])) 
    return parents 

# Get a list of files and versions from the dictionary 
items = [] 
pending = [recovery] 
while pending: 
    x = pending.pop(0) 
    for item in x['items']: 
     item = x['items'][item] 
     if item.has_key('items'): # a directory 
      pending.append(item) 
     else: # a file 
      items.append((item['full_path'], item['versions'])) 
items.sort() 

parent_dirs = [] 
for path, versions in items: 

    # Print parent directories if necessary 
    for directory in get_parent_dirs(path): 
     if not directory in parent_dirs: 
      parent_dirs.append(directory) 
      print '%s /%s' % (len(directory.split('/'))*'-', os.path.split(directory)[1]) 

    # Print file versions 
    directory, filename = os.path.split(path) 
    padding = len(path.split('/'))-1 
    print '%s %s' % (padding*'-', filename) 
    for version in versions: 
     print '%s [] Version: %s' % (' '*padding, version) 
Смежные вопросы