[gedit/wip/python3] Get snippets almost working.



commit 9349bdd557aa61aff81fcdc92ec9a6b3ed485ebf
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Sat Oct 27 19:31:55 2012 +0200

    Get snippets almost working.
    
    There is still an assertion closing the manager

 plugins/snippets/snippets/completion.py  |    2 +-
 plugins/snippets/snippets/document.py    |   60 +++++++++++++++---------------
 plugins/snippets/snippets/importer.py    |    2 +-
 plugins/snippets/snippets/library.py     |    4 +-
 plugins/snippets/snippets/manager.py     |   16 ++++----
 plugins/snippets/snippets/placeholder.py |    9 ++--
 plugins/snippets/snippets/snippet.py     |   17 ++++----
 7 files changed, 54 insertions(+), 56 deletions(-)
---
diff --git a/plugins/snippets/snippets/completion.py b/plugins/snippets/snippets/completion.py
index 8740516..25610f2 100644
--- a/plugins/snippets/snippets/completion.py
+++ b/plugins/snippets/snippets/completion.py
@@ -83,7 +83,7 @@ class Provider(GObject.Object, GtkSource.CompletionProvider):
 
                 if start.backward_word_start():
                         self.mark_position(start)
-                        return unicode(start.get_text(it), 'utf-8')
+                        return start.get_text(it)
                 else:
                         return None
 
diff --git a/plugins/snippets/snippets/document.py b/plugins/snippets/snippets/document.py
index 890216b..63283b3 100644
--- a/plugins/snippets/snippets/document.py
+++ b/plugins/snippets/snippets/document.py
@@ -327,37 +327,37 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                 bounds = buf.get_selection_bounds()
 
                 if bounds:
-                        u8 = unicode(buf.get_text(bounds[0], bounds[1], False), 'utf-8')
+                        u8 = buf.get_text(bounds[0], bounds[1], False)
 
                         return {'utf8': u8, 'noenc': self.string_in_native_doc_encoding(buf, u8)}
                 else:
-                        return u''
+                        return ''
 
         def env_get_current_word(self, buf):
                 start, end = buffer_word_boundary(buf)
                 enc = buf.get_encoding()
 
-                u8 = unicode(buf.get_text(start, end, False), 'utf-8')
+                u8 = buf.get_text(start, end, False)
 
                 return {'utf8': u8, 'noenc': self.string_in_native_doc_encoding(buf, u8)}
 
         def env_get_current_line(self, buf):
                 start, end = buffer_line_boundary(buf)
 
-                u8 = unicode(buf.get_text(start, end, False), 'utf-8')
+                u8 = buf.get_text(start, end, False)
 
                 return {'utf8': u8, 'noenc': self.string_in_native_doc_encoding(buf, u8)}
 
         def env_get_current_line_number(self, buf):
                 start, end = buffer_line_boundary(buf)
 
-                return unicode(str(start.get_line() + 1), 'utf-8')
+                return str(start.get_line() + 1)
 
         def location_uri_for_env(self, location):
                 if not location:
-                        return {'utf8': u'', 'noenc': u''}
+                        return {'utf8': '', 'noenc': ''}
 
-                u8 = unicode(location.get_parse_name(), 'utf-8')
+                u8 = location.get_parse_name()
 
                 if location.has_uri_scheme('file'):
                         u8 = "file://" + u8
@@ -372,23 +372,23 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                         except:
                                 display_name = ''
 
