Init file handling and writing
authorAlex Schroeder <alex@gnu.org>
Thu, 8 Aug 2019 09:28:54 +0000 (11:28 +0200)
committerAlex Schroeder <alex@gnu.org>
Thu, 8 Aug 2019 09:28:54 +0000 (11:28 +0200)
jan-pona-mute.py [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 85cefe0..22614f4
@@ -1,4 +1,4 @@
-#!/usr/bin/env perl
+#!/usr/bin/env python3
 # Copyright (C) 2019  Alex Schroeder <alex@gnu.org>
 
 # This program is free software: you can redistribute it and/or modify it under
@@ -25,10 +25,15 @@ _ABBREVS = {
     "q":    "quit",
 }
 
-# Config file finder
+_RC_PATHS = (
+    "~/.config/jan-pona-mute/login",
+    "~/.config/.jan-pona-mute",
+    "~/.jan-pona-mute"
+)
+
+# Init file finder
 def get_rcfile():
-    rc_paths = ("~/.config/jan-pona-mute/login", "~/.config/.jan-pona-mute", "~/.jan-pona-mute")
-    for rc_path in rc_paths:
+    for rc_path in _RC_PATHS:
         rcfile = os.path.expanduser(rc_path)
         if os.path.exists(rcfile):
             return rcfile
@@ -44,7 +49,8 @@ class DiasporaClient(cmd.Cmd):
     password = None
 
     connection = None
-    notifications = None
+    things = []
+    index = None
 
     # dict mapping user ids to usernames
     users = {}
@@ -57,14 +63,11 @@ class DiasporaClient(cmd.Cmd):
             self.users[guid] = user.handle()
             return self.users[guid]
 
-    def __init__(self, account):
-        cmd.Cmd.__init__(self)
-        self.onecmd("account " + account)
-
     def do_account(self, account):
         """Set username and pod using the format username@pod."""
         try:
             (self.username, self.pod) = account.split('@')
+            print("Username and pod set: %s@%s" % (self.username, self.pod))
         except ValueError:
             print("The account must contain an @ character, e.g. kensanata@pluspora.com.")
             print("Use the account comand to set the account.")
@@ -72,34 +75,97 @@ class DiasporaClient(cmd.Cmd):
     def do_info(self, line):
         """Get some info about things. By default, it is info about yourself."""
         print("Info about yourself:")
-        print("Username: " + self.username)
-        print("Password: " + ("unset" if self.password == None else "set"))
-        print("Pod:      " + self.pod)
+        print("Username: %s" % self.username)
+        print("Password: %s" % ("None" if self.password == None else "set"))
+        print("Pod:      %s" % self.pod)
 
     def do_password(self, password):
         """Set the password."""
         self.password = (None if self.password == "" else password)
-        print("Password " + ("unset" if self.password == "" else "set"))
+        print("Password %s" % ("unset" if self.password == "" else "set"))
+
+    def do_save(self, line):
+        if self.username == None or self.pod == None:
+            print("Use the account command to set username and pod")
+        elif self.password == None:
+            print("Use the password command")
+        else:
+            rcfile = get_rcfile()
+            if rcfile == None:
+                rfile = first(_RC_PATHS)
+            seen_account = False
+            seen_password = False
+            seen_login = False
+            changed = False
+            file = []
+            with open(rcfile, "r") as fp:
+                for line in fp:
+                    words = line.strip().split()
+                    if words:
+                        if words[0] == "account":
+                            seen_account = True
+                            account = "%s@%s" % (self.username, self.pod)
+                            if len(words) > 1 and words[1] != account:
+                                line = "account %s\n" % account
+                                changed = True
+                        elif words[0] == "password":
+                            seen_password = True
+                            if len(words) > 1 and words[1] != self.password:
+                                line = "password %s\n" % self.password
+                                changed = True
+                        elif words[0] == "login":
+                            if seen_account and seen_password:
+                                seen_login = True
+                            else:
+                                # skip login if no account or no password given
+                                line = None
+                                changed = True
+                    if line != None:
+                        file.append(line)
+                if not seen_account:
+                    file.append("account %s@%s\n" % (self.username, self.pod))
+                    changed = True
+                if not seen_password:
+                    file.append("password %s\n" % self.password)
+                    changed = True
+                if not seen_login:
+                    file.append("login\n")
+                    changed = True
+            if changed:
+                if os.path.isfile(rcfile):
+                    os.rename(rcfile, rcfile + "~")
+                if not os.path.isdir(os.path.dirname(rcfile)):
+                    os.makedirs(os.path.dirname(rcfile))
+                with open(rcfile, "w") as fp:
+                    fp.write("".join(file))
+                print("Wrote %s" % rcfile)
+            else:
+                print("No changes made, %s left unchanged" % rcfile)
 
     def do_login(self, line):
         """Login."""
         if line != "":
-            self.onecmd("account " + line)
-        if self.password == "":
-            print("Use the password command to set a password for " + self.username)
-            return
-        self.connection = diaspy.connection.Connection(
-            pod = "https://" + self.pod, username = self.username, password = self.password)
-        self.connection.login()
-        self.onecmd("notifications")
+            self.onecmd("account %s" % line)
+        if self.username == None or self.pod == None:
+            print("Use the account command to set username and pod")
+        elif self.password == None:
+            print("Use the password command")
+        else:
+            self.connection = diaspy.connection.Connection(
+                pod = "https://%s" % self.pod, username = self.username, password = self.password)
+            try:
+                self.connection.login()
+                self.onecmd("notifications")
+            except diaspy.errors.LoginError:
+                print("Login failed")
 
     def do_notifications(self, line):
-        """Show notifications."""
+        """List notifications."""
         if self.connection == None:
             print("Use the login command, first.")
             return
-        self.notifications = diaspy.notifications.Notifications(self.connection).last()
-        for n, notification in enumerate(self.notifications):
+        self.things = diaspy.notifications.Notifications(self.connection).last()
+        for n, notification in enumerate(self.things):
             print("%2d. %s %s" % (n+1, notification.when(), notification))
 
     ### The end!
@@ -119,26 +185,49 @@ class DiasporaClient(cmd.Cmd):
             expanded = line.replace(first_word, full_cmd, 1)
             return self.onecmd(expanded)
 
+        # Try to parse numerical index for lookup table
+        try:
+            n = int(line.strip())
+        except ValueError:
+            print("Use the help command")
+            return
+
+        try:
+            item = self.things[n-1]
+        except IndexError:
+            print ("Index too high!")
+            return
+
+        self.index = n
+        self.show(item)
+
+    def show(self, item):
+        """Show the current item."""
+        print (str(item))
+
 # Main function
 def main():
 
     # Parse args
     parser = argparse.ArgumentParser(description='A command line Diaspora client.')
-    parser.add_argument('account',
-                        help='your account, e.g. kensanata@pluspora.com')
+    parser.add_argument('--no-init-file', dest='init_file', action='store_const',
+                        const=False, default=True, help='Do not load a init file')
     args = parser.parse_args()
 
     # Instantiate client
-    c = DiasporaClient(args.account)
-
-    # Process config file
-    rcfile = get_rcfile()
-    if rcfile:
-        print("Using config %s" % rcfile)
-        with open(rcfile, "r") as fp:
-            for line in fp:
-                line = line.strip()
-                c.cmdqueue.append(line)
+    c = DiasporaClient()
+
+    # Process init file
+    if args.init_file:
+        rcfile = get_rcfile()
+        if rcfile:
+            print("Using init file %s" % rcfile)
+            with open(rcfile, "r") as fp:
+                for line in fp:
+                    line = line.strip()
+                    c.cmdqueue.append(line)
+        else:
+            print("Use the save command to save your login sequence to an init file")
 
     # Endless interpret loop
     while True: