def __init__(self, root): self.root = root config_path = os.path.join(self.root, 'etc', 'default', 'keyboard') if os.path.exists(config_path): self.setting = from_config_file(config_path) else: self.setting = KeyboardSetting(layout='us')
def load_autoinstall_data(self, data): if data is None: return setting = KeyboardSetting(**data) if self.model.setting != setting: self.needs_set_keyboard = True self.model.setting = setting
def for_ui(setting): """ Attempt to guess a setting the user chose which resulted in the current config. Basically the inverse of latinizable(). """ if ',' in setting.layout: layout1, layout2 = setting.layout.split(',', 1) else: layout1, layout2 = setting.layout, '' if ',' in setting.variant: variant1, variant2 = setting.variant.split(',', 1) else: variant1, variant2 = setting.variant, '' if setting.layout == 'lt,lt': layout = layout1 variant = variant1 elif setting.layout in ('rs,rs', 'us,rs', 'jp,jp', 'us,jp'): layout = layout2 variant = variant2 elif layout1 == 'us' and layout2 in standard_non_latin_layouts: layout = layout2 variant = variant2 elif ',' in setting.layout: # Something unrecognized layout = 'us' variant = '' else: return setting return KeyboardSetting( layout=layout, variant=variant, toggle=setting.toggle)
async def POST(self, data: KeyboardSetting): new = latinizable(data.layout, data.variant) if new is not None: data = KeyboardSetting(new[0], new[1], data.toggle) if data != self.model.setting: self.model.setting = data await self.set_keyboard() self.configured()
async def t(): os.environ['SUBIQUITY_REPLAY_TIMESCALE'] = '100' with tempfile.TemporaryDirectory() as tmpdir: new_setting = KeyboardSetting('fr', 'azerty') await set_keyboard(tmpdir, new_setting, True) read_setting = from_config_file( os.path.join(tmpdir, 'etc', 'default', 'keyboard')) self.assertEqual(new_setting, read_setting)
def done(self, result): layout = self.form.layout.widget.value variant = '' if self.form.variant.widget.value is not None: variant = self.form.variant.widget.value setting = KeyboardSetting(layout=layout, variant=variant) new_setting = latinizable(setting) if new_setting != setting: self.show_stretchy_overlay(ToggleQuestion(self, new_setting)) return self.really_done(setting)
async def t(): os.environ['SUBIQUITY_REPLAY_TIMESCALE'] = '100' with tempfile.TemporaryDirectory() as tmpdir: new_setting = KeyboardSetting('fr', 'azerty') model = KeyboardModel(tmpdir) model.setting = new_setting c = object.__new__(KeyboardController) c.opts = opts c.model = model await c.set_keyboard() read_setting = KeyboardModel(tmpdir).setting self.assertEqual(new_setting, read_setting)
def from_config_file(config_file): with open(config_file) as fp: content = fp.read() def optval(opt, default): match = re.search(r'(?m)^\s*%s=(.*)$' % (opt,), content) if match: r = match.group(1).strip('"') if r != '': return r return default XKBLAYOUT = optval("XKBLAYOUT", "us") XKBVARIANT = optval("XKBVARIANT", "") XKBOPTIONS = optval("XKBOPTIONS", "") toggle = None for option in XKBOPTIONS.split(','): if option.startswith('grp:'): toggle = option[4:] return KeyboardSetting(layout=XKBLAYOUT, variant=XKBVARIANT, toggle=toggle)
def latinizable(setting): """ If this setting does not allow the typing of latin characters, return a setting that can be switched to one that can. """ if setting.layout == 'rs': if setting.variant.startswith('latin'): return setting else: if setting.variant == 'yz': new_variant = 'latinyz' elif setting.variant == 'alternatequotes': new_variant = 'latinalternatequotes' else: new_variant = 'latin' return KeyboardSetting(layout='rs,rs', variant=(new_variant + ',' + setting.variant)) elif setting.layout == 'jp': if setting.variant in ('106', 'common', 'OADG109A', 'nicola_f_bs', ''): return setting else: return KeyboardSetting(layout='jp,jp', variant=',' + setting.variant) elif setting.layout == 'lt': if setting.variant == 'us': return KeyboardSetting(layout='lt,lt', variant='us,') else: return KeyboardSetting(layout='lt,lt', variant=setting.variant + ',us') elif setting.layout == 'me': if setting.variant == 'basic' or setting.variant.startswith('latin'): return setting else: return KeyboardSetting(layout='me,me', variant=setting.variant + ',us') elif setting.layout in standard_non_latin_layouts: return KeyboardSetting(layout='us,' + setting.layout, variant=',' + setting.variant) else: return setting
async def run_answers(self): if 'layout' in self.answers: layout = self.answers['layout'] variant = self.answers.get('variant', '') await self.apply(KeyboardSetting(layout=layout, variant=variant)) self.done()
def done(self, result): data = result.as_data() layout = data['layout'] variant = data.get('variant', layout.variants[0]) setting = KeyboardSetting(layout=layout.code, variant=variant.code) self.controller.app.aio_loop.create_task(self._check_toggle(setting))
def run_answers(self): if 'layout' in self.answers: layout = self.answers['layout'] variant = self.answers.get('variant', '') self.done(KeyboardSetting(layout=layout, variant=variant), True)