Ejemplo n.º 1
0
def get_cpu_info(target):
    """Get information about the CPU.

    Returns the value from the specified target field.

    :param string target: The target attribute to the value of.
    :returns: The value of the specified CPU attribute.
    :rtype: string
    :raises: raspy.invalid_operation_exception.InvalidOperationException if
    the specified target is invalid (unknown).
    """
    global __cpuInfo
    if __cpuInfo is None:
        __cpuInfo = []
        result = exec_utils.execute_command("cat /proc/cpuinfo")
        if result is not None:
            # noinspection PyTypeChecker
            for key, val in enumerate(result):
                line = val
                parts = line.split(":")
                if len(parts) >= 2:
                    tpart0 = string_utils.trim(parts[0])
                    tpart1 = string_utils.trim(parts[1])
                    if (not string_utils.is_null_or_empty(tpart0)
                            and not string_utils.is_null_or_empty(tpart1)):
                        # noinspection PyTypeChecker
                        __cpuInfo[tpart0] = tpart1

    if target in __cpuInfo:
        # noinspection PyTypeChecker
        return __cpuInfo[target]

    raise InvalidOperationException("Invalid target: " + target)
Ejemplo n.º 2
0
def get_clock_frequency(target):
    """Get the clock frequency for the specified target.

    :param string target: The target clock to get the frequency of.
    :returns: The clock frequency, if successful; Otherwise, -1.
    :rtype: int, long
    """
    if string_utils.is_null_or_empty(target):
        return -1

    target = string_utils.trim(target)
    val = -1
    cmd = "/opt/vc/bin/vcgencmd measure_clock " + target
    result = exec_utils.execute_command(cmd)
    if result is not None:
        # noinspection PyTypeChecker
        for i, line in enumerate(result):
            parts = line.split("=", 2)
            try:
                temp = float(string_utils.trim(parts[1]))
            except ValueError:
                temp = -1

            if temp != -1:
                val = temp
                break
    return val
Ejemplo n.º 3
0
    def __str__(self):
        """Return the string representation of this class instance.

        :returns: The string representation of this class.
        :rtype: str
        """
        base = Component.component_name.fget()
        if not string_utils.is_null_or_empty(base):
            return base
        return self.__class__.__name__
Ejemplo n.º 4
0
def get_scale_postfix(scale):
    """Get the scale postfix.

    :param int scale: The scale to get the postfix for.
    :returns: The scale postfix or an empty string if the specified scale is
    invalid or unknown.
    :rtype: str
    """
    name = get_scale_name(scale)
    if string_utils.is_null_or_empty(name):
        return string_utils.EMPTY
    return name[0]
Ejemplo n.º 5
0
def has_read_elf_tag(tag):
    """Determine if a specified tag exists from the ELF.

    This method will determine if a specified tag exists from the ELF info in
    the '/proc/self/exe' program (this method is used to help determine the
    HARD-FLOAT / SOFT-FLOAT ABI of the system).

    :param string tag: The tag to check for.
    :returns: True if contains the specified ELF tag.
    :rtype: boolean
    """
    tag_val = get_read_elf_tag(tag)
    return not string_utils.is_null_or_empty(tag_val)
Ejemplo n.º 6
0
    def __init__(self, file_path):
        """Initialize a new instance of raspy.io.file_info.FileInfo.

        Initializes a new instance of the FileInfo class with the
        fully-qualified or relative name of the file.

        :param str file_path: The fully-qualified name of the of the file,
        or the relative file name.
        :raises: raspy.argument_null_exception.ArgumentNullException if
        file_path is null or an empty string.
        """
        if string_utils.is_null_or_empty(file_path):
            raise ArgumentNullException(
                "'file_path' param cannot be null or " + "undefined.")

        self.__name = os.path.basename(file_path)
        self.__originalPath = file_path
        self.__fullPath = os.path.normpath(self.__originalPath)
Ejemplo n.º 7
0
def get_bash_version_info():
    """Get the BaSH version info.

    This method is used to help determine the HARD_FLOAT / SOFT_FLOAT ABI of
    the system.

    :returns: The BaSH version info.
    :rtype: string
    """
    ver = string_utils.EMPTY
    try:
        result = exec_utils.execute_command("bash --version")
        # noinspection PyTypeChecker
        for i, line in enumerate(result):
            if not string_utils.is_null_or_empty(line):
                ver = line  # Return only the first line.
                break
    except Exception:
        pass

    return ver
Ejemplo n.º 8
0
def get_memory():
    """Get the system memory info.

    :returns: The memory info.
    :rtype: array
    """
    values = []
    result = exec_utils.execute_command("free -b")
    if result is not None:
        val = 0
        # noinspection PyTypeChecker
        for i, line in enumerate(result):
            if string_utils.starts_with(line, "Mem:"):
                parts = line.split()
                for j, part in enumerate(parts):
                    line_part = string_utils.trim(part)
                    if (not string_utils.is_null_or_empty(line_part)
                            and line.upper() == "Mem:".upper()):
                        values.append(float(val))

    return values
Ejemplo n.º 9
0
def get_os_firmware_date():
    """Get the OS firmware date.

    :returns: The OS firmware date.
    :rtype: string
    :raises: raspy.invalid_operation_exception.InvalidOperationException if
    an unexpected response is received.
    """
    results = exec_utils.execute_command("/opt/vc/bin/vcgencmd version")
    val = string_utils.EMPTY
    if results is not None:
        # noinspection PyTypeChecker
        for key, value in enumerate(results):
            # We are intentionally only getting the first line.
            val = value
            break

    if not string_utils.is_null_or_empty(val):
        return val

    raise InvalidOperationException("Invalid command or response.")
