-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
132 lines (112 loc) · 4.19 KB
/
Copy pathutils.py
File metadata and controls
132 lines (112 loc) · 4.19 KB
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
131
132
import json
import os
import shutil
import spider as sp
import random
def readJson(path):
with open(path, 'r') as f:
return json.load(f)
def remove_file(path):
if os.path.isfile(path):
os.remove(path)
def remove_dir(path):
if os.path.isdir(path):
shutil.rmtree(path)
def remove_web(webFolder):
if os.path.isdir(os.path.join(os.getcwd(), webFolder)):
shutil.rmtree(os.path.join(os.getcwd(), webFolder))
def check_attribute(testObj, attr, attributeType, attributeValue):
testObj.assertIsInstance(attr, attributeType)
testObj.assertEqual(attr, attributeValue)
def check_direc_file_lists(testObj, prefix, direcList, fileList):
for item in direcList:
testObj.assertTrue(os.path.isdir(os.path.join(prefix, item)))
for item in fileList:
testObj.assertTrue(os.path.isfile(os.path.join(prefix, item)))
def makeTestFile(path):
f = open(path, "w")
f.close()
def create_basic_web(folderName, title, numNodes):
web = sp.createWeb({"path" : os.path.join(os.getcwd(), folderName), "title" : title})
nodeList = []
for i in range(numNodes):
nodeList.append(str(web.addNode({"title" : "Node " + str(i)}).uuid))
for i in range(len(nodeList) - 1):
web.addEdge({"title" : "Edge " + str(i + 1), "relation" : {"source" : nodeList[i], "target" : nodeList[i + 1]}})
web.addEdge({"title" : "Edge", "relation" : {"source" : nodeList[3], "target" : nodeList[5]}})
return web
def create_web_from_media(folderOfMediaFiles, webFolderName):
mediaFiles = collectFiles(folderOfMediaFiles)
metadata = {"path" : os.path.join(os.getcwd(), webFolderName)}
web = sp.createWeb(metadata)
# Create nodes
nodeList = []
imgNodes = []
for item in mediaFiles:
thisNode = web.mediaToNode(item, True)
nodeList.append(thisNode.uuid)
if thisNode.format.type == "image":
imgNodes.append(thisNode.uuid)
# Create inters
for node in nodeList:
for target in nodeList:
if node != target:
web.addEdge({
"title" : "test edge",
"relation" : {
"source" : node,
"target" : target
}
})
# Create non-media nested:
loaded = web.loadNode(node)
loaded.addNode({
"title" : "Nested test"
})
# Create non-media nested with dimensions:
loaded.addNode({
"title" : "Nested test with dimensions",
"instructionalMethod" : {
"annotationPaint" : True,
"annotationOverlay" : True,
"annotationDisplayPos" : [10, 10]
},
"format" : {
"fullDimensions" : [50, 50]
}
})
if loaded.format.type == "video":
# Create non-media nested with dimensions and time:
loaded.addNode({
"title" : "Nested test with dimensions and time",
"instructionalMethod" : {
"annotationPaint" : True,
"annotationOverlay" : True,
"annotationDisplayPos" : [60, 60]
},
"format" : {
"fullDimensions" : [50, 50]
},
"relation" : {
"sourceRegions" : [{
"start" : 1000,
"end" : 5000
}]
}
})
# Create image nested node
if len(imgNodes) > 0:
loadedRandomImg = web.loadNode(random.choice(imgNodes))
newNestedImgNode = loaded.duplicateNode(loadedRandomImg, duplicateNested = False)
newNestedImgNode.title = "Image nested annotation"
newNestedImgNode.write()
return web
def collectFiles(path):
acceptedFormats = ["mp4", "png", "jpg", "jpeg", "pdf"]
finalList = []
for root, dirs, files in os.walk(path):
for file in files:
extension = os.path.splitext(file)[1][1:]
if extension in acceptedFormats:
finalList.append(os.path.join(root, file))
return finalList