Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import winreg
- import socket
- import re
- # I'm going to replace this with code that actually interacts with the registry. This way, the software can be ran and
- # Set the correct default printer as listed in Software\Microsoft\Windows NT\CurrentVersion\Devices based on the
- # hostname of the PC.
- def writeInFile(site, file, printer):
- printHKEY = lambda x: file.write("[HKEY_CURRENT_USER\Software\EagleSoft\Printers\\" + x + "]\r\n" +
- "\"devicename\"=\"\\\\\\\\DDS" + site + "-Server\\\\" + printer + "\"\r\n\r\n")
- regKeys = ['APPOINTMENT CARD', 'CHART', 'CHART LABEL', 'CLINICAL EXAM', 'ENVELOPE', 'HISTORY', 'IMAGE', 'INSURANCE',
- 'LASER CHART LABEL', 'LASER MAILING LABEL', 'LASER POSTCARD', 'LETTER']
- file.write("Windows Registry Editor Version 5.00\r\n\r\n[HKEY_CURRENT_USER\Software\EagleSoft\Printers]\r\n")
- file.write("\"default\"=\"\\\\\\\\DDS" + site + "-Server\\\\" + printer + ",winspool,Ne06:\"\r\n\r\n")
- for x in regKeys:
- printHKEY(x)
- # Will replace this as well.
- def main():
- siteNumber = input("Generate registry files for site number: ")
- fFD = open("ESPrinterFD.reg", "w+")
- fOP = open("ESPrinterOP.reg", "w+")
- fTRYIN = open("ESPrinterTRYIN.reg", "w+")
- writeInFile(siteNumber, fFD, "FD")
- writeInFile(siteNumber, fOP, "OP")
- writeInFile(siteNumber, fTRYIN, "TRYIN")
- fFD.close()
- fOP.close()
- fTRYIN.close()
- # main()
- # Eaglesoft seems to have an issue when the default printer in the registry doesn't actually exist.
- # Create some functions to get the hostname so that we know what printer to add to the registry.
- # fullHostname = socket.gethostname()
- fullHostname = 'DDS61-FD3' # Use this for testing.
- hostnameLeft = fullHostname.split('-')[0] # DDS61
- hostnameRight = fullHostname.split('-')[1] # FD3
- siteNumber = re.findall(r'\d+', hostnameLeft)[0] # Get the site number, even if >= 100
- # A small function to create computer names with numbers into a list.
- def locPlusNum(location, startNumber, stopNumber):
- siteList = []
- for x in range(startNumber, stopNumber + 1):
- siteList.append(location + str(x))
- return siteList
- fdList = locPlusNum('FD', 1, 4)
- tryinList = locPlusNum('TRYIN', 1, 7)
- opList = locPlusNum('OP', 7, 14)
- managerList = ['MANAGER', 'ASSISTMANAGER']
- allList = fdList + tryinList + opList + managerList
- # This registry key will contain the values of all of the installed printers.
- registry = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices")
- valueIndex = 0
- defaultPrinter = ''
- # The name of the default Eaglesoft printer in the registry value will look like this:
- # [HKEY_CURRENT_USER\Software\EagleSoft\Printers]
- # "default"="\\\\DDS61-Server\\FD,winspool,Ne06:"
- # The variables we really care about here are the site number, the printer name, and the network port at the end.
- locationServer = '\\\\dds' + siteNumber + '-server\\'
- fdPrinter = str(locationServer + 'fd').lower()
- opPrinter = str(locationServer + 'op').lower()
- tryinPrinter = str(locationServer + 'tryin').lower()
- managerPrinter = str(locationServer + 'manager').lower()
- # Loop through the printers in the registry. When we find a valid printer, test it against the name of the PC.
- # If the printer location matches the computer name, we'll set that as default.
- try:
- while 1:
- printerValues = winreg.EnumValue(registry, valueIndex)[0:2]
- printerValuesString = str(winreg.EnumValue(registry, valueIndex)[0]).lower()
- #print(printerValuesString)
- if opPrinter in printerValuesString:
- print('Found OP printer:', printerValuesString)
- if hostnameRight in opList:
- print('And this is an OP computer!')
- computerLocation = 'OP'
- defaultPrinter = printerValues
- break
- else: print('But this isn''t an OP computer. Moving on!')
- elif fdPrinter in printerValuesString:
- print('Found FD printer:', printerValuesString)
- if hostnameRight in fdList:
- print('And this is an FD computer!')
- computerLocation = 'FD'
- defaultPrinter = printerValues
- break
- else: print('But this isn''t a FD computer. Moving on!')
- elif tryinPrinter in printerValuesString:
- print('Found TRYIN printer:', printerValuesString)
- if hostnameRight in tryinList:
- print('And this is a TRYIN computer!')
- computerLocation = 'TRYIN'
- defaultPrinter = printerValues
- break
- else: print('But this isn''t a TRYIN computer. Moving on!')
- elif managerPrinter in printerValuesString:
- print('Found MANAGER printer:', printerValuesString)
- if hostnameRight in managerList:
- print('And this is a MANAGER computer!')
- computerLocation = 'MANAGER'
- defaultPrinter = printerValues
- break
- else: print('But this isn''t a MANAGER computer. Moving on!')
- #print(printerList)
- valueIndex += 1
- except OSError:
- pass
- defaultPrinterSlashDoubled = str(','.join(defaultPrinter[0:2])).replace('\\', '\\\\')
- #print('Printer to set as default:', defaultPrinterSlashDoubled)
- def addPrintersToRegistry(siteNumber, computerLocation, defaultPrinterSlashDoubled):
- print('"default"="' + defaultPrinterSlashDoubled + '"')
- addPrintersToRegistry(siteNumber, computerLocation, defaultPrinterSlashDoubled)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement