Example #1
0
def can_tidy(config_dict, f_name, mime_type):
	"""
	Checks whether some file type pertains to this plugin (i.e., can possibly be tidied).
	
	Keyword arguments:
	
	config_dict -- The configuration dictionary describing the user preferences (see config_dict.py)
	f_name -- File's name
	mime_type -- gedit's MIME type for the content	
	"""
	log_utils.debug('checking if can tidy %s with mime type %s' % (f_name, mime_type))
	
	v = config_dict[consts.type_config_category]
	
	if v == consts.mime_type_config:
		return _can_tidy_mime_type(mime_type)		
	if v == consts.ext_type_config:
		return _can_tidy_ext(config_dict, f_name)
	if v == consts.all_type_config:
		return _can_tidy_all_type()		

	log_utils.warn('can\'t figure out type_config_category %s in config dict' % v)

	assert False

	return True			
Example #2
0
    def on_output_pane_row_activated(self, line, col, type_, what):
        target_uri = self._plugin.output_pane.target_uri

        log_utils.debug('row activated for  %s %s %s %s %s to output box' %
                        (target_uri, line, col, type_, what))

        uri = self._window.get_active_document().get_uri()

        if uri != target_uri:
            self._flash_message('Please switch to %s' % target_uri)

            return

        if line == None:
            assert col == None, col

            return

        assert col != None, col

        view = self._window.get_active_view()

        try:
            gtk_utils.scroll_view_to_line_col(view, line, col)
        except Exception, inst:
            log_utils.warn('failed to scroll')

            log_utils.warn(inst)

            self._flash_message('Huh? Can\'t scroll to this position...')
Example #3
0
	def _select_loop(self):
		out_fd = self._pr.stdout.fileno()
		err_fd = self._pr.stderr.fileno()
		
		read_txt = 'dummy'
		err_txt = 'dummy'

		while True:
			select_list = _make_select_list(read_txt, err_txt, out_fd, err_fd)
			
			if select_list == []:
				return

			read_list, write_list, except_list = select.select(select_list, [], select_list)
			
			assert write_list == [], str(write_list)
			
			assert except_list == []
			
			for fd in except_list:
				log_utils.warn('proc_dispatch::select_loop calling except function')
				self._except_fn()
				
				return
	
			if out_fd in read_list:
				log_utils.debug('proc_dispatch::select_loop calling read function')
				read_txt = os.read(out_fd,  _buf_size)
				self._read_fn(_to_none_if_empty(read_txt))
				
			if	err_fd in read_list:
				log_utils.debug('proc_dispatch::select_loop calling err function')
				err_txt = os.read(err_fd, _buf_size)
				self._err_fn(_to_none_if_empty(err_txt))					
Example #4
0
def can_tidy(config_dict, f_name, mime_type):
    """
	Checks whether some file type pertains to this plugin (i.e., can possibly be tidied).
	
	Keyword arguments:
	
	config_dict -- The configuration dictionary describing the user preferences (see config_dict.py)
	f_name -- File's name
	mime_type -- gedit's MIME type for the content	
	"""
    log_utils.debug('checking if can tidy %s with mime type %s' %
                    (f_name, mime_type))

    v = config_dict[consts.type_config_category]

    if v == consts.mime_type_config:
        return _can_tidy_mime_type(mime_type)
    if v == consts.ext_type_config:
        return _can_tidy_ext(config_dict, f_name)
    if v == consts.all_type_config:
        return _can_tidy_all_type()

    log_utils.warn('can\'t figure out type_config_category %s in config dict' %
                   v)

    assert False

    return True
Example #5
0
	def on_output_pane_row_activated(self, line, col, type_, what):
		target_uri = self._plugin.output_pane.target_uri
	
		log_utils.debug('row activated for  %s %s %s %s %s to output box' % (target_uri, line, col, type_, what))
		
		uri = self._window.get_active_document().get_uri()
		
		if uri != target_uri:
			self._flash_message('Please switch to %s' % target_uri)
	
			return
			
		if line == None:
			assert col == None, col
			
			return
			
		assert col != None, col
		
		view = self._window.get_active_view()

		try:
			gtk_utils.scroll_view_to_line_col(view, line, col)
		except Exception, inst:
			log_utils.warn('failed to scroll')
			
			log_utils.warn(inst)
			
			self._flash_message('Huh? Can\'t scroll to this position...')
Example #6
0
def read_config_dict():
	"""
	Reads the configuration dictionary from a predefined file (defined in consts.py).
	"""
	log_utils.debug('reading config dict')

	data_dir = gen_utils.data_dir()	

	d = _default_config_dict()

	f_name = os.path.join(data_dir, consts.config_f_name)

	try:
		f = open(f_name, 'r')
		d = opt_stream_utils.opt_stream_to_dict(f)					
		f.close()
	except Exception, inst:		
		log_utils.warn(str(inst))
		log_utils.warn('couldn\'t read config dict from %s' % f_name)
Example #7
0
def read_dict(f_name, use_default_on_err=False):
    """
	Returns an dictionary contained in a file.
	
	Keyword arguments:
	f_name -- File name containing the stuff.
	use_default_on_err (= True) -- Whether to return the default dict in case f_name could not be read/parsed.
	"""
    try:
        f = open(f_name, 'r')
        ret = _opt_stream_to_dict(f)
        f.close()

        return ret
    except Exception, inst:
        log_utils.warn(inst)

        if use_default_on_err:
            return names_dicts_to_dict(default_names_dicts())

        raise inst
Example #8
0
	def _on_view_file_opts(self, w):
		f_name = self._file_entry.get_text()
		
		try:
			f_dict = tidy_opt_utils.read_dict(f_name)
		except Exception, inst:
			parent = self
			flags = gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT
			type_ = gtk.MESSAGE_WARNING
			buttons = gtk.BUTTONS_OK
			log_utils.warn('can\'t view opts file')
			log_utils.warn(inst)
			msg = 'Couldn\'t read or parse file'
			
			d = gtk.MessageDialog(parent, flags, type_, buttons, msg)
			
			d.run()
			
			d.destroy()
			
			return
Example #9
0
def read_dict(f_name, use_default_on_err = False):
	"""
	Returns an dictionary contained in a file.
	
	Keyword arguments:
	f_name -- File name containing the stuff.
	use_default_on_err (= True) -- Whether to return the default dict in case f_name could not be read/parsed.
	"""
	try:
		f = open(f_name, 'r')
		ret = _opt_stream_to_dict(f)
		f.close()
		
		return ret
	except Exception, inst:
		log_utils.warn(inst)
		
		if use_default_on_err:
			return names_dicts_to_dict(default_names_dicts())
			
		raise inst
Example #10
0
    def _select_loop(self):
        out_fd = self._pr.stdout.fileno()
        err_fd = self._pr.stderr.fileno()

        read_txt = 'dummy'
        err_txt = 'dummy'

        while True:
            select_list = _make_select_list(read_txt, err_txt, out_fd, err_fd)

            if select_list == []:
                return

            read_list, write_list, except_list = select.select(
                select_list, [], select_list)

            assert write_list == [], str(write_list)

            assert except_list == []

            for fd in except_list:
                log_utils.warn(
                    'proc_dispatch::select_loop calling except function')
                self._except_fn()

                return

            if out_fd in read_list:
                log_utils.debug(
                    'proc_dispatch::select_loop calling read function')
                read_txt = os.read(out_fd, _buf_size)
                self._read_fn(_to_none_if_empty(read_txt))

            if err_fd in read_list:
                log_utils.debug(
                    'proc_dispatch::select_loop calling err function')
                err_txt = os.read(err_fd, _buf_size)
                self._err_fn(_to_none_if_empty(err_txt))
Example #11
0
	def __init__(self, what):
		log_utils.warn('raising exception %s' % what)
		self.value = what
