-1

I would like to get your help with my .py script.

I'm using xml file to store the parser path for the images.

Here's the xml file that I use:

<?xml version="1.0" encoding="utf-8"?>
<window type="dialog">
    <allowoverlay>no</allowoverlay>
<coordinates>
<system>1</system>
<posx>0</posx>
<posy>0</posy>
</coordinates>

<controls>
    <control type="image" id="1">
       <posx>0</posx>
       <posy>0</posy>
       <width>1280</width>
       <height>720</height>
       <texture>background-defeat.png</texture>
       <animation effect="fade" start="0" end="100" time="6500">WindowOpen</animation>
    </control>

     <control type="image" id="2">
      <description>Image 2</description>
      <posx>307</posx>
      <posy>7</posy>
      <width>154</width>
      <height>95</height>
      <visible>true</visible>
      <texture>Image 2.png</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>    

     <control type="image" id="3">
      <description>Image 3</description>
      <posx>460</posx>
      <posy>7</posy>
      <width>188</width>
      <height>95</height>
      <visible>true</visible>
      <texture>Image 3.jpg</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>

    <control type="image" id="4">
      <description>Image 4</description>
      <posx>648.5</posx>
      <posy>7</posy>
      <width>165</width>
      <height>95</height>
      <visible>true</visible>
      <texture>recorded_blue.jpg</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>

    <control type="image" id="5">
      <description>Image 5</description>
      <posx>813.5</posx>
      <posy>7</posy>
      <width>149</width>
      <height>95</height>
      <visible>true</visible>
      <texture>Image 5.jpg</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>
</controls>
</window>

Here is the .py script:

import xbmc 
import xbmcgui
import os

#get actioncodes from keymap.xml
ACTION_MOVE_LEFT = 1
ACTION_MOVE_RIGHT = 2
ACTION_MOVE_UP = 3
ACTION_MOVE_DOWN = 4
ACTION_PREVIOUS_MENU = 10
ACTION_BACKSPACE = 110



class MyClass(xbmcgui.WindowXML):
  def onAction(self, action):

    if action == ACTION_BACKSPACE:
         self.close()

    if action == ACTION_PREVIOUS_MENU:
         self.close()

    if action == ACTION_MOVE_LEFT:

         if os.path.exists('Q:\\resources\skins\Default\media\image 4.jpg') == True:
             self.strAction = xbmcgui.ControlLabel(300, 200, 600, 200, '', 'font14', '0xFF00FF00')
             self.addControl(self.strAction)
             self.strAction.setLabel('Image is exist')

Here's the xml parser:

import xml.etree.ElementTree as ET

filename = 'script-tvguide-mainmenu.xml'
tree = ET.parse(filename)
root = tree.getroot()
controls = root.find('controls')

for control in controls.findall('control'):
    #how do you create the if statement to check for the image through on xml if they are exist?
    # Here are the image filenames, focus and nofocus.
    focus = control.find('texturefocus').text
    nofocus = control.find('texturenofocus').text
    print('texturefocus={0}, texturenofocus={1}'.format(focus, nofocus))

I tried with:

if action == ACTION_MOVE_LEFT:
         filename = 'script-tvguide-mainmenu.xml'
         tree = ET.parse(filename)
         root = tree.getroot()
         controls = root.find('controls')

         for control in controls.findall('control'):
             texture = control.find('texture').text

             if texture == 'tvguide_yellow.png':
                  self.strAction = xbmcgui.ControlLabel(300, 200, 600, 200, '', 'font14', '0xFF00FF00')
                  self.addControl(self.strAction)
                  self.strAction.setLabel('Image is exisit')

I want to know how to do you use to write in python for xml parser include if statement that if I have the image called "Image 2.jpg" which is return as true then I could do something?

