rename.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. '''
  2. Functions to create C++ names from MuPDF names.
  3. '''
  4. import os
  5. import jlib
  6. from . import util
  7. def snake_to_camel( name, initial):
  8. '''
  9. Converts foo_bar to FooBar or fooBar.
  10. >>> snake_to_camel( 'foo_bar', True)
  11. FooBar
  12. >>> snake_to_camel( 'foo_bar_q__a', False)
  13. fooBarQ_A
  14. '''
  15. # libclang can treat size_t oddly, which we work around when parsing MuPDF
  16. # headers, so we should not be given size_t.
  17. #
  18. assert name != 'size_t'
  19. items = name.split( '_')
  20. ret = ''
  21. for i, item in enumerate( items):
  22. if not item:
  23. item = '_'
  24. elif i or initial:
  25. item = item[0].upper() + item[1:].lower()
  26. ret += item
  27. return ret
  28. # Using camel case in function names seems to result in gcc errors when
  29. # compiling the code created by swig -python. e.g. in _wrap_vthrow_fn()
  30. #
  31. # mupdfcpp_swig.cpp: In function PyObject* _wrap_vthrow_fn(PyObject*, PyObject*)
  32. # mupdfcpp_swig.cpp:88571:15: error: invalid array assignment
  33. # arg3 = *temp;
  34. def internal( name):
  35. '''
  36. Used for internal names, e.g. exception types.
  37. '''
  38. return f'internal_{name}'
  39. def error_class( error_enum):
  40. '''
  41. Name of generated class for MuPDF `FZ_ERROR_*` enum.
  42. '''
  43. assert error_enum.startswith( 'FZ_ERROR_')
  44. return snake_to_camel( error_enum, initial=True)
  45. def c_fn( fnname):
  46. '''
  47. Returns fully-qualified name of MuPDF C function `fnname()`.
  48. '''
  49. return f'::{fnname}'
  50. def ll_fn( fnname):
  51. '''
  52. Returns name of low-level wrapper function for MuPDF C function `fnname()`,
  53. adding a `ctx` arg and converting MuPDF exceptions into C++ exceptions.
  54. '''
  55. assert not fnname.startswith( 'll_'), f'fnname={fnname}'
  56. return f'll_{fnname}'
  57. if name.startswith( 'pdf_'):
  58. return 'p' + name
  59. ret = f'{util.clip( name, "fz_")}'
  60. if ret in ('stdin', 'stdout', 'stderr'):
  61. #log( 'appending underscore to {ret=}')
  62. ret += '_'
  63. return ret
  64. def namespace():
  65. return 'mupdf'
  66. def namespace_ll_fn( fnname):
  67. '''
  68. Returns full-qualified name of low-level wrapper function for MuPDF C
  69. function `fnname()`, adding a `ctx` arg and converting MuPDF exceptions
  70. into C++ exceptions.
  71. '''
  72. return f'{namespace()}::{ll_fn(fnname)}'
  73. def fn( fnname):
  74. '''
  75. Returns name of wrapper function for MuPDF C function `fnname()`, using
  76. wrapper classes for args and return value.
  77. '''
  78. return fnname
  79. def namespace_fn( fnname):
  80. '''
  81. Returns fully-qualified name of wrapper function for MuPDF C function
  82. `fnname()`, using wrapper classes for args and return values.
  83. '''
  84. return f'{namespace()}::{fn(fnname)}'
  85. def class_( structname):
  86. '''
  87. Returns name of class that wraps MuPDF struct `structname`.
  88. '''
  89. structname = util.clip( structname, 'struct ')
  90. # Note that we can't return `structname` here because this will end up with
  91. # SWIG complaining like:
  92. #
  93. # Error: 'pdf_xref' is multiply defined in the generated target language module
  94. #
  95. # - because SWIG internally puts everything into a single namespace.
  96. #
  97. #return structname
  98. return snake_to_camel( structname, initial=True)
  99. if structname.startswith( 'fz_'):
  100. return snake_to_camel( util.clip( structname, 'fz_'), initial=True)
  101. elif structname.startswith( 'pdf_'):
  102. # Retain Pdf prefix.
  103. return snake_to_camel( structname, initial=True)
  104. def namespace_class( structname):
  105. '''
  106. Returns fully-qualified name of class that wraps MuPDF struct `structname`.
  107. '''
  108. return f'{namespace()}::{class_(structname)}'
  109. def method( structname, fnname):
  110. '''
  111. Returns name of class method that wraps MuPDF function `fnname()`.
  112. '''
  113. return fnname
  114. if structname:
  115. structname = structname.lstrip( 'struct ')
  116. assert structname is None or structname.startswith( ('fz_', 'pdf_'))
  117. ret = util.clip( fnname, ('fz_', 'pdf_'))
  118. if ret in ('stdin', 'stdout', 'stderr'):
  119. jlib.log( 'appending underscore to {ret=}')
  120. ret += '_'
  121. return ret