Skip to content

Commit

Permalink
Unified yaml format parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Egil committed May 21, 2024
1 parent 1a2bcb0 commit f352e9f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 70 deletions.
2 changes: 1 addition & 1 deletion glass-components/glass_components/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, display, **kw):

configpath = os.path.expanduser(os.environ.get("GLASS_GHOSTS_CONFIG", "~/.config/glass/components.yml"))
with open(configpath) as f:
self.config = json.loads(json.dumps(yaml.load(f, Loader=yaml.SafeLoader)), object_hook=InfiniteGlass.fromjson(self.display))
self.config = InfiniteGlass.load_yaml(f, self.display)

self.components = {}
self.components_by_pid = {}
Expand Down
15 changes: 9 additions & 6 deletions glass-config-init/glass_config_init/ghosts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ignore:
- ["_NET_WM_WINDOW_TYPE", "_NET_WM_WINDOW_TYPE_MENU"]
- ["_NET_WM_WINDOW_TYPE", "_NET_WM_WINDOW_TYPE_UTILITY"]
- ["_NET_WM_WINDOW_TYPE", "_NET_WM_WINDOW_TYPE_SPLASH"]
- ["WM_WINDOW_ROLE", {"__jsonclass__": ["string", "GtkFileChooserDialog"]}]
- ["WM_WINDOW_ROLE", !string "GtkFileChooserDialog"]

# Use these properties to match a window to an existing ghost
match:
Expand Down Expand Up @@ -68,17 +68,20 @@ ghost_update:
# might match the same template (by design).
templates:
.*-gimp-gimp-.*:
WM_COMMAND: [{"__jsonclass__": ["string", "gimp"]}]
WM_COMMAND: !string gimp

.*-gimp-2-8-.*:
WM_COMMAND: [{"__jsonclass__": ["string", "gimp-2.8"]}]
WM_COMMAND: !string gimp-2.8

# Predefine ghosts. Useful for configuring windows created by the
# applications started as components.
ghosts:
- IG_APP_ID: {"__jsonclass__": ["string", "panelterm"]}
WM_CLASS: [{"__jsonclass__": ["string", "xterm"]}, {"__jsonclass__": ["string", "XTerm"]}, {"__jsonclass__": ["string", ""]}]
WM_NAME: {"__jsonclass__": ["string", "panelterm"]}
- IG_APP_ID: !string panelterm
WM_CLASS:
- !string xterm
- !string XTerm
- !string ""
WM_NAME: !string panelterm
IG_COORDS: [0.0, "eq://apply($.root.get_geometry).height/apply($.root.get_geometry).width", 1.0, 0.0576171875]
IG_SIZE: ["eq://apply($.root.get_geometry).width", "eq://int(apply($.root.get_geometry).width*0.0576171875)"]
IG_LAYER: IG_LAYER_OVERLAY
76 changes: 19 additions & 57 deletions glass-config-init/glass_config_init/widgets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,49 @@ widgets:
properties:
IG_COLOR_TRANSFORM: 1
IG_COORDS: [0.01, "eq://0.2 * apply($.root.get_geometry).height/apply($.root.get_geometry).width", 0.05, 0.05]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/circle-plus.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/circle-plus.fl.svg
action: zoom_to_fewer_windows

Zoom out:
properties:
IG_COLOR_TRANSFORM: 1
IG_COORDS: [0.01, "eq://0.3 * apply($.root.get_geometry).height/apply($.root.get_geometry).width", 0.05, 0.05]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/circle-minus.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/circle-minus.fl.svg
action: zoom_to_more_windows

Zoom home:
properties:
IG_COLOR_TRANSFORM: 1
IG_COORDS: [0.01, "eq://0.4 * apply($.root.get_geometry).height/apply($.root.get_geometry).width", 0.05, 0.05]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/home.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/home.fl.svg
action: zoom_home

Group:
properties:
IG_COLOR_TRANSFORM: 1
IG_COORDS: [0.01, "eq://0.5 * apply($.root.get_geometry).height/apply($.root.get_geometry).width", 0.05, 0.05]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/group.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/group.fl.svg
action: send_island_create

