Ejemplo n.º 1
0
	def _resolve_file(self, link):
		# TODO checks here are copy of notebook.resolve_file - should be single function
		#      to determine type of file link: attachment / document / other
		#      or generic function that takes attachment folder & document folder as args

		filename = link.replace('\\', '/')
		if filename.startswith('~') or filename.startswith('file:/'):
			file = File(filename)
		elif filename.startswith('/'):
			if self.notebook.document_root:
				file = self.notebook.document_root.file(filename)
			else:
				file = File(filename)
		elif is_win32_path_re.match(filename):
			if not filename.startswith('/'):
				filename = '/'+filename # make absolute on Unix
			file = File(filename)
		else:
			if self.source:
				dir = self.layout.attachments_dir(self.source)
			else:
				dir = self.layout.relative_root

			file = dir.resolve_file(filename)
				# Allow ../ here - limit resulting relative link
				# in self.file_object()

		return file
Ejemplo n.º 2
0
	def _resolve_file(self, link):
		# TODO checks here are copy of notebook.resolve_file - should be single function
		#      to determine type of file link: attachment / document / other
		#      or generic function that takes attachment folder & document folder as args

		filename = link.replace('\\', '/')
		if filename.startswith('~') or filename.startswith('file:/'):
			file = File(filename)
		elif filename.startswith('/'):
			if self.notebook.document_root:
				file = self.notebook.document_root.file(filename)
			else:
				file = File(filename)
		elif is_win32_path_re.match(filename):
			if not filename.startswith('/'):
				filename = '/'+filename # make absolute on Unix
			file = File(filename)
		else:
			if self.source:
				dir = self.layout.attachments_dir(self.source)
			else:
				dir = self.layout.relative_root

			file = File((dir.path, filename)) # XXX LocalDir --> File -- will need get_abspath to resolve

		return file
Ejemplo n.º 3
0
    def _link_file(self, link):
        # TODO checks here are copy of notebook.resolve_file - should be single function
        #      to determine type of file link: attachment / document / other
        #      or generic function that takes attachment folder & document folder as args

        filename = link.replace('\\', '/')
        if filename.startswith('~') or filename.startswith('file:/'):
            file = File(filename)
        elif filename.startswith('/'):
            if self.notebook.document_root:
                file = self.notebook.document_root.file(filename)
            else:
                file = File(filename)
        elif is_win32_path_re.match(filename):
            if not filename.startswith('/'):
                filename = '/' + filename  # make absolute on Unix
            file = File(filename)
        else:
            if self.source:
                dir = self.layout.attachments_dir(self.source)
            else:
                dir = self.layout.relative_root

            file = dir.resolve_file(filename)
            # Allow ../ here - limit resulting relative link
            # in self.file_object()

        return self.file_object(file)
Ejemplo n.º 4
0
    def resolve_file(self, filename, path=None):
        '''Resolve a file or directory path relative to a page or
		Notebook

		This method is intended to lookup file links found in pages and
		turn resolve the absolute path of those files.

		File URIs and paths that start with '~/' or '~user/' are
		considered absolute paths. Also windows path names like
		'C:\\user' are recognized as absolute paths.

		Paths that starts with a '/' are taken relative to the
		to the I{document root} - this can e.g. be a parent directory
		of the notebook. Defaults to the filesystem root when no document
		root is set. (So can be relative or absolute depending on the
		notebook settings.)

		Paths starting with any other character are considered
		attachments. If C{path} is given they are resolved relative to
		the I{attachment folder} of that page, otherwise they are
		resolved relative to the I{notebook folder} - if any.

		The file is resolved purely based on the path, it does not have
		to exist at all.

		@param filename: the (relative) file path or uri as string
		@param path: a L{Path} object for the page
		@returns: a L{File} object.
		'''
        assert isinstance(filename, str)
        filename = filename.replace('\\', '/')
        if filename.startswith('~') or filename.startswith('file:/'):
            return File(filename)
        elif filename.startswith('/'):
            dir = self.document_root or Dir('/')
            return dir.file(filename)
        elif is_win32_path_re.match(filename):
            if not filename.startswith('/'):
                filename = '/' + filename
                # make absolute on Unix
            return File(filename)
        else:
            if path:
                dir = self.get_attachments_dir(path)
                return File(
                    (dir.path, filename)
                )  # XXX LocalDir --> File -- will need get_abspath to resolve
            else:
                dir = Dir(self.layout.root.path)  # XXX
                return File((dir, filename))
