Package magma :: Package magma :: Module create_simple_magma_list
[hide private]
[frames] | no frames]

Source Code for Module magma.magma.create_simple_magma_list

  1  #!/usr/bin/env python 
  2  # encoding: utf-8 
  3   
  4  # MAGnet MAnifest Management - Readout and create lists of magnets in yaml format.  
  5  #  
  6  # Copyright © 2008 Arne Babenhauserheide 
  7  #  
  8  # This program is free software: you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation, either version 3 of the License, or 
 11  # (at your option) any later version. 
 12  #  
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/> 
 20   
 21  """ 
 22  Returns a magma-list containing the file name. Takes the names as input. 
 23   
 24  # Test strip_dirname 
 25   
 26      >>> a = "blah/blubb/bangala/goi.gis" 
 27      >>> print strip_dirname(a) 
 28      goi.gis 
 29       
 30      >>> a = "/blah/blubb/bangala/goi.gis" 
 31      >>> print strip_dirname(a) 
 32      goi.gis 
 33       
 34  """ 
 35   
 36  __depends__ = 'yaml, os' 
 37   
 38   
 39   
 40  #: The name of the file list parameter inside the Magma file. 
 41  FILE_LIST_NAME = "files" 
 42  #: The name of the dict holding the hashes and other unique identifiers.  
 43  URN_PARAMETER_NAME = "urn" 
 44  #: The header (magic line) of Magma v0.4 files.  
 45  MAGMA_0_4_HEADER = "#MAGMAv0.4" 
 46   
 47  #: The name of the filename parameter.  
 48  FILENAME_PARAMETER = "filename" 
 49   
 50   
 51  # Get the sha1_gnutella module to create a sha1 for Gnutella.  
 52  from sha1_gnutella import sha1_gnutella 
 53  # And get the yaml dumper and loader 
 54  from yaml import dump, load 
 55  # And the dirname to be able to strip it.  
 56  from os.path import dirname 
 57   
58 -def strip_dirname(path):
59 """Strip everything but the name of the file. 60 61 @param path: A path to a file. 62 @type path: String 63 @return: The name of the file, stripped of anything before it. 64 65 Example: /home/blah/blubb.magma becomes 66 67 blubb.magma 68 """ 69 # We remove just as many characters from the beginning of the string as the dirname is long. 70 if len(dirname(path)) > 0: 71 filename = path[len(dirname(path)) + 1:] 72 else: 73 filename = path 74 return filename
75
76 -class Magma:
77 """A Simple Magma object created from a list of input file paths."""
78 - def __init__(self, inputfilenames, *args, **kwds):
79 """A Simple Magma object created from a list of input file paths. 80 81 @param inputfilenames: The paths to the files to hash. 82 @type inputfilenames: List 83 """ 84 #: The names of the files. 85 self.filenames = inputfilenames 86 #: Sha1 values to the files. The sha1 gets computed for each one here, so this isn't ambigous. 87 self.sha1 = {} 88 # Add sha1 hashs for each file. 89 for i in self.filenames: 90 self.sha1[i] = sha1_gnutella(i) 91 #: The raw python data of the Magma file. 92 self.data = self.data() 93 #: The yaml representation. 94 self.magma = self.magma()
95
96 - def data(self):
97 """Get the Python data of the Magma file (a dict). 98 99 @return: The data of the Magma file as Python dict. """ 100 magma = {} 101 list_of_magnets = [] 102 for i in self.filenames: 103 magnet = {} 104 magnet[URN_PARAMETER_NAME] = {} 105 magnet[URN_PARAMETER_NAME]['sha1'] = self.sha1[i] 106 magnet[FILENAME_PARAMETER] = strip_dirname(i) 107 list_of_magnets.append(magnet) 108 magma[FILE_LIST_NAME] = list_of_magnets 109 return magma
110
111 - def magma(self):
112 """Dump the yaml representation of the file with a MAGMA header as string. 113 114 @return: A string representation of the Magma file (in yaml format). """ 115 return MAGMA_0_4_HEADER + "\n" + dump(self.data, default_flow_style=False) 116
117 -def _test():
118 """Do all doctests 119 120 @return: None 121 """ 122 from doctest import testmod 123 testmod() 124 125 #### Self-Test #### 126 if __name__ == '__main__': 127 from sys import argv 128 if len(argv) > 1: 129 filenames = argv[1:] 130 else: 131 # filename = "magma_liste_erzeugen.py" 132 filenames = [raw_input("Dateiname und Pfad eingeben: ")] 133 gnutella_client_ip = raw_input("Eigene IP oder dyndns-Adresse eingeben: ") 134 magma = Magma(filenames) 135 print magma.magma 136 137 # And test. 138 _test() 139