#!/usr/bin/env python3
import os, pickle, re, sys
class Content(object):
def __init__(self, title, artist, rating, ripped):
self.title = title
self.artist = artist
self.rating = rating
self.ripped = ripped
def __repr__(self):
return f'Title: {self.title}\nArtist: {self.artist}\nRating: {self.rating}\nRipped: {self.ripped}\n'
class Env(object):
def __init__(self, package, ls = [], fname="", flag = False):
self.package = package
self.ls = ls
self.fname = fname
self.flag = flag
def __repr__(self):
return f'<package: {self.package} list: {self.ls} filename: {self.fname} flag: {self.flag}>'
def install_package():
def insert_value(arg, env):
return Env(env.package, ls = [Content(*arg)] + env.ls, fname = env.fname)
def select(arg, env):
return Env(env.package, ls = env.ls, fname = env.fname, flag = True)
def new_file(arg, env):
with open(''.join(arg) + ".dat", "wb") as f:
pass
return Env(env.package, ls = env.ls, fname = arg)
def open_file(arg, env):
fname = ''.join(arg)
with open(fname, "rb") as f:
ls = pickle.load(f)
return Env(env.package, ls = ls, fname = fname)
def save_file(arg, env):
if env.fname == '':
return save_file_as('tmp.dat', env)
else:
os.rename(env.fname, env.fname + '.bak')
with open(env.fname, "wb") as f:
pickle.dump(env.ls, f)
return Env(env.package, ls = env.ls, fname = env.fname)
def save_file_as(arg, env):
fname = ''.join(arg)
with open(fname, "wb") as f:
pickle.dump(env.ls, f)
return Env(env.package, ls = env.ls, fname = fname)
def delete(arg, env):
return Env(env.package,
ls = [i for i in env.ls if i != env.ls[int(arg[0])]],
fname = env.fname)
return {'insert_value': insert_value,
'select': select,
'new_file': new_file,
'open_file': open_file,
'save_file': save_file,
'save_file_as': save_file_as,
'quit': lambda arg, env: sys.exit(),
'delete': delete}
# Read
def read(env):
return [i.strip() for i in
re.split("\(|,|、|\)", input("simple database> "))
if i != ""]
# Eval
def interp(x, env):
return env.package[x[0]](x[1:], env)
# Print
def display(env):
if env.flag:
[print(i) for i in env.ls]
# print(env) # デバッグ用
return env
if __name__ == '__main__':
env = display(Env(install_package()))
while True:
try:
env = display(interp(read(env), env))
except KeyError:
env
except ValueError:
env
except IndexError:
env
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mbmFtZSkKICAgIGRlZiBzZWxlY3QoYXJnLCBlbnYpOgogICAgICAgIHJldHVybiBFbnYoZW52LnBhY2thZ2UsIGxzID0gZW52LmxzLCBmbmFtZSA9IGVudi5mbmFtZSwgZmxhZyA9IFRydWUpCiAgICBkZWYgbmV3X2ZpbGUoYXJnLCBlbnYpOgogICAgICAgIHdpdGggb3BlbignJy5qb2luKGFyZykgKyAiLmRhdCIsICJ3YiIpIGFzIGY6CiAgICAgICAgICAgIHBhc3MKICAgICAgICByZXR1cm4gRW52KGVudi5wYWNrYWdlLCBscyA9IGVudi5scywgZm5hbWUgPSBhcmcpCiAgICBkZWYgb3Blbl9maWxlKGFyZywgZW52KToKICAgICAgICBmbmFtZSA9ICcnLmpvaW4oYXJnKQogICAgICAgIHdpdGggb3BlbihmbmFtZSwgInJiIikgYXMgZjoKICAgICAgICAgICAgbHMgPSBwaWNrbGUubG9hZChmKQogICAgICAgIHJldHVybiBFbnYoZW52LnBhY2thZ2UsIGxzID0gbHMsIGZuYW1lID0gZm5hbWUpCiAgICBkZWYgc2F2ZV9maWxlKGFyZywgZW52KToKICAgICAgICBpZiBlbnYuZm5hbWUgPT0gJyc6CiAgICAgICAgICAgIHJldHVybiBzYXZlX2ZpbGVfYXMoJ3RtcC5kYXQnLCBlbnYpCiAgICAgICAgZWxzZToKICAgICAgICAgICAgb3MucmVuYW1lKGVudi5mbmFtZSwgZW52LmZuYW1lICsgJy5iYWsnKQogICAgICAgICAgICB3aXRoIG9wZW4oZW52LmZuYW1lLCAid2IiKSBhcyBmOgogICAgICAgICAgICAgICAgcGlja2xlLmR1bXAoZW52LmxzLCBmKQogICAgICAgICAgICByZXR1cm4gRW52KGVudi5wYWNrYWdlLCBscyA9IGVudi5scywgZm5hbWUgPSBlbnYuZm5hbWUpCiAgICBkZWYgc2F2ZV9maWxlX2FzKGFyZywgZW52KToKICAgICAgICBmbmFtZSA9ICcnLmpvaW4oYXJnKQogICAgICAgIHdpdGggb3BlbihmbmFtZSwgIndiIikgYXMgZjoKICAgICAgICAgICAgcGlja2xlLmR1bXAoZW52LmxzLCBmKQogICAgICAgIHJldHVybiBFbnYoZW52LnBhY2thZ2UsIGxzID0gZW52LmxzLCBmbmFtZSA9IGZuYW1lKQogICAgZGVmIGRlbGV0ZShhcmcsIGVudik6CiAgICAgICAgcmV0dXJuIEVudihlbnYucGFja2FnZSwKICAgICAgICAgICAgICAgICAgIGxzID0gW2kgZm9yIGkgaW4gZW52LmxzIGlmIGkgIT0gZW52LmxzW2ludChhcmdbMF0pXV0sCiAgICAgICAgICAgICAgICAgICBmbmFtZSA9IGVudi5mbmFtZSkKICAgIHJldHVybiB7J2luc2VydF92YWx1ZSc6IGluc2VydF92YWx1ZSwKICAgICAgICAgICAgJ3NlbGVjdCc6IHNlbGVjdCwKICAgICAgICAgICAgJ25ld19maWxlJzogbmV3X2ZpbGUsCiAgICAgICAgICAgICdvcGVuX2ZpbGUnOiBvcGVuX2ZpbGUsCiAgICAgICAgICAgICdzYXZlX2ZpbGUnOiBzYXZlX2ZpbGUsCiAgICAgICAgICAgICdzYXZlX2ZpbGVfYXMnOiBzYXZlX2ZpbGVfYXMsCiAgICAgICAgICAgICdxdWl0JzogbGFtYmRhIGFyZywgZW52OiBzeXMuZXhpdCgpLAogICAgICAgICAgICAnZGVsZXRlJzogZGVsZXRlfQoKIyBSZWFkCmRlZiByZWFkKGVudik6CiAgICByZXR1cm4gW2kuc3RyaXAoKSBmb3IgaSBpbgogICAgICAgICAgICByZS5zcGxpdCgiXCh8LHzjgIF8XCkiLCBpbnB1dCgic2ltcGxlIGRhdGFiYXNlPiAiKSkKICAgICAgICAgICAgaWYgaSAhPSAiIl0KCiMgRXZhbApkZWYgaW50ZXJwKHgsIGVudik6CiAgICByZXR1cm4gZW52LnBhY2thZ2VbeFswXV0oeFsxOl0sIGVudikKCiMgUHJpbnQKZGVmIGRpc3BsYXkoZW52KToKICAgIGlmIGVudi5mbGFnOgogICAgICAgIFtwcmludChpKSBmb3IgaSBpbiBlbnYubHNdCiAgICAjIHByaW50KGVudikgIyDjg4fjg5Djg4PjgrDnlKgKICAgIHJldHVybiBlbnYKCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiAgICBlbnYgPSBkaXNwbGF5KEVudihpbnN0YWxsX3BhY2thZ2UoKSkpCiAgICB3aGlsZSBUcnVlOgogICAgICAgIHRyeToKICAgICAgICAgICAgZW52ID0gZGlzcGxheShpbnRlcnAocmVhZChlbnYpLCBlbnYpKQogICAgICAgIGV4Y2VwdCBLZXlFcnJvcjoKICAgICAgICAgICAgZW52CiAgICAgICAgZXhjZXB0IFZhbHVlRXJyb3I6CiAgICAgICAgICAgIGVudgogICAgICAgIGV4Y2VwdCBJbmRleEVycm9yOgogICAgICAgICAgICBlbnYK