Example #1
0
class TestModule(unittest.TestCase):
  def setUp(self):
    self.mod = Module(filename=__file__)


  def test_can_get_authors(self):
    assert any(['Michael Van Veen' in author for author in self.mod.authors]), \
      "Expected 'Michael Van Veen' to be in AUTHORS"


  def test_can_get_docstring(self):
   """Can get a module docstring"""
   with open(__file__, 'r') as file_obj:
     assert self.mod.docstring == ast.get_docstring(
      ast.parse(file_obj.read())
    ), 'Docstring does not match!'


  def test_can_get_version(self):
    #TODO: grab top-level init/include packages
    assert self.mod.version == ['0.1'], 'Unexpected version value'


  def test_can_get_credits(self):
    assert self.mod.credits == ['foo'], 'Unexpected credits value'


  def test_can_get_maintainer(self):
    assert any(['Michael Van Veen' in x for x in self.mod.maintainer]), \
      'Did not get expected maintainer!'


  def test_can_get_email(self):
    assert self.mod.email == ['*****@*****.**']


  def test_can_get_status(self):
    assert self.mod.status == ['Beta']


  def test_str(self):
    with open(__file__, 'r') as file_obj:
      assert str(self.mod) == file_obj.read()


  def test_can_get_imports(self):
    assert os in [x._import for x in self.mod.imports], \
      "Did not see os module as an import, but it is declared!"


  def test_can_get_functions(self):
    assert 'dummy_foo' in [x.name for x in self.mod.functions]


  def test_repr(self):
    print self.mod.__repr__()
    assert self.mod.__repr__() == \
      '<[Module] docs/modules/module_test.py>'
Example #2
0
class TestModule(unittest.TestCase):
    def setUp(self):
        self.mod = Module(filename=__file__)

    def test_can_get_authors(self):
        assert any(['Michael Van Veen' in author for author in self.mod.authors]), \
          "Expected 'Michael Van Veen' to be in AUTHORS"

    def test_can_get_docstring(self):
        """Can get a module docstring"""
        with open(__file__, 'r') as file_obj:
            assert self.mod.docstring == ast.get_docstring(
                ast.parse(file_obj.read())), 'Docstring does not match!'

    def test_can_get_version(self):
        #TODO: grab top-level init/include packages
        assert self.mod.version == ['0.1'], 'Unexpected version value'

    def test_can_get_credits(self):
        assert self.mod.credits == ['foo'], 'Unexpected credits value'

    def test_can_get_maintainer(self):
        assert any(['Michael Van Veen' in x for x in self.mod.maintainer]), \
          'Did not get expected maintainer!'

    def test_can_get_email(self):
        assert self.mod.email == ['*****@*****.**']

    def test_can_get_status(self):
        assert self.mod.status == ['Beta']

    def test_str(self):
        with open(__file__, 'r') as file_obj:
            assert str(self.mod) == file_obj.read()

    def test_can_get_imports(self):
        assert os in [x._import for x in self.mod.imports], \
          "Did not see os module as an import, but it is declared!"

    def test_can_get_functions(self):
        assert 'dummy_foo' in [x.name for x in self.mod.functions]

    def test_repr(self):
        print self.mod.__repr__()
        assert self.mod.__repr__() == \
          '<[Module] docs/modules/module_test.py>'
Example #3
0
  def __init__(self, filename=None):
    if not filename:
      raise ValueError

    assert os.path.isdir(filename)

    self._dir = filename

    filenames = glob.glob(os.path.join(self._dir, '__init__.py'))

    assert len(filenames) == 1, \
      'There should be one and only one __init__.py file in this directory.'

    self._init_file = Module(filename=os.path.abspath(filenames[0]))
    self._modules = None
    self._packages = None
Example #4
0
def test_construct_mod():
    with open(__file__, 'r') as file_obj:
        source = file_obj.read()

    mod = Module(source=source, ast_node=ast.parse(source))
    assert isinstance(mod.imports[0], Import)
Example #5
0
    def setUp(self):
        with open('test/imports/from_import_as_a.py', 'r') as file_obj:
            self.source = file_obj.read()

        self.mod = Module(source=self.source)
        self._import = self.mod.imports[0]
Example #6
0
def test_can_get_docs():
    assert isinstance(d.get(Module(filename='docs/modules/module.py')), Module)
Example #7
0
 def setUp(self):
   self.mod = Module(filename=__file__)
Example #8
0
 def _get_modules(self):
   return [
     Module(filename=x) for x in  glob.glob(os.path.join(self._dir, '*.py'))
   ]
Example #9
0
 def setUp(self):
     self.mod = Module(filename=__file__)
Example #10
0
 def test_can_construct_filenamet(self):
     with open(__file__, 'r') as file_obj:
         source = file_obj.read()
     mod = Module(source=source)
Example #11
0
 def test_can_construct_filenamet(self):
     with open(__file__, 'r') as file_obj:
         node = ast.parse(file_obj.read())
     mod = Module(ast_node=node)
Example #12
0
 def test_can_construct_filenamet(self):
     mod = Module(path='docs.modules.module_test')
Example #13
0
 def test_can_construct_filenamet(self):
     mod = Module(filename=__file__)
Example #14
0
def get(*args, **kw):
  """Main accessor into Python docs

  Examples

  1) Parse a live Python object

      >>> import docs
      >>> docs.get(docs)
      <[Package] docs>

  2) Parse file name

      >>> import docs as d
      >>> m  = d.get(filename='docs/modules/module.py')
      >>> m
      <[Module] docs/modules/module.py>
      >>> m.docstring
      'Wrapper object for Python modules'

  """

  item = kw.get('item') or len(args) and args[0] or None
  path = kw.get('path')
  filename = kw.get('filename')

  if not len(args) and not (item or path or filename):
    return

  if isinstance(item, basestring):
    if item in sys.modules.keys():
      return get(path=item)

  if isinstance(item, (Module, Import)):
    return item

  elif path:
    path = path.split('.')
    if len(path) > 1:
      node = Node(ast.parse('from %s import %s' % ('.'.join(path[:-1]), path[-1])).body[0])
    else:
      node = Node(ast.parse('import %s' % (path[0], )).body[0])

    path = Import(node)
    return get(path._import)

  elif inspect.ismodule(item):
    file_str = inspect.getsourcefile(item)
    return get(filename=file_str)

  elif filename:
    if os.path.isdir(filename):
      return Package(filename=filename)
    elif os.path.split(filename)[-1] == '__init__.py':
      return Package(filename=os.path.dirname(filename))
    return Module(filename=filename)

  elif isinstance(item, ast.AST):
    if isinstance(item, ast.Module):
      return Module(ast_node=item)

    elif isinstance(item, (ast.Import, ast.ImportFrom)):
      return Import(Node(item))

    elif isinstance(item, ast.ClassDef):
      return Class(item)

    elif isinstance(item, ast.FunctionDef):
      return Function(item)

    return Node(item)


  elif isinstance(item, (list, tuple)):
    return [get(y) for y in item]

  elif inspect.isclass(item):
    source = inspect.getsource(item)
    return Class(source=source)


  elif inspect.isfunction(item):
    source = inspect.getsource(item)
    return Function(source=source)

  return item
Example #15
0
 def test_get_functions(self):
   mod = Module(filename='docs/__init__.py')
   assert self._pak.source == mod.source
Example #16
0
 def test_get_functions(self):
   mod = Module(filename='docs/__init__.py')
   assert [x.name == y.name for x, y in zip(mod.functions, self._pak.functions)]