Package cheesecake :: Module cheesecake_index
[hide private]
[frames] | no frames]

Module cheesecake_index
source code

Cheesecake: How tasty is your code?

The idea of the Cheesecake project is to rank Python packages based on various empirical "kwalitee" factors, such as:



Classes [hide private]
NameSetter  
Index Class describing one index.
OneOf  
FilesIndex  
IndexUrlDownload Give points for successful downloading of a package.
IndexUnpack Give points for successful unpacking of a package archive.
IndexUnpackDir Check if package unpack directory resembles package archive name.
IndexSetupPy Reward packages that have setup.py file.
IndexInstall Check if package can be installed via "python setup.py" command.
IndexPyPIDownload Check if package was successfully downloaded from PyPI and how far from it actual package was.
IndexGeneratedFiles Lower score for automatically generated files that should not be present in a package.
IndexInstallability  
IndexRequiredFiles Check for existence of important files, like README or INSTALL.
IndexDocstrings Compute how many objects have relevant docstrings.
IndexFormattedDocstrings Compute how many of existing docstrings include any formatting, like epytext or reST.
IndexDocumentation  
IndexUnitTests Compute unittest index as percentage of methods/functions that are exercised in unit tests.
IndexUnitTested Check if the package have unit tests which can be easily found by any of known test frameworks.
IndexPyLint Compute pylint index of the whole package.
IndexCodeKwalitee  
CheesecakeError Custom exception class for Cheesecake-specific errors.
CheesecakeIndex  
Step Single step during computation of package score.
StepByVariable Step which is always run if given Cheesecake instance variable is true.
Cheesecake Computes 'goodness' of Python packages.

Functions [hide private]
  sorted(L)
  isiterable(obj)
Check whether object is iterable.
  has_extension(filename, ext)
Check if filename has given extension.
  discover_file_type(filename)
Discover type of a file according to its name and its parent directory.
  get_files_of_type(file_list, file_type)
Return files from file_list that match given file_type.
  get_package_name_from_path(path)
Get package name as file portion of path.
  get_package_name_from_url(url)
Use urlparse to obtain package name from URL.
  get_package_name_and_type(package, known_extensions)
Return package name and type.
  get_method_arguments(method)
Return tuple of arguments for given method, excluding self.
  get_attributes(obj, names)
Return attributes dictionary with keys from names.
  camel2underscore(name)
Convert name from CamelCase to underscore_name.
  index_class_to_name(clsname)
Covert index class name to index name.
  is_empty(path)
Returns True if file or directory pointed by path is empty.
  strip_dir_part(path, root)
Strip root part from path.
  get_files_dirs_list(root)
Return list of all files and directories below root.
  length(L)
Overall length of all strings in list.
  generate_arguments(arguments, max_length)
Pass list of strings in chunks of size not greater than max_length.
  make_indices_dict(indices)
  WithOptionalExt(name, extensions)
Handy way of writing Cheese rules for files with extensions.
  Doc(name)
  process_cmdline_args()
Parse command-line options.
  main()
Display Cheesecake index for package specified via command-line options.

Function Details [hide private]

sorted(L)

source code 

isiterable(obj)

source code 

Check whether object is iterable.

>>> isiterable([1,2,3])
True
>>> isiterable("string")
True
>>> isiterable(object)
False

has_extension(filename, ext)

source code 

Check if filename has given extension.

>>> has_extension("foobar.py", ".py")
True
>>> has_extension("foo.bar.py", ".py")
True
>>> has_extension("foobar.pyc", ".py")
False
This function is case insensitive.
>>> has_extension("FOOBAR.PY", ".py")
True

discover_file_type(filename)

source code 

Discover type of a file according to its name and its parent directory.

Currently supported file types:
  • pyc
  • pyo
  • module: .py files of an application
  • demo: .py files for documentation/demonstration purposes
  • test: .py files used for testing
  • special: .py file for special purposes
>>> discover_file_type('module.py')
'module'
>>> discover_file_type('./setup.py')
'special'
>>> discover_file_type('some/directory/junk.pyc')
'pyc'
>>> discover_file_type('examples/readme.txt')
>>> discover_file_type('examples/runthis.py')
'demo'
>>> discover_file_type('optimized.pyo')
'pyo'
>>> test_files = ['ut/test_this_and_that.py',
...               'another_test.py',
...               'TEST_MY_MODULE.PY']
>>> for filename in test_files:
...     assert discover_file_type(filename) == 'test', filename
>>> discover_file_type('this_is_not_a_test_really.py')
'module'

Note: This function only check file's name, and doesn't touch the filesystem. If you have to, check if file exists by yourself.

