forked from MikhailPasechnik/animation_cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.py
110 lines (89 loc) · 3.27 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright 2018 by Mikhail Pasechnik. All Rights Reserved.
#
# This library is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
"""Implements base class for animation cache and a factory
Cache class encapsulates export logic of an entity.
Cache Importer encapsulates import logic of an entity.
CacheFactory class encapsulates cache class instantiation logic and
used by cache manager in doExport and doImport routines if none cache provided
to get all cache from known location or from scene.
"""
import os
import uuid
import json
import logging
import datetime
import getpass
import animation_cache
logger = logging.getLogger(__name__)
class CacheFactory(object):
"""Base class for cache factory"""
@classmethod
def fromFile(cls, directory, dataFile=None):
"""Returns cache instances from data file"""
cache = []
if not dataFile:
dataFile = os.path.join(directory, animation_cache.DATA_FILE_NAME)
if os.path.isfile(dataFile):
with open(dataFile, 'r') as f:
data = json.load(f)
cache = [
cls.fromData(d, directory) for d in
data.values()
]
return [c for c in cache if c]
@classmethod
def fromData(cls, data, directory):
return Cache(**data)
@classmethod
def fromEntity(cls, entity):
if type(entity) == dict:
return Cache(**entity)
@classmethod
def fromScene(cls):
raise NotImplementedError
class Cache(dict):
"""Base class for cache"""
type = None
def write(self, directory, fstart, fend, samples=1, padding=4, **kwargs):
"""Writes actual cache"""
# Provide basic information about this cache
self.update({
'id': str(uuid.uuid4()),
'type': self.type,
'datetime': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'fstart': fstart,
'fend': fend,
'samples': samples,
'padding': padding,
'user': getpass.getuser(),
'files': [],
})
return self
class CacheImporter(dict):
def __init__(self, importVariants=None, **data):
super(CacheImporter, self).__init__(**data)
self._importVariants = None
self.importVariants = importVariants or []
@property
def type(self):
return self.get('type')
@property
def importVariants(self):
"""Used in import process
where first element treated as a main variant to import"""
return self._importVariants
@importVariants.setter
def importVariants(self, value):
self._importVariants = value
def import_(self, **kwargs):
raise NotImplementedError