PDA

View Full Version : OK - first program - for ALL!!



oldfart
13-05-2012, 04:04 PM
I have a directory on my server with loads of Oscam files in it.
I want a menu, which shows them, allows me to choose which one, then runs it.
This can apply to Linux boxes (I think) so should work on DM/VU etc??

That's the plan anyway.

oldfart
13-05-2012, 04:06 PM
this is as far as I got ...


#!/usr/bin/python
import fnmatch
import os
from subprocess import call

#oscamdir = '/nas/oscam/'
oscamdir = 'C:/Users/andrew/Downloads'

file_list = []
#for fileName in os.listdir ( '/nas/oscam/' ):
for fileName in os.listdir ( oscamdir ):
if fnmatch.fnmatch ( fileName, 'oscam*' ):

file_list.append(fileName)

print " ****** MENU ******"
for i in file_list:
print 1 + file_list.index(i),
print ") " + i
print "* ) to quit"
choice = raw_input("Which item do you want? ")

print choice
n = int(choice)-1
print file_list[n]
final = oscamdir+file_list[n]+ ' -b -c '+oscamdir+'config'
print final
#sudo /nas/oscam/oscam6510 -b -c /nas/oscam/config


there are quite a few 'checks' in this code; and you can change the directory it looks in.

if you want me to explain. line by line, I can

saintomer1866
14-05-2012, 02:38 PM
Thanks Oldfart,
yes i think a line by line explanation would be useful to all us novices in order to try and start to understand commands and syntax

mikie8
14-05-2012, 02:49 PM
i would also be very interested in an explaination .

oldfart
14-05-2012, 04:58 PM
well ... I am NOT an expert in Python programming, but I'll give it a go.
Bear in mind, its HOT here at the moment (50C in the sun) and the pool is 32C, so spending a fair amount of time in the pool.

Anyone can help out!

oldfart
14-05-2012, 05:01 PM
Python code will have different start-up lines (dunno what they are called) depending what machine the program is run on.
the first line
#!/usr/bin/python
is for a linux pc

a DM program will have a different first line

When do you

#!/usr/local/bin/python
You are specifying the location to the python executable in your machine, that rest of the script needs to be interpreted with.
You are pointing to python is located at /usr/local/bin/python

Consider the possiblities that in a different machine, python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail.

oldfart
14-05-2012, 05:16 PM
the 'import' commands allow you to make calls to modules previously written (and in this case, part of Python)
so the lines
import fnmatch
import os
from subprocess import call

allow you to call os.something or fnmatch.something or use the call command.

oldfart
14-05-2012, 05:21 PM
The idea of the program is to list files in a directory that contain the name "oscam"

so, rather than write out the directory every time we use it, it is best to store the directory name in a variable.


#oscamdir = '/nas/oscam/'
oscamdir = 'C:/Users/andrew/Downloads'

does this. oscamdir is shown here with two options, one is my Linux PC, the other is the Windows PC I used for testing the code.

note the '#' symbol - this is a comment and a line starting with a # is ignored when the program runs.

oldfart
14-05-2012, 05:27 PM
file_list = []
this creates a 'list'
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. List items need not all have the same type.

****> a = ['spam', 'eggs', 100, 1234]
****> a
['spam', 'eggs', 100, 1234]

oldfart
14-05-2012, 05:33 PM
for fileName in os.listdir ( oscamdir ):
if fnmatch.fnmatch ( fileName, 'oscam*' ):
file_list.append(fileName)
creates a loop (FOR .... NEXT) - except there is no NEXT in Python - the indents (TAB characters) indicate where the loop starts/ends

the program goes through the directory listing
for fileName in os.listdir ( oscamdir ):

if the entry it found matches what we are looking for
if fnmatch.fnmatch ( fileName, 'oscam*' ):

it adds the name to the list
file_list.append(fileName)

oldfart
15-05-2012, 10:55 AM
Next, we create the menu:

print " ****** MENU ******"
for i in file_list:
print 1 + file_list.index(i),
print ") " + i
print "* ) to quit"
choice = raw_input("Which item do you want? ")

this prints out each element of the list, together with a number - note that lists start at 0 - so we add 1 to the start of the list, so the numbering starts at 1.

then allow the user to choose which entry they want.

oldfart
15-05-2012, 10:57 AM
this lot of code was just so I knew what is going on:

print choice
n = int(choice)-1
print file_list[n]
final = oscamdir+file_list[n]+ ' -b -c '+oscamdir+'config'
print final

53978

oldfart
15-05-2012, 03:58 PM
haven't worked out how to actually run the command yet ....