# -*- coding: utf-8 -*-
"""
<DefineSource>
@Date : Fri Nov 14 13:20:38 2014 \n
@Author : Erwan Ledoux \n\n
</DefineSource>
The Writer is a quick object to write from a LoadedReadVariable.
"""
#<DefineAugmentation>
import ShareYourSystem as SYS
BaseModuleStr="ShareYourSystem.Standards.Interfacers.Loader"
DecorationModuleStr="ShareYourSystem.Standards.Classors.Classer"
SYS.setSubModule(globals())
#</DefineAugmentation>
#<ImportSpecificModules>
import json
import yaml
#</ImportSpecificModules>
#<DefineClass>
@DecorationClass()
class WriterClass(BaseClass):
#Definition
RepresentingKeyStrsList=[
'WritingStoreVariable',
'WritingLoadBool'
]
def default_init(self,
_WritingStoreVariable=None,
_WritingLoadBool=False,
**_KwargVariablesDict
):
#Call the parent __init__ method
BaseClass.__init__(self,**_KwargVariablesDict)
def do_write(self):
#debug
'''
self.debug([
('Before file'),
('self.',self,[
'FolderingPathStr',
])
])
'''
#file first
self.file(_ModeStr='w')
#debug
'''
self.debug([
('After file'),
('self.',self,[
'FolderingPathStr',
'WritingLoadBool'
])
])
'''
#Check
if self.WritingLoadBool:
self.WritingStoreVariable=self.LoadedReadVariable
#debug
'''
self.debug(('self.',self,[
'FiledHardVariable',
'FolderingPathStr',
'LoadingFormatStr',
'WritingStoreVariable'
]))
'''
#Check
if self.LoadingFormatStr=='txt':
#Read the FiledHardVariable
self.FiledHardVariable.write(self.WritingStoreVariable)
elif self.LoadingFormatStr=='json':
#debug
'''
self.debug(
[
'we write in json...',
'json.dumps(self.WritingStoreVariable,indent=2) is '+str(json.dumps(self.WritingStoreVariable,indent=2))
]
)
'''
#Use the json decoder
self.FiledHardVariable.write(
json.dumps(self.WritingStoreVariable,indent=2)
)
elif self.LoadingFormatStr=='yaml':
#debug
'''
print('We yamelized !')
print('self.FiledHardVariable is ',self.FiledHardVariable)
print('self.WritingStoreVariable is ',self.WritingStoreVariable)
print('')
'''
#Use the json decoder
self.FiledHardVariable.write(
yaml.dump(self.WritingStoreVariable,indent=2)
)
#Return self
#return self
#</DefineClass>
View the Writer sources on Github
#ImportModules
import ShareYourSystem as SYS
from ShareYourSystem.Standards.Interfacers import Writer
#Definition of an instance Writer and make it find the current dir
MyWriter=Writer.WriterClass().write("hello",**{
'FolderingPathStr':Writer.LocalFolderPathStr,
'FilingKeyStr':'MyFile.txt',
'FilingModeStr':'w'
}
)
#Definition the AttestedStr
SYS._attest(
[
'MyWriter is '+SYS._str(MyWriter)
]
)
#Print
>>>
*****Start of the Attest *****
MyWriter is < (WriterClass), 4540432528>
/{
/ '<New><Instance>IdInt' : 4540432528
/ '<Spe><Class>WritingLoadBool' : False
/ '<Spe><Instance>WritingStoreVariable' : hello
/}
*****End of the Attest *****