Skip to content

Commit

Permalink
Minor pylint corrections from landscale.io Health report
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresMWeber committed May 7, 2017
1 parent 0e5dcc1 commit b4664a9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 73 deletions.
20 changes: 12 additions & 8 deletions maya_signatures/__init__.py
Expand Up @@ -3,15 +3,19 @@

__all__ = ['commands', 'cli']

cache = None
scraper = None
CACHE = None
SCRAPER = None


def scrape(maya_commands):
global scraper
global cache
if scraper is None:
scraper = Scrape()
cache = scraper.cached
""" Generic entry point for ease of use, returns maya_signatures.commands.scrape.Scraper(<input>).query
:param maya_commands: Commands to query and return
:return:
"""
global SCRAPER
global CACHE
if SCRAPER is None:
SCRAPER = Scrape()
CACHE = SCRAPER.cached

scraper.query(maya_commands)
SCRAPER.query(maya_commands)
15 changes: 7 additions & 8 deletions maya_signatures/cli.py
@@ -1,5 +1,5 @@
"""
mayasig
mayasig.
Usage:
mayasig [-m|--mayaversion VERSION] [-d|--depth DEPTH] (MAYA_CMDS ...)
Expand All @@ -26,24 +26,23 @@
from pprint import pprint
import sys
from inspect import getmembers, isclass
from . import __version__ as VERSION


def main():
"""Main CLI entry point."""
try:
arguments = docopt.docopt(__doc__, version=VERSION)
except docopt.DocoptExit as e:
arguments = docopt.docopt(__doc__)
except docopt.DocoptExit:
print('invalid operation. %s' % str(sys.argv[1:]))
raise

has_run = False
for k, v in arguments.iteritems():
if hasattr(maya_signature_commands, k):
module = getattr(maya_signature_commands, k)
for command in list(arguments):
if hasattr(maya_signature_commands, command):
module = getattr(maya_signature_commands, command)
commands = getmembers(module, isclass)
command = [command[1] for command in commands if command[0] != 'Base'][0]
command = command(arguments)
command(arguments)
has_run = True

if not has_run:
Expand Down
13 changes: 1 addition & 12 deletions maya_signatures/commands/cache.py
Expand Up @@ -67,17 +67,6 @@ def __call__(self, *args, **kwargs):
# uncachable -- for instance, passing a list as an argument.
# Better to not cache than to blow up entirely.
return self.func(*args)
'''
print 'wrapper args:'
print self, args, kwargs
key = self._key(args, kwargs)
if key not in self.cache:
print('Could not find key %s in cached values...retrieving...' % key)
self.cache[key] = self.func(*args, **kwargs)
else:
print('Retrieving cached value for input %s' % key)
return self.cache[key]
'''

def _normalize_args(self, args, kwargs):
spec = inspect.getargs(self.func.__code__).args[1:]
Expand All @@ -98,4 +87,4 @@ def __repr__(self):
def __get__(self, obj, objtype=None):
if obj is None:
return self.func
return functools.partial(self, obj)
return functools.partial(self, obj)
87 changes: 45 additions & 42 deletions maya_signatures/commands/scrape.py
Expand Up @@ -14,6 +14,9 @@


class Scrape(Base):
""" Class responsible for handling Maya help doc command queries and returns function signatures.
"""
BASEURL = 'http://help.autodesk.com/cloudhelp/{MAYAVERSION}/ENU/Maya-Tech-Docs/CommandsPython/'
_EXTENSION = 'html'
_URL_BUILDER = '{BASEURL}{COMMAND}.{EXT}'
Expand All @@ -29,7 +32,7 @@ def __init__(self, *args, **kwargs):

@property
def cache_file(self):
""" Provides the cache file path
""" Provide the cache file path
- **parameters**, **types**, **return**::
:return: str
Expand All @@ -38,7 +41,7 @@ def cache_file(self):

@property
def cached(self):
""" Provides the raw cache with urls as the dictionary keys
""" Provide the raw cache with urls as the dictionary keys
- **parameters**, **types**, **return**::
:return: dict
Expand All @@ -47,15 +50,15 @@ def cached(self):

@property
def stored_commands(self):
""" Provides a list of commands that are currently stored.
""" Provide a list of commands that are currently stored.
- **parameters**, **types**, **return**::
:return: list
"""
return list(self.command_signatures)

def run(self):
""" CLI interface command runner
""" CLI interface command runner.
- **parameters**, **types**, **return**::
:return: dict, command signatures dictionary sorted the commands as the keys
Expand All @@ -66,24 +69,8 @@ def run(self):
self._write_tempfile()
return self.command_signatures

@Memoize
def _scrape_command(self, maya_command_url):
""" Actual worker command which parses the Maya online help docs for the given command URL
- **parameters**, **types**, **return**::
:return: dict(str:dict(str:str, str:str, str:str), dict with keys of flags and each flag value is a dict
of short name 'short', data type 'data_type' and description 'description'
"""
print('Trying to find command for web page: \n\t%s' % maya_command_url)
web_page_data = requests.get(maya_command_url)
soup_data = BeautifulSoup(web_page_data.content, 'html.parser')

raw_flag_table = self._parse_flag_table(soup_data)
flags = self._compile_flag_table(raw_flag_table)
return flags

def query(self, commands):
""" Builds URLs then stores all queried commands within the instance
""" Build URLs then stores all queried commands within the instance.
:param commands: list(str) or str, valid maya command(s)
:return: None
"""
Expand All @@ -94,16 +81,8 @@ def query(self, commands):
url = self._build_url(maya_command)
self.command_signatures[maya_command] = self._scrape_command(url)

def reset_cache(self):
""" Clears the cache file of contents.
- **parameters**, **types**, **return**::
:return: None
"""
open(self._CACHE_FILE, 'w').close()

def get_command_flags(self, command):
""" Returns only the flags for the given command
""" Return only the flags for the given command.
- **parameters**, **types**, **return**::
:param command: str, maya command
Expand All @@ -113,7 +92,7 @@ def get_command_flags(self, command):
[self.command_signatures[command][flag]['short'] for flag in self.command_signatures[command]])

def build_command_stub(self, command, shortname=False, combined=False):
""" Builds a Python stub for the given command.
""" Build a Python stub for the given command.
- **parameters**, **types**, **return**::
:param command: str, valid maya command
Expand All @@ -133,6 +112,7 @@ def build_command_stub(self, command, shortname=False, combined=False):
}
kwargs = []
shortname = False if combined else shortname
docstring = ''

for k, v in iteritems(self.command_signatures[command]):
flag = k if not shortname else v['short']
Expand All @@ -143,22 +123,46 @@ def build_command_stub(self, command, shortname=False, combined=False):
data_type = v['data_type']
find_types = findall('([a-z]+)', data_type)

data_type = ', '.join([lut[ftype]+'()' for ftype in find_types])
data_type = ', '.join([lut[ftype] + '()' for ftype in find_types])
if len(find_types) > 1:
data_type = '[{TYPES}]'.format(TYPES=data_type)

flag = '{FLAG}={TYPE}'.format(FLAG=flag, TYPE=data_type)

docstring += '\n' + v['description']
kwargs.append(flag)

signature = ', '.join(kwargs)
command_line = 'def {CMD}({SIG}, *args):'.format(CMD=command, SIG=signature)
docstring = '\"\"\"\n\t{DOC}\n\t\"\"\"'.format(DOC=v['description'])
docstring = '\"\"\"\n\t{DOC}\n\t\"\"\"'.format(DOC=docstring)
body = 'pass'
return '\n\t'.join([command_line, docstring, body])

def reset_cache(self):
""" Clear the cache file of contents.
- **parameters**, **types**, **return**::
:return: None
"""
open(self._CACHE_FILE, 'w').close()

@Memoize
def _scrape_command(self, maya_command_url):
""" Actual worker command which parses the Maya online help docs for the given command URL.
- **parameters**, **types**, **return**::
:return: dict(str:dict(str:str, str:str, str:str), dict with keys of flags and each flag value is a dict
of short name 'short', data type 'data_type' and description 'description'
"""
print('Trying to find command for web page: \n\t%s' % maya_command_url)
web_page_data = requests.get(maya_command_url)
soup_data = BeautifulSoup(web_page_data.content, 'html.parser')

raw_flag_table = self._parse_flag_table(soup_data)
flags = self._compile_flag_table(raw_flag_table)
return flags

def _read_tempfile(self):
""" Attempts to read and store instance data from the cache file
""" Attempt to read and store instance data from the cache file.
:return: None
"""
try:
Expand All @@ -173,7 +177,7 @@ def _read_tempfile(self):
print('No preexisting scrape.json detected in folder %s continuing...' % self.cache_file)

def _build_url(self, command):
""" Uses class variables to synthesize a URL path to the maya help lib for the given command
""" Use class variables to synthesize a URL path to the maya help lib for the given command.
:param command: str, valid maya command
:return: str, url to the maya help lib for given command.
"""
Expand All @@ -182,7 +186,7 @@ def _build_url(self, command):
EXT=self._EXTENSION)

def _write_tempfile(self):
""" Writes instance data to the cache file
""" Writes instance data to the cache file.
:return: None
"""
f = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
Expand All @@ -194,18 +198,18 @@ def _write_tempfile(self):

@classmethod
def _parse_synopsis(cls, soup_code_object):
""" Parses the webpage for the synopsis value
""" Parse the webpage for the synopsis value.
:param soup_code_object: str, return of beautiful soup for maya help doc page
:return: list(str): list of synopsis values (should be the flags)
"""
synopses = []
for child in [child for child in soup_code_object.children]:
synopses.append(unicode(child) if not hasattr(child, 'string') else child.string)
synopses.append(str(child) if not hasattr(child, 'string') else child.string)
return synopses

@classmethod
def _parse_flag_table(cls, soup_code_object):
""" Parses (naively) the webpage for the flag table
""" Parse (naively) the webpage for the flag table.
:param soup_code_object: str, return of beautiful soup for maya help doc page
:return: list(list(str, str, str, str)): list of lists len 4 of:
flag name, short name, data type, description
Expand All @@ -229,7 +233,7 @@ def _parse_flag_table(cls, soup_code_object):

@staticmethod
def _compile_flag_table(flag_data_set):
""" Parses Takes the parsed data set from Scrape.parse_flag_table and creates a dictionary
""" Take the parsed data set from Scrape.parse_flag_table and creates a dictionary.
:param flag_data_set: list(list(str, str, str, str)): list of lists len 4 of:
flag name, short name, data type, description
:return: dict(str:dict(str:str, str:str, str:str), dict with keys of flags and each flag value is a dict
Expand All @@ -241,4 +245,3 @@ def _compile_flag_table(flag_data_set):
flags[name] = {'short': short, 'data_type': data_type, 'description': description}

return flags

1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -10,3 +10,4 @@ Sphinx==1.5.5
tox==2.7.0
twine==1.8.1
virtualenv==15.1.0
docopt==0.6.2
6 changes: 3 additions & 3 deletions setup.py
@@ -1,12 +1,12 @@
"""Packaging settings."""
from codecs import open
import codecs
from os.path import abspath, dirname, join
from setuptools import find_packages, setup

__version__ = '0.5.2'

with open(join(abspath(dirname(__file__)), 'README.rst'), encoding='utf-8') as file:
long_description = file.read()
with codecs.open(join(abspath(dirname(__file__)), 'README.rst'), encoding='utf-8') as readme_file:
long_description = readme_file.read()

setup(
name='Maya Signature Scraper',
Expand Down

0 comments on commit b4664a9

Please sign in to comment.