0

Alright, so I'm writing a program to help connect to wireless networks. I have most of it down (in fact, it's complete. I'm just working on extra features.)

I'm writing a GUI frontend for a wireless network connection backend called NetCTL for the Arch Linux Operating System. Basically, people can manually create profiles and name it whatever they want (i.e., "asdfasdfasdf"), but mine will ALWAYS generate $NetworkSSID_wifiz.

However, every file will have one line in it that would be able to determine if it is for the same network.

The line is:

ESSID='$NetworkSSID'

So how would I go about opening each file that appears in os.listdir and checking if those two files have the same line (while not producing too much overhead, preferably.)?

All profiles are saved in /etc/netctl whether generated by my program, or by the user.

Sample files:

User Created:

Description='A simple WPA encrypted wireless connection'
Interface=wlp2s0
Connection=wireless
Security=wpa

IP=dhcp

ESSID='MomAndKids'
# Prepend hexadecimal keys with \"
# If your key starts with ", write it as '""<key>"'
# See also: the section on special quoting rules in netctl.profile(5)
Key='########'
# Uncomment this if your ssid is hidden
#Hidden=yes

Created by my program:

Description='A profile generated by WiFiz for MomAndKids'
Interface=wlp2s0
Connection=wireless
Security=wpa
ESSID='MomAndKids'
Key='#######'
IP=dhcp

Sample os.listdir output:

['hooks', 'interfaces', 'examples', 'ddwrt', 'MomAndKids_wifiz', 'backups', 'MomAndKids']
Cody Dostal
  • 311
  • 1
  • 3
  • 13

2 Answers2

2

This should work for you:

from glob import glob
from os import path

config_dir = '/etc/netctl'

profiles = dict((i, {'full_path': v, 'ESSID': None, 'matches': []}) for (i, v) in enumerate(glob(config_dir + '/*')) if path.isfile(v))

for K, V in profiles.items():
    with open(V['full_path']) as f:
        for line in f:
            if line.startswith('ESSID'):
                V['ESSID'] = line.split('=',1)[1].strip()
                break # no need to keep reading.
    for k, v in profiles.items():
        if K == k or k in V['matches'] or not v['ESSID']:
            continue
        if V['ESSID'] == v['ESSID']:
            V['matches'].append(k)
            v['matches'].append(K)

for k, v in profiles.items():
    print k, v
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • Returns no input after running. [cody@cody-laptop Documents]$ python2 testPy.py [cody@cody-laptop Documents]$ – Cody Dostal May 21 '13 at 08:52
  • I don't have your files, so I can not debug this. But I tested it on my files and it worked. – Inbar Rose May 21 '13 at 08:53
  • Hmm... I wonder why it doesn't do anything for me. Did you copy the sample files at the top, or just put one ESSID line in the file? – Cody Dostal May 21 '13 at 08:54
  • just added ESSID=SOMETHING into some files in the folder I used. It is strange you get NO OUTPUT it should at least output that there are no matches. – Inbar Rose May 21 '13 at 08:55
  • It's running Python 2.7.5, if that matters. I highly doubt it would. I'm really confused as to why it is doing this. Actually, what might cause it is at the end of line 6, you have "/*.*". The files will not have an extension. – Cody Dostal May 21 '13 at 08:57
  • What exact output are you getting? (did you copy the code I posted exactly?) – Inbar Rose May 21 '13 at 08:57
  • I did. But I think I figured out why, although now I need it to avoid folders. Time to try and work in os.path.isfile – Cody Dostal May 21 '13 at 08:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30309/discussion-between-inbar-rose-and-cody-dostal) So we don't spam this too much, just press the link. – Inbar Rose May 21 '13 at 08:59
0
import os

all_essid = []

for file in os.listdir('.'):

    if not os.path.isfile(file):
        break

    with open(file) as fo:
        file_lines = fo.readlines()

    for line in file_lines:

        if line.startswith('ESSID')

            if line in all_essid:
                print 'duplicate essid %s' % line

            all_essid.append(line)

Or you could try os.walk if you want to descend into the directories;

    for root, dirs, files in os.walk("."):

        for file in files:

             # etc.
danodonovan
  • 19,636
  • 10
  • 70
  • 78