Programming and Scripting :: MyDSL info parser



"retval, err" are just the return codes for some function... it depends on their declarations afaik.  It doesn't do anything except that you could print out the error contents (ie message), assuming that the function error was handled correctly and did not exit prematurely.
Quote
if you're talking about that popup dialog, maybe you could unset the DISPLAY env var
I can't say for sure, and haven't tested yet. The popup still displays, but it only displays when there is an error. There's no way to tell if the error or popup is the trouble unless I changed mountci to show a popup on success as well....haven't tried it yet.

Thanks for the info on the return codes. I guess you pretty much have to know exactly what a function returns before deciding to use it. It took a few confusing mistakes before realizing that many functions return multiple values, and I still have trouble making use of them sometimes.

Quote
Maybe pcall?
This looks promising. Thanks.
It sometimes seems to me that either murgaLua itself does not know how to handle Lua errors, or I should be using something other than Fl:run to start the FLTK display (maybe a main loop or something). Considering interactive Lua doesn't completely shut down on an error, I would think the same behavior could be forced with non-interactive.

This version addresses a couple of bugs found by JLS.

1) Added change directory at line 12
2) Removed (actually just commented for now) line 228

Code Sample
#!/bin/murgaLua
-- MyDSL Browser, 2008 mikshaw

mydsldirfile=io.open("/opt/.mydsl_dir")
if not mydsldirfile then
 fltk.fl_alert("Can't open /opt/.mydsl_dir")
 os.exit(1)
end
mydsl_dir=mydsldirfile:read("*l")
mydsldirfile:close()

if not lfs.chdir(mydsl_dir) then
 fltk.fl_alert("Could not enter directory\n"..mydsl_dir)
 os.exit(2)
end

listfile=mydsl_dir.."/mydslinfo.bz2"
listurl="ftp://ibiblio.org/pub/Linux/distributions/damnsmall/mydsl/mydslinfo.bz2"
list_sep="@b@B49@u"

function toggle(b)
local count,state,one,two=1
local me,label=b:value(),b:text(b:value())
if string.find(label,"%+%s*") then
 state,one,two="expanded","%+  ","%-  "
else
 state,one,two="collapsed","%-  ","%+  "
end
b:text(me,string.gsub(label,one,two))
while b:text(me+count) and not string.match(b:text(me+count),list_sep) do
 if state=="collapsed" then b:hide(me+count)
 else b:show(me+count)
 end
 count=count+1
end
end

function fill_list()
inputfile = io.popen("bunzip2 -c "..listfile)
data=inputfile:read("*a")
inputfile:close()
if not string.find(data,"%w") then
 info_display_buffer:text("Cannot read database "..listfile.."\nPlease try updating again.")
else
filelist:clear()
header={}
info={}
info_count=0
local current_loc=""
local headers=0
locount=0
for s in string.gmatch(data,"(Location:.-\n)\n") do
 local datestring=string.match(s,"Current:%s*(.-%d)%s")
 if string.find(datestring,"%d%d%D%d%d%D%d%d%d%d") then datestring=string.gsub(datestring,"(%d%d)%D(%d%d)%D(%d%d%d%d)","%3/%1/%2") end
 info_count=info_count+1
 info[info_count]={
 location=string.match(s,"Location:%s*(.-)%s*\n"),
 title=string.match(s,"Title:%s*(.-)%s*\n"),
 date=string.gsub(datestring,"%D","/"),
 text=s
 }
 if current_loc ~= info[info_count].location then
   current_loc=info[info_count].location
   filelist:add(list_sep.."-  "..current_loc)
   headers=headers+1
   header[headers]=filelist:size()
 end
 filelist:add(info[info_count].title)
end
for i,v in ipairs(header) do filelist:value(header[i]); toggle(filelist) end
last_update:label("Database updated "..os.date("%d %b %Y",lfs.attributes(listfile).modification)..", "..info_count.." packages")
end
filelist:value(1)
tab_browse:setonly()
end

