Skip to content

Commit

Permalink
Merge pull request #123 from ipbus/enhancement/rich
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrothea authored Mar 20, 2021
2 parents c1ace5e + e14794a commit 8b69afd
Show file tree
Hide file tree
Showing 12 changed files with 362 additions and 320 deletions.
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ click-didyoumean==0.0.3
configparser==5.0.0
future==0.18.2
ipaddress==1.0.23
ipaddress
ipdb==0.13.3
ipdb==0.13.4
ipython==7.16.1
pexpect==4.8.0
psutil==5.7.2
pytest==6.0.2
pytest==6.1.1
PyYAML==5.3.1
rich==6.1.2
sh==1.14.0
texttable==1.6.3
texttable==1.6.3
36 changes: 18 additions & 18 deletions src/ipbb/cli/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def completeDepFileImpl(ctx, args, incomplete):
# print (ctx.command.params)
if ctx.params.get(cmp_argname, None) is None:
return []
env = Context()
ictx = Context()
# nothing to complete if not in an ipbb area
if env.work.path is None:
if ictx.work.path is None:
return []

lPkg, lCmp = ctx.params[cmp_argname]
basepath = env.pathMaker.getPath(lPkg, lCmp, 'include')
basepath = ictx.pathMaker.getPath(lPkg, lCmp, 'include')
if not exists(basepath):
return []

Expand All @@ -42,67 +42,67 @@ def completeDepFileImpl(ctx, args, incomplete):

# ------------------------------------------------------------------------------
def completeProject(ctx, args, incomplete):
env = Context()
ictx = Context()

# nothing to complete if not in an ipbb area
if env.work.path is None:
if ictx.work.path is None:
return []

return [proj for proj in env.projects if incomplete in proj]
return [proj for proj in ictx.projects if incomplete in proj]


# ------------------------------------------------------------------------------
def completeSrcPackage(ctx, args, incomplete):
env = Context()
ictx = Context()

# nothing to complete if not in an ipbb area
if env.work.path is None:
if ictx.work.path is None:
return []

return [pkg for pkg in env.sources if incomplete in pkg]
return [pkg for pkg in ictx.sources if incomplete in pkg]


# ------------------------------------------------------------------------------
def completeComponent(ctx, args, incomplete):
env = Context()
ictx = Context()

# nothing to complete if not in an ipbb area
if env.work.path is None:
if ictx.work.path is None:
return []

lPkgSeps = incomplete.count(':')
if lPkgSeps > 1:
return []
elif lPkgSeps == 0:
return [ (p + ':') for p in env.sources if p.startswith(incomplete) ]
# pkgs = [p for p in env.sources if p.startswith(incomplete) ]
return [ (p + ':') for p in ictx.sources if p.startswith(incomplete) ]
# pkgs = [p for p in ictx.sources if p.startswith(incomplete) ]
# comps = []
# for p in pkgs:
# comps += _findComponentsInPackage(env, p)
# comps += _findComponentsInPackage(ictx, p)

# return comps

else:
lPkg, incomp_cmp = incomplete.split(':')

# bail out if package is misspelled
if lPkg not in env.sources:
if lPkg not in ictx.sources:
return []

# Scan the package for matches with the partial component path
return _findComponentsInPackage(env, lPkg, incomp_cmp)
return _findComponentsInPackage(ictx, lPkg, incomp_cmp)

return []


# ------------------------------------------------------------------------------
def _findComponentsInPackage(env, pkg, incomp_cmp='', exclude=['.git', '.svn'], match_subdir='firmware'):
def _findComponentsInPackage(ictx, pkg, incomp_cmp='', exclude=['.git', '.svn'], match_subdir='firmware'):
"""
Helper function to find components in a package, starting from an incomplete component path
"""
lMatchingComps = []
lPkgPath = join(env.srcdir, pkg)
lPkgPath = join(ictx.srcdir, pkg)

# Resolve the list of paths to
lSearchPaths = []
Expand Down
1 change: 0 additions & 1 deletion src/ipbb/cli/proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from ._utils import completeComponent, completeProject, completeDepFile

from os.path import join, split, exists, splitext, relpath, isdir
from click import echo, style, secho


# ------------------------------------------------------------------------------
Expand Down
56 changes: 29 additions & 27 deletions src/ipbb/cmds/dep.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
isfile,
isdir,
)
from ..console import cprint
from ..tools.common import which, SmartOpen
from ..depparser import DepFormatter
from ..utils import DirSentry, printDictTable, printAlienTable
from click import echo, secho, style, confirm
from texttable import Texttable
from rich.table import Table, Column
from rich.padding import Padding

# ------------------------------------------------------------------------------
def dep(ictx, proj):
Expand Down Expand Up @@ -97,24 +98,23 @@ def report(ictx, filters):
lParser = ictx.depParser
lDepFmt = DepFormatter(lParser)

secho('* Variables', fg='blue')
cprint('* Variables', style='blue')
# printDictTable(lParser.vars, aHeader=False)
printAlienTable(lParser.settings, aHeader=False)

echo()
secho('* Dep-tree commands', fg='blue')
cprint()
cprint('* Dep-tree commands', style='blue')

lPrepend = re.compile('(^|\n)')
for k in lParser.commands:
echo(' + {0} ({1})'.format(k, len(lParser.commands[k])))
cprint(' + {0} ({1})'.format(k, len(lParser.commands[k])))
if not lParser.commands[k]:
echo()
cprint()
continue

lCmdTable = Texttable(max_width=0)
lCmdTable.header(lCmdHeaders)
lCmdTable.set_deco(Texttable.HEADER | Texttable.BORDER)
lCmdTable.set_chars(['-', '|', '+', '-'])
lCmdTable = Table(*lCmdHeaders)
# lCmdTable.set_deco(Texttable.HEADER | Texttable.BORDER)
# lCmdTable.set_chars(['-', '|', '+', '-'])
for lCmd in lParser.commands[k]:
lRow = [
relpath(lCmd.filepath, ictx.srcdir),
Expand All @@ -127,12 +127,13 @@ def report(ictx, filters):
if lFilters and not all([rxp.match(lRow[i]) for i, rxp in lFilters]):
continue

lCmdTable.add_row(lRow)
lCmdTable.add_row(*lRow)

echo(lPrepend.sub(r'\g<1> ', lCmdTable.draw()))
echo()
# cprint(lPrepend.sub(r'\g<1> ', lCmdTable.draw()))
cprint(Padding.indent(lCmdTable, 4))
cprint()

secho('Resolved packages & components', fg='blue')
cprint('Resolved packages & components', style='blue')

lString = ''

Expand All @@ -142,36 +143,37 @@ def report(ictx, filters):
lString += 'packages: ' + lDepFmt.drawPackages() + '\n'
lString += 'components:\n'
lString += lDepFmt.drawComponents()
echo(lString+'\n')
cprint(lString+'\n')

if lParser.errors:
secho("Dep tree parsing error(s):", fg='red')
echo(lDepFmt.drawParsingErrors())
cprint("Dep tree parsing error(s):", style='red')
cprint(lDepFmt.drawParsingErrors())

if lParser.unresolved:
lString = ''
if lParser.unresolvedPackages:
secho("Unresolved packages:", fg='red')
echo(lDepFmt.drawUnresolvedPackages())
echo()
cprint("Unresolved packages:", style='red')
cprint(lDepFmt.drawUnresolvedPackages())
cprint()

# ------
lCNF = lParser.unresolvedComponents
if lCNF:
secho("Unresolved components:", fg='red')
echo(lDepFmt.drawUnresolvedComponents())
echo()
cprint("Unresolved components:", style='red')
cprint(lDepFmt.drawUnresolvedComponents())
cprint()


# ------

# ------
echo(lString)
cprint(lString)

if lParser.unresolvedFiles:
secho("Unresolved files:", fg='red')
cprint("Unresolved files:", style='red')

echo(lPrepend.sub(r'\g<1> ', lDepFmt.drawUnresolvedFiles()))
# cprint(lPrepend.sub(r'\g<1> ', lDepFmt.drawUnresolvedFiles()))
cprint(Padding.indent(lDepFmt.drawUnresolvedFiles(), 4))


# ------------------------------------------------------------------------------
Expand Down
49 changes: 26 additions & 23 deletions src/ipbb/cmds/proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,35 @@


# Elements
from ..console import cprint
from ..tools.common import SmartOpen
from ..defaults import kProjAreaFile, kProjDir
from ..context import ProjectInfo
from ..utils import DirSentry, raiseError, validateComponent, findFirstParentDir
from ..depparser import depfiletypes, Pathmaker

from rich.table import Table
from os.path import join, split, exists, splitext, relpath, isdir, basename
from click import echo, style, secho
from texttable import Texttable

# ------------------------------------------------------------------------------
def info(ictx):

secho("Projects", fg='blue')
cprint("Projects", style='blue')

# lHeader = ('name', 'toolset', 'topPkg', 'topCmp', 'topDep')
# lProjTable = Texttable(120)
# lProjTable.set_deco(Texttable.HEADER | Texttable.BORDER)
# lProjTable.set_chars(['-', '|', '+', '-'])
# lProjTable.header(lHeader)

lHeader = ('name', 'toolset', 'topPkg', 'topCmp', 'topDep')
lProjTable = Texttable(120)
lProjTable.set_deco(Texttable.HEADER | Texttable.BORDER)
lProjTable.set_chars(['-', '|', '+', '-'])
lProjTable.header(lHeader)
lProjTable = Table(*lHeader)

for p in sorted(ictx.projects):
lProjInfo = ProjectInfo(join(ictx.projdir, p))
lProjTable.add_row([p] + [lProjInfo.settings[k] for k in lHeader[1:]] )
lProjTable.add_row(p, *(lProjInfo.settings[k] for k in lHeader[1:]) )

echo(lProjTable.draw())
cprint(lProjTable)


# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -63,30 +66,30 @@ def create(ictx, toolset, projname, component, topdep):
lTopPackage, lTopComponent = component

if lTopPackage not in ictx.sources:
secho('Top-level package {} not found'.format(lTopPackage), fg='red')
echo('Available packages:')
cprint('Top-level package {} not found'.format(lTopPackage), style='red')
cprint('Available packages:')
for lPkg in ictx.sources:
echo(' - ' + lPkg)
cprint(' - ' + lPkg)

raiseError("Top-level package {} not found".format(lTopPackage))

lTopComponentPath = lPathmaker.getPath(lTopPackage, lTopComponent)
if not exists(lTopComponentPath):
secho(
cprint(
"Top-level component '{}:{}'' not found".format(lTopPackage, lTopComponent),
fg='red',
style='red',
)

lParent = findFirstParentDir(lTopComponentPath, lPathmaker.getPath(lTopPackage))
secho('\nSuggestions (based on the first existing parent path)', fg='cyan')
cprint('\nSuggestions (based on the first existing parent path)', style='cyan')
# When in Py3 https://docs.python.org/3/library/os.html#os.scandir
for d in [
join(lParent, s)
for s in os.listdir(lParent)
if isdir(join(lParent, s))
]:
echo(' - ' + d)
echo()
cprint(' - ' + d)
cprint()

raise click.Abort()

Expand All @@ -111,7 +114,7 @@ def create(ictx, toolset, projname, component, topdep):
# ------------------------------------------------------------------------------
if not lTopExists:
import glob
secho('Top-level dep file {} not found or not uniquely resolved'.format(lTopDepPath), fg='red')
cprint('Top-level dep file {} not found or not uniquely resolved'.format(lTopDepPath), style='red')

lTopDepDir = lPathmaker.getPath(lTopPackage, lTopComponent, 'include')

Expand All @@ -120,9 +123,9 @@ def create(ictx, toolset, projname, component, topdep):
"'{}'".format(relpath(p, lTopDepDir))
for p in glob.glob(join(lTopDepDir, '*' + ft))
]
echo('Suggestions (*{}):'.format(ft))
cprint('Suggestions (*{}):'.format(ft))
for lC in lTopDepCandidates:
echo(' - ' + lC)
cprint(' - ' + lC)

raiseError("Top-level dependency file {} not found".format(lTopDepPath))

Expand All @@ -140,8 +143,8 @@ def create(ictx, toolset, projname, component, topdep):
}
pi.saveSettings()

secho(
'{} project area \'{}\' created'.format(toolset.capitalize(), projname), fg='green'
cprint(
'{} project area \'{}\' created'.format(toolset.capitalize(), projname), style='green'
)


Expand Down Expand Up @@ -181,6 +184,6 @@ def cd(ictx, projname, aVerbose):

os.chdir(join(ictx.projdir, projname))
if aVerbose:
echo("New current directory %s" % os.getcwd())
cprint("New current directory %s" % os.getcwd())


Loading

0 comments on commit 8b69afd

Please sign in to comment.