+def match_asterisk(pattern, data):
+ """Match data against a pattern
+
+ The difference to fnmatch is that this function only accepts patterns with a single
+ asterisk and that it returns a "closeness" number, which is larger the better the match.
+ Zero indicates no match at all.
+ """
+ if "*" not in pattern:
+ return 1 if pattern == data else 0
+ parts = pattern.split("*")
+ if len(parts) > 2:
+ raise ValueError("Only patterns with a single asterisk are supported, %s is invalid" % pattern)
+ if not data.startswith(parts[0]):
+ return 0
+ if not data.endswith(parts[1]):
+ return 0
+ matched = len(pattern)
+ total = len(data) + 1
+ return matched * 1. / total
+
+
+def update_profiles_edid(profiles, config):
+ edid_map = {}
+ for c in config:
+ if config[c].edid is not None:
+ edid_map[config[c].edid] = c
+
+ for p in profiles:
+ profile_config = profiles[p]["config"]
+
+ for edid in edid_map:
+ for c in profile_config.keys():
+ if profile_config[c].edid != edid or c == edid_map[edid]:
+ continue
+
+ print("%s: renaming display %s to %s" % (p, c, edid_map[edid]))
+
+ tmp_disp = profile_config[c]
+
+ if edid_map[edid] in profile_config:
+ # Swap the two entries
+ profile_config[c] = profile_config[edid_map[edid]]
+ profile_config[c].output = c
+ else:
+ # Object is reassigned to another key, drop this one
+ del profile_config[c]
+
+ profile_config[edid_map[edid]] = tmp_disp
+ profile_config[edid_map[edid]].output = edid_map[edid]
+
+