Example #12
0
class window_helper:
    """
	The window-specific HTML plugin part.
	"""
    def __init__(self, plugin, window):
        log_utils.debug('Creating window helper')

        self._window = window
        self._plugin = plugin

        self._insert_menu()
        self._insert_configure_menu()

        log_utils.debug('Created window helper')

    def _flash_message(self, msg):
        self._window.get_statusbar().flash_message(112, msg)

    def deactivate(self):
        self._remove_configure_menu()
        self._remove_menu()

        self._window = None
        self._plugin = None
        self._action_group = None
        self._configure_action_group = None

    def _insert_menu(self):
        ui_str = """
<ui>
	<menubar name="MenuBar">
		<menu name="ToolsMenu" action="Tools">
			<placeholder name="ToolsOps_2">
				<separator/>
				<menuitem name="tidy" action="tidy"/>
				<menuitem name="tidy_check" action="tidy_check"/>
				<separator/>
			</placeholder>
			<placeholder name="ToolsOps_5">
				<menuitem name="configure_tidy" action="configure_tidy"/>
			</placeholder>
		</menu>
	</menubar>
</ui>
"""

        self._action_group = gtk.ActionGroup("html_tidy_plugin_actions")
        actions = [("tidy", None, _("_Tidy"), None,
                    _("Tidies HTML, XHTML, and XML"), self.on_tidy),
                   ("tidy_check", None, _("Tidy _Check"), None,
                    _("Checks HTML, XHTML, and XML"), self.on_tidy_check)]
        self._action_group.add_actions(actions)

        manager = self._window.get_ui_manager()

        manager.insert_action_group(self._action_group, -1)

        self._ui_id = manager.add_ui_from_string(ui_str)

    def _insert_configure_menu(self):
        ui_str = """
<ui>
	<menubar name="MenuBar">
		<menu name="ToolsMenu" action="Tools">
			<placeholder name="ToolsOps_5">
				<menuitem name="configure_tidy" action="configure_tidy"/>
			</placeholder>
		</menu>
	</menubar>
</ui>
"""

        self._configure_action_group = gtk.ActionGroup(
            "html_tidy_plugin_configure_actions")
        actions = [("configure_tidy", None, _("Configure Tidy..."), None,
                    _("Configures HTML, XHTML, and XML Checker"),
                    self._plugin.on_configure)]
        self._configure_action_group.add_actions(actions)

        manager = self._window.get_ui_manager()

        manager.insert_action_group(self._configure_action_group, -1)

        self._configure_ui_id = manager.add_ui_from_string(ui_str)

    def _remove_menu(self):
        manager = self._window.get_ui_manager()

        manager.remove_ui(self._ui_id)

        smanager.remove_action_group(self._action_group)

        manager.ensure_update()

    def _remove_configure_menu(self):
        manager = self._window.get_ui_manager()

        manager.remove_ui(self._configure_ui_id)

        smanager.remove_action_group(self._configure_action_group)

        manager.ensure_update()

    def _can_tidy(self):
        log_utils.debug('checking if can tidy')

        active_doc = self._window.get_active_document()

        if active_doc == None:
            log_utils.debug('No doc active - returning False')
            return False

        f_name = active_doc.get_uri()
        mime_type = active_doc.get_mime_type()

        log_utils.debug('active doc\'s name is %s' % f_name)
        log_utils.debug('active doc\'s mime type is %s' % mime_type)

        return file_types_filter.can_tidy(self._plugin.config_dict, f_name,
                                          mime_type)

    def update_ui(self):
        sensitive = self._can_tidy()

        self._action_group.set_sensitive(sensitive)

        self._configure_action_group.set_sensitive(True)

    def on_tidy(self, action):
        self._plugin.output_pane.clear()

        log_utils.debug('tidying')

        view = self._window.get_active_view()
        bf = view.get_buffer()

        non_white = gtk_utils.num_non_whites_till_cur(bf)
        text = gtk_utils.get_view_text(view)

        try:
            effective_opts_dict = config_dict.effective_opts_dict(
                self._plugin.config_dict)
            (s,
             report_items) = tidy_utils.tidy_the_stuff(text,
                                                       effective_opts_dict)
        except Exception, inst:
            self._flash_message(str(inst))

            return

        log_utils.debug('tidy checked; found %s' % len(report_items))

        if s == '':
            log_utils.warn('got empty tidied text')

            self._flash_message('failed to tidy')

            return

        doc = self._window.get_active_document()

        doc.set_text(s)

        log_utils.debug('set text')

        gtk_utils.cursor_to_non_whites(view, non_white)

        log_utils.debug('tidied')
Example #13
0
 def __init__(self, what):
     log_utils.warn('raising exception %s' % what)
     self.value = what