Skip to content
Permalink
Browse files

add magic command lsrepo

  • Loading branch information...
sdpython committed Jan 3, 2015
1 parent c0cd476 commit a801cbc555bc2a718255b394af45cecb60cfd1c3
Showing with 65 additions and 3 deletions.
  1. +14 −0 _unittests/ut_files/test_files.py
  2. +51 −3 src/pyensae/file_helper/magic_file.py
@@ -73,6 +73,20 @@ def test_tail(self) :
fLOG("#####",res)
assert "unittest.main" in res[-1]

def test_files_repo(self) :
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__")
path = os.path.abspath(os.path.dirname(__file__))
mg = MagicFile()
cmd = path
fLOG("**",cmd)
res = mg.lsrepo(cmd)
fLOG(res)
if len(res) == 0:
raise FileNotFoundError("cmd: " + cmd)
res = mg.lsrepo("")
fLOG(res)
assert len(res) > 0


if __name__ == "__main__" :
unittest.main ()
@@ -9,7 +9,7 @@
from IPython.core.magic import line_cell_magic
from IPython.core.display import HTML

from pyquickhelper.filehelper.synchelper import explore_folder_iterfile
from pyquickhelper.filehelper.synchelper import explore_folder_iterfile, explore_folder_iterfile_repo
from pyquickhelper import MagicCommandParser, run_cmd
from .format_helper import format_file_size, format_file_mtime
from .content_helper import file_head, file_tail
@@ -101,7 +101,8 @@ def lsr_parser():
@line_magic
def lsr(self, line):
"""
define ``%lsr``, the method stops after around 10000 files --> you should precise the filter.
define ``%lsr`` which returns the content of a folder,
the method stops after around 10000 files --> you should precise the filter.
"""
if MagicFile._parser_lsr is None:
MagicFile._parser_lsr = MagicFile.lsr_parser()
@@ -142,7 +143,7 @@ def lsr(self, line):
@cell_magic
def PYTHON(self, line, cell = None):
"""
defines command ``%%PIG``
defines command ``%%PYTHON``
"""
if line in [None, ""] :
print("Usage:")
@@ -186,6 +187,53 @@ def runpy(self, line, cell = None):
else:
return HTML ('<pre>\n%s\n</pre>' % out)

@staticmethod
def _lsrepo_parser():
"""
defines the way to parse the magic command ``%lsrepo``
"""
parser = MagicCommandParser(description='display the content of a repository (GIT or SVN)')
parser.add_argument('path', type=str, nargs="?", help='path', default=".")
return parser
_parser_lsrepo = None

@line_magic
def lsrepo(self, line):
"""
define ``%lsrepo``, the method returns
.. versionadded:: 1.1
"""
if MagicFile._parser_lsrepo is None:
MagicFile._parser_lsrepo = MagicFile._lsrepo_parser()
parser = MagicFile._parser_lsrepo

try:
args = parser.parse_cmd(line)
except SystemExit:
print( parser.print_help() )
args = None

if args is not None:
if args.path is None or len(args.path) == 0 :
filename = "."
else:
filename = args.path

iter = explore_folder_iterfile_repo(filename)
rows = [ ]
for r in iter :
d = os.path.isfile(r)
if d :
st = os.stat(r)
r = { "name":r,
"size":format_file_size(st.st_size),
"last_modified":format_file_mtime(st.st_mtime),
"directory":False }
else:
r = { "name":r, "directory":True }
rows.append(r)
return pandas.DataFrame(rows)


def register_file_magics():

0 comments on commit a801cbc

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