1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-03-23 21:25:08 +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
committed by deadc0de
parent b8f39858aa
commit b27f3c41b1
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)

36
docs/usage.md vendored
View File

@@ -41,8 +41,8 @@ $ dotdrop import ~/.xinitrc
``` ```
You can explicitely provide the key dotdrop should use for the dotfile entry You can explicitely provide the key dotdrop should use for the dotfile entry
in the config file with the `-K --dkey` cli switch. Note that the provided in the config file with the `-K --dkey` cli switch. Note that the provided
string will be sanitized for yaml. Also if the key already exists, string will be sanitized for yaml. Also if the key already exists,
it will be appended with `_<incremental_number>` to avoid duplicates. it will be appended with `_<incremental_number>` to avoid duplicates.
If the key is not provided, it will be automatically created based on the 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
## Environment variables ## Environment variables
The following environment variables can be used to specify different CLI options. The following environment variables can be used to specify different CLI options and change behaviors.
Note that CLI switches take precedence over environment variables (except for `DOTDROP_FORCE_NODEBUG`) Note that CLI switches take precedence over environment variables
* `DOTDROP_PROFILE`: `-p`/`--profile` `DOTDROP_PROFILE`: `-p`/`--profile`
```bash ```bash
export DOTDROP_PROFILE="my-fancy-profile" export DOTDROP_PROFILE="my-fancy-profile"
``` ```
* `DOTDROP_CONFIG`: `-c`/`--cfg`
`DOTDROP_CONFIG`: `-c`/`--cfg`
```bash ```bash
export DOTDROP_CONFIG="/home/user/dotdrop/config.yaml" export DOTDROP_CONFIG="/home/user/dotdrop/config.yaml"
``` ```
* `DOTDROP_NOBANNER`: `-b`/`--no-banner`
`DOTDROP_NOBANNER`: `-b`/`--no-banner`
```bash ```bash
export DOTDROP_NOBANNER= export DOTDROP_NOBANNER=
``` ```
* `DOTDROP_DEBUG`: `-V`/`--verbose`
`DOTDROP_DEBUG`: `-V`/`--verbose`
```bash ```bash
export DOTDROP_DEBUG= 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 ```bash
export DOTDROP_FORCE_NODEBUG= 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 ```bash
export DOTDROP_TMPDIR="/tmp/dotdrop-tmp" 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 ```bash
export DOTDROP_WORKDIR="/tmp/dotdrop-workdir" export DOTDROP_WORKDIR="/tmp/dotdrop-workdir"
``` ```
* `DOTDROP_WORKERS`: overwrite the `-w`/`--workers` cli argument
`DOTDROP_WORKERS`: overwrite the `-w`/`--workers` cli argument
```bash ```bash
export DOTDROP_WORKERS="10" 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_ENV_NAME = 'env'
DICT_VARS_NAME = '_vars' DICT_VARS_NAME = '_vars'
ENV_DOTDROP_MIME_TEXT = 'DOTDROP_MIME_TEXT'
class Templategen: class Templategen:
"""dotfile templater""" """dotfile templater"""
def __init__(self, base='.', variables=None, def __init__(self, base='.', variables=None,
func_file=None, filter_file=None, debug=False): func_file=None, filter_file=None,
debug=False):
"""constructor """constructor
@base: directory path where to search for templates @base: directory path where to search for templates
@variables: dictionary of variables for templates @variables: dictionary of variables for templates
@@ -51,6 +54,17 @@ class Templategen:
self.log = Logger(debug=self.debug) self.log = Logger(debug=self.debug)
self.log.dbg('loading templategen') self.log.dbg('loading templategen')
self.variables = {} 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) loader1 = FileSystemLoader(self.base)
loader2 = FunctionLoader(self._template_loader) loader2 = FunctionLoader(self._template_loader)
loader = ChoiceLoader([loader1, loader2]) loader = ChoiceLoader([loader1, loader2])
@@ -221,8 +235,7 @@ class Templategen:
return self._handle_bin_file(src) return self._handle_bin_file(src)
return self._handle_text_file(src) return self._handle_text_file(src)
@classmethod def _is_text(self, fileoutput):
def _is_text(cls, fileoutput):
"""return if `file -b` output is ascii text""" """return if `file -b` output is ascii text"""
out = fileoutput.lower() out = fileoutput.lower()
if out.startswith('text'): if out.startswith('text'):
@@ -235,6 +248,10 @@ class Templategen:
return True return True
if 'ecmascript' in out: if 'ecmascript' in out:
return True 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 return False
def _template_loader(self, relpath): def _template_loader(self, relpath):

View File

@@ -301,12 +301,20 @@ class TestTemplateGen(unittest.TestCase):
def test_is_text(self): def test_is_text(self):
"""test is_text""" """test is_text"""
self.assertTrue(Templategen._is_text('empty')) tmpl = Templategen()
self.assertTrue(Templategen._is_text('json')) self.assertTrue(tmpl._is_text('empty'))
self.assertTrue(Templategen._is_text('javascript')) self.assertTrue(tmpl._is_text('json'))
self.assertTrue(Templategen._is_text('ecmascript')) self.assertTrue(tmpl._is_text('javascript'))
self.assertTrue(Templategen._is_text('text')) self.assertTrue(tmpl._is_text('ecmascript'))
self.assertFalse(Templategen._is_text('binary')) 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): def test_handle_bin_file(self):
"""test handle binary file""" """test handle binary file"""