More words
[apertium.git] / apertium-forms-server / config.py
blob1b956a587647a56a0c2585950e244020a1181ba6
1 #!/usr/bin/python2.5 -Wall
2 # coding=utf-8
3 # -*- encoding: utf-8 -*-
5 import sys, string, codecs, xml, os, Ft, re, md5, cStringIO;
6 from Ft.Xml.Domlette import NonvalidatingReader;
7 from Ft.Xml.XPath import Evaluate;
8 from pair import *;
10 sys.stdout = codecs.getwriter('utf-8')(sys.stdout);
11 sys.stderr = codecs.getwriter('utf-8')(sys.stderr);
13 class Config: #{
15 def __init__(self, filename): #{
16 self.config = NonvalidatingReader.parseUri('file:///' + filename);
17 self.log_file = '';
18 self.pairs = {};
19 self.working_directory = None;
20 self.templates_directory = None;
23 def load_templates(self, _file): #{
24 print 'templates: ' + 'file:///' + self.working_directory + '/' + self.templates_directory + '/' + _file;
25 tmpldoc = NonvalidatingReader.parseUri('file:///' + self.working_directory + '/' + self.templates_directory + '/' + _file);
27 matrix = {};
29 for left in Ft.Xml.XPath.Evaluate('.//left', contextNode=tmpldoc): #{
30 left_hash = left.getAttributeNS(None, 'id');
31 if left_hash not in matrix: #{
32 matrix[left_hash] = {};
34 for right in Ft.Xml.XPath.Evaluate('.//right', contextNode=left): #{
35 right_hash = right.getAttributeNS(None, 'id');
36 if right_hash not in matrix[left_hash]: #{
37 matrix[left_hash][right_hash] = '';
39 for template in Ft.Xml.XPath.Evaluate('.//template', contextNode=right): #{
40 buf = cStringIO.StringIO();
41 Ft.Xml.Domlette.Print(template, stream=buf, encoding='utf-8');
42 text = buf.getvalue();
43 buf.close();
45 matrix[left_hash][right_hash] = text.replace('<template>', '').replace('</template>', '');
47 print 'Added template (' + left_hash + ':' + right_hash + '); length: ' + str(len(text));
52 return matrix;
55 def parse_config(self): #{
56 self.working_directory = self.config.xpath('/webforms/directories/wd')[0].firstChild.nodeValue;
57 self.cache_directory = self.config.xpath('/webforms/directories/cache')[0].firstChild.nodeValue;
58 self.templates_directory = self.config.xpath('/webforms/directories/templates')[0].firstChild.nodeValue;
59 self.log_file = self.config.xpath('/webforms/log-file')[0].firstChild.nodeValue;
61 path = '/webforms/pairs/pair';
63 for node in Ft.Xml.XPath.Evaluate(path, contextNode=self.config): #{
64 pair_name = node.getAttributeNS(None, 'n');
65 enabled = node.getAttributeNS(None, 'enabled');
67 if enabled == 'no': #{
68 continue;
71 self.pairs[pair_name] = Pair(self.working_directory, pair_name, node);
72 self.pairs[pair_name].cache = self.cache_directory + pair_name + '/';
74 templates = {};
75 for tmpls in Ft.Xml.XPath.Evaluate(".//templates",contextNode=node): #{
76 templates = self.load_templates(tmpls.getAttributeNS(None, 'file'));
79 for enabled_tag in Ft.Xml.XPath.Evaluate(".//tag",contextNode=node): #{
80 tag_name = enabled_tag.getAttributeNS(None, 'n');
81 shows = [];
83 for show_tag in Ft.Xml.XPath.Evaluate('.//show', contextNode= enabled_tag): #{
84 shows.append(show_tag.getAttributeNS(None, 'syms'));
87 for side in Ft.Xml.XPath.Evaluate('.//side', contextNode=enabled_tag): #{
88 paradigm_display_mode = None;
89 current_side = side.getAttributeNS(None, 'n');
91 for gloss_section in Ft.Xml.XPath.Evaluate('.//paradigms', contextNode=side): #{
92 paradigm_display_mode = gloss_section.getAttributeNS(None, 'display');
94 for gloss in Ft.Xml.XPath.Evaluate('.//paradigm', contextNode=gloss_section): #{
95 name = gloss.getAttributeNS(None, 'n');
96 comment = gloss.getAttributeNS(None, 'c');
98 self.pairs[pair_name].dictionary[current_side].add_gloss(tag_name, name, comment);
102 self.pairs[pair_name].dictionary[current_side].set_display(tag_name, paradigm_display_mode);
105 self.pairs[pair_name].add_tag(tag_name, shows);
107 self.pairs[pair_name].set_templates(templates);
112 # Speedup idea: Load all glossed paradigm entries at the start.
115 # for pair in self.pairs: #{
116 # for side in self.pairs[pair].dictionary.keys(): #{
117 # for tag in self.pairs[pair].dictionary[side].paradigms.keys(): #{
118 # for paradigm in self.pairs[pair].dictionary[side].paradigms[tag].keys(): #{
119 # print self.pairs[pair].dictionary[side].paradigms[tag].get(paradigm).name;
120 # self.pairs[pair].dictionary[side].get_paradigm(tag.encode('utf-8'), paradigm.encode('utf-8'));
121 # #}
122 # #}
123 # #}
124 # #}
129 def get_pairs(self): #{
130 return self.pairs;
133 def get_pair(self,name): #{
134 return self.pairs[name];
137 def show_config(self): #{
138 return self.config.toprettyxml();