Coder Social home page Coder Social logo

Comments (5)

birkanozer avatar birkanozer commented on June 24, 2024 1

Thanks for the answer. Here is a working example based on your description:

import gphoto2 as gp

context = gp.Context()
camera = gp.Camera()
camera.init(context)
config_tree = camera.get_config(context)
print('=======')

total_child = config_tree.count_children()
for i in range(total_child):
    child = config_tree.get_child(i)
    text_child = '# ' + child.get_label() + ' ' + child.get_name()
    print(text_child)

    for a in range(child.count_children()):
        grandchild = child.get_child(a)
        text_grandchild = '    * ' + grandchild.get_label() + ' -- ' + grandchild.get_name()
        print(text_grandchild)

        try:
            text_grandchild_value = '        Setted: ' + grandchild.get_value()
            print(text_grandchild_value)
            print('        Possibilities:')
            for k in range(grandchild.count_choices()):
                choice = grandchild.get_choice(k)
                text_choice = '         - ' + choice
                print(text_choice)
        except:
            pass
        print()
    print()

camera.exit(context)

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

The object returned by camera.get_config is a 'widget' (the name libgphoto2 uses) that is the root of the config tree structure. It has methods such as config.count_children to get the number of child widgets and config.get_child to get a particular child. The widget layout varies with camera type, but is unlikely to be a simple list or dict, unless you use 'compound' keys such as settings.autopoweroff for the autopoweroff child of the settings node.

The 'leaf' nodes on the tree have methods like config.get_value to get the actual camera settings.

PS sorry about the earlier (deleted) reply - I hadn't read your question properly.

from python-gphoto2.

jim-easterbrook avatar jim-easterbrook commented on June 24, 2024

Never, ever, do this:

except:
    pass

Remember that SyntaxError is an exception which you are now ignoring. That makes fixing errors in your code more difficult. Be as specific as you can about the exceptions you want to catch and let others (the unexpected ones) propagate as normal.

from python-gphoto2.

birkanozer avatar birkanozer commented on June 24, 2024

Thanks for the advice ;)

from python-gphoto2.

M4lF3s avatar M4lF3s commented on June 24, 2024

Hi πŸ‘‹

Sorry I just stumbled across this Thread and wanted to chip in my solution, since I wanted to do a similar thing (I think).
Now I know that this is by far not a perfect solution, since as Jim already mentioned the Structure of the Configuration Tree may very well vary from Camera to Camera. In my particular case I am using a Canon EOS M6 Mark II, but I thought maybe this is helpful for others too.

Now if youΒ΄re only interested in the actual Configuration Parameters and their values, you donΒ΄t really need the Information about the Hierarchy in between the Nodes. Only the Leafs and their Values.

So for that you can parse the Config into a nice little Dictionary like so:

config_leafs = {config_tree.get_child(i).get_child(a).get_name(): config_tree.get_child(i).get_child(a).get_value() for i in range(config_tree.count_children()) for a in range(config_tree.get_child(i).count_children())}

which would result in a dict like this:

{
  'syncdatetimeutc': 0, 
  'syncdatetime': 0, 
  'uilock': 2,
  ...
}

But if you actually want to preserve the Hierarchy Information I came up with this monstrosity:

config = {
  child.get_name(): dict(
    label=child.get_label(),
    **{grandchild.get_name(): {
      'label': grandchild.get_label(),
      'set_value': grandchild.get_value(),
      'possible_values': grandchild.get_type()==gp.GP_WIDGET_RADIO or grandchild.get_type()==gp.GP_WIDGET_MENU and [grandchild.get_choice(k) for k in range(grandchild.count_choices())] or []
    } for grandchild in [child.get_child(a) for a in range(child.count_children())]}
  ) for child in [config_tree.get_child(i) for i in range(config_tree.count_children())]
}

This actually gives you a dict like this:

{
  <child_name>: {
    'label': <child_label>,
    <grandchild_name>: {
      'label': <grandchild_label>,
      'set_value': <grandchild_value>,
      'possible_values': [<grandchild_choice(1)>, <grandchild_choice(2)>, ...]
    },
    ...
  },
  ...
}

The Problem with the 'possible_values' is that they only exist in some 'grandchild Nodes'. I guess that was the reason for the except: pass part. But the Documentation of libgphoto2 states that count_choices() and get_choice() should be avaibable for Camera Widgets of type GP_WIDGET_RADIO and GP_WIDGET_MENU. Hence the wild Logic Operations there πŸ˜… (I just learned that this technique is actually called "short circuiting" πŸ‘ )

Now like I said. This is by no means a perfect solution. And since I am really neither a Expert in Python nor in the underlying C I canΒ΄t actually decide if I find this code really cool or totally unmaintainable πŸ˜„ (maybe both) but I figured since I already took the time I would just share the results in case it helps someone else too πŸ™‚

from python-gphoto2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.