get_files_of_type(file_list, file_type)

source code 

Return files from file_list that match given file_type.

>>> file_list = ['test/test_foo.py', 'setup.py', 'README', 'test/test_bar.py']
>>> get_files_of_type(file_list, 'test')
['test/test_foo.py', 'test/test_bar.py']

get_package_name_from_path(path)

source code 

Get package name as file portion of path.

>>> get_package_name_from_path('/some/random/path/package.tar.gz')
'package.tar.gz'
>>> get_package_name_from_path('/path/underscored_name.zip')
'underscored_name.zip'
>>> get_package_name_from_path('/path/unknown.extension.txt')
'unknown.extension.txt'

get_package_name_from_url(url)

source code 

Use urlparse to obtain package name from URL.

>>> get_package_name_from_url('http://www.example.com/file.tar.bz2')
'file.tar.bz2'
>>> get_package_name_from_url('https://www.example.com/some/dir/file.txt')
'file.txt'

get_package_name_and_type(package, known_extensions)

source code 

Return package name and type.

Package type must exists in known_extensions list. Otherwise None is returned.

>>> extensions = ['tar.gz', 'zip']
>>> get_package_name_and_type('underscored_name.zip', extensions)
('underscored_name', 'zip')
>>> get_package_name_and_type('unknown.extension.txt', extensions)

get_method_arguments(method)

source code 

Return tuple of arguments for given method, excluding self.

>>> class Class:
...     def method(s, arg1, arg2, other_arg):
...         pass
>>> get_method_arguments(Class.method)
('arg1', 'arg2', 'other_arg')

get_attributes(obj, names)

source code 

Return attributes dictionary with keys from names.

Object is queried for each attribute name, if it doesn't have this attribute, default value None will be returned.

>>> class Class:
...     pass
>>> obj = Class()
>>> obj.attr = True
>>> obj.value = 13
>>> obj.string = "Hello"
>>> d = get_attributes(obj, ['attr', 'string', 'other'])
>>> d == {'attr': True, 'string': "Hello", 'other': None}
True

camel2underscore(name)

source code 

Convert name from CamelCase to underscore_name.

>>> camel2underscore('CamelCase')
'camel_case'
>>> camel2underscore('already_underscore_name')
'already_underscore_name'
>>> camel2underscore('BigHTMLClass')
'big_html_class'
>>> camel2underscore('')
''

index_class_to_name(clsname)

source code 

Covert index class name to index name.

>>> index_class_to_name("IndexDownload")
'download'
>>> index_class_to_name("IndexUnitTests")
'unit_tests'
>>> index_class_to_name("IndexPyPIDownload")
'py_pi_download'

is_empty(path)

source code 
Returns True if file or directory pointed by path is empty.

strip_dir_part(path, root)

source code 

Strip root part from path.

>>> strip_dir_part('/home/ruby/file', '/home')
'ruby/file'
>>> strip_dir_part('/home/ruby/file', '/home/')
'ruby/file'
>>> strip_dir_part('/home/ruby/', '/home')
'ruby/'
>>> strip_dir_part('/home/ruby/', '/home/')
'ruby/'

get_files_dirs_list(root)

source code 

Return list of all files and directories below root.

Root directory is excluded from files/directories paths.

length(L)

source code 

Overall length of all strings in list.

>>> length(['a', 'bc', 'd', '', 'efg'])
7

generate_arguments(arguments, max_length)

source code 

Pass list of strings in chunks of size not greater than max_length.

>>> for x in generate_arguments(['abc', 'def'], 4):
...     print x
['abc']
['def']
>>> for x in generate_arguments(['a', 'bc', 'd', 'e', 'f'], 2):
...     print x
['a']
['bc']
['d', 'e']
['f']
If a single argument is larger than max_length, ValueError is raised.
>>> L = []
>>> for x in generate_arguments(['abc', 'de', 'fghijk', 'l'], 4):
...     L.append(x)
Traceback (most recent call last):
  ...
ValueError: Argument 'fghijk' larger than 4.
>>> L
[['abc'], ['de']]

make_indices_dict(indices)

source code 

WithOptionalExt(name, extensions)

source code 

Handy way of writing Cheese rules for files with extensions.

Instead of writing:
>>> one_of = OneOf('readme', 'readme.html', 'readme.txt')
Write this:
>>> opt_ext = WithOptionalExt('readme', ['html', 'txt'])
It means the same! (representation have a meaning)
>>> str(one_of) == str(opt_ext)
True

Doc(name)

source code 

process_cmdline_args()

source code 
Parse command-line options.

main()

source code 
Display Cheesecake index for package specified via command-line options.