Coverage for src/robotide/spec/specimporter.py: 57%

47 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:40 +0100

1# Copyright 2008-2015 Nokia Networks 

2# Copyright 2016- Robot Framework Foundation 

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 

16import builtins 

17import os 

18import shutil 

19import wx 

20 

21from .. import context 

22from ..action import ActionInfo 

23from ..pluginapi import Plugin 

24from ..publish import PUBLISHER, RideExecuteSpecXmlImport 

25from ..widgets import RIDEDialog 

26from .xmlreaders import get_name_from_xml 

27 

28_ = wx.GetTranslation # To keep linter/code analyser happy 

29builtins.__dict__['_'] = wx.GetTranslation 

30 

31 

32class SpecImporterPlugin(Plugin): 

33 

34 HEADER = _('Import Library Spec XML') 

35 

36 def enable(self): 

37 self.register_action(ActionInfo(_('Tools'), self.HEADER, 

38 self.execute_spec_import, position=83)) 

39 PUBLISHER.subscribe(self._ps_on_execute_spec_import, RideExecuteSpecXmlImport) 

40 

41 def disable(self): 

42 self.unsubscribe_all() 

43 self.unregister_actions() 

44 

45 def _ps_on_execute_spec_import(self, message): 

46 self.execute_spec_import() 

47 

48 def execute_spec_import(self): 

49 path = self._get_path_to_library_spec() 1bc

50 if self._is_valid_path(path): 1bc

51 self._store_spec(path) 1b

52 self._execute_namespace_update() 1b

53 

54 def _is_valid_path(self, path): 

55 return path and os.path.isfile(path) 

56 

57 def _execute_namespace_update(self): 

58 self.model.update_namespace() 

59 

60 def _get_path_to_library_spec(self): 

61 wildcard = (_('Library Spec XML|*.xml|All Files|*.*')) 

62 dlg = wx.FileDialog(self.frame, 

63 message=_('Import Library Spec XML'), 

64 wildcard=wildcard, 

65 defaultDir=self.model.default_dir) # DEBUG 

66 # , style=wx.OPEN) 

67 if dlg.ShowModal() == wx.ID_OK: 

68 path = dlg.GetPath() 

69 else: 

70 path = None 

71 dlg.Destroy() 

72 return path 

73 

74 def _store_spec(self, path): 

75 name = get_name_from_xml(path) 

76 if name: 

77 shutil.copy(path, os.path.join(context.LIBRARY_XML_DIRECTORY, name+'.xml')) 

78 message_box = RIDEDialog(title=_('Info'), 

79 message=_('Library "%s" imported\nfrom "%s"\nThis may require RIDE restart.') 

80 % (name, path), style=wx.OK | wx.ICON_INFORMATION) 

81 message_box.ShowModal() 

82 else: 

83 message_box = RIDEDialog(title=_('Import failed'), 

84 message=_('Could not import library from file "%s"') 

85 % path, style=wx.OK | wx.ICON_ERROR) 

86 message_box.ShowModal()