id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
51
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
21,500
craffel/mir_eval
mir_eval/hierarchy.py
_compare_frame_rankings
def _compare_frame_rankings(ref, est, transitive=False): '''Compute the number of ranking disagreements in two lists. Parameters ---------- ref : np.ndarray, shape=(n,) est : np.ndarray, shape=(n,) Reference and estimate ranked lists. `ref[i]` is the relevance score for point `i`. transitive : bool If true, all pairs of reference levels are compared. If false, only adjacent pairs of reference levels are compared. Returns ------- inversions : int The number of pairs of indices `i, j` where `ref[i] < ref[j]` but `est[i] >= est[j]`. normalizer : float The total number of pairs (i, j) under consideration. If transitive=True, then this is |{(i,j) : ref[i] < ref[j]}| If transitive=False, then this is |{i,j) : ref[i] +1 = ref[j]}| ''' idx = np.argsort(ref) ref_sorted = ref[idx] est_sorted = est[idx] # Find the break-points in ref_sorted levels, positions, counts = np.unique(ref_sorted, return_index=True, return_counts=True) positions = list(positions) positions.append(len(ref_sorted)) index = collections.defaultdict(lambda: slice(0)) ref_map = collections.defaultdict(lambda: 0) for level, cnt, start, end in zip(levels, counts, positions[:-1], positions[1:]): index[level] = slice(start, end) ref_map[level] = cnt # Now that we have values sorted, apply the inversion-counter to # pairs of reference values if transitive: level_pairs = itertools.combinations(levels, 2) else: level_pairs = [(i, i+1) for i in levels] level_pairs, lcounter = itertools.tee(level_pairs) normalizer = float(sum([ref_map[i] * ref_map[j] for (i, j) in lcounter])) if normalizer == 0: return 0, 0.0 inversions = 0 for level_1, level_2 in level_pairs: inversions += _count_inversions(est_sorted[index[level_1]], est_sorted[index[level_2]]) return inversions, float(normalizer)
python
def _compare_frame_rankings(ref, est, transitive=False): '''Compute the number of ranking disagreements in two lists. Parameters ---------- ref : np.ndarray, shape=(n,) est : np.ndarray, shape=(n,) Reference and estimate ranked lists. `ref[i]` is the relevance score for point `i`. transitive : bool If true, all pairs of reference levels are compared. If false, only adjacent pairs of reference levels are compared. Returns ------- inversions : int The number of pairs of indices `i, j` where `ref[i] < ref[j]` but `est[i] >= est[j]`. normalizer : float The total number of pairs (i, j) under consideration. If transitive=True, then this is |{(i,j) : ref[i] < ref[j]}| If transitive=False, then this is |{i,j) : ref[i] +1 = ref[j]}| ''' idx = np.argsort(ref) ref_sorted = ref[idx] est_sorted = est[idx] # Find the break-points in ref_sorted levels, positions, counts = np.unique(ref_sorted, return_index=True, return_counts=True) positions = list(positions) positions.append(len(ref_sorted)) index = collections.defaultdict(lambda: slice(0)) ref_map = collections.defaultdict(lambda: 0) for level, cnt, start, end in zip(levels, counts, positions[:-1], positions[1:]): index[level] = slice(start, end) ref_map[level] = cnt # Now that we have values sorted, apply the inversion-counter to # pairs of reference values if transitive: level_pairs = itertools.combinations(levels, 2) else: level_pairs = [(i, i+1) for i in levels] level_pairs, lcounter = itertools.tee(level_pairs) normalizer = float(sum([ref_map[i] * ref_map[j] for (i, j) in lcounter])) if normalizer == 0: return 0, 0.0 inversions = 0 for level_1, level_2 in level_pairs: inversions += _count_inversions(est_sorted[index[level_1]], est_sorted[index[level_2]]) return inversions, float(normalizer)
[ "def", "_compare_frame_rankings", "(", "ref", ",", "est", ",", "transitive", "=", "False", ")", ":", "idx", "=", "np", ".", "argsort", "(", "ref", ")", "ref_sorted", "=", "ref", "[", "idx", "]", "est_sorted", "=", "est", "[", "idx", "]", "# Find the br...
Compute the number of ranking disagreements in two lists. Parameters ---------- ref : np.ndarray, shape=(n,) est : np.ndarray, shape=(n,) Reference and estimate ranked lists. `ref[i]` is the relevance score for point `i`. transitive : bool If true, all pairs of reference levels are compared. If false, only adjacent pairs of reference levels are compared. Returns ------- inversions : int The number of pairs of indices `i, j` where `ref[i] < ref[j]` but `est[i] >= est[j]`. normalizer : float The total number of pairs (i, j) under consideration. If transitive=True, then this is |{(i,j) : ref[i] < ref[j]}| If transitive=False, then this is |{i,j) : ref[i] +1 = ref[j]}|
[ "Compute", "the", "number", "of", "ranking", "disagreements", "in", "two", "lists", "." ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/hierarchy.py#L370-L436
21,501
craffel/mir_eval
mir_eval/hierarchy.py
validate_hier_intervals
def validate_hier_intervals(intervals_hier): '''Validate a hierarchical segment annotation. Parameters ---------- intervals_hier : ordered list of segmentations Raises ------ ValueError If any segmentation does not span the full duration of the top-level segmentation. If any segmentation does not start at 0. ''' # Synthesize a label array for the top layer. label_top = util.generate_labels(intervals_hier[0]) boundaries = set(util.intervals_to_boundaries(intervals_hier[0])) for level, intervals in enumerate(intervals_hier[1:], 1): # Make sure this level is consistent with the root label_current = util.generate_labels(intervals) validate_structure(intervals_hier[0], label_top, intervals, label_current) # Make sure all previous boundaries are accounted for new_bounds = set(util.intervals_to_boundaries(intervals)) if boundaries - new_bounds: warnings.warn('Segment hierarchy is inconsistent ' 'at level {:d}'.format(level)) boundaries |= new_bounds
python
def validate_hier_intervals(intervals_hier): '''Validate a hierarchical segment annotation. Parameters ---------- intervals_hier : ordered list of segmentations Raises ------ ValueError If any segmentation does not span the full duration of the top-level segmentation. If any segmentation does not start at 0. ''' # Synthesize a label array for the top layer. label_top = util.generate_labels(intervals_hier[0]) boundaries = set(util.intervals_to_boundaries(intervals_hier[0])) for level, intervals in enumerate(intervals_hier[1:], 1): # Make sure this level is consistent with the root label_current = util.generate_labels(intervals) validate_structure(intervals_hier[0], label_top, intervals, label_current) # Make sure all previous boundaries are accounted for new_bounds = set(util.intervals_to_boundaries(intervals)) if boundaries - new_bounds: warnings.warn('Segment hierarchy is inconsistent ' 'at level {:d}'.format(level)) boundaries |= new_bounds
[ "def", "validate_hier_intervals", "(", "intervals_hier", ")", ":", "# Synthesize a label array for the top layer.", "label_top", "=", "util", ".", "generate_labels", "(", "intervals_hier", "[", "0", "]", ")", "boundaries", "=", "set", "(", "util", ".", "intervals_to_b...
Validate a hierarchical segment annotation. Parameters ---------- intervals_hier : ordered list of segmentations Raises ------ ValueError If any segmentation does not span the full duration of the top-level segmentation. If any segmentation does not start at 0.
[ "Validate", "a", "hierarchical", "segment", "annotation", "." ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/hierarchy.py#L439-L472
21,502
craffel/mir_eval
mir_eval/hierarchy.py
evaluate
def evaluate(ref_intervals_hier, ref_labels_hier, est_intervals_hier, est_labels_hier, **kwargs): '''Compute all hierarchical structure metrics for the given reference and estimated annotations. Examples -------- A toy example with two two-layer annotations >>> ref_i = [[[0, 30], [30, 60]], [[0, 15], [15, 30], [30, 45], [45, 60]]] >>> est_i = [[[0, 45], [45, 60]], [[0, 15], [15, 30], [30, 45], [45, 60]]] >>> ref_l = [ ['A', 'B'], ['a', 'b', 'a', 'c'] ] >>> est_l = [ ['A', 'B'], ['a', 'a', 'b', 'b'] ] >>> scores = mir_eval.hierarchy.evaluate(ref_i, ref_l, est_i, est_l) >>> dict(scores) {'T-Measure full': 0.94822745804853459, 'T-Measure reduced': 0.8732458222764804, 'T-Precision full': 0.96569179094693058, 'T-Precision reduced': 0.89939075137018787, 'T-Recall full': 0.93138358189386117, 'T-Recall reduced': 0.84857799953694923} A more realistic example, using SALAMI pre-parsed annotations >>> def load_salami(filename): ... "load SALAMI event format as labeled intervals" ... events, labels = mir_eval.io.load_labeled_events(filename) ... intervals = mir_eval.util.boundaries_to_intervals(events)[0] ... return intervals, labels[:len(intervals)] >>> ref_files = ['data/10/parsed/textfile1_uppercase.txt', ... 'data/10/parsed/textfile1_lowercase.txt'] >>> est_files = ['data/10/parsed/textfile2_uppercase.txt', ... 'data/10/parsed/textfile2_lowercase.txt'] >>> ref = [load_salami(fname) for fname in ref_files] >>> ref_int = [seg[0] for seg in ref] >>> ref_lab = [seg[1] for seg in ref] >>> est = [load_salami(fname) for fname in est_files] >>> est_int = [seg[0] for seg in est] >>> est_lab = [seg[1] for seg in est] >>> scores = mir_eval.hierarchy.evaluate(ref_int, ref_lab, ... est_hier, est_lab) >>> dict(scores) {'T-Measure full': 0.66029225561405358, 'T-Measure reduced': 0.62001868041578034, 'T-Precision full': 0.66844764668949885, 'T-Precision reduced': 0.63252297209957919, 'T-Recall full': 0.6523334654992341, 'T-Recall reduced': 0.60799919710921635} Parameters ---------- ref_intervals_hier : list of list-like ref_labels_hier : list of list of str est_intervals_hier : list of list-like est_labels_hier : list of list of str Hierarchical annotations are encoded as an ordered list of segmentations. Each segmentation itself is a list (or list-like) of intervals (\*_intervals_hier) and a list of lists of labels (\*_labels_hier). kwargs additional keyword arguments to the evaluation metrics. Returns ------- scores : OrderedDict Dictionary of scores, where the key is the metric name (str) and the value is the (float) score achieved. T-measures are computed in both the "full" (``transitive=True``) and "reduced" (``transitive=False``) modes. Raises ------ ValueError Thrown when the provided annotations are not valid. ''' # First, find the maximum length of the reference _, t_end = _hierarchy_bounds(ref_intervals_hier) # Pre-process the intervals to match the range of the reference, # and start at 0 ref_intervals_hier, ref_labels_hier = _align_intervals(ref_intervals_hier, ref_labels_hier, t_min=0.0, t_max=None) est_intervals_hier, est_labels_hier = _align_intervals(est_intervals_hier, est_labels_hier, t_min=0.0, t_max=t_end) scores = collections.OrderedDict() # Force the transitivity setting kwargs['transitive'] = False (scores['T-Precision reduced'], scores['T-Recall reduced'], scores['T-Measure reduced']) = util.filter_kwargs(tmeasure, ref_intervals_hier, est_intervals_hier, **kwargs) kwargs['transitive'] = True (scores['T-Precision full'], scores['T-Recall full'], scores['T-Measure full']) = util.filter_kwargs(tmeasure, ref_intervals_hier, est_intervals_hier, **kwargs) (scores['L-Precision'], scores['L-Recall'], scores['L-Measure']) = util.filter_kwargs(lmeasure, ref_intervals_hier, ref_labels_hier, est_intervals_hier, est_labels_hier, **kwargs) return scores
python
def evaluate(ref_intervals_hier, ref_labels_hier, est_intervals_hier, est_labels_hier, **kwargs): '''Compute all hierarchical structure metrics for the given reference and estimated annotations. Examples -------- A toy example with two two-layer annotations >>> ref_i = [[[0, 30], [30, 60]], [[0, 15], [15, 30], [30, 45], [45, 60]]] >>> est_i = [[[0, 45], [45, 60]], [[0, 15], [15, 30], [30, 45], [45, 60]]] >>> ref_l = [ ['A', 'B'], ['a', 'b', 'a', 'c'] ] >>> est_l = [ ['A', 'B'], ['a', 'a', 'b', 'b'] ] >>> scores = mir_eval.hierarchy.evaluate(ref_i, ref_l, est_i, est_l) >>> dict(scores) {'T-Measure full': 0.94822745804853459, 'T-Measure reduced': 0.8732458222764804, 'T-Precision full': 0.96569179094693058, 'T-Precision reduced': 0.89939075137018787, 'T-Recall full': 0.93138358189386117, 'T-Recall reduced': 0.84857799953694923} A more realistic example, using SALAMI pre-parsed annotations >>> def load_salami(filename): ... "load SALAMI event format as labeled intervals" ... events, labels = mir_eval.io.load_labeled_events(filename) ... intervals = mir_eval.util.boundaries_to_intervals(events)[0] ... return intervals, labels[:len(intervals)] >>> ref_files = ['data/10/parsed/textfile1_uppercase.txt', ... 'data/10/parsed/textfile1_lowercase.txt'] >>> est_files = ['data/10/parsed/textfile2_uppercase.txt', ... 'data/10/parsed/textfile2_lowercase.txt'] >>> ref = [load_salami(fname) for fname in ref_files] >>> ref_int = [seg[0] for seg in ref] >>> ref_lab = [seg[1] for seg in ref] >>> est = [load_salami(fname) for fname in est_files] >>> est_int = [seg[0] for seg in est] >>> est_lab = [seg[1] for seg in est] >>> scores = mir_eval.hierarchy.evaluate(ref_int, ref_lab, ... est_hier, est_lab) >>> dict(scores) {'T-Measure full': 0.66029225561405358, 'T-Measure reduced': 0.62001868041578034, 'T-Precision full': 0.66844764668949885, 'T-Precision reduced': 0.63252297209957919, 'T-Recall full': 0.6523334654992341, 'T-Recall reduced': 0.60799919710921635} Parameters ---------- ref_intervals_hier : list of list-like ref_labels_hier : list of list of str est_intervals_hier : list of list-like est_labels_hier : list of list of str Hierarchical annotations are encoded as an ordered list of segmentations. Each segmentation itself is a list (or list-like) of intervals (\*_intervals_hier) and a list of lists of labels (\*_labels_hier). kwargs additional keyword arguments to the evaluation metrics. Returns ------- scores : OrderedDict Dictionary of scores, where the key is the metric name (str) and the value is the (float) score achieved. T-measures are computed in both the "full" (``transitive=True``) and "reduced" (``transitive=False``) modes. Raises ------ ValueError Thrown when the provided annotations are not valid. ''' # First, find the maximum length of the reference _, t_end = _hierarchy_bounds(ref_intervals_hier) # Pre-process the intervals to match the range of the reference, # and start at 0 ref_intervals_hier, ref_labels_hier = _align_intervals(ref_intervals_hier, ref_labels_hier, t_min=0.0, t_max=None) est_intervals_hier, est_labels_hier = _align_intervals(est_intervals_hier, est_labels_hier, t_min=0.0, t_max=t_end) scores = collections.OrderedDict() # Force the transitivity setting kwargs['transitive'] = False (scores['T-Precision reduced'], scores['T-Recall reduced'], scores['T-Measure reduced']) = util.filter_kwargs(tmeasure, ref_intervals_hier, est_intervals_hier, **kwargs) kwargs['transitive'] = True (scores['T-Precision full'], scores['T-Recall full'], scores['T-Measure full']) = util.filter_kwargs(tmeasure, ref_intervals_hier, est_intervals_hier, **kwargs) (scores['L-Precision'], scores['L-Recall'], scores['L-Measure']) = util.filter_kwargs(lmeasure, ref_intervals_hier, ref_labels_hier, est_intervals_hier, est_labels_hier, **kwargs) return scores
[ "def", "evaluate", "(", "ref_intervals_hier", ",", "ref_labels_hier", ",", "est_intervals_hier", ",", "est_labels_hier", ",", "*", "*", "kwargs", ")", ":", "# First, find the maximum length of the reference", "_", ",", "t_end", "=", "_hierarchy_bounds", "(", "ref_interv...
Compute all hierarchical structure metrics for the given reference and estimated annotations. Examples -------- A toy example with two two-layer annotations >>> ref_i = [[[0, 30], [30, 60]], [[0, 15], [15, 30], [30, 45], [45, 60]]] >>> est_i = [[[0, 45], [45, 60]], [[0, 15], [15, 30], [30, 45], [45, 60]]] >>> ref_l = [ ['A', 'B'], ['a', 'b', 'a', 'c'] ] >>> est_l = [ ['A', 'B'], ['a', 'a', 'b', 'b'] ] >>> scores = mir_eval.hierarchy.evaluate(ref_i, ref_l, est_i, est_l) >>> dict(scores) {'T-Measure full': 0.94822745804853459, 'T-Measure reduced': 0.8732458222764804, 'T-Precision full': 0.96569179094693058, 'T-Precision reduced': 0.89939075137018787, 'T-Recall full': 0.93138358189386117, 'T-Recall reduced': 0.84857799953694923} A more realistic example, using SALAMI pre-parsed annotations >>> def load_salami(filename): ... "load SALAMI event format as labeled intervals" ... events, labels = mir_eval.io.load_labeled_events(filename) ... intervals = mir_eval.util.boundaries_to_intervals(events)[0] ... return intervals, labels[:len(intervals)] >>> ref_files = ['data/10/parsed/textfile1_uppercase.txt', ... 'data/10/parsed/textfile1_lowercase.txt'] >>> est_files = ['data/10/parsed/textfile2_uppercase.txt', ... 'data/10/parsed/textfile2_lowercase.txt'] >>> ref = [load_salami(fname) for fname in ref_files] >>> ref_int = [seg[0] for seg in ref] >>> ref_lab = [seg[1] for seg in ref] >>> est = [load_salami(fname) for fname in est_files] >>> est_int = [seg[0] for seg in est] >>> est_lab = [seg[1] for seg in est] >>> scores = mir_eval.hierarchy.evaluate(ref_int, ref_lab, ... est_hier, est_lab) >>> dict(scores) {'T-Measure full': 0.66029225561405358, 'T-Measure reduced': 0.62001868041578034, 'T-Precision full': 0.66844764668949885, 'T-Precision reduced': 0.63252297209957919, 'T-Recall full': 0.6523334654992341, 'T-Recall reduced': 0.60799919710921635} Parameters ---------- ref_intervals_hier : list of list-like ref_labels_hier : list of list of str est_intervals_hier : list of list-like est_labels_hier : list of list of str Hierarchical annotations are encoded as an ordered list of segmentations. Each segmentation itself is a list (or list-like) of intervals (\*_intervals_hier) and a list of lists of labels (\*_labels_hier). kwargs additional keyword arguments to the evaluation metrics. Returns ------- scores : OrderedDict Dictionary of scores, where the key is the metric name (str) and the value is the (float) score achieved. T-measures are computed in both the "full" (``transitive=True``) and "reduced" (``transitive=False``) modes. Raises ------ ValueError Thrown when the provided annotations are not valid.
[ "Compute", "all", "hierarchical", "structure", "metrics", "for", "the", "given", "reference", "and", "estimated", "annotations", "." ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/hierarchy.py#L630-L751
21,503
craffel/mir_eval
mir_eval/display.py
__expand_limits
def __expand_limits(ax, limits, which='x'): '''Helper function to expand axis limits''' if which == 'x': getter, setter = ax.get_xlim, ax.set_xlim elif which == 'y': getter, setter = ax.get_ylim, ax.set_ylim else: raise ValueError('invalid axis: {}'.format(which)) old_lims = getter() new_lims = list(limits) # infinite limits occur on new axis objects with no data if np.isfinite(old_lims[0]): new_lims[0] = min(old_lims[0], limits[0]) if np.isfinite(old_lims[1]): new_lims[1] = max(old_lims[1], limits[1]) setter(new_lims)
python
def __expand_limits(ax, limits, which='x'): '''Helper function to expand axis limits''' if which == 'x': getter, setter = ax.get_xlim, ax.set_xlim elif which == 'y': getter, setter = ax.get_ylim, ax.set_ylim else: raise ValueError('invalid axis: {}'.format(which)) old_lims = getter() new_lims = list(limits) # infinite limits occur on new axis objects with no data if np.isfinite(old_lims[0]): new_lims[0] = min(old_lims[0], limits[0]) if np.isfinite(old_lims[1]): new_lims[1] = max(old_lims[1], limits[1]) setter(new_lims)
[ "def", "__expand_limits", "(", "ax", ",", "limits", ",", "which", "=", "'x'", ")", ":", "if", "which", "==", "'x'", ":", "getter", ",", "setter", "=", "ax", ".", "get_xlim", ",", "ax", ".", "set_xlim", "elif", "which", "==", "'y'", ":", "getter", "...
Helper function to expand axis limits
[ "Helper", "function", "to", "expand", "axis", "limits" ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L19-L39
21,504
craffel/mir_eval
mir_eval/display.py
__get_axes
def __get_axes(ax=None, fig=None): '''Get or construct the target axes object for a new plot. Parameters ---------- ax : matplotlib.pyplot.axes, optional If provided, return this axes object directly. fig : matplotlib.figure.Figure, optional The figure to query for axes. By default, uses the current figure `plt.gcf()`. Returns ------- ax : matplotlib.pyplot.axes An axis handle on which to draw the segmentation. If none is provided, a new set of axes is created. new_axes : bool If `True`, the axis object was newly constructed. If `False`, the axis object already existed. ''' new_axes = False if ax is not None: return ax, new_axes if fig is None: import matplotlib.pyplot as plt fig = plt.gcf() if not fig.get_axes(): new_axes = True return fig.gca(), new_axes
python
def __get_axes(ax=None, fig=None): '''Get or construct the target axes object for a new plot. Parameters ---------- ax : matplotlib.pyplot.axes, optional If provided, return this axes object directly. fig : matplotlib.figure.Figure, optional The figure to query for axes. By default, uses the current figure `plt.gcf()`. Returns ------- ax : matplotlib.pyplot.axes An axis handle on which to draw the segmentation. If none is provided, a new set of axes is created. new_axes : bool If `True`, the axis object was newly constructed. If `False`, the axis object already existed. ''' new_axes = False if ax is not None: return ax, new_axes if fig is None: import matplotlib.pyplot as plt fig = plt.gcf() if not fig.get_axes(): new_axes = True return fig.gca(), new_axes
[ "def", "__get_axes", "(", "ax", "=", "None", ",", "fig", "=", "None", ")", ":", "new_axes", "=", "False", "if", "ax", "is", "not", "None", ":", "return", "ax", ",", "new_axes", "if", "fig", "is", "None", ":", "import", "matplotlib", ".", "pyplot", ...
Get or construct the target axes object for a new plot. Parameters ---------- ax : matplotlib.pyplot.axes, optional If provided, return this axes object directly. fig : matplotlib.figure.Figure, optional The figure to query for axes. By default, uses the current figure `plt.gcf()`. Returns ------- ax : matplotlib.pyplot.axes An axis handle on which to draw the segmentation. If none is provided, a new set of axes is created. new_axes : bool If `True`, the axis object was newly constructed. If `False`, the axis object already existed.
[ "Get", "or", "construct", "the", "target", "axes", "object", "for", "a", "new", "plot", "." ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L42-L79
21,505
craffel/mir_eval
mir_eval/display.py
segments
def segments(intervals, labels, base=None, height=None, text=False, text_kw=None, ax=None, **kwargs): '''Plot a segmentation as a set of disjoint rectangles. Parameters ---------- intervals : np.ndarray, shape=(n, 2) segment intervals, in the format returned by :func:`mir_eval.io.load_intervals` or :func:`mir_eval.io.load_labeled_intervals`. labels : list, shape=(n,) reference segment labels, in the format returned by :func:`mir_eval.io.load_labeled_intervals`. base : number The vertical position of the base of the rectangles. By default, this will be the bottom of the plot. height : number The height of the rectangles. By default, this will be the top of the plot (minus ``base``). text : bool If true, each segment's label is displayed in its upper-left corner text_kw : dict If ``text == True``, the properties of the text object can be specified here. See ``matplotlib.pyplot.Text`` for valid parameters ax : matplotlib.pyplot.axes An axis handle on which to draw the segmentation. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to pass to ``matplotlib.patches.Rectangle``. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' if text_kw is None: text_kw = dict() text_kw.setdefault('va', 'top') text_kw.setdefault('clip_on', True) text_kw.setdefault('bbox', dict(boxstyle='round', facecolor='white')) # Make sure we have a numpy array intervals = np.atleast_2d(intervals) seg_def_style = dict(linewidth=1) ax, new_axes = __get_axes(ax=ax) if new_axes: ax.set_ylim([0, 1]) # Infer height if base is None: base = ax.get_ylim()[0] if height is None: height = ax.get_ylim()[1] cycler = ax._get_patches_for_fill.prop_cycler seg_map = dict() for lab in labels: if lab in seg_map: continue style = next(cycler) seg_map[lab] = seg_def_style.copy() seg_map[lab].update(style) # Swap color -> facecolor here so we preserve edgecolor on rects seg_map[lab]['facecolor'] = seg_map[lab].pop('color') seg_map[lab].update(kwargs) seg_map[lab]['label'] = lab for ival, lab in zip(intervals, labels): rect = Rectangle((ival[0], base), ival[1] - ival[0], height, **seg_map[lab]) ax.add_patch(rect) seg_map[lab].pop('label', None) if text: ann = ax.annotate(lab, xy=(ival[0], height), xycoords='data', xytext=(8, -10), textcoords='offset points', **text_kw) ann.set_clip_path(rect) if new_axes: ax.set_yticks([]) # Only expand if we have data if intervals.size: __expand_limits(ax, [intervals.min(), intervals.max()], which='x') return ax
python
def segments(intervals, labels, base=None, height=None, text=False, text_kw=None, ax=None, **kwargs): '''Plot a segmentation as a set of disjoint rectangles. Parameters ---------- intervals : np.ndarray, shape=(n, 2) segment intervals, in the format returned by :func:`mir_eval.io.load_intervals` or :func:`mir_eval.io.load_labeled_intervals`. labels : list, shape=(n,) reference segment labels, in the format returned by :func:`mir_eval.io.load_labeled_intervals`. base : number The vertical position of the base of the rectangles. By default, this will be the bottom of the plot. height : number The height of the rectangles. By default, this will be the top of the plot (minus ``base``). text : bool If true, each segment's label is displayed in its upper-left corner text_kw : dict If ``text == True``, the properties of the text object can be specified here. See ``matplotlib.pyplot.Text`` for valid parameters ax : matplotlib.pyplot.axes An axis handle on which to draw the segmentation. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to pass to ``matplotlib.patches.Rectangle``. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' if text_kw is None: text_kw = dict() text_kw.setdefault('va', 'top') text_kw.setdefault('clip_on', True) text_kw.setdefault('bbox', dict(boxstyle='round', facecolor='white')) # Make sure we have a numpy array intervals = np.atleast_2d(intervals) seg_def_style = dict(linewidth=1) ax, new_axes = __get_axes(ax=ax) if new_axes: ax.set_ylim([0, 1]) # Infer height if base is None: base = ax.get_ylim()[0] if height is None: height = ax.get_ylim()[1] cycler = ax._get_patches_for_fill.prop_cycler seg_map = dict() for lab in labels: if lab in seg_map: continue style = next(cycler) seg_map[lab] = seg_def_style.copy() seg_map[lab].update(style) # Swap color -> facecolor here so we preserve edgecolor on rects seg_map[lab]['facecolor'] = seg_map[lab].pop('color') seg_map[lab].update(kwargs) seg_map[lab]['label'] = lab for ival, lab in zip(intervals, labels): rect = Rectangle((ival[0], base), ival[1] - ival[0], height, **seg_map[lab]) ax.add_patch(rect) seg_map[lab].pop('label', None) if text: ann = ax.annotate(lab, xy=(ival[0], height), xycoords='data', xytext=(8, -10), textcoords='offset points', **text_kw) ann.set_clip_path(rect) if new_axes: ax.set_yticks([]) # Only expand if we have data if intervals.size: __expand_limits(ax, [intervals.min(), intervals.max()], which='x') return ax
[ "def", "segments", "(", "intervals", ",", "labels", ",", "base", "=", "None", ",", "height", "=", "None", ",", "text", "=", "False", ",", "text_kw", "=", "None", ",", "ax", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "text_kw", "is", "N...
Plot a segmentation as a set of disjoint rectangles. Parameters ---------- intervals : np.ndarray, shape=(n, 2) segment intervals, in the format returned by :func:`mir_eval.io.load_intervals` or :func:`mir_eval.io.load_labeled_intervals`. labels : list, shape=(n,) reference segment labels, in the format returned by :func:`mir_eval.io.load_labeled_intervals`. base : number The vertical position of the base of the rectangles. By default, this will be the bottom of the plot. height : number The height of the rectangles. By default, this will be the top of the plot (minus ``base``). text : bool If true, each segment's label is displayed in its upper-left corner text_kw : dict If ``text == True``, the properties of the text object can be specified here. See ``matplotlib.pyplot.Text`` for valid parameters ax : matplotlib.pyplot.axes An axis handle on which to draw the segmentation. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to pass to ``matplotlib.patches.Rectangle``. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes
[ "Plot", "a", "segmentation", "as", "a", "set", "of", "disjoint", "rectangles", "." ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L82-L186
21,506
craffel/mir_eval
mir_eval/display.py
labeled_intervals
def labeled_intervals(intervals, labels, label_set=None, base=None, height=None, extend_labels=True, ax=None, tick=True, **kwargs): '''Plot labeled intervals with each label on its own row. Parameters ---------- intervals : np.ndarray, shape=(n, 2) segment intervals, in the format returned by :func:`mir_eval.io.load_intervals` or :func:`mir_eval.io.load_labeled_intervals`. labels : list, shape=(n,) reference segment labels, in the format returned by :func:`mir_eval.io.load_labeled_intervals`. label_set : list An (ordered) list of labels to determine the plotting order. If not provided, the labels will be inferred from ``ax.get_yticklabels()``. If no ``yticklabels`` exist, then the sorted set of unique values in ``labels`` is taken as the label set. base : np.ndarray, shape=(n,), optional Vertical positions of each label. By default, labels are positioned at integers ``np.arange(len(labels))``. height : scalar or np.ndarray, shape=(n,), optional Height for each label. If scalar, the same value is applied to all labels. By default, each label has ``height=1``. extend_labels : bool If ``False``, only values of ``labels`` that also exist in ``label_set`` will be shown. If ``True``, all labels are shown, with those in `labels` but not in `label_set` appended to the top of the plot. A horizontal line is drawn to indicate the separation between values in or out of ``label_set``. ax : matplotlib.pyplot.axes An axis handle on which to draw the intervals. If none is provided, a new set of axes is created. tick : bool If ``True``, sets tick positions and labels on the y-axis. kwargs Additional keyword arguments to pass to `matplotlib.collection.BrokenBarHCollection`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' # Get the axes handle ax, _ = __get_axes(ax=ax) # Make sure we have a numpy array intervals = np.atleast_2d(intervals) if label_set is None: # If we have non-empty pre-existing tick labels, use them label_set = [_.get_text() for _ in ax.get_yticklabels()] # If none of the label strings have content, treat it as empty if not any(label_set): label_set = [] else: label_set = list(label_set) # Put additional labels at the end, in order if extend_labels: ticks = label_set + sorted(set(labels) - set(label_set)) elif label_set: ticks = label_set else: ticks = sorted(set(labels)) style = dict(linewidth=1) style.update(next(ax._get_patches_for_fill.prop_cycler)) # Swap color -> facecolor here so we preserve edgecolor on rects style['facecolor'] = style.pop('color') style.update(kwargs) if base is None: base = np.arange(len(ticks)) if height is None: height = 1 if np.isscalar(height): height = height * np.ones_like(base) seg_y = dict() for ybase, yheight, lab in zip(base, height, ticks): seg_y[lab] = (ybase, yheight) xvals = defaultdict(list) for ival, lab in zip(intervals, labels): if lab not in seg_y: continue xvals[lab].append((ival[0], ival[1] - ival[0])) for lab in seg_y: ax.add_collection(BrokenBarHCollection(xvals[lab], seg_y[lab], **style)) # Pop the label after the first time we see it, so we only get # one legend entry style.pop('label', None) # Draw a line separating the new labels from pre-existing labels if label_set != ticks: ax.axhline(len(label_set), color='k', alpha=0.5) if tick: ax.grid(True, axis='y') ax.set_yticks([]) ax.set_yticks(base) ax.set_yticklabels(ticks, va='bottom') ax.yaxis.set_major_formatter(IntervalFormatter(base, ticks)) if base.size: __expand_limits(ax, [base.min(), (base + height).max()], which='y') if intervals.size: __expand_limits(ax, [intervals.min(), intervals.max()], which='x') return ax
python
def labeled_intervals(intervals, labels, label_set=None, base=None, height=None, extend_labels=True, ax=None, tick=True, **kwargs): '''Plot labeled intervals with each label on its own row. Parameters ---------- intervals : np.ndarray, shape=(n, 2) segment intervals, in the format returned by :func:`mir_eval.io.load_intervals` or :func:`mir_eval.io.load_labeled_intervals`. labels : list, shape=(n,) reference segment labels, in the format returned by :func:`mir_eval.io.load_labeled_intervals`. label_set : list An (ordered) list of labels to determine the plotting order. If not provided, the labels will be inferred from ``ax.get_yticklabels()``. If no ``yticklabels`` exist, then the sorted set of unique values in ``labels`` is taken as the label set. base : np.ndarray, shape=(n,), optional Vertical positions of each label. By default, labels are positioned at integers ``np.arange(len(labels))``. height : scalar or np.ndarray, shape=(n,), optional Height for each label. If scalar, the same value is applied to all labels. By default, each label has ``height=1``. extend_labels : bool If ``False``, only values of ``labels`` that also exist in ``label_set`` will be shown. If ``True``, all labels are shown, with those in `labels` but not in `label_set` appended to the top of the plot. A horizontal line is drawn to indicate the separation between values in or out of ``label_set``. ax : matplotlib.pyplot.axes An axis handle on which to draw the intervals. If none is provided, a new set of axes is created. tick : bool If ``True``, sets tick positions and labels on the y-axis. kwargs Additional keyword arguments to pass to `matplotlib.collection.BrokenBarHCollection`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' # Get the axes handle ax, _ = __get_axes(ax=ax) # Make sure we have a numpy array intervals = np.atleast_2d(intervals) if label_set is None: # If we have non-empty pre-existing tick labels, use them label_set = [_.get_text() for _ in ax.get_yticklabels()] # If none of the label strings have content, treat it as empty if not any(label_set): label_set = [] else: label_set = list(label_set) # Put additional labels at the end, in order if extend_labels: ticks = label_set + sorted(set(labels) - set(label_set)) elif label_set: ticks = label_set else: ticks = sorted(set(labels)) style = dict(linewidth=1) style.update(next(ax._get_patches_for_fill.prop_cycler)) # Swap color -> facecolor here so we preserve edgecolor on rects style['facecolor'] = style.pop('color') style.update(kwargs) if base is None: base = np.arange(len(ticks)) if height is None: height = 1 if np.isscalar(height): height = height * np.ones_like(base) seg_y = dict() for ybase, yheight, lab in zip(base, height, ticks): seg_y[lab] = (ybase, yheight) xvals = defaultdict(list) for ival, lab in zip(intervals, labels): if lab not in seg_y: continue xvals[lab].append((ival[0], ival[1] - ival[0])) for lab in seg_y: ax.add_collection(BrokenBarHCollection(xvals[lab], seg_y[lab], **style)) # Pop the label after the first time we see it, so we only get # one legend entry style.pop('label', None) # Draw a line separating the new labels from pre-existing labels if label_set != ticks: ax.axhline(len(label_set), color='k', alpha=0.5) if tick: ax.grid(True, axis='y') ax.set_yticks([]) ax.set_yticks(base) ax.set_yticklabels(ticks, va='bottom') ax.yaxis.set_major_formatter(IntervalFormatter(base, ticks)) if base.size: __expand_limits(ax, [base.min(), (base + height).max()], which='y') if intervals.size: __expand_limits(ax, [intervals.min(), intervals.max()], which='x') return ax
[ "def", "labeled_intervals", "(", "intervals", ",", "labels", ",", "label_set", "=", "None", ",", "base", "=", "None", ",", "height", "=", "None", ",", "extend_labels", "=", "True", ",", "ax", "=", "None", ",", "tick", "=", "True", ",", "*", "*", "kwa...
Plot labeled intervals with each label on its own row. Parameters ---------- intervals : np.ndarray, shape=(n, 2) segment intervals, in the format returned by :func:`mir_eval.io.load_intervals` or :func:`mir_eval.io.load_labeled_intervals`. labels : list, shape=(n,) reference segment labels, in the format returned by :func:`mir_eval.io.load_labeled_intervals`. label_set : list An (ordered) list of labels to determine the plotting order. If not provided, the labels will be inferred from ``ax.get_yticklabels()``. If no ``yticklabels`` exist, then the sorted set of unique values in ``labels`` is taken as the label set. base : np.ndarray, shape=(n,), optional Vertical positions of each label. By default, labels are positioned at integers ``np.arange(len(labels))``. height : scalar or np.ndarray, shape=(n,), optional Height for each label. If scalar, the same value is applied to all labels. By default, each label has ``height=1``. extend_labels : bool If ``False``, only values of ``labels`` that also exist in ``label_set`` will be shown. If ``True``, all labels are shown, with those in `labels` but not in `label_set` appended to the top of the plot. A horizontal line is drawn to indicate the separation between values in or out of ``label_set``. ax : matplotlib.pyplot.axes An axis handle on which to draw the intervals. If none is provided, a new set of axes is created. tick : bool If ``True``, sets tick positions and labels on the y-axis. kwargs Additional keyword arguments to pass to `matplotlib.collection.BrokenBarHCollection`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes
[ "Plot", "labeled", "intervals", "with", "each", "label", "on", "its", "own", "row", "." ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L189-L320
21,507
craffel/mir_eval
mir_eval/display.py
hierarchy
def hierarchy(intervals_hier, labels_hier, levels=None, ax=None, **kwargs): '''Plot a hierarchical segmentation Parameters ---------- intervals_hier : list of np.ndarray A list of segmentation intervals. Each element should be an n-by-2 array of segment intervals, in the format returned by :func:`mir_eval.io.load_intervals` or :func:`mir_eval.io.load_labeled_intervals`. Segmentations should be ordered by increasing specificity. labels_hier : list of list-like A list of segmentation labels. Each element should be a list of labels for the corresponding element in `intervals_hier`. levels : list of string Each element ``levels[i]`` is a label for the ```i`` th segmentation. This is used in the legend to denote the levels in a segment hierarchy. kwargs Additional keyword arguments to `labeled_intervals`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' # This will break if a segment label exists in multiple levels if levels is None: levels = list(range(len(intervals_hier))) # Get the axes handle ax, _ = __get_axes(ax=ax) # Count the pre-existing patches n_patches = len(ax.patches) for ints, labs, key in zip(intervals_hier[::-1], labels_hier[::-1], levels[::-1]): labeled_intervals(ints, labs, label=key, ax=ax, **kwargs) # Reverse the patch ordering for anything we've added. # This way, intervals are listed in the legend from top to bottom ax.patches[n_patches:] = ax.patches[n_patches:][::-1] return ax
python
def hierarchy(intervals_hier, labels_hier, levels=None, ax=None, **kwargs): '''Plot a hierarchical segmentation Parameters ---------- intervals_hier : list of np.ndarray A list of segmentation intervals. Each element should be an n-by-2 array of segment intervals, in the format returned by :func:`mir_eval.io.load_intervals` or :func:`mir_eval.io.load_labeled_intervals`. Segmentations should be ordered by increasing specificity. labels_hier : list of list-like A list of segmentation labels. Each element should be a list of labels for the corresponding element in `intervals_hier`. levels : list of string Each element ``levels[i]`` is a label for the ```i`` th segmentation. This is used in the legend to denote the levels in a segment hierarchy. kwargs Additional keyword arguments to `labeled_intervals`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' # This will break if a segment label exists in multiple levels if levels is None: levels = list(range(len(intervals_hier))) # Get the axes handle ax, _ = __get_axes(ax=ax) # Count the pre-existing patches n_patches = len(ax.patches) for ints, labs, key in zip(intervals_hier[::-1], labels_hier[::-1], levels[::-1]): labeled_intervals(ints, labs, label=key, ax=ax, **kwargs) # Reverse the patch ordering for anything we've added. # This way, intervals are listed in the legend from top to bottom ax.patches[n_patches:] = ax.patches[n_patches:][::-1] return ax
[ "def", "hierarchy", "(", "intervals_hier", ",", "labels_hier", ",", "levels", "=", "None", ",", "ax", "=", "None", ",", "*", "*", "kwargs", ")", ":", "# This will break if a segment label exists in multiple levels", "if", "levels", "is", "None", ":", "levels", "...
Plot a hierarchical segmentation Parameters ---------- intervals_hier : list of np.ndarray A list of segmentation intervals. Each element should be an n-by-2 array of segment intervals, in the format returned by :func:`mir_eval.io.load_intervals` or :func:`mir_eval.io.load_labeled_intervals`. Segmentations should be ordered by increasing specificity. labels_hier : list of list-like A list of segmentation labels. Each element should be a list of labels for the corresponding element in `intervals_hier`. levels : list of string Each element ``levels[i]`` is a label for the ```i`` th segmentation. This is used in the legend to denote the levels in a segment hierarchy. kwargs Additional keyword arguments to `labeled_intervals`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes
[ "Plot", "a", "hierarchical", "segmentation" ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L343-L391
21,508
craffel/mir_eval
mir_eval/display.py
events
def events(times, labels=None, base=None, height=None, ax=None, text_kw=None, **kwargs): '''Plot event times as a set of vertical lines Parameters ---------- times : np.ndarray, shape=(n,) event times, in the format returned by :func:`mir_eval.io.load_events` or :func:`mir_eval.io.load_labeled_events`. labels : list, shape=(n,), optional event labels, in the format returned by :func:`mir_eval.io.load_labeled_events`. base : number The vertical position of the base of the line. By default, this will be the bottom of the plot. height : number The height of the lines. By default, this will be the top of the plot (minus `base`). ax : matplotlib.pyplot.axes An axis handle on which to draw the segmentation. If none is provided, a new set of axes is created. text_kw : dict If `labels` is provided, the properties of the text objects can be specified here. See `matplotlib.pyplot.Text` for valid parameters kwargs Additional keyword arguments to pass to `matplotlib.pyplot.vlines`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' if text_kw is None: text_kw = dict() text_kw.setdefault('va', 'top') text_kw.setdefault('clip_on', True) text_kw.setdefault('bbox', dict(boxstyle='round', facecolor='white')) # make sure we have an array for times times = np.asarray(times) # Get the axes handle ax, new_axes = __get_axes(ax=ax) # If we have fresh axes, set the limits if new_axes: # Infer base and height if base is None: base = 0 if height is None: height = 1 ax.set_ylim([base, height]) else: if base is None: base = ax.get_ylim()[0] if height is None: height = ax.get_ylim()[1] cycler = ax._get_patches_for_fill.prop_cycler style = next(cycler).copy() style.update(kwargs) # If the user provided 'colors', don't override it with 'color' if 'colors' in style: style.pop('color', None) lines = ax.vlines(times, base, base + height, **style) if labels: for path, lab in zip(lines.get_paths(), labels): ax.annotate(lab, xy=(path.vertices[0][0], height), xycoords='data', xytext=(8, -10), textcoords='offset points', **text_kw) if new_axes: ax.set_yticks([]) __expand_limits(ax, [base, base + height], which='y') if times.size: __expand_limits(ax, [times.min(), times.max()], which='x') return ax
python
def events(times, labels=None, base=None, height=None, ax=None, text_kw=None, **kwargs): '''Plot event times as a set of vertical lines Parameters ---------- times : np.ndarray, shape=(n,) event times, in the format returned by :func:`mir_eval.io.load_events` or :func:`mir_eval.io.load_labeled_events`. labels : list, shape=(n,), optional event labels, in the format returned by :func:`mir_eval.io.load_labeled_events`. base : number The vertical position of the base of the line. By default, this will be the bottom of the plot. height : number The height of the lines. By default, this will be the top of the plot (minus `base`). ax : matplotlib.pyplot.axes An axis handle on which to draw the segmentation. If none is provided, a new set of axes is created. text_kw : dict If `labels` is provided, the properties of the text objects can be specified here. See `matplotlib.pyplot.Text` for valid parameters kwargs Additional keyword arguments to pass to `matplotlib.pyplot.vlines`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' if text_kw is None: text_kw = dict() text_kw.setdefault('va', 'top') text_kw.setdefault('clip_on', True) text_kw.setdefault('bbox', dict(boxstyle='round', facecolor='white')) # make sure we have an array for times times = np.asarray(times) # Get the axes handle ax, new_axes = __get_axes(ax=ax) # If we have fresh axes, set the limits if new_axes: # Infer base and height if base is None: base = 0 if height is None: height = 1 ax.set_ylim([base, height]) else: if base is None: base = ax.get_ylim()[0] if height is None: height = ax.get_ylim()[1] cycler = ax._get_patches_for_fill.prop_cycler style = next(cycler).copy() style.update(kwargs) # If the user provided 'colors', don't override it with 'color' if 'colors' in style: style.pop('color', None) lines = ax.vlines(times, base, base + height, **style) if labels: for path, lab in zip(lines.get_paths(), labels): ax.annotate(lab, xy=(path.vertices[0][0], height), xycoords='data', xytext=(8, -10), textcoords='offset points', **text_kw) if new_axes: ax.set_yticks([]) __expand_limits(ax, [base, base + height], which='y') if times.size: __expand_limits(ax, [times.min(), times.max()], which='x') return ax
[ "def", "events", "(", "times", ",", "labels", "=", "None", ",", "base", "=", "None", ",", "height", "=", "None", ",", "ax", "=", "None", ",", "text_kw", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "text_kw", "is", "None", ":", "text_kw"...
Plot event times as a set of vertical lines Parameters ---------- times : np.ndarray, shape=(n,) event times, in the format returned by :func:`mir_eval.io.load_events` or :func:`mir_eval.io.load_labeled_events`. labels : list, shape=(n,), optional event labels, in the format returned by :func:`mir_eval.io.load_labeled_events`. base : number The vertical position of the base of the line. By default, this will be the bottom of the plot. height : number The height of the lines. By default, this will be the top of the plot (minus `base`). ax : matplotlib.pyplot.axes An axis handle on which to draw the segmentation. If none is provided, a new set of axes is created. text_kw : dict If `labels` is provided, the properties of the text objects can be specified here. See `matplotlib.pyplot.Text` for valid parameters kwargs Additional keyword arguments to pass to `matplotlib.pyplot.vlines`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes
[ "Plot", "event", "times", "as", "a", "set", "of", "vertical", "lines" ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L394-L490
21,509
craffel/mir_eval
mir_eval/display.py
pitch
def pitch(times, frequencies, midi=False, unvoiced=False, ax=None, **kwargs): '''Visualize pitch contours Parameters ---------- times : np.ndarray, shape=(n,) Sample times of frequencies frequencies : np.ndarray, shape=(n,) frequencies (in Hz) of the pitch contours. Voicing is indicated by sign (positive for voiced, non-positive for non-voiced). midi : bool If `True`, plot on a MIDI-numbered vertical axis. Otherwise, plot on a linear frequency axis. unvoiced : bool If `True`, unvoiced pitch contours are plotted and indicated by transparency. Otherwise, unvoiced pitch contours are omitted from the display. ax : matplotlib.pyplot.axes An axis handle on which to draw the pitch contours. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to `matplotlib.pyplot.plot`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' ax, _ = __get_axes(ax=ax) times = np.asarray(times) # First, segment into contiguously voiced contours frequencies, voicings = freq_to_voicing(np.asarray(frequencies, dtype=np.float)) # Here are all the change-points v_changes = 1 + np.flatnonzero(voicings[1:] != voicings[:-1]) v_changes = np.unique(np.concatenate([[0], v_changes, [len(voicings)]])) # Set up arrays of slices for voiced and unvoiced regions v_slices, u_slices = [], [] for start, end in zip(v_changes, v_changes[1:]): idx = slice(start, end) # A region is voiced if its starting sample is voiced # It's unvoiced if none of the samples in the region are voiced. if voicings[start]: v_slices.append(idx) elif frequencies[idx].all(): u_slices.append(idx) # Now we just need to plot the contour style = dict() style.update(next(ax._get_lines.prop_cycler)) style.update(kwargs) if midi: idx = frequencies > 0 frequencies[idx] = hz_to_midi(frequencies[idx]) # Tick at integer midi notes ax.yaxis.set_minor_locator(MultipleLocator(1)) for idx in v_slices: ax.plot(times[idx], frequencies[idx], **style) style.pop('label', None) # Plot the unvoiced portions if unvoiced: style['alpha'] = style.get('alpha', 1.0) * 0.5 for idx in u_slices: ax.plot(times[idx], frequencies[idx], **style) return ax
python
def pitch(times, frequencies, midi=False, unvoiced=False, ax=None, **kwargs): '''Visualize pitch contours Parameters ---------- times : np.ndarray, shape=(n,) Sample times of frequencies frequencies : np.ndarray, shape=(n,) frequencies (in Hz) of the pitch contours. Voicing is indicated by sign (positive for voiced, non-positive for non-voiced). midi : bool If `True`, plot on a MIDI-numbered vertical axis. Otherwise, plot on a linear frequency axis. unvoiced : bool If `True`, unvoiced pitch contours are plotted and indicated by transparency. Otherwise, unvoiced pitch contours are omitted from the display. ax : matplotlib.pyplot.axes An axis handle on which to draw the pitch contours. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to `matplotlib.pyplot.plot`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' ax, _ = __get_axes(ax=ax) times = np.asarray(times) # First, segment into contiguously voiced contours frequencies, voicings = freq_to_voicing(np.asarray(frequencies, dtype=np.float)) # Here are all the change-points v_changes = 1 + np.flatnonzero(voicings[1:] != voicings[:-1]) v_changes = np.unique(np.concatenate([[0], v_changes, [len(voicings)]])) # Set up arrays of slices for voiced and unvoiced regions v_slices, u_slices = [], [] for start, end in zip(v_changes, v_changes[1:]): idx = slice(start, end) # A region is voiced if its starting sample is voiced # It's unvoiced if none of the samples in the region are voiced. if voicings[start]: v_slices.append(idx) elif frequencies[idx].all(): u_slices.append(idx) # Now we just need to plot the contour style = dict() style.update(next(ax._get_lines.prop_cycler)) style.update(kwargs) if midi: idx = frequencies > 0 frequencies[idx] = hz_to_midi(frequencies[idx]) # Tick at integer midi notes ax.yaxis.set_minor_locator(MultipleLocator(1)) for idx in v_slices: ax.plot(times[idx], frequencies[idx], **style) style.pop('label', None) # Plot the unvoiced portions if unvoiced: style['alpha'] = style.get('alpha', 1.0) * 0.5 for idx in u_slices: ax.plot(times[idx], frequencies[idx], **style) return ax
[ "def", "pitch", "(", "times", ",", "frequencies", ",", "midi", "=", "False", ",", "unvoiced", "=", "False", ",", "ax", "=", "None", ",", "*", "*", "kwargs", ")", ":", "ax", ",", "_", "=", "__get_axes", "(", "ax", "=", "ax", ")", "times", "=", "...
Visualize pitch contours Parameters ---------- times : np.ndarray, shape=(n,) Sample times of frequencies frequencies : np.ndarray, shape=(n,) frequencies (in Hz) of the pitch contours. Voicing is indicated by sign (positive for voiced, non-positive for non-voiced). midi : bool If `True`, plot on a MIDI-numbered vertical axis. Otherwise, plot on a linear frequency axis. unvoiced : bool If `True`, unvoiced pitch contours are plotted and indicated by transparency. Otherwise, unvoiced pitch contours are omitted from the display. ax : matplotlib.pyplot.axes An axis handle on which to draw the pitch contours. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to `matplotlib.pyplot.plot`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes
[ "Visualize", "pitch", "contours" ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L493-L574
21,510
craffel/mir_eval
mir_eval/display.py
multipitch
def multipitch(times, frequencies, midi=False, unvoiced=False, ax=None, **kwargs): '''Visualize multiple f0 measurements Parameters ---------- times : np.ndarray, shape=(n,) Sample times of frequencies frequencies : list of np.ndarray frequencies (in Hz) of the pitch measurements. Voicing is indicated by sign (positive for voiced, non-positive for non-voiced). `times` and `frequencies` should be in the format produced by :func:`mir_eval.io.load_ragged_time_series` midi : bool If `True`, plot on a MIDI-numbered vertical axis. Otherwise, plot on a linear frequency axis. unvoiced : bool If `True`, unvoiced pitches are plotted and indicated by transparency. Otherwise, unvoiced pitches are omitted from the display. ax : matplotlib.pyplot.axes An axis handle on which to draw the pitch contours. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to `plt.scatter`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' # Get the axes handle ax, _ = __get_axes(ax=ax) # Set up a style for the plot style_voiced = dict() style_voiced.update(next(ax._get_lines.prop_cycler)) style_voiced.update(kwargs) style_unvoiced = style_voiced.copy() style_unvoiced.pop('label', None) style_unvoiced['alpha'] = style_unvoiced.get('alpha', 1.0) * 0.5 # We'll collect all times and frequencies first, then plot them voiced_times = [] voiced_freqs = [] unvoiced_times = [] unvoiced_freqs = [] for t, freqs in zip(times, frequencies): if not len(freqs): continue freqs, voicings = freq_to_voicing(np.asarray(freqs, dtype=np.float)) # Discard all 0-frequency measurements idx = freqs > 0 freqs = freqs[idx] voicings = voicings[idx] if midi: freqs = hz_to_midi(freqs) n_voiced = sum(voicings) voiced_times.extend([t] * n_voiced) voiced_freqs.extend(freqs[voicings]) unvoiced_times.extend([t] * (len(freqs) - n_voiced)) unvoiced_freqs.extend(freqs[~voicings]) # Plot the voiced frequencies ax.scatter(voiced_times, voiced_freqs, **style_voiced) # Plot the unvoiced frequencies if unvoiced: ax.scatter(unvoiced_times, unvoiced_freqs, **style_unvoiced) # Tick at integer midi notes if midi: ax.yaxis.set_minor_locator(MultipleLocator(1)) return ax
python
def multipitch(times, frequencies, midi=False, unvoiced=False, ax=None, **kwargs): '''Visualize multiple f0 measurements Parameters ---------- times : np.ndarray, shape=(n,) Sample times of frequencies frequencies : list of np.ndarray frequencies (in Hz) of the pitch measurements. Voicing is indicated by sign (positive for voiced, non-positive for non-voiced). `times` and `frequencies` should be in the format produced by :func:`mir_eval.io.load_ragged_time_series` midi : bool If `True`, plot on a MIDI-numbered vertical axis. Otherwise, plot on a linear frequency axis. unvoiced : bool If `True`, unvoiced pitches are plotted and indicated by transparency. Otherwise, unvoiced pitches are omitted from the display. ax : matplotlib.pyplot.axes An axis handle on which to draw the pitch contours. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to `plt.scatter`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' # Get the axes handle ax, _ = __get_axes(ax=ax) # Set up a style for the plot style_voiced = dict() style_voiced.update(next(ax._get_lines.prop_cycler)) style_voiced.update(kwargs) style_unvoiced = style_voiced.copy() style_unvoiced.pop('label', None) style_unvoiced['alpha'] = style_unvoiced.get('alpha', 1.0) * 0.5 # We'll collect all times and frequencies first, then plot them voiced_times = [] voiced_freqs = [] unvoiced_times = [] unvoiced_freqs = [] for t, freqs in zip(times, frequencies): if not len(freqs): continue freqs, voicings = freq_to_voicing(np.asarray(freqs, dtype=np.float)) # Discard all 0-frequency measurements idx = freqs > 0 freqs = freqs[idx] voicings = voicings[idx] if midi: freqs = hz_to_midi(freqs) n_voiced = sum(voicings) voiced_times.extend([t] * n_voiced) voiced_freqs.extend(freqs[voicings]) unvoiced_times.extend([t] * (len(freqs) - n_voiced)) unvoiced_freqs.extend(freqs[~voicings]) # Plot the voiced frequencies ax.scatter(voiced_times, voiced_freqs, **style_voiced) # Plot the unvoiced frequencies if unvoiced: ax.scatter(unvoiced_times, unvoiced_freqs, **style_unvoiced) # Tick at integer midi notes if midi: ax.yaxis.set_minor_locator(MultipleLocator(1)) return ax
[ "def", "multipitch", "(", "times", ",", "frequencies", ",", "midi", "=", "False", ",", "unvoiced", "=", "False", ",", "ax", "=", "None", ",", "*", "*", "kwargs", ")", ":", "# Get the axes handle", "ax", ",", "_", "=", "__get_axes", "(", "ax", "=", "a...
Visualize multiple f0 measurements Parameters ---------- times : np.ndarray, shape=(n,) Sample times of frequencies frequencies : list of np.ndarray frequencies (in Hz) of the pitch measurements. Voicing is indicated by sign (positive for voiced, non-positive for non-voiced). `times` and `frequencies` should be in the format produced by :func:`mir_eval.io.load_ragged_time_series` midi : bool If `True`, plot on a MIDI-numbered vertical axis. Otherwise, plot on a linear frequency axis. unvoiced : bool If `True`, unvoiced pitches are plotted and indicated by transparency. Otherwise, unvoiced pitches are omitted from the display. ax : matplotlib.pyplot.axes An axis handle on which to draw the pitch contours. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to `plt.scatter`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes
[ "Visualize", "multiple", "f0", "measurements" ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L577-L666
21,511
craffel/mir_eval
mir_eval/display.py
piano_roll
def piano_roll(intervals, pitches=None, midi=None, ax=None, **kwargs): '''Plot a quantized piano roll as intervals Parameters ---------- intervals : np.ndarray, shape=(n, 2) timing intervals for notes pitches : np.ndarray, shape=(n,), optional pitches of notes (in Hz). midi : np.ndarray, shape=(n,), optional pitches of notes (in MIDI numbers). At least one of ``pitches`` or ``midi`` must be provided. ax : matplotlib.pyplot.axes An axis handle on which to draw the intervals. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to :func:`labeled_intervals`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' if midi is None: if pitches is None: raise ValueError('At least one of `midi` or `pitches` ' 'must be provided.') midi = hz_to_midi(pitches) scale = np.arange(128) ax = labeled_intervals(intervals, np.round(midi).astype(int), label_set=scale, tick=False, ax=ax, **kwargs) # Minor tick at each semitone ax.yaxis.set_minor_locator(MultipleLocator(1)) ax.axis('auto') return ax
python
def piano_roll(intervals, pitches=None, midi=None, ax=None, **kwargs): '''Plot a quantized piano roll as intervals Parameters ---------- intervals : np.ndarray, shape=(n, 2) timing intervals for notes pitches : np.ndarray, shape=(n,), optional pitches of notes (in Hz). midi : np.ndarray, shape=(n,), optional pitches of notes (in MIDI numbers). At least one of ``pitches`` or ``midi`` must be provided. ax : matplotlib.pyplot.axes An axis handle on which to draw the intervals. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to :func:`labeled_intervals`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes ''' if midi is None: if pitches is None: raise ValueError('At least one of `midi` or `pitches` ' 'must be provided.') midi = hz_to_midi(pitches) scale = np.arange(128) ax = labeled_intervals(intervals, np.round(midi).astype(int), label_set=scale, tick=False, ax=ax, **kwargs) # Minor tick at each semitone ax.yaxis.set_minor_locator(MultipleLocator(1)) ax.axis('auto') return ax
[ "def", "piano_roll", "(", "intervals", ",", "pitches", "=", "None", ",", "midi", "=", "None", ",", "ax", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "midi", "is", "None", ":", "if", "pitches", "is", "None", ":", "raise", "ValueError", "("...
Plot a quantized piano roll as intervals Parameters ---------- intervals : np.ndarray, shape=(n, 2) timing intervals for notes pitches : np.ndarray, shape=(n,), optional pitches of notes (in Hz). midi : np.ndarray, shape=(n,), optional pitches of notes (in MIDI numbers). At least one of ``pitches`` or ``midi`` must be provided. ax : matplotlib.pyplot.axes An axis handle on which to draw the intervals. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to :func:`labeled_intervals`. Returns ------- ax : matplotlib.pyplot.axes._subplots.AxesSubplot A handle to the (possibly constructed) plot axes
[ "Plot", "a", "quantized", "piano", "roll", "as", "intervals" ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L669-L716
21,512
craffel/mir_eval
mir_eval/display.py
separation
def separation(sources, fs=22050, labels=None, alpha=0.75, ax=None, **kwargs): '''Source-separation visualization Parameters ---------- sources : np.ndarray, shape=(nsrc, nsampl) A list of waveform buffers corresponding to each source fs : number > 0 The sampling rate labels : list of strings An optional list of descriptors corresponding to each source alpha : float in [0, 1] Maximum alpha (opacity) of spectrogram values. ax : matplotlib.pyplot.axes An axis handle on which to draw the spectrograms. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to ``scipy.signal.spectrogram`` Returns ------- ax The axis handle for this plot ''' # Get the axes handle ax, new_axes = __get_axes(ax=ax) # Make sure we have at least two dimensions sources = np.atleast_2d(sources) if labels is None: labels = ['Source {:d}'.format(_) for _ in range(len(sources))] kwargs.setdefault('scaling', 'spectrum') # The cumulative spectrogram across sources # is used to establish the reference power # for each individual source cumspec = None specs = [] for i, src in enumerate(sources): freqs, times, spec = spectrogram(src, fs=fs, **kwargs) specs.append(spec) if cumspec is None: cumspec = spec.copy() else: cumspec += spec ref_max = cumspec.max() ref_min = ref_max * 1e-6 color_conv = ColorConverter() for i, spec in enumerate(specs): # For each source, grab a new color from the cycler # Then construct a colormap that interpolates from # [transparent white -> new color] color = next(ax._get_lines.prop_cycler)['color'] color = color_conv.to_rgba(color, alpha=alpha) cmap = LinearSegmentedColormap.from_list(labels[i], [(1.0, 1.0, 1.0, 0.0), color]) ax.pcolormesh(times, freqs, spec, cmap=cmap, norm=LogNorm(vmin=ref_min, vmax=ref_max), shading='gouraud', label=labels[i]) # Attach a 0x0 rect to the axis with the corresponding label # This way, it will show up in the legend ax.add_patch(Rectangle((0, 0), 0, 0, color=color, label=labels[i])) if new_axes: ax.axis('tight') return ax
python
def separation(sources, fs=22050, labels=None, alpha=0.75, ax=None, **kwargs): '''Source-separation visualization Parameters ---------- sources : np.ndarray, shape=(nsrc, nsampl) A list of waveform buffers corresponding to each source fs : number > 0 The sampling rate labels : list of strings An optional list of descriptors corresponding to each source alpha : float in [0, 1] Maximum alpha (opacity) of spectrogram values. ax : matplotlib.pyplot.axes An axis handle on which to draw the spectrograms. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to ``scipy.signal.spectrogram`` Returns ------- ax The axis handle for this plot ''' # Get the axes handle ax, new_axes = __get_axes(ax=ax) # Make sure we have at least two dimensions sources = np.atleast_2d(sources) if labels is None: labels = ['Source {:d}'.format(_) for _ in range(len(sources))] kwargs.setdefault('scaling', 'spectrum') # The cumulative spectrogram across sources # is used to establish the reference power # for each individual source cumspec = None specs = [] for i, src in enumerate(sources): freqs, times, spec = spectrogram(src, fs=fs, **kwargs) specs.append(spec) if cumspec is None: cumspec = spec.copy() else: cumspec += spec ref_max = cumspec.max() ref_min = ref_max * 1e-6 color_conv = ColorConverter() for i, spec in enumerate(specs): # For each source, grab a new color from the cycler # Then construct a colormap that interpolates from # [transparent white -> new color] color = next(ax._get_lines.prop_cycler)['color'] color = color_conv.to_rgba(color, alpha=alpha) cmap = LinearSegmentedColormap.from_list(labels[i], [(1.0, 1.0, 1.0, 0.0), color]) ax.pcolormesh(times, freqs, spec, cmap=cmap, norm=LogNorm(vmin=ref_min, vmax=ref_max), shading='gouraud', label=labels[i]) # Attach a 0x0 rect to the axis with the corresponding label # This way, it will show up in the legend ax.add_patch(Rectangle((0, 0), 0, 0, color=color, label=labels[i])) if new_axes: ax.axis('tight') return ax
[ "def", "separation", "(", "sources", ",", "fs", "=", "22050", ",", "labels", "=", "None", ",", "alpha", "=", "0.75", ",", "ax", "=", "None", ",", "*", "*", "kwargs", ")", ":", "# Get the axes handle", "ax", ",", "new_axes", "=", "__get_axes", "(", "a...
Source-separation visualization Parameters ---------- sources : np.ndarray, shape=(nsrc, nsampl) A list of waveform buffers corresponding to each source fs : number > 0 The sampling rate labels : list of strings An optional list of descriptors corresponding to each source alpha : float in [0, 1] Maximum alpha (opacity) of spectrogram values. ax : matplotlib.pyplot.axes An axis handle on which to draw the spectrograms. If none is provided, a new set of axes is created. kwargs Additional keyword arguments to ``scipy.signal.spectrogram`` Returns ------- ax The axis handle for this plot
[ "Source", "-", "separation", "visualization" ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L719-L803
21,513
craffel/mir_eval
mir_eval/display.py
__ticker_midi_note
def __ticker_midi_note(x, pos): '''A ticker function for midi notes. Inputs x are interpreted as midi numbers, and converted to [NOTE][OCTAVE]+[cents]. ''' NOTES = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] cents = float(np.mod(x, 1.0)) if cents >= 0.5: cents = cents - 1.0 x = x + 0.5 idx = int(x % 12) octave = int(x / 12) - 1 if cents == 0: return '{:s}{:2d}'.format(NOTES[idx], octave) return '{:s}{:2d}{:+02d}'.format(NOTES[idx], octave, int(cents * 100))
python
def __ticker_midi_note(x, pos): '''A ticker function for midi notes. Inputs x are interpreted as midi numbers, and converted to [NOTE][OCTAVE]+[cents]. ''' NOTES = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] cents = float(np.mod(x, 1.0)) if cents >= 0.5: cents = cents - 1.0 x = x + 0.5 idx = int(x % 12) octave = int(x / 12) - 1 if cents == 0: return '{:s}{:2d}'.format(NOTES[idx], octave) return '{:s}{:2d}{:+02d}'.format(NOTES[idx], octave, int(cents * 100))
[ "def", "__ticker_midi_note", "(", "x", ",", "pos", ")", ":", "NOTES", "=", "[", "'C'", ",", "'C#'", ",", "'D'", ",", "'D#'", ",", "'E'", ",", "'F'", ",", "'F#'", ",", "'G'", ",", "'G#'", ",", "'A'", ",", "'A#'", ",", "'B'", "]", "cents", "=", ...
A ticker function for midi notes. Inputs x are interpreted as midi numbers, and converted to [NOTE][OCTAVE]+[cents].
[ "A", "ticker", "function", "for", "midi", "notes", "." ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L806-L826
21,514
craffel/mir_eval
mir_eval/display.py
ticker_notes
def ticker_notes(ax=None): '''Set the y-axis of the given axes to MIDI notes Parameters ---------- ax : matplotlib.pyplot.axes The axes handle to apply the ticker. By default, uses the current axes handle. ''' ax, _ = __get_axes(ax=ax) ax.yaxis.set_major_formatter(FMT_MIDI_NOTE) # Get the tick labels and reset the vertical alignment for tick in ax.yaxis.get_ticklabels(): tick.set_verticalalignment('baseline')
python
def ticker_notes(ax=None): '''Set the y-axis of the given axes to MIDI notes Parameters ---------- ax : matplotlib.pyplot.axes The axes handle to apply the ticker. By default, uses the current axes handle. ''' ax, _ = __get_axes(ax=ax) ax.yaxis.set_major_formatter(FMT_MIDI_NOTE) # Get the tick labels and reset the vertical alignment for tick in ax.yaxis.get_ticklabels(): tick.set_verticalalignment('baseline')
[ "def", "ticker_notes", "(", "ax", "=", "None", ")", ":", "ax", ",", "_", "=", "__get_axes", "(", "ax", "=", "ax", ")", "ax", ".", "yaxis", ".", "set_major_formatter", "(", "FMT_MIDI_NOTE", ")", "# Get the tick labels and reset the vertical alignment", "for", "...
Set the y-axis of the given axes to MIDI notes Parameters ---------- ax : matplotlib.pyplot.axes The axes handle to apply the ticker. By default, uses the current axes handle.
[ "Set", "the", "y", "-", "axis", "of", "the", "given", "axes", "to", "MIDI", "notes" ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L839-L854
21,515
craffel/mir_eval
mir_eval/display.py
ticker_pitch
def ticker_pitch(ax=None): '''Set the y-axis of the given axes to MIDI frequencies Parameters ---------- ax : matplotlib.pyplot.axes The axes handle to apply the ticker. By default, uses the current axes handle. ''' ax, _ = __get_axes(ax=ax) ax.yaxis.set_major_formatter(FMT_MIDI_HZ)
python
def ticker_pitch(ax=None): '''Set the y-axis of the given axes to MIDI frequencies Parameters ---------- ax : matplotlib.pyplot.axes The axes handle to apply the ticker. By default, uses the current axes handle. ''' ax, _ = __get_axes(ax=ax) ax.yaxis.set_major_formatter(FMT_MIDI_HZ)
[ "def", "ticker_pitch", "(", "ax", "=", "None", ")", ":", "ax", ",", "_", "=", "__get_axes", "(", "ax", "=", "ax", ")", "ax", ".", "yaxis", ".", "set_major_formatter", "(", "FMT_MIDI_HZ", ")" ]
Set the y-axis of the given axes to MIDI frequencies Parameters ---------- ax : matplotlib.pyplot.axes The axes handle to apply the ticker. By default, uses the current axes handle.
[ "Set", "the", "y", "-", "axis", "of", "the", "given", "axes", "to", "MIDI", "frequencies" ]
f41c8dafaea04b411252a516d1965af43c7d531b
https://github.com/craffel/mir_eval/blob/f41c8dafaea04b411252a516d1965af43c7d531b/mir_eval/display.py#L857-L868
21,516
CartoDB/carto-python
carto/file_import.py
FileImportJob.run
def run(self, **import_params): """ Actually creates the import job on the CARTO server :param import_params: To be send to the Import API, see CARTO's docs on Import API for an updated list of accepted params :type import_params: kwargs :return: .. note:: The import job is asynchronous, so you should take care of the progression, by calling the :func:`carto.resources.AsyncResource.refresh` method and check the import job :py:attr:`~state` attribute. See :func:`carto.datasets.DatasetManager.create` for a unified method to import files into CARTO """ if self.file: import_params["url"] = self.file self.id_field = "id" if "connection" in import_params: self.fields.append("connector") self.update_from_dict(import_params["connection"]) self.save(force_create=True) else: super(FileImportJob, self).run(params=import_params, files=self.files)
python
def run(self, **import_params): if self.file: import_params["url"] = self.file self.id_field = "id" if "connection" in import_params: self.fields.append("connector") self.update_from_dict(import_params["connection"]) self.save(force_create=True) else: super(FileImportJob, self).run(params=import_params, files=self.files)
[ "def", "run", "(", "self", ",", "*", "*", "import_params", ")", ":", "if", "self", ".", "file", ":", "import_params", "[", "\"url\"", "]", "=", "self", ".", "file", "self", ".", "id_field", "=", "\"id\"", "if", "\"connection\"", "in", "import_params", ...
Actually creates the import job on the CARTO server :param import_params: To be send to the Import API, see CARTO's docs on Import API for an updated list of accepted params :type import_params: kwargs :return: .. note:: The import job is asynchronous, so you should take care of the progression, by calling the :func:`carto.resources.AsyncResource.refresh` method and check the import job :py:attr:`~state` attribute. See :func:`carto.datasets.DatasetManager.create` for a unified method to import files into CARTO
[ "Actually", "creates", "the", "import", "job", "on", "the", "CARTO", "server" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/file_import.py#L79-L103
21,517
CartoDB/carto-python
carto/file_import.py
FileImportJobManager.filter
def filter(self): """ Get a filtered list of file imports :return: A list of file imports, with only the id set (you need to refresh them if you want all the attributes to be filled in) :rtype: list of :class:`carto.file_import.FileImportJob` :raise: CartoException """ try: response = self.send(self.get_collection_endpoint(), "get") if self.json_collection_attribute is not None: resource_ids = self.client.get_response_data( response, self.Meta.parse_json)[self.json_collection_attribute] else: resource_ids = self.client.get_response_data( response, self.Meta.parse_json) except Exception as e: raise CartoException(e) resources = [] for resource_id in resource_ids: try: resource = self.resource_class(self.client) except (ValueError, TypeError): continue else: setattr(resource, resource.Meta.id_field, resource_id) resources.append(resource) return resources
python
def filter(self): try: response = self.send(self.get_collection_endpoint(), "get") if self.json_collection_attribute is not None: resource_ids = self.client.get_response_data( response, self.Meta.parse_json)[self.json_collection_attribute] else: resource_ids = self.client.get_response_data( response, self.Meta.parse_json) except Exception as e: raise CartoException(e) resources = [] for resource_id in resource_ids: try: resource = self.resource_class(self.client) except (ValueError, TypeError): continue else: setattr(resource, resource.Meta.id_field, resource_id) resources.append(resource) return resources
[ "def", "filter", "(", "self", ")", ":", "try", ":", "response", "=", "self", ".", "send", "(", "self", ".", "get_collection_endpoint", "(", ")", ",", "\"get\"", ")", "if", "self", ".", "json_collection_attribute", "is", "not", "None", ":", "resource_ids", ...
Get a filtered list of file imports :return: A list of file imports, with only the id set (you need to refresh them if you want all the attributes to be filled in) :rtype: list of :class:`carto.file_import.FileImportJob` :raise: CartoException
[ "Get", "a", "filtered", "list", "of", "file", "imports" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/file_import.py#L117-L150
21,518
CartoDB/carto-python
carto/auth.py
APIKeyAuthClient.send
def send(self, relative_path, http_method, **requests_args): """ Makes an API-key-authorized request :param relative_path: URL path relative to self.base_url :param http_method: HTTP method :param requests_args: kwargs to be sent to requests :type relative_path: str :type http_method: str :type requests_args: kwargs :return: A request response object :raise: CartoException """ try: http_method, requests_args = self.prepare_send(http_method, **requests_args) response = super(APIKeyAuthClient, self).send(relative_path, http_method, **requests_args) except Exception as e: raise CartoException(e) if CartoRateLimitException.is_rate_limited(response): raise CartoRateLimitException(response) return response
python
def send(self, relative_path, http_method, **requests_args): try: http_method, requests_args = self.prepare_send(http_method, **requests_args) response = super(APIKeyAuthClient, self).send(relative_path, http_method, **requests_args) except Exception as e: raise CartoException(e) if CartoRateLimitException.is_rate_limited(response): raise CartoRateLimitException(response) return response
[ "def", "send", "(", "self", ",", "relative_path", ",", "http_method", ",", "*", "*", "requests_args", ")", ":", "try", ":", "http_method", ",", "requests_args", "=", "self", ".", "prepare_send", "(", "http_method", ",", "*", "*", "requests_args", ")", "res...
Makes an API-key-authorized request :param relative_path: URL path relative to self.base_url :param http_method: HTTP method :param requests_args: kwargs to be sent to requests :type relative_path: str :type http_method: str :type requests_args: kwargs :return: A request response object :raise: CartoException
[ "Makes", "an", "API", "-", "key", "-", "authorized", "request" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/auth.py#L128-L154
21,519
CartoDB/carto-python
carto/auth.py
AuthAPIClient.is_valid_api_key
def is_valid_api_key(self): """ Checks validity. Right now, an API key is considered valid if it can list user API keys and the result contains that API key. This might change in the future. :return: True if the API key is considered valid for current user. """ res = self.send('api/v3/api_keys', 'get') return \ res.ok and \ self.api_key in (ak['token'] for ak in res.json()['result'])
python
def is_valid_api_key(self): res = self.send('api/v3/api_keys', 'get') return \ res.ok and \ self.api_key in (ak['token'] for ak in res.json()['result'])
[ "def", "is_valid_api_key", "(", "self", ")", ":", "res", "=", "self", ".", "send", "(", "'api/v3/api_keys'", ",", "'get'", ")", "return", "res", ".", "ok", "and", "self", ".", "api_key", "in", "(", "ak", "[", "'token'", "]", "for", "ak", "in", "res",...
Checks validity. Right now, an API key is considered valid if it can list user API keys and the result contains that API key. This might change in the future. :return: True if the API key is considered valid for current user.
[ "Checks", "validity", ".", "Right", "now", "an", "API", "key", "is", "considered", "valid", "if", "it", "can", "list", "user", "API", "keys", "and", "the", "result", "contains", "that", "API", "key", ".", "This", "might", "change", "in", "the", "future",...
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/auth.py#L262-L273
21,520
CartoDB/carto-python
carto/datasets.py
DatasetManager.send
def send(self, url, http_method, **client_args): """ Sends an API request, taking into account that datasets are part of the visualization endpoint. :param url: Endpoint URL :param http_method: The method used to make the request to the API :param client_args: Arguments to be sent to the auth client :type url: str :type http_method: str :type client_args: kwargs :return: A request response object :raise: CartoException """ try: client_args = client_args or {} if "params" not in client_args: client_args["params"] = {} client_args["params"].update({"type": "table", "exclude_shared": "true"}) return super(DatasetManager, self).send(url, http_method, **client_args) except Exception as e: raise CartoException(e)
python
def send(self, url, http_method, **client_args): try: client_args = client_args or {} if "params" not in client_args: client_args["params"] = {} client_args["params"].update({"type": "table", "exclude_shared": "true"}) return super(DatasetManager, self).send(url, http_method, **client_args) except Exception as e: raise CartoException(e)
[ "def", "send", "(", "self", ",", "url", ",", "http_method", ",", "*", "*", "client_args", ")", ":", "try", ":", "client_args", "=", "client_args", "or", "{", "}", "if", "\"params\"", "not", "in", "client_args", ":", "client_args", "[", "\"params\"", "]",...
Sends an API request, taking into account that datasets are part of the visualization endpoint. :param url: Endpoint URL :param http_method: The method used to make the request to the API :param client_args: Arguments to be sent to the auth client :type url: str :type http_method: str :type client_args: kwargs :return: A request response object :raise: CartoException
[ "Sends", "an", "API", "request", "taking", "into", "account", "that", "datasets", "are", "part", "of", "the", "visualization", "endpoint", "." ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/datasets.py#L96-L124
21,521
CartoDB/carto-python
carto/datasets.py
DatasetManager.is_sync_table
def is_sync_table(self, archive, interval, **import_args): """ Checks if this is a request for a sync dataset. The condition for creating a sync dataset is to provide a URL or a connection to an external database and an interval in seconds :param archive: URL to the file (both remote URLs or local paths are supported) or StringIO object :param interval: Interval in seconds. :param import_args: Connection parameters for an external database :type url: str :type interval: int :type import_args: kwargs :return: True if it is a sync dataset """ return (hasattr(archive, "startswith") and archive.startswith("http") or "connection" in import_args) \ and interval is not None
python
def is_sync_table(self, archive, interval, **import_args): return (hasattr(archive, "startswith") and archive.startswith("http") or "connection" in import_args) \ and interval is not None
[ "def", "is_sync_table", "(", "self", ",", "archive", ",", "interval", ",", "*", "*", "import_args", ")", ":", "return", "(", "hasattr", "(", "archive", ",", "\"startswith\"", ")", "and", "archive", ".", "startswith", "(", "\"http\"", ")", "or", "\"connecti...
Checks if this is a request for a sync dataset. The condition for creating a sync dataset is to provide a URL or a connection to an external database and an interval in seconds :param archive: URL to the file (both remote URLs or local paths are supported) or StringIO object :param interval: Interval in seconds. :param import_args: Connection parameters for an external database :type url: str :type interval: int :type import_args: kwargs :return: True if it is a sync dataset
[ "Checks", "if", "this", "is", "a", "request", "for", "a", "sync", "dataset", "." ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/datasets.py#L126-L146
21,522
CartoDB/carto-python
carto/datasets.py
DatasetManager.create
def create(self, archive, interval=None, **import_args): """ Creating a table means uploading a file or setting up a sync table :param archive: URL to the file (both remote URLs or local paths are supported) or StringIO object :param interval: Interval in seconds. If not None, CARTO will try to set up a sync table against the (remote) URL :param import_args: Arguments to be sent to the import job when run :type archive: str :type interval: int :type import_args: kwargs :return: New dataset object :rtype: Dataset :raise: CartoException """ archive = archive.lower() if hasattr(archive, "lower") else archive if self.is_sync_table(archive, interval, **import_args): manager = SyncTableJobManager(self.client) else: manager = FileImportJobManager(self.client) import_job = manager.create(archive) if interval is None \ else manager.create(archive, interval) import_job.run(**import_args) if import_job.get_id() is None: raise CartoException(_("Import API returned corrupt job details \ when creating dataset")) import_job.refresh() count = 0 while import_job.state in ("enqueued", "queued", "pending", "uploading", "unpacking", "importing", "guessing") \ or (isinstance(manager, SyncTableJobManager) and import_job.state == "created"): if count >= MAX_NUMBER_OF_RETRIES: raise CartoException(_("Maximum number of retries exceeded \ when polling the import API for \ dataset creation")) time.sleep(INTERVAL_BETWEEN_RETRIES_S) import_job.refresh() count += 1 if import_job.state == "failure": raise CartoException(_("Dataset creation was not successful \ because of failed import (error: {error}") .format(error=json.dumps( import_job.get_error_text))) if (import_job.state != "complete" and import_job.state != "created" and import_job.state != "success") \ or import_job.success is False: raise CartoException(_("Dataset creation was not successful \ because of unknown import error")) if hasattr(import_job, "visualization_id") \ and import_job.visualization_id is not None: visualization_id = import_job.visualization_id else: table = TableManager(self.client).get(import_job.table_id) visualization_id = table.table_visualization.get_id() \ if table is not None else None try: return self.get(visualization_id) if visualization_id is not None \ else None except AttributeError: raise CartoException(_("Dataset creation was not successful \ because of unknown error"))
python
def create(self, archive, interval=None, **import_args): archive = archive.lower() if hasattr(archive, "lower") else archive if self.is_sync_table(archive, interval, **import_args): manager = SyncTableJobManager(self.client) else: manager = FileImportJobManager(self.client) import_job = manager.create(archive) if interval is None \ else manager.create(archive, interval) import_job.run(**import_args) if import_job.get_id() is None: raise CartoException(_("Import API returned corrupt job details \ when creating dataset")) import_job.refresh() count = 0 while import_job.state in ("enqueued", "queued", "pending", "uploading", "unpacking", "importing", "guessing") \ or (isinstance(manager, SyncTableJobManager) and import_job.state == "created"): if count >= MAX_NUMBER_OF_RETRIES: raise CartoException(_("Maximum number of retries exceeded \ when polling the import API for \ dataset creation")) time.sleep(INTERVAL_BETWEEN_RETRIES_S) import_job.refresh() count += 1 if import_job.state == "failure": raise CartoException(_("Dataset creation was not successful \ because of failed import (error: {error}") .format(error=json.dumps( import_job.get_error_text))) if (import_job.state != "complete" and import_job.state != "created" and import_job.state != "success") \ or import_job.success is False: raise CartoException(_("Dataset creation was not successful \ because of unknown import error")) if hasattr(import_job, "visualization_id") \ and import_job.visualization_id is not None: visualization_id = import_job.visualization_id else: table = TableManager(self.client).get(import_job.table_id) visualization_id = table.table_visualization.get_id() \ if table is not None else None try: return self.get(visualization_id) if visualization_id is not None \ else None except AttributeError: raise CartoException(_("Dataset creation was not successful \ because of unknown error"))
[ "def", "create", "(", "self", ",", "archive", ",", "interval", "=", "None", ",", "*", "*", "import_args", ")", ":", "archive", "=", "archive", ".", "lower", "(", ")", "if", "hasattr", "(", "archive", ",", "\"lower\"", ")", "else", "archive", "if", "s...
Creating a table means uploading a file or setting up a sync table :param archive: URL to the file (both remote URLs or local paths are supported) or StringIO object :param interval: Interval in seconds. If not None, CARTO will try to set up a sync table against the (remote) URL :param import_args: Arguments to be sent to the import job when run :type archive: str :type interval: int :type import_args: kwargs :return: New dataset object :rtype: Dataset :raise: CartoException
[ "Creating", "a", "table", "means", "uploading", "a", "file", "or", "setting", "up", "a", "sync", "table" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/datasets.py#L148-L222
21,523
CartoDB/carto-python
carto/visualizations.py
VisualizationManager.send
def send(self, url, http_method, **client_args): """ Sends API request, taking into account that visualizations are only a subset of the resources available at the visualization endpoint :param url: Endpoint URL :param http_method: The method used to make the request to the API :param client_args: Arguments to be sent to the auth client :type url: str :type http_method: str :type client_args: kwargs :return: :raise: CartoException """ try: client_args.setdefault('params', {}) client_args["params"].update({"type": "derived", "exclude_shared": "true"}) return super(VisualizationManager, self).send(url, http_method, **client_args) except Exception as e: raise CartoException(e)
python
def send(self, url, http_method, **client_args): try: client_args.setdefault('params', {}) client_args["params"].update({"type": "derived", "exclude_shared": "true"}) return super(VisualizationManager, self).send(url, http_method, **client_args) except Exception as e: raise CartoException(e)
[ "def", "send", "(", "self", ",", "url", ",", "http_method", ",", "*", "*", "client_args", ")", ":", "try", ":", "client_args", ".", "setdefault", "(", "'params'", ",", "{", "}", ")", "client_args", "[", "\"params\"", "]", ".", "update", "(", "{", "\"...
Sends API request, taking into account that visualizations are only a subset of the resources available at the visualization endpoint :param url: Endpoint URL :param http_method: The method used to make the request to the API :param client_args: Arguments to be sent to the auth client :type url: str :type http_method: str :type client_args: kwargs :return: :raise: CartoException
[ "Sends", "API", "request", "taking", "into", "account", "that", "visualizations", "are", "only", "a", "subset", "of", "the", "resources", "available", "at", "the", "visualization", "endpoint" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/visualizations.py#L130-L155
21,524
CartoDB/carto-python
carto/exceptions.py
CartoRateLimitException.is_rate_limited
def is_rate_limited(response): """ Checks if the response has been rate limited by CARTO APIs :param response: The response rate limited by CARTO APIs :type response: requests.models.Response class :return: Boolean """ if (response.status_code == codes.too_many_requests and 'Retry-After' in response.headers and int(response.headers['Retry-After']) >= 0): return True return False
python
def is_rate_limited(response): if (response.status_code == codes.too_many_requests and 'Retry-After' in response.headers and int(response.headers['Retry-After']) >= 0): return True return False
[ "def", "is_rate_limited", "(", "response", ")", ":", "if", "(", "response", ".", "status_code", "==", "codes", ".", "too_many_requests", "and", "'Retry-After'", "in", "response", ".", "headers", "and", "int", "(", "response", ".", "headers", "[", "'Retry-After...
Checks if the response has been rate limited by CARTO APIs :param response: The response rate limited by CARTO APIs :type response: requests.models.Response class :return: Boolean
[ "Checks", "if", "the", "response", "has", "been", "rate", "limited", "by", "CARTO", "APIs" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/exceptions.py#L46-L59
21,525
CartoDB/carto-python
carto/maps.py
NamedMap.update_from_dict
def update_from_dict(self, attribute_dict): """ Method overriden from the base class """ if 'template' in attribute_dict: self.update_from_dict(attribute_dict['template']) setattr(self, self.Meta.id_field, attribute_dict['template']['name']) return try: for k, v in attribute_dict.items(): setattr(self, k, v) except Exception: setattr(self, self.Meta.id_field, attribute_dict)
python
def update_from_dict(self, attribute_dict): if 'template' in attribute_dict: self.update_from_dict(attribute_dict['template']) setattr(self, self.Meta.id_field, attribute_dict['template']['name']) return try: for k, v in attribute_dict.items(): setattr(self, k, v) except Exception: setattr(self, self.Meta.id_field, attribute_dict)
[ "def", "update_from_dict", "(", "self", ",", "attribute_dict", ")", ":", "if", "'template'", "in", "attribute_dict", ":", "self", ".", "update_from_dict", "(", "attribute_dict", "[", "'template'", "]", ")", "setattr", "(", "self", ",", "self", ".", "Meta", "...
Method overriden from the base class
[ "Method", "overriden", "from", "the", "base", "class" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/maps.py#L165-L179
21,526
CartoDB/carto-python
carto/resources.py
AsyncResource.run
def run(self, **client_params): """ Actually creates the async job on the CARTO server :param client_params: To be send to the CARTO API. See CARTO's documentation depending on the subclass you are using :type client_params: kwargs :return: :raise: CartoException """ try: self.send(self.get_collection_endpoint(), http_method="POST", **client_params) except Exception as e: raise CartoException(e)
python
def run(self, **client_params): try: self.send(self.get_collection_endpoint(), http_method="POST", **client_params) except Exception as e: raise CartoException(e)
[ "def", "run", "(", "self", ",", "*", "*", "client_params", ")", ":", "try", ":", "self", ".", "send", "(", "self", ".", "get_collection_endpoint", "(", ")", ",", "http_method", "=", "\"POST\"", ",", "*", "*", "client_params", ")", "except", "Exception", ...
Actually creates the async job on the CARTO server :param client_params: To be send to the CARTO API. See CARTO's documentation depending on the subclass you are using :type client_params: kwargs :return: :raise: CartoException
[ "Actually", "creates", "the", "async", "job", "on", "the", "CARTO", "server" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/resources.py#L22-L41
21,527
CartoDB/carto-python
carto/sql.py
SQLClient.send
def send(self, sql, parse_json=True, do_post=True, format=None, **request_args): """ Executes SQL query in a CARTO server :param sql: The SQL :param parse_json: Set it to False if you want raw reponse :param do_post: Set it to True to force post request :param format: Any of the data export formats allowed by CARTO's SQL API :param request_args: Additional parameters to send with the request :type sql: str :type parse_json: boolean :type do_post: boolean :type format: str :type request_args: dictionary :return: response data, either as json or as a regular response.content object :rtype: object :raise: CartoException """ try: params = {'q': sql} if format: params['format'] = format if format not in ['json', 'geojson']: parse_json = False if request_args is not None: for attr in request_args: params[attr] = request_args[attr] if len(sql) < MAX_GET_QUERY_LEN and do_post is False: resp = self.auth_client.send(self.api_url, 'GET', params=params) else: resp = self.auth_client.send(self.api_url, 'POST', data=params) return self.auth_client.get_response_data(resp, parse_json) except CartoRateLimitException as e: raise e except Exception as e: raise CartoException(e)
python
def send(self, sql, parse_json=True, do_post=True, format=None, **request_args): try: params = {'q': sql} if format: params['format'] = format if format not in ['json', 'geojson']: parse_json = False if request_args is not None: for attr in request_args: params[attr] = request_args[attr] if len(sql) < MAX_GET_QUERY_LEN and do_post is False: resp = self.auth_client.send(self.api_url, 'GET', params=params) else: resp = self.auth_client.send(self.api_url, 'POST', data=params) return self.auth_client.get_response_data(resp, parse_json) except CartoRateLimitException as e: raise e except Exception as e: raise CartoException(e)
[ "def", "send", "(", "self", ",", "sql", ",", "parse_json", "=", "True", ",", "do_post", "=", "True", ",", "format", "=", "None", ",", "*", "*", "request_args", ")", ":", "try", ":", "params", "=", "{", "'q'", ":", "sql", "}", "if", "format", ":",...
Executes SQL query in a CARTO server :param sql: The SQL :param parse_json: Set it to False if you want raw reponse :param do_post: Set it to True to force post request :param format: Any of the data export formats allowed by CARTO's SQL API :param request_args: Additional parameters to send with the request :type sql: str :type parse_json: boolean :type do_post: boolean :type format: str :type request_args: dictionary :return: response data, either as json or as a regular response.content object :rtype: object :raise: CartoException
[ "Executes", "SQL", "query", "in", "a", "CARTO", "server" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L70-L114
21,528
CartoDB/carto-python
carto/sql.py
BatchSQLClient.send
def send(self, url, http_method, json_body=None, http_header=None): """ Executes Batch SQL query in a CARTO server :param url: Endpoint url :param http_method: The method used to make the request to the API :param json_body: The information that needs to be sent, by default is set to None :param http_header: The header used to make write requests to the API, by default is none :type url: str :type http_method: str :type json_body: dict :type http_header: str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException """ try: data = self.client.send(url, http_method=http_method, headers=http_header, json=json_body) data_json = self.client.get_response_data(data) except CartoRateLimitException as e: raise e except Exception as e: raise CartoException(e) return data_json
python
def send(self, url, http_method, json_body=None, http_header=None): try: data = self.client.send(url, http_method=http_method, headers=http_header, json=json_body) data_json = self.client.get_response_data(data) except CartoRateLimitException as e: raise e except Exception as e: raise CartoException(e) return data_json
[ "def", "send", "(", "self", ",", "url", ",", "http_method", ",", "json_body", "=", "None", ",", "http_header", "=", "None", ")", ":", "try", ":", "data", "=", "self", ".", "client", ".", "send", "(", "url", ",", "http_method", "=", "http_method", ","...
Executes Batch SQL query in a CARTO server :param url: Endpoint url :param http_method: The method used to make the request to the API :param json_body: The information that needs to be sent, by default is set to None :param http_header: The header used to make write requests to the API, by default is none :type url: str :type http_method: str :type json_body: dict :type http_header: str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException
[ "Executes", "Batch", "SQL", "query", "in", "a", "CARTO", "server" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L149-L180
21,529
CartoDB/carto-python
carto/sql.py
BatchSQLClient.create
def create(self, sql_query): """ Creates a new batch SQL query. Batch SQL jobs are asynchronous, once created you should call :func:`carto.sql.BatchSQLClient.read` method given the `job_id` to retrieve the state of the batch query :param sql_query: The SQL query to be used :type sql_query: str or list of str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException """ header = {'content-type': 'application/json'} data = self.send(self.api_url, http_method="POST", json_body={"query": sql_query}, http_header=header) return data
python
def create(self, sql_query): header = {'content-type': 'application/json'} data = self.send(self.api_url, http_method="POST", json_body={"query": sql_query}, http_header=header) return data
[ "def", "create", "(", "self", ",", "sql_query", ")", ":", "header", "=", "{", "'content-type'", ":", "'application/json'", "}", "data", "=", "self", ".", "send", "(", "self", ".", "api_url", ",", "http_method", "=", "\"POST\"", ",", "json_body", "=", "{"...
Creates a new batch SQL query. Batch SQL jobs are asynchronous, once created you should call :func:`carto.sql.BatchSQLClient.read` method given the `job_id` to retrieve the state of the batch query :param sql_query: The SQL query to be used :type sql_query: str or list of str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException
[ "Creates", "a", "new", "batch", "SQL", "query", "." ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L182-L204
21,530
CartoDB/carto-python
carto/sql.py
BatchSQLClient.create_and_wait_for_completion
def create_and_wait_for_completion(self, sql_query): """ Creates a new batch SQL query and waits for its completion or failure Batch SQL jobs are asynchronous, once created this method automatically queries the job status until it's one of 'done', 'failed', 'canceled', 'unknown' :param sql_query: The SQL query to be used :type sql_query: str or list of str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException when there's an exception in the BatchSQLJob execution or the batch job status is one of the BATCH_JOBS_FAILED_STATUSES ('failed', 'canceled', 'unknown') """ header = {'content-type': 'application/json'} data = self.send(self.api_url, http_method="POST", json_body={"query": sql_query}, http_header=header) warnings.warn('Batch SQL job created with job_id: {job_id}'.format(job_id=data['job_id'])) while data and data['status'] in BATCH_JOBS_PENDING_STATUSES: time.sleep(BATCH_READ_STATUS_AFTER_SECONDS) data = self.read(data['job_id']) if data['status'] in BATCH_JOBS_FAILED_STATUSES: raise CartoException(_("Batch SQL job failed with result: {data}".format(data=data))) return data
python
def create_and_wait_for_completion(self, sql_query): header = {'content-type': 'application/json'} data = self.send(self.api_url, http_method="POST", json_body={"query": sql_query}, http_header=header) warnings.warn('Batch SQL job created with job_id: {job_id}'.format(job_id=data['job_id'])) while data and data['status'] in BATCH_JOBS_PENDING_STATUSES: time.sleep(BATCH_READ_STATUS_AFTER_SECONDS) data = self.read(data['job_id']) if data['status'] in BATCH_JOBS_FAILED_STATUSES: raise CartoException(_("Batch SQL job failed with result: {data}".format(data=data))) return data
[ "def", "create_and_wait_for_completion", "(", "self", ",", "sql_query", ")", ":", "header", "=", "{", "'content-type'", ":", "'application/json'", "}", "data", "=", "self", ".", "send", "(", "self", ".", "api_url", ",", "http_method", "=", "\"POST\"", ",", "...
Creates a new batch SQL query and waits for its completion or failure Batch SQL jobs are asynchronous, once created this method automatically queries the job status until it's one of 'done', 'failed', 'canceled', 'unknown' :param sql_query: The SQL query to be used :type sql_query: str or list of str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException when there's an exception in the BatchSQLJob execution or the batch job status is one of the BATCH_JOBS_FAILED_STATUSES ('failed', 'canceled', 'unknown')
[ "Creates", "a", "new", "batch", "SQL", "query", "and", "waits", "for", "its", "completion", "or", "failure" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L206-L238
21,531
CartoDB/carto-python
carto/sql.py
BatchSQLClient.read
def read(self, job_id): """ Reads the information for a specific Batch API request :param job_id: The id of the job to be read from :type job_id: str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException """ data = self.send(self.api_url + job_id, http_method="GET") return data
python
def read(self, job_id): data = self.send(self.api_url + job_id, http_method="GET") return data
[ "def", "read", "(", "self", ",", "job_id", ")", ":", "data", "=", "self", ".", "send", "(", "self", ".", "api_url", "+", "job_id", ",", "http_method", "=", "\"GET\"", ")", "return", "data" ]
Reads the information for a specific Batch API request :param job_id: The id of the job to be read from :type job_id: str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException
[ "Reads", "the", "information", "for", "a", "specific", "Batch", "API", "request" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L240-L254
21,532
CartoDB/carto-python
carto/sql.py
BatchSQLClient.update
def update(self, job_id, sql_query): """ Updates the sql query of a specific job :param job_id: The id of the job to be updated :param sql_query: The new SQL query for the job :type job_id: str :type sql_query: str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException """ header = {'content-type': 'application/json'} data = self.send(self.api_url + job_id, http_method="PUT", json_body={"query": sql_query}, http_header=header) return data
python
def update(self, job_id, sql_query): header = {'content-type': 'application/json'} data = self.send(self.api_url + job_id, http_method="PUT", json_body={"query": sql_query}, http_header=header) return data
[ "def", "update", "(", "self", ",", "job_id", ",", "sql_query", ")", ":", "header", "=", "{", "'content-type'", ":", "'application/json'", "}", "data", "=", "self", ".", "send", "(", "self", ".", "api_url", "+", "job_id", ",", "http_method", "=", "\"PUT\"...
Updates the sql query of a specific job :param job_id: The id of the job to be updated :param sql_query: The new SQL query for the job :type job_id: str :type sql_query: str :return: Response data, either as json or as a regular response.content object :rtype: object :raise: CartoException
[ "Updates", "the", "sql", "query", "of", "a", "specific", "job" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L256-L276
21,533
CartoDB/carto-python
carto/sql.py
BatchSQLClient.cancel
def cancel(self, job_id): """ Cancels a job :param job_id: The id of the job to be cancelled :type job_id: str :return: A status code depending on whether the cancel request was successful :rtype: str :raise CartoException: """ try: confirmation = self.send(self.api_url + job_id, http_method="DELETE") except CartoException as e: if 'Cannot set status from done to cancelled' in e.args[0].args[0]: return 'done' else: raise e return confirmation['status']
python
def cancel(self, job_id): try: confirmation = self.send(self.api_url + job_id, http_method="DELETE") except CartoException as e: if 'Cannot set status from done to cancelled' in e.args[0].args[0]: return 'done' else: raise e return confirmation['status']
[ "def", "cancel", "(", "self", ",", "job_id", ")", ":", "try", ":", "confirmation", "=", "self", ".", "send", "(", "self", ".", "api_url", "+", "job_id", ",", "http_method", "=", "\"DELETE\"", ")", "except", "CartoException", "as", "e", ":", "if", "'Can...
Cancels a job :param job_id: The id of the job to be cancelled :type job_id: str :return: A status code depending on whether the cancel request was successful :rtype: str :raise CartoException:
[ "Cancels", "a", "job" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L278-L298
21,534
CartoDB/carto-python
carto/sql.py
CopySQLClient.copyfrom
def copyfrom(self, query, iterable_data, compress=True, compression_level=DEFAULT_COMPRESSION_LEVEL): """ Gets data from an iterable object into a table :param query: The "COPY table_name [(column_name[, ...])] FROM STDIN [WITH(option[,...])]" query to execute :type query: str :param iterable_data: An object that can be iterated to retrieve the data :type iterable_data: object :return: Response data as json :rtype: str :raise CartoException: """ url = self.api_url + '/copyfrom' headers = { 'Content-Type': 'application/octet-stream', 'Transfer-Encoding': 'chunked' } params = {'api_key': self.api_key, 'q': query} if compress: headers['Content-Encoding'] = 'gzip' _iterable_data = self._compress_chunks(iterable_data, compression_level) else: _iterable_data = iterable_data try: response = self.client.send(url, http_method='POST', params=params, data=_iterable_data, headers=headers, stream=True) response_json = self.client.get_response_data(response) except CartoRateLimitException as e: raise e except Exception as e: raise CartoException(e) return response_json
python
def copyfrom(self, query, iterable_data, compress=True, compression_level=DEFAULT_COMPRESSION_LEVEL): url = self.api_url + '/copyfrom' headers = { 'Content-Type': 'application/octet-stream', 'Transfer-Encoding': 'chunked' } params = {'api_key': self.api_key, 'q': query} if compress: headers['Content-Encoding'] = 'gzip' _iterable_data = self._compress_chunks(iterable_data, compression_level) else: _iterable_data = iterable_data try: response = self.client.send(url, http_method='POST', params=params, data=_iterable_data, headers=headers, stream=True) response_json = self.client.get_response_data(response) except CartoRateLimitException as e: raise e except Exception as e: raise CartoException(e) return response_json
[ "def", "copyfrom", "(", "self", ",", "query", ",", "iterable_data", ",", "compress", "=", "True", ",", "compression_level", "=", "DEFAULT_COMPRESSION_LEVEL", ")", ":", "url", "=", "self", ".", "api_url", "+", "'/copyfrom'", "headers", "=", "{", "'Content-Type'...
Gets data from an iterable object into a table :param query: The "COPY table_name [(column_name[, ...])] FROM STDIN [WITH(option[,...])]" query to execute :type query: str :param iterable_data: An object that can be iterated to retrieve the data :type iterable_data: object :return: Response data as json :rtype: str :raise CartoException:
[ "Gets", "data", "from", "an", "iterable", "object", "into", "a", "table" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L341-L386
21,535
CartoDB/carto-python
carto/sql.py
CopySQLClient.copyfrom_file_object
def copyfrom_file_object(self, query, file_object, compress=True, compression_level=DEFAULT_COMPRESSION_LEVEL): """ Gets data from a readable file object into a table :param query: The "COPY table_name [(column_name[, ...])] FROM STDIN [WITH(option[,...])]" query to execute :type query: str :param file_object: A file-like object. Normally the return value of open('file.ext', 'rb') :type file_object: file :return: Response data as json :rtype: str :raise CartoException: """ chunk_generator = self._read_in_chunks(file_object) return self.copyfrom(query, chunk_generator, compress, compression_level)
python
def copyfrom_file_object(self, query, file_object, compress=True, compression_level=DEFAULT_COMPRESSION_LEVEL): chunk_generator = self._read_in_chunks(file_object) return self.copyfrom(query, chunk_generator, compress, compression_level)
[ "def", "copyfrom_file_object", "(", "self", ",", "query", ",", "file_object", ",", "compress", "=", "True", ",", "compression_level", "=", "DEFAULT_COMPRESSION_LEVEL", ")", ":", "chunk_generator", "=", "self", ".", "_read_in_chunks", "(", "file_object", ")", "retu...
Gets data from a readable file object into a table :param query: The "COPY table_name [(column_name[, ...])] FROM STDIN [WITH(option[,...])]" query to execute :type query: str :param file_object: A file-like object. Normally the return value of open('file.ext', 'rb') :type file_object: file :return: Response data as json :rtype: str :raise CartoException:
[ "Gets", "data", "from", "a", "readable", "file", "object", "into", "a", "table" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L388-L408
21,536
CartoDB/carto-python
carto/sql.py
CopySQLClient.copyfrom_file_path
def copyfrom_file_path(self, query, path, compress=True, compression_level=DEFAULT_COMPRESSION_LEVEL): """ Gets data from a readable file into a table :param query: The "COPY table_name [(column_name[, ...])] FROM STDIN [WITH(option[,...])]" query to execute :type query: str :param path: A path to a file :type path: str :return: Response data as json :rtype: str :raise CartoException: """ with open(path, 'rb') as f: result = self.copyfrom_file_object(query, f, compress, compression_level) return result
python
def copyfrom_file_path(self, query, path, compress=True, compression_level=DEFAULT_COMPRESSION_LEVEL): with open(path, 'rb') as f: result = self.copyfrom_file_object(query, f, compress, compression_level) return result
[ "def", "copyfrom_file_path", "(", "self", ",", "query", ",", "path", ",", "compress", "=", "True", ",", "compression_level", "=", "DEFAULT_COMPRESSION_LEVEL", ")", ":", "with", "open", "(", "path", ",", "'rb'", ")", "as", "f", ":", "result", "=", "self", ...
Gets data from a readable file into a table :param query: The "COPY table_name [(column_name[, ...])] FROM STDIN [WITH(option[,...])]" query to execute :type query: str :param path: A path to a file :type path: str :return: Response data as json :rtype: str :raise CartoException:
[ "Gets", "data", "from", "a", "readable", "file", "into", "a", "table" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L410-L430
21,537
CartoDB/carto-python
carto/sql.py
CopySQLClient.copyto
def copyto(self, query): """ Gets data from a table into a Response object that can be iterated :param query: The "COPY { table_name [(column_name[, ...])] | (query) } TO STDOUT [WITH(option[,...])]" query to execute :type query: str :return: response object :rtype: Response :raise CartoException: """ url = self.api_url + '/copyto' params = {'api_key': self.api_key, 'q': query} try: response = self.client.send(url, http_method='GET', params=params, stream=True) response.raise_for_status() except CartoRateLimitException as e: raise e except HTTPError as e: if 400 <= response.status_code < 500: # Client error, provide better reason reason = response.json()['error'][0] error_msg = u'%s Client Error: %s' % (response.status_code, reason) raise CartoException(error_msg) else: raise CartoException(e) except Exception as e: raise CartoException(e) return response
python
def copyto(self, query): url = self.api_url + '/copyto' params = {'api_key': self.api_key, 'q': query} try: response = self.client.send(url, http_method='GET', params=params, stream=True) response.raise_for_status() except CartoRateLimitException as e: raise e except HTTPError as e: if 400 <= response.status_code < 500: # Client error, provide better reason reason = response.json()['error'][0] error_msg = u'%s Client Error: %s' % (response.status_code, reason) raise CartoException(error_msg) else: raise CartoException(e) except Exception as e: raise CartoException(e) return response
[ "def", "copyto", "(", "self", ",", "query", ")", ":", "url", "=", "self", ".", "api_url", "+", "'/copyto'", "params", "=", "{", "'api_key'", ":", "self", ".", "api_key", ",", "'q'", ":", "query", "}", "try", ":", "response", "=", "self", ".", "clie...
Gets data from a table into a Response object that can be iterated :param query: The "COPY { table_name [(column_name[, ...])] | (query) } TO STDOUT [WITH(option[,...])]" query to execute :type query: str :return: response object :rtype: Response :raise CartoException:
[ "Gets", "data", "from", "a", "table", "into", "a", "Response", "object", "that", "can", "be", "iterated" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L432-L468
21,538
CartoDB/carto-python
carto/sql.py
CopySQLClient.copyto_file_object
def copyto_file_object(self, query, file_object): """ Gets data from a table into a writable file object :param query: The "COPY { table_name [(column_name[, ...])] | (query) } TO STDOUT [WITH(option[,...])]" query to execute :type query: str :param file_object: A file-like object. Normally the return value of open('file.ext', 'wb') :type file_object: file :raise CartoException: """ response = self.copyto(query) for block in response.iter_content(DEFAULT_CHUNK_SIZE): file_object.write(block)
python
def copyto_file_object(self, query, file_object): response = self.copyto(query) for block in response.iter_content(DEFAULT_CHUNK_SIZE): file_object.write(block)
[ "def", "copyto_file_object", "(", "self", ",", "query", ",", "file_object", ")", ":", "response", "=", "self", ".", "copyto", "(", "query", ")", "for", "block", "in", "response", ".", "iter_content", "(", "DEFAULT_CHUNK_SIZE", ")", ":", "file_object", ".", ...
Gets data from a table into a writable file object :param query: The "COPY { table_name [(column_name[, ...])] | (query) } TO STDOUT [WITH(option[,...])]" query to execute :type query: str :param file_object: A file-like object. Normally the return value of open('file.ext', 'wb') :type file_object: file :raise CartoException:
[ "Gets", "data", "from", "a", "table", "into", "a", "writable", "file", "object" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L470-L486
21,539
CartoDB/carto-python
carto/sql.py
CopySQLClient.copyto_file_path
def copyto_file_path(self, query, path, append=False): """ Gets data from a table into a writable file :param query: The "COPY { table_name [(column_name[, ...])] | (query) } TO STDOUT [WITH(option[,...])]" query to execute :type query: str :param path: A path to a writable file :type path: str :param append: Whether to append or not if the file already exists Default value is False :type append: bool :raise CartoException: """ file_mode = 'wb' if not append else 'ab' with open(path, file_mode) as f: self.copyto_file_object(query, f)
python
def copyto_file_path(self, query, path, append=False): file_mode = 'wb' if not append else 'ab' with open(path, file_mode) as f: self.copyto_file_object(query, f)
[ "def", "copyto_file_path", "(", "self", ",", "query", ",", "path", ",", "append", "=", "False", ")", ":", "file_mode", "=", "'wb'", "if", "not", "append", "else", "'ab'", "with", "open", "(", "path", ",", "file_mode", ")", "as", "f", ":", "self", "."...
Gets data from a table into a writable file :param query: The "COPY { table_name [(column_name[, ...])] | (query) } TO STDOUT [WITH(option[,...])]" query to execute :type query: str :param path: A path to a writable file :type path: str :param append: Whether to append or not if the file already exists Default value is False :type append: bool :raise CartoException:
[ "Gets", "data", "from", "a", "table", "into", "a", "writable", "file" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sql.py#L488-L507
21,540
CartoDB/carto-python
carto/sync_tables.py
SyncTableJob.run
def run(self, **import_params): """ Actually creates the job import on the CARTO server :param import_params: To be send to the Import API, see CARTO's docs on Import API for an updated list of accepted params :type import_params: kwargs :return: .. note:: The sync table job is asynchronous, so you should take care of the progression, by calling the :func:`carto.resources.AsyncResource.refresh` method and check the import job :py:attr:`~state` attribute. See :func:`carto.datasets.DatasetManager.create` for a unified method to import files into CARTO """ import_params["url"] = self.url import_params["interval"] = self.interval if "connection" in import_params: self.fields.append("connector") import_params["connection"]["interval"] = self.interval self.update_from_dict(import_params["connection"]) self.save(force_create=True) else: return super(SyncTableJob, self).run(params=import_params)
python
def run(self, **import_params): import_params["url"] = self.url import_params["interval"] = self.interval if "connection" in import_params: self.fields.append("connector") import_params["connection"]["interval"] = self.interval self.update_from_dict(import_params["connection"]) self.save(force_create=True) else: return super(SyncTableJob, self).run(params=import_params)
[ "def", "run", "(", "self", ",", "*", "*", "import_params", ")", ":", "import_params", "[", "\"url\"", "]", "=", "self", ".", "url", "import_params", "[", "\"interval\"", "]", "=", "self", ".", "interval", "if", "\"connection\"", "in", "import_params", ":",...
Actually creates the job import on the CARTO server :param import_params: To be send to the Import API, see CARTO's docs on Import API for an updated list of accepted params :type import_params: kwargs :return: .. note:: The sync table job is asynchronous, so you should take care of the progression, by calling the :func:`carto.resources.AsyncResource.refresh` method and check the import job :py:attr:`~state` attribute. See :func:`carto.datasets.DatasetManager.create` for a unified method to import files into CARTO
[ "Actually", "creates", "the", "job", "import", "on", "the", "CARTO", "server" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sync_tables.py#L84-L106
21,541
CartoDB/carto-python
carto/sync_tables.py
SyncTableJob.force_sync
def force_sync(self): """ Forces to sync the SyncTableJob :return: :raise: CartoException """ try: self.send(self.get_resource_endpoint(), "put") except Exception as e: raise CartoException(e)
python
def force_sync(self): try: self.send(self.get_resource_endpoint(), "put") except Exception as e: raise CartoException(e)
[ "def", "force_sync", "(", "self", ")", ":", "try", ":", "self", ".", "send", "(", "self", ".", "get_resource_endpoint", "(", ")", ",", "\"put\"", ")", "except", "Exception", "as", "e", ":", "raise", "CartoException", "(", "e", ")" ]
Forces to sync the SyncTableJob :return: :raise: CartoException
[ "Forces", "to", "sync", "the", "SyncTableJob" ]
f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16
https://github.com/CartoDB/carto-python/blob/f6ac3d17ed08e5bc3f99edd2bde4fb7dba3eee16/carto/sync_tables.py#L118-L129
21,542
ikki407/stacking
stacking/base.py
BaseModel.set_prob_type
def set_prob_type(cls, problem_type, classification_type, eval_type): """ Set problem type """ assert problem_type in problem_type_list, 'Need to set Problem Type' if problem_type == 'classification': assert classification_type in classification_type_list,\ 'Need to set Classification Type' assert eval_type in eval_type_list, 'Need to set Evaluation Type' cls.problem_type = problem_type cls.classification_type = classification_type cls.eval_type = eval_type if cls.problem_type == 'classification': print 'Setting Problem:{}, Type:{}, Eval:{}'.format(cls.problem_type, cls.classification_type, cls.eval_type) elif cls.problem_type == 'regression': print 'Setting Problem:{}, Eval:{}'.format(cls.problem_type, cls.eval_type) return
python
def set_prob_type(cls, problem_type, classification_type, eval_type): assert problem_type in problem_type_list, 'Need to set Problem Type' if problem_type == 'classification': assert classification_type in classification_type_list,\ 'Need to set Classification Type' assert eval_type in eval_type_list, 'Need to set Evaluation Type' cls.problem_type = problem_type cls.classification_type = classification_type cls.eval_type = eval_type if cls.problem_type == 'classification': print 'Setting Problem:{}, Type:{}, Eval:{}'.format(cls.problem_type, cls.classification_type, cls.eval_type) elif cls.problem_type == 'regression': print 'Setting Problem:{}, Eval:{}'.format(cls.problem_type, cls.eval_type) return
[ "def", "set_prob_type", "(", "cls", ",", "problem_type", ",", "classification_type", ",", "eval_type", ")", ":", "assert", "problem_type", "in", "problem_type_list", ",", "'Need to set Problem Type'", "if", "problem_type", "==", "'classification'", ":", "assert", "cla...
Set problem type
[ "Set", "problem", "type" ]
105073598fd4f9481212d9db9dea92559d9a9d5a
https://github.com/ikki407/stacking/blob/105073598fd4f9481212d9db9dea92559d9a9d5a/stacking/base.py#L280-L301
21,543
ikki407/stacking
stacking/base.py
BaseModel.make_multi_cols
def make_multi_cols(self, num_class, name): '''make cols for multi-class predictions''' cols = ['c' + str(i) + '_' for i in xrange(num_class)] cols = map(lambda x: x + name, cols) return cols
python
def make_multi_cols(self, num_class, name): '''make cols for multi-class predictions''' cols = ['c' + str(i) + '_' for i in xrange(num_class)] cols = map(lambda x: x + name, cols) return cols
[ "def", "make_multi_cols", "(", "self", ",", "num_class", ",", "name", ")", ":", "cols", "=", "[", "'c'", "+", "str", "(", "i", ")", "+", "'_'", "for", "i", "in", "xrange", "(", "num_class", ")", "]", "cols", "=", "map", "(", "lambda", "x", ":", ...
make cols for multi-class predictions
[ "make", "cols", "for", "multi", "-", "class", "predictions" ]
105073598fd4f9481212d9db9dea92559d9a9d5a
https://github.com/ikki407/stacking/blob/105073598fd4f9481212d9db9dea92559d9a9d5a/stacking/base.py#L308-L312
21,544
clalancette/pycdlib
pycdlib/path_table_record.py
PathTableRecord.parse
def parse(self, data): # type: (bytes) -> None ''' A method to parse an ISO9660 Path Table Record out of a string. Parameters: data - The string to parse. Returns: Nothing. ''' (self.len_di, self.xattr_length, self.extent_location, self.parent_directory_num) = struct.unpack_from(self.FMT, data[:8], 0) if self.len_di % 2 != 0: self.directory_identifier = data[8:-1] else: self.directory_identifier = data[8:] self.dirrecord = None self._initialized = True
python
def parse(self, data): # type: (bytes) -> None ''' A method to parse an ISO9660 Path Table Record out of a string. Parameters: data - The string to parse. Returns: Nothing. ''' (self.len_di, self.xattr_length, self.extent_location, self.parent_directory_num) = struct.unpack_from(self.FMT, data[:8], 0) if self.len_di % 2 != 0: self.directory_identifier = data[8:-1] else: self.directory_identifier = data[8:] self.dirrecord = None self._initialized = True
[ "def", "parse", "(", "self", ",", "data", ")", ":", "# type: (bytes) -> None", "(", "self", ".", "len_di", ",", "self", ".", "xattr_length", ",", "self", ".", "extent_location", ",", "self", ".", "parent_directory_num", ")", "=", "struct", ".", "unpack_from"...
A method to parse an ISO9660 Path Table Record out of a string. Parameters: data - The string to parse. Returns: Nothing.
[ "A", "method", "to", "parse", "an", "ISO9660", "Path", "Table", "Record", "out", "of", "a", "string", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/path_table_record.py#L46-L64
21,545
clalancette/pycdlib
pycdlib/path_table_record.py
PathTableRecord._record
def _record(self, ext_loc, parent_dir_num): # type: (int, int) -> bytes ''' An internal method to generate a string representing this Path Table Record. Parameters: ext_loc - The extent location to place in this Path Table Record. parent_dir_num - The parent directory number to place in this Path Table Record. Returns: A string representing this Path Table Record. ''' return struct.pack(self.FMT, self.len_di, self.xattr_length, ext_loc, parent_dir_num) + self.directory_identifier + b'\x00' * (self.len_di % 2)
python
def _record(self, ext_loc, parent_dir_num): # type: (int, int) -> bytes ''' An internal method to generate a string representing this Path Table Record. Parameters: ext_loc - The extent location to place in this Path Table Record. parent_dir_num - The parent directory number to place in this Path Table Record. Returns: A string representing this Path Table Record. ''' return struct.pack(self.FMT, self.len_di, self.xattr_length, ext_loc, parent_dir_num) + self.directory_identifier + b'\x00' * (self.len_di % 2)
[ "def", "_record", "(", "self", ",", "ext_loc", ",", "parent_dir_num", ")", ":", "# type: (int, int) -> bytes", "return", "struct", ".", "pack", "(", "self", ".", "FMT", ",", "self", ".", "len_di", ",", "self", ".", "xattr_length", ",", "ext_loc", ",", "par...
An internal method to generate a string representing this Path Table Record. Parameters: ext_loc - The extent location to place in this Path Table Record. parent_dir_num - The parent directory number to place in this Path Table Record. Returns: A string representing this Path Table Record.
[ "An", "internal", "method", "to", "generate", "a", "string", "representing", "this", "Path", "Table", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/path_table_record.py#L66-L79
21,546
clalancette/pycdlib
pycdlib/path_table_record.py
PathTableRecord.record_little_endian
def record_little_endian(self): # type: () -> bytes ''' A method to generate a string representing the little endian version of this Path Table Record. Parameters: None. Returns: A string representing the little endian version of this Path Table Record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized') return self._record(self.extent_location, self.parent_directory_num)
python
def record_little_endian(self): # type: () -> bytes ''' A method to generate a string representing the little endian version of this Path Table Record. Parameters: None. Returns: A string representing the little endian version of this Path Table Record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized') return self._record(self.extent_location, self.parent_directory_num)
[ "def", "record_little_endian", "(", "self", ")", ":", "# type: () -> bytes", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Path Table Record not yet initialized'", ")", "return", "self", ".", "_record", ...
A method to generate a string representing the little endian version of this Path Table Record. Parameters: None. Returns: A string representing the little endian version of this Path Table Record.
[ "A", "method", "to", "generate", "a", "string", "representing", "the", "little", "endian", "version", "of", "this", "Path", "Table", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/path_table_record.py#L81-L95
21,547
clalancette/pycdlib
pycdlib/path_table_record.py
PathTableRecord.record_big_endian
def record_big_endian(self): # type: () -> bytes ''' A method to generate a string representing the big endian version of this Path Table Record. Parameters: None. Returns: A string representing the big endian version of this Path Table Record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized') return self._record(utils.swab_32bit(self.extent_location), utils.swab_16bit(self.parent_directory_num))
python
def record_big_endian(self): # type: () -> bytes ''' A method to generate a string representing the big endian version of this Path Table Record. Parameters: None. Returns: A string representing the big endian version of this Path Table Record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized') return self._record(utils.swab_32bit(self.extent_location), utils.swab_16bit(self.parent_directory_num))
[ "def", "record_big_endian", "(", "self", ")", ":", "# type: () -> bytes", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Path Table Record not yet initialized'", ")", "return", "self", ".", "_record", "(...
A method to generate a string representing the big endian version of this Path Table Record. Parameters: None. Returns: A string representing the big endian version of this Path Table Record.
[ "A", "method", "to", "generate", "a", "string", "representing", "the", "big", "endian", "version", "of", "this", "Path", "Table", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/path_table_record.py#L97-L112
21,548
clalancette/pycdlib
pycdlib/path_table_record.py
PathTableRecord._new
def _new(self, name, parent_dir_num): # type: (bytes, int) -> None ''' An internal method to create a new Path Table Record. Parameters: name - The name for this Path Table Record. parent_dir_num - The directory number of the parent of this Path Table Record. Returns: Nothing. ''' self.len_di = len(name) self.xattr_length = 0 # FIXME: we don't support xattr for now self.parent_directory_num = parent_dir_num self.directory_identifier = name self._initialized = True
python
def _new(self, name, parent_dir_num): # type: (bytes, int) -> None ''' An internal method to create a new Path Table Record. Parameters: name - The name for this Path Table Record. parent_dir_num - The directory number of the parent of this Path Table Record. Returns: Nothing. ''' self.len_di = len(name) self.xattr_length = 0 # FIXME: we don't support xattr for now self.parent_directory_num = parent_dir_num self.directory_identifier = name self._initialized = True
[ "def", "_new", "(", "self", ",", "name", ",", "parent_dir_num", ")", ":", "# type: (bytes, int) -> None", "self", ".", "len_di", "=", "len", "(", "name", ")", "self", ".", "xattr_length", "=", "0", "# FIXME: we don't support xattr for now", "self", ".", "parent_...
An internal method to create a new Path Table Record. Parameters: name - The name for this Path Table Record. parent_dir_num - The directory number of the parent of this Path Table Record. Returns: Nothing.
[ "An", "internal", "method", "to", "create", "a", "new", "Path", "Table", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/path_table_record.py#L127-L143
21,549
clalancette/pycdlib
pycdlib/path_table_record.py
PathTableRecord.new_dir
def new_dir(self, name): # type: (bytes) -> None ''' A method to create a new Path Table Record. Parameters: name - The name for this Path Table Record. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record already initialized') # Zero for the parent dir num is bogus, but that will get fixed later. self._new(name, 0)
python
def new_dir(self, name): # type: (bytes) -> None ''' A method to create a new Path Table Record. Parameters: name - The name for this Path Table Record. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record already initialized') # Zero for the parent dir num is bogus, but that will get fixed later. self._new(name, 0)
[ "def", "new_dir", "(", "self", ",", "name", ")", ":", "# type: (bytes) -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Path Table Record already initialized'", ")", "# Zero for the parent dir num is bogus, b...
A method to create a new Path Table Record. Parameters: name - The name for this Path Table Record. Returns: Nothing.
[ "A", "method", "to", "create", "a", "new", "Path", "Table", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/path_table_record.py#L160-L174
21,550
clalancette/pycdlib
pycdlib/path_table_record.py
PathTableRecord.update_extent_location
def update_extent_location(self, extent_loc): # type: (int) -> None ''' A method to update the extent location for this Path Table Record. Parameters: extent_loc - The new extent location. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized') self.extent_location = extent_loc
python
def update_extent_location(self, extent_loc): # type: (int) -> None ''' A method to update the extent location for this Path Table Record. Parameters: extent_loc - The new extent location. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized') self.extent_location = extent_loc
[ "def", "update_extent_location", "(", "self", ",", "extent_loc", ")", ":", "# type: (int) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Path Table Record not yet initialized'", ")", "self", ".",...
A method to update the extent location for this Path Table Record. Parameters: extent_loc - The new extent location. Returns: Nothing.
[ "A", "method", "to", "update", "the", "extent", "location", "for", "this", "Path", "Table", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/path_table_record.py#L176-L189
21,551
clalancette/pycdlib
pycdlib/path_table_record.py
PathTableRecord.update_parent_directory_number
def update_parent_directory_number(self, parent_dir_num): # type: (int) -> None ''' A method to update the parent directory number for this Path Table Record from the directory record. Parameters: parent_dir_num - The new parent directory number to assign to this PTR. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized') self.parent_directory_num = parent_dir_num
python
def update_parent_directory_number(self, parent_dir_num): # type: (int) -> None ''' A method to update the parent directory number for this Path Table Record from the directory record. Parameters: parent_dir_num - The new parent directory number to assign to this PTR. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized') self.parent_directory_num = parent_dir_num
[ "def", "update_parent_directory_number", "(", "self", ",", "parent_dir_num", ")", ":", "# type: (int) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Path Table Record not yet initialized'", ")", "s...
A method to update the parent directory number for this Path Table Record from the directory record. Parameters: parent_dir_num - The new parent directory number to assign to this PTR. Returns: Nothing.
[ "A", "method", "to", "update", "the", "parent", "directory", "number", "for", "this", "Path", "Table", "Record", "from", "the", "directory", "record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/path_table_record.py#L191-L204
21,552
clalancette/pycdlib
pycdlib/path_table_record.py
PathTableRecord.equal_to_be
def equal_to_be(self, be_record): # type: (PathTableRecord) -> bool ''' A method to compare a little-endian path table record to its big-endian counterpart. This is used to ensure that the ISO is sane. Parameters: be_record - The big-endian object to compare with the little-endian object. Returns: True if this record is equal to the big-endian record passed in, False otherwise. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This Path Table Record is not yet initialized') if be_record.len_di != self.len_di or \ be_record.xattr_length != self.xattr_length or \ utils.swab_32bit(be_record.extent_location) != self.extent_location or \ utils.swab_16bit(be_record.parent_directory_num) != self.parent_directory_num or \ be_record.directory_identifier != self.directory_identifier: return False return True
python
def equal_to_be(self, be_record): # type: (PathTableRecord) -> bool ''' A method to compare a little-endian path table record to its big-endian counterpart. This is used to ensure that the ISO is sane. Parameters: be_record - The big-endian object to compare with the little-endian object. Returns: True if this record is equal to the big-endian record passed in, False otherwise. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This Path Table Record is not yet initialized') if be_record.len_di != self.len_di or \ be_record.xattr_length != self.xattr_length or \ utils.swab_32bit(be_record.extent_location) != self.extent_location or \ utils.swab_16bit(be_record.parent_directory_num) != self.parent_directory_num or \ be_record.directory_identifier != self.directory_identifier: return False return True
[ "def", "equal_to_be", "(", "self", ",", "be_record", ")", ":", "# type: (PathTableRecord) -> bool", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'This Path Table Record is not yet initialized'", ")", "if", ...
A method to compare a little-endian path table record to its big-endian counterpart. This is used to ensure that the ISO is sane. Parameters: be_record - The big-endian object to compare with the little-endian object. Returns: True if this record is equal to the big-endian record passed in, False otherwise.
[ "A", "method", "to", "compare", "a", "little", "-", "endian", "path", "table", "record", "to", "its", "big", "-", "endian", "counterpart", ".", "This", "is", "used", "to", "ensure", "that", "the", "ISO", "is", "sane", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/path_table_record.py#L206-L228
21,553
clalancette/pycdlib
pycdlib/utils.py
copy_data
def copy_data(data_length, blocksize, infp, outfp): # type: (int, int, BinaryIO, BinaryIO) -> None ''' A utility function to copy data from the input file object to the output file object. This function will use the most efficient copy method available, which is often sendfile. Parameters: data_length - The amount of data to copy. blocksize - How much data to copy per iteration. infp - The file object to copy data from. outfp - The file object to copy data to. Returns: Nothing. ''' use_sendfile = False if have_sendfile: # Python 3 implements the fileno method for all file-like objects, so # we can't just use the existence of the method to tell whether it is # available. Instead, we try to assign it, and if we fail, then we # assume it is not available. try: x_unused = infp.fileno() # NOQA y_unused = outfp.fileno() # NOQA use_sendfile = True except (AttributeError, io.UnsupportedOperation): pass if use_sendfile: # This is one of those instances where using the file object and the # file descriptor causes problems. The sendfile() call actually updates # the underlying file descriptor, but the file object does not know # about it. To get around this, we instead get the offset, allow # sendfile() to update the offset, then manually seek the file object # to the right location. This ensures that the file object gets updated # properly. in_offset = infp.tell() out_offset = outfp.tell() sendfile(outfp.fileno(), infp.fileno(), in_offset, data_length) infp.seek(in_offset + data_length) outfp.seek(out_offset + data_length) else: left = data_length readsize = blocksize while left > 0: if left < readsize: readsize = left data = infp.read(readsize) # We have seen ISOs in the wild (Tribes Vengeance 1of4.iso) that # lie about the size of their files, causing reads to fail (since # we hit EOF before the supposed end of the file). If we are using # sendfile above, sendfile just silently returns as much data as it # can, with no additional checking. We should do the same here, so # if we got less data than we asked for, abort the loop silently. data_len = len(data) if data_len != readsize: data_len = left outfp.write(data) left -= data_len
python
def copy_data(data_length, blocksize, infp, outfp): # type: (int, int, BinaryIO, BinaryIO) -> None ''' A utility function to copy data from the input file object to the output file object. This function will use the most efficient copy method available, which is often sendfile. Parameters: data_length - The amount of data to copy. blocksize - How much data to copy per iteration. infp - The file object to copy data from. outfp - The file object to copy data to. Returns: Nothing. ''' use_sendfile = False if have_sendfile: # Python 3 implements the fileno method for all file-like objects, so # we can't just use the existence of the method to tell whether it is # available. Instead, we try to assign it, and if we fail, then we # assume it is not available. try: x_unused = infp.fileno() # NOQA y_unused = outfp.fileno() # NOQA use_sendfile = True except (AttributeError, io.UnsupportedOperation): pass if use_sendfile: # This is one of those instances where using the file object and the # file descriptor causes problems. The sendfile() call actually updates # the underlying file descriptor, but the file object does not know # about it. To get around this, we instead get the offset, allow # sendfile() to update the offset, then manually seek the file object # to the right location. This ensures that the file object gets updated # properly. in_offset = infp.tell() out_offset = outfp.tell() sendfile(outfp.fileno(), infp.fileno(), in_offset, data_length) infp.seek(in_offset + data_length) outfp.seek(out_offset + data_length) else: left = data_length readsize = blocksize while left > 0: if left < readsize: readsize = left data = infp.read(readsize) # We have seen ISOs in the wild (Tribes Vengeance 1of4.iso) that # lie about the size of their files, causing reads to fail (since # we hit EOF before the supposed end of the file). If we are using # sendfile above, sendfile just silently returns as much data as it # can, with no additional checking. We should do the same here, so # if we got less data than we asked for, abort the loop silently. data_len = len(data) if data_len != readsize: data_len = left outfp.write(data) left -= data_len
[ "def", "copy_data", "(", "data_length", ",", "blocksize", ",", "infp", ",", "outfp", ")", ":", "# type: (int, int, BinaryIO, BinaryIO) -> None", "use_sendfile", "=", "False", "if", "have_sendfile", ":", "# Python 3 implements the fileno method for all file-like objects, so", ...
A utility function to copy data from the input file object to the output file object. This function will use the most efficient copy method available, which is often sendfile. Parameters: data_length - The amount of data to copy. blocksize - How much data to copy per iteration. infp - The file object to copy data from. outfp - The file object to copy data to. Returns: Nothing.
[ "A", "utility", "function", "to", "copy", "data", "from", "the", "input", "file", "object", "to", "the", "output", "file", "object", ".", "This", "function", "will", "use", "the", "most", "efficient", "copy", "method", "available", "which", "is", "often", ...
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/utils.py#L93-L151
21,554
clalancette/pycdlib
pycdlib/utils.py
encode_space_pad
def encode_space_pad(instr, length, encoding): # type: (bytes, int, str) -> bytes ''' A function to pad out an input string with spaces to the length specified. The space is first encoded into the specified encoding, then appended to the input string until the length is reached. Parameters: instr - The input string to encode and pad. length - The length to pad the input string to. encoding - The encoding to use. Returns: The input string encoded in the encoding and padded with encoded spaces. ''' output = instr.decode('utf-8').encode(encoding) if len(output) > length: raise pycdlibexception.PyCdlibInvalidInput('Input string too long!') encoded_space = ' '.encode(encoding) left = length - len(output) while left > 0: output += encoded_space left -= len(encoded_space) if left < 0: output = output[:left] return output
python
def encode_space_pad(instr, length, encoding): # type: (bytes, int, str) -> bytes ''' A function to pad out an input string with spaces to the length specified. The space is first encoded into the specified encoding, then appended to the input string until the length is reached. Parameters: instr - The input string to encode and pad. length - The length to pad the input string to. encoding - The encoding to use. Returns: The input string encoded in the encoding and padded with encoded spaces. ''' output = instr.decode('utf-8').encode(encoding) if len(output) > length: raise pycdlibexception.PyCdlibInvalidInput('Input string too long!') encoded_space = ' '.encode(encoding) left = length - len(output) while left > 0: output += encoded_space left -= len(encoded_space) if left < 0: output = output[:left] return output
[ "def", "encode_space_pad", "(", "instr", ",", "length", ",", "encoding", ")", ":", "# type: (bytes, int, str) -> bytes", "output", "=", "instr", ".", "decode", "(", "'utf-8'", ")", ".", "encode", "(", "encoding", ")", "if", "len", "(", "output", ")", ">", ...
A function to pad out an input string with spaces to the length specified. The space is first encoded into the specified encoding, then appended to the input string until the length is reached. Parameters: instr - The input string to encode and pad. length - The length to pad the input string to. encoding - The encoding to use. Returns: The input string encoded in the encoding and padded with encoded spaces.
[ "A", "function", "to", "pad", "out", "an", "input", "string", "with", "spaces", "to", "the", "length", "specified", ".", "The", "space", "is", "first", "encoded", "into", "the", "specified", "encoding", "then", "appended", "to", "the", "input", "string", "...
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/utils.py#L154-L182
21,555
clalancette/pycdlib
pycdlib/utils.py
gmtoffset_from_tm
def gmtoffset_from_tm(tm, local): # type: (float, time.struct_time) -> int ''' A function to compute the GMT offset from the time in seconds since the epoch and the local time object. Parameters: tm - The time in seconds since the epoch. local - The struct_time object representing the local time. Returns: The gmtoffset. ''' gmtime = time.gmtime(tm) tmpyear = gmtime.tm_year - local.tm_year tmpyday = gmtime.tm_yday - local.tm_yday tmphour = gmtime.tm_hour - local.tm_hour tmpmin = gmtime.tm_min - local.tm_min if tmpyday < 0: tmpyday = -1 else: if tmpyear > 0: tmpyday = 1 return -(tmpmin + 60 * (tmphour + 24 * tmpyday)) // 15
python
def gmtoffset_from_tm(tm, local): # type: (float, time.struct_time) -> int ''' A function to compute the GMT offset from the time in seconds since the epoch and the local time object. Parameters: tm - The time in seconds since the epoch. local - The struct_time object representing the local time. Returns: The gmtoffset. ''' gmtime = time.gmtime(tm) tmpyear = gmtime.tm_year - local.tm_year tmpyday = gmtime.tm_yday - local.tm_yday tmphour = gmtime.tm_hour - local.tm_hour tmpmin = gmtime.tm_min - local.tm_min if tmpyday < 0: tmpyday = -1 else: if tmpyear > 0: tmpyday = 1 return -(tmpmin + 60 * (tmphour + 24 * tmpyday)) // 15
[ "def", "gmtoffset_from_tm", "(", "tm", ",", "local", ")", ":", "# type: (float, time.struct_time) -> int", "gmtime", "=", "time", ".", "gmtime", "(", "tm", ")", "tmpyear", "=", "gmtime", ".", "tm_year", "-", "local", ".", "tm_year", "tmpyday", "=", "gmtime", ...
A function to compute the GMT offset from the time in seconds since the epoch and the local time object. Parameters: tm - The time in seconds since the epoch. local - The struct_time object representing the local time. Returns: The gmtoffset.
[ "A", "function", "to", "compute", "the", "GMT", "offset", "from", "the", "time", "in", "seconds", "since", "the", "epoch", "and", "the", "local", "time", "object", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/utils.py#L223-L246
21,556
clalancette/pycdlib
pycdlib/utils.py
zero_pad
def zero_pad(fp, data_size, pad_size): # type: (BinaryIO, int, int) -> None ''' A function to write padding out from data_size up to pad_size efficiently. Parameters: fp - The file object to use to write padding out to. data_size - The current size of the data. pad_size - The boundary size of data to pad out to. Returns: Nothing. ''' padbytes = pad_size - (data_size % pad_size) if padbytes == pad_size: # Nothing to pad, get out. return fp.seek(padbytes - 1, os.SEEK_CUR) fp.write(b'\x00')
python
def zero_pad(fp, data_size, pad_size): # type: (BinaryIO, int, int) -> None ''' A function to write padding out from data_size up to pad_size efficiently. Parameters: fp - The file object to use to write padding out to. data_size - The current size of the data. pad_size - The boundary size of data to pad out to. Returns: Nothing. ''' padbytes = pad_size - (data_size % pad_size) if padbytes == pad_size: # Nothing to pad, get out. return fp.seek(padbytes - 1, os.SEEK_CUR) fp.write(b'\x00')
[ "def", "zero_pad", "(", "fp", ",", "data_size", ",", "pad_size", ")", ":", "# type: (BinaryIO, int, int) -> None", "padbytes", "=", "pad_size", "-", "(", "data_size", "%", "pad_size", ")", "if", "padbytes", "==", "pad_size", ":", "# Nothing to pad, get out.", "ret...
A function to write padding out from data_size up to pad_size efficiently. Parameters: fp - The file object to use to write padding out to. data_size - The current size of the data. pad_size - The boundary size of data to pad out to. Returns: Nothing.
[ "A", "function", "to", "write", "padding", "out", "from", "data_size", "up", "to", "pad_size", "efficiently", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/utils.py#L249-L268
21,557
clalancette/pycdlib
pycdlib/utils.py
file_object_supports_binary
def file_object_supports_binary(fp): # type: (BinaryIO) -> bool ''' A function to check whether a file-like object supports binary mode. Parameters: fp - The file-like object to check for binary mode support. Returns: True if the file-like object supports binary mode, False otherwise. ''' if hasattr(fp, 'mode'): return 'b' in fp.mode # Python 3 if sys.version_info >= (3, 0): return isinstance(fp, (io.RawIOBase, io.BufferedIOBase)) # Python 2 return isinstance(fp, (cStringIO.OutputType, cStringIO.InputType, io.RawIOBase, io.BufferedIOBase))
python
def file_object_supports_binary(fp): # type: (BinaryIO) -> bool ''' A function to check whether a file-like object supports binary mode. Parameters: fp - The file-like object to check for binary mode support. Returns: True if the file-like object supports binary mode, False otherwise. ''' if hasattr(fp, 'mode'): return 'b' in fp.mode # Python 3 if sys.version_info >= (3, 0): return isinstance(fp, (io.RawIOBase, io.BufferedIOBase)) # Python 2 return isinstance(fp, (cStringIO.OutputType, cStringIO.InputType, io.RawIOBase, io.BufferedIOBase))
[ "def", "file_object_supports_binary", "(", "fp", ")", ":", "# type: (BinaryIO) -> bool", "if", "hasattr", "(", "fp", ",", "'mode'", ")", ":", "return", "'b'", "in", "fp", ".", "mode", "# Python 3", "if", "sys", ".", "version_info", ">=", "(", "3", ",", "0"...
A function to check whether a file-like object supports binary mode. Parameters: fp - The file-like object to check for binary mode support. Returns: True if the file-like object supports binary mode, False otherwise.
[ "A", "function", "to", "check", "whether", "a", "file", "-", "like", "object", "supports", "binary", "mode", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/utils.py#L304-L322
21,558
clalancette/pycdlib
pycdlib/isohybrid.py
IsoHybrid.parse
def parse(self, instr): # type: (bytes) -> bool ''' A method to parse ISO hybridization info out of an existing ISO. Parameters: instr - The data for the ISO hybridization. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is already initialized') if len(instr) != 512: raise pycdlibexception.PyCdlibInvalidISO('Invalid size of the instr') if instr[0:32] == self.ORIG_HEADER: self.header = self.ORIG_HEADER elif instr[0:32] == self.MAC_AFP: self.header = self.MAC_AFP else: # If we didn't see anything that we expected, then this is not an # IsoHybrid ISO, so just quietly return False return False (self.mbr, self.rba, unused1, self.mbr_id, unused2) = struct.unpack_from(self.FMT, instr[:32 + struct.calcsize(self.FMT)], 32) if unused1 != 0: raise pycdlibexception.PyCdlibInvalidISO('Invalid IsoHybrid section') if unused2 != 0: raise pycdlibexception.PyCdlibInvalidISO('Invalid IsoHybrid section') offset = 32 + struct.calcsize(self.FMT) for i in range(1, 5): if bytes(bytearray([instr[offset]])) == b'\x80': self.part_entry = i (const_unused, self.bhead, self.bsect, self.bcyle, self.ptype, self.ehead, self.esect, self.ecyle, self.part_offset, self.psize) = struct.unpack_from('=BBBBBBBBLL', instr[:offset + 16], offset) break offset += 16 else: raise pycdlibexception.PyCdlibInvalidISO('No valid partition found in IsoHybrid!') if bytes(bytearray([instr[-2]])) != b'\x55' or bytes(bytearray([instr[-1]])) != b'\xaa': raise pycdlibexception.PyCdlibInvalidISO('Invalid tail on isohybrid section') self.geometry_heads = self.ehead + 1 # FIXME: I can't see any way to compute the number of sectors from the # available information. For now, we just hard-code this at 32 and # hope for the best. self.geometry_sectors = 32 self._initialized = True return True
python
def parse(self, instr): # type: (bytes) -> bool ''' A method to parse ISO hybridization info out of an existing ISO. Parameters: instr - The data for the ISO hybridization. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is already initialized') if len(instr) != 512: raise pycdlibexception.PyCdlibInvalidISO('Invalid size of the instr') if instr[0:32] == self.ORIG_HEADER: self.header = self.ORIG_HEADER elif instr[0:32] == self.MAC_AFP: self.header = self.MAC_AFP else: # If we didn't see anything that we expected, then this is not an # IsoHybrid ISO, so just quietly return False return False (self.mbr, self.rba, unused1, self.mbr_id, unused2) = struct.unpack_from(self.FMT, instr[:32 + struct.calcsize(self.FMT)], 32) if unused1 != 0: raise pycdlibexception.PyCdlibInvalidISO('Invalid IsoHybrid section') if unused2 != 0: raise pycdlibexception.PyCdlibInvalidISO('Invalid IsoHybrid section') offset = 32 + struct.calcsize(self.FMT) for i in range(1, 5): if bytes(bytearray([instr[offset]])) == b'\x80': self.part_entry = i (const_unused, self.bhead, self.bsect, self.bcyle, self.ptype, self.ehead, self.esect, self.ecyle, self.part_offset, self.psize) = struct.unpack_from('=BBBBBBBBLL', instr[:offset + 16], offset) break offset += 16 else: raise pycdlibexception.PyCdlibInvalidISO('No valid partition found in IsoHybrid!') if bytes(bytearray([instr[-2]])) != b'\x55' or bytes(bytearray([instr[-1]])) != b'\xaa': raise pycdlibexception.PyCdlibInvalidISO('Invalid tail on isohybrid section') self.geometry_heads = self.ehead + 1 # FIXME: I can't see any way to compute the number of sectors from the # available information. For now, we just hard-code this at 32 and # hope for the best. self.geometry_sectors = 32 self._initialized = True return True
[ "def", "parse", "(", "self", ",", "instr", ")", ":", "# type: (bytes) -> bool", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'This IsoHybrid object is already initialized'", ")", "if", "len", "(", "instr", "...
A method to parse ISO hybridization info out of an existing ISO. Parameters: instr - The data for the ISO hybridization. Returns: Nothing.
[ "A", "method", "to", "parse", "ISO", "hybridization", "info", "out", "of", "an", "existing", "ISO", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/isohybrid.py#L50-L107
21,559
clalancette/pycdlib
pycdlib/isohybrid.py
IsoHybrid.new
def new(self, mac, part_entry, mbr_id, part_offset, geometry_sectors, geometry_heads, part_type): # type: (bool, int, Optional[int], int, int, int, int) -> None ''' A method to add ISO hybridization to an ISO. Parameters: mac - Whether this ISO should be made bootable for the Macintosh. part_entry - The partition entry for the hybridization. mbr_id - The mbr_id to use for the hybridization. part_offset - The partition offset to use for the hybridization. geometry_sectors - The number of sectors to use for the hybridization. geometry_heads - The number of heads to use for the hybridization. part_type - The partition type for the hybridization. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is already initialized') if mac: self.header = self.MAC_AFP else: self.header = self.ORIG_HEADER isohybrid_data_hd0 = b'\x33\xed\xfa\x8e\xd5\xbc\x00\x7c\xfb\xfc\x66\x31\xdb\x66\x31\xc9\x66\x53\x66\x51\x06\x57\x8e\xdd\x8e\xc5\x52\xbe\x00\x7c\xbf\x00\x06\xb9\x00\x01\xf3\xa5\xea\x4b\x06\x00\x00\x52\xb4\x41\xbb\xaa\x55\x31\xc9\x30\xf6\xf9\xcd\x13\x72\x16\x81\xfb\x55\xaa\x75\x10\x83\xe1\x01\x74\x0b\x66\xc7\x06\xf1\x06\xb4\x42\xeb\x15\xeb\x00\x5a\x51\xb4\x08\xcd\x13\x83\xe1\x3f\x5b\x51\x0f\xb6\xc6\x40\x50\xf7\xe1\x53\x52\x50\xbb\x00\x7c\xb9\x04\x00\x66\xa1\xb0\x07\xe8\x44\x00\x0f\x82\x80\x00\x66\x40\x80\xc7\x02\xe2\xf2\x66\x81\x3e\x40\x7c\xfb\xc0\x78\x70\x75\x09\xfa\xbc\xec\x7b\xea\x44\x7c\x00\x00\xe8\x83\x00\x69\x73\x6f\x6c\x69\x6e\x75\x78\x2e\x62\x69\x6e\x20\x6d\x69\x73\x73\x69\x6e\x67\x20\x6f\x72\x20\x63\x6f\x72\x72\x75\x70\x74\x2e\x0d\x0a\x66\x60\x66\x31\xd2\x66\x03\x06\xf8\x7b\x66\x13\x16\xfc\x7b\x66\x52\x66\x50\x06\x53\x6a\x01\x6a\x10\x89\xe6\x66\xf7\x36\xe8\x7b\xc0\xe4\x06\x88\xe1\x88\xc5\x92\xf6\x36\xee\x7b\x88\xc6\x08\xe1\x41\xb8\x01\x02\x8a\x16\xf2\x7b\xcd\x13\x8d\x64\x10\x66\x61\xc3\xe8\x1e\x00\x4f\x70\x65\x72\x61\x74\x69\x6e\x67\x20\x73\x79\x73\x74\x65\x6d\x20\x6c\x6f\x61\x64\x20\x65\x72\x72\x6f\x72\x2e\x0d\x0a\x5e\xac\xb4\x0e\x8a\x3e\x62\x04\xb3\x07\xcd\x10\x3c\x0a\x75\xf1\xcd\x18\xf4\xeb\xfd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' self.mbr = isohybrid_data_hd0 self.rba = 0 # This will be set later self.mbr_id = mbr_id if self.mbr_id is None: self.mbr_id = random.getrandbits(32) self.part_entry = part_entry self.bhead = (part_offset // geometry_sectors) % geometry_heads self.bsect = (part_offset % geometry_sectors) + 1 self.bcyle = part_offset // (geometry_heads * geometry_sectors) self.bsect += (self.bcyle & 0x300) >> 2 self.bcyle &= 0xff self.ptype = part_type self.ehead = geometry_heads - 1 self.part_offset = part_offset self.geometry_heads = geometry_heads self.geometry_sectors = geometry_sectors self._initialized = True
python
def new(self, mac, part_entry, mbr_id, part_offset, geometry_sectors, geometry_heads, part_type): # type: (bool, int, Optional[int], int, int, int, int) -> None ''' A method to add ISO hybridization to an ISO. Parameters: mac - Whether this ISO should be made bootable for the Macintosh. part_entry - The partition entry for the hybridization. mbr_id - The mbr_id to use for the hybridization. part_offset - The partition offset to use for the hybridization. geometry_sectors - The number of sectors to use for the hybridization. geometry_heads - The number of heads to use for the hybridization. part_type - The partition type for the hybridization. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is already initialized') if mac: self.header = self.MAC_AFP else: self.header = self.ORIG_HEADER isohybrid_data_hd0 = b'\x33\xed\xfa\x8e\xd5\xbc\x00\x7c\xfb\xfc\x66\x31\xdb\x66\x31\xc9\x66\x53\x66\x51\x06\x57\x8e\xdd\x8e\xc5\x52\xbe\x00\x7c\xbf\x00\x06\xb9\x00\x01\xf3\xa5\xea\x4b\x06\x00\x00\x52\xb4\x41\xbb\xaa\x55\x31\xc9\x30\xf6\xf9\xcd\x13\x72\x16\x81\xfb\x55\xaa\x75\x10\x83\xe1\x01\x74\x0b\x66\xc7\x06\xf1\x06\xb4\x42\xeb\x15\xeb\x00\x5a\x51\xb4\x08\xcd\x13\x83\xe1\x3f\x5b\x51\x0f\xb6\xc6\x40\x50\xf7\xe1\x53\x52\x50\xbb\x00\x7c\xb9\x04\x00\x66\xa1\xb0\x07\xe8\x44\x00\x0f\x82\x80\x00\x66\x40\x80\xc7\x02\xe2\xf2\x66\x81\x3e\x40\x7c\xfb\xc0\x78\x70\x75\x09\xfa\xbc\xec\x7b\xea\x44\x7c\x00\x00\xe8\x83\x00\x69\x73\x6f\x6c\x69\x6e\x75\x78\x2e\x62\x69\x6e\x20\x6d\x69\x73\x73\x69\x6e\x67\x20\x6f\x72\x20\x63\x6f\x72\x72\x75\x70\x74\x2e\x0d\x0a\x66\x60\x66\x31\xd2\x66\x03\x06\xf8\x7b\x66\x13\x16\xfc\x7b\x66\x52\x66\x50\x06\x53\x6a\x01\x6a\x10\x89\xe6\x66\xf7\x36\xe8\x7b\xc0\xe4\x06\x88\xe1\x88\xc5\x92\xf6\x36\xee\x7b\x88\xc6\x08\xe1\x41\xb8\x01\x02\x8a\x16\xf2\x7b\xcd\x13\x8d\x64\x10\x66\x61\xc3\xe8\x1e\x00\x4f\x70\x65\x72\x61\x74\x69\x6e\x67\x20\x73\x79\x73\x74\x65\x6d\x20\x6c\x6f\x61\x64\x20\x65\x72\x72\x6f\x72\x2e\x0d\x0a\x5e\xac\xb4\x0e\x8a\x3e\x62\x04\xb3\x07\xcd\x10\x3c\x0a\x75\xf1\xcd\x18\xf4\xeb\xfd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' self.mbr = isohybrid_data_hd0 self.rba = 0 # This will be set later self.mbr_id = mbr_id if self.mbr_id is None: self.mbr_id = random.getrandbits(32) self.part_entry = part_entry self.bhead = (part_offset // geometry_sectors) % geometry_heads self.bsect = (part_offset % geometry_sectors) + 1 self.bcyle = part_offset // (geometry_heads * geometry_sectors) self.bsect += (self.bcyle & 0x300) >> 2 self.bcyle &= 0xff self.ptype = part_type self.ehead = geometry_heads - 1 self.part_offset = part_offset self.geometry_heads = geometry_heads self.geometry_sectors = geometry_sectors self._initialized = True
[ "def", "new", "(", "self", ",", "mac", ",", "part_entry", ",", "mbr_id", ",", "part_offset", ",", "geometry_sectors", ",", "geometry_heads", ",", "part_type", ")", ":", "# type: (bool, int, Optional[int], int, int, int, int) -> None", "if", "self", ".", "_initialized"...
A method to add ISO hybridization to an ISO. Parameters: mac - Whether this ISO should be made bootable for the Macintosh. part_entry - The partition entry for the hybridization. mbr_id - The mbr_id to use for the hybridization. part_offset - The partition offset to use for the hybridization. geometry_sectors - The number of sectors to use for the hybridization. geometry_heads - The number of heads to use for the hybridization. part_type - The partition type for the hybridization. Returns: Nothing.
[ "A", "method", "to", "add", "ISO", "hybridization", "to", "an", "ISO", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/isohybrid.py#L109-L154
21,560
clalancette/pycdlib
pycdlib/isohybrid.py
IsoHybrid._calc_cc
def _calc_cc(self, iso_size): # type: (int) -> Tuple[int, int] ''' A method to calculate the 'cc' and the 'padding' values for this hybridization. Parameters: iso_size - The size of the ISO, excluding the hybridization. Returns: A tuple containing the cc value and the padding. ''' cylsize = self.geometry_heads * self.geometry_sectors * 512 frac = iso_size % cylsize padding = 0 if frac > 0: padding = cylsize - frac cc = (iso_size + padding) // cylsize if cc > 1024: cc = 1024 return (cc, padding)
python
def _calc_cc(self, iso_size): # type: (int) -> Tuple[int, int] ''' A method to calculate the 'cc' and the 'padding' values for this hybridization. Parameters: iso_size - The size of the ISO, excluding the hybridization. Returns: A tuple containing the cc value and the padding. ''' cylsize = self.geometry_heads * self.geometry_sectors * 512 frac = iso_size % cylsize padding = 0 if frac > 0: padding = cylsize - frac cc = (iso_size + padding) // cylsize if cc > 1024: cc = 1024 return (cc, padding)
[ "def", "_calc_cc", "(", "self", ",", "iso_size", ")", ":", "# type: (int) -> Tuple[int, int]", "cylsize", "=", "self", ".", "geometry_heads", "*", "self", ".", "geometry_sectors", "*", "512", "frac", "=", "iso_size", "%", "cylsize", "padding", "=", "0", "if", ...
A method to calculate the 'cc' and the 'padding' values for this hybridization. Parameters: iso_size - The size of the ISO, excluding the hybridization. Returns: A tuple containing the cc value and the padding.
[ "A", "method", "to", "calculate", "the", "cc", "and", "the", "padding", "values", "for", "this", "hybridization", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/isohybrid.py#L156-L176
21,561
clalancette/pycdlib
pycdlib/isohybrid.py
IsoHybrid.record
def record(self, iso_size): # type: (int) -> bytes ''' A method to generate a string containing the ISO hybridization. Parameters: iso_size - The size of the ISO, excluding the hybridization. Returns: A string containing the ISO hybridization. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is not yet initialized') outlist = [struct.pack('=32s400sLLLH', self.header, self.mbr, self.rba, 0, self.mbr_id, 0)] for i in range(1, 5): if i == self.part_entry: cc, padding_unused = self._calc_cc(iso_size) esect = self.geometry_sectors + (((cc - 1) & 0x300) >> 2) ecyle = (cc - 1) & 0xff psize = cc * self.geometry_heads * self.geometry_sectors - self.part_offset outlist.append(struct.pack('=BBBBBBBBLL', 0x80, self.bhead, self.bsect, self.bcyle, self.ptype, self.ehead, esect, ecyle, self.part_offset, psize)) else: outlist.append(b'\x00' * 16) outlist.append(b'\x55\xaa') return b''.join(outlist)
python
def record(self, iso_size): # type: (int) -> bytes ''' A method to generate a string containing the ISO hybridization. Parameters: iso_size - The size of the ISO, excluding the hybridization. Returns: A string containing the ISO hybridization. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is not yet initialized') outlist = [struct.pack('=32s400sLLLH', self.header, self.mbr, self.rba, 0, self.mbr_id, 0)] for i in range(1, 5): if i == self.part_entry: cc, padding_unused = self._calc_cc(iso_size) esect = self.geometry_sectors + (((cc - 1) & 0x300) >> 2) ecyle = (cc - 1) & 0xff psize = cc * self.geometry_heads * self.geometry_sectors - self.part_offset outlist.append(struct.pack('=BBBBBBBBLL', 0x80, self.bhead, self.bsect, self.bcyle, self.ptype, self.ehead, esect, ecyle, self.part_offset, psize)) else: outlist.append(b'\x00' * 16) outlist.append(b'\x55\xaa') return b''.join(outlist)
[ "def", "record", "(", "self", ",", "iso_size", ")", ":", "# type: (int) -> bytes", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'This IsoHybrid object is not yet initialized'", ")", "outlist", "=", "[",...
A method to generate a string containing the ISO hybridization. Parameters: iso_size - The size of the ISO, excluding the hybridization. Returns: A string containing the ISO hybridization.
[ "A", "method", "to", "generate", "a", "string", "containing", "the", "ISO", "hybridization", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/isohybrid.py#L178-L208
21,562
clalancette/pycdlib
pycdlib/isohybrid.py
IsoHybrid.record_padding
def record_padding(self, iso_size): # type: (int) -> bytes ''' A method to record padding for the ISO hybridization. Parameters: iso_size - The size of the ISO, excluding the hybridization. Returns: A string of zeros the right size to pad the ISO. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is not yet initialized') return b'\x00' * self._calc_cc(iso_size)[1]
python
def record_padding(self, iso_size): # type: (int) -> bytes ''' A method to record padding for the ISO hybridization. Parameters: iso_size - The size of the ISO, excluding the hybridization. Returns: A string of zeros the right size to pad the ISO. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is not yet initialized') return b'\x00' * self._calc_cc(iso_size)[1]
[ "def", "record_padding", "(", "self", ",", "iso_size", ")", ":", "# type: (int) -> bytes", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'This IsoHybrid object is not yet initialized'", ")", "return", "b'\...
A method to record padding for the ISO hybridization. Parameters: iso_size - The size of the ISO, excluding the hybridization. Returns: A string of zeros the right size to pad the ISO.
[ "A", "method", "to", "record", "padding", "for", "the", "ISO", "hybridization", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/isohybrid.py#L210-L223
21,563
clalancette/pycdlib
pycdlib/isohybrid.py
IsoHybrid.update_rba
def update_rba(self, current_extent): # type: (int) -> None ''' A method to update the current rba for the ISO hybridization. Parameters: current_extent - The new extent to set the RBA to. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is not yet initialized') self.rba = current_extent
python
def update_rba(self, current_extent): # type: (int) -> None ''' A method to update the current rba for the ISO hybridization. Parameters: current_extent - The new extent to set the RBA to. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This IsoHybrid object is not yet initialized') self.rba = current_extent
[ "def", "update_rba", "(", "self", ",", "current_extent", ")", ":", "# type: (int) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'This IsoHybrid object is not yet initialized'", ")", "self", ".", ...
A method to update the current rba for the ISO hybridization. Parameters: current_extent - The new extent to set the RBA to. Returns: Nothing.
[ "A", "method", "to", "update", "the", "current", "rba", "for", "the", "ISO", "hybridization", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/isohybrid.py#L225-L238
21,564
clalancette/pycdlib
pycdlib/dr.py
XARecord.parse
def parse(self, xastr): # type: (bytes) -> None ''' Parse an Extended Attribute Record out of a string. Parameters: xastr - The string to parse. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This XARecord is already initialized!') (self._group_id, self._user_id, self._attributes, signature, self._filenum, unused) = struct.unpack_from(self.FMT, xastr, 0) if signature != b'XA': raise pycdlibexception.PyCdlibInvalidISO('Invalid signature on the XARecord!') if unused != b'\x00\x00\x00\x00\x00': raise pycdlibexception.PyCdlibInvalidISO('Unused fields should be 0') self._initialized = True
python
def parse(self, xastr): # type: (bytes) -> None ''' Parse an Extended Attribute Record out of a string. Parameters: xastr - The string to parse. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This XARecord is already initialized!') (self._group_id, self._user_id, self._attributes, signature, self._filenum, unused) = struct.unpack_from(self.FMT, xastr, 0) if signature != b'XA': raise pycdlibexception.PyCdlibInvalidISO('Invalid signature on the XARecord!') if unused != b'\x00\x00\x00\x00\x00': raise pycdlibexception.PyCdlibInvalidISO('Unused fields should be 0') self._initialized = True
[ "def", "parse", "(", "self", ",", "xastr", ")", ":", "# type: (bytes) -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'This XARecord is already initialized!'", ")", "(", "self", ".", "_group_id", ",",...
Parse an Extended Attribute Record out of a string. Parameters: xastr - The string to parse. Returns: Nothing.
[ "Parse", "an", "Extended", "Attribute", "Record", "out", "of", "a", "string", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L54-L76
21,565
clalancette/pycdlib
pycdlib/dr.py
XARecord.new
def new(self): # type: () -> None ''' Create a new Extended Attribute Record. Parameters: None. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This XARecord is already initialized!') # FIXME: we should allow the user to set these self._group_id = 0 self._user_id = 0 self._attributes = 0 self._filenum = 0 self._initialized = True
python
def new(self): # type: () -> None ''' Create a new Extended Attribute Record. Parameters: None. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This XARecord is already initialized!') # FIXME: we should allow the user to set these self._group_id = 0 self._user_id = 0 self._attributes = 0 self._filenum = 0 self._initialized = True
[ "def", "new", "(", "self", ")", ":", "# type: () -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'This XARecord is already initialized!'", ")", "# FIXME: we should allow the user to set these", "self", ".", ...
Create a new Extended Attribute Record. Parameters: None. Returns: Nothing.
[ "Create", "a", "new", "Extended", "Attribute", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L78-L96
21,566
clalancette/pycdlib
pycdlib/dr.py
XARecord.record
def record(self): # type: () -> bytes ''' Record this Extended Attribute Record. Parameters: None. Returns: A string representing this Extended Attribute Record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This XARecord is not yet initialized!') return struct.pack(self.FMT, self._group_id, self._user_id, self._attributes, b'XA', self._filenum, b'\x00' * 5)
python
def record(self): # type: () -> bytes ''' Record this Extended Attribute Record. Parameters: None. Returns: A string representing this Extended Attribute Record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('This XARecord is not yet initialized!') return struct.pack(self.FMT, self._group_id, self._user_id, self._attributes, b'XA', self._filenum, b'\x00' * 5)
[ "def", "record", "(", "self", ")", ":", "# type: () -> bytes", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'This XARecord is not yet initialized!'", ")", "return", "struct", ".", "pack", "(", "self",...
Record this Extended Attribute Record. Parameters: None. Returns: A string representing this Extended Attribute Record.
[ "Record", "this", "Extended", "Attribute", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L98-L112
21,567
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord._new
def _new(self, vd, name, parent, seqnum, isdir, length, xa): # type: (headervd.PrimaryOrSupplementaryVD, bytes, Optional[DirectoryRecord], int, bool, int, bool) -> None ''' Internal method to create a new Directory Record. Parameters: vd - The Volume Descriptor this record is part of. name - The name for this directory record. parent - The parent of this directory record. seqnum - The sequence number to associate with this directory record. isdir - Whether this directory record represents a directory. length - The length of the data for this directory record. xa - True if this is an Extended Attribute record. Returns: Nothing. ''' # Adding a new time should really be done when we are going to write # the ISO (in record()). Ecma-119 9.1.5 says: # # 'This field shall indicate the date and the time of the day at which # the information in the Extent described by the Directory Record was # recorded.' # # We create it here just to have something in the field, but we'll # redo the whole thing when we are mastering. self.date = dates.DirectoryRecordDate() self.date.new() if length > 2**32 - 1: raise pycdlibexception.PyCdlibInvalidInput('Maximum supported file length is 2^32-1') self.data_length = length self.file_ident = name self.isdir = isdir self.seqnum = seqnum # For a new directory record entry, there is no original_extent_loc, # so we leave it at None. self.orig_extent_loc = None self.len_fi = len(self.file_ident) self.dr_len = struct.calcsize(self.FMT) + self.len_fi # From Ecma-119, 9.1.6, the file flag bits are: # # Bit 0 - Existence - 0 for existence known, 1 for hidden # Bit 1 - Directory - 0 for file, 1 for directory # Bit 2 - Associated File - 0 for not associated, 1 for associated # Bit 3 - Record - 0=structure not in xattr, 1=structure in xattr # Bit 4 - Protection - 0=no owner and group, 1=owner and group in xattr # Bit 5 - Reserved # Bit 6 - Reserved # Bit 7 - Multi-extent - 0=final directory record, 1=not final directory record self.file_flags = 0 if self.isdir: self.file_flags |= (1 << self.FILE_FLAG_DIRECTORY_BIT) self.file_unit_size = 0 # FIXME: we don't support setting file unit size for now self.interleave_gap_size = 0 # FIXME: we don't support setting interleave gap size for now self.xattr_len = 0 # FIXME: we don't support xattrs for now self.parent = parent if parent is None: # If no parent, then this is the root self.is_root = True if xa: self.xa_record = XARecord() self.xa_record.new() self.dr_len += XARecord.length() self.dr_len += (self.dr_len % 2) if self.is_root: self._printable_name = b'/' elif self.file_ident == b'\x00': self._printable_name = b'.' elif self.file_ident == b'\x01': self._printable_name = b'..' else: self._printable_name = self.file_ident self.vd = vd self._initialized = True
python
def _new(self, vd, name, parent, seqnum, isdir, length, xa): # type: (headervd.PrimaryOrSupplementaryVD, bytes, Optional[DirectoryRecord], int, bool, int, bool) -> None ''' Internal method to create a new Directory Record. Parameters: vd - The Volume Descriptor this record is part of. name - The name for this directory record. parent - The parent of this directory record. seqnum - The sequence number to associate with this directory record. isdir - Whether this directory record represents a directory. length - The length of the data for this directory record. xa - True if this is an Extended Attribute record. Returns: Nothing. ''' # Adding a new time should really be done when we are going to write # the ISO (in record()). Ecma-119 9.1.5 says: # # 'This field shall indicate the date and the time of the day at which # the information in the Extent described by the Directory Record was # recorded.' # # We create it here just to have something in the field, but we'll # redo the whole thing when we are mastering. self.date = dates.DirectoryRecordDate() self.date.new() if length > 2**32 - 1: raise pycdlibexception.PyCdlibInvalidInput('Maximum supported file length is 2^32-1') self.data_length = length self.file_ident = name self.isdir = isdir self.seqnum = seqnum # For a new directory record entry, there is no original_extent_loc, # so we leave it at None. self.orig_extent_loc = None self.len_fi = len(self.file_ident) self.dr_len = struct.calcsize(self.FMT) + self.len_fi # From Ecma-119, 9.1.6, the file flag bits are: # # Bit 0 - Existence - 0 for existence known, 1 for hidden # Bit 1 - Directory - 0 for file, 1 for directory # Bit 2 - Associated File - 0 for not associated, 1 for associated # Bit 3 - Record - 0=structure not in xattr, 1=structure in xattr # Bit 4 - Protection - 0=no owner and group, 1=owner and group in xattr # Bit 5 - Reserved # Bit 6 - Reserved # Bit 7 - Multi-extent - 0=final directory record, 1=not final directory record self.file_flags = 0 if self.isdir: self.file_flags |= (1 << self.FILE_FLAG_DIRECTORY_BIT) self.file_unit_size = 0 # FIXME: we don't support setting file unit size for now self.interleave_gap_size = 0 # FIXME: we don't support setting interleave gap size for now self.xattr_len = 0 # FIXME: we don't support xattrs for now self.parent = parent if parent is None: # If no parent, then this is the root self.is_root = True if xa: self.xa_record = XARecord() self.xa_record.new() self.dr_len += XARecord.length() self.dr_len += (self.dr_len % 2) if self.is_root: self._printable_name = b'/' elif self.file_ident == b'\x00': self._printable_name = b'.' elif self.file_ident == b'\x01': self._printable_name = b'..' else: self._printable_name = self.file_ident self.vd = vd self._initialized = True
[ "def", "_new", "(", "self", ",", "vd", ",", "name", ",", "parent", ",", "seqnum", ",", "isdir", ",", "length", ",", "xa", ")", ":", "# type: (headervd.PrimaryOrSupplementaryVD, bytes, Optional[DirectoryRecord], int, bool, int, bool) -> None", "# Adding a new time should rea...
Internal method to create a new Directory Record. Parameters: vd - The Volume Descriptor this record is part of. name - The name for this directory record. parent - The parent of this directory record. seqnum - The sequence number to associate with this directory record. isdir - Whether this directory record represents a directory. length - The length of the data for this directory record. xa - True if this is an Extended Attribute record. Returns: Nothing.
[ "Internal", "method", "to", "create", "a", "new", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L402-L487
21,568
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.new_symlink
def new_symlink(self, vd, name, parent, rr_target, seqnum, rock_ridge, rr_name, xa): # type: (headervd.PrimaryOrSupplementaryVD, bytes, DirectoryRecord, bytes, int, str, bytes, bool) -> None ''' Create a new symlink Directory Record. This implies that the new record will be Rock Ridge. Parameters: vd - The Volume Descriptor this record is part of. name - The name for this directory record. parent - The parent of this directory record. rr_target - The symlink target for this directory record. seqnum - The sequence number for this directory record. rock_ridge - The version of Rock Ridge to use for this directory record. rr_name - The Rock Ridge name for this directory record. xa - True if this is an Extended Attribute record. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, name, parent, seqnum, False, 0, xa) if rock_ridge: self._rr_new(rock_ridge, rr_name, rr_target, False, False, False, 0o0120555)
python
def new_symlink(self, vd, name, parent, rr_target, seqnum, rock_ridge, rr_name, xa): # type: (headervd.PrimaryOrSupplementaryVD, bytes, DirectoryRecord, bytes, int, str, bytes, bool) -> None ''' Create a new symlink Directory Record. This implies that the new record will be Rock Ridge. Parameters: vd - The Volume Descriptor this record is part of. name - The name for this directory record. parent - The parent of this directory record. rr_target - The symlink target for this directory record. seqnum - The sequence number for this directory record. rock_ridge - The version of Rock Ridge to use for this directory record. rr_name - The Rock Ridge name for this directory record. xa - True if this is an Extended Attribute record. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, name, parent, seqnum, False, 0, xa) if rock_ridge: self._rr_new(rock_ridge, rr_name, rr_target, False, False, False, 0o0120555)
[ "def", "new_symlink", "(", "self", ",", "vd", ",", "name", ",", "parent", ",", "rr_target", ",", "seqnum", ",", "rock_ridge", ",", "rr_name", ",", "xa", ")", ":", "# type: (headervd.PrimaryOrSupplementaryVD, bytes, DirectoryRecord, bytes, int, str, bytes, bool) -> None", ...
Create a new symlink Directory Record. This implies that the new record will be Rock Ridge. Parameters: vd - The Volume Descriptor this record is part of. name - The name for this directory record. parent - The parent of this directory record. rr_target - The symlink target for this directory record. seqnum - The sequence number for this directory record. rock_ridge - The version of Rock Ridge to use for this directory record. rr_name - The Rock Ridge name for this directory record. xa - True if this is an Extended Attribute record. Returns: Nothing.
[ "Create", "a", "new", "symlink", "Directory", "Record", ".", "This", "implies", "that", "the", "new", "record", "will", "be", "Rock", "Ridge", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L489-L514
21,569
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.new_file
def new_file(self, vd, length, isoname, parent, seqnum, rock_ridge, rr_name, xa, file_mode): # type: (headervd.PrimaryOrSupplementaryVD, int, bytes, DirectoryRecord, int, str, bytes, bool, int) -> None ''' Create a new file Directory Record. Parameters: vd - The Volume Descriptor this record is part of. length - The length of the data. isoname - The name for this directory record. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. rr_name - The Rock Ridge name for this directory record. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode for this entry. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, isoname, parent, seqnum, False, length, xa) if rock_ridge: self._rr_new(rock_ridge, rr_name, b'', False, False, False, file_mode)
python
def new_file(self, vd, length, isoname, parent, seqnum, rock_ridge, rr_name, xa, file_mode): # type: (headervd.PrimaryOrSupplementaryVD, int, bytes, DirectoryRecord, int, str, bytes, bool, int) -> None ''' Create a new file Directory Record. Parameters: vd - The Volume Descriptor this record is part of. length - The length of the data. isoname - The name for this directory record. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. rr_name - The Rock Ridge name for this directory record. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode for this entry. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, isoname, parent, seqnum, False, length, xa) if rock_ridge: self._rr_new(rock_ridge, rr_name, b'', False, False, False, file_mode)
[ "def", "new_file", "(", "self", ",", "vd", ",", "length", ",", "isoname", ",", "parent", ",", "seqnum", ",", "rock_ridge", ",", "rr_name", ",", "xa", ",", "file_mode", ")", ":", "# type: (headervd.PrimaryOrSupplementaryVD, int, bytes, DirectoryRecord, int, str, bytes,...
Create a new file Directory Record. Parameters: vd - The Volume Descriptor this record is part of. length - The length of the data. isoname - The name for this directory record. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. rr_name - The Rock Ridge name for this directory record. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode for this entry. Returns: Nothing.
[ "Create", "a", "new", "file", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L516-L541
21,570
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.new_root
def new_root(self, vd, seqnum, log_block_size): # type: (headervd.PrimaryOrSupplementaryVD, int, int) -> None ''' Create a new root Directory Record. Parameters: vd - The Volume Descriptor this record is part of. seqnum - The sequence number for this directory record. log_block_size - The logical block size to use. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, b'\x00', None, seqnum, True, log_block_size, False)
python
def new_root(self, vd, seqnum, log_block_size): # type: (headervd.PrimaryOrSupplementaryVD, int, int) -> None ''' Create a new root Directory Record. Parameters: vd - The Volume Descriptor this record is part of. seqnum - The sequence number for this directory record. log_block_size - The logical block size to use. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, b'\x00', None, seqnum, True, log_block_size, False)
[ "def", "new_root", "(", "self", ",", "vd", ",", "seqnum", ",", "log_block_size", ")", ":", "# type: (headervd.PrimaryOrSupplementaryVD, int, int) -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory...
Create a new root Directory Record. Parameters: vd - The Volume Descriptor this record is part of. seqnum - The sequence number for this directory record. log_block_size - The logical block size to use. Returns: Nothing.
[ "Create", "a", "new", "root", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L543-L558
21,571
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.new_dot
def new_dot(self, vd, parent, seqnum, rock_ridge, log_block_size, xa, file_mode): # type: (headervd.PrimaryOrSupplementaryVD, DirectoryRecord, int, str, int, bool, int) -> None ''' Create a new 'dot' Directory Record. Parameters: vd - The Volume Descriptor this record is part of. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. log_block_size - The logical block size to use. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode to set for this directory. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, b'\x00', parent, seqnum, True, log_block_size, xa) if rock_ridge: self._rr_new(rock_ridge, b'', b'', False, False, False, file_mode)
python
def new_dot(self, vd, parent, seqnum, rock_ridge, log_block_size, xa, file_mode): # type: (headervd.PrimaryOrSupplementaryVD, DirectoryRecord, int, str, int, bool, int) -> None ''' Create a new 'dot' Directory Record. Parameters: vd - The Volume Descriptor this record is part of. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. log_block_size - The logical block size to use. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode to set for this directory. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, b'\x00', parent, seqnum, True, log_block_size, xa) if rock_ridge: self._rr_new(rock_ridge, b'', b'', False, False, False, file_mode)
[ "def", "new_dot", "(", "self", ",", "vd", ",", "parent", ",", "seqnum", ",", "rock_ridge", ",", "log_block_size", ",", "xa", ",", "file_mode", ")", ":", "# type: (headervd.PrimaryOrSupplementaryVD, DirectoryRecord, int, str, int, bool, int) -> None", "if", "self", ".", ...
Create a new 'dot' Directory Record. Parameters: vd - The Volume Descriptor this record is part of. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. log_block_size - The logical block size to use. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode to set for this directory. Returns: Nothing.
[ "Create", "a", "new", "dot", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L560-L582
21,572
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.new_dotdot
def new_dotdot(self, vd, parent, seqnum, rock_ridge, log_block_size, rr_relocated_parent, xa, file_mode): # type: (headervd.PrimaryOrSupplementaryVD, DirectoryRecord, int, str, int, bool, bool, int) -> None ''' Create a new 'dotdot' Directory Record. Parameters: vd - The Volume Descriptor this record is part of. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. log_block_size - The logical block size to use. rr_relocated_parent - True if this is a Rock Ridge relocated parent. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode to set for this directory. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, b'\x01', parent, seqnum, True, log_block_size, xa) if rock_ridge: self._rr_new(rock_ridge, b'', b'', False, False, rr_relocated_parent, file_mode)
python
def new_dotdot(self, vd, parent, seqnum, rock_ridge, log_block_size, rr_relocated_parent, xa, file_mode): # type: (headervd.PrimaryOrSupplementaryVD, DirectoryRecord, int, str, int, bool, bool, int) -> None ''' Create a new 'dotdot' Directory Record. Parameters: vd - The Volume Descriptor this record is part of. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. log_block_size - The logical block size to use. rr_relocated_parent - True if this is a Rock Ridge relocated parent. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode to set for this directory. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, b'\x01', parent, seqnum, True, log_block_size, xa) if rock_ridge: self._rr_new(rock_ridge, b'', b'', False, False, rr_relocated_parent, file_mode)
[ "def", "new_dotdot", "(", "self", ",", "vd", ",", "parent", ",", "seqnum", ",", "rock_ridge", ",", "log_block_size", ",", "rr_relocated_parent", ",", "xa", ",", "file_mode", ")", ":", "# type: (headervd.PrimaryOrSupplementaryVD, DirectoryRecord, int, str, int, bool, bool,...
Create a new 'dotdot' Directory Record. Parameters: vd - The Volume Descriptor this record is part of. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. log_block_size - The logical block size to use. rr_relocated_parent - True if this is a Rock Ridge relocated parent. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode to set for this directory. Returns: Nothing.
[ "Create", "a", "new", "dotdot", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L584-L607
21,573
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.new_dir
def new_dir(self, vd, name, parent, seqnum, rock_ridge, rr_name, log_block_size, rr_relocated_child, rr_relocated, xa, file_mode): # type: (headervd.PrimaryOrSupplementaryVD, bytes, DirectoryRecord, int, str, bytes, int, bool, bool, bool, int) -> None ''' Create a new directory Directory Record. Parameters: vd - The Volume Descriptor this record is part of. name - The name for this directory record. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. rr_name - The Rock Ridge name for this directory record. log_block_size - The logical block size to use. rr_relocated_child - True if this is a Rock Ridge relocated child. rr_relocated - True if this is a Rock Ridge relocated entry. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode to set for this directory. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, name, parent, seqnum, True, log_block_size, xa) if rock_ridge: self._rr_new(rock_ridge, rr_name, b'', rr_relocated_child, rr_relocated, False, file_mode) if rr_relocated_child and self.rock_ridge: # Relocated Rock Ridge entries are not exactly treated as directories, so # fix things up here. self.isdir = False self.file_flags = 0 self.rock_ridge.add_to_file_links()
python
def new_dir(self, vd, name, parent, seqnum, rock_ridge, rr_name, log_block_size, rr_relocated_child, rr_relocated, xa, file_mode): # type: (headervd.PrimaryOrSupplementaryVD, bytes, DirectoryRecord, int, str, bytes, int, bool, bool, bool, int) -> None ''' Create a new directory Directory Record. Parameters: vd - The Volume Descriptor this record is part of. name - The name for this directory record. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. rr_name - The Rock Ridge name for this directory record. log_block_size - The logical block size to use. rr_relocated_child - True if this is a Rock Ridge relocated child. rr_relocated - True if this is a Rock Ridge relocated entry. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode to set for this directory. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record already initialized') self._new(vd, name, parent, seqnum, True, log_block_size, xa) if rock_ridge: self._rr_new(rock_ridge, rr_name, b'', rr_relocated_child, rr_relocated, False, file_mode) if rr_relocated_child and self.rock_ridge: # Relocated Rock Ridge entries are not exactly treated as directories, so # fix things up here. self.isdir = False self.file_flags = 0 self.rock_ridge.add_to_file_links()
[ "def", "new_dir", "(", "self", ",", "vd", ",", "name", ",", "parent", ",", "seqnum", ",", "rock_ridge", ",", "rr_name", ",", "log_block_size", ",", "rr_relocated_child", ",", "rr_relocated", ",", "xa", ",", "file_mode", ")", ":", "# type: (headervd.PrimaryOrSu...
Create a new directory Directory Record. Parameters: vd - The Volume Descriptor this record is part of. name - The name for this directory record. parent - The parent of this directory record. seqnum - The sequence number for this directory record. rock_ridge - Whether to make this a Rock Ridge directory record. rr_name - The Rock Ridge name for this directory record. log_block_size - The logical block size to use. rr_relocated_child - True if this is a Rock Ridge relocated child. rr_relocated - True if this is a Rock Ridge relocated entry. xa - True if this is an Extended Attribute record. file_mode - The POSIX file mode to set for this directory. Returns: Nothing.
[ "Create", "a", "new", "directory", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L609-L642
21,574
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.change_existence
def change_existence(self, is_hidden): # type: (bool) -> None ''' Change the ISO9660 existence flag of this Directory Record. Parameters: is_hidden - True if this Directory Record should be hidden, False otherwise. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') if is_hidden: self.file_flags |= (1 << self.FILE_FLAG_EXISTENCE_BIT) else: self.file_flags &= ~(1 << self.FILE_FLAG_EXISTENCE_BIT)
python
def change_existence(self, is_hidden): # type: (bool) -> None ''' Change the ISO9660 existence flag of this Directory Record. Parameters: is_hidden - True if this Directory Record should be hidden, False otherwise. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') if is_hidden: self.file_flags |= (1 << self.FILE_FLAG_EXISTENCE_BIT) else: self.file_flags &= ~(1 << self.FILE_FLAG_EXISTENCE_BIT)
[ "def", "change_existence", "(", "self", ",", "is_hidden", ")", ":", "# type: (bool) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory Record not yet initialized'", ")", "if", "is_hidden", ...
Change the ISO9660 existence flag of this Directory Record. Parameters: is_hidden - True if this Directory Record should be hidden, False otherwise. Returns: Nothing.
[ "Change", "the", "ISO9660", "existence", "flag", "of", "this", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L644-L660
21,575
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord._recalculate_extents_and_offsets
def _recalculate_extents_and_offsets(self, index, logical_block_size): # type: (int, int) -> Tuple[int, int] ''' Internal method to recalculate the extents and offsets associated with children of this directory record. Parameters: index - The index at which to start the recalculation. logical_block_size - The block size to use for comparisons. Returns: A tuple where the first element is the total number of extents required by the children and where the second element is the offset into the last extent currently being used. ''' if index == 0: dirrecord_offset = 0 num_extents = 1 else: dirrecord_offset = self.children[index - 1].offset_to_here num_extents = self.children[index - 1].extents_to_here for i in range(index, len(self.children)): c = self.children[i] dirrecord_len = c.dr_len if (dirrecord_offset + dirrecord_len) > logical_block_size: num_extents += 1 dirrecord_offset = 0 dirrecord_offset += dirrecord_len c.extents_to_here = num_extents c.offset_to_here = dirrecord_offset c.index_in_parent = i return num_extents, dirrecord_offset
python
def _recalculate_extents_and_offsets(self, index, logical_block_size): # type: (int, int) -> Tuple[int, int] ''' Internal method to recalculate the extents and offsets associated with children of this directory record. Parameters: index - The index at which to start the recalculation. logical_block_size - The block size to use for comparisons. Returns: A tuple where the first element is the total number of extents required by the children and where the second element is the offset into the last extent currently being used. ''' if index == 0: dirrecord_offset = 0 num_extents = 1 else: dirrecord_offset = self.children[index - 1].offset_to_here num_extents = self.children[index - 1].extents_to_here for i in range(index, len(self.children)): c = self.children[i] dirrecord_len = c.dr_len if (dirrecord_offset + dirrecord_len) > logical_block_size: num_extents += 1 dirrecord_offset = 0 dirrecord_offset += dirrecord_len c.extents_to_here = num_extents c.offset_to_here = dirrecord_offset c.index_in_parent = i return num_extents, dirrecord_offset
[ "def", "_recalculate_extents_and_offsets", "(", "self", ",", "index", ",", "logical_block_size", ")", ":", "# type: (int, int) -> Tuple[int, int]", "if", "index", "==", "0", ":", "dirrecord_offset", "=", "0", "num_extents", "=", "1", "else", ":", "dirrecord_offset", ...
Internal method to recalculate the extents and offsets associated with children of this directory record. Parameters: index - The index at which to start the recalculation. logical_block_size - The block size to use for comparisons. Returns: A tuple where the first element is the total number of extents required by the children and where the second element is the offset into the last extent currently being used.
[ "Internal", "method", "to", "recalculate", "the", "extents", "and", "offsets", "associated", "with", "children", "of", "this", "directory", "record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L662-L694
21,576
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord._add_child
def _add_child(self, child, logical_block_size, allow_duplicate, check_overflow): # type: (DirectoryRecord, int, bool, bool) -> bool ''' An internal method to add a child to this object. Note that this is called both during parsing and when adding a new object to the system, so it it shouldn't have any functionality that is not appropriate for both. Parameters: child - The child directory record object to add. logical_block_size - The size of a logical block for this volume descriptor. allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed. check_overflow - Whether to check for overflow; if we are parsing, we don't want to do this. Returns: True if adding this child caused the directory to overflow into another extent, False otherwise. ''' if not self.isdir: raise pycdlibexception.PyCdlibInvalidInput('Trying to add a child to a record that is not a directory') # First ensure that this is not a duplicate. For speed purposes, we # recognize that bisect_left will always choose an index to the *left* # of a duplicate child. Thus, to check for duplicates we only need to # see if the child to be added is a duplicate with the entry that # bisect_left returned. index = bisect.bisect_left(self.children, child) if index != len(self.children) and self.children[index].file_ident == child.file_ident: if not self.children[index].is_associated_file() and not child.is_associated_file(): if not (self.rock_ridge is not None and self.file_identifier() == b'RR_MOVED'): if not allow_duplicate: raise pycdlibexception.PyCdlibInvalidInput('Failed adding duplicate name to parent') else: self.children[index].data_continuation = child index += 1 self.children.insert(index, child) if child.rock_ridge is not None and not child.is_dot() and not child.is_dotdot(): lo = 0 hi = len(self.rr_children) while lo < hi: mid = (lo + hi) // 2 rr = self.rr_children[mid].rock_ridge if rr is not None: if rr.name() < child.rock_ridge.name(): lo = mid + 1 else: hi = mid else: raise pycdlibexception.PyCdlibInternalError('Expected all children to have Rock Ridge, but one did not') rr_index = lo self.rr_children.insert(rr_index, child) # We now have to check if we need to add another logical block. # We have to iterate over the entire list again, because where we # placed this last entry may rearrange the empty spaces in the blocks # that we've already allocated. num_extents, offset_unused = self._recalculate_extents_and_offsets(index, logical_block_size) overflowed = False if check_overflow and (num_extents * logical_block_size > self.data_length): overflowed = True # When we overflow our data length, we always add a full block. self.data_length += logical_block_size # We also have to make sure to update the length of the dot child, # as that should always reflect the length. self.children[0].data_length = self.data_length # We also have to update all of the dotdot entries. If this is # the root directory record (no parent), we first update the root # dotdot entry. In all cases, we update the dotdot entry of all # children that are directories. if self.parent is None: self.children[1].data_length = self.data_length for c in self.children: if not c.is_dir(): continue if len(c.children) > 1: c.children[1].data_length = self.data_length return overflowed
python
def _add_child(self, child, logical_block_size, allow_duplicate, check_overflow): # type: (DirectoryRecord, int, bool, bool) -> bool ''' An internal method to add a child to this object. Note that this is called both during parsing and when adding a new object to the system, so it it shouldn't have any functionality that is not appropriate for both. Parameters: child - The child directory record object to add. logical_block_size - The size of a logical block for this volume descriptor. allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed. check_overflow - Whether to check for overflow; if we are parsing, we don't want to do this. Returns: True if adding this child caused the directory to overflow into another extent, False otherwise. ''' if not self.isdir: raise pycdlibexception.PyCdlibInvalidInput('Trying to add a child to a record that is not a directory') # First ensure that this is not a duplicate. For speed purposes, we # recognize that bisect_left will always choose an index to the *left* # of a duplicate child. Thus, to check for duplicates we only need to # see if the child to be added is a duplicate with the entry that # bisect_left returned. index = bisect.bisect_left(self.children, child) if index != len(self.children) and self.children[index].file_ident == child.file_ident: if not self.children[index].is_associated_file() and not child.is_associated_file(): if not (self.rock_ridge is not None and self.file_identifier() == b'RR_MOVED'): if not allow_duplicate: raise pycdlibexception.PyCdlibInvalidInput('Failed adding duplicate name to parent') else: self.children[index].data_continuation = child index += 1 self.children.insert(index, child) if child.rock_ridge is not None and not child.is_dot() and not child.is_dotdot(): lo = 0 hi = len(self.rr_children) while lo < hi: mid = (lo + hi) // 2 rr = self.rr_children[mid].rock_ridge if rr is not None: if rr.name() < child.rock_ridge.name(): lo = mid + 1 else: hi = mid else: raise pycdlibexception.PyCdlibInternalError('Expected all children to have Rock Ridge, but one did not') rr_index = lo self.rr_children.insert(rr_index, child) # We now have to check if we need to add another logical block. # We have to iterate over the entire list again, because where we # placed this last entry may rearrange the empty spaces in the blocks # that we've already allocated. num_extents, offset_unused = self._recalculate_extents_and_offsets(index, logical_block_size) overflowed = False if check_overflow and (num_extents * logical_block_size > self.data_length): overflowed = True # When we overflow our data length, we always add a full block. self.data_length += logical_block_size # We also have to make sure to update the length of the dot child, # as that should always reflect the length. self.children[0].data_length = self.data_length # We also have to update all of the dotdot entries. If this is # the root directory record (no parent), we first update the root # dotdot entry. In all cases, we update the dotdot entry of all # children that are directories. if self.parent is None: self.children[1].data_length = self.data_length for c in self.children: if not c.is_dir(): continue if len(c.children) > 1: c.children[1].data_length = self.data_length return overflowed
[ "def", "_add_child", "(", "self", ",", "child", ",", "logical_block_size", ",", "allow_duplicate", ",", "check_overflow", ")", ":", "# type: (DirectoryRecord, int, bool, bool) -> bool", "if", "not", "self", ".", "isdir", ":", "raise", "pycdlibexception", ".", "PyCdlib...
An internal method to add a child to this object. Note that this is called both during parsing and when adding a new object to the system, so it it shouldn't have any functionality that is not appropriate for both. Parameters: child - The child directory record object to add. logical_block_size - The size of a logical block for this volume descriptor. allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed. check_overflow - Whether to check for overflow; if we are parsing, we don't want to do this. Returns: True if adding this child caused the directory to overflow into another extent, False otherwise.
[ "An", "internal", "method", "to", "add", "a", "child", "to", "this", "object", ".", "Note", "that", "this", "is", "called", "both", "during", "parsing", "and", "when", "adding", "a", "new", "object", "to", "the", "system", "so", "it", "it", "shouldn", ...
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L696-L776
21,577
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.add_child
def add_child(self, child, logical_block_size, allow_duplicate=False): # type: (DirectoryRecord, int, bool) -> bool ''' A method to add a new child to this directory record. Parameters: child - The child directory record object to add. logical_block_size - The size of a logical block for this volume descriptor. allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed. Returns: True if adding this child caused the directory to overflow into another extent, False otherwise. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') return self._add_child(child, logical_block_size, allow_duplicate, True)
python
def add_child(self, child, logical_block_size, allow_duplicate=False): # type: (DirectoryRecord, int, bool) -> bool ''' A method to add a new child to this directory record. Parameters: child - The child directory record object to add. logical_block_size - The size of a logical block for this volume descriptor. allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed. Returns: True if adding this child caused the directory to overflow into another extent, False otherwise. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') return self._add_child(child, logical_block_size, allow_duplicate, True)
[ "def", "add_child", "(", "self", ",", "child", ",", "logical_block_size", ",", "allow_duplicate", "=", "False", ")", ":", "# type: (DirectoryRecord, int, bool) -> bool", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalE...
A method to add a new child to this directory record. Parameters: child - The child directory record object to add. logical_block_size - The size of a logical block for this volume descriptor. allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed. Returns: True if adding this child caused the directory to overflow into another extent, False otherwise.
[ "A", "method", "to", "add", "a", "new", "child", "to", "this", "directory", "record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L778-L795
21,578
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.track_child
def track_child(self, child, logical_block_size, allow_duplicate=False): # type: (DirectoryRecord, int, bool) -> None ''' A method to track an existing child of this directory record. Parameters: child - The child directory record object to add. logical_block_size - The size of a logical block for this volume descriptor. allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') self._add_child(child, logical_block_size, allow_duplicate, False)
python
def track_child(self, child, logical_block_size, allow_duplicate=False): # type: (DirectoryRecord, int, bool) -> None ''' A method to track an existing child of this directory record. Parameters: child - The child directory record object to add. logical_block_size - The size of a logical block for this volume descriptor. allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') self._add_child(child, logical_block_size, allow_duplicate, False)
[ "def", "track_child", "(", "self", ",", "child", ",", "logical_block_size", ",", "allow_duplicate", "=", "False", ")", ":", "# type: (DirectoryRecord, int, bool) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInterna...
A method to track an existing child of this directory record. Parameters: child - The child directory record object to add. logical_block_size - The size of a logical block for this volume descriptor. allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed. Returns: Nothing.
[ "A", "method", "to", "track", "an", "existing", "child", "of", "this", "directory", "record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L797-L813
21,579
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.remove_child
def remove_child(self, child, index, logical_block_size): # type: (DirectoryRecord, int, int) -> bool ''' A method to remove a child from this Directory Record. Parameters: child - The child DirectoryRecord object to remove. index - The index of the child into this DirectoryRecord children list. logical_block_size - The size of a logical block on this volume descriptor. Returns: True if removing this child caused an underflow, False otherwise. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') if index < 0: # This should never happen raise pycdlibexception.PyCdlibInternalError('Invalid child index to remove') # Unfortunately, Rock Ridge specifies that a CL 'directory' is replaced # by a *file*, not another directory. Thus, we can't just depend on # whether this child is marked as a directory by the file flags during # parse time. Instead, we check if this is either a true directory, # or a Rock Ridge CL entry, and in either case try to manipulate the # file links. if child.rock_ridge is not None: if child.isdir or child.rock_ridge.child_link_record_exists(): if len(self.children) < 2: raise pycdlibexception.PyCdlibInvalidISO('Expected a dot and dotdot entry, but missing; ISO is corrupt') if self.children[0].rock_ridge is None or self.children[1].rock_ridge is None: raise pycdlibexception.PyCdlibInvalidISO('Missing Rock Ridge entry on dot or dotdot; ISO is corrupt') if self.parent is None: self.children[0].rock_ridge.remove_from_file_links() self.children[1].rock_ridge.remove_from_file_links() else: if self.rock_ridge is None: raise pycdlibexception.PyCdlibInvalidISO('Child has Rock Ridge, but parent does not; ISO is corrupt') self.rock_ridge.remove_from_file_links() self.children[0].rock_ridge.remove_from_file_links() del self.children[index] # We now have to check if we need to remove a logical block. # We have to iterate over the entire list again, because where we # removed this last entry may rearrange the empty spaces in the blocks # that we've already allocated. num_extents, dirrecord_offset = self._recalculate_extents_and_offsets(index, logical_block_size) underflow = False total_size = (num_extents - 1) * logical_block_size + dirrecord_offset if (self.data_length - total_size) > logical_block_size: self.data_length -= logical_block_size # We also have to make sure to update the length of the dot child, # as that should always reflect the length. self.children[0].data_length = self.data_length # We also have to update all of the dotdot entries. If this is # the root directory record (no parent), we first update the root # dotdot entry. In all cases, we update the dotdot entry of all # children that are directories. if self.parent is None: self.children[1].data_length = self.data_length for c in self.children: if not c.is_dir(): continue if len(c.children) > 1: c.children[1].data_length = self.data_length underflow = True return underflow
python
def remove_child(self, child, index, logical_block_size): # type: (DirectoryRecord, int, int) -> bool ''' A method to remove a child from this Directory Record. Parameters: child - The child DirectoryRecord object to remove. index - The index of the child into this DirectoryRecord children list. logical_block_size - The size of a logical block on this volume descriptor. Returns: True if removing this child caused an underflow, False otherwise. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') if index < 0: # This should never happen raise pycdlibexception.PyCdlibInternalError('Invalid child index to remove') # Unfortunately, Rock Ridge specifies that a CL 'directory' is replaced # by a *file*, not another directory. Thus, we can't just depend on # whether this child is marked as a directory by the file flags during # parse time. Instead, we check if this is either a true directory, # or a Rock Ridge CL entry, and in either case try to manipulate the # file links. if child.rock_ridge is not None: if child.isdir or child.rock_ridge.child_link_record_exists(): if len(self.children) < 2: raise pycdlibexception.PyCdlibInvalidISO('Expected a dot and dotdot entry, but missing; ISO is corrupt') if self.children[0].rock_ridge is None or self.children[1].rock_ridge is None: raise pycdlibexception.PyCdlibInvalidISO('Missing Rock Ridge entry on dot or dotdot; ISO is corrupt') if self.parent is None: self.children[0].rock_ridge.remove_from_file_links() self.children[1].rock_ridge.remove_from_file_links() else: if self.rock_ridge is None: raise pycdlibexception.PyCdlibInvalidISO('Child has Rock Ridge, but parent does not; ISO is corrupt') self.rock_ridge.remove_from_file_links() self.children[0].rock_ridge.remove_from_file_links() del self.children[index] # We now have to check if we need to remove a logical block. # We have to iterate over the entire list again, because where we # removed this last entry may rearrange the empty spaces in the blocks # that we've already allocated. num_extents, dirrecord_offset = self._recalculate_extents_and_offsets(index, logical_block_size) underflow = False total_size = (num_extents - 1) * logical_block_size + dirrecord_offset if (self.data_length - total_size) > logical_block_size: self.data_length -= logical_block_size # We also have to make sure to update the length of the dot child, # as that should always reflect the length. self.children[0].data_length = self.data_length # We also have to update all of the dotdot entries. If this is # the root directory record (no parent), we first update the root # dotdot entry. In all cases, we update the dotdot entry of all # children that are directories. if self.parent is None: self.children[1].data_length = self.data_length for c in self.children: if not c.is_dir(): continue if len(c.children) > 1: c.children[1].data_length = self.data_length underflow = True return underflow
[ "def", "remove_child", "(", "self", ",", "child", ",", "index", ",", "logical_block_size", ")", ":", "# type: (DirectoryRecord, int, int) -> bool", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Director...
A method to remove a child from this Directory Record. Parameters: child - The child DirectoryRecord object to remove. index - The index of the child into this DirectoryRecord children list. logical_block_size - The size of a logical block on this volume descriptor. Returns: True if removing this child caused an underflow, False otherwise.
[ "A", "method", "to", "remove", "a", "child", "from", "this", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L815-L887
21,580
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.record
def record(self): # type: () -> bytes ''' A method to generate the string representing this Directory Record. Parameters: None. Returns: String representing this Directory Record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') # Ecma-119 9.1.5 says the date should reflect the time when the # record was written, so we make a new date now and use that to # write out the record. self.date = dates.DirectoryRecordDate() self.date.new() padlen = struct.calcsize(self.FMT) + self.len_fi padstr = b'\x00' * (padlen % 2) extent_loc = self._extent_location() xa_rec = b'' if self.xa_record is not None: xa_rec = b'\x00' * self.xa_pad_size + self.xa_record.record() rr_rec = b'' if self.rock_ridge is not None: rr_rec = self.rock_ridge.record_dr_entries() outlist = [struct.pack(self.FMT, self.dr_len, self.xattr_len, extent_loc, utils.swab_32bit(extent_loc), self.data_length, utils.swab_32bit(self.data_length), self.date.record(), self.file_flags, self.file_unit_size, self.interleave_gap_size, self.seqnum, utils.swab_16bit(self.seqnum), self.len_fi) + self.file_ident + padstr + xa_rec + rr_rec] outlist.append(b'\x00' * (len(outlist[0]) % 2)) return b''.join(outlist)
python
def record(self): # type: () -> bytes ''' A method to generate the string representing this Directory Record. Parameters: None. Returns: String representing this Directory Record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') # Ecma-119 9.1.5 says the date should reflect the time when the # record was written, so we make a new date now and use that to # write out the record. self.date = dates.DirectoryRecordDate() self.date.new() padlen = struct.calcsize(self.FMT) + self.len_fi padstr = b'\x00' * (padlen % 2) extent_loc = self._extent_location() xa_rec = b'' if self.xa_record is not None: xa_rec = b'\x00' * self.xa_pad_size + self.xa_record.record() rr_rec = b'' if self.rock_ridge is not None: rr_rec = self.rock_ridge.record_dr_entries() outlist = [struct.pack(self.FMT, self.dr_len, self.xattr_len, extent_loc, utils.swab_32bit(extent_loc), self.data_length, utils.swab_32bit(self.data_length), self.date.record(), self.file_flags, self.file_unit_size, self.interleave_gap_size, self.seqnum, utils.swab_16bit(self.seqnum), self.len_fi) + self.file_ident + padstr + xa_rec + rr_rec] outlist.append(b'\x00' * (len(outlist[0]) % 2)) return b''.join(outlist)
[ "def", "record", "(", "self", ")", ":", "# type: () -> bytes", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory Record not yet initialized'", ")", "# Ecma-119 9.1.5 says the date should reflect the time ...
A method to generate the string representing this Directory Record. Parameters: None. Returns: String representing this Directory Record.
[ "A", "method", "to", "generate", "the", "string", "representing", "this", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L1001-L1042
21,581
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.is_associated_file
def is_associated_file(self): # type: () -> bool ''' A method to determine whether this file is 'associated' with another file on the ISO. Parameters: None. Returns: True if this file is associated with another file on the ISO, False otherwise. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') return self.file_flags & (1 << self.FILE_FLAG_ASSOCIATED_FILE_BIT)
python
def is_associated_file(self): # type: () -> bool ''' A method to determine whether this file is 'associated' with another file on the ISO. Parameters: None. Returns: True if this file is associated with another file on the ISO, False otherwise. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') return self.file_flags & (1 << self.FILE_FLAG_ASSOCIATED_FILE_BIT)
[ "def", "is_associated_file", "(", "self", ")", ":", "# type: () -> bool", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory Record not yet initialized'", ")", "return", "self", ".", "file_flags", ...
A method to determine whether this file is 'associated' with another file on the ISO. Parameters: None. Returns: True if this file is associated with another file on the ISO, False otherwise.
[ "A", "method", "to", "determine", "whether", "this", "file", "is", "associated", "with", "another", "file", "on", "the", "ISO", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L1044-L1059
21,582
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.set_ptr
def set_ptr(self, ptr): # type: (path_table_record.PathTableRecord) -> None ''' A method to set the Path Table Record associated with this Directory Record. Parameters: ptr - The path table record to associate with this Directory Record. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') self.ptr = ptr
python
def set_ptr(self, ptr): # type: (path_table_record.PathTableRecord) -> None ''' A method to set the Path Table Record associated with this Directory Record. Parameters: ptr - The path table record to associate with this Directory Record. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') self.ptr = ptr
[ "def", "set_ptr", "(", "self", ",", "ptr", ")", ":", "# type: (path_table_record.PathTableRecord) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory Record not yet initialized'", ")", "self",...
A method to set the Path Table Record associated with this Directory Record. Parameters: ptr - The path table record to associate with this Directory Record. Returns: Nothing.
[ "A", "method", "to", "set", "the", "Path", "Table", "Record", "associated", "with", "this", "Directory", "Record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L1061-L1075
21,583
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.set_data_location
def set_data_location(self, current_extent, tag_location): # pylint: disable=unused-argument # type: (int, int) -> None ''' A method to set the new extent location that the data for this Directory Record should live at. Parameters: current_extent - The new extent. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') self.new_extent_loc = current_extent if self.ptr is not None: self.ptr.update_extent_location(current_extent)
python
def set_data_location(self, current_extent, tag_location): # pylint: disable=unused-argument # type: (int, int) -> None ''' A method to set the new extent location that the data for this Directory Record should live at. Parameters: current_extent - The new extent. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') self.new_extent_loc = current_extent if self.ptr is not None: self.ptr.update_extent_location(current_extent)
[ "def", "set_data_location", "(", "self", ",", "current_extent", ",", "tag_location", ")", ":", "# pylint: disable=unused-argument", "# type: (int, int) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", ...
A method to set the new extent location that the data for this Directory Record should live at. Parameters: current_extent - The new extent. Returns: Nothing.
[ "A", "method", "to", "set", "the", "new", "extent", "location", "that", "the", "data", "for", "this", "Directory", "Record", "should", "live", "at", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L1077-L1093
21,584
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.get_data_length
def get_data_length(self): # type: () -> int ''' A method to get the length of the data that this Directory Record points to. Parameters: None. Returns: The length of the data that this Directory Record points to. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') if self.inode is not None: return self.inode.get_data_length() return self.data_length
python
def get_data_length(self): # type: () -> int ''' A method to get the length of the data that this Directory Record points to. Parameters: None. Returns: The length of the data that this Directory Record points to. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') if self.inode is not None: return self.inode.get_data_length() return self.data_length
[ "def", "get_data_length", "(", "self", ")", ":", "# type: () -> int", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory Record not yet initialized'", ")", "if", "self", ".", "inode", "is", "not"...
A method to get the length of the data that this Directory Record points to. Parameters: None. Returns: The length of the data that this Directory Record points to.
[ "A", "method", "to", "get", "the", "length", "of", "the", "data", "that", "this", "Directory", "Record", "points", "to", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L1095-L1110
21,585
clalancette/pycdlib
pycdlib/dr.py
DirectoryRecord.set_data_length
def set_data_length(self, length): # type: (int) -> None ''' A method to set the length of the data that this Directory Record points to. Parameters: length - The new length for the data. Returns: The length of the data that this Directory Record points to. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') self.data_length = length
python
def set_data_length(self, length): # type: (int) -> None ''' A method to set the length of the data that this Directory Record points to. Parameters: length - The new length for the data. Returns: The length of the data that this Directory Record points to. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record not yet initialized') self.data_length = length
[ "def", "set_data_length", "(", "self", ",", "length", ")", ":", "# type: (int) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory Record not yet initialized'", ")", "self", ".", "data_len...
A method to set the length of the data that this Directory Record points to. Parameters: length - The new length for the data. Returns: The length of the data that this Directory Record points to.
[ "A", "method", "to", "set", "the", "length", "of", "the", "data", "that", "this", "Directory", "Record", "points", "to", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dr.py#L1112-L1125
21,586
clalancette/pycdlib
pycdlib/inode.py
Inode.new
def new(self, length, fp, manage_fp, offset): # type: (int, BinaryIO, bool, int) -> None ''' Initialize a new Inode. Parameters: None. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Inode is already initialized') self.data_length = length self.data_fp = fp self.manage_fp = manage_fp self.fp_offset = offset self.original_data_location = self.DATA_IN_EXTERNAL_FP self._initialized = True
python
def new(self, length, fp, manage_fp, offset): # type: (int, BinaryIO, bool, int) -> None ''' Initialize a new Inode. Parameters: None. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Inode is already initialized') self.data_length = length self.data_fp = fp self.manage_fp = manage_fp self.fp_offset = offset self.original_data_location = self.DATA_IN_EXTERNAL_FP self._initialized = True
[ "def", "new", "(", "self", ",", "length", ",", "fp", ",", "manage_fp", ",", "offset", ")", ":", "# type: (int, BinaryIO, bool, int) -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Inode is already in...
Initialize a new Inode. Parameters: None. Returns: Nothing.
[ "Initialize", "a", "new", "Inode", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/inode.py#L56-L76
21,587
clalancette/pycdlib
pycdlib/inode.py
Inode.parse
def parse(self, extent, length, fp, log_block_size): # type: (int, int, BinaryIO, int) -> None ''' Parse an existing Inode. This just saves off the extent for later use. Parameters: extent - The original extent that the data lives at. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Inode is already initialized') self.orig_extent_loc = extent self.data_length = length self.data_fp = fp self.manage_fp = False self.fp_offset = extent * log_block_size self.original_data_location = self.DATA_ON_ORIGINAL_ISO self._initialized = True
python
def parse(self, extent, length, fp, log_block_size): # type: (int, int, BinaryIO, int) -> None ''' Parse an existing Inode. This just saves off the extent for later use. Parameters: extent - The original extent that the data lives at. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Inode is already initialized') self.orig_extent_loc = extent self.data_length = length self.data_fp = fp self.manage_fp = False self.fp_offset = extent * log_block_size self.original_data_location = self.DATA_ON_ORIGINAL_ISO self._initialized = True
[ "def", "parse", "(", "self", ",", "extent", ",", "length", ",", "fp", ",", "log_block_size", ")", ":", "# type: (int, int, BinaryIO, int) -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Inode is alre...
Parse an existing Inode. This just saves off the extent for later use. Parameters: extent - The original extent that the data lives at. Returns: Nothing.
[ "Parse", "an", "existing", "Inode", ".", "This", "just", "saves", "off", "the", "extent", "for", "later", "use", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/inode.py#L78-L100
21,588
clalancette/pycdlib
pycdlib/inode.py
Inode.add_boot_info_table
def add_boot_info_table(self, boot_info_table): # type: (eltorito.EltoritoBootInfoTable) -> None ''' A method to add a boot info table to this Inode. Parameters: boot_info_table - The Boot Info Table object to add to this Inode. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Inode is not yet initialized') self.boot_info_table = boot_info_table
python
def add_boot_info_table(self, boot_info_table): # type: (eltorito.EltoritoBootInfoTable) -> None ''' A method to add a boot info table to this Inode. Parameters: boot_info_table - The Boot Info Table object to add to this Inode. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Inode is not yet initialized') self.boot_info_table = boot_info_table
[ "def", "add_boot_info_table", "(", "self", ",", "boot_info_table", ")", ":", "# type: (eltorito.EltoritoBootInfoTable) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Inode is not yet initialized'", "...
A method to add a boot info table to this Inode. Parameters: boot_info_table - The Boot Info Table object to add to this Inode. Returns: Nothing.
[ "A", "method", "to", "add", "a", "boot", "info", "table", "to", "this", "Inode", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/inode.py#L149-L162
21,589
clalancette/pycdlib
pycdlib/inode.py
Inode.update_fp
def update_fp(self, fp, length): # type: (BinaryIO, int) -> None ''' Update the Inode to use a different file object and length. Parameters: fp - A file object that contains the data for this Inode. length - The length of the data. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Inode is not yet initialized') self.original_data_location = self.DATA_IN_EXTERNAL_FP self.data_fp = fp self.data_length = length self.fp_offset = 0
python
def update_fp(self, fp, length): # type: (BinaryIO, int) -> None ''' Update the Inode to use a different file object and length. Parameters: fp - A file object that contains the data for this Inode. length - The length of the data. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Inode is not yet initialized') self.original_data_location = self.DATA_IN_EXTERNAL_FP self.data_fp = fp self.data_length = length self.fp_offset = 0
[ "def", "update_fp", "(", "self", ",", "fp", ",", "length", ")", ":", "# type: (BinaryIO, int) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Inode is not yet initialized'", ")", "self", ".", ...
Update the Inode to use a different file object and length. Parameters: fp - A file object that contains the data for this Inode. length - The length of the data. Returns: Nothing.
[ "Update", "the", "Inode", "to", "use", "a", "different", "file", "object", "and", "length", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/inode.py#L164-L181
21,590
clalancette/pycdlib
pycdlib/dates.py
string_to_timestruct
def string_to_timestruct(input_string): # type: (bytes) -> time.struct_time ''' A cacheable function to take an input string and decode it into a time.struct_time from the time module. If the string cannot be decoded because of an illegal value, then the all-zero time.struct_time will be returned instead. Parameters: input_string - The string to attempt to parse. Returns: A time.struct_time object representing the time. ''' try: timestruct = time.strptime(input_string.decode('utf-8'), VolumeDescriptorDate.TIME_FMT) except ValueError: # Ecma-119, 8.4.26.1 specifies that if the string was all the digit # zero, with the last byte 0, the time wasn't specified. In that # case, time.strptime() with our format will raise a ValueError. # In practice we have found that some ISOs specify various wacky # things in this field, so if we see *any* ValueError, we just # assume the date is unspecified and go with that. timestruct = time.struct_time((0, 0, 0, 0, 0, 0, 0, 0, 0)) return timestruct
python
def string_to_timestruct(input_string): # type: (bytes) -> time.struct_time ''' A cacheable function to take an input string and decode it into a time.struct_time from the time module. If the string cannot be decoded because of an illegal value, then the all-zero time.struct_time will be returned instead. Parameters: input_string - The string to attempt to parse. Returns: A time.struct_time object representing the time. ''' try: timestruct = time.strptime(input_string.decode('utf-8'), VolumeDescriptorDate.TIME_FMT) except ValueError: # Ecma-119, 8.4.26.1 specifies that if the string was all the digit # zero, with the last byte 0, the time wasn't specified. In that # case, time.strptime() with our format will raise a ValueError. # In practice we have found that some ISOs specify various wacky # things in this field, so if we see *any* ValueError, we just # assume the date is unspecified and go with that. timestruct = time.struct_time((0, 0, 0, 0, 0, 0, 0, 0, 0)) return timestruct
[ "def", "string_to_timestruct", "(", "input_string", ")", ":", "# type: (bytes) -> time.struct_time", "try", ":", "timestruct", "=", "time", ".", "strptime", "(", "input_string", ".", "decode", "(", "'utf-8'", ")", ",", "VolumeDescriptorDate", ".", "TIME_FMT", ")", ...
A cacheable function to take an input string and decode it into a time.struct_time from the time module. If the string cannot be decoded because of an illegal value, then the all-zero time.struct_time will be returned instead. Parameters: input_string - The string to attempt to parse. Returns: A time.struct_time object representing the time.
[ "A", "cacheable", "function", "to", "take", "an", "input", "string", "and", "decode", "it", "into", "a", "time", ".", "struct_time", "from", "the", "time", "module", ".", "If", "the", "string", "cannot", "be", "decoded", "because", "of", "an", "illegal", ...
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dates.py#L35-L59
21,591
clalancette/pycdlib
pycdlib/dates.py
DirectoryRecordDate.parse
def parse(self, datestr): # type: (bytes) -> None ''' Parse a Directory Record date out of a string. Parameters: datestr - The string to parse the date out of. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record Date already initialized') (self.years_since_1900, self.month, self.day_of_month, self.hour, self.minute, self.second, self.gmtoffset) = struct.unpack_from(self.FMT, datestr, 0) self._initialized = True
python
def parse(self, datestr): # type: (bytes) -> None ''' Parse a Directory Record date out of a string. Parameters: datestr - The string to parse the date out of. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record Date already initialized') (self.years_since_1900, self.month, self.day_of_month, self.hour, self.minute, self.second, self.gmtoffset) = struct.unpack_from(self.FMT, datestr, 0) self._initialized = True
[ "def", "parse", "(", "self", ",", "datestr", ")", ":", "# type: (bytes) -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory Record Date already initialized'", ")", "(", "self", ".", "years_since_...
Parse a Directory Record date out of a string. Parameters: datestr - The string to parse the date out of. Returns: Nothing.
[ "Parse", "a", "Directory", "Record", "date", "out", "of", "a", "string", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dates.py#L81-L98
21,592
clalancette/pycdlib
pycdlib/dates.py
DirectoryRecordDate.new
def new(self): # type: () -> None ''' Create a new Directory Record date based on the current time. Parameters: tm - An optional argument that must be None Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record Date already initialized') # This algorithm was ported from cdrkit, genisoimage.c:iso9660_date() tm = time.time() local = time.localtime(tm) self.years_since_1900 = local.tm_year - 1900 self.month = local.tm_mon self.day_of_month = local.tm_mday self.hour = local.tm_hour self.minute = local.tm_min self.second = local.tm_sec self.gmtoffset = utils.gmtoffset_from_tm(tm, local) self._initialized = True
python
def new(self): # type: () -> None ''' Create a new Directory Record date based on the current time. Parameters: tm - An optional argument that must be None Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record Date already initialized') # This algorithm was ported from cdrkit, genisoimage.c:iso9660_date() tm = time.time() local = time.localtime(tm) self.years_since_1900 = local.tm_year - 1900 self.month = local.tm_mon self.day_of_month = local.tm_mday self.hour = local.tm_hour self.minute = local.tm_min self.second = local.tm_sec self.gmtoffset = utils.gmtoffset_from_tm(tm, local) self._initialized = True
[ "def", "new", "(", "self", ")", ":", "# type: () -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory Record Date already initialized'", ")", "# This algorithm was ported from cdrkit, genisoimage.c:iso9660_...
Create a new Directory Record date based on the current time. Parameters: tm - An optional argument that must be None Returns: Nothing.
[ "Create", "a", "new", "Directory", "Record", "date", "based", "on", "the", "current", "time", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dates.py#L100-L123
21,593
clalancette/pycdlib
pycdlib/dates.py
DirectoryRecordDate.record
def record(self): # type: () -> bytes ''' Return a string representation of the Directory Record date. Parameters: None. Returns: A string representing this Directory Record Date. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record Date not initialized') return struct.pack(self.FMT, self.years_since_1900, self.month, self.day_of_month, self.hour, self.minute, self.second, self.gmtoffset)
python
def record(self): # type: () -> bytes ''' Return a string representation of the Directory Record date. Parameters: None. Returns: A string representing this Directory Record Date. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Directory Record Date not initialized') return struct.pack(self.FMT, self.years_since_1900, self.month, self.day_of_month, self.hour, self.minute, self.second, self.gmtoffset)
[ "def", "record", "(", "self", ")", ":", "# type: () -> bytes", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'Directory Record Date not initialized'", ")", "return", "struct", ".", "pack", "(", "self",...
Return a string representation of the Directory Record date. Parameters: None. Returns: A string representing this Directory Record Date.
[ "Return", "a", "string", "representation", "of", "the", "Directory", "Record", "date", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dates.py#L125-L140
21,594
clalancette/pycdlib
pycdlib/dates.py
VolumeDescriptorDate.parse
def parse(self, datestr): # type: (bytes) -> None ''' Parse a Volume Descriptor Date out of a string. A string of all zeros is valid, which means that the date in this field was not specified. Parameters: datestr - string to be parsed Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This Volume Descriptor Date object is already initialized') if len(datestr) != 17: raise pycdlibexception.PyCdlibInvalidISO('Invalid ISO9660 date string') timestruct = string_to_timestruct(datestr[:-3]) self.year = timestruct.tm_year self.month = timestruct.tm_mon self.dayofmonth = timestruct.tm_mday self.hour = timestruct.tm_hour self.minute = timestruct.tm_min self.second = timestruct.tm_sec if timestruct.tm_year == 0 and timestruct.tm_mon == 0 and timestruct.tm_mday == 0 and timestruct.tm_hour == 0 and timestruct.tm_min == 0 and timestruct.tm_sec == 0: self.hundredthsofsecond = 0 self.gmtoffset = 0 self.date_str = self.EMPTY_STRING else: self.hundredthsofsecond = int(datestr[14:15]) self.gmtoffset, = struct.unpack_from('=b', datestr, 16) self.date_str = datestr self._initialized = True
python
def parse(self, datestr): # type: (bytes) -> None ''' Parse a Volume Descriptor Date out of a string. A string of all zeros is valid, which means that the date in this field was not specified. Parameters: datestr - string to be parsed Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('This Volume Descriptor Date object is already initialized') if len(datestr) != 17: raise pycdlibexception.PyCdlibInvalidISO('Invalid ISO9660 date string') timestruct = string_to_timestruct(datestr[:-3]) self.year = timestruct.tm_year self.month = timestruct.tm_mon self.dayofmonth = timestruct.tm_mday self.hour = timestruct.tm_hour self.minute = timestruct.tm_min self.second = timestruct.tm_sec if timestruct.tm_year == 0 and timestruct.tm_mon == 0 and timestruct.tm_mday == 0 and timestruct.tm_hour == 0 and timestruct.tm_min == 0 and timestruct.tm_sec == 0: self.hundredthsofsecond = 0 self.gmtoffset = 0 self.date_str = self.EMPTY_STRING else: self.hundredthsofsecond = int(datestr[14:15]) self.gmtoffset, = struct.unpack_from('=b', datestr, 16) self.date_str = datestr self._initialized = True
[ "def", "parse", "(", "self", ",", "datestr", ")", ":", "# type: (bytes) -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'This Volume Descriptor Date object is already initialized'", ")", "if", "len", "(",...
Parse a Volume Descriptor Date out of a string. A string of all zeros is valid, which means that the date in this field was not specified. Parameters: datestr - string to be parsed Returns: Nothing.
[ "Parse", "a", "Volume", "Descriptor", "Date", "out", "of", "a", "string", ".", "A", "string", "of", "all", "zeros", "is", "valid", "which", "means", "that", "the", "date", "in", "this", "field", "was", "not", "specified", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/dates.py#L170-L203
21,595
clalancette/pycdlib
pycdlib/rockridge.py
RRSPRecord.parse
def parse(self, rrstr): # type: (bytes) -> None ''' Parse a Rock Ridge Sharing Protocol record out of a string. Parameters: rrstr - The string to parse the record out of. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('SP record already initialized!') (su_len, su_entry_version_unused, check_byte1, check_byte2, self.bytes_to_skip) = struct.unpack_from('=BBBBB', rrstr[:7], 2) # We assume that the caller has already checked the su_entry_version, # so we don't bother. if su_len != RRSPRecord.length(): raise pycdlibexception.PyCdlibInvalidISO('Invalid length on rock ridge extension') if check_byte1 != 0xbe or check_byte2 != 0xef: raise pycdlibexception.PyCdlibInvalidISO('Invalid check bytes on rock ridge extension') self._initialized = True
python
def parse(self, rrstr): # type: (bytes) -> None ''' Parse a Rock Ridge Sharing Protocol record out of a string. Parameters: rrstr - The string to parse the record out of. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('SP record already initialized!') (su_len, su_entry_version_unused, check_byte1, check_byte2, self.bytes_to_skip) = struct.unpack_from('=BBBBB', rrstr[:7], 2) # We assume that the caller has already checked the su_entry_version, # so we don't bother. if su_len != RRSPRecord.length(): raise pycdlibexception.PyCdlibInvalidISO('Invalid length on rock ridge extension') if check_byte1 != 0xbe or check_byte2 != 0xef: raise pycdlibexception.PyCdlibInvalidISO('Invalid check bytes on rock ridge extension') self._initialized = True
[ "def", "parse", "(", "self", ",", "rrstr", ")", ":", "# type: (bytes) -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'SP record already initialized!'", ")", "(", "su_len", ",", "su_entry_version_unused...
Parse a Rock Ridge Sharing Protocol record out of a string. Parameters: rrstr - The string to parse the record out of. Returns: Nothing.
[ "Parse", "a", "Rock", "Ridge", "Sharing", "Protocol", "record", "out", "of", "a", "string", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/rockridge.py#L59-L83
21,596
clalancette/pycdlib
pycdlib/rockridge.py
RRSPRecord.new
def new(self, bytes_to_skip): # type: (int) -> None ''' Create a new Rock Ridge Sharing Protocol record. Parameters: bytes_to_skip - The number of bytes to skip. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('SP record already initialized!') self.bytes_to_skip = bytes_to_skip self._initialized = True
python
def new(self, bytes_to_skip): # type: (int) -> None ''' Create a new Rock Ridge Sharing Protocol record. Parameters: bytes_to_skip - The number of bytes to skip. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('SP record already initialized!') self.bytes_to_skip = bytes_to_skip self._initialized = True
[ "def", "new", "(", "self", ",", "bytes_to_skip", ")", ":", "# type: (int) -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'SP record already initialized!'", ")", "self", ".", "bytes_to_skip", "=", "by...
Create a new Rock Ridge Sharing Protocol record. Parameters: bytes_to_skip - The number of bytes to skip. Returns: Nothing.
[ "Create", "a", "new", "Rock", "Ridge", "Sharing", "Protocol", "record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/rockridge.py#L85-L99
21,597
clalancette/pycdlib
pycdlib/rockridge.py
RRSPRecord.record
def record(self): # type: () -> bytes ''' Generate a string representing the Rock Ridge Sharing Protocol record. Parameters: None. Returns: String containing the Rock Ridge record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('SP record not yet initialized!') return b'SP' + struct.pack('=BBBBB', RRSPRecord.length(), SU_ENTRY_VERSION, 0xbe, 0xef, self.bytes_to_skip)
python
def record(self): # type: () -> bytes ''' Generate a string representing the Rock Ridge Sharing Protocol record. Parameters: None. Returns: String containing the Rock Ridge record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('SP record not yet initialized!') return b'SP' + struct.pack('=BBBBB', RRSPRecord.length(), SU_ENTRY_VERSION, 0xbe, 0xef, self.bytes_to_skip)
[ "def", "record", "(", "self", ")", ":", "# type: () -> bytes", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'SP record not yet initialized!'", ")", "return", "b'SP'", "+", "struct", ".", "pack", "("...
Generate a string representing the Rock Ridge Sharing Protocol record. Parameters: None. Returns: String containing the Rock Ridge record.
[ "Generate", "a", "string", "representing", "the", "Rock", "Ridge", "Sharing", "Protocol", "record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/rockridge.py#L101-L114
21,598
clalancette/pycdlib
pycdlib/rockridge.py
RRRRRecord.new
def new(self): # type: () -> None ''' Create a new Rock Ridge Rock Ridge record. Parameters: None. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('RR record already initialized!') self.rr_flags = 0 self._initialized = True
python
def new(self): # type: () -> None ''' Create a new Rock Ridge Rock Ridge record. Parameters: None. Returns: Nothing. ''' if self._initialized: raise pycdlibexception.PyCdlibInternalError('RR record already initialized!') self.rr_flags = 0 self._initialized = True
[ "def", "new", "(", "self", ")", ":", "# type: () -> None", "if", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'RR record already initialized!'", ")", "self", ".", "rr_flags", "=", "0", "self", ".", "_initialized...
Create a new Rock Ridge Rock Ridge record. Parameters: None. Returns: Nothing.
[ "Create", "a", "new", "Rock", "Ridge", "Rock", "Ridge", "record", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/rockridge.py#L167-L181
21,599
clalancette/pycdlib
pycdlib/rockridge.py
RRRRRecord.append_field
def append_field(self, fieldname): # type: (str) -> None ''' Mark a field as present in the Rock Ridge records. Parameters: fieldname - The name of the field to mark as present; should be one of 'PX', 'PN', 'SL', 'NM', 'CL', 'PL', 'RE', or 'TF'. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('RR record not yet initialized!') if fieldname == 'PX': bit = 0 elif fieldname == 'PN': bit = 1 elif fieldname == 'SL': bit = 2 elif fieldname == 'NM': bit = 3 elif fieldname == 'CL': bit = 4 elif fieldname == 'PL': bit = 5 elif fieldname == 'RE': bit = 6 elif fieldname == 'TF': bit = 7 else: raise pycdlibexception.PyCdlibInternalError('Unknown RR field name %s' % (fieldname)) self.rr_flags |= (1 << bit)
python
def append_field(self, fieldname): # type: (str) -> None ''' Mark a field as present in the Rock Ridge records. Parameters: fieldname - The name of the field to mark as present; should be one of 'PX', 'PN', 'SL', 'NM', 'CL', 'PL', 'RE', or 'TF'. Returns: Nothing. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('RR record not yet initialized!') if fieldname == 'PX': bit = 0 elif fieldname == 'PN': bit = 1 elif fieldname == 'SL': bit = 2 elif fieldname == 'NM': bit = 3 elif fieldname == 'CL': bit = 4 elif fieldname == 'PL': bit = 5 elif fieldname == 'RE': bit = 6 elif fieldname == 'TF': bit = 7 else: raise pycdlibexception.PyCdlibInternalError('Unknown RR field name %s' % (fieldname)) self.rr_flags |= (1 << bit)
[ "def", "append_field", "(", "self", ",", "fieldname", ")", ":", "# type: (str) -> None", "if", "not", "self", ".", "_initialized", ":", "raise", "pycdlibexception", ".", "PyCdlibInternalError", "(", "'RR record not yet initialized!'", ")", "if", "fieldname", "==", "...
Mark a field as present in the Rock Ridge records. Parameters: fieldname - The name of the field to mark as present; should be one of 'PX', 'PN', 'SL', 'NM', 'CL', 'PL', 'RE', or 'TF'. Returns: Nothing.
[ "Mark", "a", "field", "as", "present", "in", "the", "Rock", "Ridge", "records", "." ]
1e7b77a809e905d67dc71e12d70e850be26b6233
https://github.com/clalancette/pycdlib/blob/1e7b77a809e905d67dc71e12d70e850be26b6233/pycdlib/rockridge.py#L183-L216