Skip to content
Permalink
Browse files

add statistics on code (number of lines, number of files)

  • Loading branch information...
sdpython committed Dec 13, 2014
1 parent 2e46061 commit 5851ad751ef044be7b39871b258925e90cf0a92d
@@ -46,6 +46,7 @@ Versions
* **fix:** better behavior while running the unit test
* **change:** catch warnings when running the unit tests
* **change:** expose the function :func:`process_notebooks <pyquickhelper.helpgen.sphinx_main.process_notebooks>` to convert a notebook into html, pdf, rst, docx formats
* **add:** add simple statistics while generation the documentation
* **0.8 - 2014/11/03**
* **add:** Python version is now checked, ImportError is raised if it used on Python 2
* **new:** function :func:`run_doc_server <pyquickhelper.serverdoc.documentation_server.run_doc_server>` creates a local server to display documentation
@@ -28,13 +28,13 @@ Functionalities
Indices and tables
==================

+------------------+---------------------+---------------------+------------------+------------------------+------------------------------------------------+
| :ref:`l-modules` | :ref:`l-functions` | :ref:`l-classes` | :ref:`l-methods` | :ref:`l-staticmethods` | :ref:`l-properties` |
+------------------+---------------------+---------------------+------------------+------------------------+------------------------------------------------+
| :ref:`genindex` | :ref:`modindex` | :ref:`search` | :ref:`l-license` | :ref:`l-changes` | :ref:`l-README` |
+------------------+---------------------+---------------------+------------------+------------------------+------------------------------------------------+
| :ref:`l-example` | :ref:`l-FAQ` | :ref:`l-notebooks` | | | `Unit Test Coverage <coverage/index.html>`_ |
+------------------+---------------------+---------------------+------------------+------------------------+------------------------------------------------+
+------------------+---------------------+---------------------+--------------------+------------------------+------------------------------------------------+
| :ref:`l-modules` | :ref:`l-functions` | :ref:`l-classes` | :ref:`l-methods` | :ref:`l-staticmethods` | :ref:`l-properties` |
+------------------+---------------------+---------------------+--------------------+------------------------+------------------------------------------------+
| :ref:`genindex` | :ref:`modindex` | :ref:`search` | :ref:`l-license` | :ref:`l-changes` | :ref:`l-README` |
+------------------+---------------------+---------------------+--------------------+------------------------+------------------------------------------------+
| :ref:`l-example` | :ref:`l-FAQ` | :ref:`l-notebooks` | | :ref:`l-statcode` | `Unit Test Coverage <coverage/index.html>`_ |
+------------------+---------------------+---------------------+--------------------+------------------------+------------------------------------------------+

Navigation
==========
@@ -55,4 +55,5 @@ Navigation
license
filechanges
README
all_indexes
all_indexes
all_report
@@ -70,6 +70,7 @@ def test_full_documentation (self) :
os.path.join(temp, "all_example_otherpageofexamples.rst"),
os.path.join(temp, "all_FAQ.rst"),
os.path.join(temp, "all_example_pagewithanaccentinthetitle.rst"),
os.path.join(temp, "all_report.rst"),
]
for f in files :
if not os.path.exists(f) :
@@ -82,6 +83,9 @@ def test_full_documentation (self) :
with open(f, "r", encoding="utf8") as ff : content = ff.read()
if "Same page with an accent." not in content:
raise Exception("file " + f + " is empty:\n" + content)
if "report" in f :
with open(f, "r", encoding="utf8") as ff : content = ff.read()
assert ".py" in content

with open(os.path.join(temp, "all_FAQ.rst"), "r") as f : contentf = f.read()
assert "How to activate the logs?" in contentf
@@ -121,6 +121,10 @@ def _private_process_one_file(fullname, to, silent, fmod, replace_relative_impor
@param silent no logs if True
@param fmod modification functions
@param replace_relative_import replace relative import
@return extension, number of lines
.. versionchanged:: 0.9
return extension, number of lines
"""
ext = os.path.splitext(fullname)[-1]

@@ -132,11 +136,16 @@ def _private_process_one_file(fullname, to, silent, fmod, replace_relative_impor
with open(to, "wb") as f : f.write(bin)
else :
shutil.copy(fullname, to)
return os.path.splitext(fullname)[-1], 0
else :
try :
with open(fullname, "r", encoding="utf8") as g : content = g.read()
except UnicodeDecodeError:
with open(fullname, "r") as g : content = g.read()

lines = [ _.strip(" \t\n\r") for _ in content.split("\n") ]
lines = [ _ for _ in lines if len(_) > 0 ]
nblines = len(lines)

keepc = content
try :
@@ -154,6 +163,8 @@ def _private_process_one_file(fullname, to, silent, fmod, replace_relative_impor
if replace_relative_import:
content = replace_relative_import(fullname, content)
with open(to, "w", encoding="utf8") as g : g.write(content)

return os.path.splitext(fullname)[-1], nblines

def remove_undesired_part_for_documentation(content, filename):
"""
@@ -254,15 +265,16 @@ def copy_source_files ( input,
if file.name.endswith("setup.py") : continue
if "setup.py" in file.name :
raise FileNotFoundError("are you sure (setup.py)?, file: " + file.fullname)
ractions.append ( (a, file, dest))

to = os.path.join(dest, file.name)
dd = os.path.split(to)[0]
if not os.path.exists (dd) :
fLOG("copy_source_files: create ", dd, softfile = softfile, fexclude = fexclude)
os.makedirs(dd)
fLOG("copy_source_files: copy ", file.fullname, " to ", to)

_private_process_one_file(file.fullname, to, silent, fmod, replace_relative_import)
rext, rline = _private_process_one_file(file.fullname, to, silent, fmod, replace_relative_import)
ractions.append ( (a, file, dest, rext, rline))

return ractions

@@ -473,7 +485,8 @@ def add_file_rst (rootm,

memo = { }
app = []
for a,file,dest in actions :
for action in actions :
a,file,dest = action[:3]
if not isinstance (file, str) : file = file.name

to = os.path.join(dest, file)
@@ -788,6 +801,9 @@ def prepare_file_for_sphinx_help_generation (
optional_dirs = optional_dirs,
mapped_function = [ (".*[.]tohelp$", None) ] )
@endcode
.. versionchanged:: 0.9
produce a file with the number of lines and files per extension
"""
fLOG("* starting documentation preparation in",output)
rootm = os.path.abspath(output)
@@ -807,6 +823,7 @@ def prepare_file_for_sphinx_help_generation (

if os.path.isfile(src) :
_private_process_one_file(src, dst, silent, fmod_copy)

temp = os.path.split(dst)
actions_t = [ (">", temp[1], temp[0]) ]
rstadd = add_file_rst ( rootm,
@@ -885,7 +902,30 @@ def prepare_file_for_sphinx_help_generation (
with open(out, "w", encoding="utf8") as f :
f.write(v)
rsts.append ( RstFileHelp (None, out, None) )


# geneates a table with the number of lines per extension
rows = []
for act in actions:
v = 1 if act[-1] > 0 else 0
rows.append ( act[-2:] + (v,) )
from pandas import DataFrame
df = DataFrame(data=rows, columns=["extension", "nb lines", "nb files"])
df = df.groupby("extension", as_index=False).sum().sort("extension")

all_report = os.path.join(output, "all_report.rst")
with open(all_report,"w") as falli :
falli.write("\n")
falli.write(".. _l-statcode:\n")
falli.write("\n")
falli.write("Statistics on code\n")
falli.write("==================\n")
falli.write("\n\n")
sph = df_to_rst(df)
falli.write(sph)
falli.write("\n")
rsts.append( RstFileHelp(None, all_report, None))

# all indexes
all_index = os.path.join(output, "all_indexes.rst")
with open(all_index,"w") as falli :
falli.write("\n")

0 comments on commit 5851ad7

Please sign in to comment.
You can’t perform that action at this time.