1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-03-22 19:05:07 +00:00

add ability to force specific mime-types to text through env var

This commit is contained in:
deadc0de6
2026-03-10 22:33:10 +01:00
parent b8f39858aa
commit 31c0e50cd5
4 changed files with 79 additions and 21 deletions

21
docs/howto/force-mimetype-to-text.md vendored Normal file
View File

@@ -0,0 +1,21 @@
# Force mime type to text
The following `.gitignore` is for example considered
binary by dotdrop since its mime type is `application/x-wine-extension-ini`
```
[user]
name = user
email = user@example.com
[credential]
helper = cache
```
Dotdrop can be forced to consider specific mime types as text.
Set the following environment variable:
```bash
export DOTDROP_MIME_TEXT=application/x-wine-extension-ini
```
see [environment variables](../usage.md#environment-variables)

32
docs/usage.md vendored
View File

@@ -286,38 +286,50 @@ Also, if you find it useful and have been able to successfully speed up your ope
## Environment variables
The following environment variables can be used to specify different CLI options.
Note that CLI switches take precedence over environment variables (except for `DOTDROP_FORCE_NODEBUG`)
The following environment variables can be used to specify different CLI options and change behaviors.
Note that CLI switches take precedence over environment variables
* `DOTDROP_PROFILE`: `-p`/`--profile`
`DOTDROP_PROFILE`: `-p`/`--profile`
```bash
export DOTDROP_PROFILE="my-fancy-profile"
```
* `DOTDROP_CONFIG`: `-c`/`--cfg`
`DOTDROP_CONFIG`: `-c`/`--cfg`
```bash
export DOTDROP_CONFIG="/home/user/dotdrop/config.yaml"
```
* `DOTDROP_NOBANNER`: `-b`/`--no-banner`
`DOTDROP_NOBANNER`: `-b`/`--no-banner`
```bash
export DOTDROP_NOBANNER=
```
* `DOTDROP_DEBUG`: `-V`/`--verbose`
`DOTDROP_DEBUG`: `-V`/`--verbose`
```bash
export DOTDROP_DEBUG=
```
* `DOTDROP_FORCE_NODEBUG`: disable debug output even if `-V`/`--verbose` is provided or `DOTDROP_DEBUG` is set
`DOTDROP_FORCE_NODEBUG`: disable debug output even if `-V`/`--verbose` is provided or `DOTDROP_DEBUG` is set
```bash
export DOTDROP_FORCE_NODEBUG=
```
* `DOTDROP_TMPDIR`: defines a temporary directory for dotdrop to use for its operations instead of using a system generated one
`DOTDROP_TMPDIR`: defines a temporary directory for dotdrop to use for its operations instead of using a system generated one
```bash
export DOTDROP_TMPDIR="/tmp/dotdrop-tmp"
```
* `DOTDROP_WORKDIR`: overwrite the `workdir` defined in the config
`DOTDROP_WORKDIR`: overwrite the `workdir` defined in the config
```bash
export DOTDROP_WORKDIR="/tmp/dotdrop-workdir"
```
* `DOTDROP_WORKERS`: overwrite the `-w`/`--workers` cli argument
`DOTDROP_WORKERS`: overwrite the `-w`/`--workers` cli argument
```bash
export DOTDROP_WORKERS="10"
```
`DOTDROP_MIME_TEXT`: comma separated list of mime type to treat as text during templating
```bash
export DOTDROP_MIME_TEXT=application/x-wine-extension-ini
```

View File

@@ -33,12 +33,15 @@ LOG = Logger()
DICT_ENV_NAME = 'env'
DICT_VARS_NAME = '_vars'
ENV_DOTDROP_MIME_TEXT = 'DOTDROP_MIME_TEXT'
class Templategen:
"""dotfile templater"""
def __init__(self, base='.', variables=None,
func_file=None, filter_file=None, debug=False):
func_file=None, filter_file=None,
debug=False):
"""constructor
@base: directory path where to search for templates
@variables: dictionary of variables for templates
@@ -51,6 +54,17 @@ class Templategen:
self.log = Logger(debug=self.debug)
self.log.dbg('loading templategen')
self.variables = {}
self.mime_text = []
if ENV_DOTDROP_MIME_TEXT in os.environ:
# retrieve a comma separated list of
# mime types to treat as text
mimes = os.environ[ENV_DOTDROP_MIME_TEXT]
try:
mimes = mimes.split(',')
self.mime_text = [mime.strip().lower() for mime in mimes]
except Exception as e:
self.log.warn(f'{ENV_DOTDROP_MIME_TEXT} parsing: {e}')
self.mime_text = []
loader1 = FileSystemLoader(self.base)
loader2 = FunctionLoader(self._template_loader)
loader = ChoiceLoader([loader1, loader2])
@@ -221,8 +235,7 @@ class Templategen:
return self._handle_bin_file(src)
return self._handle_text_file(src)
@classmethod
def _is_text(cls, fileoutput):
def _is_text(self, fileoutput):
"""return if `file -b` output is ascii text"""
out = fileoutput.lower()
if out.startswith('text'):
@@ -235,6 +248,10 @@ class Templategen:
return True
if 'ecmascript' in out:
return True
if self.mime_text:
if out in self.mime_text:
self.log.dbg('mime type forced to \"text\" due to type')
return True
return False
def _template_loader(self, relpath):

View File

@@ -301,12 +301,20 @@ class TestTemplateGen(unittest.TestCase):
def test_is_text(self):
"""test is_text"""
self.assertTrue(Templategen._is_text('empty'))
self.assertTrue(Templategen._is_text('json'))
self.assertTrue(Templategen._is_text('javascript'))
self.assertTrue(Templategen._is_text('ecmascript'))
self.assertTrue(Templategen._is_text('text'))
self.assertFalse(Templategen._is_text('binary'))
tmpl = Templategen()
self.assertTrue(tmpl._is_text('empty'))
self.assertTrue(tmpl._is_text('json'))
self.assertTrue(tmpl._is_text('javascript'))
self.assertTrue(tmpl._is_text('ecmascript'))
self.assertTrue(tmpl._is_text('text'))
self.assertFalse(tmpl._is_text('binary'))
@patch.dict(os.environ, {"DOTDROP_MIME_TEXT": "application/x-wine-extension-ini"})
def test_is_text_force(self):
"""test is_text with env var"""
tmpl = Templategen()
istext = tmpl._is_text("application/x-wine-extension-ini")
self.assertTrue(istext)
def test_handle_bin_file(self):
"""test handle binary file"""