function updatedb()
tempfile=os.tmpname()
os.execute(terminal.." -title \"Database Update\" -e wget "..listurl.." -O "..tempfile)
if os.execute("bzip2 -t "..tempfile) == 0 then
 os.execute("mv "..tempfile.." "..listfile)
 install:activate()
 tab_search:activate()
 tab_browse:activate()
 info_display_buffer:text("\nDatabase updated successfully.")
 fill_list()
else fltk.fl_alert("Database check failed.\nThis may be the result of an incomplete download\nor gremlins in your internets.")
os.remove(tempfile)
end
filelist:take_focus()
end

function do_search()
 if search_text:value() ~= "" and info_count and info_count>0 then
   results:clear()
   results:add("@B49@b@uPackage Name\t@B49@b@uLocation\t@B49@b@uRelease Date")
   for i=1,info_count do
     if string.find(string.lower(info[i].text),string.lower(search_text:value()),1,1) then
       results:add(info[i].title.."\t"..info[i].location.."\t"..info[i].date)
     end
   end
   results:insert(1,"@B49@."..tostring(results:size()-1).." search results")
 end
end

function pick_search_result()
if results:value() > 2 then
 get_loc=string.match(results:text(results:value()),"\t(.*)\t")
 get_fname=string.match(results:text(results:value()),"(.-)\t")
 local myloc=1
 while myloc<=filelist:size() do
   if not string.find(filelist:text(myloc),"  "..get_loc,1,1) then
     myloc=myloc+1
   else filelist:value(myloc)
     break
   end
 end
 if string.find(filelist:text(myloc),list_sep.."+  ",1,1) then toggle(filelist) end
 while myloc<=filelist:size() do
   if filelist:text(myloc) ~= get_fname then
     myloc=myloc+1
   else filelist:value(myloc)
     break
   end
 end
 for i=1,info_count do
   if info[i].title == get_fname and info[i].location == get_loc then
     info_display_buffer:text(info[i].text)
     tab_browse:setonly()
     myfile=info[i]
     break
   end
 end
 tabs:value(tab1)
end
end

function list_cb()
if filelist:value()>0 then
 if Fl:event() == fltk.FL_RELEASE or Fl:event_key()==fltk.FL_Enter then
   if string.find(filelist:text(filelist:value()),list_sep) then
     toggle(filelist)
   else
     local myloc=filelist:value()
     while myloc > 0 do
       myloc=myloc-1
       if string.find(filelist:text(myloc),list_sep) then
         myloc=string.gsub(filelist:text(myloc),".-%s%s","")
         break
       end
     end
     for i=1,info_count do
       if info[i].title == filelist:text(filelist:value()) and info[i].location == myloc then
         info_display_buffer:text(info[i].text)
         myfile=info[i]
         tab_browse:setonly()
         break
       end
     end
   end
 end
end
end

function toggle_list()
if listw > 0 then
 oldw=listw
 tiles:position(listw+5,nil,6,nil)
 listw=0
 info_display:take_focus()
else
 tiles:position(6,nil,oldw+5,nil)
 listw=oldw
 filelist:take_focus()
end
end

terminal="aterm +tr -bg white -fg black -geometry 80x4"
ww=560; wh=380; bh=30; bw=(ww-10)/3
win=fltk:Fl_Window(ww,wh,"MyDSL Browser")

tabs=fltk:Fl_Wizard(0,0,ww,wh-bh-5)
tab1=fltk:Fl_Group(0,0,ww,wh-bh-5,"browse")

tiles=fltk:Fl_Tile(5,5+bh,ww-10,wh-(15+bh*2))
filelist=fltk:Fl_Hold_Browser(5,5+bh,1,wh-(15+bh*2))
filelist:callback(list_cb)

info_display=fltk:Fl_Text_Display(6,5+bh,ww-11,wh-(15+bh*2))
info_display:textfont(fltk.FL_SCREEN)
info_display:textsize(12)
info_display_buffer=fltk:Fl_Text_Buffer()
info_display:buffer(info_display_buffer)
fltk:Fl_End()

tiles:position(6,nil,ww/3,nil)
tiles:callback(function() listw=filelist:w() end)
listw=filelist:w()

