This repository has been archived by the owner on Jul 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonManager.py
51 lines (47 loc) · 2.47 KB
/
jsonManager.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
#!/usr/bin/env python3
# JSON/Metadata pre-push script for python-based HDWX
# Created 4 August 2021 by Sam Gardner <[email protected]>
import json
from os import path, listdir, walk, remove
from datetime import datetime as dt
import shutil
from pathlib import Path
from natsort import natsorted
if __name__ == "__main__":
tmpFrameMetaStorage = path.join(path.dirname(path.abspath(__file__)), "frameMetaData/")
outputDir = path.join(path.dirname(path.abspath(__file__)), "output/")
metadataOutDir = path.join(outputDir, "metadata/")
publishTime = dt.utcnow()
for hour in listdir(tmpFrameMetaStorage):
metadataForHour = path.join(tmpFrameMetaStorage, hour)
if int(hour) == publishTime.hour:
currentRunDir = path.join(tmpFrameMetaStorage, str(hour))
for productID in sorted(listdir(metadataForHour)):
framesArray = list()
productFramesJsonPath = path.join(metadataForHour, productID)
for frameJsonFilename in natsorted(listdir(productFramesJsonPath)):
frameJsonPath = path.join(productFramesJsonPath, frameJsonFilename)
with open(frameJsonPath) as readJson:
frameJson = json.load(readJson)
framesArray.append(frameJson)
productRunDict = {
"publishTime" : int(dt.utcnow().strftime("%Y%m%d%H%M")),
"pathExtension" : publishTime.strftime("%Y/%m/%d/%H00/"),
"runName" : publishTime.strftime("%d %b %Y %HZ"),
"availableFrameCount" : len(framesArray),
"totalFrameCount" : 12,
"productFrames" : framesArray
}
saveFilePath = path.join(path.join(metadataOutDir, "products/"), productID)
Path(saveFilePath).mkdir(parents=True, exist_ok=True)
for oldMetaData in listdir(saveFilePath):
remove(path.join(saveFilePath, oldMetaData))
saveFilePath = path.join(saveFilePath, publishTime.strftime("%Y%m%d%H00")+".json")
with open(saveFilePath, "w") as jsonWrite:
json.dump(productRunDict, jsonWrite, indent=4)
else:
shutil.rmtree(metadataForHour)
for tree in walk(outputDir):
if tree[1] == [] and "metadata" not in tree[0]:
if publishTime.strftime("%Y/%m/%d/%H00") not in tree[0]:
shutil.rmtree(tree[0])