Exemplo n.º 1
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
Exemplo n.º 2
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)
Exemplo n.º 3
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]
Exemplo n.º 4
0
def test_can_get_docs():
    assert isinstance(d.get(Module(filename='docs/modules/module.py')), Module)
Exemplo n.º 5
0
 def _get_modules(self):
   return [
     Module(filename=x) for x in  glob.glob(os.path.join(self._dir, '*.py'))
   ]
Exemplo n.º 6
0
 def setUp(self):
     self.mod = Module(filename=__file__)
Exemplo n.º 7
0
 def test_can_construct_filenamet(self):
     with open(__file__, 'r') as file_obj:
         source = file_obj.read()
     mod = Module(source=source)
Exemplo n.º 8
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)
Exemplo n.º 9
0
 def test_can_construct_filenamet(self):
     mod = Module(path='docs.modules.module_test')
Exemplo n.º 10
0
 def test_can_construct_filenamet(self):
     mod = Module(filename=__file__)
Exemplo n.º 11
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
Exemplo n.º 12
0
 def test_get_functions(self):
   mod = Module(filename='docs/__init__.py')
   assert self._pak.source == mod.source
Exemplo n.º 13
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)]