Debian Codegraph @

dictionaries/ru_RU/Lightproof.py

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