"Fossies" - the Fresh Open Source Software Archive

Member "libreoffice-6.3.2.2/dictionaries/ru_RU/Lightproof.py" (18 Sep 2019, 6845 Bytes) of package /linux/misc/libreoffice/src/libreoffice-dictionaries-6.3.2.2.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

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