Results 1 to 13 of 13

Thread: OK - first program - for ALL!!

  1. #1
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts

    OK - first program - for ALL!!

    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.

  2. The Following 5 Users Say Thank You to oldfart For This Useful Post:

    catoro (14-05-2012), Dig Deep (14-05-2012), Friendly-Face (15-05-2012), mikie8 (14-05-2012), saintomer1866 (14-05-2012)

  3. #2
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    this is as far as I got ...

    Code:
    #!/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
    Last edited by oldfart; 14-05-2012 at 05:09 PM.

  4. The Following 2 Users Say Thank You to oldfart For This Useful Post:

    mikie8 (14-05-2012), saintomer1866 (14-05-2012)

  5. #3
    Super Moderator saintomer1866's Avatar
    Join Date
    Sep 2005
    Posts
    3,516
    Thanks
    4,440
    Thanked 7,747 Times in 2,327 Posts
    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

  6. #4
    V.I.P
    Join Date
    May 2010
    Posts
    945
    Thanks
    160
    Thanked 499 Times in 291 Posts
    i would also be very interested in an explaination .

  7. #5
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    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!

  8. #6
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    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.
    Last edited by oldfart; 14-05-2012 at 05:05 PM.

  9. The Following User Says Thank You to oldfart For This Useful Post:

    scrapman (15-05-2012)

  10. #7
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    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.

  11. #8
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    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.

    Code:
    #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.
    Last edited by oldfart; 14-05-2012 at 05:29 PM.

  12. The Following User Says Thank You to oldfart For This Useful Post:

    scrapman (15-05-2012)

  13. #9
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    Code:
    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]

  14. #10
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    Code:
    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)
    Last edited by oldfart; 15-05-2012 at 10:43 AM.

  15. #11
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    Next, we create the menu:
    Code:
    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.

  16. #12
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    this lot of code was just so I knew what is going on:
    Code:
    print choice
    n = int(choice)-1
    print file_list[n]
    final = oscamdir+file_list[n]+ ' -b -c '+oscamdir+'config'
    print final
    Attachment 53978
    Last edited by oldfart; 15-05-2012 at 03:57 PM.

  17. #13
    V.I.P oldfart's Avatar
    Join Date
    Oct 2008
    Posts
    1,488
    Thanks
    159
    Thanked 422 Times in 246 Posts
    haven't worked out how to actually run the command yet ....

Similar Threads

  1. He@d HD - PC Program - All in One
    By Correct in forum Tools & Editors
    Replies: 0
    Last Post: 03-12-2007, 03:18 PM
  2. He@d SD - PC Program - All in One
    By Correct in forum Tools & Editors
    Replies: 0
    Last Post: 03-12-2007, 03:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •