Lightproof.py 6.9 KB

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