communal 0.4.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- communal-0.4.5/.gitignore +160 -0
- communal-0.4.5/LICENSE +21 -0
- communal-0.4.5/PKG-INFO +34 -0
- communal-0.4.5/README.md +2 -0
- communal-0.4.5/communal/__init__.py +1 -0
- communal-0.4.5/communal/attrdict.py +16 -0
- communal-0.4.5/communal/binary.py +79 -0
- communal-0.4.5/communal/casing.py +23 -0
- communal-0.4.5/communal/client.py +14 -0
- communal-0.4.5/communal/collections.py +15 -0
- communal-0.4.5/communal/dates.py +26 -0
- communal-0.4.5/communal/download.py +36 -0
- communal-0.4.5/communal/encoding.py +34 -0
- communal-0.4.5/communal/enum.py +32 -0
- communal-0.4.5/communal/exceptions.py +6 -0
- communal-0.4.5/communal/files.py +42 -0
- communal-0.4.5/communal/floats.py +5 -0
- communal-0.4.5/communal/functions.py +349 -0
- communal-0.4.5/communal/getter.py +18 -0
- communal-0.4.5/communal/guids.py +11 -0
- communal-0.4.5/communal/hashing.py +25 -0
- communal-0.4.5/communal/http.py +31 -0
- communal-0.4.5/communal/inflections.py +7 -0
- communal-0.4.5/communal/inheritance.py +20 -0
- communal-0.4.5/communal/ips.py +64 -0
- communal-0.4.5/communal/iterables.py +226 -0
- communal-0.4.5/communal/memory.py +11 -0
- communal-0.4.5/communal/mixins.py +19 -0
- communal-0.4.5/communal/nesting.py +277 -0
- communal-0.4.5/communal/nulls.py +32 -0
- communal-0.4.5/communal/properties.py +86 -0
- communal-0.4.5/communal/sets.py +8 -0
- communal-0.4.5/communal/sizes.py +7 -0
- communal-0.4.5/communal/submodules.py +27 -0
- communal-0.4.5/communal/time_periods.py +7 -0
- communal-0.4.5/communal/timezone.py +15 -0
- communal-0.4.5/communal/tracebacks.py +6 -0
- communal-0.4.5/communal/zip_archive.py +56 -0
- communal-0.4.5/pyproject.toml +87 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# poetry
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
102
|
+
#poetry.lock
|
|
103
|
+
|
|
104
|
+
# pdm
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
106
|
+
#pdm.lock
|
|
107
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
108
|
+
# in version control.
|
|
109
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
110
|
+
.pdm.toml
|
|
111
|
+
|
|
112
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
113
|
+
__pypackages__/
|
|
114
|
+
|
|
115
|
+
# Celery stuff
|
|
116
|
+
celerybeat-schedule
|
|
117
|
+
celerybeat.pid
|
|
118
|
+
|
|
119
|
+
# SageMath parsed files
|
|
120
|
+
*.sage.py
|
|
121
|
+
|
|
122
|
+
# Environments
|
|
123
|
+
.env
|
|
124
|
+
.venv
|
|
125
|
+
env/
|
|
126
|
+
venv/
|
|
127
|
+
ENV/
|
|
128
|
+
env.bak/
|
|
129
|
+
venv.bak/
|
|
130
|
+
|
|
131
|
+
# Spyder project settings
|
|
132
|
+
.spyderproject
|
|
133
|
+
.spyproject
|
|
134
|
+
|
|
135
|
+
# Rope project settings
|
|
136
|
+
.ropeproject
|
|
137
|
+
|
|
138
|
+
# mkdocs documentation
|
|
139
|
+
/site
|
|
140
|
+
|
|
141
|
+
# mypy
|
|
142
|
+
.mypy_cache/
|
|
143
|
+
.dmypy.json
|
|
144
|
+
dmypy.json
|
|
145
|
+
|
|
146
|
+
# Pyre type checker
|
|
147
|
+
.pyre/
|
|
148
|
+
|
|
149
|
+
# pytype static type analyzer
|
|
150
|
+
.pytype/
|
|
151
|
+
|
|
152
|
+
# Cython debug symbols
|
|
153
|
+
cython_debug/
|
|
154
|
+
|
|
155
|
+
# PyCharm
|
|
156
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
157
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
158
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
159
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
160
|
+
#.idea/
|
communal-0.4.5/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 goodcleanfun
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
communal-0.4.5/PKG-INFO
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: communal
|
|
3
|
+
Version: 0.4.5
|
|
4
|
+
Summary: A library of commonly used Python utilities and functions
|
|
5
|
+
Project-URL: Homepage, https://github.com/goodcleanfun/communal
|
|
6
|
+
Project-URL: Repository, https://github.com/goodcleanfun/communal
|
|
7
|
+
Author: Al Barrentine
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: communal,utils
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Natural Language :: English
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Requires-Python: >=3.8
|
|
21
|
+
Requires-Dist: chardet
|
|
22
|
+
Requires-Dist: fsspec>=2022.3.0
|
|
23
|
+
Requires-Dist: ftfy
|
|
24
|
+
Requires-Dist: furl
|
|
25
|
+
Requires-Dist: inflect
|
|
26
|
+
Requires-Dist: pendulum
|
|
27
|
+
Requires-Dist: python-dateutil
|
|
28
|
+
Requires-Dist: pytz>=2022.1
|
|
29
|
+
Requires-Dist: requests
|
|
30
|
+
Requires-Dist: user-agents
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# communal
|
|
34
|
+
A library of common Python utilities and functions
|
communal-0.4.5/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.4.5"
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class AttrDict(dict):
|
|
2
|
+
"""
|
|
3
|
+
A dictionary that allows using the dot operator to get and set keys.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
def __getattr__(self, key):
|
|
7
|
+
return self[key]
|
|
8
|
+
|
|
9
|
+
def __setattr__(self, key, value):
|
|
10
|
+
self[key] = value
|
|
11
|
+
|
|
12
|
+
def __delattr__(self, key):
|
|
13
|
+
del self[key]
|
|
14
|
+
|
|
15
|
+
def __repr__(self):
|
|
16
|
+
return f"{self.__class__.__name__}({dict.__repr__(self)})"
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
|
|
3
|
+
from communal.encoding import safe_decode, safe_encode
|
|
4
|
+
from communal.functions import Pipeline
|
|
5
|
+
from communal.properties import classproperty
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def int_to_bytes(x: int, endianness="little"):
|
|
9
|
+
if not isinstance(x, int):
|
|
10
|
+
x = int(x)
|
|
11
|
+
return x.to_bytes((x.bit_length() + 7) // 8, endianness)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def int_from_bytes(x: bytes, endianness="little"):
|
|
15
|
+
return int.from_bytes(x, endianness)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class BinaryEncoder(object):
|
|
19
|
+
@classmethod
|
|
20
|
+
def strip_padding(cls, s):
|
|
21
|
+
return s.rstrip(b"=")
|
|
22
|
+
|
|
23
|
+
@classproperty
|
|
24
|
+
def encode(cls):
|
|
25
|
+
return Pipeline(cls.encoder, cls.strip_padding)
|
|
26
|
+
|
|
27
|
+
@classmethod
|
|
28
|
+
def add_padding(cls, s):
|
|
29
|
+
return s if len(s) % cls.group_width == 0 else s.ljust(cls.group_width, b"=")
|
|
30
|
+
|
|
31
|
+
@classproperty
|
|
32
|
+
def decode(cls):
|
|
33
|
+
return Pipeline(cls.add_padding, cls.decoder)
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
def encode_str(cls, s: str):
|
|
37
|
+
return safe_decode(cls.encode(safe_encode(s)))
|
|
38
|
+
|
|
39
|
+
@classmethod
|
|
40
|
+
def encode_int(cls, x: int, endianness: str = "little"):
|
|
41
|
+
return safe_decode(cls.encode(int_to_bytes(x, endianness=endianness)))
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def decode_str(cls, s: str):
|
|
45
|
+
return safe_decode(cls.decode(safe_encode(s)))
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def decode_int(cls, s: str, endianness: str = "little"):
|
|
49
|
+
return int_from_bytes(cls.decode(safe_encode(s)), endianness=endianness)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Base16(BinaryEncoder):
|
|
53
|
+
bits = 16
|
|
54
|
+
group_width = 2
|
|
55
|
+
encoder = base64.b16encode
|
|
56
|
+
decoder = base64.b16decode
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class Base32(BinaryEncoder):
|
|
60
|
+
bits = 32
|
|
61
|
+
group_width = 8
|
|
62
|
+
encoder = base64.b32encode
|
|
63
|
+
decoder = base64.b32decode
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class Base64(BinaryEncoder):
|
|
67
|
+
bits = 64
|
|
68
|
+
group_width = 8
|
|
69
|
+
encoder = base64.b64encode
|
|
70
|
+
decoder = base64.b64decode
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class Base85(BinaryEncoder):
|
|
74
|
+
bits = 85
|
|
75
|
+
encoder = base64.b85encode
|
|
76
|
+
decoder = base64.b85decode
|
|
77
|
+
|
|
78
|
+
encode = encoder
|
|
79
|
+
decode = decoder
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
camel_to_snake_re_all_caps = re.compile("([A-Z\\d]+)([A-Z][a-z])")
|
|
4
|
+
camel_to_snake_re_before_word = re.compile("([a-z])([A-Z])")
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def camel_to_snake_case(name):
|
|
8
|
+
name = camel_to_snake_re_all_caps.sub("\\1_\\2", name)
|
|
9
|
+
name = camel_to_snake_re_before_word.sub("\\1_\\2", name)
|
|
10
|
+
name = name.replace("-", "_")
|
|
11
|
+
return name.lower()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def to_camel_case(name):
|
|
15
|
+
return name.title().replace(" ", "").replace("_", "")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def to_title_case(name):
|
|
19
|
+
"""Convert all upper or all lower case names to title case, otherwise preserve casing."""
|
|
20
|
+
if name.islower() or name.isupper():
|
|
21
|
+
return name.title()
|
|
22
|
+
else:
|
|
23
|
+
return name
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class ClientProxy(object):
|
|
2
|
+
client = None
|
|
3
|
+
|
|
4
|
+
def __getattr__(self, name):
|
|
5
|
+
return getattr(self.client, name)
|
|
6
|
+
|
|
7
|
+
def __getitem__(self, name):
|
|
8
|
+
return self.client[name]
|
|
9
|
+
|
|
10
|
+
def __setitem__(self, name, value):
|
|
11
|
+
self.client[name] = value
|
|
12
|
+
|
|
13
|
+
def __delitem__(self, name):
|
|
14
|
+
del self.client[name]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from collections import OrderedDict
|
|
2
|
+
from collections.abc import Hashable, Mapping
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def unique_list(values):
|
|
6
|
+
"""Produces a unique list while preserving input order"""
|
|
7
|
+
return list(OrderedDict.fromkeys(values))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def is_mapping(val):
|
|
11
|
+
return isinstance(val, Mapping)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def is_hashable(val):
|
|
15
|
+
return isinstance(val, Hashable)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from datetime import date, datetime
|
|
2
|
+
|
|
3
|
+
from dateutil.parser import parse as _dateutil_parse
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def current_timestamp():
|
|
7
|
+
return datetime.utcnow().timestamp()
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def current_year():
|
|
11
|
+
return date.today().year
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
parse_datetime = _dateutil_parse
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def parse_date(dt):
|
|
18
|
+
return _dateutil_parse(dt).date()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def parse_datetime_ignore_tz_microseconds(d):
|
|
22
|
+
return _dateutil_parse(d, ignoretz=True)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def parse_datetime_ignore_tz_seconds(d):
|
|
26
|
+
return _dateutil_parse(d, ignoretz=True).replace(microsecond=0)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import shutil
|
|
3
|
+
|
|
4
|
+
import fsspec
|
|
5
|
+
import furl
|
|
6
|
+
import requests
|
|
7
|
+
|
|
8
|
+
from communal.files import ensure_dir
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def save_response_content(response, destination):
|
|
12
|
+
with fsspec.open(destination, "wb") as f:
|
|
13
|
+
shutil.copyfileobj(response.raw, f)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def download_file(url, dest, retries=3, retry_delay=5, session=requests, headers=None):
|
|
17
|
+
url = furl.furl(url)
|
|
18
|
+
|
|
19
|
+
exists = os.path.exists(dest)
|
|
20
|
+
is_dir = os.path.isdir(dest) if exists else len(os.path.splitext(dest)[1]) == 0
|
|
21
|
+
|
|
22
|
+
base_dir = dest if is_dir else os.path.dirname(dest)
|
|
23
|
+
if base_dir:
|
|
24
|
+
ensure_dir(base_dir)
|
|
25
|
+
|
|
26
|
+
dest_filename = os.path.basename(dest) if not is_dir else None
|
|
27
|
+
if not dest_filename and url.path.isfile:
|
|
28
|
+
dest_filename = url.path.segments[-1]
|
|
29
|
+
dest = os.path.join(dest, dest_filename)
|
|
30
|
+
|
|
31
|
+
with session.get(url, headers=headers, stream=True) as resp:
|
|
32
|
+
resp.raw.decode_content = True
|
|
33
|
+
resp.raise_for_status()
|
|
34
|
+
save_response_content(resp, dest)
|
|
35
|
+
|
|
36
|
+
return dest
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
def safe_decode(
|
|
2
|
+
value, encoding="utf-8", errors="strict", text_types=str, binary_types=bytes
|
|
3
|
+
):
|
|
4
|
+
if isinstance(value, text_types):
|
|
5
|
+
return value
|
|
6
|
+
|
|
7
|
+
if isinstance(value, binary_types):
|
|
8
|
+
return value.decode(encoding, errors)
|
|
9
|
+
else:
|
|
10
|
+
return str(value)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def safe_encode(
|
|
14
|
+
value,
|
|
15
|
+
encoding="utf-8",
|
|
16
|
+
from_encoding=None,
|
|
17
|
+
errors="strict",
|
|
18
|
+
text_types=str,
|
|
19
|
+
binary_types=bytes,
|
|
20
|
+
):
|
|
21
|
+
if isinstance(value, text_types):
|
|
22
|
+
return value.encode(encoding, errors=errors)
|
|
23
|
+
elif isinstance(value, binary_types):
|
|
24
|
+
if hasattr(from_encoding, "lower"):
|
|
25
|
+
from_encoding = from_encoding.lower()
|
|
26
|
+
if hasattr(encoding, "lower"):
|
|
27
|
+
encoding = encoding.lower()
|
|
28
|
+
|
|
29
|
+
if value and from_encoding and encoding and encoding != from_encoding:
|
|
30
|
+
value = safe_decode(value, encoding=from_encoding, errors=errors)
|
|
31
|
+
return value.encode(encoding, errors)
|
|
32
|
+
return value
|
|
33
|
+
else:
|
|
34
|
+
return bytes(repr(value), encoding=encoding)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class StringEnum(str, enum.Enum):
|
|
5
|
+
def __str__(self):
|
|
6
|
+
return str(self.value)
|
|
7
|
+
|
|
8
|
+
@classmethod
|
|
9
|
+
def display_name(cls, choice):
|
|
10
|
+
return choice.name
|
|
11
|
+
|
|
12
|
+
@property
|
|
13
|
+
def display(self):
|
|
14
|
+
return self.value.replace("_", " ").title()
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def choices(cls):
|
|
18
|
+
return [(choice.value, cls.display_name(choice)) for choice in cls]
|
|
19
|
+
|
|
20
|
+
@classmethod
|
|
21
|
+
def from_value(cls, value):
|
|
22
|
+
return cls(value)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class LabeledEnum(enum.Enum):
|
|
26
|
+
label: str
|
|
27
|
+
|
|
28
|
+
def __new__(cls, value, label):
|
|
29
|
+
obj = object.__new__(cls)
|
|
30
|
+
obj._value = value
|
|
31
|
+
obj.label = label
|
|
32
|
+
return obj
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import shutil
|
|
3
|
+
|
|
4
|
+
import fsspec
|
|
5
|
+
|
|
6
|
+
from communal.sizes import COPY_BUFFER_SIZE
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def ensure_dir(d):
|
|
10
|
+
if not os.path.exists(d):
|
|
11
|
+
os.makedirs(d)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def copy_file(filename, dest, buffer_size=COPY_BUFFER_SIZE):
|
|
15
|
+
dest_kwargs = {}
|
|
16
|
+
if dest.endswith(".gz") and not filename.endswith(".gz"):
|
|
17
|
+
dest_kwargs.update(compression="gzip")
|
|
18
|
+
elif dest.endswith(".zip") and not filename.endswith(".zip"):
|
|
19
|
+
dest_kwargs.update(compression="zip")
|
|
20
|
+
|
|
21
|
+
with fsspec.open(dest, "wb", **dest_kwargs) as df:
|
|
22
|
+
with fsspec.open(filename, "rb") as sf:
|
|
23
|
+
shutil.copyfileobj(sf, df)
|
|
24
|
+
return dest
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def remove_file(filename):
|
|
28
|
+
os.unlink(filename)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class cd:
|
|
32
|
+
"""Context manager for changing the current working directory"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, path):
|
|
35
|
+
self.path = path
|
|
36
|
+
|
|
37
|
+
def __enter__(self):
|
|
38
|
+
self.saved_path = os.getcwd()
|
|
39
|
+
os.chdir(self.path)
|
|
40
|
+
|
|
41
|
+
def __exit__(self, etype, value, traceback):
|
|
42
|
+
os.chdir(self.saved_path)
|