_jpackage.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #*****************************************************************************
  2. # Copyright 2004-2008 Steve Menard
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. #*****************************************************************************
  17. import _jpype
  18. from . import _jclass
  19. class JPackage(object):
  20. def __init__(self, name):
  21. self.__name = name
  22. def __getattribute__(self, n):
  23. try:
  24. return object.__getattribute__(self, n)
  25. except:
  26. # not found ...
  27. # perhaps it is a class?
  28. subname = "{0}.{1}".format(self.__name, n)
  29. cc = _jpype.findClass(subname)
  30. if cc is None:
  31. # can only assume it is a sub-package then ...
  32. cc = JPackage(subname)
  33. else:
  34. cc = _jclass._getClassFor(cc)
  35. self.__setattr__(n, cc, True)
  36. return cc
  37. def __setattr__(self, n, v, intern=False):
  38. if not n[:len('_JPackage')] == '_JPackage' \
  39. and not intern: # NOTE this shadows name mangling
  40. raise RuntimeError("Cannot set attributes in a package {0}"
  41. .format(n))
  42. object.__setattr__(self, n, v)
  43. def __str__(self):
  44. return "<Java package {0}>".format(self.__name)
  45. def __call__(self, *arg, **kwarg):
  46. raise TypeError("Package {0} is not Callable".format(self.__name))