def __init__(self, tag=None, broker=None): """Dict object constructor. A dictionary belongs to a specific language, identified by the string <tag>. If the tag is not given or is None, an attempt to determine the language currently in use is made using the 'locale' module. If the current language cannot be determined, Error is raised. If <tag> is instead given the value of False, a 'dead' Dict object is created without any reference to a language. This is typically only useful within PyEnchant itself. Any other non-string value for <tag> raises Error. Each dictionary must also have an associated Broker object which obtains the dictionary information from the underlying system. This may be specified using <broker>. If not given, the default broker is used. """ # Initialise misc object attributes to None self.provider = None # If no tag was given, use the default language if tag is None: tag = get_default_language() if tag is None: err = "No tag specified and default language could not " err = err + "be determined." raise Error(err) self.tag = tag # If no broker was given, use the default broker if broker is None: broker = _broker self._broker = broker # Now let the superclass initialise the C-library object super().__init__()
def get_default_language(default=None): """Determine the user's default language, if possible. This function uses the 'locale' module to try to determine the user's preferred language. The return value is as follows: * if a locale is available for the LC_MESSAGES category, that language is used * if a default locale is available, that language is used * if the keyword argument <default> is given, it is used * if nothing else works, None is returned Note that determining the user's language is in general only possible if they have set the necessary environment variables on their system. """ try: tag = locale.getlocale()[0] if tag is None: tag = locale.getdefaultlocale()[0] if tag is None: raise Error("No default language available") return tag except Exception: pass return default
def _check_this(self, msg=None): """Check that self._this is set to a pointer, rather than None.""" if self._this is None: if msg is None: msg = "%s unusable: the underlying C-library object has been freed." msg = msg % (self.__class__.__name__, ) raise Error(msg)
def get_resource_filename(resname): """Get the absolute path to the named resource file. This serves widely the same purpose as pkg_resources.resource_filename(), but tries to avoid loading pkg_resources unless we're actually in an egg. """ path = os.path.dirname(os.path.abspath(__file__)) path = os.path.join(path, resname) if os.path.exists(path): return path if hasattr(sys, "frozen"): exe_path = sys.executable exe_dir = os.path.dirname(exe_path) path = os.path.join(exe_dir, resname) if os.path.exists(path): return path else: import pkg_resources try: path = pkg_resources.resource_filename("enchant", resname) except KeyError: pass else: path = os.path.abspath(path) if os.path.exists(path): return path raise Error("Could not locate resource '%s'" % (resname,))
def _init_this(self): self._this = _e.broker_init() if not self._this: raise Error("Could not initialise an enchant broker.") self._live_dicts = {}