Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import winreg
  2. import socket
  3. import re
  4.  
  5. # I'm going to replace this with code that actually interacts with the registry. This way, the software can be ran and
  6. # Set the correct default printer as listed in Software\Microsoft\Windows NT\CurrentVersion\Devices based on the
  7. # hostname of the PC.
  8. def writeInFile(site, file, printer):
  9. printHKEY = lambda x: file.write("[HKEY_CURRENT_USER\Software\EagleSoft\Printers\\" + x + "]\r\n" +
  10. "\"devicename\"=\"\\\\\\\\DDS" + site + "-Server\\\\" + printer + "\"\r\n\r\n")
  11. regKeys = ['APPOINTMENT CARD', 'CHART', 'CHART LABEL', 'CLINICAL EXAM', 'ENVELOPE', 'HISTORY', 'IMAGE', 'INSURANCE',
  12. 'LASER CHART LABEL', 'LASER MAILING LABEL', 'LASER POSTCARD', 'LETTER']
  13. file.write("Windows Registry Editor Version 5.00\r\n\r\n[HKEY_CURRENT_USER\Software\EagleSoft\Printers]\r\n")
  14. file.write("\"default\"=\"\\\\\\\\DDS" + site + "-Server\\\\" + printer + ",winspool,Ne06:\"\r\n\r\n")
  15. for x in regKeys:
  16. printHKEY(x)
  17.  
  18. # Will replace this as well.
  19. def main():
  20. siteNumber = input("Generate registry files for site number: ")
  21. fFD = open("ESPrinterFD.reg", "w+")
  22. fOP = open("ESPrinterOP.reg", "w+")
  23. fTRYIN = open("ESPrinterTRYIN.reg", "w+")
  24. writeInFile(siteNumber, fFD, "FD")
  25. writeInFile(siteNumber, fOP, "OP")
  26. writeInFile(siteNumber, fTRYIN, "TRYIN")
  27. fFD.close()
  28. fOP.close()
  29. fTRYIN.close()
  30.  
  31. # main()
  32.  
  33. # Eaglesoft seems to have an issue when the default printer in the registry doesn't actually exist.
  34. # Create some functions to get the hostname so that we know what printer to add to the registry.
  35.  
  36. # fullHostname = socket.gethostname()
  37. fullHostname = 'DDS61-FD3' # Use this for testing.
  38. hostnameLeft = fullHostname.split('-')[0] # DDS61
  39. hostnameRight = fullHostname.split('-')[1] # FD3
  40. siteNumber = re.findall(r'\d+', hostnameLeft)[0] # Get the site number, even if >= 100
  41.  
  42. # A small function to create computer names with numbers into a list.
  43. def locPlusNum(location, startNumber, stopNumber):
  44. siteList = []
  45. for x in range(startNumber, stopNumber + 1):
  46. siteList.append(location + str(x))
  47. return siteList
  48.  
  49. fdList = locPlusNum('FD', 1, 4)
  50. tryinList = locPlusNum('TRYIN', 1, 7)
  51. opList = locPlusNum('OP', 7, 14)
  52. managerList = ['MANAGER', 'ASSISTMANAGER']
  53. allList = fdList + tryinList + opList + managerList
  54.  
  55. # This registry key will contain the values of all of the installed printers.
  56. registry = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices")
  57. valueIndex = 0
  58.  
  59. defaultPrinter = ''
  60.  
  61. # The name of the default Eaglesoft printer in the registry value will look like this:
  62. # [HKEY_CURRENT_USER\Software\EagleSoft\Printers]
  63. # "default"="\\\\DDS61-Server\\FD,winspool,Ne06:"
  64. # The variables we really care about here are the site number, the printer name, and the network port at the end.
  65. locationServer = '\\\\dds' + siteNumber + '-server\\'
  66. fdPrinter = str(locationServer + 'fd').lower()
  67. opPrinter = str(locationServer + 'op').lower()
  68. tryinPrinter = str(locationServer + 'tryin').lower()
  69. managerPrinter = str(locationServer + 'manager').lower()
  70.  
  71. # Loop through the printers in the registry. When we find a valid printer, test it against the name of the PC.
  72. # If the printer location matches the computer name, we'll set that as default.
  73. try:
  74. while 1:
  75. printerValues = winreg.EnumValue(registry, valueIndex)[0:2]
  76. printerValuesString = str(winreg.EnumValue(registry, valueIndex)[0]).lower()
  77. #print(printerValuesString)
  78. if opPrinter in printerValuesString:
  79. print('Found OP printer:', printerValuesString)
  80. if hostnameRight in opList:
  81. print('And this is an OP computer!')
  82. computerLocation = 'OP'
  83. defaultPrinter = printerValues
  84. break
  85. else: print('But this isn''t an OP computer. Moving on!')
  86. elif fdPrinter in printerValuesString:
  87. print('Found FD printer:', printerValuesString)
  88. if hostnameRight in fdList:
  89. print('And this is an FD computer!')
  90. computerLocation = 'FD'
  91. defaultPrinter = printerValues
  92. break
  93. else: print('But this isn''t a FD computer. Moving on!')
  94. elif tryinPrinter in printerValuesString:
  95. print('Found TRYIN printer:', printerValuesString)
  96. if hostnameRight in tryinList:
  97. print('And this is a TRYIN computer!')
  98. computerLocation = 'TRYIN'
  99. defaultPrinter = printerValues
  100. break
  101. else: print('But this isn''t a TRYIN computer. Moving on!')
  102. elif managerPrinter in printerValuesString:
  103. print('Found MANAGER printer:', printerValuesString)
  104. if hostnameRight in managerList:
  105. print('And this is a MANAGER computer!')
  106. computerLocation = 'MANAGER'
  107. defaultPrinter = printerValues
  108. break
  109. else: print('But this isn''t a MANAGER computer. Moving on!')
  110. #print(printerList)
  111. valueIndex += 1
  112. except OSError:
  113. pass
  114.  
  115. defaultPrinterSlashDoubled = str(','.join(defaultPrinter[0:2])).replace('\\', '\\\\')
  116. #print('Printer to set as default:', defaultPrinterSlashDoubled)
  117.  
  118. def addPrintersToRegistry(siteNumber, computerLocation, defaultPrinterSlashDoubled):
  119. print('"default"="' + defaultPrinterSlashDoubled + '"')
  120.  
  121.  
  122. addPrintersToRegistry(siteNumber, computerLocation, defaultPrinterSlashDoubled)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement