File: _common.py

package info (click to toggle)
python-pathvalidate 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 692 kB
  • sloc: python: 3,987; makefile: 262; sh: 6
file content (136 lines) | stat: -rw-r--r-- 2,127 bytes parent folder | download | duplicates (2)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""

import random
import string
from itertools import product


alphanum_chars = tuple(x for x in string.digits + string.ascii_letters)


INVALID_PATH_CHARS = ("\0",)
INVALID_FILENAME_CHARS = ("/",)
INVALID_WIN_PATH_CHARS = (":", "*", "?", '"', "<", ">", "|") + INVALID_PATH_CHARS
INVALID_WIN_FILENAME_CHARS = INVALID_WIN_PATH_CHARS + INVALID_FILENAME_CHARS + ("\\",)

VALID_FILENAME_CHARS = (
    "!",
    "#",
    "$",
    "&",
    "'",
    "_",
    "=",
    "~",
    "^",
    "@",
    "`",
    "[",
    "]",
    "+",
    "-",
    ";",
    "{",
    "}",
    ",",
    ".",
    "(",
    ")",
    "%",
)
VALID_PATH_CHARS = VALID_FILENAME_CHARS + ("/",)
VALID_PLATFORM_NAMES = ["universal", "linux", "windows", "macos"]

INVALID_JS_VAR_CHARS = INVALID_WIN_FILENAME_CHARS + (
    "!",
    "#",
    "&",
    "'",
    "=",
    "~",
    "^",
    "@",
    "`",
    "[",
    "]",
    "+",
    "-",
    ";",
    "{",
    "}",
    ",",
    ".",
    "(",
    ")",
    "%",
    " ",
    "\t",
    "\n",
    "\r",
    "\f",
    "\v",
)
INVALID_PYTHON_VAR_CHARS = INVALID_JS_VAR_CHARS + ("$",)

WIN_RESERVED_FILE_NAMES = [
    "CON",
    "con",
    "PRN",
    "prn",
    "AUX",
    "aux",
    "CLOCK$",
    "clock$",
    "NUL",
    "nul",
] + [
    f"{name:s}{num}"
    for name, num in product(
        ["COM", "com", "LPT", "lpt"],
        list(range(0, 10))
        + ["\N{SUPERSCRIPT ONE}", "\N{SUPERSCRIPT TWO}", "\N{SUPERSCRIPT THREE}"],
    )
]
NTFS_RESERVED_FILE_NAMES = [
    "$Mft",
    "$MftMirr",
    "$LogFile",
    "$Volume",
    "$AttrDef",
    "$Bitmap",
    "$Boot",
    "$BadClus",
    "$Secure",
    "$Upcase",
    "$Extend",
    "$Quota",
    "$ObjId",
    "$Reparse",
]


def is_faker_installed():
    try:
        import faker  # noqa
    except ImportError:
        return False

    return True


def randstr(length, char_list=alphanum_chars):
    return "".join([random.choice(char_list) for _i in range(length)])


def platform_macos():
    return "macos"


def platform_windows():
    return "windows"


def platform_linux():
    return "linux"