Skip to content

Commit

Permalink
fix. MIME Type detection is not reliable enough on YAML files to be u…
Browse files Browse the repository at this point in the history
…sed for now.
  • Loading branch information
tamere-allo-peter committed Apr 7, 2022
1 parent 2abb677 commit b67c46d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 59 deletions.
29 changes: 0 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,6 @@ To uninstall :
pipx uninstall yamlfixer-opt-nc
```

## Recommendation

`yamlfixer` can make use of the `libmagic` library to automatically
skip files that can't be YAML based on their MIME type, if this
library is installed on your system. If it is not installed, you can
install it using the method best matching your environment. The
different methods are explained on [python-magic's
homepage](https://github.com/ahupp/python-magic) and are pasted below
for convenience.

### Debian/Ubuntu

```
sudo apt-get install libmagic1
```

### Windows

You'll need DLLs for libmagic. @julian-r maintains a pypi package with the DLLs, you can fetch it with:

```
pip install python-magic-bin
```

### OSX

- When using Homebrew: `brew install libmagic`
- When using macports: `port install file`

# 🚀 Usage

This software automatically fixes some errors and warnings reported by
Expand Down
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
version = 0.5.3
version = 0.5.4
name = yamlfixer-opt-nc
description = automates the fixing of problems reported by yamllint
long_description = file: README.md
Expand Down Expand Up @@ -31,7 +31,6 @@ packages = find:
python_requires = >=3.6
include_package_data = True
install_requires =
python-magic >= 0.4.25
yamllint >= 1.26.3
setuptools

Expand Down
2 changes: 1 addition & 1 deletion yamlfixer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import time

__version__ = "0.5.3"
__version__ = "0.5.4"
__author__ = "OPT-NC"
__license__ = "GPLv3+"
__copyright__ = "Copyright (C) 2021-%s %s" % (time.strftime("%Y",
Expand Down
29 changes: 2 additions & 27 deletions yamlfixer/filefixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@
import os
import subprocess

try:
import magic
except ImportError:
sys.stderr.write(f"WARNING: python-magic or it's dependencies are not installed. Please "
"see https://github.com/ahupp/python-magic on how to set it up\n")
HASMAGIC = False
else:
HASMAGIC = True

from .constants import FIX_PASSEDLINTER, FIX_MODIFIED, FIX_FIXED, FIX_SKIPPED, FIX_PERMERROR
from .constants import FIXER_HANDLED
from .constants import EXIT_PROBLEM
Expand All @@ -47,9 +38,6 @@
]


class NotYAMLError(Exception):
"""An exception for files which can't be YAML formatted because of their mime type."""

class FileFixer: # pylint: disable=too-many-instance-attributes
"""To hold file fixing logic."""
def __init__(self, yamlfixer, filename):
Expand Down Expand Up @@ -112,26 +100,13 @@ def load(self):
except KeyboardInterrupt:
self.yfixer.error("\nInterrupted at user's request.")
self.incontents = "" # Initialized but empty
else:
if HASMAGIC:
mimetype = magic.from_buffer(self.incontents, mime=True)
if mimetype not in ALLOWEDMIMETYPES:
self.incontents = "" # Empty it
raise NotYAMLError(mimetype)
else:
try:
with open(self.filename, 'r') as yamlfile:
if HASMAGIC:
mimetype = magic.from_file(self.filename, mime=True)
if mimetype in ALLOWEDMIMETYPES:
self.incontents = yamlfile.read()
else:
raise NotYAMLError(mimetype)
else:
self.incontents = yamlfile.read()
self.incontents = yamlfile.read()
except (FileNotFoundError, PermissionError) as msg:
self.yfixer.error(f"{msg}")
except (UnicodeDecodeError, IsADirectoryError, NotYAMLError) as msg:
except (UnicodeDecodeError, IsADirectoryError) as msg:
self.yfixer.error(f"{self.filename} doesn't seem to be YAML : {msg}")

def dump(self, outcontents):
Expand Down

0 comments on commit b67c46d

Please sign in to comment.