Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

process_connect_string.py 5.5KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """ a clumsy attempt at a macro language to let the programmer execute code on the server (ex: determine 64bit)"""
  2. from . import is64bit as is64bit
  3. def macro_call(macro_name, args, kwargs):
  4. """allow the programmer to perform limited processing on the server by passing macro names and args
  5. :new_key - the key name the macro will create
  6. :args[0] - macro name
  7. :args[1:] - any arguments
  8. :code - the value of the keyword item
  9. :kwargs - the connection keyword dictionary. ??key has been removed
  10. --> the value to put in for kwargs['name'] = value
  11. """
  12. if isinstance(args, (str, str)):
  13. args = [
  14. args
  15. ] # the user forgot to pass a sequence, so make a string into args[0]
  16. new_key = args[0]
  17. try:
  18. if macro_name == "is64bit":
  19. if is64bit.Python(): # if on 64 bit Python
  20. return new_key, args[1] # return first argument
  21. else:
  22. try:
  23. return new_key, args[2] # else return second argument (if defined)
  24. except IndexError:
  25. return new_key, "" # else return blank
  26. elif (
  27. macro_name == "getuser"
  28. ): # get the name of the user the server is logged in under
  29. if not new_key in kwargs:
  30. import getpass
  31. return new_key, getpass.getuser()
  32. elif macro_name == "getnode": # get the name of the computer running the server
  33. import platform
  34. try:
  35. return new_key, args[1] % platform.node()
  36. except IndexError:
  37. return new_key, platform.node()
  38. elif macro_name == "getenv": # expand the server's environment variable args[1]
  39. try:
  40. dflt = args[2] # if not found, default from args[2]
  41. except IndexError: # or blank
  42. dflt = ""
  43. return new_key, os.environ.get(args[1], dflt)
  44. elif macro_name == "auto_security":
  45. if (
  46. not "user" in kwargs or not kwargs["user"]
  47. ): # missing, blank, or Null username
  48. return new_key, "Integrated Security=SSPI"
  49. return new_key, "User ID=%(user)s; Password=%(password)s" % kwargs
  50. elif (
  51. macro_name == "find_temp_test_path"
  52. ): # helper function for testing ado operation -- undocumented
  53. import os
  54. import tempfile
  55. return new_key, os.path.join(
  56. tempfile.gettempdir(), "adodbapi_test", args[1]
  57. )
  58. raise ValueError("Unknown connect string macro=%s" % macro_name)
  59. except:
  60. raise ValueError("Error in macro processing %s %s" % (macro_name, repr(args)))
  61. def process(
  62. args, kwargs, expand_macros=False
  63. ): # --> connection string with keyword arguments processed.
  64. """attempts to inject arguments into a connection string using Python "%" operator for strings
  65. co: adodbapi connection object
  66. args: positional parameters from the .connect() call
  67. kvargs: keyword arguments from the .connect() call
  68. """
  69. try:
  70. dsn = args[0]
  71. except IndexError:
  72. dsn = None
  73. if isinstance(
  74. dsn, dict
  75. ): # as a convenience the first argument may be django settings
  76. kwargs.update(dsn)
  77. elif (
  78. dsn
  79. ): # the connection string is passed to the connection as part of the keyword dictionary
  80. kwargs["connection_string"] = dsn
  81. try:
  82. a1 = args[1]
  83. except IndexError:
  84. a1 = None
  85. # historically, the second positional argument might be a timeout value
  86. if isinstance(a1, int):
  87. kwargs["timeout"] = a1
  88. # if the second positional argument is a string, then it is user
  89. elif isinstance(a1, str):
  90. kwargs["user"] = a1
  91. # if the second positional argument is a dictionary, use it as keyword arguments, too
  92. elif isinstance(a1, dict):
  93. kwargs.update(a1)
  94. try:
  95. kwargs["password"] = args[2] # the third positional argument is password
  96. kwargs["host"] = args[3] # the fourth positional argument is host name
  97. kwargs["database"] = args[4] # the fifth positional argument is database name
  98. except IndexError:
  99. pass
  100. # make sure connection string is defined somehow
  101. if not "connection_string" in kwargs:
  102. try: # perhaps 'dsn' was defined
  103. kwargs["connection_string"] = kwargs["dsn"]
  104. except KeyError:
  105. try: # as a last effort, use the "host" keyword
  106. kwargs["connection_string"] = kwargs["host"]
  107. except KeyError:
  108. raise TypeError("Must define 'connection_string' for ado connections")
  109. if expand_macros:
  110. for kwarg in list(kwargs.keys()):
  111. if kwarg.startswith("macro_"): # If a key defines a macro
  112. macro_name = kwarg[6:] # name without the "macro_"
  113. macro_code = kwargs.pop(
  114. kwarg
  115. ) # we remove the macro_key and get the code to execute
  116. new_key, rslt = macro_call(
  117. macro_name, macro_code, kwargs
  118. ) # run the code in the local context
  119. kwargs[new_key] = rslt # put the result back in the keywords dict
  120. # special processing for PyRO IPv6 host address
  121. try:
  122. s = kwargs["proxy_host"]
  123. if ":" in s: # it is an IPv6 address
  124. if s[0] != "[": # is not surrounded by brackets
  125. kwargs["proxy_host"] = s.join(("[", "]")) # put it in brackets
  126. except KeyError:
  127. pass
  128. return kwargs