Chris Johnson
  • 119
  • 11
  • Are you asking us to recommend an XML parser? There are [a few](http://docs.python.org/2/library/xml.html) in the stdlib, and popular third-party libraries like [`lxml`](http://lxml.de). Stack Overflow is not a good site for recommendations on which library to use, or similar subjective questions—but if you pick any of those, try to write the code yourself, and get stuck, it's a great place to get help to get unstuck. – abarnert Jan 09 '14 at 20:16
  • As an aside, why is this tagged both python-2.7 and python-3.x? Are you trying to write code that works with both versions? Or using one version but open to switching if it makes your life easier? Or just not sure which one you're using? – abarnert Jan 09 '14 at 20:17
  • @abarnert yes I do. I have XML parser, but I just want to know how do you write to make the if statement include the xml parser in python that if I have the image called `image 4.jpg` then i can change the image using the xml id? please see the xml parser in my update post. – Chris Johnson Jan 09 '14 at 20:21
  • @abarnert Sorry, I'm using xmbc so i don't know which one of them (python-2.7 or python-3.x) is the one that work for xbmc? – Chris Johnson Jan 09 '14 at 20:22
  • Meanwhile, it looks like [XBMC uses Python 2.6](http://wiki.xbmc.org/index.php?title=HOW-TO_write_plugins_for_XBMC), so both of your tags are wrong. More generally, if you have no idea which version you're using, just use the `python` tag and don't try to guess and add tags that may be wrong. – abarnert Jan 09 '14 at 20:25
  • OK, in your edited version, it looks like you've written an XML parser that just prints out a bunch of values. You need to change it to (a) find the values you want rather than some different ones, and (b) store them so that your other code can use them. – abarnert Jan 09 '14 at 20:26
  • yeah, but you can see on my edited version that I have written an XML parser. I want to know how do you use to write to make the if statement to check if the image is exist? – Chris Johnson Jan 09 '14 at 20:31
  • because I tried with `if os.path.exists('Q:\\resources\skins\Default\media\image 4.jpg') == True:` it did not work. Any idea? – Chris Johnson Jan 09 '14 at 20:32
  • I'm completely lost at what you're trying to accomplish here. First, there is no image named `Image 4.jpg` in your XML file—image #4 is `recorded_blue.jpg`. And none of the images have a `texturefocus` or `texturenonfocus`; they all have instead a `texture`. Second, where did you get that path to check? I'm sure XBMC provides a path for you to use in some way, instead of expecting you to hard-code one. – abarnert Jan 09 '14 at 20:36
  • Anyway, there's one obvious problem with your code: You're using backslashes in a non-raw string literal, without escaping them. So, that `\r` isn't two characters, a backslash followed by an `r`, it's a single character, a carriage return. You can solve this by doubling each backslashes, or by using forward slashes instead (which work fine on Windows), or, best of all, by using a raw string literal—`r'Q:\\resources\skins\…jpg'`. – abarnert Jan 09 '14 at 20:37
  • Also, you shouldn't check `if os.path.exists(…) == True:`. Just check `if os.path.exists(…):` Many things in Python are defined to return "some true value", which works fine in `if`, but which isn't necessarily equal to the singleton `True` value. – abarnert Jan 09 '14 at 20:38
  • @abarnert thank you for post the source. I have tried with the source the one you post, but it did not work when I press the left button on the keyboard. Please see the update on my first post. I want to check on xbmc that if the image 2.png is exist. How do you check on xbmc if i have the image called `image 2.png` as return to true? – Chris Johnson Jan 09 '14 at 21:11
  • @abarnert I only want to check the `image 2.png` in the `def onAction(self, action):` because I'm using the keyboard control. How do you check the `image 2.png` in the keyboard control that if they are return as true or false? and how would you change the image with the id if it return as true? e.g. I have `image 2.png` which it have id `2` so i can change it from `image 2.png` to `image 3.png` in the id `2`. – Chris Johnson Jan 09 '14 at 21:13

1 Answers1

2

Here's the simplest way to adapt your code to find the control whose texture is Image 2.jpg:

First, you're looking for an element named texturefocus. But in your XML sample, there is no such element—and even if there were, the one you're looking for is named texture. So obviously you need to fix that:

texture = control.find('texture').text

Second, you're looking for an image Image 2.jpg, but there is no such image in your XML, so you're not going to find it. There is an Image 2.png, but that's not the same thing. So, presumably you need to fix that as well.

And now, the if statement is trivial:

if texture == 'Image 2.png':

The question is, what do you want to do when you find it? Just printing out a string isn't going to help the rest of your code use that value.

Let's say that what you want to do is to write a function that returns the description if there's an image whose texture is Image 2.png, or returns None otherwise. Then:

def find_image2(filename):
    tree = ET.parse(filename)
    root = tree.getroot()
    controls = root.find('controls')

    for control in controls.findall('control'):
        texture = control.find('texture')
        if texture and texture.text == 'Image 2.png':
            return control.find('description').text
abarnert
  • 354,177
  • 51
  • 601
  • 671