dwc_config.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """DWC Network Server Emulator
  2. Copyright (C) 2014 SMTDDR
  3. Copyright (C) 2014 kyle95wm
  4. Copyright (C) 2014 AdmiralCurtiss
  5. Copyright (C) 2015 Sepalani
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License as
  8. published by the Free Software Foundation, either version 3 of the
  9. License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Affero General Public License for more details.
  14. You should have received a copy of the GNU Affero General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. Configuration module.
  17. """
  18. try:
  19. # Python 2
  20. import ConfigParser
  21. except ImportError:
  22. # Python 3
  23. import configparser as ConfigParser
  24. import other.utils as utils
  25. def get_config_filename(filename='altwfc.cfg'):
  26. """Return the config filename that will be used."""
  27. try:
  28. config = ConfigParser.RawConfigParser(allow_no_value=True)
  29. config.read(filename)
  30. if config.getboolean('Config', 'AlternativeConfig'):
  31. return config.get('Config', 'AlternativeConfigFile')
  32. except Exception as e:
  33. pass
  34. return filename
  35. def get_ip_port(section, filename='altwfc.cfg'):
  36. """Return a tuple (IP, Port) of the corresponding section."""
  37. config = ConfigParser.RawConfigParser(allow_no_value=True)
  38. config.read(get_config_filename(filename))
  39. return (config.get(section, 'IP'), config.getint(section, 'Port'))
  40. def get_ip(section, filename='altwfc.cfg'):
  41. """Return the IP of the corresponding section."""
  42. config = ConfigParser.RawConfigParser(allow_no_value=True)
  43. config.read(get_config_filename(filename))
  44. return config.get(section, 'IP')
  45. def get_port(section, filename='altwfc.cfg'):
  46. """Return the port of the corresponding section."""
  47. config = ConfigParser.RawConfigParser(allow_no_value=True)
  48. config.read(get_config_filename(filename))
  49. return config.getint(section, 'Port')
  50. def get_logger(section, filename='altwfc.cfg'):
  51. """Return the logger of the corresponding section."""
  52. config = ConfigParser.RawConfigParser(allow_no_value=True)
  53. config.read(get_config_filename(filename))
  54. return utils.create_logger(
  55. config.get(section, 'LoggerName'),
  56. config.get(section, 'LoggerFilename'),
  57. config.getint(section, 'LoggerLevel'),
  58. config.getboolean(section, 'LoggerOutputConsole'),
  59. config.getboolean(section, 'LoggerOutputFile')
  60. )
  61. def get_svchost(section, filename='altwfc.cfg'):
  62. """Return the svchost of the corresponding section."""
  63. config = ConfigParser.RawConfigParser(allow_no_value=True)
  64. config.read(get_config_filename(filename))
  65. return config.get(section, 'SvcHost')