Last active
September 26, 2018 14:02
-
-
Save JakubMifek/48f6ec402482cb8f68209481e9e3f8a9 to your computer and use it in GitHub Desktop.
Filter for snapshots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import json | |
import sys | |
def tokenize(line): | |
return [token.strip() for token in line.split(':')] | |
def getSnapshot(line, idx, f): | |
print(line) | |
tokens = tokenize(line) | |
if len(tokens) > 0: | |
if tokens[0] == 'Name': | |
snapshot = {} | |
snapshot[tokens[0]] = tokens[1] | |
for line in f[idx:]: | |
tokens = tokenize(line) | |
if len(tokens) < 2: | |
break | |
name = tokens[0] | |
value = ':'.join(tokens[1:]) | |
snapshot[name] = value | |
return snapshot | |
elif tokens[0] == 'Track Name': | |
return tokens[1] | |
def getArray(): | |
with open('tmp.txt', 'r') as f: | |
list_of_snapshots = [] | |
track = 'None' | |
for line in f: | |
if '\\n' in line: | |
line = line.replace('\\n', '\n') | |
line = line.replace('\\t', '\t') | |
items = line.split('\n') | |
for idx, l in enumerate(items): | |
result = getSnapshot(l, idx, items) | |
if type(result) == str: | |
track = result | |
elif result != None: | |
result['Track'] = track | |
list_of_snapshots.append(result) | |
else: | |
result = getSnapshot(line, f) | |
if type(result) == str: | |
track = result | |
elif ressult != None: | |
result['Track'] = track | |
list_of_snapshots.append(result) | |
return list_of_snapshots | |
def getJSON(): | |
array = getArray() | |
print('number of snapshots:') | |
print(len(array)) | |
return json.dumps(array, indent=2) | |
def saveJSON(): | |
with open('tmp.json', 'w+') as f: | |
f.write(getJSON()) | |
def getFilteredItems(track=None): | |
array = getArray() | |
print('number of snapshots:') | |
print(len(array)) | |
filtered = [ | |
item for item in array | |
if 'State' in item and item['State'] == 'State[Archived]' and ( | |
track == None or item['Track'] == track) | |
] | |
print('number of archived snapshots:') | |
print(len(filtered)) | |
return filtered | |
def getFilteredNames(track=None): | |
return [item['Name'] for item in getFilteredItems(track=track)] | |
def saveFilteredItems(track=None): | |
with open('tmp.json', 'w+') as f: | |
f.write(json.dumps(getFilteredItems(track=track), indent=2)) | |
def saveFilteredNames(track=None): | |
with open('tmp.json', 'w+') as f: | |
f.write(json.dumps(getFilteredNames(track=track), indent=2)) | |
if __name__ == '__main__': | |
track = None | |
if len(sys.argv) > 1: | |
track = sys.argv[1] | |
# Uncomment for various functionality: | |
saveFilteredItems(track=track) # Saves all information about snapshot | |
#saveFilteredNames(track=track) # Saves snapshots' names | |
#saveJSON() # Saves all snapshots |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment