lib/sup/util.rb in sup-0.11 vs lib/sup/util.rb in sup-0.12

- old
+ new

@@ -1,12 +1,22 @@ require 'thread' require 'lockfile' require 'mime/types' require 'pathname' require 'set' +require 'enumerator' +require 'benchmark' ## time for some monkeypatching! +class Symbol + unless method_defined? :to_proc + def to_proc + proc { |obj, *args| obj.send(self, *args) } + end + end +end + class Lockfile def gen_lock_id Hash[ 'host' => "#{ Socket.gethostname }", 'pid' => "#{ Process.pid }", @@ -86,10 +96,23 @@ raise EncodingUnsupportedError, encoding.inspect end a end end + + class Serialize + ## Don't add MIME-Version headers on serialization. Sup sometimes want's to serialize + ## message parts where these headers are not needed and messing with the message on + ## serialization breaks gpg signatures. The commented section shows the original RMail + ## code. + def calculate_boundaries(message) + calculate_boundaries_low(message, []) + # unless message.header['MIME-Version'] + # message.header['MIME-Version'] = "1.0" + # end + end + end end class Range ## only valid for integer ranges (unless I guess it's exclusive) def size @@ -173,10 +196,17 @@ end end EOF end end + + def benchmark s, &b + ret = nil + times = Benchmark.measure { ret = b.call } + debug "benchmark #{s}: #{times}" + ret + end end class String ## nasty multibyte hack for ruby 1.8. if it's utf-8, split into chars using ## the utf8 regex and count those. otherwise, use the byte length. @@ -205,11 +235,11 @@ end ## a very complicated regex found on teh internets to split on ## commas, unless they occurr within double quotes. def split_on_commas - split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/) + normalize_whitespace().split(/,\s*(?=(?:[^"]*"[^"]*")*(?![^"]*"))/) end ## ok, here we do it the hard way. got to have a remainder for purposes of ## tab-completing full email addresses def split_on_commas_with_remainder @@ -299,11 +329,11 @@ each_line &b end end ## takes a list of words, and returns an array of symbols. typically used in - ## Sup for translating Ferret's representation of a list of labels (a string) + ## Sup for translating Xapian's representation of a list of labels (a string) ## to an array of label symbols. ## ## split_on will be passed to String#split, so you can leave this nil for space. def to_set_of_symbols split_on=nil; Set.new split(split_on).map { |x| x.strip.intern } end @@ -457,12 +487,21 @@ end def max_of map { |e| yield e }.max end + + ## returns all the entries which are equal to startline up to endline + def between startline, endline + select { |l| true if l == startline .. l == endline } + end end +unless Object.const_defined? :Enumerator + Enumerator = Enumerable::Enumerator +end + class Array def flatten_one_level inject([]) { |a, e| a + e } end @@ -568,55 +607,21 @@ return nil if @instance.nil? @instance.send meth, *a, &b end def init *args - raise "there can be only one! (instance)" if defined? @instance + raise "there can be only one! (instance)" if instantiated? @instance = new(*args) end end def self.included klass klass.private_class_method :allocate, :new klass.extend ClassMethods end end -## wraps an object. if it throws an exception, keeps a copy. -class Recoverable - def initialize o - @o = o - @error = nil - @mutex = Mutex.new - end - - attr_accessor :error - - def clear_error!; @error = nil; end - def has_errors?; !@error.nil?; end - - def method_missing m, *a, &b; __pass m, *a, &b end - - def id; __pass :id; end - def to_s; __pass :to_s; end - def to_yaml x; __pass :to_yaml, x; end - def is_a? c; @o.is_a? c; end - - def respond_to?(m, include_private=false) - @o.respond_to?(m, include_private) - end - - def __pass m, *a, &b - begin - @o.send(m, *a, &b) - rescue Exception => e - @error ||= e - raise - end - end -end - ## acts like a hash with an initialization block, but saves any ## newly-created value even upon lookup. ## ## for example: ## @@ -705,12 +710,12 @@ when /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/i then 'ASCII' else orig_charset end begin - returning(Iconv.iconv(target, charset, text + " ").join[0 .. -2]) { |str| str.check } + returning(Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2]) { |str| str.check } rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::InvalidCharacter, Iconv::IllegalSequence, String::CheckError - debug "couldn't transcode text from #{orig_charset} (#{charset}) to #{target}) (#{text[0 ... 20].inspect}...) (got #{$!.message} (#{$!.class}))" + debug "couldn't transcode text from #{orig_charset} (#{charset}) to #{target} (#{text[0 ... 20].inspect}...): got #{$!.class} (#{$!.message})" text.ascii end end end