Commits

Anonymous committed f95c018

end port no test

Comments (0)

Files changed (1)

snippets_plugin/snippets/snippets.py

      the extension does not exist or no snippet
      searches the group *GENERAL*.
 
-- Use Export Button for copy yours plugin in json file.
+- Use Export Button for copy yours snippets in cfg file.
 - Use Import Button for merge your snippets with exported file.
 - Create your own variables or hide the default ones.
 - Use your variables as '${VARIABLE NAME}'.
 #===============================================================================
 
 __prj__ = 'Snippets Plugin'
-__version__ = '0.2.1'
+__version__ = '0.'
 __license__ = "THE WISKEY-WARE LICENSE"
 __author__ = 'JuanBC'
 __email__ = 'jbc.develop@gmail.com'
 #===============================================================================
 
 import os
-import json
 import datetime
 import string
 import socket
 from PyQt4.QtCore import Qt
 from PyQt4.QtCore import pyqtSignal
 
-from PyQt4.QtGui import QInputDialog
 from PyQt4.QtGui import QTextCursor
 from PyQt4.QtGui import QMessageBox
 from PyQt4.QtGui import QListWidgetItem
 from PyQt4.QtGui import QFileDialog
-from PyQt4.QtGui import QTableWidgetItem
 
 from ninja_ide.core import plugin
 
 
 import constants
 import caller
+import cfgparser
 
 
 #===============================================================================
     # emit this signal on save method
     saved = pyqtSignal(dict, name="saved")
 
-    def __init__(self, snippets, *args):
+    def __init__(self, *args):
         super(self.__class__, self).__init__(*args)
 
-        self.editor = editor.create_editor()
+        self.editor = editor.create_editor(constants.SNIPPETS_FILE_PATH, 
+                                           syntax=constants.SYNTAX)
         self.editor.setParent(self)
-        self.editor.register_syntax(syntax=constants.SYNTAX)
         self.editorLayout.addWidget(self.editor)
-        self.vars = snippets.get(constants.USER_VARS_KEY, {})
-        self.snippets = snippets
-        self.reset()
-
 
     
     def on_importButton_pressed(self, *args):
-        """Open a file dialog for merge the snippets with json file
+        """Open a file dialog for merge the snippets with cfg file
 
-        """
+        
         filepath = QFileDialog.getOpenFileName(
                        self, self.tr("Import Snippets..."),
                        constants.HOME_PATH,
-                       self.tr("JSON (*.json)")
+                       self.tr("CFG (*.cfg)")
                    )
         if filepath:
             try:
                 with open(filepath) as fp:
-                    imported = json.load(fp)
+                    imported = cfgparser.load(fp)
                 for lang_name, lang_snippets in imported.items():
                     if lang_name in self.snippets:
                         self.snippets[lang_name].update(lang_snippets)
                 title = self.tr("Success!")
                 msg = self.tr("Successfully merged imported file: %1").arg(filepath)
                 QMessageBox.information(self, title, msg)
+        """
 
     def on_exportButton_pressed(self, *args):
         """Open a file dialog for save the snippets in json file
 
         """
-        s_path = os.path.join(constants.HOME_PATH, "export.json")
+        s_path = os.path.join(constants.HOME_PATH, "export.cfg")
         count = 0
         while os.path.exists(s_path):
             count += 1
             s_path = os.path.join(constants.HOME_PATH,
-                                  "export_{0}.json".format(count))
+                                  "export_{0}.cfg".format(count))
         filepath = QFileDialog.getSaveFileName(
                        self, self.tr("Export Snippets..."),
                        s_path,
-                       self.tr("JSON (*.json)")
+                       self.tr("CFG (*.cfg)")
                    )
         if filepath:
             try:
         msg.append("License:\n{0}".format(__full_license__))
         QMessageBox.about(self, title, "\n".join(msg))
 
-    def reset(self):
-        """Restart all the gui bases on self.snippets"""
-        self.removeLanguageButton.setEnabled(False)
-        self.newSnippetButton.setEnabled(False)
-        self.removeSnippetButton.setEnabled(False)
-        self.snippetPathLabel.setText("-")
-        self.languagesList.clear()
-        self.snippetList.clear()
-        self.editor.setPlainText("")
-        self.editor.setReadOnly(True)
-        for lang_name in sorted(self.snippets.keys()):
-            if not lang_name.startswith("_"):
-                lang_item = QListWidgetItem()
-                lang_item.setText(lang_name)
-                self.languagesList.addItem(lang_item)
-
     def parent(self):
         """This method return a fake hierarchy of parent
 
         """Called from ninja ide when save button is called
 
         """
-        last_snippet_item_edited = self.snippetList.currentItem()
-        self.snippetList.currentItemChanged.emit(None, last_snippet_item_edited)
-        data = self.snippets
-        data.update({constants.USER_VARS_KEY: self.vars})
-        self.saved.emit(data)
+        try:
+            src = unicode(self.editor.get_text())
+            new_snippets = cfgparser.loads(src)
+            self.saved.emit(new_snippets)
+        except Exception as err:
+            title = self.tr("Snippets Format Error")
+            msg = str(err)
+            QMessageBox.critical(self, title, msg)
 
 
 #===============================================================================
 
         """
         with open(constants.SNIPPETS_FILE_PATH, "w") as fp:
-            json.dump(snippets, fp, indent=2)
+            cfgparser.dump(snippets, fp)
         self.snippets = snippets
 
     def replace_slot(self, event):
             shutil.copyfile(constants.SNIPPETS_TEMPLATE_FILE_PATH,
                             constants.SNIPPETS_FILE_PATH)
         with open(constants.SNIPPETS_FILE_PATH) as fp:
-            self.snippets = json.load(fp)
+            self.snippets = cfgparser.load(fp)
         self.editor_s = self.locator.get_service('editor')
         self.editor_s.editorKeyPressEvent.connect(self.replace_slot)
 
 
     def get_preferences_widget(self):
         #Return a widget for customize yor plugin
-        confFrame = SnippetConfigurationFrame(self.snippets)
+        confFrame = SnippetConfigurationFrame()
         confFrame.saved.connect(self.save_snippets_slot)
         return confFrame