WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: (tcz) info 2 JSON (the GOAT)  (Read 97 times)

Offline mocore

  • Hero Member
  • *****
  • Posts: 711
  • ~.~
(tcz) info 2 JSON (the GOAT)
« on: March 03, 2025, 10:45:57 AM »

- Ability to change the rendering format of .info files in an easier way


As for the format itself, any format (except XML, please) does the work. CSV, JSON (the GOAT), JSONC, YAML and TOML are a few options that I currently think would work fine.


idk about the other formats but if
...they are text ( hint they are indeed text )
.... then awk can re-fry them i thunk

tcz-info2json.sh
Code: [Select]

[ $# -eq 0 ] && {
l="
Suggestion to embrace the yaml format for .info files
https://forum.tinycorelinux.net/index.php/topic,26661.0.html


http://tinycorelinux.net/14.x/x86_64/tcz/advcomp.tcz.info


http://tinycorelinux.net/14.x/x86_64/tcz/aterm.tcz.info


http://tinycorelinux.net/14.x/x86_64/tcz/brave-browser.tcz.info


http://tinycorelinux.net/14.x/x86_64/tcz/bash-completion.tcz.info


http://tinycorelinux.net/14.x/x86_64/tcz/alsa-modules-6.1.2-tinycore64.tcz.info
"

# get the info files
echo "$l" | awk '/:\/\/.*\.info/{system("cd /tmp;mkdir tc; cd tc; wget -nH -x  "$0 )}'
}

#pass files as or get some tcz.info from string above

awk '

function d(m){ print m >"/dev/stderr"}

function beginfile(bf) { d("begin-file:" bf) ;  }
function endfile(ef) { d("endfile:" ef );
addj(ef," } , ","]")
}

FNR == 1 {
    if (_filename_ != "")
        endfile(_filename_)
    _filename_ = FILENAME
    beginfile(FILENAME)
}

END { endfile(_filename_) }

BEGIN{ count=0 }

# match feild ; starting with capital letter , ending in ":"
/^[A-Z][a-z_-]*:/{

count++;
field_name=substr($0,1,index($0,":")-1 );
# info-file / feild -name ( index by number )
sect[count]=FILENAME"_"field_name;
# info-file / feild -name array ( index by string )
sect[sect[count]]=$0


addj(FILENAME," ","],")
j=$0
field_text=substr(j,index(j,":")+1 );
gsub("\t"," ",field_text)
gsub("^[ ]*[ ]","",field_text)


# add match line
addj(FILENAME,"\42" tolower(field_name) "\42:" " [ \42"field_text"\42"," ")

}

# other lines
{
  j=""
  j=$0
  # not match field name
  m=match(j,"^[A-Z][a-z_-]*:");
gsub("\t"," ",j)
gsub("^[ ]*[ ]","",j)
# escape quotes
gsub("\42","\\\42",j)
   if( m == 0 &&  j != "" ) 
  {
    sect[sect[count]] = sect[sect[count]] "\n" j
   
    addj(FILENAME,"\42"j"\42",", ")
  } 
 
}

#NR==FNR{max++; next }
#FNR == max { print "Final line:",$0 }

END{

    print "{ \42a\42:["
    #
    os=""
    for( xj in json ) os= os " " json[xj]
    #print xj, json[xj]
   
    #
    print os " { \42_b_\42:\42\42 }" " ] }"

}

# adjust string format / add json
function addj(k,v  ,sep,j) {
# separator empty then default-sep or set from arg
sep=(sep=="" ? ",":sep ) ;
# add / append stings to array
json[k] = ( json[ k ] =="" ? "{ " v : json[k] sep  v);
}


'  ${@-/tmp/tc/14.x/x86_64/tcz/*} | tee json-tcz-info.txt

 # do stuff eg
# | jq  -s '.[].[].[].title'



* it works for those files **
if you have some tcz.info handy please test & report
success failure
ect ...

**example json-tcz-info.txt of the json attached!
... jq seams happy ...

Quote from: traumatik
We live inside the simulation
« Last Edit: March 03, 2025, 11:03:17 AM by mocore »

Offline mocore

  • Hero Member
  • *****
  • Posts: 711
  • ~.~
Re: (tcz) info 2 JSON (the GOAT)
« Reply #1 on: March 03, 2025, 10:56:51 AM »

ps i suspect this could be re written
 with out so many variables / with a few awks in a pipeline
funking around with RS ORS FS OFS ect ??

 8)

if anyone is (code)golfing 

Offline mocore

  • Hero Member
  • *****
  • Posts: 711
  • ~.~
Re: (tcz) info 2 JSON (the GOAT)
« Reply #2 on: March 03, 2025, 11:34:18 AM »

... i only found "double quotes" that required escaping
... probably json requires a few more
see also
https://stackoverflow.com/questions/19176024/how-to-escape-special-characters-in-building-a-json-string