├── .gitignore ├── LICENSE ├── README.md ├── pysource ├── __init__.py ├── __main__.py ├── conftest.py └── pysource.py ├── setup.cfg ├── setup.py └── tests ├── conftest.py └── test_pysource.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | *.egg-info 3 | **__pycache__** 4 | .pytest_cache 5 | .pytest_cache 6 | .eggs 7 | build 8 | dist 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 PyBites 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PyBites Pysource 2 | 3 | A command line tool to read Python source code. 4 | 5 | _Update 5th of Nov 2021_: we did not know it at the time, but this actually can be accomplished by Python's `inspect` module! So you might want to use that over this tool, for more info see [this Twitter thread](https://twitter.com/bbelderbos/status/1456234396810362885). 6 | 7 | 8 | ## Installation 9 | 10 | You can install `pybites-pysource` from [PyPI](https://pypi.org/project/pybites-pysource/): 11 | 12 | pip install pybites-pysource 13 | 14 | Or use `pipx` if you want to have it available globally. 15 | 16 | This tool uses Python 3.x 17 | 18 | ## Usage 19 | 20 | You can use `pybites-pysource`: 21 | 22 | ``` 23 | $ pysource -m re.match 24 | def match(pattern, string, flags=0): 25 | """Try to apply the pattern at the start of the string, returning 26 | a Match object, or None if no match was found.""" 27 | return _compile(pattern, flags).match(string) 28 | ``` 29 | 30 | To show the resulting code with paging add `-p` to the `pysource` command, so in this case: `pysource -m re.match -p`. 31 | 32 | Check out [our blog post](https://pybit.es/get-python-source.html) for a demo. 33 | 34 | ### Vim integration 35 | 36 | If you want to dump `pysource`'s output to a vertical split window I recommend installing [`ConqueTerm`](https://github.com/gingerhot/conque-term-vim) and then add this to your `.vimrc`: 37 | 38 | ``` 39 | nmap s :ConqueTermVSplit pysource -m 40 | ``` 41 | 42 | Now if you hit `s` (`,s` in my case as my Vim _leader_ key is `,`) on any object, it will open a split window with the source code (if it can be found, otherwise you will see a `ModuleNotFoundError`). 43 | 44 | ## Tests 45 | 46 | To run the tests: 47 | 48 | $ python setup.py test 49 | 50 | See: [Integrating with setuptools / python setup.py test / pytest-runner](https://docs.pytest.org/en/documentation-restructure/background/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner). 51 | 52 | --- 53 | 54 | Enjoy! 55 | -------------------------------------------------------------------------------- /pysource/__init__.py: -------------------------------------------------------------------------------- 1 | from pysource.pysource import get_object, print_source 2 | -------------------------------------------------------------------------------- /pysource/__main__.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from pysource.pysource import get_object, print_source 4 | 5 | 6 | def main(): 7 | parser = argparse.ArgumentParser(description='Read Python source.') 8 | parser.add_argument("-m", "--module", required=True, dest='module', 9 | help='module(.submodule).name') 10 | parser.add_argument("-p", "--pager", action='store_true', dest='use_pager', 11 | help='page output (like Unix more command)') 12 | args = parser.parse_args() 13 | 14 | obj = get_object(args.module) 15 | print_source(obj, pager=args.use_pager) 16 | 17 | 18 | if __name__ == '__main__': 19 | main() 20 | -------------------------------------------------------------------------------- /pysource/conftest.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PyBites-Open-Source/pysource/a90a2290ef657e4f264d06bc13b18dbbc1418ea1/pysource/conftest.py -------------------------------------------------------------------------------- /pysource/pysource.py: -------------------------------------------------------------------------------- 1 | import importlib 2 | import inspect 3 | import pydoc 4 | 5 | 6 | def get_object(arg): 7 | module_str, sep, name = arg.rpartition(".") 8 | if sep: 9 | module = importlib.import_module(module_str) 10 | return getattr(module, name) 11 | else: 12 | return importlib.import_module(name) 13 | 14 | 15 | def print_source(func, pager=False): 16 | output = pydoc.pager if pager else print 17 | output(inspect.getsource(func)) 18 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [aliases] 2 | test=pytest 3 | 4 | [tool:pytest] 5 | addopts = --verbose 6 | python_files = tests/*.py 7 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import pathlib 2 | from setuptools import setup 3 | 4 | HERE = pathlib.Path(__file__).parent 5 | README = (HERE / "README.md").read_text() 6 | 7 | setup( 8 | name="pybites-pysource", 9 | version="1.1.0", 10 | description="Read Python source code from the command line", 11 | long_description=README, 12 | long_description_content_type="text/markdown", 13 | url="https://github.com/PyBites-Open-Source/pysource", 14 | author="PyBites", 15 | author_email="support@pybit.es", 16 | license="MIT", 17 | classifiers=[ 18 | "License :: OSI Approved :: MIT License", 19 | "Programming Language :: Python :: 3", 20 | "Programming Language :: Python :: 3.9", 21 | ], 22 | packages=["pysource"], 23 | include_package_data=True, 24 | setup_requires=['pytest-runner'], 25 | tests_require=['pytest'], 26 | entry_points={ 27 | "console_scripts": [ 28 | "pysource=pysource.__main__:main", 29 | ] 30 | }, 31 | ) 32 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | choice = ''' 4 | def choice(self, seq): 5 | """Choose a random element from a non-empty sequence.""" 6 | # raises IndexError if seq is empty 7 | return seq[self._randbelow(len(seq))] 8 | ''' 9 | match = ''' 10 | def match(pattern, string, flags=0): 11 | """Try to apply the pattern at the start of the string, returning 12 | a Match object, or None if no match was found.""" 13 | return _compile(pattern, flags).match(string) 14 | ''' 15 | getsource = ''' 16 | def getsource(object): 17 | """Return the text of the source code for an object. 18 | 19 | The argument may be a module, class, method, function, traceback, frame, 20 | or code object. The source code is returned as a single string. An 21 | OSError is raised if the source code cannot be retrieved.""" 22 | lines, lnum = getsourcelines(object) 23 | return ''.join(lines) 24 | ''' 25 | this = ''' 26 | s = """Gur Mra bs Clguba, ol Gvz Crgref 27 | 28 | Ornhgvshy vf orggre guna htyl. 29 | Rkcyvpvg vf orggre guna vzcyvpvg. 30 | Fvzcyr vf orggre guna pbzcyrk. 31 | Pbzcyrk vf orggre guna pbzcyvpngrq. 32 | Syng vf orggre guna arfgrq. 33 | Fcnefr vf orggre guna qrafr. 34 | Ernqnovyvgl pbhagf. 35 | Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf. 36 | Nygubhtu cenpgvpnyvgl orngf chevgl. 37 | Reebef fubhyq arire cnff fvyragyl. 38 | Hayrff rkcyvpvgyl fvyraprq. 39 | Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff. 40 | Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg. 41 | Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu. 42 | Abj vf orggre guna arire. 43 | Nygubhtu arire vf bsgra orggre guna *evtug* abj. 44 | Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn. 45 | Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn. 46 | Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!""" 47 | 48 | d = {} 49 | for c in (65, 97): 50 | for i in range(26): 51 | d[chr(i+c)] = chr((i+13) % 26 + c) 52 | 53 | print("".join([d.get(c, c) for c in s])) 54 | ''' 55 | capwords = ''' 56 | def capwords(s, sep=None): 57 | """capwords(s [,sep]) -> string 58 | 59 | Split the argument into words using split, capitalize each 60 | word using capitalize, and join the capitalized words using 61 | join. If the optional second argument sep is absent or None, 62 | runs of whitespace characters are replaced by a single space 63 | and leading and trailing whitespace are removed, otherwise 64 | sep is used to split and join the words. 65 | 66 | """ 67 | return (sep or ' ').join(x.capitalize() for x in s.split(sep)) 68 | ''' 69 | SOURCE_CODE = dict( 70 | choice=choice, 71 | match=match, 72 | getsource=getsource, 73 | this=this, 74 | capwords=capwords 75 | ) 76 | 77 | 78 | @pytest.fixture(scope='module') 79 | def source_code(): 80 | return SOURCE_CODE 81 | -------------------------------------------------------------------------------- /tests/test_pysource.py: -------------------------------------------------------------------------------- 1 | import collections 2 | from inspect import getsource 3 | from os.path import join 4 | from pathlib import PurePath 5 | from re import match 6 | from random import sample, choice 7 | from string import capwords 8 | import this 9 | 10 | import pytest 11 | 12 | from pysource import get_object, print_source 13 | 14 | 15 | @pytest.mark.parametrize("arg, expected", [ 16 | ("random.sample", sample), 17 | ("pathlib.PurePath", PurePath), 18 | ("os.path.join", join), 19 | ("collections", collections), 20 | ]) 21 | def test_get_object(arg, expected): 22 | assert get_object(arg) is expected 23 | 24 | 25 | @pytest.mark.parametrize("func", [ 26 | choice, 27 | match, 28 | getsource, 29 | capwords, 30 | this # works for modules now too 31 | ]) 32 | def test_print_source(func, capfd, source_code): 33 | """pager=True gives the same result in terms of output""" 34 | print_source(func) 35 | 36 | output = capfd.readouterr()[0].strip() 37 | actual = [line.lstrip() for line in output.splitlines()] 38 | 39 | src = source_code.get(func.__name__).strip() 40 | expected = [line.lstrip() for line in src.splitlines()] 41 | 42 | assert actual == expected 43 | --------------------------------------------------------------------------------