Skip to content

Commit b27f3c4

Browse files
committed
add ability to force specific mime-types to text through env var
1 parent b8f3985 commit b27f3c4

4 files changed

Lines changed: 79 additions & 21 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Force mime type to text
2+
3+
The following `.gitignore` is for example considered
4+
binary by dotdrop since its mime type is `application/x-wine-extension-ini`
5+
6+
```
7+
[user]
8+
name = user
9+
email = user@example.com
10+
11+
[credential]
12+
helper = cache
13+
```
14+
15+
Dotdrop can be forced to consider specific mime types as text.
16+
Set the following environment variable:
17+
```bash
18+
export DOTDROP_MIME_TEXT=application/x-wine-extension-ini
19+
```
20+
21+
see [environment variables](../usage.md#environment-variables)

docs/usage.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ $ dotdrop import ~/.xinitrc
4141
```
4242

4343
You can explicitely provide the key dotdrop should use for the dotfile entry
44-
in the config file with the `-K --dkey` cli switch. Note that the provided
45-
string will be sanitized for yaml. Also if the key already exists,
44+
in the config file with the `-K --dkey` cli switch. Note that the provided
45+
string will be sanitized for yaml. Also if the key already exists,
4646
it will be appended with `_<incremental_number>` to avoid duplicates.
4747

4848
If the key is not provided, it will be automatically created based on the
@@ -286,38 +286,50 @@ Also, if you find it useful and have been able to successfully speed up your ope
286286

287287
## Environment variables
288288

289-
The following environment variables can be used to specify different CLI options.
290-
Note that CLI switches take precedence over environment variables (except for `DOTDROP_FORCE_NODEBUG`)
289+
The following environment variables can be used to specify different CLI options and change behaviors.
290+
Note that CLI switches take precedence over environment variables
291291

292-
* `DOTDROP_PROFILE`: `-p`/`--profile`
292+
`DOTDROP_PROFILE`: `-p`/`--profile`
293293
```bash
294294
export DOTDROP_PROFILE="my-fancy-profile"
295295
```
296-
* `DOTDROP_CONFIG`: `-c`/`--cfg`
296+
297+
`DOTDROP_CONFIG`: `-c`/`--cfg`
297298
```bash
298299
export DOTDROP_CONFIG="/home/user/dotdrop/config.yaml"
299300
```
300-
* `DOTDROP_NOBANNER`: `-b`/`--no-banner`
301+
302+
`DOTDROP_NOBANNER`: `-b`/`--no-banner`
301303
```bash
302304
export DOTDROP_NOBANNER=
303305
```
304-
* `DOTDROP_DEBUG`: `-V`/`--verbose`
306+
307+
`DOTDROP_DEBUG`: `-V`/`--verbose`
305308
```bash
306309
export DOTDROP_DEBUG=
307310
```
308-
* `DOTDROP_FORCE_NODEBUG`: disable debug output even if `-V`/`--verbose` is provided or `DOTDROP_DEBUG` is set
311+
312+
`DOTDROP_FORCE_NODEBUG`: disable debug output even if `-V`/`--verbose` is provided or `DOTDROP_DEBUG` is set
309313
```bash
310314
export DOTDROP_FORCE_NODEBUG=
311315
```
312-
* `DOTDROP_TMPDIR`: defines a temporary directory for dotdrop to use for its operations instead of using a system generated one
316+
317+
`DOTDROP_TMPDIR`: defines a temporary directory for dotdrop to use for its operations instead of using a system generated one
313318
```bash
314319
export DOTDROP_TMPDIR="/tmp/dotdrop-tmp"
315320
```
316-
* `DOTDROP_WORKDIR`: overwrite the `workdir` defined in the config
321+
322+
`DOTDROP_WORKDIR`: overwrite the `workdir` defined in the config
317323
```bash
318324
export DOTDROP_WORKDIR="/tmp/dotdrop-workdir"
319325
```
320-
* `DOTDROP_WORKERS`: overwrite the `-w`/`--workers` cli argument
326+
327+
`DOTDROP_WORKERS`: overwrite the `-w`/`--workers` cli argument
321328
```bash
322329
export DOTDROP_WORKERS="10"
323330
```
331+
332+
`DOTDROP_MIME_TEXT`: comma separated list of mime type to treat as text during templating
333+
```bash
334+
export DOTDROP_MIME_TEXT=application/x-wine-extension-ini
335+
```

dotdrop/templategen.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
DICT_ENV_NAME = 'env'
3434
DICT_VARS_NAME = '_vars'
3535

36+
ENV_DOTDROP_MIME_TEXT = 'DOTDROP_MIME_TEXT'
37+
3638

3739
class Templategen:
3840
"""dotfile templater"""
3941

4042
def __init__(self, base='.', variables=None,
41-
func_file=None, filter_file=None, debug=False):
43+
func_file=None, filter_file=None,
44+
debug=False):
4245
"""constructor
4346
@base: directory path where to search for templates
4447
@variables: dictionary of variables for templates
@@ -51,6 +54,17 @@ def __init__(self, base='.', variables=None,
5154
self.log = Logger(debug=self.debug)
5255
self.log.dbg('loading templategen')
5356
self.variables = {}
57+
self.mime_text = []
58+
if ENV_DOTDROP_MIME_TEXT in os.environ:
59+
# retrieve a comma separated list of
60+
# mime types to treat as text
61+
mimes = os.environ[ENV_DOTDROP_MIME_TEXT]
62+
try:
63+
mimes = mimes.split(',')
64+
self.mime_text = [mime.strip().lower() for mime in mimes]
65+
except Exception as e:
66+
self.log.warn(f'{ENV_DOTDROP_MIME_TEXT} parsing: {e}')
67+
self.mime_text = []
5468
loader1 = FileSystemLoader(self.base)
5569
loader2 = FunctionLoader(self._template_loader)
5670
loader = ChoiceLoader([loader1, loader2])
@@ -221,8 +235,7 @@ def _handle_file(self, src):
221235
return self._handle_bin_file(src)
222236
return self._handle_text_file(src)
223237

224-
@classmethod
225-
def _is_text(cls, fileoutput):
238+
def _is_text(self, fileoutput):
226239
"""return if `file -b` output is ascii text"""
227240
out = fileoutput.lower()
228241
if out.startswith('text'):
@@ -235,6 +248,10 @@ def _is_text(cls, fileoutput):
235248
return True
236249
if 'ecmascript' in out:
237250
return True
251+
if self.mime_text:
252+
if out in self.mime_text:
253+
self.log.dbg('mime type forced to \"text\" due to type')
254+
return True
238255
return False
239256

240257
def _template_loader(self, relpath):

tests/test_misc.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,20 @@ def test_lodaer(self):
301301

302302
def test_is_text(self):
303303
"""test is_text"""
304-
self.assertTrue(Templategen._is_text('empty'))
305-
self.assertTrue(Templategen._is_text('json'))
306-
self.assertTrue(Templategen._is_text('javascript'))
307-
self.assertTrue(Templategen._is_text('ecmascript'))
308-
self.assertTrue(Templategen._is_text('text'))
309-
self.assertFalse(Templategen._is_text('binary'))
304+
tmpl = Templategen()
305+
self.assertTrue(tmpl._is_text('empty'))
306+
self.assertTrue(tmpl._is_text('json'))
307+
self.assertTrue(tmpl._is_text('javascript'))
308+
self.assertTrue(tmpl._is_text('ecmascript'))
309+
self.assertTrue(tmpl._is_text('text'))
310+
self.assertFalse(tmpl._is_text('binary'))
311+
312+
@patch.dict(os.environ, {"DOTDROP_MIME_TEXT": "application/x-wine-extension-ini"})
313+
def test_is_text_force(self):
314+
"""test is_text with env var"""
315+
tmpl = Templategen()
316+
istext = tmpl._is_text("application/x-wine-extension-ini")
317+
self.assertTrue(istext)
310318

311319
def test_handle_bin_file(self):
312320
"""test handle binary file"""

0 commit comments

Comments
 (0)