This file is indexed.

/usr/share/pyshared/cfflib/loadsave.py is in python-cfflib 2.0.5-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
""" Routines to load, save a connectome files """

#import cfflib_modified as cfflib
#import cfflib

import cfflib2 as cf

import os.path as op
from zipfile import ZipFile, ZIP_DEFLATED

def load(filename):
    """ Load a connectome file either from meta.cml (default)
    or from a zipped .cff file
    
    Parameter
    ---------
    filename : string
        File to load, either a connectome markup file with ending .cml
        or a zipped connectome file with ending .cff
        
    Notes
    -----
    By the file name ending, the appropriate load function is selected.
    They can be either .cml or .cff for unzipped or zipped connectome
    files respectively.
    """

    if isinstance(filename, basestring):
        # check if file exists
        from os.path import isfile, abspath
        
        if isfile(filename):
            if filename.endswith('.cff'):
                fname = abspath(filename)
                return _load_from_cff(fname)
            elif filename.endswith('.cml'):
                fname = abspath(filename)
                return _load_from_meta_cml(fname)
            else:
                raise RuntimeError('%s must end with either .cml or .cff' % filename)       
        else:
            raise RuntimeError('%s seems not to be a valid file string' % filename)
    else:
        raise RuntimeError('%s seems not to be a valid file string' % filename)


def _load_from_meta_cml(filename):
    """ Load connectome file from a meta.cml file. """
    
    with open(filename, 'r') as metacml:
        metastr = metacml.read()
        
    connectome = cf.parseString(metastr)
    # update references
    connectome._update_parent_reference()
    connectome.iszip = False
    connectome.fname = op.abspath(filename)
    # check if names are unique!
    connectome.check_names_unique()
    
    return connectome

def _load_from_url(url, to_filename):
    """ First downloads the connectome file to a file to_filename
    load it and return the reference to the connectome object
    
    Not tested.
    """
    
    from util import download    
    download(url, to_filename)
    return _load_from_cff(to_filename)
    
    
def _load_from_cff(filename, *args, **kwargs):
    """ Load connectome file given filename
    
        Returns
        -------
        connectome : ``Connectome``
                Connectome Instance
                
    """
    
    # XXX: take care to have allowZip64 = True (but not supported by unix zip/unzip, same for ubuntu?) ?
    _zipfile = ZipFile(filename, 'a', ZIP_DEFLATED)
    try:
        metadata_string = _zipfile.read('meta.cml')
    except: # XXX: what is the correct exception for read error?
        raise RuntimeError('Can not extract meta.cml from connectome file.')
    
    # create connectome instance
    connectome = cf.parseString(metadata_string)
    
    # update references
    connectome._update_parent_reference()
    
    # add additional attributes
    connectome.src = filename
    
    # it is from the zip file
    connectome.iszip = True
    # if it is a zip file, we can assume that the src paths are given relatively
    
    connectome._zipfile = _zipfile
    
    # check if all referenced container elements exist in the archive
    connectome.check_file_in_cff()
    
    # check if names are unique!
    connectome.check_names_unique()
    
    return connectome

def save_to_meta_cml(connectome, filename = 'meta.cml'):
    """ Stores a Connectome Markup File to filename """
    if connectome.get_connectome_meta() == None:
        print "ERROR - there is no connectome metadata in this connectome"
        return
    elif connectome.get_connectome_meta().title == None or connectome.get_connectome_meta().title == '':
        print "ERROR - the connectome metadata have to contain a unique title"
        return
    f = open(filename, 'w')
    f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
    connectome.export(f, 0, namespacedef_='xmlns="http://www.connectomics.org/cff-2" xmlns:cml="http://www.connectomics.org/cff-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dcterms="http://purl.org/dc/terms/" xsi:schemaLocation="http://www.connectomics.org/cff-2 connectome.xsd"')
    f.close()


def save_to_cff(connectome, filename):
    """ Save connectome file to new .cff file on disk """
    if connectome.get_connectome_meta() == None:
        print "ERROR - there is no connectome metadata in this connectome"
        return
    elif connectome.get_connectome_meta().title == None or connectome.get_connectome_meta().title == '':
        print "ERROR - the connectome metadata have to contain a unique title"
        return
    
    _newzip = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64 = True)
    
    allcobj = connectome.get_all()
    
    # tmpdir
    import tempfile
    import os
    tmpdir = tempfile.gettempdir()
    
    # check if names are unique!
    connectome.check_names_unique()

    for ele in allcobj:
        print "----"
        print "Storing element: ", str(ele)
        
        # discover the relative path to use for the save
        if hasattr(ele, 'src'):
            if ele.src == '':
                wt = ele.get_unique_relpath()
                print "Created a unique path for element %s: %s" % (str(ele), wt)
            else:
                wt = ele.src
                print "Used .src attribute for relative path: %s" % wt
        else:            
            ele.src = ele.get_unique_relpath()
            wt = ele.src
            print "Element has no .src attribute. Create it and set it to %s" % ele.src
        
        if not hasattr(ele, 'data'):
            
            # Add if iszip is undefined
#            if not hasattr(connectome, 'iszip'):
#                connectome.iszip = False
            
            if connectome.iszip:
                # extract zip content and add it to new zipfile
                if not wt in connectome._zipfile.namelist():
                    msg = """There exists no file %s in the connectome file you want to save to
                    "Please create .data and set the attributes right
                    "according to the documentation"""
                    raise Exception(msg)
                else:
                    ftmp = connectome._zipfile.extract(wt)
            else:
                # connectome was not created from a zip
                # but for example in an IPython session
                
                # if now fname
                if not hasattr(connectome, 'fname'):
                    connectome.fname = op.abspath(filename)
                
                if hasattr(ele, 'tmpsrc'):
                    try:
                        ele.save()
                    except:
                        pass
                    ftmp = ele.tmpsrc
                else:
                    # save the element
                    try:
                        ele.save()
                    except:
                        # e.g. there is nothing to save exception
                        ftmp = op.join(op.dirname(connectome.fname), wt)

                # use the saved location
#                ftmp = ele.tmpsrc
                
                # create path coming from filesystem
                #ftmp = op.join(op.dirname(connectome.fname), wt)
                
                if not op.exists(ftmp):
                    msg = """There exists no file %s for element %s. 
                    "Cannot save connectome file. Please update the element or bug report if it should work.""" % (ftmp, str(ele))
                    raise Exception(msg)
            
            _newzip.write(ftmp, wt)
            
        else:
            
            # save content to a temporary file according to the objects specs
            ele.save()
            
            # get the path
            ftmp = ele.tmpsrc
            
            _newzip.write(ftmp, wt)
        
        if connectome.iszip:
            # remove file
            os.remove(ftmp)
        
        # update ele.src
        ele.src = wt
  
    # export and store meta.cml
    mpth = op.join(tmpdir, 'meta.cml')
    f = open(mpth, 'w')
    f.write(connectome.to_xml())
    f.close()
    
    _newzip.write(mpth, 'meta.cml')
    os.remove(mpth)
    
    _newzip.close()
    
    print "New connectome file written to %s " % op.abspath(filename)