-
Notifications
You must be signed in to change notification settings - Fork 0
/
via_resource.py
131 lines (103 loc) · 4.02 KB
/
via_resource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import json
import os
class ViaResource:
def __init__(self, json_path=None):
if json_path:
self.load(json_path)
def validate(self, json_path):
try:
with open(json_path) as json_file:
data = json.load(json_file)
if 'title' not in data:
print("Can't find a title field!")
return False
except:
print("Error loading " + str(json_path))
return False
return True
def load(self, json_path):
try:
with open(json_path) as json_file:
data = json.load(json_file)
except:
print("Error loading " + str(json_path))
return False
self.data = data
return True
def save(self, save_path):
with open(save_path, 'w') as save_file:
json.dump(self.data, save_file)
class ViaResourceSet(ViaResource):
def __init__(self, resource_type, slug, resource_set_dir, resource_dir):
super().__init__(resource_set_dir + slug + '.json')
self.get_clean_set()
self.resource_type = resource_type
self.resource_dir = resource_dir
self.resource_set_dir = resource_set_dir
self.init_resources()
self.slug = slug
def save_set(self, slug, description):
self.data['title'] = slug
self.data['description'] = description
self.save(self.resource_set_dir + slug + '.json')
self.slug = slug
self.get_clean_set()
def load_set(self, slug):
self.load(self.resource_set_dir + slug + '.json')
self.get_clean_set()
self.slug = slug
self.init_resources()
def is_clean(self):
if self.data['slug_list'] == self.clean_set:
return True
else:
return False
def restore(self):
self.data['slug_list'] = []
for slug in self.clean_set:
self.data['slug_list'].append(slug)
def get_clean_set(self):
self.clean_set = []
for slug in self.data['slug_list']:
self.clean_set.append(slug)
def init_resources(self):
self.resources = []
for slug in self.data['slug_list']:
self.add_resource(self.load_resource(slug))
def add_resource(self, resource):
self.resources.append(resource)
def load_resource(self, slug):
return self.resource_type(self.resource_dir + slug + '.json')
def restore_resource(self, idx):
self.resources[idx]=self.load_resource(self.data['slug_list'][idx])
def replace_resource(self, slug, index):
self.resources[index] = self.load_resource(slug)
self.data['slug_list'][index] = slug
def save_resource(self, slug, index, description):
self.resources[index].data['title'] = slug
self.resources[index].data['description'] = description
self.resources[index].save(self.resource_dir + slug + '.json')
self.data['slug_list'][index] = slug
def make_title_maps(self, slugs, slug_path):
slug_to_title = {}
title_to_slug = {}
for slug in slugs:
with open(slug_path + slug + '.json') as thefile:
title = json.load(thefile)['title']
slug_to_title[slug] = title
title_to_slug[title] = slug
return slug_to_title, title_to_slug
def get_available_resources(self, search_dir=None):
if not search_dir:
search_dir = self.resource_dir
resources = []
for root, dirs, files in os.walk(search_dir):
for file in [x for x in files if ".json" in x]:
if self.validate(os.path.join(root, file)):
resources.append(file.replace('.json', ''))
else:
print("Bad file at " + os.path.join(root, file))
break
return self.make_title_maps(resources, search_dir)
def get_available_resource_sets(self):
return self.get_available_resources(self.resource_set_dir)