chiark / gitweb /
Adjust match context radiobutton states
[ypp-sc-tools.db-test.git] / pctb / dictionary-manager
index 0037a2a6f57474e6770dea0ad6ab54966874a6d9..51140c69a003cda31d76952d41b4c878030c19d0 100755 (executable)
 # sponsored by Three Rings.
 
 
+# ./dictionary-manager --approve-updates ijackson@login.chiark.greenend.org.uk /home/ijackson/things/ypp-sc-tools.pctb-dict-test/pctb /home/ftp/users/ijackson/pctb/test
+# ./dictionary-manager --approve-updates ijackson@login.chiark.greenend.org.uk /home/ijackson/things/ypp-sc-tools.pctb-dict/pctb /home/ftp/users/ijackson/pctb
+
+
 # invocation:
 # OUT OF DATE
 #  run this without args
@@ -71,6 +75,12 @@ proc puts_counted {f dvar} {
     debug "PUTS_COUNTED $count $dvar"
 }
 
+proc bgerror {m} {
+    global errorCode errorInfo
+    puts stderr "ERROR: $m\n[list $errorCode]\n$errorInfo\n";
+    exit 16
+}
+
 #---------- display core ----------
 
 set mul 6
@@ -85,11 +95,16 @@ proc init_widgets {} {
     global csrh gotsh ctxh
 
     if {[winfo exists .d]} return
+
+    frame .privacy -bd 2 -relief groove
+    pack .privacy -side top -padx 2 -pady 2 -fill x
+
+    upload_init
     
-    frame .d
+    frame .d -bd 2 -relief groove -pady 2 -padx 2
 
     image create bitmap image/main
-    label .d.mi -image image/main -borderwidth 0
+    label .d.mi -image image/main -bd 0
 
     frame .d.csr -bg black -height $csrh
     frame .d.got -bg black -height $gotsh
@@ -108,18 +123,20 @@ static unsigned char csr_bits[] = {
     entry .d.csr.csr.e -bd 0
     pack .d.csr.csr.l -side left
 
+    frame .d.selctx -bd 2 -relief groove
     frame .d.mi.csr_0 -bg white -width 1
     frame .d.mi.csr_1 -bg white -width 1
     frame .d.pe
     frame .d.pe.grid
+
     button .d.pe.ok -text OK
     pack .d.pe.grid .d.pe.ok -side left
 
     pack .d.mi .d.ctx -side top
-    pack .d
+    pack .d -fill x -padx 2 -pady 2
 
-    frame .help
-    pack .help
+    frame .help -bd 2 -relief groove
+    pack .help -pady 2 -padx 2
 }
 
 proc resize_widgets_core {} {
@@ -143,7 +160,7 @@ proc helptext {t} {
        set x 0; foreach c $l {
            set w .help.at${x}x${y}
            label $w -text $c
-           grid $w -row $y -column $x -padx 5
+           grid $w -row $y -column $x -padx 5 -sticky w
            incr x
        }
        incr y
@@ -153,12 +170,14 @@ proc helptext {t} {
 
 proc bind_key {k proc} {
     global keybindings
-    bind . <Key-$k> $proc
+    bind .d <Key-$k> $proc
     set keybindings($k) [expr {!![string length $proc]}]
+    .d configure -takefocus 1
 }
 proc unbind_all_keys {} {
     global keybindings
     foreach k [array names keybindings] { bind_key $k {} }
+    .d configure -takefocus 0
 }
 
 #---------- database read and write common wrapper ----------
@@ -212,8 +231,22 @@ proc write_database {} {
     file rename -force $database_fn.new $database_fn
 }
 
+proc select_database {dbname_spec} {
+    global dbname
+    set dbname $dbname_spec
+    read_database "./#local-$dbname#.txt"
+}
+
+proc do_database_update {im def} {
+    global database
+    maybe_upload_entry $im $def
+    set database($im) $def
+    write_database
+}
+    
 proc required/char {} {
-    global mulrows glyphsdone unk_l unk_r unk_contexts rows
+    global mulrows glyphsdone unk_l unk_r unk_contexts rows new_context
+    global all_contexts
     
     must_gets stdin l
 
@@ -223,6 +256,8 @@ proc required/char {} {
 
     char_read_xpm stdin
 
+    catch { unset all_contexts }
+
     resize_widgets_core
     foreach w {0 1} {
        .d.mi.csr_$w configure -height $mulrows
@@ -230,18 +265,46 @@ proc required/char {} {
     set maxh 0
     foreach {min max contexts got} $glyphsdone {
        show_context maxh $min $contexts
+       foreach ctx $contexts { set all_contexts($ctx) 1 }
+    }
+    foreach ctx $unk_contexts { set all_contexts($ctx) 1 }
+
+    destroy [winfo children .d.selctx]
+    label .d.selctx.title -text \
+       {Select match context for altering dictionary:}
+    pack .d.selctx.title -side left
+    set new_context [lindex $unk_contexts 0]
+
+    set ci 0; foreach ctx [lsort [array names all_contexts]] {
+       set all_contexts($ctx) $ci
+       set selw .d.selctx.c$ci
+       set seltxt $ctx
+       radiobutton $selw -variable new_context -value $ctx -text $seltxt
+       pack $selw -side left
+       incr ci
     }
+    $selw configure -text "$seltxt."
+    label .d.selctx.warning -text {See README.charset.}
+    pack .d.selctx.warning -side left
+
     show_context maxh $unk_l $unk_contexts
     .d.ctx configure -height $maxh
     pack forget .d.pe
-    pack .d.csr -side top -before .d.mi
+    pack .d.selctx .d.csr -side top -before .d.mi
     pack .d.got .d.ctx -side top -after .d.mi
+    pack configure .d.selctx -fill x
+    focus .d
 
-    read_database ./charset-$rows.txt
+    select_database char$rows
     draw_glyphsdone
     startup_cursor
 }
 
+proc approve_showentry_xinfo/char {w def} {
+    set unic [string2unicodenames $def]
+    label $w -text $unic
+}
+
 #========== PIXMAPS ==========
 
 #---------- pixmap database read and write ----------
@@ -294,11 +357,14 @@ proc pixmap_maybe_ok {} {
     set nsel 0
     foreach_pixmap_col col {
        set cs [.d.pe.grid.l$col curselection]
-       incr nsel [llength $cs]
+       set lcs [llength $cs]
+       if {!$lcs} continue
+       incr nsel $lcs
        set pixmap_selcol $col
        set pixmap_selrow [lindex $cs 0]
     }
     if {$nsel==1} {
+       debug "MAYBE_OK YES col=$pixmap_selcol row=$pixmap_selrow."
        .d.pe.ok configure -state normal -command pixmap_ok
     } else {
        .d.pe.ok configure -state disabled -command {}
@@ -317,8 +383,8 @@ proc pixmap_ok {} {
        rowname rowdesc
     set result "$colname - $rowname"
     debug "UPDATE PIXMAP AS >$result<"
-    set database($ppm) $result
-    write_database
+
+    do_database_update $ppm $result
     done/$mainkind
 }
 
@@ -337,7 +403,7 @@ proc required/pixmap {} {
 
     set alloptions [exec ./dictionary-pixmap-options $unk_what]
 
-    read_database ./pixmaps.txt
+    select_database pixmap
 
     set mulcols [image width image/main]
     set mulrows [image height image/main]
@@ -345,8 +411,10 @@ proc required/pixmap {} {
     place forget .d.mi.csr_0
     place forget .d.mi.csr_1
 
-    pack forget .d.csr .d.got
-    pack .d.pe -side top -before .d.mi -pady 10
+    pack forget .d.selctx .d.csr .d.got
+    pack .d.pe -side top -before .d.mi -pady 2
+    .d configure -takefocus 0
+    #-pady 2 -fill x
 
     eval destroy [winfo children .d.pe.grid]
     set col 0; foreach {colname coldesc rows} $alloptions {
@@ -365,7 +433,139 @@ proc required/pixmap {} {
     pixmap_maybe_ok
     
     helptext {
-       {{Indicate the correct parse of this image, and click OK.}}
+       {{Indicate the meaning of this image, and click OK.}}
+    }
+}
+
+proc approve_showentry_xinfo/pixmap {w def} {
+    label $w -image image/empty
+}
+
+#========== UPLOADS TO DICTIONARY SERVER ==========
+
+proc upload_init {} {
+    global privacy_setting
+
+    set privacy_setting [upload_status]
+
+    label .privacy.warn -text " Privacy "
+    if {$privacy_setting} {
+       .privacy.warn configure -background yellow -foreground black
+    }
+    label .privacy.overall -text " Upload new dictionary entry:"
+    label .privacy.reference -text " See README.privacy."
+
+    pack .privacy.warn .privacy.overall -side left
+
+    foreach {setting string} {
+       0 {No}
+       1 {Yes, anonymously}
+       2 {Yes, quoting my pirate name.}
+    } {
+       radiobutton .privacy.o$setting -text $string \
+           -value $setting -variable privacy_setting
+       pack .privacy.o$setting -side left
+       if {$setting > $privacy_setting} {
+           .privacy.o$setting configure -state disabled
+       }
+    }
+    pack .privacy.reference -side left
+
+    if {!$privacy_setting} {
+       foreach w [winfo children .privacy] {
+           $w configure -state disabled
+       }
+    }
+    if {$privacy_setting} {
+       package require http
+       ::http::config -urlencoding utf-8
+    }
+}
+
+proc upload_status {} {
+    # returns 0, 1, 2 for none, anon, with pirate name
+    global env
+
+    if {![info exists env(YPPSC_PCTB_DICT_SUBMIT)]} { debug a; return 0 }
+    if {![string compare 0 $env(YPPSC_PCTB_DICT_SUBMIT)]} { debug b; return 0 }
+
+    if {![info exists env(YPPSC_PIRATE)]} { return 1 }
+    if {![info exists env(YPPSC_OCEAN)]} { return 1 }
+    if {![string length $env(YPPSC_PIRATE)]} { return 1 }
+    if {![string length $env(YPPSC_OCEAN)]} { return 1 }
+
+    return 2
+}
+
+proc maybe_upload_entry {im def} {
+    global reqkind privacy_setting env dbname quiet
+
+    debug "DB-UPDATE PRIVACY $privacy_setting"
+    if {!$privacy_setting} return
+
+    debug "DB-UPDATE UPLOADING"
+
+    set pl {}
+    lappend pl dict $dbname
+
+    if {$privacy_setting>=2} {
+       set pirate [string totitle $env(YPPSC_PIRATE)]
+       set ocean [string totitle $env(YPPSC_OCEAN)]
+       debug "DB-UPDATE NON-ANON $ocean $pirate"
+       lappend pl \
+           pirate $pirate \
+           ocean $ocean
+    }
+    lappend pl entry [format_database_entry/$reqkind $im $def]
+
+    set url $env(YPPSC_PCTB_DICT_SUBMIT)
+    append url dictionary-update-receiver
+
+    set query [eval ::http::formatQuery $pl]
+    regsub -all {%0d} $query {} query
+    debug "DB-UPDATE QUERY $query"
+
+    if {[regexp {^\.?/} $url]} {
+       set cmd [list $url $query]
+       debug "SUBMIT CMD [string range $cmd 0 200]..."
+       set body [eval exec $cmd 2>@ stderr]
+       regsub {^Content-Type: text/plain.*\n\n} $body {} body
+    } else {
+
+       if {[catch {
+           set req [::http::geturl $url -query $query]
+       } emsg]} {
+           puts stderr \
+               "\nWARNING: Cannot do dictionary upload: $emsg\n"
+           return
+       }
+       upvar #0 $req s
+       debug "DB-UPDATE DONE $req $s(status) [array names s]"
+       set ncode [::http::ncode $req]
+
+       if {!(![string compare ok $s(status)] &&
+             ![string compare 200 $ncode])} {
+           set m "\nWARNING: Dictionary upload failed:"
+           foreach v {status http error posterror} {
+               if {[info exists s($v)]} { append m "\n $v: $s($v)" }
+           }
+           puts stderr $m
+           return
+       }
+       set body $s(body)
+       ::http::cleanup $req
+    }
+
+    if {![string match {OK *} $body]} {
+       set m "\nWARNING: Dictionary upload went wrong:\n"
+       append m "\n  " [join [split $body "\n"] "\n  "]
+       puts stderr $m
+       return
+    }
+
+    if {!$quiet} {
+       puts stderr \
+           "Uploaded $dbname dictionary entry `$def': $body"
     }
 }
 
@@ -518,19 +718,37 @@ proc startup_cursor {} {
 
 #---------- character set runtime display and keystroke handling ----------
 
+proc char_exactly_selctxts {contexts} {
+    global all_contexts
+    foreach ctx [array names all_contexts] {
+       set ci $all_contexts($ctx)
+       set selw .d.selctx.c$ci
+       if {[lsearch -exact $contexts $ctx]>=0} {
+           set state normal
+       } else {
+           set state disabled
+       }
+       $selw configure -state $state
+    }
+}
+
 proc recursor/0 {} { recursor//01 0 }
 proc recursor/1 {} { recursor//01 1 }
 proc recursor//01 {z1} {
-    global mul rhsmost_max cols glyphsdone
+    global mul rhsmost_max cols glyphsdone cur_0 cur_1
     upvar #0 cur_$z1 cur
     .d.csr.csr.l configure -text {adjust}
     place .d.csr.csr -x [expr {$cur*$mul - 7}]
+
+    manyset [char_get_definition_info $cur_0 $cur_1] contexts
+    char_exactly_selctxts $contexts
+
     bind_key space { othercursor }
     bind_leftright_q cur_$z1 0 [expr {$cols-1}]
     if {[llength $glyphsdone]} {
-       bind_key Tab { set cur_mode already; recursor }
+       bind_key BackSpace { set cur_mode already; recursor }
     } else {
-       bind_key Tab {}
+       bind_key BackSpace {}
     }
     bind_key Return {
        if {$cur_0 != $cur_1} {
@@ -543,7 +761,7 @@ proc recursor//01 {z1} {
        {{<- ->}   {move cursor, adjusting area to define}}
        {Space     {switch to moving other cursor}}
        {Return    {confirm location, enter letter(s)}}
-       {Tab       {switch to correcting earlier ocr}}
+       {Backspace {switch to correcting earlier ocr}}
        {Q         {quit and abandon OCR run}}
     }
 }
@@ -554,6 +772,8 @@ proc othercursor {} {
 }
 
 proc recursor/text {} {
+    global all_contexts
+    
     helptext {
        {Return   {confirm entry of new glyph}}
        {Escape   {abandon entry}}
@@ -562,16 +782,17 @@ proc recursor/text {} {
     .d.csr.csr.l configure -text {define:}
     pack .d.csr.csr.e -side left
     focus .d.csr.csr.e
-    bind_key Return {
+    bind .d.csr.csr.e <Key-Return> {
        set strq [.d.csr.csr.e get]
-       if {[regexp -line {^(?:[!-[]|[]-~]|\\\\|\\x[0-9a-f]{2})+} $strq]} {
-           RETURN_RESULT DEFINE "$cur_0 $cur_1 $strq"
+       if {[string length $strq]} {
+           RETURN_RESULT DEFINE [list $cur_0 $cur_1 $strq]
        }
     }
-    bind_key Escape {
+    bind .d.csr.csr.e <Key-Escape> {
        bind_key Escape {}
        pack forget .d.csr.csr.e
        set cur_mode 1
+       focus .d
        recursor
     }
 }
@@ -581,13 +802,15 @@ proc recursor/already {} {
     global glyphsdone
     global cur_already mul
     global glyphsdone cur_already mul
+
+    char_exactly_selctxts [lindex $glyphsdone [expr {$cur_already*4+2}]]
+
     .d.csr.csr.l configure -text {correct}
     set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
     place .d.csr.csr -x [expr {$rmax*$mul-3}]
     bind_key Return {}
-    bind_key space {}
     bind_leftright_q cur_already 0 [expr {[llength $glyphsdone]/4-1}]
-    bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
+    bind_key space { bind_key Delete {}; set cur_mode 1; recursor }
     bind_key Delete {
        RETURN_RESULT DELETE [lrange $glyphsdone \
                                  [expr $cur_already*4] \
@@ -596,7 +819,7 @@ proc recursor/already {} {
     helptext {
        {{<- ->}   {move cursor, selecting glyph to correct}}
        {Del       {clear this glyph from the recognition database}}
-       {Tab       {switch to selecting area to define as new glyph}}
+       {Space     {switch to selecting area to define as new glyph}}
        {Q         {quit and abandon OCR run}}
     }
 }
@@ -675,9 +898,12 @@ proc dbkey {ctx l r} {
     return $bm
 }
 
-proc update_database/DEFINE {c0 c1 strq} {
+proc char_get_definition_info {c0 c1} {
+    # => ncontexts cl cr
     global glyphsdone unk_l unk_contexts wordmap database
+    
     if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
+    
     if {$c0 == $unk_l} {
        set ncontexts $unk_contexts
     } else {
@@ -685,16 +911,25 @@ proc update_database/DEFINE {c0 c1 strq} {
            if {$l==$c0} { set ncontexts $contexts; break }
        }
        if {![info exists ncontexts]} {
-           puts stderr "must start at letter LHS!"
-           return
+           set ncontexts {}
        }
     }
     incr c1 -1
+    set r [list $ncontexts $c0 $c1]
+    debug "CDGI $r"
+    return $r
+}
+
+proc update_database/DEFINE {c0 c1 strq} {
+    manyset [char_get_definition_info $c0 $c1] ncontexts c0 c1
+    if {![llength $ncontexts]} {
+       puts stderr "must start at letter LHS!"
+       return
+    }
     foreach c $ncontexts {
        set bm [dbkey $c $c0 $c1]
-       set database($bm) $strq
+       do_database_update $bm $strq
     }
-    write_database
 }
 
 proc update_database/DELETE {l r ctxs} {
@@ -720,11 +955,11 @@ proc RETURN_RESULT {how what} {
 
 #========== server for approving updates ==========
 
-proc remote-serv-log {dict pirate file event} {
+proc remote-serv-log {dict pirate caller file event} {
     global remoteserv_logf
     set t [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S %Z}]
-    set s [format "%s %-6s %-31s %s %s\n" \
-              $t $dict $pirate [file tail $file] $event]
+    set s [format "%s %-6s %-31s %-31s %s %s\n" \
+              $t $dict $pirate $caller [file tail $file] $event]
     puts -nonewline $remoteserv_logf $s
 }
 
@@ -746,19 +981,13 @@ proc remote-serv/take {yesno file dict} {
     set rows ""
     debug "TAKE [list $yesno $file $dict]"
     read_counted stdin pirate
+    read_counted stdin caller
     read_counted stdin key
     read_counted stdin val
 
     must_gets_exactly stdin confirmed
 
-    if {![string compare pixmap $dict]} {
-       set reqkind pixmap
-       debug "DICT PIXMAP"
-    } elseif {[regexp {^(char)([1-9]\d*)$} $dict dummy reqkind rows]} {
-       debug "DICT CHAR rqk=$reqkind r=$rows."
-    } else {
-       error "$dict ?"
-    }
+    manyset [dict2_reqkind_rows reqkind rows]
     
     if {$yesno} {
        read_database $dictdir/master-$dict.txt
@@ -768,7 +997,7 @@ proc remote-serv/take {yesno file dict} {
     } else {
        set desc reject
     }
-    remote-serv-log $dict $pirate $file "$desc $reqkind $rows"
+    remote-serv-log $dict $pirate $caller $file "$desc $reqkind $rows"
     file delete -force $file
 
     puts done
@@ -819,6 +1048,18 @@ proc regsub-data {exp subspec args} {
     }
 }
 
+proc dict2_reqkind_rows {dict} {
+    if {![string compare pixmap $dict]} {
+       return {pixmap {}}
+       debug "DICT PIXMAP"
+    } elseif {[regexp {^(char)([1-9]\d*)$} $dict dummy reqkind rows]} {
+       debug "DICT CHAR rqk=$reqkind r=$rows."
+       return [list $reqkind rows]
+    } else {
+       error "$dict ?"
+    }
+}
+
 proc chop_counted {var} {
     upvar 1 $var val
     global data
@@ -836,6 +1077,7 @@ proc approve_decompose_data {specdata} {
     
     regsub-data {^ypp-sc-tools dictionary update v1\n} {}
     uplevel 1 chop_counted pirate
+    uplevel 1 chop_counted caller
     uplevel 1 chop_counted dict
     uplevel 1 chop_counted ctx
     uplevel 1 chop_counted def
@@ -852,8 +1094,18 @@ proc approve_compare {fd1 fd2} {
     return [string compare $sv1 $sv2]
 }
 
+proc string2unicodenames {str} {
+    return [exec perl -e {
+       use Unicode::CharName qw(uname);
+       $ARGV[0] =~ s/^ //;
+       foreach $_ (split //,$ARGV[0]) {
+           print uname(ord),"\n" or die $!
+       }
+    } " $str"]
+}
+
 proc approve_showentry {ix file specdata} {
-    global approve_ixes
+    global approve_ixes reqkind
     
     approve_decompose_data $specdata
 
@@ -877,11 +1129,19 @@ proc approve_showentry {ix file specdata} {
     image create photo approve/$ix -data $ppm
     label $wb-image -image approve/$ix -bd 2 -relief sunken
 
+    manyset [dict2_reqkind_rows $dict] reqkind
+    approve_showentry_xinfo/$reqkind $wb-xinfo $def
+
+    if {$ix} {
+       label $wb-div -bd 1 -relief sunken -image image/empty
+       grid configure $wb-div -columnspan 5 -sticky ew -padx 5
+    }
+
     frame $wb-act
     button $wb-act.rej -text Reject -command [list approve_reject $ix]
     pack $wb-act.rej
 
-    grid $wb-def $wb-image $wb-act $wb-inf -padx 3
+    grid $wb-def $wb-image $wb-xinfo $wb-act $wb-inf -padx 3
     grid configure $wb-image -ipadx 3 -ipady 3 -sticky w
 
     lappend approve_ixes $ix
@@ -893,6 +1153,7 @@ proc approve_approve_reject_one {ix yesno} {
     approve_decompose_data $tdata
     puts_server "take $yesno $file $dict"
     puts_counted $server pirate
+    puts_counted $server caller
     puts_counted $server key
     puts_counted $server val
     puts_server confirmed
@@ -949,14 +1210,18 @@ proc main/approve {} {
 
     if {[llength $argv] != 3} { error "wrong # args" }
     manyset $argv userhost directory dictdir
+    debug "APPROVER FOR $userhost $directory $dictdir"
+
     set cmd [list tclsh $directory/dictionary-manager]
     if {$debug} { lappend cmd --debug-server }
     lappend cmd --remote-server-1 $directory $dictdir
     switch -glob $userhost {
        {} { }
        {* *} { set cmd $userhost }
-       * { set cmd [append [list ssh $userhost] $cmd] }
+       * { set cmd [concat [list ssh $userhost] $cmd] }
     }
+    debug "APPROVER RUNS $cmd"
+
     lappend cmd 2>@ stderr
     set server [open |$cmd r+]
     must_gets_exactly_server $remoteserv_banner
@@ -972,6 +1237,8 @@ proc main/approve {} {
     pack .right -side right
     pack .ok -side bottom
 
+    image create bitmap image/empty
+
     set approve_page 0
     approve_fetch_list
 }
@@ -1044,9 +1311,11 @@ proc debug {m} { }
 set mainkind default
 set ai 0
 set debug 0
+set quiet 0
 foreach arg $argv {
     incr ai
     switch -exact -- $arg {
+       {--quiet}           { set quiet 1 }
        {--debug}           { set debug 1 }
        {--debug-server}    { proc debug {m} { puts stderr "DICT-MGR-SVR $m" }}
        {--noop-arg}        { }