diff --git a/clusterview/point_manager.py b/clusterview/point_manager.py index 2d8c049..1ae1e21 100644 --- a/clusterview/point_manager.py +++ b/clusterview/point_manager.py @@ -1,3 +1,7 @@ +import json + +from .points import PointSet + class PointManager(): """ A state class that represents the absolute state of the @@ -5,3 +9,44 @@ class PointManager(): """ point_set = None + + @staticmethod + def load(location): + """ + Loads the JSON file from the location and populates point_set + with it's contents. + + @param location The location of the JSON file. + """ + with open(location) as json_file: + data = json.load(json_file) + + PointManager.point_set = PointSet(data['point_size'], + data['viewport_width'], + data['viewport_height']) + + for point in data['points']: + PointManager.point_set.add_point(point['x'], point['y']) + + @staticmethod + def save(location): + """ + Persists the point_set as a JSON file at location. + + @param location The persistence location. + """ + + data = {} + data['point_size'] = PointManager.point_set.point_size + data['viewport_width'] = PointManager.point_set.viewport_width + data['viewport_height'] = PointManager.point_set.viewport_height + data['points'] = [] + + for p in PointManager.point_set.points: + data['points'].append({ + 'x': p.x, + 'y': p.y + }) + + with open(location, 'w') as out_file: + json.dump(data, out_file) diff --git a/clusterview/points.py b/clusterview/points.py index be81f05..662a79b 100644 --- a/clusterview/points.py +++ b/clusterview/points.py @@ -109,9 +109,18 @@ class Point: return hash((self.__x, self.__y, self.__point_size)) def __repr__(self): - return "POINT".format(self.__x, - self.__y, - self.__point_size) + + # For some reason I had to split this instead of using one giant + # string chained with `+` inside of `()`. + s = "