Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjusted file_storage.py all() to handle class specifier #5

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions models/engine/file_storage.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#!/usr/bin/python3
"""This module defines a class to manage file storage for hbnb clone"""
import json
from console import HBNBCommand


class FileStorage:
"""This class manages storage of hbnb models in JSON format"""
__file_path = 'file.json'
__objects = {}

def all(self):
"""Returns a dictionary of models currently in storage"""
return FileStorage.__objects
def all(self, cls=None):
"""Returns a dictionary of models (of a class) currently in storage"""
if cls in HBNBCommand.classes:
cls_dict = {key: value for key, value in
FileStorage.__objects.items()
if isinstance(value, cls)}
return cls_dict
else:
return FileStorage.__objects

def new(self, obj):
"""Adds new object to storage dictionary"""
Expand Down Expand Up @@ -48,3 +55,9 @@ def reload(self):
self.all()[key] = classes[val['__class__']](**val)
except FileNotFoundError:
pass

def delete(self, obj=None):
"""Deletes object in database"""
if obj and obj in FileStorage.__objects:
del self.all()[obj.id]
self.save()