Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/howto/force-mimetype-to-text.md
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 12 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ $ dotdrop import ~/.xinitrc
```

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
string will be sanitized for yaml. Also if the key already exists,
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,
it will be appended with `_<incremental_number>` to avoid duplicates.

If the key is not provided, it will be automatically created based on the
Expand Down Expand Up @@ -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
```
27 changes: 24 additions & 3 deletions dotdrop/templategen.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
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
Expand All @@ -51,6 +54,21 @@ def __init__(self, base='.', variables=None,
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 TypeError as e:
self.log.warn(f'{ENV_DOTDROP_MIME_TEXT} parsing: {e}')
self.mime_text = []
except AttributeError 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])
Expand Down Expand Up @@ -221,8 +239,7 @@ def _handle_file(self, src):
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'):
Expand All @@ -235,6 +252,10 @@ def _is_text(cls, fileoutput):
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):
Expand Down
20 changes: 14 additions & 6 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,20 @@ def test_lodaer(self):

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"""
Expand Down
Loading