Ejemplo n.º 5
0
	def spawn(self, args, callback=None):
		if callback:
			logger.warn('os.startfile does not support a callback')

		for arg in args:
			if isinstance(arg, (zim.fs.File, zim.fs.Dir)):
				path = os.path.normpath(arg.path)
			elif is_uri_re.match(arg) and not is_win32_path_re.match(arg):
				# URL or e.g. mailto: or outlook: URI
				path = unicode(arg)
			else:
				# must be file
				path = os.path.normpath(unicode(arg))

			logger.info('Opening with os.startfile: %s', path)
			os.startfile(path)
Ejemplo n.º 6
0
    def spawn(self, args, callback=None):
        if callback:
            logger.warn('os.startfile does not support a callback')

        for arg in args:
            if isinstance(arg, (zim.fs.File, zim.fs.Dir)):
                path = os.path.normpath(arg.path)
            elif is_uri_re.match(arg) and not is_win32_path_re.match(arg):
                # URL or e.g. mailto: or outlook: URI
                path = unicode(arg)
            else:
                # must be file
                path = os.path.normpath(unicode(arg))

            logger.info('Opening with os.startfile: %s', path)
            os.startfile(path)
Ejemplo n.º 7
0
	def resolve_file(self, filename, path):
		'''Resolves a file or directory path relative to a page. Returns a
		File object. However the file does not have to exist.

		File urls and paths that start with '~/' or '~user/' are considered
		absolute paths and are returned unmodified.

		In case the file path starts with '/' the the path is taken relative
		to the document root - this can e.g. be a parent directory of the
		notebook. Defaults to the home dir.

		Other paths are considered attachments and are resolved relative
		to the namespce below the page.

		Because this is used to resolve file links and is supposed to be
		platform independent it tries to convert windows filenames to
		unix equivalents.
		'''
		filename = filename.replace('\\', '/')
		if filename.startswith('~') or filename.startswith('file:/'):
			return File(filename)
		elif filename.startswith('/'):
			dir = self.get_document_root() or Dir('~')
			return dir.file(filename)
		elif is_win32_path_re.match(filename):
			if not filename.startswith('/'):
				filename = '/'+filename
				# make absolute on unix
			return File(filename)
		else:
			# TODO - how to deal with '..' in the middle of the path ?
			filepath = [p for p in filename.split('/') if len(p) and p != '.']
			if not filepath: # filename is e.g. "."
				return self.get_attachments_dir(path)
			pagepath = path.name.split(':')
			filename = filepath.pop()
			while filepath and filepath[0] == '..':
				if not pagepath:
					print 'TODO: handle paths relative to notebook but outside notebook dir'
					return File('/TODO')
				else:
					filepath.pop(0)
					pagepath.pop()
			pagename = ':'+':'.join(pagepath + filepath)
			dir = self.get_attachments_dir(Path(pagename))
			return dir.file(filename)
Ejemplo n.º 8
0
    def spawn(self, args, callback=None):
        if callback:
            logger.warn('os.startfile does not support a callback')

        for arg in args:
            if isinstance(arg, (zim.fs.File, zim.fs.Dir)):
                path = os.path.normpath(arg.path)
            elif is_uri_re.match(arg) and not is_win32_path_re.match(arg):
                # URL or e.g. mailto: or outlook: URI
                path = str(arg)
            else:
                # must be file
                path = os.path.normpath(str(arg))

            logger.info('Opening with os.startfile: %s', path)
            if TEST_MODE:
                TEST_MODE_RUN_CB((self.__class__.__name__, url))
            else:
                os.startfile(path)
Ejemplo n.º 9
0
    def spawn(self, args, callback=None):
        if callback:
            raise NotImplementedError(
                'os.startfile does not support a callback')

        for arg in args:
            if isinstance(arg, (zim.fs.File, zim.fs.Dir)):
                path = os.path.normpath(arg.path).replace(
                    '/', SEP)  # msys can use '/' instead of '\\'
            elif is_uri_re.match(arg) and not is_win32_path_re.match(arg):
                # URL or e.g. mailto: or outlook: URI
                path = str(arg)
            else:
                # must be file
                path = os.path.normpath(str(arg)).replace(
                    '/', SEP)  # msys can use '/' instead of '\\'

            logger.info('Opening with os.startfile: %s', path)
            if TEST_MODE:
                TEST_MODE_RUN_CB((self.__class__.__name__, path))
            else:
                os.startfile(path)