File: i_file_dialog.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (117 lines) | stat: -rw-r--r-- 3,874 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

""" The interface for a dialog that allows the user to open/save files etc. """


import sys


from traits.api import Enum, HasTraits, Int, List, Str


from pyface.i_dialog import IDialog


class IFileDialog(IDialog):
    """ The interface for a dialog that allows the user to open/save files etc.
    """

    # 'IFileDialog' interface ---------------------------------------------#

    #: The 'action' that the user is peforming on the directory ("open files"
    #: differs from "open" in that the former supports multiple selections).
    action = Enum("open", "open files", "save as")

    #: The default directory.
    default_directory = Str()

    #: The default filename.
    default_filename = Str()

    #: The default path (directory and filename) of the chosen file.  This is
    #: only used when the *default_directory* and *default_filename* are not set
    #: and is equivalent to setting both.
    default_path = Str()

    #: The directory containing the chosen file.
    directory = Str()

    #: The name (basename only) of the chosen file.
    filename = Str()

    #: The path (directory and filename) of the chosen file. To be used when only
    #: single selection is allowed: if *action* is "open files", use *paths* instead.
    path = Str()

    #: The paths (directory and filename) of the chosen files. To be used when
    #: multiple selection is allowed.
    paths = List(Str())

    #: The wildcard used to restrict the set of files.
    wildcard = Str()

    #: The index of the selected wildcard.
    wildcard_index = Int(0)


class MFileDialog(HasTraits):
    """ The mixin class that contains common code for toolkit specific
    implementations of the IFileDialog interface.

    Implements: create_wildcard()
    """

    # FIXME v3: These are referenced elsewhere so maybe should be part of the
    # interface. The format is toolkit specific and so shouldn't be exposed.
    # The create_wildcard() class method (why isn't it a static method?) should
    # be the basis for this - but nothing seems to use it.  For now the PyQt
    # implementation will convert the wx format to its own.  Actually we need
    # the format to be portable between toolkits - so stick with PyQt
    # supporting the wx format or invent a data structure.

    #: A file dialog wildcard for Python files.
    WILDCARD_PY = "Python files (*.py)|*.py|"

    #: A file dialog wildcard for text files.
    WILDCARD_TXT = "Text files (*.txt)|*.txt|"

    #: A file dialog wildcard for all files.
    if sys.platform == "win32":
        WILDCARD_ALL = "All files (*.*)|*.*|"
    else:
        WILDCARD_ALL = "All files (*)|*"

    #: A file dialog wildcard for Zip archives.
    WILDCARD_ZIP = "Zip files (*.zip)|*.zip|"

    # ------------------------------------------------------------------------
    # 'MFileDialog' *CLASS* interface.
    # ------------------------------------------------------------------------

    @classmethod
    def create_wildcard(cls, description, extension):
        """ Creates a wildcard for a given extension.

        Parameters
        ----------
        description : str
            A human-readable description of the pattern.
        extenstion : list
            The wildcard patterns for the extension.
        """

        if isinstance(extension, str):
            pattern = extension

        else:
            pattern = ";".join(extension)

        return "%s (%s)|%s|" % (description, pattern, pattern)