Source

chester / chester / actions.py

Full commit
"""
actions.py: CRUDs the chester file written in JSON.
File is located in ~/.chester by default, change location if you wish.
"""

import os.path
import json

LIST_PATH = os.path.expanduser('~') + "/.chester"


class Actions:
    """
        Copies JSON to dictionary, does CRUD actions, and outputs JSON to file.
    """
    def __init__(self):
        """
            Open JSON file and load contents to dictionary.
        """
        self.storage = {}
   
        if not os.path.exists(LIST_PATH):
            with open(LIST_PATH, "w") as f:
                json.dump(self.storage, f)
   
        with open(LIST_PATH, 'r') as f:
            self.storage = json.load(f)
    
    def create(self, sublist):
        """
            Creates a new sublist in the master list.
        """
        self.storage[sublist] = {}
        print "{0} (sublist) created".format(sublist)
        
    def add(self, sublist, entry, value=""):
        """
            Adds an element to specified sublist.
        """
        self.storage[sublist][entry] = value
        print "{0} {1} added to {2}".format(entry, value, sublist)
    
    def delete(self, sublist, entry='', value=''):
        """
            Deletes a complete sublist or single entry.
        """
        if sublist and not entry and not value:
            del self.storage[sublist]
            print "{0} (sublist) deleted".format(sublist)
        elif sublist and entry and value == "delete":
            del self.storage[sublist][entry]

    def overview(self):
        """
            Prints all sublist entries with number of elements.
        """
        print "-------\nCHESTER\n-------"
        for entry in self.storage:
            print "\t{0}({1})".format(entry, len(self.storage[entry]))

    def show(self, sublist='', entry=''):
        """
            Prints elements of each sublist according to the params supplied.
            if no params passed, shows all sublists with elements.
            if sublist is given, shows all elements in the sublist.
            if sublist and entry params are given, shows associated value.
        """
        print "-------\nCHESTER\n-------"
        if sublist and not entry:
            for item in self.storage:
                if item == sublist:
                    print "({0})\n".format(item)
                    for element in self.storage[item]:
                        value = self.storage[item][element]
                        print "\t{0} ~ {1}".format(element, value)
        elif sublist and entry:
            print self.storage[sublist][entry]
        else:
            for item in self.storage:
                print "({0})".format(item)
                for element in self.storage[item]:
                    # TODO: add a "smart" delimiter. "by" "-" ":" "~"
                    value = self.storage[item][element]
                    print "\t{0} ~ {1}".format(element, value)

    def help(self):
        """
            Prints a help menu.
        """
        print "help on the way..."
        help = """
Command             Description
-------             -----------
chester             Displays an overview
chester all         Displays every list and elements

chester <list name>     Creates a sublist or displays it

"""
        print help

    def save(self):
        """
            Saves dictionary to JSON file.
        """
        with open(LIST_PATH, 'w') as f:
            json.dump(self.storage, f, indent=2)