Tile:
properties:
IG_COLOR_TRANSFORM: 1
IG_COORDS: [0.01, "eq://0.6 * apply($.root.get_geometry).height/apply($.root.get_geometry).width", 0.05, 0.05]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/th-list.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/th-list.fl.svg
action: tile_visible_to_1_1

Exit:
properties:
IG_COLOR_TRANSFORM: 1
IG_COORDS: [0.01, "eq://0.7 * apply($.root.get_geometry).height/apply($.root.get_geometry).width", 0.05, 0.05]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/eject.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/eject.fl.svg
action: send_exit

Debug:
properties:
IG_COLOR_TRANSFORM: 1
IG_COORDS: [0.01, "eq://0.8 * apply($.root.get_geometry).height/apply($.root.get_geometry).width", 0.05, 0.05]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/bug.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/bug.fl.svg
action: send_debug

window-decorations:
Expand All @@ -70,12 +56,8 @@ window-decorations:
IG_COORDS: [1.0, -1.0, 0.0, 0.0,
0.01, 0.02, 0.03, 0.03]
IG_COORD_TYPES: ["IG_COORD_PARENT_BASE", "IG_COORD_SCREEN_X"]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/pause.fl.svg
IG_CONTENT_ALT:
type: IG_SVG
value: resource://glass_widgets/icons/play.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/pause.fl.svg
IG_CONTENT_ALT: !IG_SVG resource://glass_widgets/icons/play.fl.svg
action: toggle_sleep

Enable/disable ghosts:
Expand All @@ -85,12 +67,8 @@ window-decorations:
IG_COORDS: [1.0, -1.0, 0.0, 0.0,
0.01, 0.055, 0.03, 0.03]
IG_COORD_TYPES: ["IG_COORD_PARENT_BASE", "IG_COORD_SCREEN_X"]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/pin-inactive.fl.svg
IG_CONTENT_ALT:
type: IG_SVG
value: resource://glass_widgets/icons/pin-active.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/pin-inactive.fl.svg
IG_CONTENT_ALT: !IG_SVG resource://glass_widgets/icons/pin-active.fl.svg
action: toggle_ghosts_enabled

Close:
Expand All @@ -99,9 +77,7 @@ window-decorations:
IG_COORDS: [1.0, -1.0, 0.0, 0.0,
0.01, 0.09, 0.03, 0.03]
IG_COORD_TYPES: ["IG_COORD_PARENT_BASE", "IG_COORD_SCREEN_X"]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/times.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/times.fl.svg
action: send_close

Maximize:
Expand All @@ -110,19 +86,15 @@ window-decorations:
IG_COORDS: [1.0, -1.0, 0.0, 0.0,
0.01, 0.125, 0.03, 0.03]
IG_COORD_TYPES: ["IG_COORD_PARENT_BASE", "IG_COORD_SCREEN_X"]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/tv.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/tv.fl.svg
action: zoom_1_1_1

#More:
# properties:
# IG_COORDS: [1.0, -1.0, 0.0, 0.0,
# 0.01, 0.12, 0.02, 0.02]
# IG_COORD_TYPES: ["IG_COORD_PARENT_BASE", "IG_COORD_SCREEN_X"]
# IG_CONTENT:
# type: IG_SVG
# value: resource://glass_widgets/icons/bars.svg
# IG_CONTENT: !IG_SVG resource://glass_widgets/icons/bars.svg
# action: ""

island-decorations:
Expand All @@ -132,40 +104,30 @@ island-decorations:
IG_COORDS: [1.0, -1.0, 0.0, 0.0,
0.01, 0.02, 0.03, 0.03]
IG_COORD_TYPES: ["IG_COORD_PARENT_BASE", "IG_COORD_SCREEN_X"]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/pause.fl.svg
IG_CONTENT_ALT:
type: IG_SVG
value: resource://glass_widgets/icons/play.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/pause.fl.svg
IG_CONTENT_ALT: !IG_SVG resource://glass_widgets/icons/play.fl.svg
action: island_toggle_sleep

Close island:
properties:
IG_COORDS: [1.0, -1.0, 0.0, 0.0,
0.01, 0.055, 0.03, 0.03]
IG_COORD_TYPES: ["IG_COORD_PARENT_BASE", "IG_COORD_SCREEN_X"]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/times.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/times.fl.svg
action: island_delete

