Package backend :: Package satellite_tools :: Module SequenceServer
[hide private]
[frames] | no frames]

Source Code for Module backend.satellite_tools.SequenceServer

 1  # 
 2  # Copyright (c) 2008--2015 Red Hat, Inc. 
 3  # 
 4  # This software is licensed to you under the GNU General Public License, 
 5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 8  # along with this software; if not, see 
 9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
10  # 
11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
12  # granted to use or replicate Red Hat trademarks that are incorporated 
13  # in this software or its documentation. 
14  # 
15  # class SequenceServer, a class that nicely serves chunks 
16  #    of any sequence. 
17  # 
18   
19   
20 -class SequenceServer:
21 22 """Given a sequence to serve, this class dishes them out in chunks 23 or one at a time. This was written originally to reduce redundant 24 code. Used only by class Syncer. 25 26 This class was written so that a chunk can be snagged and 27 processed. If the processing fails, one can tell the class to only 28 present one at a time until that chunk is complete... and then 29 resume normal operations. 30 31 For example: the sequence can be a bunch of packageIds. We try to 32 process all data that corresponds to some chunk of those package 33 ids. If that process fails, we go back and reprocess that chunk, 34 one at a time, until it is complete (identifying precisely which 35 package broke). Then we resume processing a chunk at a time. 36 37 """ 38 39 DIVISOR = 10 40 NEVER_LESS_THAN = 10 41 NEVER_MORE_THAN = 50 42
43 - def __init__(self, seq, divisor=None, neverlessthan=None, nevermorethan=None):
44 """Constructor. 45 46 Arguments: 47 48 seq - any sequence to server chunks of 49 divisor - the chunk size: 10 would be 1/10th of the seq length 50 neverlessthan - chunk-size is never less than this (unless out 51 of data). 52 nevermorethan - chunk-size if never more than this number. 53 54 """ 55 56 divisor = divisor or self.DIVISOR 57 neverlessthan = neverlessthan or self.NEVER_LESS_THAN 58 nevermorethan = nevermorethan or self.NEVER_MORE_THAN 59 60 self.seq = seq 61 self.chunksize = min(max(len(seq) / divisor, neverlessthan), 62 nevermorethan) 63 self.oneYN = 0 64 self.alwaysOneYN = 0 65 self.returnedChunksize = 0 66 self.chunk = self.seq[:self.chunksize]
67
68 - def getChunk(self):
69 """fetch a chunk from the sequence. 70 Does not refresh the chunk until you self.clearChunk()""" 71 72 if not self.chunk: 73 self.chunk = self.seq[:self.chunksize] 74 if self.oneYN or self.alwaysOneYN: 75 _chunk = self.chunk[:1] 76 else: 77 _chunk = self.chunk 78 self.returnedChunksize = len(_chunk) 79 return _chunk
80
81 - def clearChunk(self):
82 """zero the self.chunk you were working with.""" 83 84 del self.chunk[:self.returnedChunksize] 85 del self.seq[:self.returnedChunksize] 86 if not self.chunk: 87 self.oneYN = 0
88
89 - def doneYN(self):
90 return not self.seq
91