Last active
          October 16, 2022 06:38 
        
      - 
      
 - 
        
Save Kentzo/42f7e3a7476baef2b42dbf1eab5bc1c8 to your computer and use it in GitHub Desktop.  
Revisions
- 
        
Kentzo revised this gist
Oct 16, 2022 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,11 +33,10 @@ def file_proc(): @contextlib.contextmanager def file_typer(): proc = file_proc() def getter(path): proc.send(None) posix_type = proc.send(path.as_posix()) return posix_type with contextlib.closing(proc):  - 
        
Kentzo revised this gist
Oct 12, 2022 . No changes.There are no files selected for viewing
 - 
        
Kentzo created this gist
Oct 12, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ import contextlib import subprocess def file_proc(): args = [ '/usr/bin/file', '--brief', '-E', '--no-dereference', '--no-buffer', '--preserve-date', '-00', '--mime-type', '--files-from', '-' ] with contextlib.ExitStack() as stack: proc = subprocess.Popen(args, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, preexec_fn=None, text=True) stack.callback(proc.kill) while True: file_path = yield proc.stdin.write(file_path + '\n') proc.stdin.flush() file_type = '' while (c := proc.stdout.read(1)) != '\0': file_type += c yield file_type.splitlines()[0] @contextlib.contextmanager def file_typer(): proc = file_proc() proc.send(None) def getter(path): posix_type = proc.send(path.as_posix()) proc.send(None) return posix_type with contextlib.closing(proc): yield getter with file_typer() as typer: typer('/usr/bin/python')