~ubuntu-branches/ubuntu/trusty/libreoffice-dictionaries/trusty-proposed

« back to all changes in this revision

Viewing changes to dictionaries/hu_HU/Lightproof.py

  • Committer: Package Import Robot
  • Author(s): Bjoern Michaelsen
  • Date: 2014-02-21 22:54:52 UTC
  • Revision ID: package-import@ubuntu.com-20140221225452-008k3n2a73g2o6ga
Tags: upstream-4.2.1
Import upstream version 4.2.1

Show diffs side-by-side

added added

removed removed

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