diff options
author | shaiton <shaiton@fedoraproject.org> | 2011-05-28 21:56:56 (GMT) |
---|---|---|
committer | shaiton <shaiton@fedoraproject.org> | 2011-05-28 21:56:56 (GMT) |
commit | 7bada2b167cf538e10fd62dabb14b06d81de813a (patch) | |
tree | 3510a4cb9a1b353cf9c6dbd567cd42a4f271d197 | |
parent | 8cd7af1fbf310d9cc203b6c0a03d6f2d5715548e (diff) |
new feature: different output file format => new parameters
-rwxr-xr-x | jparser | 129 |
1 files changed, 91 insertions, 38 deletions
@@ -6,6 +6,10 @@ # for exemple # org.laptop.WebActivity:1305831706:b7b8928d-b203-4aea-8924-f5a070637282:OLPC:0: # in a file named like user_YYYY-MM-DD.txt +# blank line separate new record (erase the file manually if you don't like this feature) + +# This script is under the GPLv2 license. If you get this file without the license, please read it there: +# http://www.gnu.org/licenses/gpl-2.0.html metadata=.metadata @@ -13,15 +17,21 @@ path=$PWD callme=child separator=: template=#activity${separator}timestamp${separator}uid${separator}title${separator}title_set_by_user${separator}buddies +template_simple=#date${separator}uid${separator}title${separator}buddies +simple_suffix=_simple.txt user=$USER +format=-c +user_cfg=/home/olpc/.sugar/default/config helpme() { - echo "Usage: $0 [user_name] [path to $metadata files]." + echo "Usage: $0 [output_format] [user_name] [path to $metadata files]." echo " add -h as first arg to print this help" + echo " - file(s) output format could be -s to create a simplified output" + echo " -c to create the complet output (Default)" + echo " -f to create both files" echo " - if no path specified, the current dir would be used" echo ' - if no user name is specified, the current $USER would be used' #should check an XO id - echo 'Ex of call: ./jparser $USER .' exit 0 } @@ -37,16 +47,13 @@ get_sed() # # function used to parse all metadata files from $path +# parameters are: +# parse filename output name output_format # parse() { out=$2 - # create template file - if [ ! -f $out ]; then - echo $template > $out - fi - tmp=$(sed -e 's/": ["]/:/g;s/["], "/#/g;s/^{"//g;s/: ""}//g;s/": /:/g;s/, "/#/g;s/"}//g' $1) IFS='#' read -ra ADDR <<< "$tmp" @@ -64,56 +71,102 @@ parse() esac done - line=${activity}${separator}${timestamp}${separator}${uid}${separator}${title}${separator}${title_set}${separator}${buddies} - echo $line >> $out - exit 0 + case $3 in + -s) #simplified output + line_so=true + ;; + -f) # both output + line_so=true + line=true + ;; + *) # only complet output + line=true + ;; + esac + + if [[ -n "$line_so" ]]; then + echo `date -d @${timestamp} +%d/%m/%Y`${separator}${uid}${separator}${title}${separator}${buddies} >> ${out}${simple_suffix} + fi + if [[ -n "$line" ]]; then + echo ${activity}${separator}${timestamp}${separator}${uid}${separator}${title}${separator}${title_set}${separator}${buddies} >> $out + fi + + exit 0 } # # Start argument processing # -if [[ "$#" -gt 3 || "$1" = "-h" ]]; then +if [[ "$#" -gt 4 ]]; then helpme fi -# -# Am I a child process used for parsing? -# - -if [[ "$1" = "$callme" ]]; then - if [ ! "$#" = 3 ]; then - echo "Error there should have been 3 args: callme path filename" - echo " But you give me: $*" - exit 1 +if [[ -n "$1" ]]; then + if [[ "$1" = "-h" ]]; then + helpme + elif [[ "$1" = "$callme" ]]; then # Am I a child process used for parsing? + if [ ! "$#" = 4 ]; then + echo "Error there should have been 4 args: callme path filename format" + echo " But you give me: $*" + exit 1 + else parse $2 $3 $4 + fi + elif [[ "$1" = "-s" || "$1" = "-f" || "$1" = "-c" ]]; then # Format output provided as arg? + format=$1 + if [[ -n "$2" ]]; then # User provided as arg? + user=$2 + if [ $# -lt 4 ]; then # Path provided as arg? + path=$3 + fi + elif [ -e /ofw/serial-number ]; then # if we are from an XO, grad its serial + user=`cat /ofw/serial-number` + fi # default value is $USER else - parse $2 $3 + echo "Error, wrong output specified" + helpme fi fi # -# Path provided as arg? +# Let's start parsing # -if [ $# -lt 3 ]; then - path=$2 -fi - -# -# User provided as arg? -# +out=${user}_`date +%F`.txt -if [[ -n "$1" ]]; then - user=$1 # it could also be found somewhere, depending where this script is used (XO or XS?) +# create template files +if [[ "$1" = "-s" || "$1" = "-f" ]]; then + if [ ! -f ${out}${simple_suffix} ]; then + echo $template_simple > ${out}${simple_suffix} + else + echo >> ${out}${simple_suffix} + fi +elif [[ "$1" = "-c" || "$1" = "-f" ]]; then + if [ ! -f $out ]; then + echo $template > $out + else + echo >> $out + fi fi -# -# Let's start parsing -# - -out=${user}_`date +%F`.txt +#if [[ -e $user_cfg ]]; then # yep, that's ugly but fast. This should be erased shortly.. +# IFS='nickname =' read name <<< "$user_cfgi" +# echo ===== $name ===== +#fi + # Now we call ourself to be able to inter in the function. Also used to run in concurrency -find $path -name "*$metadata" -exec $0 $callme '{}' $out \;& +find $path -name "*$metadata" -exec $0 $callme '{}' $out $format \;& wait # wait the end of parsing -echo "Metadata could be find in $out" +case $format in + -s) #simplified output + echo "Simple output file could be find in $out$simple_suffix" + ;; + -f) # both output + echo "Metadata could be find in $out" + echo "Simple output file could be find in $out$simple_suffix" + ;; + *) # only complet output + echo "Metadata could be find in $out" + ;; +esac exit 0 |