Lightproof.py 6.7 KB

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