-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHwpWrapper.py
More file actions
106 lines (81 loc) · 2.78 KB
/
HwpWrapper.py
File metadata and controls
106 lines (81 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from . import HwpUtils
from ._HwpRegistery import HwpSecurityReg
class _InheritHwp:
_hwp = None
@classmethod
def __init__(cls, hwp):
cls._hwp = hwp
class _HRun(_InheritHwp):
@classmethod
def __getattr__(cls, name):
return lambda: cls._hwp.HAction.Run(name)
class _HParameterSet(_InheritHwp):
_in_action = False
def __init__(self, hwp, hparam, action_obj):
super().__init__(hwp)
self._hparameter = getattr(self.__class__._hwp.HParameterSet, hparam)
self._haction = action_obj
def __enter__(self):
cls = self.__class__
if cls._in_action:
raise RuntimeError(
f"Do not open the new HParameterSet context until the prior one is closed: {self._haction}")
cls._hwp.HAction.GetDefault(self._haction, self._hparameter.HSet)
cls._in_action = True
return self._hparameter
def __exit__(self, *_):
cls = self.__class__
cls._hwp.HAction.Execute(self._haction, self._hparameter.HSet)
cls._in_action = False
del self
class _HUtils:
def __init__(self, hwp_wrapper):
self._wrap = hwp_wrapper
def __getattr__(self, name):
return lambda *args, **kwargs: (
getattr(HwpUtils, name)(self._wrap, *args, **kwargs))
def _register_hwp(hwp):
hwp.RegisterModule("FilePathCheckDLL", HwpSecurityReg.name.value)
return hwp
class HwpWrapper:
def __init__(self, hwp):
self._hwp = _register_hwp(hwp)
self._run = _HRun(self._hwp)
self._util = _HUtils(self)
def __bool__(self):
try:
self._hwp.XHwpDocuments.Active_XHwpDocument.DocumentID
except:
return False
else:
return True
def __getattr__(self, name):
return getattr(self._hwp, name)
def __del__(self):
try:
self._hwp.Clear(1)
self._hwp.Quit()
except:
pass
def __str__(self):
if self:
return self._hwp.XHwpDocuments.Active_XHwpDocument.FullName
else:
return ''
def _Release(self):
self.Visible(True)
self._hwp = None
def Visible(self, option=True):
if self:
self._hwp.XHwpWindows.Item(0).Visible = option
def Open(self, filepath):
self._hwp.Open(filepath,
arg="versionwarning:False;forceopen:True;suspendpassword:True")
def HParameterSet(self, hparam, action_obj):
return _HParameterSet(self._hwp, str(hparam), str(action_obj))
@property
def Run(self):
return self._run
@property
def Util(self):
return self._util