whimsical-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [whimsy] branch master updated: Allow output to named file or stdout
Date Fri, 29 May 2020 13:29:29 GMT
This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new 56511a8  Allow output to named file or stdout
56511a8 is described below

commit 56511a818fe9ddf8e2f0cb2927be6a52f081fb81
Author: Sebb <sebb@apache.org>
AuthorDate: Fri May 29 14:29:18 2020 +0100

    Allow output to named file or stdout
    
    Also default optional args where possible
---
 tools/ponyapi.rb | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/tools/ponyapi.rb b/tools/ponyapi.rb
old mode 100644
new mode 100755
index ad327d8..29db95c
--- a/tools/ponyapi.rb
+++ b/tools/ponyapi.rb
@@ -25,7 +25,7 @@ module PonyAPI
   # you will be prompted to enter the cookie value
   # The method writes the file 'lists.json' if dir != nil
   # it returns the data as a hash
-  def get_pony_lists(dir, cookie=nil, sort_list=false)
+  def get_pony_lists(dir=nil, cookie=nil, sort_list=false)
     cookie=get_cookie() if cookie == 'prompt'
     jzon = get_pony_prefs(nil, cookie)
     lists = jzon['lists']
@@ -33,7 +33,7 @@ module PonyAPI
       if dir
         # no real point sorting unless writing the file
         lists = Hash[lists.sort] if sort_list
-        File.open(File.join("#{dir}", 'lists.json'), "w") do |f|
+        openfile(dir, 'lists.json') do |f|
           begin
             f.puts JSON.pretty_generate(lists)
           rescue JSON::GeneratorError => e
@@ -52,7 +52,7 @@ module PonyAPI
   # you will be prompted to enter the cookie value
   # The method writes the file 'preferences.json' if dir != nil
   # it returns the data as a hash
-  def get_pony_prefs(dir, cookie=nil, sort_list=false)
+  def get_pony_prefs(dir=nil, cookie=nil, sort_list=false)
     cookie=get_cookie() if cookie == 'prompt'
     uri, request, response = fetch_pony(PONYPREFS, cookie)
     jzon = {}
@@ -61,7 +61,7 @@ module PonyAPI
       if dir
         # no real point sorting unless writing the file
         jzon['lists'] = Hash[jzon['lists'].sort] if sort_list && jzon['lists']
-        File.open(File.join("#{dir}", 'preferences.json'), "w") do |f|
+        openfile(dir, 'preferences.json') do |f|
           begin
             f.puts JSON.pretty_generate(jzon)
           rescue JSON::GeneratorError
@@ -83,12 +83,12 @@ module PonyAPI
 
   # Download one month of stats as a JSON
   # Must supply cookie = 'ponymail-logged-in-cookie' if a private list
-  def get_pony_stats(dir, list, subdomain, year, month, cookie)
+  def get_pony_stats(dir, list, subdomain, year, month, cookie=nil)
     cookie=get_cookie() if cookie == 'prompt'
     args =  make_args(list, subdomain, year, month)
     uri, request, response = fetch_pony(PONYSTATS % args, cookie)
     if response.code == '200' then
-      File.open(File.join(dir, STATSMBOX % args), "w") do |f|
+      openfile(dir, STATSMBOX % args) do |f|
         begin
           f.puts JSON.pretty_generate(JSON.parse(response.body))
         rescue JSON::JSONError
@@ -108,7 +108,7 @@ module PonyAPI
   end
   
   # Get multiple years/months of public stats as json
-  def get_pony_stats_many(dir, list, subdomain, years, months, cookie)
+  def get_pony_stats_many(dir, list, subdomain, years, months, cookie=nil)
     cookie=get_cookie() if cookie == 'prompt'
     years.each do |y|
       months.each do |m|
@@ -120,12 +120,12 @@ module PonyAPI
   # Download one month as mbox
   # Caveats: uses response's encoding; overwrites existing .json file
   # Must supply cookie = 'ponymail-logged-in-cookie' if a private list
-  def get_pony_mbox(dir, list, subdomain, year, month, cookie)
+  def get_pony_mbox(dir, list, subdomain, year, month, cookie=nil)
     cookie=get_cookie() if cookie == 'prompt'
     args =  make_args(list, subdomain, year, month)
     uri, request, response = fetch_pony(PONYMBOX % args, cookie)
     if response.code == '200'
-      File.open(File.join(dir, FILEMBOX % args), "w:#{response.body.encoding}") do |f|
+      openfile(dir, FILEMBOX % args, "w:#{response.body.encoding}") do |f|
         f.puts response.body
       end
     else
@@ -134,7 +134,7 @@ module PonyAPI
   end
   
   # Get multiple years/months of mboxes
-  def get_pony_mbox_many(dir, list, subdomain, years, months, cookie)
+  def get_pony_mbox_many(dir, list, subdomain, years, months, cookie=nil)
     cookie=get_cookie() if cookie == 'prompt'
     years.each do |y|
       months.each do |m|
@@ -146,6 +146,19 @@ module PonyAPI
 
   private
 
+  # Open the output file
+  # if dir == '-' then use stdout
+  # if dir ends with '.json' then treat it as the full file name
+  def openfile(dir,file, mode='w')
+    if dir == '-'
+      yield $stdout
+    elsif dir.end_with? '.json'
+      yield File.open(dir, mode)
+    else
+      yield File.open(File.join(dir, file), mode)
+    end
+  end
+
   # create an argument list suitable for string formatting
   def make_args(list, subdomain, year, month)
     if subdomain.nil? || subdomain == ''


Mime
View raw message