import yaml
import os
import shlex

CONFIG_FILE_PATH = os.path.join(
    os.path.expanduser("~"), ".config", "discurses.yaml")
CACHE_DIR_PATH = os.path.join(os.path.expanduser("~"), ".cache", "discurses")
CACHE_AVATARS_PATH = os.path.join(CACHE_DIR_PATH, "avatars")

try:
    with open(CONFIG_FILE_PATH, 'r') as file:
        table = yaml.load(file)
except FileNotFoundError:
    print(
        "Create an authentication configuration file {path}"
        .format(
            path=shlex.quote(CONFIG_FILE_PATH)))
    raise


def create_dir(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)
    return directory


create_dir(CACHE_AVATARS_PATH)


async def send_notification(discord, message):
    """
    Send a system notification
    This will be called depending on the current notification settings
    """
    avatar = await discord.get_avatar(message.author)
    nickname = message.author.display_name
    if message.channel.is_private:
        os.system(
            "notify-send -i {avatar} \"{author} in chat with {users}:\" {content}"
            .format(
                avatar=shlex.quote(avatar),
                author=shlex.quote(nickname),
                content=shlex.quote(message.clean_content),
                users=shlex.quote(', '.join(a.display_name for a in message.channel.recipients))))
    else:
        os.system(
            "notify-send -i {avatar} \"{author} in {server}#{channel}\" {content}"
            .format(
                avatar=shlex.quote(avatar),
                author=shlex.quote(nickname),
                server=shlex.quote(message.server.name),
                channel=shlex.quote(message.channel.name),
                content=shlex.quote(message.clean_content)))


def file_picker(callback, chat_widget):
    """
    Open some sort of file picker
    This default implementation opens a text prompt

    The callback takes one argument, the file path

    chat_widget is the instance of ChatWidget.
    """

    def _callback2(txt):
        if txt is not None:
            path = os.path.expanduser(txt)
            if os.path.isfile(path):
                callback(path)
            else:
                chat_widget.open_text_prompt(_callback2, "File not found",
                                             path)
        else:
            chat_widget.close_pop_up()

    chat_widget.open_text_prompt(_callback2, "Send File")


def to_clipboard(text):
    os.system("echo {} | xclip -selection c".format(shlex.quote(text)))

@@ -1,16 +1,4 @@
-import yaml%0A
 impo
@@ -15,16 +15,62 @@
 rt shlex
+%0Aimport platform%0Aimport subprocess%0Aimport yaml
 %0A%0ACONFIG
@@ -294,16 +294,46 @@
 tars%22)%0A%0A
+PLATFORM = platform.system()%0A%0A
 try:%0A   
@@ -701,17 +701,16 @@
 ectory%0A%0A
-%0A
 create_d
@@ -733,16 +733,464 @@
 _PATH)%0A%0A
+def macos_notify(title, text):%0A    %22%22%22%0A    Create a MacOS notification%0A    Snippet from:%0A    https://stackoverflow.com/questions/17651017/python-post-osx-notification/41318195#41318195%0A    %22%22%22%0A    os.system(%22%22%22%0A              osascript -e 'display notification %22%7B%7D%22 with title %22%7B%7D%22'%0A              %22%22%22.format(text, title))%0A%0Adef linux_notify(arguments):%0A    %22%22%22%0A    Create a linux notification%0A    %22%22%22%0A    subprocess.Popen(%5B%22notify-send%22, arguments%5D)%0A
 %0Aasync d
@@ -1491,44 +1491,78 @@
     
-os.system(%0A            %22notify-send 
+if PLATFORM == %22Linux%22 and table%5B'notify'%5D:%0A            linux_notify(%22
 -i %7B
@@ -1607,32 +1607,45 @@
 %7D:%5C%22 %7Bcontent%7D%22%0A
+             
             .for
@@ -1657,32 +1657,45 @@
                 
+       
+      
 avatar=shlex.quo
@@ -1714,32 +1714,45 @@
                 
+           
+  
 author=shlex.quo
@@ -1757,32 +1757,45 @@
 uote(nickname),%0A
+             
                 
@@ -1854,16 +1854,29 @@
         
+  
+           
 users=sh
@@ -1889,16 +1889,64 @@
 ote(', '
+%0A                                               
 .join(a.
@@ -2006,58 +2006,235 @@
     
-else:%0A        os.system(%0A            %22notify-send 
+    elif PLATFORM == %22Darwin%22 and table%5B'notify'%5D:%0A            macos_notify(shlex.quote(nickname), shlex.quote(message.clean_content))%0A    else:%0A        if PLATFORM == %22Linux%22 and table%5B'notify'%5D:%0A                linux_notify(%22
 -i %7B
@@ -2295,33 +2295,69 @@
             
-.format(%0A
+                  .format(%0A                  
             
@@ -2396,32 +2396,50 @@
                 
+           
+       
 author=shlex.quo
@@ -2468,16 +2468,34 @@
         
+          
+        
 server=s
@@ -2543,16 +2543,34 @@
         
+        
+          
 channel=
@@ -2612,32 +2612,50 @@
                 
+       
+           
 content=shlex.qu
@@ -2683,16 +2683,154 @@
 tent)))%0A
+        elif PLATFORM == %22Darwin%22 and table%5B'notify'%5D:%0A            macos_notify(shlex.quote(nickname), shlex.quote(message.clean_content))
 %0A%0Adef fi

implement better notification (basic for MacOS for now), change to subprocess rather than os.system calls)