1
# -*- encoding: UTF-8 -*-
2
# Lightproof grammar checker for LibreOffice and OpenOffice.org
3
# 2009-2012 (c) László Németh (nemeth at numbertext org), license: MPL 1.1 / GPLv3+ / LGPLv3+
5
import uno, unohelper, os, sys, traceback
6
from lightproof_impl_hu_HU import locales
7
from lightproof_impl_hu_HU import pkg
8
import lightproof_impl_hu_HU
9
import lightproof_handler_hu_HU
11
from com.sun.star.linguistic2 import XProofreader, XSupportedLocales
12
from com.sun.star.linguistic2 import ProofreadingResult, SingleProofreadingError
13
from com.sun.star.lang import XServiceInfo, XServiceName, XServiceDisplayName
14
from com.sun.star.lang import Locale
15
# reload in obj.reload in Python 3
17
from obj import reload
21
class Lightproof( unohelper.Base, XProofreader, XServiceInfo, XServiceName, XServiceDisplayName, XSupportedLocales):
23
def __init__( self, ctx, *args ):
25
self.ServiceName = "com.sun.star.linguistic2.Proofreader"
26
self.ImplementationName = "org.libreoffice.comp.pyuno.Lightproof." + pkg
27
self.SupportedServiceNames = (self.ServiceName, )
31
self.locales += [Locale(l[0], l[1], l[2])]
32
self.locales = tuple(self.locales)
33
currentContext = uno.getComponentContext()
34
lightproof_impl_hu_HU.SMGR = currentContext.ServiceManager
35
lightproof_impl_hu_HU.spellchecker = \
36
lightproof_impl_hu_HU.SMGR.createInstanceWithContext("com.sun.star.linguistic2.SpellChecker", currentContext)
37
lightproof_handler_hu_HU.load(currentContext)
39
# XServiceName method implementations
40
def getServiceName(self):
41
return self.ImplementationName
43
# XServiceInfo method implementations
44
def getImplementationName (self):
45
return self.ImplementationName
47
def supportsService(self, ServiceName):
48
return (ServiceName in self.SupportedServiceNames)
50
def getSupportedServiceNames (self):
51
return self.SupportedServiceNames
54
def hasLocale(self, aLocale):
55
if aLocale in self.locales:
57
for i in self.locales:
58
if (i.Country == aLocale.Country or i.Country == "") and aLocale.Language == i.Language:
66
def isSpellChecker(self):
69
def doProofreading(self, nDocId, rText, rLocale, nStartOfSentencePos, \
70
nSuggestedSentenceEndPos, rProperties):
71
aRes = uno.createUnoStruct( "com.sun.star.linguistic2.ProofreadingResult" )
72
aRes.aDocumentIdentifier = nDocId
74
aRes.aLocale = rLocale
75
aRes.nStartOfSentencePosition = nStartOfSentencePos
76
aRes.nStartOfNextSentencePosition = nSuggestedSentenceEndPos
78
aRes.xProofreader = self
80
if len(rProperties) > 0 and rProperties[0].Name == "Update":
82
import lightproof_compile_hu_HU
84
code = lightproof_compile_hu_HU.c(rProperties[0].Value, rLocale.Language, True)
85
except Exception as e:
86
aRes.aText, aRes.nStartOfSentencePosition = e
88
path = lightproof_impl_hu_HU.get_path()
89
f = open(path.replace("_impl", ""), "w")
90
f.write("dic = %s" % code["rules"])
92
if pkg in lightproof_impl_hu_HU.langrule:
93
mo = lightproof_impl_hu_HU.langrule[pkg]
95
lightproof_impl_hu_HU.compile_rules(mo.dic)
96
lightproof_impl_hu_HU.langrule[pkg] = mo
102
f.write(ft[:ft.find("# [code]") + 8] + "\n" + code["code"])
105
reload(lightproof_impl_hu_HU)
106
except Exception as e:
107
aRes.aText = e.args[0]
108
if e.args[1][3] == "": # "expected an indented block" (end of file)
109
aRes.nStartOfSentencePosition = len(rText.split("\n"))
111
aRes.nStartOfSentencePosition = rText.split("\n").index(e.args[1][3][:-1]) + 1
116
if 'PYUNO_LOGLEVEL' in os.environ:
117
print(traceback.format_exc())
119
l = rText[nSuggestedSentenceEndPos:nSuggestedSentenceEndPos+1]
121
aRes.nStartOfNextSentencePosition = aRes.nStartOfNextSentencePosition + 1
122
l = rText[aRes.nStartOfNextSentencePosition:aRes.nStartOfNextSentencePosition+1]
123
if aRes.nStartOfNextSentencePosition == nSuggestedSentenceEndPos and l!="":
124
aRes.nStartOfNextSentencePosition = nSuggestedSentenceEndPos + 1
125
aRes.nBehindEndOfSentencePosition = aRes.nStartOfNextSentencePosition
128
aRes.aErrors = lightproof_impl_hu_HU.proofread( nDocId, rText, rLocale, \
129
nStartOfSentencePos, aRes.nBehindEndOfSentencePosition, rProperties)
130
except Exception as e:
131
if len(rProperties) > 0 and rProperties[0].Name == "Debug" and len(e.args) == 2:
132
aRes.aText, aRes.nStartOfSentencePosition = e
134
if 'PYUNO_LOGLEVEL' in os.environ:
135
print(traceback.format_exc())
138
def ignoreRule(self, rid, aLocale):
139
lightproof_impl_hu_HU.ignore[rid] = 1
141
def resetIgnoreRules(self):
142
lightproof_impl_hu_HU.ignore = {}
144
# XServiceDisplayName
145
def getServiceDisplayName(self, aLocale):
146
return lightproof_impl_hu_HU.name
148
g_ImplementationHelper = unohelper.ImplementationHelper()
149
g_ImplementationHelper.addImplementation( Lightproof, \
150
"org.libreoffice.comp.pyuno.Lightproof." + pkg,
151
("com.sun.star.linguistic2.Proofreader",))
153
g_ImplementationHelper.addImplementation( lightproof_handler_hu_HU.LightproofOptionsEventHandler, \
154
"org.libreoffice.comp.pyuno.LightproofOptionsEventHandler." + pkg,