yaml: Represent weboob date[time] objects as timestamps
Unfortunately, I couldn't make timestamps always load as our date[time] objects, at least with CLoader.
This commit is contained in:
parent
da611a08f8
commit
3ffee483b0
1 changed files with 17 additions and 3 deletions
|
|
@ -18,11 +18,15 @@
|
||||||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import logging
|
|
||||||
|
import weboob.tools.date
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
from .iconfig import ConfigError, IConfig
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from yaml import CLoader as Loader
|
from yaml import CLoader as Loader
|
||||||
from yaml import CDumper as Dumper
|
from yaml import CDumper as Dumper
|
||||||
|
|
@ -30,12 +34,22 @@ except ImportError:
|
||||||
from yaml import Loader
|
from yaml import Loader
|
||||||
from yaml import Dumper
|
from yaml import Dumper
|
||||||
|
|
||||||
from .iconfig import IConfig, ConfigError
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['YamlConfig']
|
__all__ = ['YamlConfig']
|
||||||
|
|
||||||
|
|
||||||
|
class WeboobDumper(Dumper):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
WeboobDumper.add_representer(weboob.tools.date.date,
|
||||||
|
WeboobDumper.represent_date)
|
||||||
|
|
||||||
|
WeboobDumper.add_representer(weboob.tools.date.datetime,
|
||||||
|
WeboobDumper.represent_datetime)
|
||||||
|
|
||||||
|
|
||||||
class YamlConfig(IConfig):
|
class YamlConfig(IConfig):
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
self.path = path
|
self.path = path
|
||||||
|
|
@ -59,7 +73,7 @@ class YamlConfig(IConfig):
|
||||||
def save(self):
|
def save(self):
|
||||||
# write in a temporary file to avoid corruption problems
|
# write in a temporary file to avoid corruption problems
|
||||||
with tempfile.NamedTemporaryFile(dir=os.path.dirname(self.path), delete=False) as f:
|
with tempfile.NamedTemporaryFile(dir=os.path.dirname(self.path), delete=False) as f:
|
||||||
yaml.dump(self.values, f, Dumper=Dumper)
|
yaml.dump(self.values, f, Dumper=WeboobDumper)
|
||||||
os.rename(f.name, self.path)
|
os.rename(f.name, self.path)
|
||||||
|
|
||||||
def get(self, *args, **kwargs):
|
def get(self, *args, **kwargs):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue