Repository: caronc/apprise

Issue:
AppriseLocale.py TypeError: 'NoneType' object has no attribute '__getitem__'
An system without any universal language identifier existing in it's environment at all causes an unhanded exception:
```python
  File "/usr/lib/python2.7/site-packages/apprise/Apprise.py", line 78, in __init__
    self.locale = AppriseLocale()
  File "/usr/lib/python2.7/site-packages/apprise/AppriseLocale.py", line 111, in __init__
    self.lang = AppriseLocale.detect_language(language)
  File "/usr/lib/python2.7/site-packages/apprise/AppriseLocale.py", line 207, in detect_language
    return lang[0:2].lower()
TypeError: 'NoneType' object has no attribute '__getitem__'
```

This was identified [here](https://github.com/linuxserver/docker-lazylibrarian/issues/30).

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=h1) Report
> Merging [#119](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=desc) into [master](https://codecov.io/gh/caronc/apprise/commit/cbd35c3b7e4b0559fbdb16b481d7c2cabbb602a1?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/caronc/apprise/pull/119/graphs/tree.svg?width=650&token=skAfz96t81&height=150&src=pr)](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master   #119   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          48     48           
  Lines        5313   5317    +4     
  Branches      817    817           
=====================================
+ Hits         5313   5317    +4
```


| [Impacted Files](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [apprise/AppriseLocale.py](https://codecov.io/gh/caronc/apprise/pull/119/diff?src=pr&el=tree#diff-YXBwcmlzZS9BcHByaXNlTG9jYWxlLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=footer). Last update [cbd35c3...6a66f6d](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/apprise/AppriseLocale.py b/apprise/AppriseLocale.py
index ea88acc..714d201 100644
--- a/apprise/AppriseLocale.py
+++ b/apprise/AppriseLocale.py
@@ -192,16 +192,24 @@ class AppriseLocale(object):
 
             if hasattr(ctypes, 'windll'):
                 windll = ctypes.windll.kernel32
-                lang = locale.windows_locale[
-                    windll.GetUserDefaultUILanguage()]
-            else:
                 try:
-                    # Detect language
-                    lang = locale.getdefaultlocale()[0]
+                    lang = locale.windows_locale[
+                        windll.GetUserDefaultUILanguage()]
+
+                    # Our detected windows language
+                    return lang[0:2].lower()
+
+                except (TypeError, KeyError):
+                    # Fallback to posix detection
+                    pass
 
-                except TypeError:
-                    # None is returned if the default can't be determined
-                    # we're done in this case
-                    return None
+            try:
+                # Detect language
+                lang = locale.getdefaultlocale()[0]
+
+            except TypeError:
+                # None is returned if the default can't be determined
+                # we're done in this case
+                return None
 
-        return lang[0:2].lower()
+        return None if not lang else lang[0:2].lower()


Test patch:
diff --git a/test/test_locale.py b/test/test_locale.py
index 15de3d1..ce2fcc8 100644
--- a/test/test_locale.py
+++ b/test/test_locale.py
@@ -27,6 +27,7 @@ import mock
 import ctypes
 
 from apprise import AppriseLocale
+from apprise.utils import environ
 
 try:
     # Python v3.4+
@@ -133,8 +134,7 @@ def test_gettext_installs(mock_gettext_trans):
         pass
 
 
-@mock.patch('locale.getdefaultlocale')
-def test_detect_language(mock_getlocale):
+def test_detect_language_windows_users():
     """
     API: Apprise() Detect language
 
@@ -147,14 +147,40 @@ def test_detect_language(mock_getlocale):
         setattr(ctypes, 'windll', windll)
 
     # The below accesses the windows fallback code
-    assert AppriseLocale.AppriseLocale.detect_language() == 'en'
+    with environ('LANG', 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', LANG="en_CA"):
+        assert AppriseLocale.AppriseLocale.detect_language() == 'en'
 
     assert AppriseLocale.AppriseLocale\
         .detect_language(detect_fallback=False) is None
 
+    # 0 = IndexError
+    windll.kernel32.GetUserDefaultUILanguage.return_value = 0
+    setattr(ctypes, 'windll', windll)
+    with environ('LANG', 'LC_ALL', 'LC_CTYPE', LANGUAGE="en_CA"):
+        assert AppriseLocale.AppriseLocale.detect_language() == 'en'
+
+    # The below accesses the windows fallback code and fail
+    # then it will resort to the environment variables
+    with environ('LANG', 'LANGUAGE', 'LC_ALL', 'LC_CTYPE'):
+        # Language can't be detected
+        assert AppriseLocale.AppriseLocale.detect_language() is None
+
+    with environ('LANGUAGE', 'LC_ALL', 'LC_CTYPE', LANG="fr_CA"):
+        # Detect french language
+        assert AppriseLocale.AppriseLocale.detect_language() == 'fr'
+
+    # Tidy
+    delattr(ctypes, 'windll')
+
+
+@mock.patch('locale.getdefaultlocale')
+def test_detect_language_defaultlocale(mock_getlocale):
+    """
+    API: Apprise() Default locale detection
+
+    """
     # Handle case where getdefaultlocale() can't be detected
     mock_getlocale.return_value = None
-    delattr(ctypes, 'windll')
     assert AppriseLocale.AppriseLocale.detect_language() is None
 
     # if detect_language and windows env fail us, then we don't