Insert a string into the Language Builder and return the root widget (if defined) of the kv string. :Parameters: `rulesonly`: bool, defaults to False If True, the Builder will raise an exception if you have a root widget inside the definit
(self, string, **kwargs)
| 337 | Factory.unregister_from_filename(filename) |
| 338 | |
| 339 | def load_string(self, string, **kwargs): |
| 340 | '''Insert a string into the Language Builder and return the root widget |
| 341 | (if defined) of the kv string. |
| 342 | |
| 343 | :Parameters: |
| 344 | `rulesonly`: bool, defaults to False |
| 345 | If True, the Builder will raise an exception if you have a root |
| 346 | widget inside the definition. |
| 347 | `filename`: str, defaults to None |
| 348 | If specified, the filename used to index the kv rules. |
| 349 | |
| 350 | The filename parameter can be used to unload kv strings in the same way |
| 351 | as you unload kv files. This can be achieved using pseudo file names |
| 352 | e.g.:: |
| 353 | |
| 354 | Build.load_string(""" |
| 355 | <MyRule>: |
| 356 | Label: |
| 357 | text="Hello" |
| 358 | """, filename="myrule.kv") |
| 359 | |
| 360 | can be unloaded via:: |
| 361 | |
| 362 | Build.unload_file("myrule.kv") |
| 363 | |
| 364 | ''' |
| 365 | |
| 366 | kwargs.setdefault('rulesonly', False) |
| 367 | self._current_filename = fn = kwargs.get('filename', None) |
| 368 | |
| 369 | # put a warning if a file is loaded multiple times |
| 370 | if fn in self.files: |
| 371 | Logger.warning( |
| 372 | 'Lang: The file {} is loaded multiples times, ' |
| 373 | 'you might have unwanted behaviors.'.format(fn)) |
| 374 | |
| 375 | try: |
| 376 | # parse the string |
| 377 | parser = Parser(content=string, filename=fn) |
| 378 | |
| 379 | # merge rules with our rules |
| 380 | self.rules.extend(parser.rules) |
| 381 | self._clear_matchcache() |
| 382 | |
| 383 | # add the template found by the parser into ours |
| 384 | for name, cls, template in parser.templates: |
| 385 | self.templates[name] = (cls, template, fn) |
| 386 | Factory.register(name, |
| 387 | cls=partial(self.template, name), |
| 388 | is_template=True, warn=True) |
| 389 | |
| 390 | # register all the dynamic classes |
| 391 | for name, baseclasses in parser.dynamic_classes.items(): |
| 392 | Factory.register(name, baseclasses=baseclasses, filename=fn, |
| 393 | warn=True) |
| 394 | |
| 395 | # create root object is exist |
| 396 | if kwargs['rulesonly'] and parser.root: |