Last active
May 20, 2021 21:54
-
-
Save Hiroshiba/b47c8e5fa9a67007dfc87e8c74ce5489 to your computer and use it in GitHub Desktop.
PySide2 Property Generator
This file contains 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
import re | |
from pathlib import Path | |
data = """\ | |
MainModel | |
now_recording_uniq str "" | |
now_playing_uniq str "" | |
""" | |
name = None | |
properties = [] | |
types = [] | |
defaults = [] | |
for i, line in enumerate(Path("data.txt").read_text().strip().splitlines()): | |
if i == 0: | |
name = line.strip() | |
else: | |
p, t, d = line.split() | |
properties.append(p) | |
types.append(t) | |
defaults.append(d) | |
output = f"""\ | |
from PySide2.QtCore import Property, QObject, Signal | |
class {name}(QObject): | |
def __init__(self): | |
super().__init__(parent=None) | |
""" | |
for p, d in zip(properties, defaults): | |
output += f"""\ | |
self.{p} = {d} | |
""" | |
for p, t, d in zip(properties, types, defaults): | |
c = re.sub("_(.)", lambda m: m.group(1).upper(), p) | |
output += f""" | |
def get_{p}(self): | |
return self.{p} | |
def set_{p}(self, value): | |
self.{p} = value | |
self.changed_{p}.emit() | |
@Signal | |
def changed_{p}(self): | |
pass | |
{c} = Property( | |
{t}, | |
fget=get_{p}, | |
fset=set_{p}, | |
notify=changed_{p}, | |
) | |
""" | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment