Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Predict requires according to variables used
#-*- coding: utf-8 -*-
import fnmatch
import os
def glob(dir, pattern):
for root, dirnames, filenames in os.walk(dir):
for filename in fnmatch.filter(filenames, pattern):
yield os.path.join(root, filename)
def mkObject(str):
lines = [l for l in str.split('\n') if len(l.strip()) > 0]
return {k:v for (k,v) in map(lambda l: l.lstrip().split(' : '), lines)}
def format_require(require):
return '@require \'%s\'' % require
def format_requires(requires):
return '\n'.join([format_require(require) for require in requires])
def main():
for filepath in glob('src', '*.styl'):
if 'node_modules' in filepath:
continue
with open(filepath) as f:
content = f.read()
requires = []
for needle, require in search.iteritems():
if needle in content and format_require(require) not in content:
requires.append(require)
requires = sorted(list(set(requires)))
if len(requires) > 0:
print filepath
print format_requires(requires)
print
if __name__ == '__main__':
search = mkObject("""
$form : components/forms.styl
$button : components/button.styl
$empty : components/empty.styl
$form-text : components/forms.styl
$popover : components/popover.styl
$icon-16 : settings/icons.styl
$icon-24 : settings/icons.styl
$icon-spinner-blue : settings/icons.styl
$icon-spinner-grey : settings/icons.styl
$alerts : components/alerts.styl
$bar-index : settings/z-index.styl
$nav-index : settings/z-index.styl
$modal-index : settings/z-index.styl
$alert-index-mobile : settings/z-index.styl
$overlay-index : settings/z-index.styl
$file-action-menu : settings/z-index.styl
coolGrey : settings/palette.styl
dodgerBlue : settings/palette.styl
paleGrey : settings/palette.styl
charcoalGrey : settings/palette.styl
medium-screen : settings/breakpoints.styl
small-screen : settings/breakpoints.styl
large-screen : settings/breakpoints.styl
tiny-screen : settings/breakpoints.styl
$app-2panes-sticky : objects/layouts.styl
$visuallyhidden : utilities/display.styl
$hide--mob : utilities/display.styl
$selectionbar : components/selectionbar.styl
""")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.