Thursday, December 30, 2010

gnocl::notify -new gnome widget

I was asked today to sort out an issue with the gnome package. In the process of doing so I noticed that the implementation of a gnome notify object was restricted to use as an option of a statusicon object. In all fairness, I've never seen it used in any other way, but.... I thought that I really ought take a look at completing the binding. So, I've just hacked together the first attempt. There's still quite a bit left to do, but it will appear like a widget allowing changes to be made until is the notification object is 'closed'.

Here's my test script.

# statusIcon.tcl
#
#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require Gnocl
package require GnoclGnome

set gui [gnocl::window]

set not [gnocl::notify \
    -appName GNOCL \
    -summary SUMMARY \
    -body BODY \
    -icon ICON \
    -sound bell.wav \
    -timeout 500 \
    -urgency critical \
    -attach $gui \
    -onClosed "%w Puts CLOSED"]

puts $not


Tuesday, December 21, 2010

gnocl::keyFile - Documentation Uploaded

Just uploaded some user documentation to the website. As always, there are still a few things to tidy up. I resolved the problems I was encountering delete or 'freeing' key-value lists. I'm getting some GLIB-CRITICAL warnings written to the console but there seems to be no imparement to the functionality.

Read / Save and Creating config files using gnocl::keyFile

I'm pretty well satisfied that this module is in a usable form. This morning I put together some Tcl proc to manage the creation of application config files. This is what I came up with;

#---------------
# test-keyfile.tcl
#---------------
# William J Giddings
# 21/12/2010
#---------------

#!/bin/sh
#\
exec tclsh "$0" "$@"

package require Gnocl

#---------------
# read named ini file and create global config array
#---------------
# args:
#    fname    name of file to load
# notes:
#    resets any existing config array
# returns:
#    id of the created keyframe object
#---------------
proc config:read { {fname app.ini} } {
    global config
    array unset config

    set i 0
    set kf [gnocl::keyFile load $fname]

    foreach group [$kf get groups] {
        foreach key  [$kf get keys -group $group ] {
            set config($key) [$kf get value -group $group -key $key]
            incr i
        }
    }
    return $kf
}

#---------------
# Write current configuration setting to .ini file
#---------------
# args:
#    kf        keyfile object id
#    fname    name of the ini file to create
# notes:
#    key group memberships checked internally
# returns:
#    number of items written to keyfile
#---------------
proc config:write {kf {fname app.ini} } {
    global config
    set i 0

    foreach key [array names config] {
        foreach group [$kf get groups] {
            if { [$kf has -group $group -key $key ] } {
                $kf set value -group $group -key $key -value $config($key)
                incr i
            }
        }
    }
    $kf write $fname
    return i
}

#---------------
# Sets default configuration settings.
#---------------
# args:
#    fname        name of the ini file to create
#    defaults    list of entries in the form <group:key value>
# notes:
#    if no values set, then an empty ini file will be created
# returns:
#    id of newly created keyfile object
#---------------
proc config:new { {fname app.ini} {defaults {} } } {
    global config

    array unset config

    set kf [gnocl::keyFile new $fname]

    if {$defaults == {}} {return $kf}

    foreach {a b} $defaults {
        foreach {group key} [split $a :] {}
        $kf set value -group $group -key $key -value $b
        set config($key) $b
    }

    return $kf
}


#----------
# test code
#----------

set defaults {
    group_1:key_a 1
    group_1:key_b 2
    group_2:key_c 3
    group_2:key_d 4
    group_3:key_e 5
    group_3:key_f 6
    group_3:key_g 7
    }

set kf [config:new test.ini $defaults ]

config:write $kf test.ini



Monday, December 20, 2010

gnocl::keyFile module completed!

Had some more time this evening and so finished off this module. There's only one item left to resolve but this is not unique to this module -the 'clear' sub-command. The existing Gnocl support funcs are geared towards GObjects which are basically widgets. A GKeyFile or even a Pixbuf for that matter are not widget although they are commands in the same way as a text or button widget. So, a little more thought needs to go into completing this last piece of functionality. But, that doesn't stop the use of the command. Here's a test .ini file from my ScanDocs app.

