def load_zim_stock_icons():
    '''Function to load zim custom stock icons for Gtk. Will load all
	icons found in the "pixmaps" folder with a stock name prefixed
	with "zim-", so "data/pixmaps/link.png" becomes the "zim-link"
	stock icon. Called directly when this module is loaded.
	'''
    factory = Gtk.IconFactory()
    factory.add_default()
    for dir in data_dirs(('pixmaps')):
        for basename in dir.list('*.png'):
            # not all installs have svg support, so only check png for now..
            name = 'zim-' + basename[:
                                     -4]  # e.g. checked-box.png -> zim-checked-box
            icon_theme = Gtk.IconTheme.get_default()
            try:
                pixbuf = icon_theme.load_icon(name, 24, 0)
            except:
                path = dir.file(basename).path
                pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)

            try:
                set = Gtk.IconSet(pixbuf)
                factory.add(name, set)
            except Exception:
                logger.exception(
                    'Got exception while loading application icons')
Esempio n. 2
0
def get_template(format, template):
	'''Returns a Template object for a template name, file path, or File object'''
	# NOTE: here the "category" needs to be a format at the same time !
	if isinstance(template, File):
		file = template
	else:
		if not is_path_re.match(template):
			file = None
			path = list(data_dirs(('templates', format)))
			path.reverse()
			for dir in path:
				for basename in dir.list():
					name = basename.rsplit('.')[0] # robust if no '.' in basename
					if name == template:
						file = dir.file(basename)
						if file.exists(): # is a file
							break

			if not file:
				file = File(template)
		else:
			file = File(template)

	logger.info('Loading template from: %s', file)
	if not file.exists():
		raise AssertionError, 'No such file: %s' % file

	basename, ext = file.basename.rsplit('.', 1)
	resources = file.dir.subdir(basename)

	return Template(file.readlines(), format, name=file.path, resources_dir=resources)
Esempio n. 3
0
def get_template(category, template):
	'''Returns a Template object for a template name or file path
	@param category: the template category (e.g. "html"). Use to resolve
	the template if a template name is given
	@param template: the template name or file path
	'''
	assert isinstance(template, basestring)

	if is_path_re.match(template):
		file = File(template)
	else:
		file = None
		for dir in data_dirs(('templates', category)):
			for basename in dir.list():
				name = basename.rsplit('.')[0] # robust if no '.' in basename
				if basename == template or name == template:
					file = dir.file(basename)
					if file.exists(): # is a file
						break
			if file and file.exists():
				break
		else:
			file = File(template)
			if not file.exists():
				raise PathLookupError, _('Could not find template "%s"') % template
					# T: Error message in template lookup

	if not file.exists():
		raise PathLookupError, _('No such file: %s') % file
			# T: Error message in template lookup

	logger.info('Loading template from: %s', file)
	#~ basename, ext = file.basename.rsplit('.', 1)
	#~ resources = file.dir.subdir(basename)
	return Template(file)
Esempio n. 4
0
def get_helper_applications(type):
	'''Returns a list of known applications that can be used as a helper
	of a certain type.
	Type can e.g. be 'web_browser', 'file_browser' or 'email_client'.
	'''
	# Be aware that X-Zim-AppType can be a list of types
	seen = set()
	helpers = []
	for dir in data_dirs('applications'):
		for basename in [n for n in dir.list() if n.endswith('.desktop')]:
			key = basename[:-8] # len('.desktop') == 8
			if key in seen:
				continue
			seen.add(key)
			entry = DesktopEntryFile(dir.file(basename))
			if entry.isvalid():
				if ('X-Zim-AppType' in entry['Desktop Entry']
				and type in entry['Desktop Entry']['X-Zim-AppType']):
					helpers.append(entry)

	if type == 'web_browser':
		for entry in get_applications('text/html'):
			if not entry.key in seen:
				helpers.append(entry)
				seen.add(entry.key)
	
	if not 'startfile' in seen:
		helpers.append( get_application('startfile') )

	helpers = [helper for helper in helpers if helper.tryexec()]
	return helpers
Esempio n. 5
0
def get_template(format, template):
    '''Returns a Template object for a template name, file path, or File object'''
    # NOTE: here the "category" needs to be a format at the same time !
    if isinstance(template, File):
        file = template
    else:
        if not is_path_re.match(template):
            file = None
            path = list(data_dirs(('templates', format)))
            path.reverse()
            for dir in path:
                for basename in dir.list():
                    name = basename.rsplit('.')[
                        0]  # robust if no '.' in basename
                    if name == template:
                        file = dir.file(basename)
                        if file.exists():  # is a file
                            break

            if not file:
                file = File(template)
        else:
            file = File(template)

    logger.info('Loading template from: %s', file)
    if not file.exists():
        raise AssertionError, 'No such file: %s' % file

    basename, ext = file.basename.rsplit('.', 1)
    resources = file.dir.subdir(basename)

    return Template(file.readlines(),
                    format,
                    name=file.path,
                    resources_dir=resources)
Esempio n. 6
0
def _application_dirs():
	# Generator for application directories, first check zim specific paths,
	# then general applications
	for dir in data_dirs('applications'):
		yield dir

	yield XDG_DATA_HOME.subdir('applications')

	for dir in XDG_DATA_DIRS:
		yield dir.subdir('applications')
Esempio n. 7
0
def _application_dirs():
    # Generator for application directories, first check zim specific paths,
    # then general applications
    for dir in data_dirs('applications'):
        yield dir

    yield XDG_DATA_HOME.subdir('applications')

    for dir in XDG_DATA_DIRS:
        yield dir.subdir('applications')
def get_side_by_side_app():
    for dir in data_dirs('helpers/compare_files/'):
        for name in dir.list():  # XXX need object list
            file = dir.file(name)
            if name.endswith('.desktop') and file.exists():
                app = DesktopEntryFile(file)
                if app.tryexec():
                    return app
    else:
        return None
Esempio n. 9
0
File: __init__.py Progetto: gdw2/zim
def get_side_by_side_app():
	for dir in data_dirs('helpers/compare_files/'):
		for name in dir.list(): # XXX need object list
			file = dir.file(name)
			if name.endswith('.desktop') and file.exists():
				app = DesktopEntryFile(file)
				if app.tryexec():
					return app
	else:
		return None
Esempio n. 10
0
def list_template_categories():
	'''Returns a list of categories (sub folders)'''
	dirs = data_dirs('templates')
	categories = set()
	for dir in dirs:
		for name in dir.list():
			## TODO list_objects would help here + a filter like type=Dir
			if dir.subdir(name).isdir():
				categories.add(name)

	return sorted(categories)
Esempio n. 11
0
def list_template_categories():
    '''Returns a list of categories (sub folders)'''
    dirs = data_dirs('templates')
    categories = set()
    for dir in dirs:
        for name in dir.list():
            ## TODO list_objects would help here + a filter like type=Dir
            if dir.subdir(name).isdir():
                categories.add(name)

    return sorted(categories)
Esempio n. 12
0
def list_templates(format):
	'''Returns a dict mapping template names to file paths.'''
	format = format.lower()
	templates = {}
	path = list(data_dirs(('templates', format)))
	path.reverse()
	for dir in path:
		for file in dir.list():
			i = file.rfind('.') # match begin of file extension
			if i >= 0:
				#~ templates[file[0:i]] = dir.file(file) FIXME
				import os
				templates[file[0:i]] = os.path.join(dir.path, file)
	return templates
Esempio n. 13
0
def list_templates(category):
	'''Returns a list of template names
	@param category: a category (sub folder) with tempaltes, e.g. "html"
	@returns: a list of 2-tuples of the template names and the file
	basename for the template file
	'''
	category = category.lower()
	templates = set()
	path = list(data_dirs(('templates', category)))
	path.reverse()
	for dir in path:
		for basename in dir.list():
			if dir.file(basename).exists(): # is a file
				name = basename.rsplit('.', 1)[0] # robust if no '.' in basename
				templates.add((name, basename))
	return sorted(templates)
Esempio n. 14
0
def list_templates(category):
	'''Returns a list of template names
	@param category: a category (sub folder) with tempaltes, e.g. "html"
	@returns: a list of 2-tuples of the template names and the file
	basename for the template file
	'''
	category = category.lower()
	templates = set()
	path = list(data_dirs(('templates', category)))
	path.reverse()
	for dir in path:
		for basename in dir.list():
			if dir.file(basename).exists(): # is a file
				name = basename.rsplit('.', 1)[0] # robust if no '.' in basename
				templates.add((name, basename))
	return sorted(templates)
def set_icon_search_path():
    icon_theme = Gtk.IconTheme.get_default()
    for dir in data_dirs():
        if dir.subdir('icons').exists():
            icon_theme.append_search_path(dir.subdir('icons').path)
Esempio n. 16
0
logger = logging.getLogger('zim.plugins')

# Extend path for importing and searching plugins
#
# Set C{__path__} for the C{zim.plugins} module. This determines what
# directories are searched when importing plugin packages in the
# C{zim.plugins} namespace.
#
# Originally this added to the C{__path__} folders based on C{sys.path}
# however this leads to conflicts when multiple zim versions are
# installed. By switching to XDG_DATA_HOME this conflict is removed
# by separating custom plugins and default plugins from other versions.
# Also this switch makes it easier to have a single instruction for
# users where to put custom plugins.

for dir in data_dirs('plugins'):
    __path__.append(dir.path)

__path__.append(__path__.pop(0))  # reshuffle real module path to the end

#~ print "PLUGIN PATH:", __path__


class PluginManager(ConnectorMixin, collections.Mapping):
    '''Manager that maintains a set of active plugins

	This class is the interface towards the rest of the application to
	load/unload plugins and to let plugins extend specific application
	objects.

	All object that want to instantiate new objects that are extendable