#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Very quick and dirty module to do basic GPIO stuff on /sys/class/gpio/ (to which normal users can be given access with relative safety, 
/dev/mem, which RPi.GPIO uses is definitely root-only -land)"""
import os

def channel_controlpath(channel):
    """Get the /sys/class/gpio/ -path for the given channel"""
    return "/sys/class/gpio/gpio%d" % int(channel)

def conditional_export(channel):
    """Call export if channel is not exported"""
    if os.path.isdir(channel_controlpath(channel)):
        return True
    return export(channel)

def export(channel):
    """Exports the given channel to /sys/class/gpio/"""
    with open("/sys/class/gpio/export", 'w') as f:
        f.write(str(channel))
    return True

def set_direction(channel, direction):
    """Set direction ('in' or 'out') of the channel"""
    conditional_export(channel)
    path = "%s/direction" % channel_controlpath(channel)
    with open(path, 'w') as f:
        f.write(direction)
    return True

def set_value(channel, value):
    """Set value for the channel (forces it to 'out' direction first)"""
    set_direction(channel, 'out')
    path = "%s/value" % channel_controlpath(channel)
    #print("path=%s value=%s" % (path, value))
    with open(path, 'w') as f:
        f.write(str(value))
    return True

def red_value(channel):
    """UNTESTED: Reads the value from given channel (forces it to 'in' direction first)"""
    set_direction(channel, 'in')
    path = "%s/value" % channel_controlpath(channel)
    with open(path, 'r') as f:
        ret = f.readline()
    return ret



if __name__ == '__main__':
    import sys
    if len(sys.argv) < 3:
        print "Usage (for output): usergpio.py channel state"
        sys.exit(1)
    set_value(int(sys.argv[1]), int(sys.argv[2]))

@@ -254,16 +254,143 @@
 ort os%0A%0A
+#TODO: Steal the epoll idea from https://github.com/derekstavis/python-sysfs-gpio/blob/master/src/sysfs/gpio.py%0A%0AFAKE = False%0A%0A
 def chan
@@ -785,24 +785,96 @@
 ss/gpio/%22%22%22%0A
+    if FAKE:%0A        print(%22export(%25s)%22 %25 channel)%0A        return False%0A
     with ope
@@ -1134,32 +1134,128 @@
 olpath(channel)%0A
+    if FAKE:%0A        print(%22set_direction(%25s, %25s)%22 %25 (channel, direction))%0A        return False%0A
     with open(pa
@@ -1547,24 +1547,112 @@
 th, value))%0A
+    if FAKE:%0A        print(%22set_value(%25s, %25s)%22 %25 (channel, value))%0A        return False%0A
     with ope
@@ -1721,16 +1721,17 @@
 %0A%0Adef re
+a
 d_value(
@@ -1864,16 +1864,16 @@
 , 'in')%0A
-
     path
@@ -1913,24 +1913,100 @@
 th(channel)%0A
+    if FAKE:%0A        print(%22read_value(%25s)%22 %25 channel)%0A        return False%0A
     with ope

add FAKE mode for testing on VMs etc