-                        return {'utf8': unicode(display_name, 'utf-8'),
+                        return {'utf8': display_name,
                                 'noenc': location.get_basename()}
                 else:
-                        return u''
+                        return ''
 
         def location_scheme_for_env(self, location):
                 if location:
-                        return unicode(location.get_uri_scheme(), 'utf-8')
+                        return location.get_uri_scheme()
                 else:
-                        return u''
+                        return ''
 
         def location_path_for_env(self, location):
                 if location and location.has_uri_scheme('file'):
-                        return {'utf8': unicode(location.get_parse_name(), 'utf-8'),
+                        return {'utf8': location.get_parse_name(),
                                 'noenc': location.get_path()}
                 else:
-                        return u''
+                        return ''
 
         def location_dir_for_env(self, location):
                 if location:
@@ -398,7 +398,7 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                                 return {'utf8': parent.get_parse_name(),
                                         'noenc': parent.get_path()}
 
-                return u''
+                return ''
 
         def env_add_for_location(self, environ, location, prefix):
                 parts = {'URI': self.location_uri_for_env,
@@ -424,9 +424,9 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                 typ = buf.get_mime_type()
 
                 if typ:
-                        return unicode(typ, 'utf-8')
+                        return typ
                 else:
-                        return u''
+                        return ''
 
         def env_get_documents_uri(self, buf):
                 toplevel = self.view.get_toplevel()
@@ -444,7 +444,7 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                                         documents_uri['utf8'].append(r)
                                         documents_uri['noenc'].append(str(r))
 
-                return {'utf8': u' '.join(documents_uri['utf8']),
+                return {'utf8': ' '.join(documents_uri['utf8']),
                         'noenc': ' '.join(documents_uri['noenc'])}
 
         def env_get_documents_path(self, buf):
@@ -463,7 +463,7 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                                         documents_path['utf8'].append(r)
                                         documents_path['noenc'].append(str(r))
 
-                return {'utf8': u' '.join(documents_path['utf8']),
+                return {'utf8': ' '.join(documents_path['utf8']),
                         'noenc': ' '.join(documents_path['noenc'])}
 
         def get_environment(self):
@@ -474,15 +474,15 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                         # Get the original environment, as utf-8
                         v = os.environ[k]
                         environ['noenc'][k] = v
-                        environ['utf8'][k] = unicode(os.environ[k], 'utf-8')
+                        environ['utf8'][k] = os.environ[k].encode('utf-8')
 
-                variables = {u'GEDIT_SELECTED_TEXT': self.env_get_selected_text,
-                             u'GEDIT_CURRENT_WORD': self.env_get_current_word,
-                             u'GEDIT_CURRENT_LINE': self.env_get_current_line,
-                             u'GEDIT_CURRENT_LINE_NUMBER': self.env_get_current_line_number,
-                             u'GEDIT_CURRENT_DOCUMENT_TYPE': self.env_get_document_type,
-                             u'GEDIT_DOCUMENTS_URI': self.env_get_documents_uri,
-                             u'GEDIT_DOCUMENTS_PATH': self.env_get_documents_path}
+                variables = {'GEDIT_SELECTED_TEXT': self.env_get_selected_text,
+                             'GEDIT_CURRENT_WORD': self.env_get_current_word,
+                             'GEDIT_CURRENT_LINE': self.env_get_current_line,
+                             'GEDIT_CURRENT_LINE_NUMBER': self.env_get_current_line_number,
+                             'GEDIT_CURRENT_DOCUMENT_TYPE': self.env_get_document_type,
+                             'GEDIT_DOCUMENTS_URI': self.env_get_documents_uri,
+                             'GEDIT_DOCUMENTS_PATH': self.env_get_documents_path}
 
                 for var in variables:
                         v = variables[var](buf)
@@ -570,7 +570,7 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                 self.active_snippets.append(sn)
 
                 # Put cursor at first tab placeholder
-                keys = filter(lambda x: x > 0, sn.placeholders.keys())
+                keys = list(filter(lambda x: x > 0, sn.placeholders.keys()))
 
                 if len(keys) == 0:
                         if 0 in sn.placeholders:
@@ -625,7 +625,7 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                         first = False
 
                 if not start.equal(end):
-                        word = unicode(buf.get_text(start, end, False), 'utf-8')
+                        word = buf.get_text(start, end, False)
 
                         if word and word != '':
                                 return (word, start, end)
@@ -859,7 +859,7 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                 # Remove file scheme
                 gfile = Gio.file_new_for_uri(uri)
 
-                environ = {'utf8': {'GEDIT_DROP_DOCUMENT_TYPE': unicode(mime, 'utf-8')},
+                environ = {'utf8': {'GEDIT_DROP_DOCUMENT_TYPE': mime.encode('utf-8')},
                            'noenc': {'GEDIT_DROP_DOCUMENT_TYPE': mime}}
 
                 self.env_add_for_location(environ, gfile, 'GEDIT_DROP_DOCUMENT')
@@ -870,7 +870,7 @@ class Document(GObject.Object, Gedit.ViewActivatable, Signals):
                 relpath = location.get_relative_path(gfile)
 
                 # CHECK: what is the encoding of relpath?
-                environ['utf8']['GEDIT_DROP_DOCUMENT_RELATIVE_PATH'] = unicode(relpath, 'utf-8')
+                environ['utf8']['GEDIT_DROP_DOCUMENT_RELATIVE_PATH'] = relpath.encode('utf-8')
                 environ['noenc']['GEDIT_DROP_DOCUMENT_RELATIVE_PATH'] = relpath
 
                 mark = buf.get_mark('gtk_drag_target')
diff --git a/plugins/snippets/snippets/importer.py b/plugins/snippets/snippets/importer.py
index da0d977..14206f7 100644
--- a/plugins/snippets/snippets/importer.py
+++ b/plugins/snippets/snippets/importer.py
@@ -92,7 +92,7 @@ class Importer:
                 shutil.rmtree(dirname)
 
                 if len(errors) > 0:
-                        return _('The following files could not be imported: %s') % u', '.join(errors)
+                        return _('The following files could not be imported: %s') % ', '.join(errors)
 
         def import_targz(self):
                 self.import_archive('tar -x --gzip -f')
diff --git a/plugins/snippets/snippets/library.py b/plugins/snippets/snippets/library.py
index 959dd78..5ff0ffb 100644
--- a/plugins/snippets/snippets/library.py
+++ b/plugins/snippets/snippets/library.py
@@ -87,7 +87,7 @@ class SnippetData:
                                         keyval, mod = Gtk.accelerator_parse(child.text)
 
                                         if Gtk.accelerator_valid(keyval, mod):
-                                                child.text = unicode(Gtk.accelerator_name(keyval, mod), 'utf-8')
+                                                child.text = Gtk.accelerator_name(keyval, mod)
                                         else:
                                                 child.text = ''
 
@@ -138,7 +138,7 @@ class SnippetData:
                         return
 
                 if isinstance(value, list):
-                        value = u','.join(value)
+                        value = ','.join(value)
 
                 if not self.can_modify() and self.properties[prop] != value:
                         # ohoh, this is not can_modify, but it needs to be changed...
diff --git a/plugins/snippets/snippets/manager.py b/plugins/snippets/snippets/manager.py
index 33348c5..7223920 100644
--- a/plugins/snippets/snippets/manager.py
+++ b/plugins/snippets/snippets/manager.py
@@ -515,7 +515,7 @@ class Manager(Gtk.Dialog, Gtk.Buildable):
                         self['entry_tab_trigger'].set_text(self.snippet['tag'])
                         self['entry_accelerator'].set_text( \
                                         self.snippet.accelerator_display())
-                        self['combo_drop_targets'].get_child().set_text(u', '.join(self.snippet['drop-targets']))
+                        self['combo_drop_targets'].get_child().set_text(', '.join(self.snippet['drop-targets']))
 
                         buf = self['source_view_snippet'].get_buffer()
                         buf.begin_not_undoable_action()
@@ -652,7 +652,7 @@ class Manager(Gtk.Dialog, Gtk.Buildable):
 
         def entry_tab_trigger_update_valid(self):
                 entry = self['entry_tab_trigger']
-                text = unicode(entry.get_text(), 'utf-8')
+                text = entry.get_text()
 
                 if text and not Library().valid_tab_trigger(text):
                         img = self['image_tab_trigger']
@@ -675,7 +675,7 @@ class Manager(Gtk.Dialog, Gtk.Buildable):
                 if not self.snippet:
                         return
 
-                text = unicode(entry.get_text(), 'utf-8')
+                text = entry.get_text()
 
                 # save tag
                 self.snippet['tag'] = text
@@ -685,7 +685,7 @@ class Manager(Gtk.Dialog, Gtk.Buildable):
                 if not self.snippet:
                         return
 
-                text = unicode(entry.get_text(), 'utf-8')
+                text = entry.get_text()
 
                 # save drop targets
                 self.snippet['drop-targets'] = text
@@ -699,8 +699,8 @@ class Manager(Gtk.Dialog, Gtk.Buildable):
                         return
 
                 buf = source_view.get_buffer()
-                text = unicode(buf.get_text(buf.get_start_iter(), \
-                                buf.get_end_iter(), False), 'utf-8')
+                text = buf.get_text(buf.get_start_iter(), \
+                       buf.get_end_iter(), False)
 
                 self.snippet['text'] = text
                 self.snippet_changed()
@@ -1113,7 +1113,7 @@ class Manager(Gtk.Dialog, Gtk.Buildable):
                 if not uris:
                         return
 
-                text = unicode(entry.get_text(), 'utf-8')
+                text = entry.get_text()
 
                 if text:
                         mimes = [text]
@@ -1129,7 +1129,7 @@ class Manager(Gtk.Dialog, Gtk.Buildable):
                         if mime:
                                 mimes.append(mime)
 
-                entry.set_text(u', '.join(mimes))
+                entry.set_text(', '.join(mimes))
                 self.on_entry_drop_targets_focus_out(entry, None)
                 context.finish(True, False, timestamp)
 
diff --git a/plugins/snippets/snippets/placeholder.py b/plugins/snippets/snippets/placeholder.py
index 688195c..46f338e 100644
--- a/plugins/snippets/snippets/placeholder.py
+++ b/plugins/snippets/snippets/placeholder.py
@@ -143,7 +143,7 @@ class Placeholder:
                         eiter = self.end_iter()
 
                         if biter and eiter:
-                                return unicode(self.buf.get_text(self.begin_iter(), self.end_iter(), False), 'utf-8')
+                                return self.buf.get_text(self.begin_iter(), self.end_iter(), False)
                         else:
                                 return ''
                 else:
@@ -422,7 +422,7 @@ class PlaceholderShell(PlaceholderExpand):
                 self.close_shell()
                 self.remove_timeout()
 
-                self.set_text(unicode.join(u'', self.shell_output).rstrip('\n'))
+                self.set_text(str.join('', self.shell_output).rstrip('\n'))
 
                 if self.default == None:
                         self.default = self.get_text()
@@ -437,10 +437,9 @@ class PlaceholderShell(PlaceholderExpand):
 
                         if len(line) > 0:
                                 try:
-                                        line = unicode(line, 'utf-8')
+                                        line = line
                                 except:
-                                        line = unicode(line, locale.getdefaultlocale()[1],
-                                                        'replace')
+                                        line = line.encode(locale.getdefaultlocale()[1])
 
                                 self.shell_output += line
                                 self.install_timeout()
diff --git a/plugins/snippets/snippets/snippet.py b/plugins/snippets/snippets/snippet.py
index a92f0be..280483a 100644
--- a/plugins/snippets/snippets/snippet.py
+++ b/plugins/snippets/snippets/snippet.py
@@ -113,9 +113,9 @@ class Snippet:
 
                 if accel:
                         keyval, mod = Gtk.accelerator_parse(accel)
-                        accel = unicode(Gtk.accelerator_get_label(keyval, mod), 'utf-8')
+                        accel = Gtk.accelerator_get_label(keyval, mod)
 
-                return accel or u''
+                return accel or ''
 
         def display(self):
                 nm = markup_escape(self['description'])
@@ -133,7 +133,7 @@ class Snippet:
                 if not detail:
                         return nm
                 else:
-                        return nm + ' (<b>' + markup_escape(unicode.join(u', ', detail)) + \
+                        return nm + ' (<b>' + markup_escape(', '.join(detail)) + \
                                         '</b>)'
 
         def _add_placeholder(self, placeholder):
@@ -150,7 +150,7 @@ class Snippet:
 
         def _insert_text(self, text):
                 # Insert text keeping indentation in mind
-                indented = unicode.join(u'\n' + self._indent, spaces_instead_of_tabs(self._view, text).split('\n'))
+                indented = str.join('\n' + self._indent, spaces_instead_of_tabs(self._view, text).split('\n'))
                 self._view.get_buffer().insert(self._insert_iter(), indented)
 
         def _insert_iter(self):
@@ -160,7 +160,7 @@ class Snippet:
                 if data in self.environ['utf8']:
                         val = self.environ['utf8'][data]
                 else:
-                        val = u''
+                        val = ''
 
                 # Get all the current indentation
                 all_indent = compute_indentation(self._view, self._insert_iter())
@@ -169,7 +169,7 @@ class Snippet:
                 indent = all_indent[len(self._indent):]
 
                 # Keep indentation
-                return unicode.join(u'\n' + indent, val.split('\n'))
+                return str.join('\n' + indent, val.split('\n'))
 
         def _create_placeholder(self, data):
                 tabstop = data['tabstop']
@@ -247,7 +247,7 @@ class Snippet:
                                 sys.stderr.write('Token class not supported: %s (%s)\n' % token.klass)
                                 continue
 
-                        if isinstance(val, basestring):
+                        if isinstance(val, str):
                                 # Insert text
                                 self._insert_text(val)
                         else:
@@ -321,8 +321,7 @@ class Snippet:
                 # So now all of the snippet is in the buffer, we have all our
                 # placeholders right here, what's next, put all marks in the
                 # plugin_data.marks
-                k = self.placeholders.keys()
-                k.sort(reverse=True)
+                k = sorted(self.placeholders.keys(), reverse=True)
 
                 plugin_data.placeholders.insert(last_index, self.placeholders[0])
                 last_iter = self.placeholders[0].end_iter()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]