Add some Python utilities
This commit is contained in:
parent
97193353cb
commit
4b2e6e131d
5 changed files with 89 additions and 2 deletions
14
bin/findup
Executable file
14
bin/findup
Executable file
|
@ -0,0 +1,14 @@
|
||||||
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from mbutils import find_in_parent
|
||||||
|
|
||||||
|
try:
|
||||||
|
print(find_in_parent(*sys.argv[1:]))
|
||||||
|
sys.exit(0)
|
||||||
|
except IOError:
|
||||||
|
print("File not found in any directory", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
except TypeError:
|
||||||
|
print("No filename specified", file=sys.stderr)
|
||||||
|
sys.exit(2)
|
BIN
lib/python/__pycache__/mbutils.cpython-35.pyc
Normal file
BIN
lib/python/__pycache__/mbutils.cpython-35.pyc
Normal file
Binary file not shown.
71
lib/python/mbutils.py
Normal file
71
lib/python/mbutils.py
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from os import path
|
||||||
|
|
||||||
|
def find_in_parent(filename, starting_dir=None):
|
||||||
|
'''Look up the directory tree until you find a file.'''
|
||||||
|
if starting_dir is None:
|
||||||
|
starting_dir = os.getcwd()
|
||||||
|
|
||||||
|
candidate_file = path.join(starting_dir, filename)
|
||||||
|
|
||||||
|
if (path.isfile(candidate_file)):
|
||||||
|
return candidate_file
|
||||||
|
elif starting_dir == '/':
|
||||||
|
raise IOError('File not found in any directory')
|
||||||
|
else:
|
||||||
|
return find_in_parent(filename, path.dirname(starting_dir))
|
||||||
|
|
||||||
|
def _parse_composer_manifest(path):
|
||||||
|
'''Parse composer.json into dict'''
|
||||||
|
with open(path, 'r') as composer_file:
|
||||||
|
manifest = json.load(composer_file)
|
||||||
|
return manifest
|
||||||
|
|
||||||
|
def _get_all_autoloaders(manifest):
|
||||||
|
'''Combine all PSR-0 and PSR-4 autoloaders'''
|
||||||
|
autoloaders = dict()
|
||||||
|
|
||||||
|
for type in ['psr-0', 'psr-4']:
|
||||||
|
autoloaders.update(_get_autoloaders_by_type(manifest, type))
|
||||||
|
|
||||||
|
return dict((p, ns) for (ns, p) in autoloaders.items())
|
||||||
|
|
||||||
|
def _get_autoloaders_by_type(manifest, type):
|
||||||
|
'''Get one kind of autoloader'''
|
||||||
|
try:
|
||||||
|
return manifest['autoload'][type]
|
||||||
|
except KeyError:
|
||||||
|
return dict()
|
||||||
|
|
||||||
|
def match_path(autoloaders, dirname):
|
||||||
|
'''Find namespace for a given path'''
|
||||||
|
prefixes = sorted(autoloaders.keys(), key=len)
|
||||||
|
|
||||||
|
try:
|
||||||
|
prefix = next(p for p in prefixes if dirname.startswith(p))
|
||||||
|
namespace = autoloaders[prefix]
|
||||||
|
remainder = dirname.replace(prefix, '', 1)
|
||||||
|
except StopIteration:
|
||||||
|
namespace = ''
|
||||||
|
remainder = dirname
|
||||||
|
|
||||||
|
return (namespace, remainder)
|
||||||
|
|
||||||
|
def convert_path(dirname):
|
||||||
|
'''Naively convert path to namespace'''
|
||||||
|
return dirname.replace('/', '\\')
|
||||||
|
|
||||||
|
def get_namespace(dirname):
|
||||||
|
'''Convert dirname into namespace intelligently.'''
|
||||||
|
composer_json = find_in_parent('composer.json', dirname)
|
||||||
|
composer_manifest = _parse_composer_manifest(composer_json)
|
||||||
|
repo_root = path.dirname(composer_json)
|
||||||
|
relative_path = path.relpath(dirname, repo_root)
|
||||||
|
|
||||||
|
autoloaders = _get_all_autoloaders(composer_manifest)
|
||||||
|
|
||||||
|
(namespace, remainder) = match_path(autoloaders, relative_path)
|
||||||
|
|
||||||
|
return namespace + convert_path(remainder)
|
||||||
|
|
BIN
lib/python/mbutils.pyc
Normal file
BIN
lib/python/mbutils.pyc
Normal file
Binary file not shown.
|
@ -1,5 +1,4 @@
|
||||||
## Path
|
## Path
|
||||||
|
|
||||||
# Default path
|
# Default path
|
||||||
export PATH="/sbin"
|
export PATH="/sbin"
|
||||||
PATH="/usr/sbin:$PATH"
|
PATH="/usr/sbin:$PATH"
|
||||||
|
@ -19,11 +18,14 @@ PATH="/usr/local/Cellar/ruby/$RUBY_VERSION/bin$PATH"
|
||||||
PATH="/usr/texbin:$PATH"
|
PATH="/usr/texbin:$PATH"
|
||||||
|
|
||||||
# Local path
|
# Local path
|
||||||
PATH="$HOME/.config/bin:$PATH"
|
PATH="$HOME/Dropbox/dotfiles/bin:$PATH"
|
||||||
|
|
||||||
# Composer (PHP)
|
# Composer (PHP)
|
||||||
PATH="$HOME/.composer/vendor/bin:$PATH"
|
PATH="$HOME/.composer/vendor/bin:$PATH"
|
||||||
|
|
||||||
|
# Python
|
||||||
|
export PYTHONPATH="$PYTHONPATH:$HOME/Dropbox/dotfiles/lib/python"
|
||||||
|
|
||||||
## RVM
|
## RVM
|
||||||
|
|
||||||
source /Users/maxbucknell/.rvm/scripts/rvm
|
source /Users/maxbucknell/.rvm/scripts/rvm
|
||||||
|
|
Loading…
Add table
Reference in a new issue