~sbaldassin/autopilot/fix_mir

« back to all changes in this revision

Viewing changes to autopilot/introspection/_xpathselect.py

Upstream custom helpers in lp:ubuntu-system-tests to autopilot.

Approved by Richard Huddie, platform-qa-bot, Santiago Baldassin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
Queries are executed in the autopilot.introspection.backends module.
47
47
 
48
48
"""
 
49
from pathlib import Path
49
50
import re
50
51
 
51
52
from autopilot.utilities import compatible_repr
390
391
        )
391
392
 
392
393
 
393
 
def get_classname_from_path(object_path):
394
 
    """Given an object path, return the class name component."""
 
394
def _get_node(object_path, index):
395
395
    # TODO: Find places where paths are strings, and convert them to
396
396
    # bytestrings. Figure out what to do with the whole string vs. bytestring
397
397
    # mess.
398
 
    is_string = isinstance(object_path, str)
399
 
    if is_string:
400
 
        object_path = object_path.encode('utf-8')
401
 
    class_name = object_path.split(b"/")[-1]
402
 
    if is_string:
403
 
        class_name = class_name.decode('utf-8')
404
 
    return class_name
 
398
    try:
 
399
        return Path(object_path).parts[index]
 
400
    except TypeError:
 
401
        if not isinstance(object_path, bytes):
 
402
            raise TypeError(
 
403
                'Object path needs to be a string literal or a bytes literal'
 
404
            )
 
405
        return object_path.split(b"/")[index]
 
406
 
 
407
 
 
408
def get_classname_from_path(object_path):
 
409
    """Given an object path, return the class name component."""
 
410
    return _get_node(object_path, -1)
 
411
 
 
412
 
 
413
def get_path_root(object_path):
 
414
    """Return the name of the root node of specified path."""
 
415
    return _get_node(object_path, 1)