1 #!/usr/bin/env python 2 3 """ 4 File access. 5 6 Copyright (C) 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program. If not, see <http://www.gnu.org/licenses/>. 19 """ 20 21 from iixr.terms import * 22 from os import listdir, remove, rename # partition manipulation 23 from shutil import copy # index updating 24 from os.path import join 25 26 try: 27 set 28 except NameError: 29 from sets import Set as set 30 31 # Constants. 32 33 TERM_FILENAMES = "terms", "term-index" 34 35 # Utility functions. 36 37 def get_partitions(pathname, prefix): 38 39 """ 40 Return a set of partition names for partitions residing at the given 41 'pathname' having the given 'prefix'. 42 """ 43 44 prefix_length = len(prefix) 45 46 partitions = set() 47 for filename in listdir(pathname): 48 if filename.startswith(prefix): 49 partition = filename[prefix_length:] 50 if partition.isdigit(): 51 partitions.add(int(partition)) 52 return partitions 53 54 def get_term_partitions(pathname): 55 56 """ 57 Return a set of term partition identifiers for partitions residing at the 58 given 'pathname'. 59 """ 60 61 return get_partitions(pathname, "terms-") 62 63 def get_next_partition(partitions): 64 return max(partitions or [-1]) + 1 65 66 def get_writer(pathname, name, partition, cls): 67 f = open(join(pathname, "%s-%s" % (name, partition)), "wb") 68 return cls(f) 69 70 def get_term_writer(pathname, partition): 71 72 """ 73 Return a term writer using files under the given 'pathname' labelled 74 according to the given 'partition'. 75 """ 76 77 return get_writer(pathname, "terms", partition, TermWriter) 78 79 def get_term_index_writer(pathname, partition): 80 81 """ 82 Return a term index writer using files under the given 'pathname' labelled 83 according to the given 'partition'. 84 """ 85 86 return get_writer(pathname, "term-index", partition, TermIndexWriter) 87 88 def get_reader(pathname, name, partition, cls): 89 f = open(join(pathname, "%s-%s" % (name, partition)), "rb") 90 return cls(f) 91 92 def get_term_reader(pathname, partition): 93 94 """ 95 Return a term reader using files under the given 'pathname' labelled 96 according to the given 'partition'. 97 """ 98 99 return get_reader(pathname, "terms", partition, TermIterator) 100 101 def get_term_data_reader(pathname, partition): 102 103 """ 104 Return a term plus data reader using files under the given 'pathname' 105 labelled according to the given 'partition'. 106 """ 107 108 return get_reader(pathname, "terms", partition, TermDataIterator) 109 110 def get_term_index_reader(pathname, partition): 111 112 """ 113 Return a term index reader using files under the given 'pathname' labelled 114 according to the given 'partition'. 115 """ 116 117 return get_reader(pathname, "term-index", partition, TermIndexIterator) 118 119 def get_combined_term_reader(pathname, partition): 120 121 """ 122 Return a combined term reader using files under the given 'pathname' 123 labelled according to the given 'partition'. 124 """ 125 126 return CombinedIterator(get_term_reader(pathname, partition), get_term_index_reader(pathname, partition)) 127 128 # Renaming. 129 130 def rename_files(pathname, names, from_partition, to_partition): 131 for name in names: 132 try: 133 rename(join(pathname, "%s-%s" % (name, from_partition)), join(pathname, "%s-%s" % (name, to_partition))) 134 except OSError: 135 pass 136 137 def rename_term_files(pathname, from_partition, to_partition): 138 rename_files(pathname, TERM_FILENAMES, from_partition, to_partition) 139 140 # Removal/deletion. 141 142 def remove_files(pathname, names, partition): 143 for name in names: 144 try: 145 remove(join(pathname, "%s-%s" % (name, partition))) 146 except OSError: 147 pass 148 149 def remove_term_files(pathname, partition): 150 remove_files(pathname, TERM_FILENAMES, partition) 151 152 # Copying. 153 154 def copy_files(source, names, partition, destination, suffix): 155 for name in names: 156 filename = "%s-%s" % (name, partition) 157 try: 158 copy(join(source, filename), join(destination, filename + suffix)) 159 except OSError: 160 pass 161 162 def copy_term_files(source, partition, destination, suffix): 163 copy_files(source, TERM_FILENAMES, partition, destination, suffix) 164 165 # vim: tabstop=4 expandtab shiftwidth=4