/initConfig.py

https://github.com/pylight/P-2-Config-Tool · Python · 144 lines · 104 code · 30 blank · 10 comment · 23 complexity · 9b70dab6eaff0beea75a6b64c99acab0 MD5 · raw file

  1. #!/usr/bin/env python
  2. # Filename: initConfig.py
  3. import configparser, os
  4. from dialogs import errorDialog, infoDialog, questionDialog, choiceDialog
  5. from sys import exit
  6. # default config settings
  7. defaultID = "PP"
  8. defaultPath = "/etc/NetworkManager/system-connections/"
  9. defaultEditor = "gedit"
  10. defaultFile = "~/.ppvpntool.conf"
  11. # checks if the default path exists and else asks for another path
  12. def checkPath(path, conID):
  13. if(os.path.exists(path)):
  14. print("Using " + path + " as vpn config path.")
  15. infoDialog("This Tool will look for your VPN Connection settings at " + path + ". If you need to change this, use the Tool Menu. (Connection -> Tool Settings)")
  16. else:
  17. print("Error: File not found at " + path)
  18. nmPath = questionDialog("No file found at " + path + "\nPlease specify the NetworkManager path for VPN-Connection-Settings on your system.", defaultPath)
  19. path = nmPath + conID
  20. if not os.path.exists(path):
  21. errorDialog("Error: no VPN configfile found at " + path + ". Please make sure you entered the right values and created the PP VPN connection in NetworkManager and rerun this tool!")
  22. exit("Error: Setup failed (invalid VPN config file)")
  23. return path
  24. # ask the user for the settings
  25. def setID():
  26. return questionDialog("Please input the name (ID) of your VPN Connection", defaultID)
  27. def setEditor():
  28. return questionDialog("Please input your prefered (graphical) Editor:", defaultEditor)
  29. def setvpnType():
  30. return choiceDialog("Please choose the type of your VPN connection", ["pptp", "openvpn"])
  31. def setPath():
  32. path = questionDialog("Invalid setting: please specify the NetworkManager path for VPN-Connection-Settings on your system.", defaultPath)
  33. if not os.path.exists(path):
  34. errorDialog("Error: the path " + path + " doesn't exist. Please make sure you entered the right values, check the tool configuration (" + defaultFile + ") and rerun this tool!")
  35. exit("Error: Setup failed (invalid VPN config path)")
  36. print("Using " + path + " as NM path.")
  37. return path
  38. def setOvpnFolder():
  39. path = questionDialog("You choose openvpn as connection type. PP Config Tool will use the openvpn configuration files from the PP member downloads. Please get that package (https://www.perfect-privacy.com/members/All.ovpn.ubuntu.zip), extract it and input the path of that folder below:", os.path.expanduser("~/Downloads/All.ovpn.ubuntu"))
  40. if not os.path.exists(path):
  41. errorDialog("Error: the path " + path + " doesn't exist. Please make sure you entered the right values, check the tool configuration (" + defaultFile + ") and rerun this tool!")
  42. exit("Error: Setup failed (invalid VPN config path)")
  43. return path
  44. # validate the config file
  45. def checkConfig(config, path):
  46. config.read(path)
  47. eCount = 0
  48. try:
  49. config.get('General', 'connection')
  50. except configparser.NoOptionError as ex:
  51. print("Warning: " + str(ex))
  52. eCount = eCount + 1
  53. config.set('General', 'connection', setID())
  54. try:
  55. config.get('General', 'path')
  56. except configparser.NoOptionError as ex:
  57. print("Warning: " + str(ex))
  58. eCount = eCount + 1
  59. config.set('General', 'path', setPath())
  60. try:
  61. config.get('General', 'editor')
  62. except configparser.NoOptionError as ex:
  63. print("Warning: " + str(ex))
  64. eCount = eCount + 1
  65. config.set('General', 'editor', setEditor())
  66. try:
  67. config.get('General', 'type')
  68. except configparser.NoOptionError as ex:
  69. print("Warning: " + str(ex))
  70. eCount = eCount + 1
  71. config.set('General', 'type', setvpnType())
  72. if config.get('General', 'type') == "openvpn":
  73. try:
  74. config.get('Openvpn', 'certfolder')
  75. except configparser.NoSectionError:
  76. config.add_section('Openvpn')
  77. config.set('Openvpn', 'certfolder', setOvpnFolder())
  78. except configparser.NoOptionError:
  79. config.set('Openvpn', 'certfolder', setOvpnFolder())
  80. if eCount > 0:
  81. print("Rewriting config file.")
  82. with open(path, 'w') as configfile:
  83. config.write(configfile)
  84. else:
  85. print("Configuration read without errors.")
  86. # running whenever the tool starts
  87. def initConfig():
  88. # try to read tool configuration
  89. mainconfig = configparser.RawConfigParser()
  90. confpath = os.path.expanduser(defaultFile)
  91. if (os.path.exists(confpath) == False):
  92. print("\nConfigfile", confpath, "not found. Running Config Setup...")
  93. infoDialog("The configuration was not found at " + defaultFile + ". Starting Setup-Process...")
  94. conID = setID()
  95. path = defaultPath + conID
  96. # validate vpn config path and set editor
  97. path = checkPath(path, conID)
  98. vpntype = setvpnType()
  99. editor = setEditor()
  100. # write config file
  101. mainconfig.add_section('General')
  102. mainconfig.set('General', 'path', path)
  103. mainconfig.set('General', 'connection', conID)
  104. mainconfig.set('General', 'type', vpntype)
  105. mainconfig.set('General', 'editor', editor)
  106. if mainconfig.get('General', 'type') == "openvpn":
  107. mainconfig.add_section('Openvpn')
  108. mainconfig.set('Openvpn', 'certfolder', setOvpnFolder())
  109. with open(confpath, 'w') as configfile:
  110. mainconfig.write(configfile)
  111. infoDialog("The configuration was written to \n" + confpath)
  112. print("Reading configuration...")
  113. checkConfig(mainconfig, confpath)
  114. return mainconfig