OpenSprinkler › Forums › Hardware Questions › OpenSprinkler Pi (OSPi) › Custom Additions to Interval Program for OSPi? › Re: Re: Custom Additions to Interval Program for OSPi?
djagerif
Ok, seeing that I didn’t get any response I went ahead and did my own thing. For starters I modified the ospi.py program to look for a file called custom.py, if found then it imports the classes from that file and also loads a different ‘urls’ declaration. If the file doesn’t exist then it loads the standard ‘urls’ definition and everything is as per normal.
With the custom file I can add my own stuff to enhance the interval program without messing with the original program. Here are my changes for anyone interested.
PS. I am sure there are better ways of doing it but hey, no one offered any advice.
Just below the import statement I added the following and deleted the original ‘urls’ command.
#### Load Custom Import file if it exists ####
try:
with open('./custom.py'):
import custom
urls = (
'/', 'home',
'/cv', 'change_values',
'/vo', 'view_options',
'/co', 'change_options',
'/vs', 'view_stations',
'/cs', 'change_stations', # name and master
'/sn(d+?Z)', 'get_station', # regular expression, accepts any station number
'/sn(d+?=d(&t=d+?Z)?)', 'set_station', # regular expression, accepts any digits
'/vr', 'view_runonce',
'/cr', 'change_runonce',
'/vp', 'view_programs',
'/mp', 'modify_program', # open 'Modify program' window
'/cp', 'change_program',
'/dp', 'delete_program',
'/gp', 'graph_programs',
'/vl', 'view_log',
'/cl', 'clear_log',
'/lo', 'log_options',
'/c1', 'custom.custom_1', # Custom function 1
'/c2', 'custom.custom_2', # Custom function 2
'/c3', 'custom.custom_3', # Custom function 3
)
except IOError:
print 'Custom Imports not found.'
urls = (
'/', 'home',
'/cv', 'change_values',
'/vo', 'view_options',
'/co', 'change_options',
'/vs', 'view_stations',
'/cs', 'change_stations', # name and master
'/sn(d+?Z)', 'get_station', # regular expression, accepts any station number
'/sn(d+?=d(&t=d+?Z)?)', 'set_station', # regular expression, accepts any digits
'/vr', 'view_runonce',
'/cr', 'change_runonce',
'/vp', 'view_programs',
'/mp', 'modify_program', # open 'Modify program' window
'/cp', 'change_program',
'/dp', 'delete_program',
'/gp', 'graph_programs',
'/vl', 'view_log',
'/cl', 'clear_log',
'/lo', 'log_options',
)
Then I created a custom.py file containing my code I wish to add. Here is a sample
#!/usr/bin/python
"""Updated 01/07/2013."""
import ospi
class custom_1:
"""View all the options above and also station names."""
def GET(self):
custpg = 'n'
custpg += 'n'
custpg += 'n'
custpg += 'n'
return custpg
class custom_2:
""" Custom Script 2 """
def GET(self):
custpg = 'n'
#Insert Custom Code here.
return custpg
class custom_3:
""" Custom Script 3 """
def GET(self):
custpg = 'n'
#Insert Custom Code here.
return custpg