Skip to content

Commit

Permalink
Final version 0!
Browse files Browse the repository at this point in the history
* Added file Multiple_Phones.txt where people with more than one phone
end up

* Translated leftover comment

WISHLIST:
*Create input and output folders if missing
*Prompt for input file names if standard Export_Phones and Export_Users
are missing
  • Loading branch information
icovada committed Jul 19, 2013
1 parent adf53e6 commit 277ac00
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions userphoneupdate/userphoneupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,54 +38,56 @@ def asciify(string):
for row in reader:
c.execute('INSERT INTO users values (?,?,?)',(row[0], row[2], row[3]))

c.execute('INSERT INTO nophones SELECT * FROM phones')
c.execute('INSERT INTO nophones SELECT * FROM phones') #Copy phones table to nophones

conn.commit()

#AOpen output files
userupdate = open("output/Update_Users.txt", "w")
phoneupdateFL = open("output/Update_Phones_FL.txt", "w")
phoneupdateLF = open("output/Update_Phones_LF.txt", "w")
#Open output files
userupdate = open("output/Update_Users.txt", "w")
phoneupdateFL = open("output/Update_Phones_FL.txt", "w") # Update phone descriptions with FirstName LastName
phoneupdateLF = open("output/Update_Phones_LF.txt", "w") # or LastName FirstName
multiple = open("output/Multiple_Phones.txt","w")

#Write CSV headers
userupdate.write("USER ID, CONTROLLED DEVICE 1\n")
phoneupdateFL.write("MAC ADDRESS,DESCRIPTION,DIRECTORY NUMBER 1,LINE DESCRIPTION 1,LINE TEXT LABEL 1,ASCII LINE TEXT LABEL 1,ALERTING NAME 1,ASCII ALERTING NAME 1,DISPLAY 1,ASCII DISPLAY 1\n")
phoneupdateLF.write("MAC ADDRESS,DESCRIPTION,DIRECTORY NUMBER 1,LINE DESCRIPTION 1,LINE TEXT LABEL 1,ASCII LINE TEXT LABEL 1,ALERTING NAME 1,ASCII ALERTING NAME 1,DISPLAY 1,ASCII DISPLAY 1\n")

#Get all users ordered by last name
c.execute('SELECT * FROM users ORDER BY LastName')
result= c.fetchall()
userdump = c.fetchall()

counter=0
many=0
total=len(result)
total=len(userdump)

for row in result:
for row in userdump:
counter = counter + 1
if counter/100 == int(counter/100):
print("Processing", counter, "out of", total)

query = "SELECT * FROM phones WHERE "
names = str(row[0] + ' ' + row[1]).split() #Unisci nome e cognome, dividi singole parole
names = str(row[0] + ' ' + row[1]).split() #Merge first and last name, then split at every space ['Anne', 'Mary', 'Von', 'Munchausen']

for i in names:
query = query + "Description REGEXP '" + i.replace('\'','') + "' AND " #Create array of len(arr) REGEX, sanitize and drop apostrophes
query = query + "Description REGEXP '" + i.replace('\'','') + "' AND " #Create query of len(arr) REGEX, sanitize and drop apostrophes

query = query[:-5] #delete last AND

c.execute(query)
results=c.fetchall()

if len(results) == 1:
phonedump=c.fetchall()

if len(phonedump) > 0:
SEP = str(row[2])
Description = str(results[0][0])
Description = str(phonedump[0][0])
MAC = Description[3:]
FirstName = str(row[0]).title()
LastName = str(row[1]).title()
LineNumber = str(results[0][3])
LineNumber = str(phonedump[0][3])
FL = FirstName +" "+ LastName
LF = LastName +" "+ FirstName

if len(phonedump) == 1:

line = SEP +","+ Description +"\n" #JonesM,SEP12345
#Mac Address, Description, Dir Num 1, LINE DESCRIPTION 1,LINE TEXT LABEL 1,ASCII LINE TEXT LABEL 1,ALERTING NAME 1,ASCII ALERTING NAME 1,DISPLAY 1,ASCII DISPLAY 1
firstLast = MAC +","+ FL +","+ LineNumber+","+ FL +","+ FL +","+ asciify(FL) +","+ FL +","+ asciify(FL) +","+ FL +","+ asciify(FL)+"\n"
Expand All @@ -95,22 +97,29 @@ def asciify(string):
phoneupdateFL.write(firstLast)
phoneupdateLF.write(lastFirst)

query = "DELETE FROM nophones WHERE DeviceName == '"+str(results[0][0])+"'"
c.execute(query)
query = "DELETE FROM nophones WHERE DeviceName == '"+str(userdump[0][0])+"'" #If a phone is found, delete it from the nophones table
c.execute(query) #so that only unmatched phones remain

elif len(phonedump) > 1: #If the user has more than one phone
multiple.write(" " + FirstName + LastName+"\n")

for i in phonedump:
line = str(i)+"\n"
multiple.write(line)

multiple.write("\n")

elif len(results) > 1: #If the user has more than one phone
many = many + 1

for i in c.execute('SELECT DISTINCT DeviceType FROM nophones').fetchall():
emptyphone = open("output/Empty_"+i[0].replace(' ','_')+".txt","w")
for i in c.execute('SELECT DISTINCT DeviceType FROM nophones').fetchall(): # Create lists of unmatched phones divided
emptyphone = open("output/Empty_"+i[0].replace(' ','_')+".txt","w") # per phone model
emptyphone.write("USER ID, CONTROLLED DEVICE 1\n")

query = 'SELECT Description, DeviceName FROM nophones WHERE DeviceType == \''+i[0]+'\' ORDER BY Description'
c.execute(query)
result=c.fetchall()

for row in result:
emptyphone.write(row[0] + "," + row[1].title())
emptyphone.write(row[0].title() + "," + row[1].title())
emptyphone.write("\n")

emptyphone.close()
Expand All @@ -120,3 +129,4 @@ def asciify(string):
userupdate.close()
phoneupdateFL.close()
phoneupdateLF.close()
multiple.close()

0 comments on commit 277ac00

Please sign in to comment.