This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
card.py
110 lines (96 loc) · 3.6 KB
/
card.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
import glob, os
from yaml import load, dump
from common import Model
from figure import Figure
import config
class Card(Model):
filename = ""#filename without extentions
title = "title"
figure = ["code"]
figure_source = "material-icons"
figure_parsed = []
#material-icons = https://material.io/icons/
#mdi = https://materialdesignicons.com/
#fa = http://fontawesome.io/icons/
#svg = the svgs/ folder
description = ""
steps = []
effects = []
cost = ""
power = None
cp = None
gp = None#gold
flags = []
notes = ""#not shown, but used to keep track of things
copies_owned = 1
tag = ""
def has_flag(self, flag):
return flag.lower() in map(lambda x: x.lower(), self.flags)
#todo: make the relevant ones into coroutines:
def from_file(filename, in_carddir=True):#yaml syntax
if filename[-5:] != ".yaml":
filename += ".yaml"
name = ".".join(os.path.basename(filename).split(".")[:-1])
with open(os.path.join(config.carddir, filename) if in_carddir else filename, "r") as f:
return from_yaml(f.read(), name)
def to_file(card, in_carddir=True):
assert card.filename, "no filename set"
filename = card.filename+".yaml"
with open(os.path.join(config.carddir, filename) if in_carddir else filename, "w") as f:
f.write(to_yaml(card))
def del_file(card, in_carddir=True):
assert card.filename, "no filename set"
filename = card.filename+".yaml"
os.remove(os.path.join(config.carddir, filename) if in_carddir else filename)
def from_yaml(data, filename="from_yaml"):
card = Card()
card.filename = filename
for key, val in load(data).items():
setattr(card, key, val)
setattr(card, "figure_parsed", [Figure(line, getattr(card, "figure_source")) for line in getattr(card, "figure")])
return card
def to_yaml(card):
out = {}
for key in dir(card):
if "_" not in key[0] and key not in ("filename","has_flag","figure_parsed"):
val = getattr(card, key)
if (val or val==0) and val != getattr(Card, key):
out[key] = val
return dump(out, default_flow_style=False)
def from_dir(path):
return [from_file(i, in_carddir=False) for i in glob.glob(os.path.join(path, "*.yaml"))]
def from_form(form):#sanic's request.form
card = Card()
for key, val in form.items():
if not val[0]: continue
if key in ("save", "delete"): continue
if type(getattr(Card, key)) in (tuple, list):
if len(val) == 1 and "\n" in val[0]:
val = val[0].strip().replace("\r\n", "\n").split("\n")
#val = [i for i in val if i]
setattr(card, key, val)
else:
setattr(card, key, val[0].strip().replace("\r\n", "\n"))
setattr(card, "figure_parsed", [Figure(line, getattr(card, "figure_source")) for line in getattr(card, "figure")])
return card
def is_filename_vacant(filename, in_carddir=True):
if in_carddir:
filename = os.path.join(config.carddir, filename)
if filename[-5:] != ".yaml":
filename += ".yaml"
return not os.path.exists(filename)
def get_vacant_filename():
i = 1
while 1:
if is_filename_vacant("card-%s" % str(i).zfill(4)):
return "card-%s" % str(i).zfill(4)
i += 1
class open_file:#contextmanager
def __init__(self, filename):
self.filename = filename
def __enter__(self):
self.card = from_file(self.filename)
return self.card
def __exit__(self, *args):
to_file(self.card)