[Program]
name=ScanDocs
version=0.0.1

[Files]
batchDir=/home/wjgiddings/Desktop/ScanDocs/batches
batchName=default
lastfile={}
outFileName={DUNDES (1984) Sacred Narrative - Readings in the Theory of Myth}
pageSize=125x197
path=/home/wjgiddings/Desktop/ScanDocs

[Scanner]
mode=Binary
resolution=300
format=bw
threshold=80
type=pnm

[Job]
autoscan=0
i=100
scanRate=2

[Process]
deskew=0
composite=0
split=1
clear=1
clearBottom=100
clearCentre=100
clearSides=100
clearTop=0
trim=0
trimBottom=250
trimSides=300
trimTop=0
turn=0

[Unpaper]
unpaper,in_start=1
unpaper,out_start=1


And here's a code snippet of how to read that data and assign it to an application config array placed in the global namespace.


#---------------
# test-keyfile.tcl
#---------------
# William J Giddings
# 20/12/2010
#---------------

#!/bin/sh
#\
exec tclsh "$0" "$@"

package require Gnocl

set kf1 [gnocl::keyFile load scandocs.ini]

foreach group [$kf1 get groups] {
    foreach key  [$kf1 get keys -group $group ] {
        set config($key) [$kf1 get value -group $group -key $key]
    }
}

parray config


When changing key values, how to know which group the key belongs to?
 
set variable autoscan

foreach group [$kf1 get groups] {
    if { [$kf1 has -group $group -key $variable ] } {
        puts "$variable is in group $group"
    }
}




gnocl::keyFile -remove functionality

Today's task is now completed. Its now possible to inquire what groups are present in the .ini file and what they keynames are. The final step is to remove entries and to close any open files. The next steps are to review the syntax and to add the documentation.

Here's the test-script:

#---------------
# test-keyfile.tcl
#---------------
# William J Giddings, 28/12/2010
#---------------

#!/bin/sh
#\
exec tclsh "$0" "$@"

package require Gnocl

set kf1 [gnocl::keyFile load sample_config.ini]

puts "1 [$kf1 get value -group Person -key name]"
puts "2 [$kf1 get value -key name -group Person ]"
puts "3 [$kf1 get value -group Person -key name]"
puts "4 [$kf1 get value -key name -group Person ]"
puts "5 [$kf1 get integer -key age -group Person ]"
puts "6 [$kf1 get value -key sex -group Person ]"
puts "6 [$kf1 get value -key sex -group Person -locale UK ]"
puts "7 [$kf1 get comment -group Person -key name ]"

$kf1 set comment -group Person -key name -value "HIDI HI, HO DI HO!! HEHE"
$kf1 set value -group NEW -key ENTRY -value HAPPY_CHRISTMAS

puts [$kf1 write sample_config.ini]


puts GROUPS>>[$kf1 get groups]
puts KEYS>>[$kf1 get keys -group Person]
puts KEYS>>[$kf1 get keys -group "Another Group"]

set name [$kf1 get value -group Person -key name]

gnocl::window -setSize 0.25 -child [gnocl::label -textVariable name]

# remove functionality
$kf1 remove group NEW
$kf1 remove group NEW -key ENTRY
$kf1 remove comment Person -key name

$kf1 set comment -group Person -key sex -value "THIS IS A NEW COMMENT"
puts [$kf1 write sample_config.ini]

gnocl::keyFile -getting lists of groups and keys

Today's task is now completed. Its now possible to inquire what groups are present in the .ini file and what they keynames are. The final step is to remove entries and to close any open files. Here's the test-script:

#---------------
# test-keyfile.tcl
#---------------
# William J Giddings, 28/12/2010
#---------------

#!/bin/sh
#\
exec tclsh "$0" "$@"

package require Gnocl

set kf1 [gnocl::keyFile load sample_config.ini]

puts "1 [$kf1 get value -group Person -key name]"
puts "2 [$kf1 get value -key name -group Person ]"
puts "3 [$kf1 get value -group Person -key name]"
puts "4 [$kf1 get value -key name -group Person ]"
puts "5 [$kf1 get integer -key age -group Person ]"
puts "6 [$kf1 get value -key sex -group Person ]"
puts "6 [$kf1 get value -key sex -group Person -locale UK ]"
puts "7 [$kf1 get comment -group Person -key name ]"

