Last active
September 21, 2022 21:01
-
-
Save 648trindade/2c2e462d16071f6c89826c534d358b9b to your computer and use it in GitHub Desktop.
Python script to convert the text file that is inside the compressed .bak project file from software Magic Set Editor to a .db sqlite3 file. Please read the comments to custom it your way
This file contains hidden or 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
#!/usr/bin/python3 | |
""" | |
The MIT License (MIT) | |
Copyright (c) 2016 Rafael Gauna Trindade | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
of the Software, and to permit persons to whom the Software is furnished to do | |
so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
#!/usr/bin/python3 | |
from mse import magicseteditor | |
import sys, os | |
if (len(sys.argv) != 2): | |
print("Insira o nome do arquivo .bak!") | |
sys.exit(1) | |
bakfile = sys.argv[1] | |
iName = bakfile.rfind('/') | |
setname = bakfile[iName+1:bakfile.find('.',iName+1)] | |
msefile = setname + "/set" | |
dbfile = setname + ".db" | |
if not os.path.isdir(setname): | |
os.system("mkdir " + setname) | |
os.system("unzip -oqq {a} -d {b}/".format(a=bakfile, b=setname)) | |
to_remove = [ | |
'has_styling', | |
'notes', | |
'time_created', | |
'time_modified', | |
] | |
cards = magicseteditor(msefile) | |
cards.remove_fields(to_remove) | |
cards.normalize() | |
cards.optimize() | |
cards.create_sqlite_table(dbfile) | |
cards.dump_sqlite_table(dbfile) |
This file contains hidden or 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
""" | |
The MIT License (MIT) | |
Copyright (c) 2016 Rafael Gauna Trindade | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
of the Software, and to permit persons to whom the Software is furnished to do | |
so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
import os, sqlite3 | |
class magicseteditor: | |
def __init__(self, file): | |
with open(file) as fp: | |
s = fp.read() | |
lines = s.split('\n') | |
firstCard = lines.index('card:') | |
cards = list() | |
card = None | |
key, value = None, None | |
# cards | |
for line in lines[firstCard:]: | |
if line == 'card:': | |
if card is not None: | |
card[key] = value | |
cards.append(card) | |
card = dict() | |
elif line.count('\t'): | |
if line.count('\t') is 1: | |
if key is not None: | |
card[key] = value | |
index = line.find(':') | |
key = line[:index].strip().replace(' ','_') | |
value = line[index+1:].strip() | |
else: | |
value += line.strip() | |
else: | |
card[key] = value | |
cards.append(card) | |
break | |
self.cards = cards | |
def normalize(self): | |
# filling field non-existents | |
fields = self.get_fields() | |
for card in self.cards: | |
for field in fields: | |
if card.get(field) is None: | |
card[field] = "" | |
def remove_fields(self, fields): | |
for card in self.cards: | |
for field in fields: | |
if card.get(field) is not None: | |
card.pop(field) | |
def get_fields(self): | |
fields = set() | |
for card in self.cards: | |
for field in sorted(card.keys()): | |
fields.add(field) | |
return fields | |
def optimize(self): | |
to_keep = set() | |
fields = self.get_fields() | |
for card in self.cards: | |
for field in fields: | |
if card.get(field) not in (None, ""): | |
to_keep.add(field) | |
to_remove = fields - to_keep | |
for field in to_remove: | |
for card in self.cards: | |
try: | |
card.pop(field) | |
except: | |
pass | |
def create_sqlite_table(self, sqlite_file): | |
conn = sqlite3.connect(sqlite_file) | |
cursor = conn.cursor() | |
cursor.execute("DROP TABLE IF EXISTS cards;") | |
fields = sorted(self.get_fields()) | |
sql = "CREATE TABLE CARDS (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " | |
for field in fields: | |
sql += field | |
sql += ' VARCHAR(80), ' if field != 'text' else ' TEXT, ' | |
sql = sql[:-2] + ");" | |
cursor.execute(sql) | |
conn.commit() | |
conn.close() | |
def dump_sqlite_table(self, sqlite_file): | |
conn = sqlite3.connect(sqlite_file) | |
cursor = conn.cursor() | |
fields = sorted(self.get_fields()) | |
sql = "INSERT INTO cards (" + ', '.join(fields) + ") VALUES ('_*_');" | |
try: | |
for card in self.cards: | |
values = "', '".join( [card[field] for field in fields] ) | |
_sql = sql.replace('_*_',values) | |
cursor.execute(_sql) | |
except Exception as e: | |
print(_sql) | |
print("Erro ao inserir na tabela! Motivo:", e) | |
conn.commit() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh! Que bom que foi útil para você! Obrigado pela estrelinha!