Sahli/editor.py
2020-04-10 02:18:54 +03:00

147 lines
4.6 KiB
Python

#!/usr/bin/env python
# coding:utf-8
"""
Author: Sir Garbagetruck --<truck@notonfire.somewhere>
Purpose: make editing the list.sahli file easier
Created: 2020/04/09
"""
import json
import argparse
import os
from sauce import SAUCE
from PIL import Image
from sahliEditorPython import sahlifile as SF
def getfilesindir(directory):
"""return the files in a directory as an array"""
for root, dirs, files, rootfd in os.fwalk(directory):
return files
def getfilenames(filedata):
"""return the file names from a sahli filedata array"""
f = []
for i in filedata:
f.append(i['file'])
return f
def getdata(filedata, name):
"""get the filedata entry where file = name"""
for i in filedata:
if i['file'] == name:
return i
return []
def getpicdata(filename):
"""extract picture data from filename"""
imagedata = Image.open(filename)
picdata = {
'width': imagedata.width,
'height': imagedata.height
}
return picdata
def getansidata(filename):
"""extract SAUCE data from filename"""
saucedata = SAUCE(filename)
ansidata = {
'author': saucedata.author,
'group': saucedata.group,
'title': saucedata.title,
'filesize': saucedata.filesize,
'comments': saucedata.comments,
'width': None,
'height': None
}
tinfonames = [saucedata.tinfo1_name,
saucedata.tinfo2_name,
saucedata.tinfo3_name,
saucedata.tinfo4_name]
tinfo = [saucedata.tinfo1,
saucedata.tinfo2,
saucedata.tinfo3,
saucedata.tinfo4]
for i in range(0, 3):
if tinfonames[i] == 'width':
ansidata['width'] = tinfo[i]
if tinfonames[i] == 'height':
ansidata['height'] = tinfo[i]
# print(tinfonames[i])
return ansidata
def getamigadata(filename):
"""try to get some form of info from file (:"""
return 'todo.txt'
def main(args):
"""maintain a list.sahli file"""
if args.new:
mysahli = SF.sahlifile(None)
else:
mysahli = SF.sahlifile(args.filename)
mysahli.sahli['location'] = args.directory
files = getfilesindir(args.directory)
filedata = mysahli.sahli['filedata']
filedatanames = getfilenames(filedata)
newdata = []
for i in files:
dirfile = '{}/{}'.format(args.directory, i)
if i in filedatanames:
print('found! {}'.format(i))
# todo: _if_ I ever make this a non-preparser, then... futz with
a = getansidata(dirfile)
newdata.append(getdata(filedata, i))
else:
print('not found! {}'.format(i))
suf = i.split('.')[-1]
if suf in ['png', 'jpg', 'jpeg', 'gif',
'PNG', 'JPG', 'JPEG', 'GIF']:
stuff = getpicdata(dirfile)
entry = mysahli.blank_picture()
entry['width'] = stuff['width']
entry['height'] = stuff['height']
entry['file'] = i
entry['name'] = i
newdata.append(entry)
elif suf in ['ans', 'ANS', 'BIN', 'bin', 'XB', 'xb']:
stuff = getansidata(dirfile)
entry = mysahli.blank_ansi()
entry['file'] = i
entry['name'] = stuff['title']
entry['author'] = '{}/{}'.format(
stuff['author'], stuff['group'])
entry['text'] = stuff['comments']
if stuff['height'] is not None:
entry['height'] = stuff['height']
if stuff['width'] is not None:
entry['width'] = stuff['width']
newdata.append(entry)
elif suf in ['TXT', 'ASC', 'txt', 'asc',
'NFO', 'nfo', 'diz', 'DIZ']:
stuff = getamigadata(dirfile)
else:
print("dunno what type of file this is...")
mysahli.sahli['filedata'] = newdata
out = json.dumps(mysahli.sahli, sort_keys=False, indent=4)
if args.outfile == '>stdout':
print(out)
else:
with open(args.outfile, 'w') as f:
json.dump(mysahli.sahli, f, sort_keys=False, indent=4)
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument('-f', '--filename', default='list.sahli')
ap.add_argument('-n', '--new', action='store_true')
ap.add_argument('-o', '--outfile', type=str, default='>stdout')
ap.add_argument('-d', '--directory', type=str, required=True,
help='directory where compo files are')
main(ap.parse_args())