diff --git a/updatePacks.py b/updatePacks.py new file mode 100644 index 0000000000000000000000000000000000000000..159b50d0e33e7d5d5237ffc4dc97d73f6ca7c219 --- /dev/null +++ b/updatePacks.py @@ -0,0 +1,48 @@ +import json +import sys + +# Check the arguments +# Pack is updated only if index and description are precised +update = False +if len(sys.argv) <= 1 or len(sys.argv) == 3: + print("Usage : python updatePack.py pack.json [sticker_index] [new_description]") + exit(1) +elif len(sys.argv) > 3: + update = True + +# Read the pack file +with open(sys.argv[1],'r') as myfile: + stickerpack = myfile.read() + +# Parse the pack +json_pack = json.loads(stickerpack) +nb_stickers = len(json_pack['stickers']) + + +if update == False: + # Print the number of stickers in this pack + print("There are " +str(nb_stickers) + " stickers in this pack: ") + for i in range(nb_stickers): + print(str(i+1) + ". " + json_pack['stickers'][i]['body']) + print("To update one of them type:") + print("python updatePack.py " + sys.argv[1] + " sticker_index 'new_description'") + exit(0) + +# Informations +sticker_index = int(sys.argv[2]) +new_body = sys.argv[3] + + +# Change the body of the sticker_index +if sticker_index > nb_stickers: + print("Index out of range : "+ str(sticker_index) + " > "+str(nb_stickers)) +elif sticker_index == 0: + print("Index starts at 1") +else: + json_pack['stickers'][sticker_index-1]['body'] = new_body + # Print the body of all the stickers + for i in range(nb_stickers): + print(str(i+1) + ". " + json_pack['stickers'][i]['body']) + out_file = open(sys.argv[1], 'w') + json.dump(json_pack, out_file, indent = 3) +