Ejemplo n.º 10
0
    def set_display_to_string(self, string, dots=False, pos=0):
        """Set the display to the specified string.

        :param string string: The string to set the display to.
        :param bool dots: Set True to turn on dots.
        :param byte, int pos: The character position to start the string at.
        """
        if string_utils.is_null_or_empty(string):
            self.clear_display()
            return

        global C_MAP
        length = len(string)
        for i in range(0, self.__displays - 1):
            if i < length:
                l_pos = i + pos
                l_data = C_MAP[string[i]]
                l_dot = (dots & (1 << (self.__displays - i - 1))) != 0
                self.send_char(l_pos, l_data, l_dot)
            else:
                break
Ejemplo n.º 11
0
def get_os_firmware_build():
    """Get the OS firmware build.

    :returns: the OS firmware build.
    :rtype: string
    :raises: raspy.invalid_operation_exception.InvalidOperationException if
    an unexpected response is received.
    """
    results = exec_utils.execute_command("/opt/vc/bin/vcgencmd version")
    val = string_utils.EMPTY
    if results is not None:
        # noinspection PyTypeChecker
        for key, val in enumerate(results):
            line = val
            if string_utils.starts_with(line, "version "):
                val = line
                break

    if not string_utils.is_null_or_empty(val):
        return val[8:]

    raise InvalidOperationException("Invalid command or response")
Ejemplo n.º 12
0
def create_output_pin(pin, name):
    """Factory method for creating a PiFace digital output pin.

    :param raspy.io.pi_face_pins.PiFacePin pin: The pin to create an output
    for.
    :param str name: The name of the pin. If not specified, the default
    hardware name of the pin will be used instead.
    :returns: A PiFace digital output.
    :rtype: raspy.io.pi_face_gpio_digital.PiFaceGpioDigital
    :raises: raspy.io.io_exception.IOException if unable to communicate with
    the SPI bus.
    """
    if string_utils.is_null_or_empty(name):
        name = pin.name

    val = pin.value
    speed = PiFaceGpioDigital.BUS_SPEED
    pfgd = PiFaceGpioDigital(pin, pin_state.LOW, val, speed)
    pfgd.pin_name = name
    pfgd.mode = pin_mode.OUT
    pfgd.pull_resistance = pin_pull_resistance.Off
    return pfgd
Ejemplo n.º 13
0
    def __init__(self, pn, initial_val, name):
        """Initialize a new instance of the raspy.io.PiFaceGPIO class.

        :param raspy.io.pi_face_pin.PiFacePin pn: The underlying PiFace pin.
        :param int initial_val: The initial pin value (state).
        :param string name: The pin name.
        :raises: raspy.argument_null_exception.ArgumentNullException if the
        specified pin is NonePin.
        :raises: raspy.illegal_argument_exception.IllegalArgumentException if
        the specified pin is not of type raspy.io.pi_face_pins.PiFacePin.
        """
        gpio.Gpio.__init__(self, pn, pin_mode.IN, initial_val)
        self.__pwm = 0
        self.__pwmRange = 0

        if pn is None:
            raise ArgumentNullException("pn param cannot be NonePin.")

        if not isinstance(pn, pi_face_pins.PiFacePin):
            err_msg = "pn param must be a PiFacePins member."
            raise IllegalArgumentException(err_msg)

        self.pin_name = name
        if string_utils.is_null_or_empty(self.pin_name):
            self.pin_name = pn.name

        if (pn == pi_face_pins.Input00 or pn == pi_face_pins.Input01
                or pn == pi_face_pins.Input02 or pn == pi_face_pins.Input03
                or pn == pi_face_pins.Input04 or pn == pi_face_pins.Input05
                or pn == pi_face_pins.Input06 or pn == pi_face_pins.Input07):
            self.mode = pin_mode.IN
        elif (pn == pi_face_pins.Output00 or pn == pi_face_pins.Output01
              or pn == pi_face_pins.Output02 or pn == pi_face_pins.Output03
              or pn == pi_face_pins.Output04 or pn == pi_face_pins.Output05
              or pn == pi_face_pins.Output06 or pn == pi_face_pins.Output07):
            self.mode = pin_mode.OUT
Ejemplo n.º 14
0
def execute_command(command):
    """Execute the specified command.

    Executes the specified command and returns the output.

    :param command: The command to execute.
    :type command: str
    :returns: A string array containing each line of the output.
    :rtype: list
    """
    if string_utils.is_null_or_empty(command):
        return []

    args = string_utils.EMPTY
    cmd_line = command.split(" ")
    if len(cmd_line) > 1:
        command = cmd_line[0]
        for i in range(1, len(cmd_line)):
            args += cmd_line[i] + " "

        if string_utils.ends_with(args, " "):
            args = args[0:len(args) - 1]

    result = []
    cmd = [command] + args.split(" ")
    cmd_spawn = Popen(cmd, stdout=PIPE, stderr=PIPE)
    output, err = cmd_spawn.communicate()
    if cmd_spawn.returncode == 0:
        for line in output.strip().split("\n"):
            result.append(line)

    if err:
        # TODO should we do anything with this? # pylint: disable=fixme
        pass

    return result
Ejemplo n.º 15
0
def test_is_null_or_empty():
    """Test is_null_or_empty method."""
    assert string_utils.is_null_or_empty(None)
    assert string_utils.is_null_or_empty(string_utils.EMPTY)