~fmos/zim/features

« back to all changes in this revision

Viewing changes to zim/notebook.py

  • Committer: Fabian Moser
  • Date: 2011-01-26 20:33:29 UTC
  • mfrom: (297.1.43 pyzim-trunk)
  • Revision ID: e-mail+launchpad@fabianmoser.at-20110126203329-9qhtvpsf169hb18y
Merged main branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
110
110
                lines = [line + '\n' for line in lines]
111
111
                self.file.writelines(lines)
112
112
 
 
113
        def get_default(self):
 
114
                '''Returns uri for the default notebook'''
 
115
                if self.default:
 
116
                        return self.default
 
117
                elif len(self) == 1:
 
118
                        return self[0]
 
119
                else:
 
120
                        return None
 
121
 
113
122
        def get_names(self):
114
123
                '''Generator function that yield tuples with the notebook
115
124
                name and the notebook path.
120
129
                                yield (name, path)
121
130
 
122
131
        def get_name(self, uri):
 
132
                '''Find the name for the notebook at 'uri' '''
123
133
                # TODO support for paths that turn out to be files
124
134
                file = Dir(uri).file('notebook.zim')
125
135
                if file.exists():
129
139
                return None
130
140
 
131
141
        def get_by_name(self, name):
 
142
                '''Get the uri for a notebook'''
132
143
                for n, path in self.get_names():
133
144
                        if n.lower() == name.lower():
134
145
                                return path
201
212
        or for the only notebook if there is only a single notebook
202
213
        in the list.
203
214
        '''
204
 
        default = None
205
 
        list = get_notebook_list()
206
 
        if list.default:
207
 
                default = list.default
208
 
        elif len(list) == 1:
209
 
                default = list[0]
 
215
        default = get_notebook_list().get_default()
210
216
 
211
217
        if default:
212
218
                if zim.fs.isfile(default):
1172
1178
 
1173
1179
                page.set_parsetree(tree)
1174
1180
 
1175
 
        def resolve_file(self, filename, path):
1176
 
                '''Resolves a file or directory path relative to a page. Returns a
1177
 
                File object. However the file does not have to exist.
 
1181
        def resolve_file(self, filename, path=None):
 
1182
                '''Resolves a file or directory path relative to a page or
 
1183
                Notebook. Returns a File object. However the file does not
 
1184
                have to exist.
1178
1185
 
1179
 
                File urls and paths that start with '~/' or '~user/' are considered
1180
 
                absolute paths and are returned unmodified.
 
1186
                File urls and paths that start with '~/' or '~user/' are
 
1187
                considered absolute paths and the corresponding File objects
 
1188
                are returned. Also handles windows absolute paths.
1181
1189
 
1182
1190
                In case the file path starts with '/' the the path is taken relative
1183
1191
                to the document root - this can e.g. be a parent directory of the
1184
 
                notebook. Defaults to the home dir.
 
1192
                notebook. Defaults to the filesystem root when no document root
 
1193
                is set.
1185
1194
 
1186
1195
                Other paths are considered attachments and are resolved relative
1187
 
                to the namespce below the page.
1188
 
 
1189
 
                Because this is used to resolve file links and is supposed to be
1190
 
                platform independent it tries to convert windows filenames to
1191
 
                unix equivalents.
 
1196
                to the namespace below the page. If no path is given but the
 
1197
                notebook has a root folder, this folder is used as base path.
1192
1198
                '''
 
1199
                assert isinstance(filename, basestring)
1193
1200
                filename = filename.replace('\\', '/')
1194
1201
                if filename.startswith('~') or filename.startswith('file:/'):
1195
1202
                        return File(filename)
1202
1209
                                # make absolute on unix
1203
1210
                        return File(filename)
1204
1211
                else:
1205
 
                        # TODO - how to deal with '..' in the middle of the path ?
1206
 
                        filepath = [p for p in filename.split('/') if len(p) and p != '.']
1207
 
                        if not filepath: # filename is e.g. "."
1208
 
                                return self.get_attachments_dir(path)
1209
 
                        pagepath = path.name.split(':')
1210
 
                        filename = filepath.pop()
1211
 
                        while filepath and filepath[0] == '..':
1212
 
                                if not pagepath:
1213
 
                                        print 'TODO: handle paths relative to notebook but outside notebook dir'
1214
 
                                        return File('/TODO')
1215
 
                                else:
1216
 
                                        filepath.pop(0)
1217
 
                                        pagepath.pop()
1218
 
                        pagename = ':'+':'.join(pagepath + filepath)
1219
 
                        dir = self.get_attachments_dir(Path(pagename))
1220
 
                        return dir.file(filename)
 
1212
                        if path:
 
1213
                                dir = self.get_attachments_dir(path)
 
1214
                        else:
 
1215
                                assert self.dir, 'Can not resolve relative path for notebook without root folder'
 
1216
                                dir = self.dir
 
1217
 
 
1218
                        return File((dir, filename))
1221
1219
 
1222
1220
        def relative_filepath(self, file, path=None):
1223
 
                '''Returns a filepath relative to either the documents dir (/xxx), the
1224
 
                attachments dir (if a path is given) (./xxx or ../xxx) or the users
1225
 
                home dir (~/xxx). Returns None otherwise.
 
1221
                '''Returns a filepath relative to either the documents dir
 
1222
                (/xxx), the attachments dir (if a path is given) or the notebook
 
1223
                folder (./xxx or ../xxx) or the users home dir (~/xxx).
 
1224
                Returns None otherwise.
1226
1225
 
1227
 
                Intended as the counter part of resolve_file().
1228
 
                Typically this function is used to present the user with readable paths
1229
 
                or to shorten the paths inserted in the wiki code. It is advised to
 
1226
                Intended as the counter part of resolve_file(). Typically this
 
1227
                function is used to present the user with readable paths or to
 
1228
                shorten the paths inserted in the wiki code. It is advised to
1230
1229
                use file uris for links that can not be made relative.
1231
1230
                '''
 
1231
                root = self.dir
1232
1232
                if path:
1233
 
                        root = self.dir
1234
1233
                        dir = self.get_attachments_dir(path)
1235
1234
                        if file.ischild(dir):
1236
1235
                                return './'+file.relpath(dir)
1240
1239
                                downpath = file.relpath(parent)
1241
1240
                                up = 1 + uppath.count('/')
1242
1241
                                return '../'*up + downpath
 
1242
                elif root and file.ischild(root):
 
1243
                                return './'+file.relpath(root)
1243
1244
 
1244
1245
                dir = self.get_document_root()
1245
1246
                if dir and file.ischild(dir):