~duplicity-team/duplicity/0.8-series

« back to all changes in this revision

Viewing changes to duplicity/backends/jottacloudbackend.py

  • Committer: kenneth at loafman
  • Date: 2018-07-23 16:32:30 UTC
  • Revision ID: kenneth@loafman.com-20180723163230-i226wdy5q2zzgfc7
* Fixed unadorned strings to unicode in duplicity/*/*
  - Some fixup due to shifting indenataion not matching PEP8.
  - Substituted for non-ascii char in jottlibbackend.py comment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding:utf-8 -*-
2
2
#
3
 
# Copyright 2014 Håvard Gulldahl
 
3
# Copyright 2014 HAvard Gulldahl
4
4
#
5
5
# in part based on dpbxbackend.py:
6
6
# Copyright 2013 jno <jno@pisem.net>
35
35
def get_jotta_device(jfs):
36
36
    jottadev = None
37
37
    for j in jfs.devices:  # find Jotta/Shared folder
38
 
        if j.name == 'Jotta':
 
38
        if j.name == u'Jotta':
39
39
            jottadev = j
40
40
    return jottadev
41
41
 
42
42
 
43
43
def get_root_dir(jfs):
44
44
    jottadev = get_jotta_device(jfs)
45
 
    root_dir = jottadev.mountPoints['Archive']
 
45
    root_dir = jottadev.mountPoints[u'Archive']
46
46
    return root_dir
47
47
 
48
48
 
49
49
def set_jottalib_logging_level(log_level):
50
 
    logger = logging.getLogger('jottalib')
 
50
    logger = logging.getLogger(u'jottalib')
51
51
    logger.setLevel(getattr(logging, log_level))
52
52
 
53
53
 
54
54
def set_jottalib_log_handlers(handlers):
55
 
    logger = logging.getLogger('jottalib')
 
55
    logger = logging.getLogger(u'jottalib')
56
56
    for handler in handlers:
57
57
        logger.addHandler(handler)
58
58
 
59
59
 
60
60
def get_duplicity_log_level():
61
 
    """ Get the current duplicity log level as a stdlib-compatible logging level"""
 
61
    u""" Get the current duplicity log level as a stdlib-compatible logging level"""
62
62
    duplicity_log_level = log.LevelName(log.getverbosity())
63
63
 
64
64
    # notice is a duplicity-specific logging level not supported by stdlib
65
 
    if duplicity_log_level == 'NOTICE':
66
 
        duplicity_log_level = 'INFO'
 
65
    if duplicity_log_level == u'NOTICE':
 
66
        duplicity_log_level = u'INFO'
67
67
 
68
68
    return duplicity_log_level
69
69
 
70
70
 
71
71
class JottaCloudBackend(duplicity.backend.Backend):
72
 
    """Connect to remote store using JottaCloud API"""
 
72
    u"""Connect to remote store using JottaCloud API"""
73
73
 
74
74
    def __init__(self, parsed_url):
75
75
        duplicity.backend.Backend.__init__(self, parsed_url)
79
79
            from jottalib import JFS
80
80
            from jottalib.JFS import JFSNotFoundError, JFSIncompleteFile
81
81
        except ImportError:
82
 
            raise BackendException('JottaCloud backend requires jottalib'
83
 
                                   ' (see https://pypi.python.org/pypi/jottalib).')
 
82
            raise BackendException(u'JottaCloud backend requires jottalib'
 
83
                                   u' (see https://pypi.python.org/pypi/jottalib).')
84
84
 
85
85
        # Set jottalib loggers to the same verbosity as duplicity
86
86
        duplicity_log_level = get_duplicity_log_level()
92
92
        # Will fetch jottacloud auth from environment or .netrc
93
93
        self.client = JFS.JFS()
94
94
 
95
 
        self.folder = self.get_or_create_directory(parsed_url.path.lstrip('/'))
96
 
        log.Debug("Jottacloud folder for duplicity: %r" % self.folder.path)
 
95
        self.folder = self.get_or_create_directory(parsed_url.path.lstrip(u'/'))
 
96
        log.Debug(u"Jottacloud folder for duplicity: %r" % self.folder.path)
97
97
 
98
98
    def get_or_create_directory(self, directory_name):
99
99
        root_directory = get_root_dir(self.client)
107
107
        # - Upload one file
108
108
        # - Retried if an exception is thrown
109
109
        resp = self.folder.up(source_path.open(), remote_filename)
110
 
        log.Debug('jottacloud.put(%s,%s): %s' % (source_path.name, remote_filename, resp))
 
110
        log.Debug(u'jottacloud.put(%s,%s): %s' % (source_path.name, remote_filename, resp))
111
111
 
112
112
    def _get(self, remote_filename, local_path):
113
113
        # - Get one file
114
114
        # - Retried if an exception is thrown
115
115
        remote_file = self.client.getObject(posixpath.join(self.folder.path, remote_filename))
116
 
        log.Debug('jottacloud.get(%s,%s): %s' % (remote_filename, local_path.name, remote_file))
117
 
        with open(local_path.name, 'wb') as to_file:
 
116
        log.Debug(u'jottacloud.get(%s,%s): %s' % (remote_filename, local_path.name, remote_file))
 
117
        with open(local_path.name, u'wb') as to_file:
118
118
            for chunk in remote_file.stream():
119
119
                to_file.write(chunk)
120
120
 
123
123
        # - Return a list of filenames
124
124
        # - Retried if an exception is thrown
125
125
        return list([f.name for f in self.folder.files()
126
 
                     if not f.is_deleted() and f.state != 'INCOMPLETE'])
 
126
                     if not f.is_deleted() and f.state != u'INCOMPLETE'])
127
127
 
128
128
    def _delete(self, filename):
129
129
        # - Delete one file
130
130
        # - Retried if an exception is thrown
131
131
        remote_path = posixpath.join(self.folder.path, filename)
132
132
        remote_file = self.client.getObject(remote_path)
133
 
        log.Debug('jottacloud.delete deleting: %s (%s)' % (remote_file, type(remote_file)))
 
133
        log.Debug(u'jottacloud.delete deleting: %s (%s)' % (remote_file, type(remote_file)))
134
134
        remote_file.delete()
135
135
 
136
136
    def _query(self, filename):
137
 
        """Get size of filename"""
 
137
        u"""Get size of filename"""
138
138
        #  - Query metadata of one file
139
139
        #  - Return a dict with a 'size' key, and a file size value (-1 for not found)
140
140
        #  - Retried if an exception is thrown
141
 
        log.Info('Querying size of %s' % filename)
 
141
        log.Info(u'Querying size of %s' % filename)
142
142
        remote_path = posixpath.join(self.folder.path, filename)
143
143
        try:
144
144
            remote_file = self.client.getObject(remote_path)
145
145
        except JFSNotFoundError:
146
 
            return {'size': -1}
 
146
            return {u'size': -1}
147
147
        return {
148
 
            'size': remote_file.size,
 
148
            u'size': remote_file.size,
149
149
        }
150
150
 
151
151
    def _close(self):
153
153
        pass
154
154
 
155
155
 
156
 
duplicity.backend.register_backend("jottacloud", JottaCloudBackend)
157
 
""" jottacloud is a Norwegian backup company """
 
156
duplicity.backend.register_backend(u"jottacloud", JottaCloudBackend)
 
157
u""" jottacloud is a Norwegian backup company """