pythonLoaderFileType.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file pythonLoaderFileType.h
  10. * @author rdb
  11. * @date 2019-07-29
  12. */
  13. #ifndef PYTHONLOADERFILETYPE_H
  14. #define PYTHONLOADERFILETYPE_H
  15. #include "pandabase.h"
  16. #ifdef HAVE_PYTHON
  17. #include "loaderFileType.h"
  18. /**
  19. * This defines a Python-based loader plug-in. An instance of this can be
  20. * constructed by inheritance and explicitly registered, or it can be created
  21. * by passing in a pkg_resources.EntryPoint instance.
  22. *
  23. * @since 1.10.4
  24. */
  25. class PythonLoaderFileType : public LoaderFileType {
  26. public:
  27. PythonLoaderFileType();
  28. PythonLoaderFileType(std::string extension, PyObject *entry_point);
  29. ~PythonLoaderFileType();
  30. bool init(PyObject *loader);
  31. bool ensure_loaded() const;
  32. virtual std::string get_name() const override;
  33. virtual std::string get_extension() const override;
  34. virtual std::string get_additional_extensions() const override;
  35. virtual bool supports_compressed() const override;
  36. virtual bool supports_load() const override;
  37. virtual bool supports_save() const override;
  38. virtual PT(PandaNode) load_file(const Filename &path, const LoaderOptions &options,
  39. BamCacheRecord *record) const override;
  40. virtual bool save_file(const Filename &path, const LoaderOptions &options,
  41. PandaNode *node) const override;
  42. private:
  43. std::string _extension;
  44. std::string _additional_extensions;
  45. PyObject *_entry_point = nullptr;
  46. PyObject *_load_func = nullptr;
  47. PyObject *_save_func = nullptr;
  48. bool _supports_compressed = false;
  49. public:
  50. static TypeHandle get_class_type() {
  51. return _type_handle;
  52. }
  53. static void init_type() {
  54. LoaderFileType::init_type();
  55. register_type(_type_handle, "PythonLoaderFileType",
  56. LoaderFileType::get_class_type());
  57. }
  58. virtual TypeHandle get_type() const override {
  59. return get_class_type();
  60. }
  61. virtual TypeHandle force_init_type() override {
  62. init_type();
  63. return get_class_type();
  64. }
  65. private:
  66. static TypeHandle _type_handle;
  67. };
  68. #endif // HAVE_PYTHON
  69. #endif