__init__.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (c) 2012-2013 LiuYC https://github.com/liuyichen/
  2. # Copyright 2012-2014 ksyun.com, Inc. or its affiliates. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"). You
  5. # may not use this file except in compliance with the License. A copy of
  6. # the License is located at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # or in the "license" file accompanying this file. This file is
  11. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. # ANY KIND, either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the License.
  14. import os
  15. import re
  16. import logging
  17. __version__ = '1.3.62'
  18. class NullHandler(logging.Handler):
  19. def emit(self, record):
  20. pass
  21. # Configure default logger to do nothing
  22. log = logging.getLogger('kscore')
  23. log.addHandler(NullHandler())
  24. _first_cap_regex = re.compile('(.)([A-Z][a-z]+)')
  25. _number_cap_regex = re.compile('([a-z])([0-9]+)')
  26. _end_cap_regex = re.compile('([a-z0-9])([A-Z])')
  27. # The regex below handles the special case where some acryonym
  28. # name is pluralized, e.g GatewayARNs, ListWebACLs, SomeCNAMEs.
  29. _special_case_transform = re.compile('[A-Z]{3,}s$')
  30. # Prepopulate the cache with special cases that don't match
  31. # our regular transformation.
  32. _xform_cache = {
  33. ('CreateCachediSCSIVolume', '_'): 'create_cached_iscsi_volume',
  34. ('CreateCachediSCSIVolume', '-'): 'create-cached-iscsi-volume',
  35. ('DescribeCachediSCSIVolumes', '_'): 'describe_cached_iscsi_volumes',
  36. ('DescribeCachediSCSIVolumes', '-'): 'describe-cached-iscsi-volumes',
  37. ('DescribeStorediSCSIVolumes', '_'): 'describe_stored_iscsi_volumes',
  38. ('DescribeStorediSCSIVolumes', '-'): 'describe-stored-iscsi-volumes',
  39. ('CreateStorediSCSIVolume', '_'): 'create_stored_iscsi_volume',
  40. ('CreateStorediSCSIVolume', '-'): 'create-stored-iscsi-volume',
  41. }
  42. ScalarTypes = ('string', 'integer', 'boolean', 'timestamp', 'float', 'double')
  43. KSCORE_ROOT = os.path.dirname(os.path.abspath(__file__))
  44. # Used to specify anonymous (unsigned) request signature
  45. UNSIGNED = object()
  46. def xform_name(name, sep='_', _xform_cache=_xform_cache):
  47. """Convert camel case to a "pythonic" name.
  48. If the name contains the ``sep`` character, then it is
  49. returned unchanged.
  50. """
  51. if sep in name:
  52. # If the sep is in the name, assume that it's already
  53. # transformed and return the string unchanged.
  54. return name
  55. key = (name, sep)
  56. if key not in _xform_cache:
  57. if _special_case_transform.search(name) is not None:
  58. is_special = _special_case_transform.search(name)
  59. matched = is_special.group()
  60. # Replace something like ARNs, ACLs with _arns, _acls.
  61. name = name[:-len(matched)] + sep + matched.lower()
  62. s1 = _first_cap_regex.sub(r'\1' + sep + r'\2', name)
  63. s2 = _number_cap_regex.sub(r'\1' + sep + r'\2', s1)
  64. transformed = _end_cap_regex.sub(r'\1' + sep + r'\2', s2).lower()
  65. _xform_cache[key] = transformed
  66. return _xform_cache[key]