Ajout du GUI

This commit is contained in:
thatscringebro
2022-08-08 16:31:52 -04:00
parent db362ccdca
commit abd15f28b6
851 changed files with 99957 additions and 1 deletions

View File

@@ -0,0 +1,135 @@
'''
Spelling
========
Provides abstracted access to a range of spellchecking backends as well as
word suggestions. The API is inspired by enchant but other backends can be
added that implement the same API.
Spelling currently requires `python-enchant` for all platforms except
OSX, where a native implementation exists.
::
>>> from kivy.core.spelling import Spelling
>>> s = Spelling()
>>> s.list_languages()
['en', 'en_CA', 'en_GB', 'en_US']
>>> s.select_language('en_US')
>>> s.suggest('helo')
[u'hole', u'help', u'helot', u'hello', u'halo', u'hero', u'hell', u'held',
u'helm', u'he-lo']
'''
__all__ = ('Spelling', 'SpellingBase', 'NoSuchLangError',
'NoLanguageSelectedError')
import sys
from kivy.core import core_select_lib
class NoSuchLangError(Exception):
'''
Exception to be raised when a specific language could not be found.
'''
pass
class NoLanguageSelectedError(Exception):
'''
Exception to be raised when a language-using method is called but no
language was selected prior to the call.
'''
pass
class SpellingBase(object):
'''
Base class for all spelling providers.
Supports some abstract methods for checking words and getting suggestions.
'''
def __init__(self, language=None):
'''
If a `language` identifier (such as 'en_US') is provided and a matching
language exists, it is selected. If an identifier is provided and no
matching language exists, a NoSuchLangError exception is raised by
self.select_language().
If no `language` identifier is provided, we just fall back to the first
one that is available.
:Parameters:
`language`: str, defaults to None
If provided, indicates the language to be used. This needs
to be a language identifier understood by select_language(),
i.e. one of the options returned by list_languages().
If nothing is provided, the first available language is used.
If no language is available, NoLanguageSelectedError is raised.
'''
langs = self.list_languages()
try:
# If no language was specified, we just use the first one
# that is available.
fallback_lang = langs[0]
except IndexError:
raise NoLanguageSelectedError("No languages available!")
self.select_language(language or fallback_lang)
def select_language(self, language):
'''
From the set of registered languages, select the first language
for `language`.
:Parameters:
`language`: str
Language identifier. Needs to be one of the options returned by
list_languages(). Sets the language used for spell checking and
word suggestions.
'''
raise NotImplementedError('select_language() method not implemented '
'by abstract spelling base class!')
def list_languages(self):
'''
Return a list of all supported languages.
E.g. ['en', 'en_GB', 'en_US', 'de', ...]
'''
raise NotImplementedError('list_languages() is not implemented '
'by abstract spelling base class!')
def check(self, word):
'''
If `word` is a valid word in `self._language` (the currently active
language), returns True. If the word shouldn't be checked, returns
None (e.g. for ''). If it is not a valid word in `self._language`,
return False.
:Parameters:
`word`: str
The word to check.
'''
raise NotImplementedError('check() not implemented by abstract ' +
'spelling base class!')
def suggest(self, fragment):
'''
For a given `fragment` (i.e. part of a word or a word by itself),
provide corrections (`fragment` may be misspelled) or completions
as a list of strings.
:Parameters:
`fragment`: str
The word fragment to get suggestions/corrections for.
E.g. 'foo' might become 'of', 'food' or 'foot'.
'''
raise NotImplementedError('suggest() not implemented by abstract ' +
'spelling base class!')
_libs = (('enchant', 'spelling_enchant', 'SpellingEnchant'), )
if sys.platform == 'darwin':
_libs += (('osxappkit', 'spelling_osxappkit', 'SpellingOSXAppKit'), )
Spelling = core_select_lib('spelling', _libs)

View File

@@ -0,0 +1,50 @@
'''
Enchant Spelling
================
Implementation spelling backend based on enchant.
.. warning:: pyenchant doesn't have dedicated build anymore for Windows/x64.
See https://github.com/kivy/kivy/issues/5816 for more information
'''
import enchant
from kivy.core.spelling import SpellingBase, NoSuchLangError
from kivy.compat import PY2
class SpellingEnchant(SpellingBase):
'''
Spelling backend based on the enchant library.
'''
def __init__(self, language=None):
self._language = None
super(SpellingEnchant, self).__init__(language)
def select_language(self, language):
try:
self._language = enchant.Dict(language)
except enchant.DictNotFoundError:
err = 'Enchant Backend: No language for "%s"' % (language, )
raise NoSuchLangError(err)
def list_languages(self):
# Note: We do NOT return enchant.list_dicts because that also returns
# the enchant dict objects and not only the language identifiers.
return enchant.list_languages()
def check(self, word):
if not word:
return None
return self._language.check(word)
def suggest(self, fragment):
suggestions = self._language.suggest(fragment)
# Don't show suggestions that are invalid
suggestions = [s for s in suggestions if self.check(s)]
if PY2:
suggestions = [s.decode('utf-8') for s in suggestions]
return suggestions

View File

@@ -0,0 +1,64 @@
'''
AppKit Spelling: Implements spelling backend based on OSX's spellchecking
features provided by the ApplicationKit.
NOTE:
Requires pyobjc and setuptools to be installed!
`sudo easy_install pyobjc setuptools`
Developers should read:
http://developer.apple.com/mac/library/documentation/
Cocoa/Conceptual/SpellCheck/SpellCheck.html
http://developer.apple.com/cocoa/pyobjc.html
'''
from AppKit import NSSpellChecker, NSMakeRange
from kivy.core.spelling import SpellingBase, NoSuchLangError
class SpellingOSXAppKit(SpellingBase):
'''
Spelling backend based on OSX's spelling features provided by AppKit.
'''
def __init__(self, language=None):
self._language = NSSpellChecker.alloc().init()
super(SpellingOSXAppKit, self).__init__(language)
def select_language(self, language):
success = self._language.setLanguage_(language)
if not success:
err = 'AppKit Backend: No language "%s" ' % (language, )
raise NoSuchLangError(err)
def list_languages(self):
return list(self._language.availableLanguages())
def check(self, word):
# TODO Implement this!
# NSSpellChecker provides several functions that look like what we
# need, but they're a) slooow and b) return a strange result.
# Might be a snow leopard bug. Have to test further.
# See: http://paste.pocoo.org/show/217968/
if not word:
return None
err = 'check() not currently supported by the OSX AppKit backend'
raise NotImplementedError(err)
def suggest(self, fragment):
l = self._language
# XXX Both ways below work on OSX 10.6. It has not been tested on any
# other version, but it should work.
try:
# This is deprecated as of OSX 10.6, hence the try-except
return list(l.guessesForWord_(fragment))
except AttributeError:
# From 10.6 onwards you're supposed to do it like this:
checkrange = NSMakeRange(0, len(fragment))
g = l.\
guessesForWordRange_inString_language_inSpellDocumentWithTag_(
checkrange, fragment, l.language(), 0)
# Right, this was much easier, Apple! :-)
return list(g)