silver2row

glob, pathlib, and errno

Jul 5th, 2020
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from glob import glob
  2. from pathlib import Path
  3. import errno
  4.  
  5. def pinmux_path( name ):
  6.     paths = glob( '/sys/bus/platform/drivers/bone-pinmux-helper/*%s*/state' % name )
  7.     if len(paths) == 0:
  8.         raise KeyError( 'Pinmux node not found: ' + name )
  9.     if len(paths) > 1:
  10.         raise KeyError( 'Pinmux node ambiguous: ' + name )
  11.     return Path( paths[0] )
  12.  
  13. def get_pinmux_state( name ):
  14.     return pinmux_path( name ).read_text().rstrip()
  15.  
  16. def set_pinmux_state( name, state ):
  17.     try:
  18.         pinmux_path( name ).write_text( state )
  19.     except OSError as err:
  20.         if err.errno == errno.ENODEV:
  21.             raise ValueError( 'Invalid state for ' + name + ': ' + state ) from None
  22.         else:
  23.             raise
Add Comment
Please, Sign In to add comment