summaryrefslogtreecommitdiff
path: root/bindings/swig/examples
diff options
context:
space:
mode:
authorDimitri John Ledkov <xnox@ubuntu.com>2014-05-11 22:09:52 +0100
committerDimitri John Ledkov <xnox@ubuntu.com>2014-05-11 22:09:52 +0100
commit3525014850e3800ac7b28fd34e7f7af427f1c620 (patch)
tree3d1b8a17b86cfa9af178ceb818a4dc9daf52a46b /bindings/swig/examples
sword (1.7.2+dfsg-2) unstable; urgency=medium
* Correct shared library symlink. (Closes: #747420) # imported from the archive
Diffstat (limited to 'bindings/swig/examples')
-rwxr-xr-xbindings/swig/examples/av11n.py112
-rwxr-xr-xbindings/swig/examples/mod2zmod.pl105
2 files changed, 217 insertions, 0 deletions
diff --git a/bindings/swig/examples/av11n.py b/bindings/swig/examples/av11n.py
new file mode 100755
index 0000000..9ce7979
--- /dev/null
+++ b/bindings/swig/examples/av11n.py
@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+#
+# This does a very roughshod attempt to compare the osisIDs found in an
+# XML file with each of the versifications that SWORD knows about to help
+# a user find the one which is most akin to the one they are using. It is
+# limited in its need for your file to be at least segregated into OT/NT
+# in the proper order, although within each testament, it requires nothing
+# special as for ordering.
+#
+# Invoke simply by calling the program and the file name. If you want
+# more output, change the following line to be True instead of False
+verbose = False
+debug = True
+import sys
+import re
+verseid = re.compile('^.+\..+\..+$')
+
+# Inform the user that we need the SWORD extension
+try:
+ import Sword
+except:
+ print "You do not have the SWORD library installed. Please install it."
+ sys.exit(1)
+
+# Inform the user that we need pyquery, as it makes parsing XML files that much easier
+try:
+ from pyquery import PyQuery as pq
+except:
+ print "You do not appear to have PyQuery installed. Please install it."
+ sys.exit(2)
+
+# Without the name of a file, we cannot proceed any further
+if len(sys.argv) < 2 or sys.argv[1] == '--help':
+ print "Usage: %s <OSISfile>" % (sys.argv[0],)
+
+# Open the file
+if debug:
+ print 'Opening %s' % (sys.argv[1],)
+d = pq(filename=sys.argv[1])
+# Get the list of versifications
+if debug:
+ print 'Fetching a list of versifications'
+vmgr = Sword.VersificationMgr.getSystemVersificationMgr()
+av11ns = vmgr.getVersificationSystems()
+
+# Get the list of all osisIDs
+if debug:
+ print 'Fetching a list of OSIS IDs'
+ids = d("*[osisID]")
+# Iterate each versification scheme
+for v11n in av11ns:
+ print 'Checking %s' % (v11n.c_str(),)
+ # Construct a list of the IDs in this versification
+ key = Sword.VerseKey()
+ key.setVersificationSystem(v11n.c_str())
+ otkeyList = [] # Anything left in this afterwards is missing from the OSIS ot
+ ntkeyList = [] # Anything left in this afterwards is missing from the OSIS nt
+ otextraKeys = [] # Anything that gets placed in here is extraneous OT material (we think)
+ ntextraKeys = [] # Anything that gets placed in here is extraneous NT material (we think)
+
+ inNT = False
+ while key.popError() == '\x00':
+ skey = key.getOSISRef()
+ if not inNT and re.match('^Matt', skey): # Assume we enter the NT when we hit Matthew
+ inNT = True
+ if inNT:
+ ntkeyList.append(skey)
+ else:
+ otkeyList.append(skey)
+ key.increment()
+ ntkeyList = set(ntkeyList) # The 'in' operator only works on a set
+ otkeyList = set(otkeyList)
+
+ inNT = False
+ # Now iterate the ones we have in this file
+ for e in ids:
+ osisid = e.attrib.get('osisID')
+ #print 'Checking key %s' % (osisid,)
+ if osisid in otkeyList:
+ otkeyList.remove(osisid)
+ elif osisid in ntkeyList:
+ ntkeyList.remove(osisid)
+ inNT = True
+ elif verseid.match(osisid) and inNT:
+ ntextraKeys.append(osisid)
+ elif verseid.match(osisid) and not inNT:
+ otextraKeys.append(osisid)
+ # Ignore it if not verseid.match()
+
+ # Now let's see what is left over
+ keyList = list(otkeyList.union(ntkeyList)) # Sets in Python cannot be ordered
+ keyList.sort()
+ if len(keyList) > 0:
+ if verbose:
+ print '\tThe following IDs do not appear in your file:'
+ for k in keyList:
+ print k
+ else:
+ print '\tThere are %d OT IDs and %d NT IDs in the versification which are not in your file.' % (len(otkeyList), len(ntkeyList))
+ else:
+ print '\tYour file has all the references in this versification'
+
+ # Now let's see if you had extra
+ if len(otextraKeys + ntextraKeys) > 0:
+ if verbose:
+ print '\tThe following IDs do not appear in the versification:'
+ for k in ntextraKeys + otextraKeys:
+ print k
+ else:
+ print '\tThere are %d OT IDs and %d NT IDs in your file which do not appear in the versification.' % (len(otextraKeys), len(ntextraKeys))
+ else:
+ print '\tYour file has no extra references'
diff --git a/bindings/swig/examples/mod2zmod.pl b/bindings/swig/examples/mod2zmod.pl
new file mode 100755
index 0000000..d771830
--- /dev/null
+++ b/bindings/swig/examples/mod2zmod.pl
@@ -0,0 +1,105 @@
+#!/usr/bin/perl
+
+#******************************************************************************
+#
+# mod2zmod.pl - This program converts a given module into a compressed
+# module of the same type. This is just an example to
+# demomstrate the power of the Perl Sword bindings. The
+# code is almost written the same way the C++ of
+# mod2zmod.cpp code was written
+#
+# $Id: mod2zmod.pl 2841 2013-06-29 10:58:08Z chrislit $
+#
+# Copyright 2002-2009 CrossWire Bible Society (http://www.crosswire.org)
+# CrossWire Bible Society
+# P. O. Box 2528
+# Tempe, AZ 85280-2528
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation version 2.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+
+use Sword;
+use strict;
+
+my $appname = "mod2zmod.pl";
+
+sub printUsage()
+{
+ print "\n$appname - Convert a module into a compressed module of the same type.\n";
+ print "Usage: $appname <module> <datapth> [blocktype [compresstype]]\n";
+ print("datapath: the directory in which to write the zModule\n");
+ print("blockType : (default 4)\n\t2 - verses\n\t3 - chapters\n\t4 - books\n");
+ print("compressType: (default 1):\n\t1 - LZSS\n\t2 - Zip\n\n");
+
+ exit(-1);
+}
+
+#main part of the program
+if (scalar(@ARGV) < 2 || scalar(@ARGV) > 4) {
+ printUsage;
+}
+
+#initialization stuff
+my $datapath = $ARGV[1];
+my $blockType = defined $ARGV[2] ? $ARGV[2] : 4;
+my $compressType = defined $ARGV[3] ? $ARGV[3] : 1;
+my $mgr = new Sword::SWMgr();
+my $module = $mgr->module($ARGV[0]);
+my $compressor = ($compressType == 1) ? new Sword::LZSSCompress() : new Sword::ZipCompress();
+
+my $newmod;
+
+if ($module->Type() eq "Biblical Texts") {
+ if (!Sword::zText::createModule( $datapath, $blockType )) {
+ print "$appname: Couldn't create module in $datapath";
+ exit(-1);
+ }
+ $newmod = new Sword::zText( $datapath, 0, 0, $blockType, $compressor );
+
+} elsif ($module->Type() eq "Lexicons / Dictionaries") {
+ if (!Sword::zLD::createModule( $datapath )){
+ print "$appname: Couldn't create module in $datapath";
+ exit(-1);
+ }
+ $newmod = new Sword::zLD( $datapath, 0, 0, $blockType, $compressor)
+} elsif ($module->Type() eq "Commentaries") {
+ if (!Sword::zCom::createModule( $datapath, $blockType )){
+ print "$appname: Couldn't create module in $datapath";
+ exit(-1);
+ }
+ $newmod = new Sword::zCom( $datapath, 0, 0, $blockType, $compressor)
+}
+
+# now copy the content of the module!
+
+my $buffer;
+
+$module->top();
+$module->setSkipConsecutiveLinks(0);
+do {
+ my $key = $module->Key();
+ if (($buffer eq $module->getRawEntry()) &&($buffer ne "")) {
+ print "Adding [", $key->getText(), "] link to: \n";
+ $newmod->writeLink($key);
+ }
+ else {
+ $buffer = $module->getRawEntry();
+ if ($buffer ne "") {
+ $newmod->SetKey($key);
+ $newmod->write($buffer);
+ # print "Added ", $key->getText(), "\n";
+ }
+ else {
+ print "Skipping empty ", $key->getText(), "\n";
+ }
+ }
+} while($module->next());
+
+print "The new module is now available in $datapath!\n";