55from ..exceptions import CTERAException
66from ..convert import fromxmlstr , toxmlstr
77from ..common import Device , delete_attrs
8- from ..lib import FileSystem , TempfileServices
8+ from ..lib import TempfileServices
9+ from ..lib .storage import commonfs , synfs
910from .base_command import BaseCommand
1011
1112
13+ logger = logging .getLogger ('cterasdk.edge' )
14+
15+
1216class Config (BaseCommand ):
1317 """ Edge Filer General Configuration APIs """
1418
15- def __init__ (self , edge ):
16- super ().__init__ (edge )
17- self ._filesystem = FileSystem .instance ()
18-
1919 def get_location (self ):
2020 """
2121 Get the location of the Edge Filer
@@ -31,7 +31,7 @@ def set_location(self, location):
3131 :param str location: New location to set
3232 :return str: The new location
3333 """
34- logging . getLogger ( 'cterasdk.edge' ) .info ('Configuring device location. %s' , {'location' : location })
34+ logger .info ('Configuring device location. %s' , {'location' : location })
3535 return self ._edge .api .put ('/config/device/location' , location )
3636
3737 def get_hostname (self ):
@@ -49,7 +49,7 @@ def set_hostname(self, hostname):
4949 :param str hostname: New hostname to set
5050 :return str: The new hostname
5151 """
52- logging . getLogger ( 'cterasdk.edge' ) .info ('Configuring device hostname. %s' , {'hostname' : hostname })
52+ logger .info ('Configuring device hostname. %s' , {'hostname' : hostname })
5353 return self ._edge .api .put ('/config/device/hostname' , hostname )
5454
5555 def import_config (self , config , exclude = None ):
@@ -64,19 +64,19 @@ def import_config(self, config, exclude=None):
6464 if isinstance (config , Device ):
6565 database = copy .deepcopy (config )
6666 elif isinstance (config , str ):
67- database = self .load_config (config )
67+ database = Config .load_config (config )
6868
6969 if exclude :
7070 delete_attrs (database , exclude )
7171
72- path = self . _filesystem .join (TempfileServices .mkdir (), f'{ self ._edge .session ().address } .xml' )
73- self . _filesystem . write (path , toxmlstr (database , True ).encode ('utf-8' ))
72+ path = commonfs .join (TempfileServices .mkdir (), f'{ self ._edge .session ().address } .xml' )
73+ synfs . overwrite (path , toxmlstr (database , True ).encode ('utf-8' ))
7474
7575 return self ._import_configuration (path )
7676
7777 def _import_configuration (self , path ):
78- self . _filesystem .properties (path )
79- logging . getLogger ( 'cterasdk.edge' ) .info ('Importing Edge Filer configuration.' )
78+ commonfs .properties (path )
79+ logger .info ('Importing Edge Filer configuration.' )
8080 with open (path , 'rb' ) as fd :
8181 response = self ._edge .api .form_data (
8282 '/config' ,
@@ -86,28 +86,29 @@ def _import_configuration(self, path):
8686 config = fd
8787 )
8888 )
89- logging . getLogger ( 'cterasdk.edge' ) .info ('Imported Edge Filer configuration.' )
89+ logger .info ('Imported Edge Filer configuration.' )
9090 return response
9191
92- def load_config (self , config ):
92+ @staticmethod
93+ def load_config (config ):
9394 """
9495 Load the Edge Filer configuration
9596
9697 :param str config: A string or a path to the Edge Filer configuration file
9798 """
9899 data = None
99- if self . _filesystem .exists (config ):
100- logging . getLogger ( 'cterasdk.edge' ) .info ('Reading the Edge Filer configuration from file. %s' , {'path' : config })
100+ if commonfs .exists (config ):
101+ logger .info ('Reading the Edge Filer configuration from file. %s' , {'path' : config })
101102 with open (config , 'r' , encoding = 'utf-8' ) as f :
102103 data = f .read ()
103104 else :
104105 data = config
105106
106107 database = fromxmlstr (data )
107108 if database :
108- logging . getLogger ( 'cterasdk.edge' ) .info ('Completed parsing the Edge Filer configuration. %s' , {'firmware' : database .firmware })
109+ logger .info ('Completed parsing the Edge Filer configuration. %s' , {'firmware' : database .firmware })
109110 return database
110- logging . getLogger ( 'cterasdk.edge' ) .error ("Failed parsing the Edge Filer's configuration." )
111+ logger .error ("Failed parsing the Edge Filer's configuration." )
111112 raise CTERAException ("Failed parsing the Edge Filer's configuration" )
112113
113114 def export (self , destination = None ):
@@ -118,11 +119,12 @@ def export(self, destination=None):
118119 File destination, defaults to the default directory
119120 """
120121 default_filename = self ._edge .host () + datetime .now ().strftime ('_%Y-%m-%dT%H_%M_%S' ) + '.xml'
121- directory , filename = self . _filesystem . generate_file_location (destination , default_filename )
122- logging . getLogger ( 'cterasdk.edge' ) .info ('Exporting configuration. %s' , {'host' : self ._edge .host ()})
122+ directory , filename = commonfs . generate_file_destination (destination , default_filename )
123+ logger .info ('Exporting configuration. %s' , {'host' : self ._edge .host ()})
123124 handle = self ._edge .api .handle ('/export' )
124- filepath = FileSystem .instance ().save (directory , filename , handle )
125- logging .getLogger ('cterasdk.edge' ).info ('Exported configuration. %s' , {'filepath' : filepath })
125+ filepath = synfs .write (directory , filename , handle )
126+ logger .info ('Exported configuration. %s' , {'filepath' : filepath })
127+ return filepath
126128
127129 def is_wizard_enabled (self ):
128130 """
@@ -145,5 +147,5 @@ def disable_wizard(self):
145147 return self ._set_wizard (False )
146148
147149 def _set_wizard (self , state ):
148- logging . getLogger ( 'cterasdk.edge' ) .info ('Disabling first time wizard' )
150+ logger .info ('Disabling first time wizard' )
149151 return self ._edge .api .put ('/config/gui/openFirstTimeWizard' , state )
0 commit comments