prevheader=fltk:Fl_Button(ww,wh,bh,bh,"&[")
prevheader:callback(
function()
for i=table.getn(header),1,-1 do
 if header[i] < filelist:value() then filelist:value(header[i]); break end
end
filelist:take_focus()
end)
nextheader=fltk:Fl_Button(ww,wh,bh,bh,"&]")
nextheader:callback(
function()
for i=1,table.getn(header) do
 if header[i] > filelist:value() then filelist:value(header[i]); break end
end
filelist:take_focus()
end)
enter=fltk:Fl_Return_Button(ww,wh,10,10)
enter:callback(function() filelist:do_callback() end)
togglist=fltk:Fl_Button(ww,wh,10,10,"&t")
togglist:callback(toggle_list)

b_updatedb=fltk:Fl_Button(5,5,bw,bh,"&Update Database")
b_updatedb:callback(updatedb)
install=fltk:Fl_Button(bw+5,5,bw,bh,"&Install Selected Package")
install:callback(
function()
if myfile then
 mydslload=os.execute("mydsl-load "..myfile.title.." "..myfile.location)
--  if mydslload~=0 then fltk.fl_alert(myfile.title..":\nDownload or Checksum error!") end
end
filelist:take_focus()
end)
install_local=fltk:Fl_Button(bw*2+5,5,bw,bh,"Install &Local Package  @DnArrow")
install_local:callback(
function()
local filename=fltk.fl_file_chooser("Browse for a MyDSL file","Mydsl files (*.{dsl,tar.gz,uci,unc})",mydsl_dir,nil)
if filename then os.execute("mydsl-load "..filename) end
end)
fltk:Fl_End()

tab2=fltk:Fl_Group(0,0,ww,wh-bh-5,"search text")
search_text=fltk:Fl_Input(bh*3+5,5,ww-10-bh*3,bh,"search text ")
search_text:when(fltk.FL_WHEN_ENTER_KEY)
search_text:callback(do_search)

results=fltk:Fl_Hold_Browser(5,5+bh,ww-10,wh-(15+bh*2))
results:column_widths({results:w()/2,150,0})
results:callback( function() if results:value() > 1 and Fl:event()==fltk.FL_RELEASE then pick_search_result() end end)
results_cb=fltk:Fl_Return_Button(ww,wh,bh,bh)
results_cb:callback(pick_search_result)
fltk:Fl_End()
fltk:Fl_End()

tabbuttons=fltk:Fl_Pack(5,wh-bh,200,bh-5)
tabbuttons:type(fltk.FL_HORIZONTAL)
tabbuttons:spacing(5)
tab_browse=fltk:Fl_Radio_Button(0,0,100,bh-5,"&browse")
tab_browse:when(fltk.FL_WHEN_CHANGED)
tab_browse:box(fltk.FL_BORDER_BOX); tab_browse:selection_color(7)
tab_browse:callback(function() tabs:value(tab1) end)
tab_search=fltk:Fl_Radio_Button(0,0,100,bh-5,"text &search")
tab_search:when(fltk.FL_WHEN_CHANGED)
tab_search:box(fltk.FL_BORDER_BOX); tab_search:selection_color(7)
tab_search:callback(function() tabs:value(tab2); Fl:focus(search_text) end)
fltk:Fl_End()
last_update=fltk:Fl_Box(ww-5,wh-bh-5,5,bh+5)
last_update:align(fltk.FL_ALIGN_LEFT)
last_update:labelsize(12)

win:resizable(tabs)
tabs:resizable(tab1)
tabs:resizable(tab2)
tabs:resizable(tab3)
tab1:resizable(tiles)
tab2:resizable(results)
win:show()

find_file=io.open(listfile)
if find_file then data=find_file:read("*a") else data="" end
if string.find(data,"%w") then find_file:close(); fill_list()
else
 ask4db=fltk.fl_choice("You have no database for online MyDSL apps.\nDo you want to download it now?","No","Yes",NULL)
 if ask4db==1 then Fl:check(); updatedb() else
 install:deactivate(); tab_search:deactivate(); tab_browse:deactivate()
 info_display_buffer:text("\nThere is no MyDSL database installed.\nClick \"Update Database\" \(Alt+U\) to download it.")
 end
end
Fl:run()

Thanks. I am including it in final 4.3 image.
You just made the cutoff.

=o)
Next Page...
original here.