Debian Codegraph @

dictionaries/pt_BR/Lightproof.py

raw
1 # -*- encoding: UTF-8 -*-
2 # Lightproof grammar checker for LibreOffice and OpenOffice.org
3 # 2009-2012 (c) Laszlo Nemeth (nemeth at numbertext org), license: MPL 1.1 / GPLv3+ / LGPLv3+
4
5 import uno
6 import unohelper
7 import os
8 import traceback
9
10 locales = {'pt-BR': ['pt', 'BR', '']}
11 pkg = "pt_BR"
12
13 import lightproof_handler_pt_BR
14 from com.sun.star.linguistic2 import XProofreader, XSupportedLocales
15 from com.sun.star.linguistic2 import ProofreadingResult, SingleProofreadingError
16 from com.sun.star.lang import XServiceInfo, XServiceName, XServiceDisplayName
17 from com.sun.star.lang import Locale
18 # reload in obj.reload in Python 3
19 try:
20 from obj import reload
21 except Exception:
22 pass
23
24 class Lightproof( unohelper.Base, XProofreader, XServiceInfo, XServiceName, XServiceDisplayName, XSupportedLocales):
25
26 def __init__( self, ctx, *args ):
27 self.ctx = ctx
28 self.ServiceName = "com.sun.star.linguistic2.Proofreader"
29 self.ImplementationName = "org.libreoffice.comp.pyuno.Lightproof." + pkg
30 self.SupportedServiceNames = (self.ServiceName, )
31 self.locales = []
32 for i in locales:
33 l = locales[i]
34 self.locales += [Locale(l[0], l[1], l[2])]
35 self.locales = tuple(self.locales)
36
37 # XServiceName method implementations
38 def getServiceName(self):
39 return self.ImplementationName
40
41 # XServiceInfo method implementations
42 def getImplementationName (self):
43 return self.ImplementationName
44
45 def supportsService(self, ServiceName):
46 return (ServiceName in self.SupportedServiceNames)
47
48 def getSupportedServiceNames (self):
49 return self.SupportedServiceNames
50
51 # XSupportedLocales
52 def hasLocale(self, aLocale):
53 if aLocale in self.locales:
54 return True
55 for i in self.locales:
56 if (i.Country == aLocale.Country or i.Country == "") and aLocale.Language == i.Language:
57 return True
58 return False
59
60 def getLocales(self):
61 return self.locales
62
63 # XProofreader
64 def isSpellChecker(self):
65 return False
66
67 def doProofreading(self, nDocId, rText, rLocale, nStartOfSentencePos, \
68 nSuggestedSentenceEndPos, rProperties):
69 import lightproof_impl_pt_BR
70 currentContext = uno.getComponentContext()
71 if lightproof_impl_pt_BR.SMGR is None:
72 lightproof_impl_pt_BR.SMGR = currentContext.ServiceManager
73 lightproof_impl_pt_BR.spellchecker = \
74 lightproof_impl_pt_BR.SMGR.createInstanceWithContext("com.sun.star.linguistic2.SpellChecker", currentContext)
75 lightproof_handler_pt_BR.load(currentContext)
76 aRes = uno.createUnoStruct( "com.sun.star.linguistic2.ProofreadingResult" )
77 aRes.aDocumentIdentifier = nDocId
78 aRes.aText = rText
79 aRes.aLocale = rLocale
80 aRes.nStartOfSentencePosition = nStartOfSentencePos
81 aRes.nStartOfNextSentencePosition = nSuggestedSentenceEndPos
82 aRes.aProperties = ()
83 aRes.xProofreader = self
84 aRes.aErrors = ()
85 # PATCH FOR LO 4
86 # Fix for http://nabble.documentfoundation.org/Grammar-checker-Undocumented-change-in-the-API-for-LO-4-td4030639.html
87 if nStartOfSentencePos != 0:
88 return aRes
89 aRes.nStartOfNextSentencePosition = len(rText)
90 # END OF PATCH
91 if len(rProperties) > 0 and rProperties[0].Name == "Update":
92 try:
93 import lightproof_compile_pt_BR
94 try:
95 code = lightproof_compile_pt_BR.c(rProperties[0].Value, rLocale.Language, True)
96 except Exception as e:
97 aRes.aText, aRes.nStartOfSentencePosition = e
98 return aRes
99 path = lightproof_impl_pt_BR.get_path()
100 f = open(path.replace("_impl", ""), "w")
101 f.write("dic = %s" % code["rules"])
102 f.close()
103 if pkg in lightproof_impl_pt_BR.langrule:
104 mo = lightproof_impl_pt_BR.langrule[pkg]
105 reload(mo)
106 lightproof_impl_pt_BR.compile_rules(mo.dic)
107 lightproof_impl_pt_BR.langrule[pkg] = mo
108 if "code" in code:
109 f = open(path, "r")
110 ft = f.read()
111 f.close()
112 f = open(path, "w")
113 f.write(ft[:ft.find("# [code]") + 8] + "\n" + code["code"])
114 f.close()
115 try:
116 reload(lightproof_impl_pt_BR)
117 except Exception as e:
118 aRes.aText = e.args[0]
119 if e.args[1][3] == "": # "expected an indented block" (end of file)
120 aRes.nStartOfSentencePosition = len(rText.split("\n"))
121 else:
122 aRes.nStartOfSentencePosition = rText.split("\n").index(e.args[1][3][:-1]) + 1
123 return aRes
124 aRes.aText = ""
125 return aRes
126 except Exception:
127 if 'PYUNO_LOGLEVEL' in os.environ:
128 print(traceback.format_exc())
129
130 l = rText[aRes.nStartOfNextSentencePosition:aRes.nStartOfNextSentencePosition+1]
131 while l == " ":
132 aRes.nStartOfNextSentencePosition = aRes.nStartOfNextSentencePosition + 1
133 l = rText[aRes.nStartOfNextSentencePosition:aRes.nStartOfNextSentencePosition+1]
134 if aRes.nStartOfNextSentencePosition == nSuggestedSentenceEndPos and l!="":
135 aRes.nStartOfNextSentencePosition = nSuggestedSentenceEndPos + 1
136 aRes.nBehindEndOfSentencePosition = aRes.nStartOfNextSentencePosition
137
138 try:
139 aRes.aErrors = lightproof_impl_pt_BR.proofread( nDocId, rText, rLocale, \
140 nStartOfSentencePos, aRes.nBehindEndOfSentencePosition, rProperties)
141 except Exception as e:
142 if len(rProperties) > 0 and rProperties[0].Name == "Debug" and len(e.args) == 2:
143 aRes.aText, aRes.nStartOfSentencePosition = e
144 else:
145 if 'PYUNO_LOGLEVEL' in os.environ:
146 print(traceback.format_exc())
147 return aRes
148
149 def ignoreRule(self, rid, aLocale):
150 import lightproof_impl_pt_BR
151 lightproof_impl_pt_BR.ignore[rid] = 1
152
153 def resetIgnoreRules(self):
154 import lightproof_impl_pt_BR
155 lightproof_impl_pt_BR.ignore = {}
156
157 # XServiceDisplayName
158 def getServiceDisplayName(self, aLocale):
159 import lightproof_impl_pt_BR
160 return lightproof_impl_pt_BR.name
161
162 g_ImplementationHelper = unohelper.ImplementationHelper()
163 g_ImplementationHelper.addImplementation( Lightproof, \
164 "org.libreoffice.comp.pyuno.Lightproof." + pkg,
165 ("com.sun.star.linguistic2.Proofreader",),)
166
167 g_ImplementationHelper.addImplementation( lightproof_handler_pt_BR.LightproofOptionsEventHandler, \
168 "org.libreoffice.comp.pyuno.LightproofOptionsEventHandler." + pkg,
169 ("com.sun.star.awt.XContainerWindowEventHandler",),)