Break apart island:
properties:
IG_COORDS: [1.0, -1.0, 0.0, 0.0,
0.01, 0.09, 0.03, 0.03]
IG_COORD_TYPES: ["IG_COORD_PARENT_BASE", "IG_COORD_SCREEN_X"]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/unlink.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/unlink.fl.svg
action: island_ungroup

Change background:
properties:
IG_COORDS: [1.0, -1.0, 0.0, 0.0,
0.01, 0.125, 0.03, 0.03]
IG_COORD_TYPES: ["IG_COORD_PARENT_BASE", "IG_COORD_SCREEN_X"]
IG_CONTENT:
type: IG_SVG
value: resource://glass_widgets/icons/image.fl.svg
IG_CONTENT: !IG_SVG resource://glass_widgets/icons/image.fl.svg
action: "island_change_background"
2 changes: 1 addition & 1 deletion glass-ghosts/glass_ghosts/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, display, **kw):

configpath = os.path.expanduser(os.environ.get("GLASS_GHOSTS_CONFIG", "~/.config/glass/ghosts.yml"))
with open(configpath) as f:
self.config = json.loads(json.dumps(yaml.load(f, Loader=yaml.SafeLoader)), object_hook=InfiniteGlass.fromjson(self.display))
self.config = InfiniteGlass.load_yaml(f, self.display)

self.changes = False
self.windows = {}
Expand Down
2 changes: 1 addition & 1 deletion glass-islands/glass_islands/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, display):

configpath = os.path.expanduser(os.environ.get("GLASS_ISLANDS_CONFIG", "~/.config/glass/islands.yml"))
with open(configpath) as f:
self.config = json.loads(json.dumps(yaml.load(f, Loader=yaml.SafeLoader)), object_hook=InfiniteGlass.fromjson(self.display))
self.config = InfiniteGlass.load_yaml(f, self.display)

self.changes = False
self.islands = {}
Expand Down
33 changes: 33 additions & 0 deletions glass-lib/InfiniteGlass/valueencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import json
from . import keymap
import sakstig
import yaml
import base64

class apply(sakstig.Function):
def call(self, global_qs, local_qs, args):
Expand Down Expand Up @@ -196,3 +198,34 @@ def fromjson(obj):
return tuple(cls[1:])
return obj
return fromjson

class YamlLoader(yaml.SafeLoader):
pass

def _yaml_constructor(loader, tag_suffix, node):
if isinstance(node, yaml.nodes.ScalarNode):
value = loader.construct_scalar(node)
elif isinstance(node, yaml.nodes.MappingNode):
value = loader.construct_mapping(node)
elif isinstance(node, yaml.nodes.SequenceNode):
value = loader.construct_sequence(node)
else:
assert False, "Unknown node type: %s" % node
if tag_suffix.startswith("!array_"):
return array.array(tag_suffix[len("!array_"):], value)
elif tag_suffix == "!string":
return value.encode("utf-8")
elif tag_suffix == "!base64":
return base64.b64decode(value)
elif tag_suffix == "!WINDOW":
return loader.display.create_resource_object("window", value)
elif tag_suffix == "!tuple":
return tuple(value)
return {"type": tag_suffix[1:], "value": value}
YamlLoader.add_multi_constructor("", _yaml_constructor)

def load_yaml(f, display=None):
d = display
class Loader(YamlLoader):
display = d
return yaml.load(f, Loader=Loader)
8 changes: 4 additions & 4 deletions glass-widgets/glass_widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def redraw(display, win):

@InfiniteGlass.profilable
def main(*arg, **kw):
configpath = os.path.expanduser(os.environ.get("GLASS_WIDGET_CONFIG", "~/.config/glass/widgets.yml"))
with open(configpath) as f:
config = yaml.load(f, Loader=yaml.SafeLoader)

with InfiniteGlass.Display() as display:
configpath = os.path.expanduser(os.environ.get("GLASS_WIDGET_CONFIG", "~/.config/glass/widgets.yml"))
with open(configpath) as f:
config = InfiniteGlass.load_yaml(f, display)

for widget_type, widgets in config.items():
for name, widget in widgets.items():
w = display.root.create_window()
Expand Down

0 comments on commit f352e9f

Please sign in to comment.