Exemplo n.º 1
0
    def __init__(self, file_descriptors, file_format=None):
        """
        Create model.

        Arguments:
            file_descriptors: File descriptors proxy object.
            file_format (Optional[str]): File format. Defaults to
                *None*.
        """
        super(UnitModel, self).__init__()
        self._file_descriptors = file_descriptors

        self._units2file = OrderedDict()
        self._units2stat = OrderedDict()
        self._emb2ext = OrderedDict()
        self._ext2emb = OrderedDict()
        self._extfiles = {}

        file_dict = {}
        for handle, info in self._file_descriptors.handle2info.viewitems():
            self._units2file[handle] = info.filename
            self._units2stat[handle] = info.embedded
            file_dict[info.filename] = handle

        unit = -10
        for fileuid in external_files(file_format):
            filename = external_file(fileuid)
            if not filename:
                continue
            self._extfiles[fileuid] = filename
            if fileuid in file_dict:
                continue
            self._units2file[unit] = fileuid
            unit = unit - 1
Exemplo n.º 2
0
    def _text(self, short=False):
        """
        Get internal representation of the item's filename.

        Arguments:
            short (Optional[bool]): Says long or short reprsentation to
                get.

        Returns:
            str: String representation of filename.
        """
        prefix = NodeType.value2str(NodeType.Unit) + ": " if not short else ''
        if self.filename:
            if self.is_reference:
                res = external_file(self.filename)
                if res is None:
                    res = "<{}>".format(translate("DataFiles", "undefined"))
                res = res if short else res + " ({})".format(self.filename)
            else:
                res = os.path.basename(self.filename) if short \
                    else self.filename
        else:
            res = "<{}>".format(translate("DataFiles", "undefined"))
        if self.embedded and not self.is_reference:
            res += ' ({})'.format(translate("DataFiles", "embedded"))
        return prefix + res
Exemplo n.º 3
0
    def valid(self):
        """
        Check if file object is valid.

        Returns:
            bool: *True* if file is valid; *False* otherwise.
        """
        return external_file(self.filename) is not None if self.is_reference \
            else self.filename is not None
Exemplo n.º 4
0
 def exists(self):
     """Returns file name presence in file system."""
     exists = False
     if self.filename:
         if self.isreference:
             sobj_name = external_file(self.filename)
             exists = sobj_name is not None
         else:
             exists = osp.isfile(self.filename)
     return exists
Exemplo n.º 5
0
    def updateControls(self):
        """
        Called when current index of filename combobox is changed.

        Updates state of 'exists' and 'embedded' controls.
        """
        exists = isvalid = isreference = isforced = False
        combo_index = self.file_combo.currentIndex()
        index = self.file_combo.model().index(combo_index, 0)
        if index.isValid():
            filename = index.data(Role.CustomRole)
            isvalid = index.data(Role.ValidityRole)
            isreference = index.data(Role.ReferenceRole)
            if isreference:
                exists = external_file(filename) is not None
            else:
                exists = isvalid and os.path.isfile(filename)
            isforced = get_node_type(self.node) in (NodeType.Unit,) \
                and self.node.is_forced_attr
        self.exists_check.setChecked(exists)
        # undefined file can not be embedded
        self.embedded_check.setDisabled(isreference or not isvalid or isforced)