示例#1
0
    def convert(self, edit):
        """Convert the read data to the desired format."""

        errors = False
        try:
            if not errors:
                # Convert Python dict to JSON buffer.
                default_flow_style = None
                flow_setting = self.settings.get("yaml_default_flow_style", None)
                if flow_setting == "true":
                    default_flow_style = True
                elif flow_setting == "false":
                    default_flow_style = False

                self.output = yaml.yaml_dumps(
                    self.json,
                    default_flow_style=default_flow_style,
                    indent=self.settings.get("yaml_indent", 4),
                    strip_tabs=self.strip_tabs,
                    detect_timestamp=self.settings.get("yaml_detect_timestamp", True)
                )
                self.json = None
        except Exception:
            errors = True
            error_msg(self.errors["json2yaml"], traceback.format_exc())
        return errors
示例#2
0
    def read_source(self):
        """Read the source."""

        errors = False
        ext_tbl = self.settings.get("yaml_strip_tabs_from", [])
        filename = self.view.file_name()
        self.strip_tabs = False
        if filename is not None:
            for ext in ext_tbl:
                m = re.match("^(.*)\\." + re.escape(ext) + "$", filename, re.IGNORECASE)
                if m is not None:
                    self.strip_tabs = True
                    break
        try:
            # Ensure view buffer is in a UTF8 format.
            # Wrap string in a file structure so it can be accessed by readPlist
            # Read view buffer as PLIST and dump to Python dict
            if self.binary and self.view.encoding() == 'Hexadecimal':
                self.plist = plist.read_plist_from_hex_view(self.view)
            elif self.binary and filename is not None and os.path.exists(filename):
                self.plist = plist.read_plist_from_file(filename)
            else:
                self.plist = plist.read_plist_from_view(self.view)
        except Exception:
            errors = True
            error_type = 'view2bplist' if self.binary else 'view2plist'
            error_msg(self.errors[error_type], traceback.format_exc())
        return errors
示例#3
0
    def write_file(self, edit, show_file):
        """Write data to a file if a location can be acquired else save to a view buffer."""

        errors = False

        if self.save_filename is not None and os.path.exists(os.path.dirname(self.save_filename)):
            # Save content to UTF file
            try:
                if self.save_binary:
                    with open(self.save_filename, "wb") as f:
                        f.write(self.output)
                else:
                    with codecs.open(self.save_filename, "w", "utf-8") as f:
                        f.write(self.output)
                self.output = None
                if show_file:
                    self.output_view = self.view.window().open_file(self.save_filename)
            except Exception:
                errors = True
                error_msg(self.errors["filewrite"], traceback.format_exc())
            if not errors and show_file:
                self.set_syntax()
        else:
            # Could not acquire a name that exists on disk
            # Fallback to buffer write
            self.write_buffer(edit, force_new_buffer=True)
示例#4
0
    def read_source(self):
        """Read the source."""

        errors = False
        try:
            # Strip comments and dangling commas from view buffer
            # Read view buffer as JSON
            # Dump data to Python dict
            self.yaml = yaml.read_yaml_from_view(self.view)
        except Exception:
            errors = True
            error_msg(self.errors["view2yaml"], traceback.format_exc())
        return errors
示例#5
0
    def convert(self, edit):
        """Convert the read data to the desired format."""

        errors = False
        try:
            # Convert Python dict to PLIST buffer
            self.output = json.json_dumps(
                self.yaml,
                preserve_binary=self.settings.get("json_preserve_binary_data", True)
            )
            self.yaml = None
        except Exception:
            errors = True
            error_msg(self.errors["yaml2json"], traceback.format_exc())
        return errors
示例#6
0
    def write_buffer(self, edit, force_new_buffer=False):
        """Write the data to a view buffer."""

        errors = False
        new_buffer = bool(self.settings.get("open_in_new_buffer", False))

        # Save content to view buffer
        try:
            self.output_view = self.view.window().new_file() if new_buffer or force_new_buffer else self.view
            if self.save_binary:
                self.output_view.set_encoding('Hexadecimal')
                bin_output = []
                count = 0
                for b in self.output:
                    if count % 16 == 0 and count != 0:
                        bin_output += ['\n', to_hex(b)]
                    else:
                        if count % 2 == 0 and count != 0:
                            bin_output += [' ', to_hex(b)]
                        else:
                            bin_output.append(to_hex(b))
                    count += 1
                self.output = None
                self.output_view.replace(
                    edit,
                    sublime.Region(0, self.view.size()),
                    ''.join(bin_output)
                )
                bin_output = None
            else:
                self.output_view.set_encoding('UTF-8')
                self.output_view.replace(
                    edit,
                    sublime.Region(0, self.view.size()),
                    self.output
                )
                self.output = None
        except Exception:
            errors = True
            error_msg(self.errors["bufferwrite"], traceback.format_exc())

        if not errors:
            if new_buffer or force_new_buffer:
                # If a name can be acquired from the original view,
                # give buffer a modified derivative of the name.
                if self.save_filename is not None:
                    self.output_view.set_name(os.path.basename(self.save_filename))
            self.set_syntax()
示例#7
0
    def convert(self, edit):
        """Convert the read data to the desired format."""

        errors = False
        try:
            if not errors:
                self.output = json.json_dumps(
                    self.plist,
                    preserve_binary=self.settings.get("json_preserve_binary_data", True)
                )
                self.plist = None
        except Exception:
            errors = True
            error_type = 'bplist2json' if self.binary else 'plist2json'
            error_msg(self.errors[error_type], traceback.format_exc())
        return errors
示例#8
0
    def read_source(self):
        """Read the source."""

        errors = False
        try:
            # Ensure view buffer is in a UTF8 format.
            # Wrap string in a file structure so it can be accessed by readPlist
            # Read view buffer as PLIST and dump to Python dict
            filename = self.view.file_name()
            if self.binary and self.view.encoding() == 'Hexadecimal':
                self.plist = plist.read_plist_from_hex_view(self.view)
            elif self.binary and filename is not None and os.path.exists(filename):
                self.plist = plist.read_plist_from_file(filename)
            else:
                self.plist = plist.read_plist_from_view(self.view)
        except Exception:
            errors = True
            error_type = 'view2bplist' if self.binary else 'view2plist'
            error_msg(self.errors[error_type], traceback.format_exc())
        return errors
示例#9
0
    def read_source(self):
        """Read the source."""

        errors = False
        ext_tbl = self.settings.get("yaml_strip_tabs_from", [])
        filename = self.view.file_name()
        self.strip_tabs = False
        if filename is not None:
            for ext in ext_tbl:
                m = re.match("^(.*)\\." + re.escape(ext) + "$", filename, re.IGNORECASE)
                if m is not None:
                    self.strip_tabs = True
                    break
        try:
            # Ensure view buffer is in a UTF8 format.
            # Wrap string in a file structure so it can be accessed by readPlist
            # Read view buffer as PLIST and dump to Python dict
            self.json = json.read_json_from_view(self.view)
        except Exception:
            errors = True
            error_msg(self.errors["view2json"], traceback.format_exc())
        return errors
示例#10
0
    def convert(self, edit):
        """Convert the read data to the desired format."""

        errors = False
        try:
            # Convert Python dict to PLIST buffer
            if self.save_binary:
                self.output = plist.plist_binary_dumps(
                    self.plist,
                    detect_timestamp=self.settings.get("plist_detect_timestamp", True),
                    none_handler=self.settings.get("plist_none_handler", "fail")
                )
            else:
                self.output = plist.plist_dumps(
                    self.plist,
                    detect_timestamp=self.settings.get("plist_detect_timestamp", True),
                    none_handler=self.settings.get("plist_none_handler", "fail")
                )
            self.plist = None
        except Exception:
            errors = True
            error_type = "bplist2plist" if self.binary else 'plist2bplist'
            error_msg(self.errors[error_type], traceback.format_exc())
        return errors