$kf1 set comment -group Person -key name -value "HIDI HI, HO DI HO!! HEHE"

# this will add a new group and set a specific key value
$kf1 set value -group NEW -key ENTRY -value HAPPY_CHRISTMAS

puts [$kf1 write sample_config.ini]

# 20/12/2010
# retrieve info on groups and keys
puts GROUPS>>[$kf1 get groups]
puts KEYS>>[$kf1 get keys -group Person]
puts KEYS>>[$kf1 get keys -group "Another Group"]

# extract a value and use it!
set name [$kf1 get value -group Person -key name]

gnocl::window -setSize 0.25 -child [gnocl::label -textVariable name]

Sunday, December 19, 2010

gnocl::keyFile almost done

After this morning's coding session the package now has functionality to change values and write these changes to disk.  The next step is implementing some of the introspection functionality and its pretty-well complete.

Here's the test script updated to reflect changes:

#---------------
# test-keyfile.tcl
#---------------
# William J Giddings, 28/12/2010
#---------------

#!/bin/sh
#\
exec tclsh "$0" "$@"

package require Gnocl

# 18/12/10 Additions
set kf1 [gnocl::keyFile load sample_config.ini]

puts "1 [$kf1 get value -group Person -key name]"
puts "2 [$kf1 get value -key name -group Person ]"
puts "3 [$kf1 get value -group Person -key name]"
puts "4 [$kf1 get value -key name -group Person ]"
puts "5 [$kf1 get integer -key age -group Person ]"
puts "6 [$kf1 get value -key sex -group Person ]"
puts "6 [$kf1 get value -key sex -group Person -locale UK ]"
puts "7 [$kf1 get comment -group Person -key name ]"

# 19/12/10 Additions
$kf1 set comment -group Person -key name -value "HIDI HI, HO DI HO!! HEHE"
$kf1 write sample_config.ini

# Next sub-command to implement
puts >>[$kf1 get groups]



Saturday, December 18, 2010

gnocl::keyFile -progress

Got the basics running now. Seeing as in TCL 'everything is a string' the process of retrieving and setting values in the Tcl side is simple. I've got the information retrieval side working well and some skeletal bits for adding values which I'll bash into shape tomorrow. Here's my sample_config.ini file:

# this is just an example
# there can be comments before the first group

[First Group]
Name=Key File Example\tthis value shows\nescaping
# localized strings are stored in multiple key-value pairs
Welcome=Hello

[Another Group]
Numbers=2;20;-200;0
Booleans=true;false;true;true

[Person]
#This is me!
name=William
sex=male
age=54

And the test script to access it.

#---------------
# test-keyfile.tcl
#---------------
# William J Giddings, 28/12/2010
#---------------

#!/bin/sh
#\
exec tclsh "$0" "$@"

package require Gnocl

set kf1 [gnocl::keyFile load sample_config.ini]

puts "1 [$kf1 get string -group Person -key name]"
puts "2 [$kf1 get string -key name -group Person ]"
puts "3 [$kf1 get value -group Person -key name]"
puts "4 [$kf1 get value -key name -group Person ]"
puts "5 [$kf1 get string -key age -group Person ]"
puts "6 [$kf1 get string -key sex -group Person ]"
puts "6 [$kf1 get string -key sex -group Person -locale UK ]"
puts "7 [$kf1 get comment -group Person -key name ]"

new command gnocl::keyFile

The GLIBs provides functionality for the creation and manipulation of application configuration files. This task is already pretty easy to implement using Tcl list and dictionary functionality and hitherto I've used it all the time. But, having seen how useful this functionality is, I thought that it was time to implement the bindings. The way in which this is achieved is to treat such lists as objects so that multiple files can be opened for an application. This will allow the use of such lists to be more than simply an application config file. One of my pet apps is the scanning and creation of djvu ebooks. Sometimes the settings for a particular page need to be modified in order to get a good scan. Creating a config page for to control the page creation operation would be the better than an a 'one setting fits all' approach.