Skip to content

Instantly share code, notes, and snippets.

@fulcrum6378
Created March 22, 2023 02:55
Show Gist options
  • Save fulcrum6378/ea5785086f24c5cab46ae23706405db9 to your computer and use it in GitHub Desktop.
Save fulcrum6378/ea5785086f24c5cab46ae23706405db9 to your computer and use it in GitHub Desktop.
Convert Fortuna data (*.VITA) from Humanist Iranian calendar to Gregorian calendar
import sys
from calendar import monthrange
from datetime import date
from typing import Dict, List, Optional, Tuple
from persiantools.jdatetime import JalaliDate
class Luna:
diebus: List[Optional[float]]
emojis: List[Optional[str]]
verba: List[Optional[str]]
default: Optional[float]
emoji: Optional[str]
verbum: Optional[str]
def __init__(self, cal: int, year: int, month: int,
default: Optional[float] = None, emoji: Optional[str] = None, verbum: Optional[str] = None):
maxDies = Luna.max_dies(cal, year, month)
self.diebus = [None] * maxDies
self.emojis = [None] * maxDies
self.verba = [None] * maxDies
self.default = default
self.emoji = emoji
self.verbum = verbum
def __getitem__(self, item):
return self.diebus[item]
@staticmethod
def max_dies(cal, year, month) -> int:
if cal == Luna.CAL_GREGORIAN:
return monthrange(year, month)[1]
elif cal == Luna.CAL_IRANIAN:
return JalaliDate.days_in_month(month, year - 5000)
@staticmethod
def save_vita_text(s: str):
return s.replace('\n', '\\n')
CAL_GREGORIAN = 0
CAL_IRANIAN = 1
file = "fortuna.vita"
args = sys.argv[1:]
if len(args) > 0: file = args[0]
text = open(file, "r", encoding='utf-8').read()
del file, args
#
# Start loading VITA
#
vita: Dict[str, Luna] = dict()
key: Optional[str] = None
loadVitaText = lambda st: st.replace('\\n', '\n')
dies = 0
for ln in text.split('\n'):
if key is None:
if not ln.startswith('@'): continue
sn = ln.split(';', 3)
s = sn[0].split('~')
key = s[0][1:]
splitKey = key.split('.')
vita[key] = Luna(Luna.CAL_IRANIAN, int(splitKey[0]) + 5000, int(splitKey[1]),
float(s[1]) if len(s) > 1 else None,
sn[1] if len(sn) > 1 else None,
loadVitaText(sn[2]) if len(sn) > 2 else None)
dies = 0
else:
if len(ln) == 0:
key = None
continue
sn = ln.split(';', 3)
s = sn[0].split(':')
if len(s) == 2:
dies = int(s[0]) - 1
vita[key].diebus[dies] = float(s[1] if len(s) > 1 else s[0])
vita[key].emojis[dies] = sn[1] if len(sn) > 1 else None
vita[key].verba[dies] = loadVitaText(sn[2]) if len(sn) > 2 else None
dies += 1
del text, key, loadVitaText, dies, ln, sn, s, splitKey
#
# VITA loaded now!
#
map_dates: Dict[date, Tuple[Optional[float], Optional[str], Optional[str]]] = dict()
for k, v in vita.items():
spl = k.split('.')
for i in range(len(v.diebus)):
if v.diebus[i] is None: continue
map_dates[JalaliDate(int(spl[0]) - 5000, int(spl[1]), i + 1).to_gregorian()] = \
(v.diebus[i], v.emojis[i], v.verba[i])
vita: Dict[str, Luna] = dict()
for d, tup in map_dates.items():
luna = "{:04d}".format(d.year) + '.' + "{:02d}".format(d.month)
if luna not in vita: vita[luna] = Luna(Luna.CAL_GREGORIAN, d.year, d.month)
vita[luna].diebus[d.day - 1] = tup[0]
vita[luna].emojis[d.day - 1] = tup[1]
vita[luna].verba[d.day - 1] = tup[2]
del map_dates, k, v, i, spl, d, tup, luna
#
# Start dumping VITA
#
out = ""
vita = dict(sorted(vita.items()))
for k, luna in vita.items():
out += "@" + k
if luna.default is not None:
out += "~" + luna.default
if luna.emoji is not None and len(luna.emoji) > 0:
out += ";" + luna.emoji
elif luna.verbum is not None and len(luna.verbum) > 0:
out += ";"
if luna.verbum is not None and len(luna.verbum) > 0:
out += ";" + Luna.save_vita_text(luna.verbum)
out += "\n"
skipped = False
for d in range(len(luna.diebus)):
if luna.diebus[d] is None:
skipped = True
continue
if skipped:
out += str(d + 1) + ":"
skipped = False
out += str(luna.diebus[d])
if luna.emojis[d] is not None and len(luna.emojis[d]) > 0:
out += ";" + luna.emojis[d]
elif luna.verba[d] is not None and len(luna.verba[d]) > 0:
out += ";"
if luna.verba[d] is not None and len(luna.verba[d]) > 0:
out += ";" + Luna.save_vita_text(luna.verba[d])
out += "\n"
out += "\n"
del k, luna, skipped
# TODO read and write score functions
#
# VITA dumped now!
#
open("new_fortuna.vita", "w", encoding='utf-8').write(out)
del out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment