Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jfischer committed Jan 13, 2020
1 parent 4b7855f commit b571b89
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions dataworkspaces/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def print_snapshot_history(workspace:SnapshotWorkspaceMixin, reverse:bool=True,
columns = {'Hash':hashes, 'Tags':tags, 'Created':created}
for m in metric_names:
columns[m] = metrics[m]
spec[m] = ColSpec(width=25, truncate=True)
columns['Message'] = messages
click.echo("\n")
print_columns(columns, null_value='', spec=spec,
Expand Down
10 changes: 6 additions & 4 deletions dataworkspaces/resources/git_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
checkout_and_apply_commit, GIT_EXE_PATH,\
is_git_repo, commit_changes_in_repo_subdir,\
checkout_subdir_and_apply_commit, get_subdirectory_hash,\
is_pull_needed_from_remote, git_remove_subtree, git_commit
is_pull_needed_from_remote, git_remove_subtree, git_commit,\
is_git_staging_dirty
from dataworkspaces.utils.git_fat_utils import \
is_a_git_fat_repo,\
has_git_fat_been_initialized, validate_git_fat_in_path,\
Expand Down Expand Up @@ -119,9 +120,10 @@ def upload_file(self, src_local_path:str,
assert rel_to_repo_path is not None
call_subprocess([GIT_EXE_PATH, 'add', rel_to_repo_path],
cwd=self.repo_dir, verbose=self.workspace.verbose)
call_subprocess([GIT_EXE_PATH, 'commit',
'-m', "Added %s" % rel_to_repo_path],
cwd=self.repo_dir, verbose=self.workspace.verbose)
if is_git_staging_dirty(self.repo_dir, rel_to_repo_path):
call_subprocess([GIT_EXE_PATH, 'commit',
'-m', "Added %s" % rel_to_repo_path],
cwd=self.repo_dir, verbose=self.workspace.verbose)
if self.workspace.verbose:
click.echo("%s: Copied file to %s" % (self.name, rel_dest_path))

Expand Down
47 changes: 39 additions & 8 deletions dataworkspaces/utils/print_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,25 @@ class ColSpec(NamedTuple):
precision: Optional[int] = None
width: Optional[int] = None # if spec, specifies exact width. Does not include
# one spec padding each side
alignment: str = 'left'
truncate: bool = False # If True and width is specified, truncate column
# rather than wrapping it.
alignment: str = 'auto'

def _truncate(s, width):
truncated = False
if '\n' in s:
s = s[0, s.index('\n')] # tuncate at the first newline
truncated = True
if len(s)>(width-2):
s = s[0:width-2]
truncated = True
if truncated:
s += '..'
return s

def pad_left(s, width):
def pad_left(s, width, truncate=False):
if truncate:
s = _truncate(s, width)
if '\n' in s:
return '\n'.join([pad_left(fragment, width) for fragment in s.split('\n')])
if len(s)==width:
Expand All @@ -28,7 +43,9 @@ def pad_left(s, width):
wrapped.append(pad_left(s, width))
return '\n'.join(wrapped)

def pad_right(s, width):
def pad_right(s, width, truncate=False):
if truncate:
s = _truncate(s, width)
if '\n' in s:
return '\n'.join([pad_right(fragment, width) for fragment in s.split('\n')])
if len(s)==width:
Expand Down Expand Up @@ -77,6 +94,7 @@ def format_columns(columns:Dict[str,Any], precision=-1, null_value:str='None',
max_value_width = 0
strvalues = []
all_numeric = True
truncate = False
# find the maxium width, determine if numeric, and do rounding
for v in values:
if isinstance(v, float):
Expand All @@ -101,19 +119,32 @@ def format_columns(columns:Dict[str,Any], precision=-1, null_value:str='None',
if len_s>max_value_width:
max_value_width = len_s
strvalues.append(s)
width_needed = max(len(col), max_value_width)
if cspec is not None and cspec.width is not None:
colwidth = cspec.width
if width_needed>=cspec.width:
colwidth = cspec.width
if cspec.truncate:
assert cspec.width>3, "Column too short to truncate"
truncate = True
else:
# if possible, we make the column narrower than
# specified.
colwidth = width_needed
else:
colwidth = max(len(col), max_value_width)
colwidth = width_needed
headers.append(pad_right(col, colwidth)) # headers are always left aligned
widths.append(colwidth)
if (cspec is not None and cspec.alignment=='right') or \
(cspec is None and all_numeric):
if cspec is not None and cspec.alignment!='auto':
if cspec.alignment=='right':
pad_fn = pad_left
else:
pad_fn = pad_right
elif all_numeric:
pad_fn = pad_left
else:
pad_fn = pad_right
# second pass does padding and line breaks
str_cols[col] =[pad_fn(strvalue, colwidth) for strvalue in strvalues]
str_cols[col] =[pad_fn(strvalue, colwidth, truncate) for strvalue in strvalues]
return FormattedColumns(nitems, headers, str_cols, widths)


Expand Down
2 changes: 1 addition & 1 deletion docs/kits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Loading the magics
To load the magics, run the following in an interactive cell of your Jupyter Notebook::

import dataworkspaces.kits.jupyter
%load_ext dtaworkspaces.kits.jupyter
%load_ext dataworkspaces.kits.jupyter

If the load runs correctly, you should see output like this in your cell:

Expand Down

0 comments on commit b571b89

Please sign in to comment.