Exemple #1
0
    def do_destroy(self, args):
        """
        Deletes an instance and saves the changes into JSON file

        Usage: destroy [class name] [id]
        """

        if not args:  # if no arguments are passed
            print("** class name missing **")
            return

        try:  # testing arguments
            cls_name, cls_id = args.split(' ')
            obj_constructor(cls_name)
        except ValueError:  # test for second argument
            print("** instance id missing **")
            return
        except NameError:  # test if class exist
            print("** class doesn't exist **")
            return

        inst = cls_name + '.' + cls_id
        if inst in storage.all():  # check if instance exist
            del storage.all()[inst]  # delete obj
            storage.save()  # save changes into JSON file
            self.__instances = storage.all()  # update inst for autocomplete
        else:
            print("** no instance found **")

        return
Exemple #2
0
    def do_show(self, args):
        """
        Prints the string representation of an instance

        Usage: show [class name] [id]
        """

        if not args:  # if no arguments are passed
            print("** class name missing **")
            return

        try:  # testing arguments
            cls_name, cls_id = args.split(' ')
            obj_constructor(cls_name)
        except ValueError:  # test for second argument
            print("** instance id missing **")
            return
        except NameError:  # test if class exist
            print("** class doesn't exist **")
            return

        inst = cls_name + '.' + cls_id
        instances = storage.all()
        if inst in instances:  # check if instance exist
            print(instances[inst])  # get obj
        else:
            print("** no instance found **")
        return
Exemple #3
0
    def do_all(self, cls_name):
        """
        Prints all string representation of all or class instances

        Usage: all [(class name)]
        """

        result = ['[']
        trigger = 0 if cls_name else 1  # if optional argument was passed

        if not trigger:
            try:  # check if class exist
                obj_constructor(cls_name)
            except:
                print("** class doesn't exist **")
                return

        for obj in storage.all().values():
            if trigger or obj.__class__.__name__ == cls_name:
                result.append(str(obj) + '\n')

        result = "".join(result)
        if len(result) == 1:  # if no instances exist, send empty '[]'
            result += ']'

        print(result[:-1] + ']')  # last '\n' will be overwritten with ']'
Exemple #4
0
    def do_update(self, args):
        """
         Updates an instance based on the class name and id

         Usage: update <class name> <id> <attribute name> "<attribute value>"
         """

        if not args:  # testing arguments are passed
            print("** class name missing **")
            return

        args = args.split()

        if len(args) >= 1:
            try:  # testing `class` argument
                obj_constructor(args[0])
            except NameError as error:
                print("** class doesn't exist **")
                return
        else:
            print("** class name missing **")

        if len(args) < 2:  # testing `id` argument
            print("** instance id missing **")
            return
        else:
            inst = args[0] + '.' + args[1]  # `class.id`
            instances = storage.all()
            if inst in instances:  # get obj
                obj = instances[inst]
            else:
                print("** no instance found **")
                return

        if len(args) < 3:  # testing `attribute` argument
            print("** attribute name missing **")
            return
        elif args[2] in "id, created_at, updated_at":  # shouldnt modify
            return
        else:
            if len(args) < 4:  # testing `value` argument
                print("** value missing **")
                return
            else:  # strip the quotes
                args[3] = args[3].strip('\"')

        try:
            setattr(obj, args[2], args[3])  # update instance
        except TypeError:
            print("** Invalid value type for respective key **")

        obj.save()  # save updated obj
        return
Exemple #5
0
    def do_create(self, cls_name):
        """
        Creates a new instance of `BaseModel`

        Usage: create [class name]
        """
        if not cls_name:
            print("** class name missing **")
        else:
            try:  # test if class exist
                instance = obj_constructor(cls_name, 1)
            except NameError:
                print("** class doesn't exist **")
            else:
                print(instance.id)
                instance.save()  # save instance to JSON file
                self.__instances = storage.all()  # update inst autocomplete
        return