Lightproof.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_en import locales
  6. from lightproof_impl_en import pkg
  7. import lightproof_impl_en
  8. import lightproof_handler_en
  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_en.SMGR = currentContext.ServiceManager
  31. lightproof_impl_en.spellchecker = \
  32. lightproof_impl_en.SMGR.createInstanceWithContext("com.sun.star.linguistic2.SpellChecker", currentContext)
  33. lightproof_handler_en.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. if len(rProperties) > 0 and rProperties[0].Name == "Update":
  74. try:
  75. import lightproof_compile_en
  76. try:
  77. code = lightproof_compile_en.c(rProperties[0].Value, rLocale.Language, True)
  78. except Exception as e:
  79. aRes.aText, aRes.nStartOfSentencePosition = e
  80. return aRes
  81. path = lightproof_impl_en.get_path()
  82. f = open(path.replace("_impl", ""), "w")
  83. f.write("dic = %s" % code["rules"])
  84. f.close()
  85. if pkg in lightproof_impl_en.langrule:
  86. mo = lightproof_impl_en.langrule[pkg]
  87. reload(mo)
  88. lightproof_impl_en.compile_rules(mo.dic)
  89. lightproof_impl_en.langrule[pkg] = mo
  90. if "code" in code:
  91. f = open(path, "r")
  92. ft = f.read()
  93. f.close()
  94. f = open(path, "w")
  95. f.write(ft[:ft.find("# [code]") + 8] + "\n" + code["code"])
  96. f.close()
  97. try:
  98. reload(lightproof_impl_en)
  99. except Exception as e:
  100. aRes.aText = e.args[0]
  101. if e.args[1][3] == "": # "expected an indented block" (end of file)
  102. aRes.nStartOfSentencePosition = len(rText.split("\n"))
  103. else:
  104. aRes.nStartOfSentencePosition = rText.split("\n").index(e.args[1][3][:-1]) + 1
  105. return aRes
  106. aRes.aText = ""
  107. return aRes
  108. except:
  109. if 'PYUNO_LOGLEVEL' in os.environ:
  110. print(traceback.format_exc())
  111. l = rText[aRes.nStartOfNextSentencePosition:aRes.nStartOfNextSentencePosition+1]
  112. while l == " ":
  113. aRes.nStartOfNextSentencePosition = aRes.nStartOfNextSentencePosition + 1
  114. l = rText[aRes.nStartOfNextSentencePosition:aRes.nStartOfNextSentencePosition+1]
  115. if aRes.nStartOfNextSentencePosition == nSuggestedSentenceEndPos and l!="":
  116. aRes.nStartOfNextSentencePosition = nSuggestedSentenceEndPos + 1
  117. aRes.nBehindEndOfSentencePosition = aRes.nStartOfNextSentencePosition
  118. try:
  119. aRes.aErrors = lightproof_impl_en.proofread( nDocId, rText, rLocale, \
  120. nStartOfSentencePos, aRes.nBehindEndOfSentencePosition, rProperties)
  121. except Exception as e:
  122. if len(rProperties) > 0 and rProperties[0].Name == "Debug" and len(e.args) == 2:
  123. aRes.aText, aRes.nStartOfSentencePosition = e
  124. else:
  125. if 'PYUNO_LOGLEVEL' in os.environ:
  126. print(traceback.format_exc())
  127. return aRes
  128. def ignoreRule(self, rid, aLocale):
  129. lightproof_impl_en.ignore[rid] = 1
  130. def resetIgnoreRules(self):
  131. lightproof_impl_en.ignore = {}
  132. # XServiceDisplayName
  133. def getServiceDisplayName(self, aLocale):
  134. return lightproof_impl_en.name
  135. g_ImplementationHelper = unohelper.ImplementationHelper()
  136. g_ImplementationHelper.addImplementation( Lightproof, \
  137. "org.libreoffice.comp.pyuno.Lightproof." + pkg,
  138. ("com.sun.star.linguistic2.Proofreader",))
  139. g_ImplementationHelper.addImplementation( lightproof_handler_en.LightproofOptionsEventHandler, \
  140. "org.libreoffice.comp.pyuno.LightproofOptionsEventHandler." + pkg,
  141. ())