Package osh :: Package command :: Module unique
[frames] | no frames]

Source Code for Module osh.command.unique

  1  # osh 
  2  # Copyright (C) Jack Orenstein <jao@geophile.com> 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published by 
  6  # the Free Software Foundation; either version 2 of the License, or 
  7  # (at your option) any later version. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with this program; if not, write to the Free Software 
 16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 17   
 18  """C{unique [-c]} 
 19   
 20  Input objects are passed to output, removing duplicates. No output is 
 21  generated until the end of the input stream occurs. However, if the 
 22  duplicates are known to be consecutive, then specifying C{-c} allows 
 23  output to be generated sooner. Input order is preserved only if C{-c} 
 24  is specified. 
 25   
 26  """ 
 27   
 28  import types 
 29   
 30  import osh.args 
 31  import osh.core 
 32   
 33  Option = osh.args.Option 
 34   
 35  # CLI 
36 -def _unique():
37 return _Unique()
38 39 # API
40 -def unique(consecutive = False):
41 """Input objects are passed to output, removing duplicates. No output 42 is generated until the end of the input stream occurs. However, if the 43 duplicates are known to be consecutive, then setting C{consecutive} to true 44 allows output to be generated sooner. Input order is preserved only if 45 C{consecutive} is true. 46 """ 47 args = [] 48 if consecutive: 49 args.append(Option('-c')) 50 return _Unique().process_args(*args)
51
52 -class _Unique(osh.core.Op):
53 54 _uniquer = None 55 56 57 # object interface 58
59 - def __init__(self):
60 osh.core.Op.__init__(self, 'c', (0, 0))
61 62 63 # BaseOp interface 64
65 - def doc(self):
66 return __doc__
67
68 - def setup(self):
69 args = self.args() 70 if args.has_next(): 71 self.usage() 72 if args.flag('-c'): 73 self._uniquer = _ConsecutiveUniquer(self) 74 else: 75 self._uniquer = _GeneralUniquer(self)
76
77 - def receive(self, object):
78 self._uniquer.receive(object)
79
80 - def receive_complete(self):
81 self._uniquer.receive_complete()
82 83
84 -class _GeneralUniquer(object):
85 86 _command = None 87 _unique = None 88
89 - def __init__(self, command):
90 self._command = command 91 self._unique = {}
92
93 - def receive(self, object):
94 if type(object) == types.ListType: 95 object = tuple(object) 96 self._unique[object] = None
97
98 - def receive_complete(self):
99 for key in self._unique.keys(): 100 self._command.send(key) 101 self._command.send_complete()
102
103 -class _ConsecutiveUniquer(object):
104 105 _command = None 106 _current = None 107
108 - def __init__(self, command):
109 self._command = command
110
111 - def receive(self, object):
112 if self._current != object: 113 if self._current is not None: 114 self._command.send(self._current) 115 self._current = object
116
117 - def receive_complete(self):
118 if self._current is not None: 119 self._command.send(self._current) 120 self._command.send_complete()
121