Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 18th, 2012  |  syntax: None  |  size: 1.50 KB  |  views: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/env ruby
  2. #-*- coding: utf-8 -*-
  3. require("rexml/document");
  4.  
  5. def compare_entry_by_property_value(e1,e2,property)
  6.   p1 = REXML::XPath.first(e1,"apps:property[@name='#{property}']");
  7.   p2 = REXML::XPath.first(e2,"apps:property[@name='#{property}']");
  8.   return p1.attribute("value").value <=> p2.attribute("value").value if (p1 && p2);
  9.   return -1 if (p1);
  10.   return +1 if (p2);
  11.   return 0;
  12. end
  13.  
  14. def compare_entry_by_property_exists(e1,e2,property)
  15.   p1 = REXML::XPath.first(e1,"apps:property[@name='#{property}']");
  16.   p2 = REXML::XPath.first(e1,"apps:property[@name='#{property}']");
  17.   return -1 if (p1 && !p2);
  18.   return +1 if (!p1 && p2);
  19.   return 0;
  20. end
  21.  
  22.  
  23. def compare_entry(e1,e2)
  24.   c = compare_entry_by_property_value(e1,e2,"label");
  25.   return  c if (c!=0);
  26.  
  27.   c = compare_entry_by_property_exists(e1,e2,"shouldTrash");
  28.   return -c if (c!=0);
  29.   c = compare_entry_by_property_exists(e1,e2,"shouldArchive");
  30.   return -c if (c!=0);
  31.   c = compare_entry_by_property_exists(e1,e2,"shouldMarkAsRead");
  32.   return -c if (c!=0);
  33.   c = compare_entry_by_property_exists(e1,e2,"shouldStar");
  34.   return  c if (c!=0);
  35.  
  36.   c = compare_entry_by_property_value(e1,e2,"from");
  37.   return c if (c!=0);
  38.  
  39.   return 0;
  40. end
  41.  
  42. doc = REXML::Document.new(File.open(ARGV[0]));
  43. entries = REXML::XPath.match(doc,"/feed/entry");
  44.  
  45. entries.sort! { |e1,e2| compare_entry(e1,e2); }
  46.  
  47. puts("<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>");
  48. entries.each { |entry| puts(entry); }
  49. puts("</feed>");