@@ -59,45 +59,59 @@ def _extract_match(t):
59
59
# hint attribute, text content
60
60
return m .group (1 ), m .group (2 )
61
61
62
-
63
- def _solve_ship (fitting , sMkt , b_localized ):
64
- # type: (minidom.Element, Market, bool) -> Fit
65
- """ NOTE: Since it is meaningless unless a correct ship object can be constructed,
66
- process flow changed
67
- """
68
- # ------ Confirm ship
69
- # <localized hint="Maelstrom">Maelstrom</localized>
70
- shipType = fitting .getElementsByTagName ("shipType" )[0 ].getAttribute ("value" )
62
+ def doIt (text , b_localized ):
63
+ # type: (str, bool) -> tuple[str, str|None]
71
64
anything = None
72
65
if b_localized :
73
66
try :
74
67
# expect an official name, emergency cache
75
- shipType , anything = _extract_match (shipType )
68
+ text , anything = _extract_match (text )
76
69
except ExtractingError :
77
70
pass
78
71
72
+ return text , anything
73
+
74
+ def _solve (name , altName , handler ):
75
+ # type: (str, str|None, function) -> object
79
76
limit = 2
80
- ship = None
77
+ subject = None
81
78
while True :
82
79
must_retry = False
83
80
try :
84
- try :
85
- ship = Ship (sMkt .getItem (shipType ))
86
- except ValueError :
87
- ship = Citadel (sMkt .getItem (shipType ))
81
+ subject = handler (name )
88
82
except (KeyboardInterrupt , SystemExit ):
89
83
raise
90
84
except Exception as e :
91
- pyfalog .warning ("Caught exception on _solve_ship " )
92
- pyfalog .error (e )
85
+ # pyfalog.warning("Caught exception on _solve ")
86
+ pyfalog .error ("Caught exception on _solve:: {}" , e )
93
87
limit -= 1
94
88
if limit == 0 :
95
89
break
96
- shipType = anything
90
+ name = altName
97
91
must_retry = True
98
92
if not must_retry :
99
93
break
100
94
95
+ return subject
96
+
97
+ def _solve_ship (fitting , sMkt , b_localized ):
98
+ # type: (minidom.Element, Market, bool) -> Fit
99
+ """ NOTE: Since it is meaningless unless a correct ship object can be constructed,
100
+ process flow changed
101
+ """
102
+ def handler (name ):
103
+ try :
104
+ return Ship (sMkt .getItem (name ))
105
+ except ValueError :
106
+ return Citadel (sMkt .getItem (name ))
107
+
108
+ # ------ Confirm ship
109
+ # <localized hint="Maelstrom">Maelstrom</localized>
110
+ shipType , anything = doIt (
111
+ fitting .getElementsByTagName ("shipType" )[0 ].getAttribute ("value" ), b_localized
112
+ )
113
+ ship = _solve (shipType , anything , handler )
114
+
101
115
if ship is None :
102
116
raise Exception ("cannot resolve ship type." )
103
117
@@ -116,37 +130,17 @@ def _solve_ship(fitting, sMkt, b_localized):
116
130
117
131
def _solve_module (hardware , sMkt , b_localized ):
118
132
# type: (minidom.Element, Market, bool) -> Item
119
- moduleName = hardware .getAttribute ("base_type" ) or hardware .getAttribute ("type" )
120
- emergency = None
121
- if b_localized :
122
- try :
123
- # expect an official name, emergency cache
124
- moduleName , emergency = _extract_match (moduleName )
125
- except ExtractingError :
126
- pass
127
-
128
- item = None
129
- limit = 2
130
- while True :
131
- must_retry = False
132
- try :
133
- item = sMkt .getItem (moduleName , eager = "group.category" )
134
- if not item :
135
- raise ValueError (f"{ moduleName } is not valid" )
136
- pyfalog .info ('_solve_module - sMkt.getItem: {}' , item )
137
- except (KeyboardInterrupt , SystemExit ):
138
- raise
139
- except Exception as e :
140
- pyfalog .warning ("Caught exception on _solve_module, name:{}" , moduleName )
141
- pyfalog .error (e )
142
- limit -= 1
143
- if limit == 0 :
144
- break
145
- moduleName = emergency
146
- must_retry = True
147
- if not must_retry and item :
148
- break
149
-
133
+ def handler (name ):
134
+ item = sMkt .getItem (name , eager = "group.category" )
135
+ if not item :
136
+ raise ValueError (f'"{ name } " is not valid' )
137
+ pyfalog .info ('_solve_module - sMkt.getItem: {}' , item )
138
+ return item
139
+
140
+ moduleName , emergency = doIt (
141
+ hardware .getAttribute ("base_type" ) or hardware .getAttribute ("type" ), b_localized
142
+ )
143
+ item = _solve (moduleName , emergency , handler )
150
144
if item is None :
151
145
raise Exception ("cannot resolve module or item." )
152
146
@@ -174,7 +168,7 @@ def importXml(text, progress):
174
168
failed = 0
175
169
176
170
pyfalog .info (
177
- f"importXml - localized fitting { 'detected ' if b_localized else 'is normaly ' } "
171
+ f"importXml - fitting is { 'localized ' if b_localized else 'normally ' } "
178
172
)
179
173
for fitting in fittings :
180
174
if progress and progress .userCancelled :
0 commit comments