file_name
large_stringlengths
4
140
prefix
large_stringlengths
0
12.1k
suffix
large_stringlengths
0
12k
middle
large_stringlengths
0
7.51k
fim_type
large_stringclasses
4 values
__init__.py
trans_counts[l1 - 1, l2 - 1] += 1 # Normalize by rows row_sums = trans_counts.sum(axis=1) trans_probs = trans_counts / row_sums.reshape((-1, 1)) # Get rid of NaNs return np.nan_to_num(trans_probs) def norm_by_rows(matrix): """Normalizes a numpy array by rows (axis 1). Parameters ---------- matrix : array_like A numpy array with at least two axes. Returns ------- a : array_like A row-normalized numpy array """ row_sums = matrix.sum(axis=1) return matrix / row_sums.reshape((-1, 1)) def gauss_kern(size, sigma=1.0): """ Returns a normalized 2D gauss kernel array for convolutions """ h1 = size[0] h2 = size[1] x, y = np.mgrid[0:h2, 0:h1] x = x-h2/2 y = y-h1/2 g = np.exp( -( x**2 + y**2 ) / (2*sigma**2) ); return g / g.sum() def make_heatmap(points, screen_size, point_size, sigma_denom=5.0): point_radius = point_size / 2 screen = np.zeros((screen_size[0] + point_size, screen_size[1] + point_size)) kernel = gauss_kern((point_size, point_size), sigma=(point_size / sigma_denom)) for pt in points: x_start, y_start = pt[0], pt[1] x_end, y_end = x_start + point_size, y_start + point_size scr_slice = screen[x_start:x_end, y_start:y_end] width, height = scr_slice.shape[0], scr_slice.shape[1] screen[x_start:x_end, y_start:y_end] = scr_slice + kernel[:width, :height] screen = screen / screen.max() screen = screen[point_radius:-point_radius, point_radius:-point_radius] return screen def python_line_tokens(code_lines, blank_lines=False): from pygments.lexers import PythonLexer lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) rows = [] for i, tokens in enumerate(line_tokens): # Check for blank line line_str = code_lines[i].rstrip() if (not blank_lines) and len(line_str.strip()) == 0: continue for t in tokens: kind, value = str(t[0]), t[1] yield line_str, i, kind, value, t def python_line_categories(code_lines): from pygments.lexers import PythonLexer lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) line_categories = [] for i, tokens in enumerate(line_tokens): # Check for blank line line_str = code_lines[i].rstrip() if len(line_str.strip()) == 0: line_categories.append(["blank line"]) continue assert len(tokens) > 0, "No tokens for line" categories = [] last_kind, last_value = None, None for t in tokens: kind, value = str(t[0]), t[1] if kind == u"Token.Keyword" and value == u"def": categories.append("function definition") elif kind == u"Token.Keyword" and value == u"if": categories.append("if statement") elif kind == u"Token.Keyword" and value == u"for": categories.append("for loop") elif kind == u"Token.Keyword" and value == u"return": categories.append("return statement") elif kind == u"Token.Keyword" and value == u"print": categories.append("print statement") elif kind == u"Token.Keyword" and value == u"class": categories.append("class definition") elif kind == u"Token.Operator" and value == u"=": categories.append("assignment") elif kind == u"Token.Operator" and value == u".": categories.append("object access") elif kind == u"Token.Operator" and value in [u"+", u"*"]: categories.append("mathematical operation") elif last_kind == u"Token.Operator" and last_value == u"-" and kind == "Token.Whitespace": categories.append("mathematical operation") elif kind == u"Token.Operator" and value in [u"<", u">"]: categories.append("comparison") elif last_kind == u"Token.Name" and kind == "Token.Punctuation" and value == u"(": categories.append("function call") elif kind == "Token.Punctuation" and value == u"[": categories.append("list creation") last_kind, last_value = kind, value if len(categories) == 0: categories.append("unknown") line_categories.append(set(categories)) return line_categories def python_token_metrics(code_lines, indent_size=4): from pygments.lexers import PythonLexer indent_regex = re.compile(r"^\s*") lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) rows = [] for i, tokens in enumerate(line_tokens): line_number = i + 1 # Check for blank line line_str = code_lines[i].rstrip() if len(line_str.strip()) == 0: rows.append([line_number, 0, 0, 0, 0, 0, 0]) continue assert len(tokens) > 0, "No tokens for line" num_keywords = 0 num_identifiers = 0 num_operators = 0 line_length = len(line_str) line_indent = len(indent_regex.findall(line_str)[0]) / indent_size # Indentation is not considered line_str_noindent = line_str.lstrip() line_length_noindent = len(line_str_noindent) whitespace_prop = line_str_noindent.count(" ") / float(line_length_noindent) for t in tokens: kind, value = str(t[0]), t[1] if kind.startswith(u"Token.Keyword"): num_keywords += 1 elif kind.startswith(u"Token.Name"): num_identifiers += 1 elif kind.startswith(u"Token.Operator"): num_operators += 1 rows.append([line_number, line_length_noindent, num_keywords, num_identifiers, num_operators, whitespace_prop, line_indent]) columns = ["line", "line_length", "keywords", "identifiers", "operators", "whitespace_prop", "line_indent"] return pandas.DataFrame(rows, columns=columns) def all_pairs(items, fun, same_value=np.NaN): results = np.zeros((len(items), len(items))) for i, item_i in enumerate(items): for j, item_j in enumerate(items): if i < j: results[i, j] = fun(item_i, item_j) results[j, i] = results[i, j] else: results[i, j] = same_value return results def file_to_text_buffer(f, pad_left=0): if isinstance(f, str): f = open(f, "r") lines = [l.strip() for l in f.readlines()] rows = len(lines) cols = max([len(l) for l in lines]) + pad_left buffer = np.zeros((rows, cols), dtype=str) buffer[:, :] = " " # Fill with white space # Fill buffer for r, line in enumerate(lines):
return buffer @contextlib.contextmanager def stdoutIO(stdout=None): old = sys.stdout if stdout is None: stdout = cStringIO.StringIO() sys.stdout = stdout yield stdout sys.stdout = old def rolling_func(fixations, fun, window_size_ms, step_ms): start, end = 0, window_size_ms return_series = False if not isinstance(fun, dict): fun = { "value" : fun } return_series = True values = { k : [] for k, v in fun.iteritems() } times = [] while start < fixations.end_ms.max(): times.append(start + (window_size_ms / 2)) win_fixations =
chars = ([" "] * pad_left) + list(line) buffer[r, :len(chars)] = chars
conditional_block
__init__.py
trans_counts[l1 - 1, l2 - 1] += 1 # Normalize by rows row_sums = trans_counts.sum(axis=1) trans_probs = trans_counts / row_sums.reshape((-1, 1)) # Get rid of NaNs return np.nan_to_num(trans_probs) def norm_by_rows(matrix): """Normalizes a numpy array by rows (axis 1). Parameters ---------- matrix : array_like A numpy array with at least two axes. Returns ------- a : array_like A row-normalized numpy array """ row_sums = matrix.sum(axis=1) return matrix / row_sums.reshape((-1, 1)) def gauss_kern(size, sigma=1.0): """ Returns a normalized 2D gauss kernel array for convolutions """ h1 = size[0] h2 = size[1] x, y = np.mgrid[0:h2, 0:h1] x = x-h2/2 y = y-h1/2 g = np.exp( -( x**2 + y**2 ) / (2*sigma**2) ); return g / g.sum() def make_heatmap(points, screen_size, point_size, sigma_denom=5.0): point_radius = point_size / 2 screen = np.zeros((screen_size[0] + point_size, screen_size[1] + point_size)) kernel = gauss_kern((point_size, point_size), sigma=(point_size / sigma_denom)) for pt in points: x_start, y_start = pt[0], pt[1] x_end, y_end = x_start + point_size, y_start + point_size scr_slice = screen[x_start:x_end, y_start:y_end] width, height = scr_slice.shape[0], scr_slice.shape[1] screen[x_start:x_end, y_start:y_end] = scr_slice + kernel[:width, :height] screen = screen / screen.max() screen = screen[point_radius:-point_radius, point_radius:-point_radius] return screen def python_line_tokens(code_lines, blank_lines=False): from pygments.lexers import PythonLexer lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) rows = [] for i, tokens in enumerate(line_tokens): # Check for blank line line_str = code_lines[i].rstrip() if (not blank_lines) and len(line_str.strip()) == 0: continue for t in tokens: kind, value = str(t[0]), t[1] yield line_str, i, kind, value, t def python_line_categories(code_lines): from pygments.lexers import PythonLexer lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) line_categories = [] for i, tokens in enumerate(line_tokens): # Check for blank line line_str = code_lines[i].rstrip() if len(line_str.strip()) == 0: line_categories.append(["blank line"]) continue assert len(tokens) > 0, "No tokens for line" categories = [] last_kind, last_value = None, None for t in tokens: kind, value = str(t[0]), t[1] if kind == u"Token.Keyword" and value == u"def": categories.append("function definition") elif kind == u"Token.Keyword" and value == u"if": categories.append("if statement") elif kind == u"Token.Keyword" and value == u"for": categories.append("for loop") elif kind == u"Token.Keyword" and value == u"return": categories.append("return statement") elif kind == u"Token.Keyword" and value == u"print": categories.append("print statement") elif kind == u"Token.Keyword" and value == u"class": categories.append("class definition") elif kind == u"Token.Operator" and value == u"=": categories.append("assignment") elif kind == u"Token.Operator" and value == u".": categories.append("object access") elif kind == u"Token.Operator" and value in [u"+", u"*"]: categories.append("mathematical operation") elif last_kind == u"Token.Operator" and last_value == u"-" and kind == "Token.Whitespace": categories.append("mathematical operation") elif kind == u"Token.Operator" and value in [u"<", u">"]: categories.append("comparison") elif last_kind == u"Token.Name" and kind == "Token.Punctuation" and value == u"(": categories.append("function call") elif kind == "Token.Punctuation" and value == u"[": categories.append("list creation") last_kind, last_value = kind, value if len(categories) == 0: categories.append("unknown") line_categories.append(set(categories)) return line_categories def
(code_lines, indent_size=4): from pygments.lexers import PythonLexer indent_regex = re.compile(r"^\s*") lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) rows = [] for i, tokens in enumerate(line_tokens): line_number = i + 1 # Check for blank line line_str = code_lines[i].rstrip() if len(line_str.strip()) == 0: rows.append([line_number, 0, 0, 0, 0, 0, 0]) continue assert len(tokens) > 0, "No tokens for line" num_keywords = 0 num_identifiers = 0 num_operators = 0 line_length = len(line_str) line_indent = len(indent_regex.findall(line_str)[0]) / indent_size # Indentation is not considered line_str_noindent = line_str.lstrip() line_length_noindent = len(line_str_noindent) whitespace_prop = line_str_noindent.count(" ") / float(line_length_noindent) for t in tokens: kind, value = str(t[0]), t[1] if kind.startswith(u"Token.Keyword"): num_keywords += 1 elif kind.startswith(u"Token.Name"): num_identifiers += 1 elif kind.startswith(u"Token.Operator"): num_operators += 1 rows.append([line_number, line_length_noindent, num_keywords, num_identifiers, num_operators, whitespace_prop, line_indent]) columns = ["line", "line_length", "keywords", "identifiers", "operators", "whitespace_prop", "line_indent"] return pandas.DataFrame(rows, columns=columns) def all_pairs(items, fun, same_value=np.NaN): results = np.zeros((len(items), len(items))) for i, item_i in enumerate(items): for j, item_j in enumerate(items): if i < j: results[i, j] = fun(item_i, item_j) results[j, i] = results[i, j] else: results[i, j] = same_value return results def file_to_text_buffer(f, pad_left=0): if isinstance(f, str): f = open(f, "r") lines = [l.strip() for l in f.readlines()] rows = len(lines) cols = max([len(l) for l in lines]) + pad_left buffer = np.zeros((rows, cols), dtype=str) buffer[:, :] = " " # Fill with white space # Fill buffer for r, line in enumerate(lines): chars = ([" "] * pad_left) + list(line) buffer[r, :len(chars)] = chars return buffer @contextlib.contextmanager def stdoutIO(stdout=None): old = sys.stdout if stdout is None: stdout = cStringIO.StringIO() sys.stdout = stdout yield stdout sys.stdout = old def rolling_func(fixations, fun, window_size_ms, step_ms): start, end = 0, window_size_ms return_series = False if not isinstance(fun, dict): fun = { "value" : fun } return_series = True values = { k : [] for k, v in fun.iteritems() } times = [] while start < fixations.end_ms.max(): times.append(start + (window_size_ms / 2)) win_fixations
python_token_metrics
identifier_name
__init__.py
2*sigma**2) ); return g / g.sum() def make_heatmap(points, screen_size, point_size, sigma_denom=5.0): point_radius = point_size / 2 screen = np.zeros((screen_size[0] + point_size, screen_size[1] + point_size)) kernel = gauss_kern((point_size, point_size), sigma=(point_size / sigma_denom)) for pt in points: x_start, y_start = pt[0], pt[1] x_end, y_end = x_start + point_size, y_start + point_size scr_slice = screen[x_start:x_end, y_start:y_end] width, height = scr_slice.shape[0], scr_slice.shape[1] screen[x_start:x_end, y_start:y_end] = scr_slice + kernel[:width, :height] screen = screen / screen.max() screen = screen[point_radius:-point_radius, point_radius:-point_radius] return screen def python_line_tokens(code_lines, blank_lines=False): from pygments.lexers import PythonLexer lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) rows = [] for i, tokens in enumerate(line_tokens): # Check for blank line line_str = code_lines[i].rstrip() if (not blank_lines) and len(line_str.strip()) == 0: continue for t in tokens: kind, value = str(t[0]), t[1] yield line_str, i, kind, value, t def python_line_categories(code_lines): from pygments.lexers import PythonLexer lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) line_categories = [] for i, tokens in enumerate(line_tokens): # Check for blank line line_str = code_lines[i].rstrip() if len(line_str.strip()) == 0: line_categories.append(["blank line"]) continue assert len(tokens) > 0, "No tokens for line" categories = [] last_kind, last_value = None, None for t in tokens: kind, value = str(t[0]), t[1] if kind == u"Token.Keyword" and value == u"def": categories.append("function definition") elif kind == u"Token.Keyword" and value == u"if": categories.append("if statement") elif kind == u"Token.Keyword" and value == u"for": categories.append("for loop") elif kind == u"Token.Keyword" and value == u"return": categories.append("return statement") elif kind == u"Token.Keyword" and value == u"print": categories.append("print statement") elif kind == u"Token.Keyword" and value == u"class": categories.append("class definition") elif kind == u"Token.Operator" and value == u"=": categories.append("assignment") elif kind == u"Token.Operator" and value == u".": categories.append("object access") elif kind == u"Token.Operator" and value in [u"+", u"*"]: categories.append("mathematical operation") elif last_kind == u"Token.Operator" and last_value == u"-" and kind == "Token.Whitespace": categories.append("mathematical operation") elif kind == u"Token.Operator" and value in [u"<", u">"]: categories.append("comparison") elif last_kind == u"Token.Name" and kind == "Token.Punctuation" and value == u"(": categories.append("function call") elif kind == "Token.Punctuation" and value == u"[": categories.append("list creation") last_kind, last_value = kind, value if len(categories) == 0: categories.append("unknown") line_categories.append(set(categories)) return line_categories def python_token_metrics(code_lines, indent_size=4): from pygments.lexers import PythonLexer indent_regex = re.compile(r"^\s*") lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) rows = [] for i, tokens in enumerate(line_tokens): line_number = i + 1 # Check for blank line line_str = code_lines[i].rstrip() if len(line_str.strip()) == 0: rows.append([line_number, 0, 0, 0, 0, 0, 0]) continue assert len(tokens) > 0, "No tokens for line" num_keywords = 0 num_identifiers = 0 num_operators = 0 line_length = len(line_str) line_indent = len(indent_regex.findall(line_str)[0]) / indent_size # Indentation is not considered line_str_noindent = line_str.lstrip() line_length_noindent = len(line_str_noindent) whitespace_prop = line_str_noindent.count(" ") / float(line_length_noindent) for t in tokens: kind, value = str(t[0]), t[1] if kind.startswith(u"Token.Keyword"): num_keywords += 1 elif kind.startswith(u"Token.Name"): num_identifiers += 1 elif kind.startswith(u"Token.Operator"): num_operators += 1 rows.append([line_number, line_length_noindent, num_keywords, num_identifiers, num_operators, whitespace_prop, line_indent]) columns = ["line", "line_length", "keywords", "identifiers", "operators", "whitespace_prop", "line_indent"] return pandas.DataFrame(rows, columns=columns) def all_pairs(items, fun, same_value=np.NaN): results = np.zeros((len(items), len(items))) for i, item_i in enumerate(items): for j, item_j in enumerate(items): if i < j: results[i, j] = fun(item_i, item_j) results[j, i] = results[i, j] else: results[i, j] = same_value return results def file_to_text_buffer(f, pad_left=0): if isinstance(f, str): f = open(f, "r") lines = [l.strip() for l in f.readlines()] rows = len(lines) cols = max([len(l) for l in lines]) + pad_left buffer = np.zeros((rows, cols), dtype=str) buffer[:, :] = " " # Fill with white space # Fill buffer for r, line in enumerate(lines): chars = ([" "] * pad_left) + list(line) buffer[r, :len(chars)] = chars return buffer @contextlib.contextmanager def stdoutIO(stdout=None): old = sys.stdout if stdout is None: stdout = cStringIO.StringIO() sys.stdout = stdout yield stdout sys.stdout = old def rolling_func(fixations, fun, window_size_ms, step_ms): start, end = 0, window_size_ms return_series = False if not isinstance(fun, dict): fun = { "value" : fun } return_series = True values = { k : [] for k, v in fun.iteritems() } times = [] while start < fixations.end_ms.max(): times.append(start + (window_size_ms / 2)) win_fixations = fixations[ ((fixations.start_ms >= start) & (fixations.start_ms < end)) | ((fixations.end_ms >= start) & (fixations.end_ms < end))] for k, f in fun.iteritems(): values[k].append(f(win_fixations)) start += step_ms end += step_ms first_values = values[values.keys()[0]] if return_series: series = pandas.Series(first_values, index=times) return series else: df = pandas.DataFrame(values, index=times) return df def window(seq, n): """Returns a sliding window (of width n) over data from the iterable s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...""" seq_it = iter(seq) result = tuple(it.islice(seq_it, n)) if len(result) == n: yield result for elem in seq_it: result = result[1:] + (elem,) yield result def just(n, seq):
"""Splits a sequence into n, rest parts.""" it = iter(seq) for _ in range(n - 1):
random_line_split
__init__.py
def norm_by_rows(matrix): """Normalizes a numpy array by rows (axis 1). Parameters ---------- matrix : array_like A numpy array with at least two axes. Returns ------- a : array_like A row-normalized numpy array """ row_sums = matrix.sum(axis=1) return matrix / row_sums.reshape((-1, 1)) def gauss_kern(size, sigma=1.0): """ Returns a normalized 2D gauss kernel array for convolutions """ h1 = size[0] h2 = size[1] x, y = np.mgrid[0:h2, 0:h1] x = x-h2/2 y = y-h1/2 g = np.exp( -( x**2 + y**2 ) / (2*sigma**2) ); return g / g.sum() def make_heatmap(points, screen_size, point_size, sigma_denom=5.0): point_radius = point_size / 2 screen = np.zeros((screen_size[0] + point_size, screen_size[1] + point_size)) kernel = gauss_kern((point_size, point_size), sigma=(point_size / sigma_denom)) for pt in points: x_start, y_start = pt[0], pt[1] x_end, y_end = x_start + point_size, y_start + point_size scr_slice = screen[x_start:x_end, y_start:y_end] width, height = scr_slice.shape[0], scr_slice.shape[1] screen[x_start:x_end, y_start:y_end] = scr_slice + kernel[:width, :height] screen = screen / screen.max() screen = screen[point_radius:-point_radius, point_radius:-point_radius] return screen def python_line_tokens(code_lines, blank_lines=False): from pygments.lexers import PythonLexer lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) rows = [] for i, tokens in enumerate(line_tokens): # Check for blank line line_str = code_lines[i].rstrip() if (not blank_lines) and len(line_str.strip()) == 0: continue for t in tokens: kind, value = str(t[0]), t[1] yield line_str, i, kind, value, t def python_line_categories(code_lines): from pygments.lexers import PythonLexer lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) line_categories = [] for i, tokens in enumerate(line_tokens): # Check for blank line line_str = code_lines[i].rstrip() if len(line_str.strip()) == 0: line_categories.append(["blank line"]) continue assert len(tokens) > 0, "No tokens for line" categories = [] last_kind, last_value = None, None for t in tokens: kind, value = str(t[0]), t[1] if kind == u"Token.Keyword" and value == u"def": categories.append("function definition") elif kind == u"Token.Keyword" and value == u"if": categories.append("if statement") elif kind == u"Token.Keyword" and value == u"for": categories.append("for loop") elif kind == u"Token.Keyword" and value == u"return": categories.append("return statement") elif kind == u"Token.Keyword" and value == u"print": categories.append("print statement") elif kind == u"Token.Keyword" and value == u"class": categories.append("class definition") elif kind == u"Token.Operator" and value == u"=": categories.append("assignment") elif kind == u"Token.Operator" and value == u".": categories.append("object access") elif kind == u"Token.Operator" and value in [u"+", u"*"]: categories.append("mathematical operation") elif last_kind == u"Token.Operator" and last_value == u"-" and kind == "Token.Whitespace": categories.append("mathematical operation") elif kind == u"Token.Operator" and value in [u"<", u">"]: categories.append("comparison") elif last_kind == u"Token.Name" and kind == "Token.Punctuation" and value == u"(": categories.append("function call") elif kind == "Token.Punctuation" and value == u"[": categories.append("list creation") last_kind, last_value = kind, value if len(categories) == 0: categories.append("unknown") line_categories.append(set(categories)) return line_categories def python_token_metrics(code_lines, indent_size=4): from pygments.lexers import PythonLexer indent_regex = re.compile(r"^\s*") lexer = PythonLexer() code_str = "".join(code_lines) all_tokens = list(lexer.get_tokens(code_str, unfiltered=True)) line_tokens = [] current_line = [] for t in all_tokens: if t[1] == u"\n": line_tokens.append(current_line) current_line = [] else: current_line.append(t) rows = [] for i, tokens in enumerate(line_tokens): line_number = i + 1 # Check for blank line line_str = code_lines[i].rstrip() if len(line_str.strip()) == 0: rows.append([line_number, 0, 0, 0, 0, 0, 0]) continue assert len(tokens) > 0, "No tokens for line" num_keywords = 0 num_identifiers = 0 num_operators = 0 line_length = len(line_str) line_indent = len(indent_regex.findall(line_str)[0]) / indent_size # Indentation is not considered line_str_noindent = line_str.lstrip() line_length_noindent = len(line_str_noindent) whitespace_prop = line_str_noindent.count(" ") / float(line_length_noindent) for t in tokens: kind, value = str(t[0]), t[1] if kind.startswith(u"Token.Keyword"): num_keywords += 1 elif kind.startswith(u"Token.Name"): num_identifiers += 1 elif kind.startswith(u"Token.Operator"): num_operators += 1 rows.append([line_number, line_length_noindent, num_keywords, num_identifiers, num_operators, whitespace_prop, line_indent]) columns = ["line", "line_length", "keywords", "identifiers", "operators", "whitespace_prop", "line_indent"] return pandas.DataFrame(rows, columns=columns) def all_pairs(items, fun, same_value=np.NaN): results = np.zeros((len(items), len(items))) for i, item_i in enumerate(items): for j, item_j in enumerate(items): if i < j: results[i, j] = fun(item_i, item_j) results[j, i] = results[i, j] else: results[i, j] = same_value return results def file_to_text_buffer(f, pad_left=0): if isinstance(f, str): f = open(f, "r") lines = [l.strip() for l in f.readlines()] rows = len(lines) cols = max([len(l) for l in lines]) + pad_left buffer = np.zeros((rows, cols), dtype=str) buffer[:, :] = " " # Fill with white space # Fill buffer for r, line in enumerate(lines): chars = ([" "] * pad_left) + list(line) buffer[r, :len(chars)] = chars return buffer @contextlib.contextmanager def stdoutIO(stdout=None): old = sys.stdout if stdout is None: stdout = cStringIO.StringIO() sys.stdout = stdout yield stdout sys.stdout = old def rolling_func(fixations, fun, window_size_ms, step_ms):
start, end = 0, window_size_ms return_series = False if not isinstance(fun, dict): fun = { "value" : fun } return_series = True values = { k : [] for k, v in fun.iteritems() } times = [] while start < fixations.end_ms.max(): times.append(start + (window_size_ms / 2)) win_fixations = fixations[ ((fixations.start_ms >= start) & (fixations.start_ms < end)) | ((fixations.end_ms >= start) & (fixations.end_ms < end))] for k, f in fun.iteritems(): values[k].append(f(win_fixations)) start += step_ms end += step_ms
identifier_body
scheduler.rs
#[export_name = "__lumen_builtin_yield"] pub unsafe extern "C" fn process_yield() -> bool { let s = <Scheduler as rt_core::Scheduler>::current(); // NOTE: We always set root=false here because the root // process never invokes this function s.process_yield(/* root= */ false) } #[naked] #[inline(never)] #[cfg(all(unix, target_arch = "x86_64"))] pub unsafe extern "C" fn process_return_continuation() { let f: fn() -> () = process_return; asm!(" callq *$0 " : : "r"(f) : : "volatile", "alignstack" ); } #[inline(never)] fn process_return() { let s = <Scheduler as rt_core::Scheduler>::current(); do_process_return(&s); } #[export_name = "__lumen_builtin_malloc"] pub unsafe extern "C" fn builtin_malloc(kind: u32, arity: usize) -> *mut u8 { use core::convert::TryInto; use liblumen_alloc::erts::term::closure::ClosureLayout; use liblumen_alloc::erts::term::prelude::*; use liblumen_core::alloc::Layout; use liblumen_term::TermKind; let kind_result: Result<TermKind, _> = kind.try_into(); match kind_result { Ok(TermKind::Closure) => { let s = <Scheduler as rt_core::Scheduler>::current(); let cl = ClosureLayout::for_env_len(arity); let result = s.current.alloc_nofrag_layout(cl.layout().clone()); if let Ok(nn) = result { return nn.as_ptr() as *mut u8; } } Ok(TermKind::Tuple) => { let s = <Scheduler as rt_core::Scheduler>::current(); let layout = Tuple::layout_for_len(arity); let result = s.current.alloc_nofrag_layout(layout); if let Ok(nn) = result { return nn.as_ptr() as *mut u8; } } Ok(TermKind::Cons) => { let s = <Scheduler as rt_core::Scheduler>::current(); let layout = Layout::new::<Cons>(); let result = s.current.alloc_nofrag_layout(layout); if let Ok(nn) = result { return nn.as_ptr() as *mut u8; } } Ok(tk) => { unimplemented!("unhandled use of malloc for {:?}", tk); } Err(_) => { panic!("invalid term kind: {}", kind); } } ptr::null_mut() } /// Called when the current process has finished executing, and has /// returned all the way to its entry function. This marks the process /// as exiting (if it wasn't already), and then yields to the scheduler fn do_process_return(scheduler: &Scheduler) -> bool { use liblumen_alloc::erts::term::prelude::*; if scheduler.current.pid() != scheduler.root.pid() { scheduler .current .exit(atom!("normal"), anyhow!("Out of code").into()); // NOTE: We always set root=false here, even though this can // be called from the root process, since returning from the // root process exits the scheduler loop anyway, so no stack // swapping can occur scheduler.process_yield(/* root= */ false) } else { true } } pub struct Scheduler { id: id::ID, hierarchy: RwLock<Hierarchy>, // References are always 64-bits even on 32-bit platforms reference_count: AtomicU64, run_queues: RwLock<run_queue::Queues>, // Non-monotonic unique integers are scoped to the scheduler ID and then use this per-scheduler // `u64`. unique_integer: AtomicU64, root: Arc<Process>, init: ThreadLocalCell<Arc<Process>>, current: ThreadLocalCell<Arc<Process>>, } // This guarantee holds as long as `init` and `current` are only // ever accessed by the scheduler when scheduling unsafe impl Sync for Scheduler {} impl rt_core::Scheduler for Scheduler { #[inline] fn current() -> Arc<Self> { SCHEDULER.with(|s| s.clone()) } fn id(&self) -> id::ID { self.id } fn hierarchy(&self) -> &RwLock<Hierarchy> { &self.hierarchy } /// Gets the next available reference number fn next_reference_number(&self) -> ReferenceNumber { self.reference_count.fetch_add(1, Ordering::SeqCst) } } impl Scheduler { /// Creates a new scheduler with the default configuration fn new() -> anyhow::Result<Scheduler> { let id = id::next(); // The root process is how the scheduler gets time for itself, // and is also how we know when to shutdown the scheduler due // to termination of all its processes let root = Arc::new(Process::new( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("root"), function: Atom::from_str("init"), arity: 0, }), ptr::null_mut(), 0, )); let run_queues = Default::default(); Scheduler::spawn_root(root.clone(), id, &run_queues)?; // Placeholder let init = Arc::new(Process::new( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("undef"), function: Atom::from_str("undef"), arity: 0, }), ptr::null_mut(), 0, )); // The scheduler starts with the root process running let current = ThreadLocalCell::new(root.clone()); Ok(Self { id, run_queues, root, init: ThreadLocalCell::new(init), current, hierarchy: Default::default(), reference_count: AtomicU64::new(0), unique_integer: AtomicU64::new(0), }) } // Spawns the init process, should be called immediately after // scheduler creation pub fn init(&self) -> anyhow::Result<()> { // The init process is the actual "root" Erlang process, it acts // as the entry point for the program from Erlang's perspective, // and is responsible for starting/stopping the system in Erlang. // // If this process exits, the scheduler terminates let (init_heap, init_heap_size) = process::alloc::default_heap()?; let init = Arc::new(Process::new_with_stack( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("init"), function: Atom::from_str("start"), arity: 0, }), init_heap, init_heap_size, )?); let clone = init.clone(); unsafe { self.init.set(init); } Scheduler::spawn_internal(clone, self.id, &self.run_queues); Ok(()) } /// Gets the scheduler registered to this thread /// /// If no scheduler has been created for this thread, one is created fn registered() -> Arc<Self> { let mut schedulers = SCHEDULERS.lock(); let s = Arc::new(Self::new().unwrap()); if let Some(_) = schedulers.insert(s.id, Arc::downgrade(&s)) { panic!("Scheduler already registered with ID ({:?}", s.id); } s } /// Gets a scheduler by its ID pub fn from_id(id: &id::ID) -> Option<Arc<Self>> { Self::current_from_id(id).or_else(|| SCHEDULERS.lock().get(id).and_then(|s| s.upgrade())) } /// Returns the current thread's scheduler if it matches the given ID fn current_from_id(id: &id::ID) -> Option<Arc<Self>> { SCHEDULER.with(|s| if &s.id == id { Some(s.clone()) } else { None }) } /// Gets the next available unique integer pub fn next_unique_integer(&self) -> u64 { self.unique_integer.fetch_add(1, Ordering::SeqCst) } /// Returns the length of the current scheduler's run queue pub fn run_queues_len(&self) -> usize { self.run_queues.read().len() } /// Returns the length of a specific run queue in the current scheduler #[cfg(test)] pub fn run_queue_len(&self, priority: Priority) -> usize { self.run_queues.read().run_queue_len(priority) } /// Returns true if the given process is in the current scheduler's run queue #[cfg(test)] pub fn is_run_queued(&self, value: &Arc<Process>) -> bool { self.run_queues.read().contains(value) } pub fn stop_waiting(&self, process: &Process) { self.run_queues.write().stop_waiting(process); } // TODO: Request application master termination for controlled shutdown // This request will always come from the thread which spawned the application // master, i.e. the "main" scheduler thread // // Returns `
{ unimplemented!() }
identifier_body
scheduler.rs
-> bool { use liblumen_alloc::erts::term::prelude::*; if scheduler.current.pid() != scheduler.root.pid() { scheduler .current .exit(atom!("normal"), anyhow!("Out of code").into()); // NOTE: We always set root=false here, even though this can // be called from the root process, since returning from the // root process exits the scheduler loop anyway, so no stack // swapping can occur scheduler.process_yield(/* root= */ false) } else { true } } pub struct Scheduler { id: id::ID, hierarchy: RwLock<Hierarchy>, // References are always 64-bits even on 32-bit platforms reference_count: AtomicU64, run_queues: RwLock<run_queue::Queues>, // Non-monotonic unique integers are scoped to the scheduler ID and then use this per-scheduler // `u64`. unique_integer: AtomicU64, root: Arc<Process>, init: ThreadLocalCell<Arc<Process>>, current: ThreadLocalCell<Arc<Process>>, } // This guarantee holds as long as `init` and `current` are only // ever accessed by the scheduler when scheduling unsafe impl Sync for Scheduler {} impl rt_core::Scheduler for Scheduler { #[inline] fn current() -> Arc<Self> { SCHEDULER.with(|s| s.clone()) } fn id(&self) -> id::ID { self.id } fn hierarchy(&self) -> &RwLock<Hierarchy> { &self.hierarchy } /// Gets the next available reference number fn next_reference_number(&self) -> ReferenceNumber { self.reference_count.fetch_add(1, Ordering::SeqCst) } } impl Scheduler { /// Creates a new scheduler with the default configuration fn new() -> anyhow::Result<Scheduler> { let id = id::next(); // The root process is how the scheduler gets time for itself, // and is also how we know when to shutdown the scheduler due // to termination of all its processes let root = Arc::new(Process::new( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("root"), function: Atom::from_str("init"), arity: 0, }), ptr::null_mut(), 0, )); let run_queues = Default::default(); Scheduler::spawn_root(root.clone(), id, &run_queues)?; // Placeholder let init = Arc::new(Process::new( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("undef"), function: Atom::from_str("undef"), arity: 0, }), ptr::null_mut(), 0, )); // The scheduler starts with the root process running let current = ThreadLocalCell::new(root.clone()); Ok(Self { id, run_queues, root, init: ThreadLocalCell::new(init), current, hierarchy: Default::default(), reference_count: AtomicU64::new(0), unique_integer: AtomicU64::new(0), }) } // Spawns the init process, should be called immediately after // scheduler creation pub fn init(&self) -> anyhow::Result<()> { // The init process is the actual "root" Erlang process, it acts // as the entry point for the program from Erlang's perspective, // and is responsible for starting/stopping the system in Erlang. // // If this process exits, the scheduler terminates let (init_heap, init_heap_size) = process::alloc::default_heap()?; let init = Arc::new(Process::new_with_stack( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("init"), function: Atom::from_str("start"), arity: 0, }), init_heap, init_heap_size, )?); let clone = init.clone(); unsafe { self.init.set(init); } Scheduler::spawn_internal(clone, self.id, &self.run_queues); Ok(()) } /// Gets the scheduler registered to this thread /// /// If no scheduler has been created for this thread, one is created fn registered() -> Arc<Self> { let mut schedulers = SCHEDULERS.lock(); let s = Arc::new(Self::new().unwrap()); if let Some(_) = schedulers.insert(s.id, Arc::downgrade(&s)) { panic!("Scheduler already registered with ID ({:?}", s.id); } s } /// Gets a scheduler by its ID pub fn from_id(id: &id::ID) -> Option<Arc<Self>> { Self::current_from_id(id).or_else(|| SCHEDULERS.lock().get(id).and_then(|s| s.upgrade())) } /// Returns the current thread's scheduler if it matches the given ID fn current_from_id(id: &id::ID) -> Option<Arc<Self>> { SCHEDULER.with(|s| if &s.id == id { Some(s.clone()) } else { None }) } /// Gets the next available unique integer pub fn next_unique_integer(&self) -> u64 { self.unique_integer.fetch_add(1, Ordering::SeqCst) } /// Returns the length of the current scheduler's run queue pub fn run_queues_len(&self) -> usize { self.run_queues.read().len() } /// Returns the length of a specific run queue in the current scheduler #[cfg(test)] pub fn
(&self, priority: Priority) -> usize { self.run_queues.read().run_queue_len(priority) } /// Returns true if the given process is in the current scheduler's run queue #[cfg(test)] pub fn is_run_queued(&self, value: &Arc<Process>) -> bool { self.run_queues.read().contains(value) } pub fn stop_waiting(&self, process: &Process) { self.run_queues.write().stop_waiting(process); } // TODO: Request application master termination for controlled shutdown // This request will always come from the thread which spawned the application // master, i.e. the "main" scheduler thread // // Returns `Ok(())` if shutdown was successful, `Err(anyhow::Error)` if something // went wrong during shutdown, and it was not able to complete normally pub fn shutdown(&self) -> anyhow::Result<()> { // For now just Ok(()), but this needs to be addressed when proper // system startup/shutdown is in place CURRENT_PROCESS.with(|cp| cp.replace(None)); Ok(()) } } impl Debug for Scheduler { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Scheduler") .field("id", &self.id) // The hiearchy slots take a lot of space, so don't print them by default .field("reference_count", &self.reference_count) .field("run_queues", &self.run_queues) .finish() } } impl Drop for Scheduler { fn drop(&mut self) { let mut locked_scheduler_by_id = SCHEDULERS.lock(); locked_scheduler_by_id .remove(&self.id) .expect("Scheduler not registered"); } } impl PartialEq for Scheduler { fn eq(&self, other: &Self) -> bool { self.id == other.id } } /// What to run pub enum Run { /// Run the process now Now(Arc<Process>), /// There was a process in the queue, but it needs to be delayed because it is `Priority::Low` /// and hadn't been delayed enough yet. Ask the `RunQueue` again for another process. /// -- https://github.com/erlang/otp/blob/fe2b1323a3866ed0a9712e9d12e1f8f84793ec47/erts/emulator/beam/erl_process.c#L9601-L9606 Delayed, /// There are no processes in the run queue, do other work None, } impl Scheduler { /// > 1. Update reduction counters /// > 2. Check timers /// > 3. If needed check balance /// > 4. If needed migrated processes and ports /// > 5. Do auxiliary scheduler work /// > 6. If needed check I/O and update time /// > 7. While needed pick a port task to execute /// > 8. Pick a process to execute /// > -- [The Scheduler Loop](https://blog.stenmans.org/theBeamBook/#_the_scheduler_loop) /// /// Returns `true` if a process was run. Returns `false` if no process could be run and the /// scheduler should sleep or work steal. #[must_use] pub fn run_once(&self) -> bool { // We always set root=true here, since calling this function is always done // from the scheduler loop, and only ever from the root context
run_queue_len
identifier_name
scheduler.rs
Err(_) => { panic!("invalid term kind: {}", kind); } } ptr::null_mut() } /// Called when the current process has finished executing, and has /// returned all the way to its entry function. This marks the process /// as exiting (if it wasn't already), and then yields to the scheduler fn do_process_return(scheduler: &Scheduler) -> bool { use liblumen_alloc::erts::term::prelude::*; if scheduler.current.pid() != scheduler.root.pid() { scheduler .current .exit(atom!("normal"), anyhow!("Out of code").into()); // NOTE: We always set root=false here, even though this can // be called from the root process, since returning from the // root process exits the scheduler loop anyway, so no stack // swapping can occur scheduler.process_yield(/* root= */ false) } else { true } } pub struct Scheduler { id: id::ID, hierarchy: RwLock<Hierarchy>, // References are always 64-bits even on 32-bit platforms reference_count: AtomicU64, run_queues: RwLock<run_queue::Queues>, // Non-monotonic unique integers are scoped to the scheduler ID and then use this per-scheduler // `u64`. unique_integer: AtomicU64, root: Arc<Process>, init: ThreadLocalCell<Arc<Process>>, current: ThreadLocalCell<Arc<Process>>, } // This guarantee holds as long as `init` and `current` are only // ever accessed by the scheduler when scheduling unsafe impl Sync for Scheduler {} impl rt_core::Scheduler for Scheduler { #[inline] fn current() -> Arc<Self> { SCHEDULER.with(|s| s.clone()) } fn id(&self) -> id::ID { self.id } fn hierarchy(&self) -> &RwLock<Hierarchy> { &self.hierarchy } /// Gets the next available reference number fn next_reference_number(&self) -> ReferenceNumber { self.reference_count.fetch_add(1, Ordering::SeqCst) } } impl Scheduler { /// Creates a new scheduler with the default configuration fn new() -> anyhow::Result<Scheduler> { let id = id::next(); // The root process is how the scheduler gets time for itself, // and is also how we know when to shutdown the scheduler due // to termination of all its processes let root = Arc::new(Process::new( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("root"), function: Atom::from_str("init"), arity: 0, }), ptr::null_mut(), 0, )); let run_queues = Default::default(); Scheduler::spawn_root(root.clone(), id, &run_queues)?; // Placeholder let init = Arc::new(Process::new( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("undef"), function: Atom::from_str("undef"), arity: 0, }), ptr::null_mut(), 0, )); // The scheduler starts with the root process running let current = ThreadLocalCell::new(root.clone()); Ok(Self { id, run_queues, root, init: ThreadLocalCell::new(init), current, hierarchy: Default::default(), reference_count: AtomicU64::new(0), unique_integer: AtomicU64::new(0), }) } // Spawns the init process, should be called immediately after // scheduler creation pub fn init(&self) -> anyhow::Result<()> { // The init process is the actual "root" Erlang process, it acts // as the entry point for the program from Erlang's perspective, // and is responsible for starting/stopping the system in Erlang. // // If this process exits, the scheduler terminates let (init_heap, init_heap_size) = process::alloc::default_heap()?; let init = Arc::new(Process::new_with_stack( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("init"), function: Atom::from_str("start"), arity: 0, }), init_heap, init_heap_size, )?); let clone = init.clone(); unsafe { self.init.set(init); } Scheduler::spawn_internal(clone, self.id, &self.run_queues); Ok(()) } /// Gets the scheduler registered to this thread /// /// If no scheduler has been created for this thread, one is created fn registered() -> Arc<Self> { let mut schedulers = SCHEDULERS.lock(); let s = Arc::new(Self::new().unwrap()); if let Some(_) = schedulers.insert(s.id, Arc::downgrade(&s)) { panic!("Scheduler already registered with ID ({:?}", s.id); } s } /// Gets a scheduler by its ID pub fn from_id(id: &id::ID) -> Option<Arc<Self>> { Self::current_from_id(id).or_else(|| SCHEDULERS.lock().get(id).and_then(|s| s.upgrade())) } /// Returns the current thread's scheduler if it matches the given ID fn current_from_id(id: &id::ID) -> Option<Arc<Self>> { SCHEDULER.with(|s| if &s.id == id { Some(s.clone()) } else { None }) } /// Gets the next available unique integer pub fn next_unique_integer(&self) -> u64 { self.unique_integer.fetch_add(1, Ordering::SeqCst) } /// Returns the length of the current scheduler's run queue pub fn run_queues_len(&self) -> usize { self.run_queues.read().len() } /// Returns the length of a specific run queue in the current scheduler #[cfg(test)] pub fn run_queue_len(&self, priority: Priority) -> usize { self.run_queues.read().run_queue_len(priority) } /// Returns true if the given process is in the current scheduler's run queue #[cfg(test)] pub fn is_run_queued(&self, value: &Arc<Process>) -> bool { self.run_queues.read().contains(value) } pub fn stop_waiting(&self, process: &Process) { self.run_queues.write().stop_waiting(process); } // TODO: Request application master termination for controlled shutdown // This request will always come from the thread which spawned the application // master, i.e. the "main" scheduler thread // // Returns `Ok(())` if shutdown was successful, `Err(anyhow::Error)` if something // went wrong during shutdown, and it was not able to complete normally pub fn shutdown(&self) -> anyhow::Result<()> { // For now just Ok(()), but this needs to be addressed when proper // system startup/shutdown is in place CURRENT_PROCESS.with(|cp| cp.replace(None)); Ok(()) } } impl Debug for Scheduler { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Scheduler") .field("id", &self.id) // The hiearchy slots take a lot of space, so don't print them by default .field("reference_count", &self.reference_count) .field("run_queues", &self.run_queues) .finish() } } impl Drop for Scheduler { fn drop(&mut self) { let mut locked_scheduler_by_id = SCHEDULERS.lock(); locked_scheduler_by_id .remove(&self.id) .expect("Scheduler not registered"); } } impl PartialEq for Scheduler { fn eq(&self, other: &Self) -> bool { self.id == other.id } } /// What to run pub enum Run { /// Run the process now Now(Arc<Process>), /// There was a process in the queue, but it needs to be delayed because it is `Priority::Low` /// and hadn't been delayed enough yet. Ask the `RunQueue` again for another process. /// -- https://github.com/erlang/otp/blob/fe2b1323a3866ed0a9712e9d12e1f8f84793ec47/erts/emulator/beam/erl_process.c#L9601-L9606 Delayed, /// There are no processes in the run queue, do other work None, } impl Scheduler { /// > 1. Update reduction counters /// > 2. Check timers /// > 3. If needed check balance /// > 4. If needed migrated processes and ports /// > 5. Do auxiliary scheduler work /// > 6. If needed check I/O and update time /// > 7. While needed pick a port task to execute /// > 8. Pick a process to execute /// > -- [The Scheduler Loop](https://blog
{ unimplemented!("unhandled use of malloc for {:?}", tk); }
conditional_block
scheduler.rs
) -> bool { use liblumen_alloc::erts::term::prelude::*; if scheduler.current.pid() != scheduler.root.pid() { scheduler .current .exit(atom!("normal"), anyhow!("Out of code").into()); // NOTE: We always set root=false here, even though this can // be called from the root process, since returning from the // root process exits the scheduler loop anyway, so no stack // swapping can occur scheduler.process_yield(/* root= */ false) } else { true } } pub struct Scheduler { id: id::ID, hierarchy: RwLock<Hierarchy>, // References are always 64-bits even on 32-bit platforms reference_count: AtomicU64, run_queues: RwLock<run_queue::Queues>, // Non-monotonic unique integers are scoped to the scheduler ID and then use this per-scheduler // `u64`. unique_integer: AtomicU64, root: Arc<Process>, init: ThreadLocalCell<Arc<Process>>, current: ThreadLocalCell<Arc<Process>>, } // This guarantee holds as long as `init` and `current` are only // ever accessed by the scheduler when scheduling unsafe impl Sync for Scheduler {} impl rt_core::Scheduler for Scheduler { #[inline] fn current() -> Arc<Self> { SCHEDULER.with(|s| s.clone()) } fn id(&self) -> id::ID { self.id } fn hierarchy(&self) -> &RwLock<Hierarchy> { &self.hierarchy } /// Gets the next available reference number fn next_reference_number(&self) -> ReferenceNumber { self.reference_count.fetch_add(1, Ordering::SeqCst) } } impl Scheduler { /// Creates a new scheduler with the default configuration fn new() -> anyhow::Result<Scheduler> { let id = id::next(); // The root process is how the scheduler gets time for itself, // and is also how we know when to shutdown the scheduler due // to termination of all its processes let root = Arc::new(Process::new( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("root"), function: Atom::from_str("init"), arity: 0, }), ptr::null_mut(), 0, )); let run_queues = Default::default(); Scheduler::spawn_root(root.clone(), id, &run_queues)?; // Placeholder let init = Arc::new(Process::new( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("undef"), function: Atom::from_str("undef"), arity: 0, }), ptr::null_mut(), 0, )); // The scheduler starts with the root process running let current = ThreadLocalCell::new(root.clone()); Ok(Self { id, run_queues, root, init: ThreadLocalCell::new(init), current, hierarchy: Default::default(), reference_count: AtomicU64::new(0), unique_integer: AtomicU64::new(0), }) } // Spawns the init process, should be called immediately after // scheduler creation pub fn init(&self) -> anyhow::Result<()> { // The init process is the actual "root" Erlang process, it acts // as the entry point for the program from Erlang's perspective, // and is responsible for starting/stopping the system in Erlang. // // If this process exits, the scheduler terminates let (init_heap, init_heap_size) = process::alloc::default_heap()?; let init = Arc::new(Process::new_with_stack( Priority::Normal, None, Arc::new(ModuleFunctionArity { module: Atom::from_str("init"), function: Atom::from_str("start"), arity: 0, }), init_heap, init_heap_size, )?); let clone = init.clone(); unsafe { self.init.set(init); } Scheduler::spawn_internal(clone, self.id, &self.run_queues); Ok(()) } /// Gets the scheduler registered to this thread /// /// If no scheduler has been created for this thread, one is created fn registered() -> Arc<Self> { let mut schedulers = SCHEDULERS.lock(); let s = Arc::new(Self::new().unwrap()); if let Some(_) = schedulers.insert(s.id, Arc::downgrade(&s)) { panic!("Scheduler already registered with ID ({:?}", s.id); } s } /// Gets a scheduler by its ID pub fn from_id(id: &id::ID) -> Option<Arc<Self>> { Self::current_from_id(id).or_else(|| SCHEDULERS.lock().get(id).and_then(|s| s.upgrade())) } /// Returns the current thread's scheduler if it matches the given ID fn current_from_id(id: &id::ID) -> Option<Arc<Self>> { SCHEDULER.with(|s| if &s.id == id { Some(s.clone()) } else { None }) } /// Gets the next available unique integer pub fn next_unique_integer(&self) -> u64 { self.unique_integer.fetch_add(1, Ordering::SeqCst) } /// Returns the length of the current scheduler's run queue pub fn run_queues_len(&self) -> usize { self.run_queues.read().len() } /// Returns the length of a specific run queue in the current scheduler #[cfg(test)] pub fn run_queue_len(&self, priority: Priority) -> usize { self.run_queues.read().run_queue_len(priority) } /// Returns true if the given process is in the current scheduler's run queue #[cfg(test)] pub fn is_run_queued(&self, value: &Arc<Process>) -> bool { self.run_queues.read().contains(value) } pub fn stop_waiting(&self, process: &Process) { self.run_queues.write().stop_waiting(process); } // TODO: Request application master termination for controlled shutdown // This request will always come from the thread which spawned the application // master, i.e. the "main" scheduler thread // // Returns `Ok(())` if shutdown was successful, `Err(anyhow::Error)` if something // went wrong during shutdown, and it was not able to complete normally pub fn shutdown(&self) -> anyhow::Result<()> { // For now just Ok(()), but this needs to be addressed when proper // system startup/shutdown is in place CURRENT_PROCESS.with(|cp| cp.replace(None)); Ok(()) } } impl Debug for Scheduler { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Scheduler") .field("id", &self.id) // The hiearchy slots take a lot of space, so don't print them by default .field("reference_count", &self.reference_count) .field("run_queues", &self.run_queues) .finish() } } impl Drop for Scheduler { fn drop(&mut self) { let mut locked_scheduler_by_id = SCHEDULERS.lock(); locked_scheduler_by_id .remove(&self.id) .expect("Scheduler not registered"); } } impl PartialEq for Scheduler { fn eq(&self, other: &Self) -> bool { self.id == other.id
pub enum Run { /// Run the process now Now(Arc<Process>), /// There was a process in the queue, but it needs to be delayed because it is `Priority::Low` /// and hadn't been delayed enough yet. Ask the `RunQueue` again for another process. /// -- https://github.com/erlang/otp/blob/fe2b1323a3866ed0a9712e9d12e1f8f84793ec47/erts/emulator/beam/erl_process.c#L9601-L9606 Delayed, /// There are no processes in the run queue, do other work None, } impl Scheduler { /// > 1. Update reduction counters /// > 2. Check timers /// > 3. If needed check balance /// > 4. If needed migrated processes and ports /// > 5. Do auxiliary scheduler work /// > 6. If needed check I/O and update time /// > 7. While needed pick a port task to execute /// > 8. Pick a process to execute /// > -- [The Scheduler Loop](https://blog.stenmans.org/theBeamBook/#_the_scheduler_loop) /// /// Returns `true` if a process was run. Returns `false` if no process could be run and the /// scheduler should sleep or work steal. #[must_use] pub fn run_once(&self) -> bool { // We always set root=true here, since calling this function is always done // from the scheduler loop, and only ever from the root context
} } /// What to run
random_line_split
client.go
0", "http_code": "202", "phone_numbers": [ "developer@email.com", "901-111-2222" ], "success": false } // Submit a valid email address or phone number from "phone_numbers" list res, err := user.Select2FA("developer@email.com") // MFA sent to developer@email.com res, err := user.VerifyPIN("123456") Set an `IDEMPOTENCY_KEY` (for `POST` requests only) scopeSettings := `{ "scope": [ "USERS|POST", "USER|PATCH", "NODES|POST", "NODE|PATCH", "TRANS|POST", "TRAN|PATCH" ], "url": "https://requestb.in/zp216zzp" }` idempotencyKey := `1234567890` data, err := client.CreateSubscription(scopeSettings, idempotencyKey) Submit optional query parameters params := "per_page=3&page=2" data, err := client.GetUsers(params) */ package synapse import ( "github.com/mitchellh/mapstructure" ) /********** GLOBAL VARIABLES **********/ var logMode = false var developerMode = true /********** TYPES **********/ type ( // Client represents the credentials used by the developer to instantiate a client Client struct { ClientID string ClientSecret string Fingerprint string IP string request Request } ) /********** METHODS **********/ func (c *Client) do(method, url, data string, queryParams []string) (map[string]interface{}, error) { var body []byte var err error switch method { case "GET": body, err = c.request.Get(url, queryParams) case "POST": body, err = c.request.Post(url, data, queryParams) case "PATCH": body, err = c.request.Patch(url, data, queryParams) case "DELETE": body, err = c.request.Delete(url) } return readStream(body), err } /********** CLIENT **********/ // New creates a client object func New(clientID, clientSecret, fingerprint, ipAddress string, modes ...bool) *Client { log.info("========== CREATING CLIENT INSTANCE ==========") if len(modes) > 0 { if modes[0] == true { logMode = true } if len(modes) > 1 && modes[1] == false { developerMode = false } } request := Request{ clientID: clientID, clientSecret: clientSecret, fingerprint: fingerprint, ipAddress: ipAddress, } return &Client{ ClientID: clientID, ClientSecret: clientSecret, Fingerprint: fingerprint, IP: ipAddress, request: request, } } /********** AUTHENTICATION **********/ // GetPublicKey returns a public key as a token representing client credentials func (c *Client) GetPublicKey(scope ...string) (map[string]interface{}, error) { log.info("========== GET PUBLIC KEY ==========") url := buildURL(path["client"]) defaultScope := "OAUTH|POST,USERS|POST,USERS|GET,USER|GET,USER|PATCH,SUBSCRIPTIONS|GET,SUBSCRIPTIONS|POST,SUBSCRIPTION|GET,SUBSCRIPTION|PATCH,CLIENT|REPORTS,CLIENT|CONTROLS" if len(scope) > 0 { defaultScope = scope[0] } qp := []string{"issue_public_key=YES&scope=" + defaultScope} if len(scope) > 1 { userId := scope[1] qp[0] += "&user_id=" + userId } return c.do("GET", url, "", qp) } /********** NODE **********/ // GetNodes returns all of the nodes func (c *Client) GetNodes(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT NODES ==========") url := buildURL(path["nodes"]) return c.do("GET", url, "", queryParams) } // GetTradeMarketData returns data on a stock based on its ticker symbol func (c *Client) GetTradeMarketData(tickerSymbol string) (map[string]interface{}, error) { log.info("========== GET TRADE MARKET DATA ==========") url := buildURL(path["nodes"], "trade-market-watch") ts := []string{tickerSymbol} return c.do("GET", url, "", ts) } // GetNodeTypes returns available node types func (c *Client) GetNodeTypes() (map[string]interface{}, error) { log.info("========== GET NODE TYPES ==========") url := buildURL(path["nodes"], "types")
return c.do("GET", url, "", nil) } /********** OTHER **********/ // GetCryptoMarketData returns market data for cryptocurrencies func (c *Client) GetCryptoMarketData() (map[string]interface{}, error) { log.info("========== GET CRYPTO MARKET DATA ==========") url := buildURL(path["nodes"], "crypto-market-watch") return c.do("GET", url, "", nil) } // GetCryptoQuotes returns all of the quotes for crypto currencies func (c *Client) GetCryptoQuotes(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CRYPTO QUOTES ==========") url := buildURL(path["nodes"], "crypto-quotes") return c.do("GET", url, "", queryParams) } // GetInstitutions returns a list of all available banking institutions func (c *Client) GetInstitutions() (map[string]interface{}, error) { log.info("========== GET INSTITUTIONS ==========") url := buildURL(path["institutions"]) return c.do("GET", url, "", nil) } // LocateATMs returns a list of nearby ATMs func (c *Client) LocateATMs(queryParams ...string) (map[string]interface{}, error) { log.info("========== LOCATE ATMS ==========") url := buildURL(path["nodes"], "atms") return c.do("GET", url, "", queryParams) } // VerifyAddress checks if an address if valid func (c *Client) VerifyAddress(data string) (map[string]interface{}, error) { log.info("========== VERIFY ADDRESS ==========") url := buildURL("address-verification") return c.do("POST", url, data, nil) } // VerifyRoutingNumber checks and returns the bank details of a routing number func (c *Client) VerifyRoutingNumber(data string) (map[string]interface{}, error) { log.info("========== VERIFY ROUTING NUMBER ==========") url := buildURL("routing-number-verification") return c.do("POST", url, data, nil) } /********** SUBSCRIPTION **********/ // GetSubscriptions returns all of the nodes associated with a user func (c *Client) GetSubscriptions(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET SUBSCRIPTIONS ==========") url := buildURL(path["subscriptions"]) return c.do("GET", url, "", queryParams) } // GetSubscription returns a single subscription func (c *Client) GetSubscription(subscriptionID string) (map[string]interface{}, error) { log.info("========== GET SUBSCRIPTION ==========") url := buildURL(path["subscriptions"], subscriptionID) return c.do("GET", url, "", nil) } // CreateSubscription creates a subscription and returns the subscription data func (c *Client) CreateSubscription(data string, idempotencyKey ...string) (map[string]interface{}, error) { log.info("========== CREATE SUBSCRIPTION ==========") url := buildURL(path["subscriptions"]) return c.do("POST", url, data, idempotencyKey) } // UpdateSubscription updates an existing subscription func (c *Client) UpdateSubscription(subscriptionID string, data string) (map[string]interface{}, error) { log.info("========== UPDATE SUBSCRIPTION ==========") url := buildURL(path["subscriptions"], subscriptionID) return c.do("PATCH", url, data, nil) } // GetWebhookLogs returns all of the webhooks sent to a specific client func (c *Client) GetWebhookLogs() (map[string]interface{}, error) { log.info("========== GET WEBHOOK LOGS ==========") url := buildURL(path["subscriptions"], "logs") return c.do("GET", url, "", nil) } /********** TRANSACTION **********/ // GetTransactions returns all client transactions func (c *Client) GetTransactions(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT TRANSACTIONS ==========") url := buildURL(path["transactions"]) return c.do("GET", url, "", queryParams) } /********** USER **********/ // GetUsers returns a list of users func (c *Client) GetUsers(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT USERS ==========") url := buildURL(path["users"]) return c.do("GET", url, "", queryParams) } // GetUser returns a single user func (c *Client) GetUser(userID, fingerprint, ipAddress string, queryParams ...string) (*User, error) { log.info("========== GET USER ==========") url := buildURL(path["users"], userID) res, err := c.do("GET", url, "", queryParams) var user User mapstructure.Decode(res, &user) user.Response = res request := Request
random_line_split
client.go
0", "http_code": "202", "phone_numbers": [ "developer@email.com", "901-111-2222" ], "success": false } // Submit a valid email address or phone number from "phone_numbers" list res, err := user.Select2FA("developer@email.com") // MFA sent to developer@email.com res, err := user.VerifyPIN("123456") Set an `IDEMPOTENCY_KEY` (for `POST` requests only) scopeSettings := `{ "scope": [ "USERS|POST", "USER|PATCH", "NODES|POST", "NODE|PATCH", "TRANS|POST", "TRAN|PATCH" ], "url": "https://requestb.in/zp216zzp" }` idempotencyKey := `1234567890` data, err := client.CreateSubscription(scopeSettings, idempotencyKey) Submit optional query parameters params := "per_page=3&page=2" data, err := client.GetUsers(params) */ package synapse import ( "github.com/mitchellh/mapstructure" ) /********** GLOBAL VARIABLES **********/ var logMode = false var developerMode = true /********** TYPES **********/ type ( // Client represents the credentials used by the developer to instantiate a client Client struct { ClientID string ClientSecret string Fingerprint string IP string request Request } ) /********** METHODS **********/ func (c *Client) do(method, url, data string, queryParams []string) (map[string]interface{}, error) { var body []byte var err error switch method { case "GET": body, err = c.request.Get(url, queryParams) case "POST": body, err = c.request.Post(url, data, queryParams) case "PATCH": body, err = c.request.Patch(url, data, queryParams) case "DELETE": body, err = c.request.Delete(url) } return readStream(body), err } /********** CLIENT **********/ // New creates a client object func New(clientID, clientSecret, fingerprint, ipAddress string, modes ...bool) *Client { log.info("========== CREATING CLIENT INSTANCE ==========") if len(modes) > 0 { if modes[0] == true { logMode = true } if len(modes) > 1 && modes[1] == false { developerMode = false } } request := Request{ clientID: clientID, clientSecret: clientSecret, fingerprint: fingerprint, ipAddress: ipAddress, } return &Client{ ClientID: clientID, ClientSecret: clientSecret, Fingerprint: fingerprint, IP: ipAddress, request: request, } } /********** AUTHENTICATION **********/ // GetPublicKey returns a public key as a token representing client credentials func (c *Client) GetPublicKey(scope ...string) (map[string]interface{}, error) { log.info("========== GET PUBLIC KEY ==========") url := buildURL(path["client"]) defaultScope := "OAUTH|POST,USERS|POST,USERS|GET,USER|GET,USER|PATCH,SUBSCRIPTIONS|GET,SUBSCRIPTIONS|POST,SUBSCRIPTION|GET,SUBSCRIPTION|PATCH,CLIENT|REPORTS,CLIENT|CONTROLS" if len(scope) > 0
qp := []string{"issue_public_key=YES&scope=" + defaultScope} if len(scope) > 1 { userId := scope[1] qp[0] += "&user_id=" + userId } return c.do("GET", url, "", qp) } /********** NODE **********/ // GetNodes returns all of the nodes func (c *Client) GetNodes(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT NODES ==========") url := buildURL(path["nodes"]) return c.do("GET", url, "", queryParams) } // GetTradeMarketData returns data on a stock based on its ticker symbol func (c *Client) GetTradeMarketData(tickerSymbol string) (map[string]interface{}, error) { log.info("========== GET TRADE MARKET DATA ==========") url := buildURL(path["nodes"], "trade-market-watch") ts := []string{tickerSymbol} return c.do("GET", url, "", ts) } // GetNodeTypes returns available node types func (c *Client) GetNodeTypes() (map[string]interface{}, error) { log.info("========== GET NODE TYPES ==========") url := buildURL(path["nodes"], "types") return c.do("GET", url, "", nil) } /********** OTHER **********/ // GetCryptoMarketData returns market data for cryptocurrencies func (c *Client) GetCryptoMarketData() (map[string]interface{}, error) { log.info("========== GET CRYPTO MARKET DATA ==========") url := buildURL(path["nodes"], "crypto-market-watch") return c.do("GET", url, "", nil) } // GetCryptoQuotes returns all of the quotes for crypto currencies func (c *Client) GetCryptoQuotes(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CRYPTO QUOTES ==========") url := buildURL(path["nodes"], "crypto-quotes") return c.do("GET", url, "", queryParams) } // GetInstitutions returns a list of all available banking institutions func (c *Client) GetInstitutions() (map[string]interface{}, error) { log.info("========== GET INSTITUTIONS ==========") url := buildURL(path["institutions"]) return c.do("GET", url, "", nil) } // LocateATMs returns a list of nearby ATMs func (c *Client) LocateATMs(queryParams ...string) (map[string]interface{}, error) { log.info("========== LOCATE ATMS ==========") url := buildURL(path["nodes"], "atms") return c.do("GET", url, "", queryParams) } // VerifyAddress checks if an address if valid func (c *Client) VerifyAddress(data string) (map[string]interface{}, error) { log.info("========== VERIFY ADDRESS ==========") url := buildURL("address-verification") return c.do("POST", url, data, nil) } // VerifyRoutingNumber checks and returns the bank details of a routing number func (c *Client) VerifyRoutingNumber(data string) (map[string]interface{}, error) { log.info("========== VERIFY ROUTING NUMBER ==========") url := buildURL("routing-number-verification") return c.do("POST", url, data, nil) } /********** SUBSCRIPTION **********/ // GetSubscriptions returns all of the nodes associated with a user func (c *Client) GetSubscriptions(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET SUBSCRIPTIONS ==========") url := buildURL(path["subscriptions"]) return c.do("GET", url, "", queryParams) } // GetSubscription returns a single subscription func (c *Client) GetSubscription(subscriptionID string) (map[string]interface{}, error) { log.info("========== GET SUBSCRIPTION ==========") url := buildURL(path["subscriptions"], subscriptionID) return c.do("GET", url, "", nil) } // CreateSubscription creates a subscription and returns the subscription data func (c *Client) CreateSubscription(data string, idempotencyKey ...string) (map[string]interface{}, error) { log.info("========== CREATE SUBSCRIPTION ==========") url := buildURL(path["subscriptions"]) return c.do("POST", url, data, idempotencyKey) } // UpdateSubscription updates an existing subscription func (c *Client) UpdateSubscription(subscriptionID string, data string) (map[string]interface{}, error) { log.info("========== UPDATE SUBSCRIPTION ==========") url := buildURL(path["subscriptions"], subscriptionID) return c.do("PATCH", url, data, nil) } // GetWebhookLogs returns all of the webhooks sent to a specific client func (c *Client) GetWebhookLogs() (map[string]interface{}, error) { log.info("========== GET WEBHOOK LOGS ==========") url := buildURL(path["subscriptions"], "logs") return c.do("GET", url, "", nil) } /********** TRANSACTION **********/ // GetTransactions returns all client transactions func (c *Client) GetTransactions(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT TRANSACTIONS ==========") url := buildURL(path["transactions"]) return c.do("GET", url, "", queryParams) } /********** USER **********/ // GetUsers returns a list of users func (c *Client) GetUsers(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT USERS ==========") url := buildURL(path["users"]) return c.do("GET", url, "", queryParams) } // GetUser returns a single user func (c *Client) GetUser(userID, fingerprint, ipAddress string, queryParams ...string) (*User, error) { log.info("========== GET USER ==========") url := buildURL(path["users"], userID) res, err := c.do("GET", url, "", queryParams) var user User mapstructure.Decode(res, &user) user.Response = res request :=
{ defaultScope = scope[0] }
conditional_block
client.go
0", "http_code": "202", "phone_numbers": [ "developer@email.com", "901-111-2222" ], "success": false } // Submit a valid email address or phone number from "phone_numbers" list res, err := user.Select2FA("developer@email.com") // MFA sent to developer@email.com res, err := user.VerifyPIN("123456") Set an `IDEMPOTENCY_KEY` (for `POST` requests only) scopeSettings := `{ "scope": [ "USERS|POST", "USER|PATCH", "NODES|POST", "NODE|PATCH", "TRANS|POST", "TRAN|PATCH" ], "url": "https://requestb.in/zp216zzp" }` idempotencyKey := `1234567890` data, err := client.CreateSubscription(scopeSettings, idempotencyKey) Submit optional query parameters params := "per_page=3&page=2" data, err := client.GetUsers(params) */ package synapse import ( "github.com/mitchellh/mapstructure" ) /********** GLOBAL VARIABLES **********/ var logMode = false var developerMode = true /********** TYPES **********/ type ( // Client represents the credentials used by the developer to instantiate a client Client struct { ClientID string ClientSecret string Fingerprint string IP string request Request } ) /********** METHODS **********/ func (c *Client) do(method, url, data string, queryParams []string) (map[string]interface{}, error) { var body []byte var err error switch method { case "GET": body, err = c.request.Get(url, queryParams) case "POST": body, err = c.request.Post(url, data, queryParams) case "PATCH": body, err = c.request.Patch(url, data, queryParams) case "DELETE": body, err = c.request.Delete(url) } return readStream(body), err } /********** CLIENT **********/ // New creates a client object func New(clientID, clientSecret, fingerprint, ipAddress string, modes ...bool) *Client { log.info("========== CREATING CLIENT INSTANCE ==========") if len(modes) > 0 { if modes[0] == true { logMode = true } if len(modes) > 1 && modes[1] == false { developerMode = false } } request := Request{ clientID: clientID, clientSecret: clientSecret, fingerprint: fingerprint, ipAddress: ipAddress, } return &Client{ ClientID: clientID, ClientSecret: clientSecret, Fingerprint: fingerprint, IP: ipAddress, request: request, } } /********** AUTHENTICATION **********/ // GetPublicKey returns a public key as a token representing client credentials func (c *Client) GetPublicKey(scope ...string) (map[string]interface{}, error) { log.info("========== GET PUBLIC KEY ==========") url := buildURL(path["client"]) defaultScope := "OAUTH|POST,USERS|POST,USERS|GET,USER|GET,USER|PATCH,SUBSCRIPTIONS|GET,SUBSCRIPTIONS|POST,SUBSCRIPTION|GET,SUBSCRIPTION|PATCH,CLIENT|REPORTS,CLIENT|CONTROLS" if len(scope) > 0 { defaultScope = scope[0] } qp := []string{"issue_public_key=YES&scope=" + defaultScope} if len(scope) > 1 { userId := scope[1] qp[0] += "&user_id=" + userId } return c.do("GET", url, "", qp) } /********** NODE **********/ // GetNodes returns all of the nodes func (c *Client) GetNodes(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT NODES ==========") url := buildURL(path["nodes"]) return c.do("GET", url, "", queryParams) } // GetTradeMarketData returns data on a stock based on its ticker symbol func (c *Client) GetTradeMarketData(tickerSymbol string) (map[string]interface{}, error) { log.info("========== GET TRADE MARKET DATA ==========") url := buildURL(path["nodes"], "trade-market-watch") ts := []string{tickerSymbol} return c.do("GET", url, "", ts) } // GetNodeTypes returns available node types func (c *Client) GetNodeTypes() (map[string]interface{}, error) { log.info("========== GET NODE TYPES ==========") url := buildURL(path["nodes"], "types") return c.do("GET", url, "", nil) } /********** OTHER **********/ // GetCryptoMarketData returns market data for cryptocurrencies func (c *Client) GetCryptoMarketData() (map[string]interface{}, error) { log.info("========== GET CRYPTO MARKET DATA ==========") url := buildURL(path["nodes"], "crypto-market-watch") return c.do("GET", url, "", nil) } // GetCryptoQuotes returns all of the quotes for crypto currencies func (c *Client) GetCryptoQuotes(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CRYPTO QUOTES ==========") url := buildURL(path["nodes"], "crypto-quotes") return c.do("GET", url, "", queryParams) } // GetInstitutions returns a list of all available banking institutions func (c *Client) GetInstitutions() (map[string]interface{}, error) { log.info("========== GET INSTITUTIONS ==========") url := buildURL(path["institutions"]) return c.do("GET", url, "", nil) } // LocateATMs returns a list of nearby ATMs func (c *Client) LocateATMs(queryParams ...string) (map[string]interface{}, error) { log.info("========== LOCATE ATMS ==========") url := buildURL(path["nodes"], "atms") return c.do("GET", url, "", queryParams) } // VerifyAddress checks if an address if valid func (c *Client) VerifyAddress(data string) (map[string]interface{}, error) { log.info("========== VERIFY ADDRESS ==========") url := buildURL("address-verification") return c.do("POST", url, data, nil) } // VerifyRoutingNumber checks and returns the bank details of a routing number func (c *Client) VerifyRoutingNumber(data string) (map[string]interface{}, error) { log.info("========== VERIFY ROUTING NUMBER ==========") url := buildURL("routing-number-verification") return c.do("POST", url, data, nil) } /********** SUBSCRIPTION **********/ // GetSubscriptions returns all of the nodes associated with a user func (c *Client) GetSubscriptions(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET SUBSCRIPTIONS ==========") url := buildURL(path["subscriptions"]) return c.do("GET", url, "", queryParams) } // GetSubscription returns a single subscription func (c *Client) GetSubscription(subscriptionID string) (map[string]interface{}, error) { log.info("========== GET SUBSCRIPTION ==========") url := buildURL(path["subscriptions"], subscriptionID) return c.do("GET", url, "", nil) } // CreateSubscription creates a subscription and returns the subscription data func (c *Client) CreateSubscription(data string, idempotencyKey ...string) (map[string]interface{}, error) { log.info("========== CREATE SUBSCRIPTION ==========") url := buildURL(path["subscriptions"]) return c.do("POST", url, data, idempotencyKey) } // UpdateSubscription updates an existing subscription func (c *Client)
(subscriptionID string, data string) (map[string]interface{}, error) { log.info("========== UPDATE SUBSCRIPTION ==========") url := buildURL(path["subscriptions"], subscriptionID) return c.do("PATCH", url, data, nil) } // GetWebhookLogs returns all of the webhooks sent to a specific client func (c *Client) GetWebhookLogs() (map[string]interface{}, error) { log.info("========== GET WEBHOOK LOGS ==========") url := buildURL(path["subscriptions"], "logs") return c.do("GET", url, "", nil) } /********** TRANSACTION **********/ // GetTransactions returns all client transactions func (c *Client) GetTransactions(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT TRANSACTIONS ==========") url := buildURL(path["transactions"]) return c.do("GET", url, "", queryParams) } /********** USER **********/ // GetUsers returns a list of users func (c *Client) GetUsers(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT USERS ==========") url := buildURL(path["users"]) return c.do("GET", url, "", queryParams) } // GetUser returns a single user func (c *Client) GetUser(userID, fingerprint, ipAddress string, queryParams ...string) (*User, error) { log.info("========== GET USER ==========") url := buildURL(path["users"], userID) res, err := c.do("GET", url, "", queryParams) var user User mapstructure.Decode(res, &user) user.Response = res request := Request
UpdateSubscription
identifier_name
client.go
0", "http_code": "202", "phone_numbers": [ "developer@email.com", "901-111-2222" ], "success": false } // Submit a valid email address or phone number from "phone_numbers" list res, err := user.Select2FA("developer@email.com") // MFA sent to developer@email.com res, err := user.VerifyPIN("123456") Set an `IDEMPOTENCY_KEY` (for `POST` requests only) scopeSettings := `{ "scope": [ "USERS|POST", "USER|PATCH", "NODES|POST", "NODE|PATCH", "TRANS|POST", "TRAN|PATCH" ], "url": "https://requestb.in/zp216zzp" }` idempotencyKey := `1234567890` data, err := client.CreateSubscription(scopeSettings, idempotencyKey) Submit optional query parameters params := "per_page=3&page=2" data, err := client.GetUsers(params) */ package synapse import ( "github.com/mitchellh/mapstructure" ) /********** GLOBAL VARIABLES **********/ var logMode = false var developerMode = true /********** TYPES **********/ type ( // Client represents the credentials used by the developer to instantiate a client Client struct { ClientID string ClientSecret string Fingerprint string IP string request Request } ) /********** METHODS **********/ func (c *Client) do(method, url, data string, queryParams []string) (map[string]interface{}, error) { var body []byte var err error switch method { case "GET": body, err = c.request.Get(url, queryParams) case "POST": body, err = c.request.Post(url, data, queryParams) case "PATCH": body, err = c.request.Patch(url, data, queryParams) case "DELETE": body, err = c.request.Delete(url) } return readStream(body), err } /********** CLIENT **********/ // New creates a client object func New(clientID, clientSecret, fingerprint, ipAddress string, modes ...bool) *Client { log.info("========== CREATING CLIENT INSTANCE ==========") if len(modes) > 0 { if modes[0] == true { logMode = true } if len(modes) > 1 && modes[1] == false { developerMode = false } } request := Request{ clientID: clientID, clientSecret: clientSecret, fingerprint: fingerprint, ipAddress: ipAddress, } return &Client{ ClientID: clientID, ClientSecret: clientSecret, Fingerprint: fingerprint, IP: ipAddress, request: request, } } /********** AUTHENTICATION **********/ // GetPublicKey returns a public key as a token representing client credentials func (c *Client) GetPublicKey(scope ...string) (map[string]interface{}, error) { log.info("========== GET PUBLIC KEY ==========") url := buildURL(path["client"]) defaultScope := "OAUTH|POST,USERS|POST,USERS|GET,USER|GET,USER|PATCH,SUBSCRIPTIONS|GET,SUBSCRIPTIONS|POST,SUBSCRIPTION|GET,SUBSCRIPTION|PATCH,CLIENT|REPORTS,CLIENT|CONTROLS" if len(scope) > 0 { defaultScope = scope[0] } qp := []string{"issue_public_key=YES&scope=" + defaultScope} if len(scope) > 1 { userId := scope[1] qp[0] += "&user_id=" + userId } return c.do("GET", url, "", qp) } /********** NODE **********/ // GetNodes returns all of the nodes func (c *Client) GetNodes(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT NODES ==========") url := buildURL(path["nodes"]) return c.do("GET", url, "", queryParams) } // GetTradeMarketData returns data on a stock based on its ticker symbol func (c *Client) GetTradeMarketData(tickerSymbol string) (map[string]interface{}, error) { log.info("========== GET TRADE MARKET DATA ==========") url := buildURL(path["nodes"], "trade-market-watch") ts := []string{tickerSymbol} return c.do("GET", url, "", ts) } // GetNodeTypes returns available node types func (c *Client) GetNodeTypes() (map[string]interface{}, error) { log.info("========== GET NODE TYPES ==========") url := buildURL(path["nodes"], "types") return c.do("GET", url, "", nil) } /********** OTHER **********/ // GetCryptoMarketData returns market data for cryptocurrencies func (c *Client) GetCryptoMarketData() (map[string]interface{}, error) { log.info("========== GET CRYPTO MARKET DATA ==========") url := buildURL(path["nodes"], "crypto-market-watch") return c.do("GET", url, "", nil) } // GetCryptoQuotes returns all of the quotes for crypto currencies func (c *Client) GetCryptoQuotes(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CRYPTO QUOTES ==========") url := buildURL(path["nodes"], "crypto-quotes") return c.do("GET", url, "", queryParams) } // GetInstitutions returns a list of all available banking institutions func (c *Client) GetInstitutions() (map[string]interface{}, error) { log.info("========== GET INSTITUTIONS ==========") url := buildURL(path["institutions"]) return c.do("GET", url, "", nil) } // LocateATMs returns a list of nearby ATMs func (c *Client) LocateATMs(queryParams ...string) (map[string]interface{}, error) { log.info("========== LOCATE ATMS ==========") url := buildURL(path["nodes"], "atms") return c.do("GET", url, "", queryParams) } // VerifyAddress checks if an address if valid func (c *Client) VerifyAddress(data string) (map[string]interface{}, error) { log.info("========== VERIFY ADDRESS ==========") url := buildURL("address-verification") return c.do("POST", url, data, nil) } // VerifyRoutingNumber checks and returns the bank details of a routing number func (c *Client) VerifyRoutingNumber(data string) (map[string]interface{}, error)
/********** SUBSCRIPTION **********/ // GetSubscriptions returns all of the nodes associated with a user func (c *Client) GetSubscriptions(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET SUBSCRIPTIONS ==========") url := buildURL(path["subscriptions"]) return c.do("GET", url, "", queryParams) } // GetSubscription returns a single subscription func (c *Client) GetSubscription(subscriptionID string) (map[string]interface{}, error) { log.info("========== GET SUBSCRIPTION ==========") url := buildURL(path["subscriptions"], subscriptionID) return c.do("GET", url, "", nil) } // CreateSubscription creates a subscription and returns the subscription data func (c *Client) CreateSubscription(data string, idempotencyKey ...string) (map[string]interface{}, error) { log.info("========== CREATE SUBSCRIPTION ==========") url := buildURL(path["subscriptions"]) return c.do("POST", url, data, idempotencyKey) } // UpdateSubscription updates an existing subscription func (c *Client) UpdateSubscription(subscriptionID string, data string) (map[string]interface{}, error) { log.info("========== UPDATE SUBSCRIPTION ==========") url := buildURL(path["subscriptions"], subscriptionID) return c.do("PATCH", url, data, nil) } // GetWebhookLogs returns all of the webhooks sent to a specific client func (c *Client) GetWebhookLogs() (map[string]interface{}, error) { log.info("========== GET WEBHOOK LOGS ==========") url := buildURL(path["subscriptions"], "logs") return c.do("GET", url, "", nil) } /********** TRANSACTION **********/ // GetTransactions returns all client transactions func (c *Client) GetTransactions(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT TRANSACTIONS ==========") url := buildURL(path["transactions"]) return c.do("GET", url, "", queryParams) } /********** USER **********/ // GetUsers returns a list of users func (c *Client) GetUsers(queryParams ...string) (map[string]interface{}, error) { log.info("========== GET CLIENT USERS ==========") url := buildURL(path["users"]) return c.do("GET", url, "", queryParams) } // GetUser returns a single user func (c *Client) GetUser(userID, fingerprint, ipAddress string, queryParams ...string) (*User, error) { log.info("========== GET USER ==========") url := buildURL(path["users"], userID) res, err := c.do("GET", url, "", queryParams) var user User mapstructure.Decode(res, &user) user.Response = res request :=
{ log.info("========== VERIFY ROUTING NUMBER ==========") url := buildURL("routing-number-verification") return c.do("POST", url, data, nil) }
identifier_body
controller.go
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package operator import ( "context" "errors" "fmt" "reflect" "strings" "sync" "github.com/google/uuid" "github.com/sirupsen/logrus" coreV1 "k8s.io/api/core/v1" k8sError "k8s.io/apimachinery/pkg/api/errors" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/source" api "github.com/dell/csi-baremetal/api/generated/v1" "github.com/dell/csi-baremetal/api/v1/nodecrd" "github.com/dell/csi-baremetal/pkg/base/k8s" "github.com/dell/csi-baremetal/pkg/base/util" observer "github.com/dell/csi-baremetal/pkg/common" "github.com/dell/csi-baremetal/pkg/crcontrollers/operator/common" ) const ( // namePrefix it is a prefix for Node CR name namePrefix = "csibmnode-" // finalizer for Node custom resource csibmNodeFinalizer = "dell.emc.csi/csibmnode-cleanup" ) // Controller is a controller for Node CR type Controller struct { k8sClient *k8s.KubeClient nodeSelector *label cache nodesMapping // holds k8s node names for which special ID settings is enabled, // it is used in Node CR deletion for avoiding recreation enabledForNode map[string]bool enabledMu sync.RWMutex observer observer.Observer log *logrus.Entry // if used external annotations externalAnnotation bool // holds annotation which contains node UUID annotationKey string } type label struct { key string value string } // nodesMapping it is not a thread safety cache that holds mapping between names for k8s node and BMCSINode CR objects type nodesMapping struct { k8sToBMNode map[string]string // k8s node name to Node CR name bmToK8sNode map[string]string // Node CR name to k8s node name } func (nc *nodesMapping) getK8sNodeName(bmNodeName string) (string, bool) { res, ok := nc.bmToK8sNode[bmNodeName] return res, ok } func (nc *nodesMapping) getCSIBMNodeName(k8sNodeName string) (string, bool) { res, ok := nc.k8sToBMNode[k8sNodeName] return res, ok } func (nc *nodesMapping) put(k8sNodeName, bmNodeName string) { nc.k8sToBMNode[k8sNodeName] = bmNodeName nc.bmToK8sNode[bmNodeName] = k8sNodeName } // NewController returns instance of Controller func NewController(nodeSelector string, useExternalAnnotaion bool, nodeAnnotaion string, k8sClient *k8s.KubeClient, observer observer.Observer, logger *logrus.Logger) (*Controller, error) { c := &Controller{ k8sClient: k8sClient, cache: nodesMapping{ k8sToBMNode: make(map[string]string), bmToK8sNode: make(map[string]string), }, observer: observer, enabledForNode: make(map[string]bool, 3), // a little optimization, if cluster has 3 worker nodes this map won't be extended log: logger.WithField("component", "Controller"), externalAnnotation: useExternalAnnotaion, } if nodeSelector != "" { splitted := strings.Split(nodeSelector, ":") if len(splitted) != 2 { return nil, fmt.Errorf("unable to parse nodeSelector %s", nodeSelector) } c.nodeSelector = &label{key: splitted[0], value: splitted[1]} c.log.Infof("Controller will be working with nodes that matched next selector: %v", c.nodeSelector) } if c.externalAnnotation { c.annotationKey = nodeAnnotaion c.log.Infof("External annotation feature is enabled. Annotation: %s", c.annotationKey) } else { c.annotationKey = common.DeafultNodeIDAnnotationKey c.log.Infof("External annotation feature is disabled. Annotation: %s", c.annotationKey) } return c, nil } func (bmc *Controller) enableForNode(nodeName string) { bmc.enabledMu.Lock() bmc.enabledForNode[nodeName] = true bmc.enabledMu.Unlock() } func (bmc *Controller) disableForNode(nodeName string) { bmc.enabledMu.Lock() bmc.enabledForNode[nodeName] = false bmc.enabledMu.Unlock() } func (bmc *Controller) isEnabledForNode(nodeName string) bool { var enabled, ok bool bmc.enabledMu.RLock() defer bmc.enabledMu.RUnlock() if enabled, ok = bmc.enabledForNode[nodeName]; !ok { return false } return enabled } func (bmc *Controller) isMatchSelector(k8sNode *coreV1.Node) bool { if bmc.nodeSelector == nil { return true } val, ok := k8sNode.GetLabels()[bmc.nodeSelector.key] matched := ok && val == bmc.nodeSelector.value bmc.log.WithField("method", "isMatchSelector"). Debugf("Node %s matches node selector %v: %v", k8sNode.Name, bmc.nodeSelector, matched) return matched } // SetupWithManager registers Controller to k8s controller manager func (bmc *Controller) SetupWithManager(m ctrl.Manager) error { return ctrl.NewControllerManagedBy(m). For(&nodecrd.Node{}). // primary resource WithOptions(controller.Options{ MaxConcurrentReconciles: 1, // reconcile all object by turn, concurrent reconciliation isn't supported }). Watches(&source.Kind{Type: &coreV1.Node{}}, &handler.EnqueueRequestForObject{}). // secondary resource WithEventFilter(predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { if _, ok := e.Object.(*nodecrd.Node); ok { return true } k8sNode, ok := e.Object.(*coreV1.Node) if !ok || !bmc.isMatchSelector(k8sNode) { return false } bmc.enableForNode(k8sNode.Name) return true }, UpdateFunc: func(e event.UpdateEvent) bool { if _, ok := e.ObjectOld.(*nodecrd.Node); ok { return true } nodeOld, ok := e.ObjectOld.(*coreV1.Node) if !ok { return false } nodeNew := e.ObjectNew.(*coreV1.Node) if !bmc.isMatchSelector(nodeNew) { return false } if !bmc.isEnabledForNode(nodeNew.Name) { bmc.enableForNode(nodeNew.Name) } annotationAreTheSame := reflect.DeepEqual(nodeOld.GetAnnotations(), nodeNew.GetAnnotations()) addressesAreTheSame := reflect.DeepEqual(nodeOld.Status.Addresses, nodeNew.Status.Addresses) labelsAreTheSame := bmc.nodeSelector == nil || reflect.DeepEqual(nodeOld.GetLabels(), nodeNew.GetLabels()) return !annotationAreTheSame || !addressesAreTheSame || !labelsAreTheSame }, }). Complete(bmc) } // Reconcile reconciles Node CR and k8s Node objects // at first define for which object current Reconcile is triggered and then run corresponding reconciliation method func (bmc *Controller) Reconcile(req ctrl.Request) (ctrl.Result, error) { ll := bmc.log.WithFields(logrus.Fields{ "method": "Reconcile", "name": req.Name, }) var err error // if name in request doesn't start with namePrefix controller tries to read k8s node object at first // however if it get NotFound error it tries to read Node object as well if !strings.HasPrefix(req.Name, namePrefix) { k8sNode := new(coreV1.Node) err = bmc.k8sClient.ReadCR(context.Background(), req.Name, "", k8sNode) switch { case err == nil: ll.Infof("Reconcile k8s node %s", k8sNode.Name) return bmc.reconcileForK8sNode(k8sNode
/* Copyright © 2020 Dell Inc. or its subsidiaries. All Rights Reserved.
random_line_split
controller.go
func (nc *nodesMapping) getCSIBMNodeName(k8sNodeName string) (string, bool) { res, ok := nc.k8sToBMNode[k8sNodeName] return res, ok } func (nc *nodesMapping) put(k8sNodeName, bmNodeName string) { nc.k8sToBMNode[k8sNodeName] = bmNodeName nc.bmToK8sNode[bmNodeName] = k8sNodeName } // NewController returns instance of Controller func NewController(nodeSelector string, useExternalAnnotaion bool, nodeAnnotaion string, k8sClient *k8s.KubeClient, observer observer.Observer, logger *logrus.Logger) (*Controller, error) { c := &Controller{ k8sClient: k8sClient, cache: nodesMapping{ k8sToBMNode: make(map[string]string), bmToK8sNode: make(map[string]string), }, observer: observer, enabledForNode: make(map[string]bool, 3), // a little optimization, if cluster has 3 worker nodes this map won't be extended log: logger.WithField("component", "Controller"), externalAnnotation: useExternalAnnotaion, } if nodeSelector != "" { splitted := strings.Split(nodeSelector, ":") if len(splitted) != 2 { return nil, fmt.Errorf("unable to parse nodeSelector %s", nodeSelector) } c.nodeSelector = &label{key: splitted[0], value: splitted[1]} c.log.Infof("Controller will be working with nodes that matched next selector: %v", c.nodeSelector) } if c.externalAnnotation { c.annotationKey = nodeAnnotaion c.log.Infof("External annotation feature is enabled. Annotation: %s", c.annotationKey) } else { c.annotationKey = common.DeafultNodeIDAnnotationKey c.log.Infof("External annotation feature is disabled. Annotation: %s", c.annotationKey) } return c, nil } func (bmc *Controller) enableForNode(nodeName string) { bmc.enabledMu.Lock() bmc.enabledForNode[nodeName] = true bmc.enabledMu.Unlock() } func (bmc *Controller) disableForNode(nodeName string) { bmc.enabledMu.Lock() bmc.enabledForNode[nodeName] = false bmc.enabledMu.Unlock() } func (bmc *Controller) isEnabledForNode(nodeName string) bool { var enabled, ok bool bmc.enabledMu.RLock() defer bmc.enabledMu.RUnlock() if enabled, ok = bmc.enabledForNode[nodeName]; !ok { return false } return enabled } func (bmc *Controller) isMatchSelector(k8sNode *coreV1.Node) bool { if bmc.nodeSelector == nil { return true } val, ok := k8sNode.GetLabels()[bmc.nodeSelector.key] matched := ok && val == bmc.nodeSelector.value bmc.log.WithField("method", "isMatchSelector"). Debugf("Node %s matches node selector %v: %v", k8sNode.Name, bmc.nodeSelector, matched) return matched } // SetupWithManager registers Controller to k8s controller manager func (bmc *Controller) SetupWithManager(m ctrl.Manager) error { return ctrl.NewControllerManagedBy(m). For(&nodecrd.Node{}). // primary resource WithOptions(controller.Options{ MaxConcurrentReconciles: 1, // reconcile all object by turn, concurrent reconciliation isn't supported }). Watches(&source.Kind{Type: &coreV1.Node{}}, &handler.EnqueueRequestForObject{}). // secondary resource WithEventFilter(predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { if _, ok := e.Object.(*nodecrd.Node); ok { return true } k8sNode, ok := e.Object.(*coreV1.Node) if !ok || !bmc.isMatchSelector(k8sNode) { return false } bmc.enableForNode(k8sNode.Name) return true }, UpdateFunc: func(e event.UpdateEvent) bool { if _, ok := e.ObjectOld.(*nodecrd.Node); ok { return true } nodeOld, ok := e.ObjectOld.(*coreV1.Node) if !ok { return false } nodeNew := e.ObjectNew.(*coreV1.Node) if !bmc.isMatchSelector(nodeNew) { return false } if !bmc.isEnabledForNode(nodeNew.Name) { bmc.enableForNode(nodeNew.Name) } annotationAreTheSame := reflect.DeepEqual(nodeOld.GetAnnotations(), nodeNew.GetAnnotations()) addressesAreTheSame := reflect.DeepEqual(nodeOld.Status.Addresses, nodeNew.Status.Addresses) labelsAreTheSame := bmc.nodeSelector == nil || reflect.DeepEqual(nodeOld.GetLabels(), nodeNew.GetLabels()) return !annotationAreTheSame || !addressesAreTheSame || !labelsAreTheSame }, }). Complete(bmc) } // Reconcile reconciles Node CR and k8s Node objects // at first define for which object current Reconcile is triggered and then run corresponding reconciliation method func (bmc *Controller) Reconcile(req ctrl.Request) (ctrl.Result, error) { ll := bmc.log.WithFields(logrus.Fields{ "method": "Reconcile", "name": req.Name, }) var err error // if name in request doesn't start with namePrefix controller tries to read k8s node object at first // however if it get NotFound error it tries to read Node object as well if !strings.HasPrefix(req.Name, namePrefix) { k8sNode := new(coreV1.Node) err = bmc.k8sClient.ReadCR(context.Background(), req.Name, "", k8sNode) switch { case err == nil: ll.Infof("Reconcile k8s node %s", k8sNode.Name) return bmc.reconcileForK8sNode(k8sNode) case !k8sError.IsNotFound(err): ll.Errorf("Unable to read node object: %v", err) return ctrl.Result{Requeue: true}, err } } // try to read Node bmNode := new(nodecrd.Node) err = bmc.k8sClient.ReadCR(context.Background(), req.Name, "", bmNode) switch { case err == nil: ll.Infof("Reconcile Node %s", bmNode.Name) return bmc.reconcileForCSIBMNode(bmNode) case !k8sError.IsNotFound(err): ll.Errorf("Unable to read Node object: %v", err) return ctrl.Result{Requeue: true}, err } ll.Warnf("unable to detect for which object (%s) that reconcile is. The object may have been deleted", req.String()) return ctrl.Result{}, nil } func (bmc *Controller) reconcileForK8sNode(k8sNode *coreV1.Node) (ctrl.Result, error) { ll := bmc.log.WithFields(logrus.Fields{ "method": "reconcileForK8sNode", "name": k8sNode.Name, }) if len(k8sNode.Status.Addresses) == 0 { err := errors.New("addresses are missing for current k8s node instance") ll.Error(err) return ctrl.Result{Requeue: false}, err } var ( bmNode = &nodecrd.Node{} bmNodeFromCache bool bmNodeName string bmNodes []nodecrd.Node ) // get corresponding Node CR name from cache if bmNodeName, bmNodeFromCache = bmc.cache.getCSIBMNodeName(k8sNode.Name); bmNodeFromCache { if err := bmc.k8sClient.ReadCR(context.Background(), bmNodeName, "", bmNode); err != nil { ll.Errorf("Unable to read Node %s: %v", bmNodeName, err) return ctrl.Result{Requeue: true}, err } bmNodes = []nodecrd.Node{*bmNode} } if !bmNodeFromCache { bmNodeCRs := new(nodecrd.NodeList) if err := bmc.k8sClient.ReadList(context.Background(), bmNodeCRs); err != nil { ll.Errorf("Unable to read Node CRs list: %v", err) return ctrl.Result{Requeue: true}, err } bmNodes = bmNodeCRs.Items } matchedCRs := make([]string, 0) for i := range bmNodes { matchedAddresses := bmc.matchedAddressesCount(&bmNodes[i], k8sNode) if len(bmNodes[i].Spec.Addresses) > 0 && matchedAddresses == len(bmNodes[i].Spec.Addresses) { bmNode = &bmNodes[i] matchedCRs = append(matchedCRs, bm
res, ok := nc.bmToK8sNode[bmNodeName] return res, ok }
identifier_body
controller.go
.log.WithFields(logrus.Fields{ "method": "reconcileForK8sNode", "name": k8sNode.Name, }) if len(k8sNode.Status.Addresses) == 0 { err := errors.New("addresses are missing for current k8s node instance") ll.Error(err) return ctrl.Result{Requeue: false}, err } var ( bmNode = &nodecrd.Node{} bmNodeFromCache bool bmNodeName string bmNodes []nodecrd.Node ) // get corresponding Node CR name from cache if bmNodeName, bmNodeFromCache = bmc.cache.getCSIBMNodeName(k8sNode.Name); bmNodeFromCache { if err := bmc.k8sClient.ReadCR(context.Background(), bmNodeName, "", bmNode); err != nil { ll.Errorf("Unable to read Node %s: %v", bmNodeName, err) return ctrl.Result{Requeue: true}, err } bmNodes = []nodecrd.Node{*bmNode} } if !bmNodeFromCache { bmNodeCRs := new(nodecrd.NodeList) if err := bmc.k8sClient.ReadList(context.Background(), bmNodeCRs); err != nil { ll.Errorf("Unable to read Node CRs list: %v", err) return ctrl.Result{Requeue: true}, err } bmNodes = bmNodeCRs.Items } matchedCRs := make([]string, 0) for i := range bmNodes { matchedAddresses := bmc.matchedAddressesCount(&bmNodes[i], k8sNode) if len(bmNodes[i].Spec.Addresses) > 0 && matchedAddresses == len(bmNodes[i].Spec.Addresses) { bmNode = &bmNodes[i] matchedCRs = append(matchedCRs, bmNode.Name) continue } if matchedAddresses > 0 { ll.Errorf("There is Node %s that partially match k8s node %s. Node.Spec: %v, k8s node addresses: %v. "+ "Node Spec should be edited to match exactly one kubernetes node", bmNodes[i].Name, k8sNode.Name, bmNodes[i].Spec, k8sNode.Status.Addresses) return ctrl.Result{}, nil } } if len(matchedCRs) > 1 { ll.Errorf("More then one Node CR corresponds to the current k8s node (%d). Matched Node CRs: %v", len(matchedCRs), matchedCRs) return ctrl.Result{}, nil } // create Node CR if len(matchedCRs) == 0 { id := bmc.constructNodeID(k8sNode) bmNodeName := namePrefix + id bmNode = bmc.k8sClient.ConstructCSIBMNodeCR(bmNodeName, api.Node{ UUID: id, Addresses: bmc.constructAddresses(k8sNode), }) bmNode.Finalizers = []string{csibmNodeFinalizer} if err := bmc.k8sClient.CreateCR(context.Background(), bmNodeName, bmNode); err != nil { ll.Errorf("Unable to create Node CR: %v", err) return ctrl.Result{Requeue: true}, err } } bmc.cache.put(k8sNode.Name, bmNode.Name) return bmc.updateNodeLabelsAndAnnotation(k8sNode, bmNode.Spec.UUID) } func (bmc *Controller) reconcileForCSIBMNode(bmNode *nodecrd.Node) (ctrl.Result, error) { ll := bmc.log.WithFields(logrus.Fields{ "method": "reconcileForCSIBMNode", "name": bmNode.Name, }) if len(bmNode.Spec.Addresses) == 0 { err := errors.New("addresses are missing for current Node instance") ll.Error(err) return ctrl.Result{Requeue: false}, err } var ( k8sNode = &coreV1.Node{} k8sNodes []coreV1.Node k8sNodeFromCache bool ) // get corresponding k8s node name from cache if k8sNodeName, k8sNodeFromCache := bmc.cache.getK8sNodeName(bmNode.Name); k8sNodeFromCache { if err := bmc.k8sClient.ReadCR(context.Background(), k8sNodeName, "", k8sNode); err != nil { ll.Errorf("Unable to read k8s node %s: %v", k8sNodeName, err) return ctrl.Result{Requeue: true}, err } k8sNodes = []coreV1.Node{*k8sNode} } if !k8sNodeFromCache { k8sNodeCRs := new(coreV1.NodeList) if err := bmc.k8sClient.ReadList(context.Background(), k8sNodeCRs); err != nil { ll.Errorf("Unable to read k8s nodes list: %v", err) return ctrl.Result{Requeue: true}, err } k8sNodes = k8sNodeCRs.Items } matchedNodes := make([]string, 0) for i := range k8sNodes { matchedAddresses := bmc.matchedAddressesCount(bmNode, &k8sNodes[i]) if matchedAddresses == len(bmNode.Spec.Addresses) { k8sNode = &k8sNodes[i] matchedNodes = append(matchedNodes, k8sNode.Name) continue } if matchedAddresses > 0 { ll.Errorf("There is k8s node %s that partially match Node CR %s. Node.Spec: %v, k8s node addresses: %v", k8sNodes[i].Name, bmNode.Name, bmNode.Spec, k8sNodes[i].Status.Addresses) return ctrl.Result{}, nil } } if !bmNode.GetDeletionTimestamp().IsZero() { bmc.disableForNode(k8sNode.Name) if err := bmc.removeLabelsAndAnnotation(k8sNode); err != nil { ll.Errorf("Unable to remove annotations or labels from node %s: %v", k8sNode.Name, err) bmc.enableForNode(k8sNode.Name) return ctrl.Result{Requeue: true}, err } ll.Infof("Annotations and labels from node %s was removed. Removing finalizer from %s.", k8sNode.Name, bmNode.Name) bmNode.Finalizers = nil err := bmc.k8sClient.UpdateCR(context.Background(), bmNode) if err != nil { ll.Errorf("Unable to update Node %s: %v", bmNode.Name, err) } return ctrl.Result{}, err } if len(matchedNodes) == 1 { bmc.cache.put(k8sNode.Name, bmNode.Name) return bmc.updateNodeLabelsAndAnnotation(k8sNode, bmNode.Spec.UUID) } ll.Warnf("Unable to detect k8s node that corresponds to Node %v, matched nodes: %v", bmNode, matchedNodes) return ctrl.Result{}, nil } // updateNodeLabelsAndAnnotation checks nodeIDAnnotationKey annotation value for provided k8s Node and compare that value with goalValue // parses OS Image info and put/update os-name and os-version labels if needed func (bmc *Controller) updateNodeLabelsAndAnnotation(k8sNode *coreV1.Node, nodeUUID string) (ctrl.Result, error) { ll := bmc.log.WithField("method", "updateNodeLabelsAndAnnotation") toUpdate := false // check for annotations val, ok := k8sNode.GetAnnotations()[bmc.annotationKey] if bmc.externalAnnotation && !ok { ll.Errorf("external annotaion %s is not accesible on node %s", bmc.annotationKey, k8sNode) } if !bmc.externalAnnotation && ok { if val == nodeUUID { ll.Tracef("%s value for node %s is already %s", bmc.annotationKey, k8sNode.Name, nodeUUID) } else { ll.Warnf("%s value for node %s is %s, however should have (according to corresponding Node's UUID) %s, going to update annotation's value.", bmc.annotationKey, k8sNode.Name, val, nodeUUID) k8sNode.ObjectMeta.Annotations[bmc.annotationKey] = nodeUUID toUpdate = true } } if !bmc.externalAnnotation && !ok { ll.Errorf("annotaion %s is not accesible on node %s", bmc.annotationKey, k8sNode) if k8sNode.ObjectMeta.Annotations == nil { k8sNode.ObjectMeta.Annotations = make(map[string]string, 1) } k8sNode.ObjectMeta.Annotations[bmc.annotationKey] = nodeUUID toUpdate = true } // initialize labels map if needed if k8sNode.Labels == nil {
k8sNode.ObjectMeta.Labels = make(map[string]string, 1) }
conditional_block
controller.go
("Unable to read k8s node %s: %v", k8sNodeName, err) return ctrl.Result{Requeue: true}, err } k8sNodes = []coreV1.Node{*k8sNode} } if !k8sNodeFromCache { k8sNodeCRs := new(coreV1.NodeList) if err := bmc.k8sClient.ReadList(context.Background(), k8sNodeCRs); err != nil { ll.Errorf("Unable to read k8s nodes list: %v", err) return ctrl.Result{Requeue: true}, err } k8sNodes = k8sNodeCRs.Items } matchedNodes := make([]string, 0) for i := range k8sNodes { matchedAddresses := bmc.matchedAddressesCount(bmNode, &k8sNodes[i]) if matchedAddresses == len(bmNode.Spec.Addresses) { k8sNode = &k8sNodes[i] matchedNodes = append(matchedNodes, k8sNode.Name) continue } if matchedAddresses > 0 { ll.Errorf("There is k8s node %s that partially match Node CR %s. Node.Spec: %v, k8s node addresses: %v", k8sNodes[i].Name, bmNode.Name, bmNode.Spec, k8sNodes[i].Status.Addresses) return ctrl.Result{}, nil } } if !bmNode.GetDeletionTimestamp().IsZero() { bmc.disableForNode(k8sNode.Name) if err := bmc.removeLabelsAndAnnotation(k8sNode); err != nil { ll.Errorf("Unable to remove annotations or labels from node %s: %v", k8sNode.Name, err) bmc.enableForNode(k8sNode.Name) return ctrl.Result{Requeue: true}, err } ll.Infof("Annotations and labels from node %s was removed. Removing finalizer from %s.", k8sNode.Name, bmNode.Name) bmNode.Finalizers = nil err := bmc.k8sClient.UpdateCR(context.Background(), bmNode) if err != nil { ll.Errorf("Unable to update Node %s: %v", bmNode.Name, err) } return ctrl.Result{}, err } if len(matchedNodes) == 1 { bmc.cache.put(k8sNode.Name, bmNode.Name) return bmc.updateNodeLabelsAndAnnotation(k8sNode, bmNode.Spec.UUID) } ll.Warnf("Unable to detect k8s node that corresponds to Node %v, matched nodes: %v", bmNode, matchedNodes) return ctrl.Result{}, nil } // updateNodeLabelsAndAnnotation checks nodeIDAnnotationKey annotation value for provided k8s Node and compare that value with goalValue // parses OS Image info and put/update os-name and os-version labels if needed func (bmc *Controller) updateNodeLabelsAndAnnotation(k8sNode *coreV1.Node, nodeUUID string) (ctrl.Result, error) { ll := bmc.log.WithField("method", "updateNodeLabelsAndAnnotation") toUpdate := false // check for annotations val, ok := k8sNode.GetAnnotations()[bmc.annotationKey] if bmc.externalAnnotation && !ok { ll.Errorf("external annotaion %s is not accesible on node %s", bmc.annotationKey, k8sNode) } if !bmc.externalAnnotation && ok { if val == nodeUUID { ll.Tracef("%s value for node %s is already %s", bmc.annotationKey, k8sNode.Name, nodeUUID) } else { ll.Warnf("%s value for node %s is %s, however should have (according to corresponding Node's UUID) %s, going to update annotation's value.", bmc.annotationKey, k8sNode.Name, val, nodeUUID) k8sNode.ObjectMeta.Annotations[bmc.annotationKey] = nodeUUID toUpdate = true } } if !bmc.externalAnnotation && !ok { ll.Errorf("annotaion %s is not accesible on node %s", bmc.annotationKey, k8sNode) if k8sNode.ObjectMeta.Annotations == nil { k8sNode.ObjectMeta.Annotations = make(map[string]string, 1) } k8sNode.ObjectMeta.Annotations[bmc.annotationKey] = nodeUUID toUpdate = true } // initialize labels map if needed if k8sNode.Labels == nil { k8sNode.ObjectMeta.Labels = make(map[string]string, 1) } // check for OS labels name, version, err := util.GetOSNameAndVersion(k8sNode.Status.NodeInfo.OSImage) if err == nil { // os name if k8sNode.Labels[common.NodeOSNameLabelKey] != name { // not set or matches ll.Infof("Setting label %s=%s on node %s", common.NodeOSNameLabelKey, name, k8sNode.Name) k8sNode.Labels[common.NodeOSNameLabelKey] = name toUpdate = true } // os version if k8sNode.Labels[common.NodeOSVersionLabelKey] != version { // not set or matches ll.Infof("Setting label %s=%s on node %s", common.NodeOSVersionLabelKey, version, k8sNode.Name) k8sNode.Labels[common.NodeOSVersionLabelKey] = version toUpdate = true } } else { ll.Errorf("Failed to obtain OS information: %s", err) } // check for kernel version label version, err = util.GetKernelVersion(k8sNode.Status.NodeInfo.KernelVersion) if err == nil { // os name if k8sNode.Labels[common.NodeKernelVersionLabelKey] != version { // not set or matches ll.Infof("Setting label %s=%s on node %s", common.NodeKernelVersionLabelKey, version, k8sNode.Name) k8sNode.Labels[common.NodeKernelVersionLabelKey] = version toUpdate = true if bmc.observer != nil { bmc.observer.Notify(version) } } } else { ll.Errorf("Failed to obtain Kernel version information: %s", err) } if toUpdate { if err := bmc.k8sClient.UpdateCR(context.Background(), k8sNode); err != nil { ll.Errorf("Unable to update node object: %v", err) return ctrl.Result{Requeue: true}, err } } return ctrl.Result{}, nil } func (bmc *Controller) removeLabelsAndAnnotation(k8sNode *coreV1.Node) error { toUpdate := false // check annotations annotations := k8sNode.GetAnnotations() if _, ok := annotations[bmc.annotationKey]; ok { if !bmc.externalAnnotation { delete(annotations, bmc.annotationKey) toUpdate = true } } // check labels labels := k8sNode.GetLabels() // os name if _, ok := labels[common.NodeOSNameLabelKey]; ok { delete(labels, common.NodeOSNameLabelKey) toUpdate = true } // os version if _, ok := labels[common.NodeOSVersionLabelKey]; ok { delete(labels, common.NodeOSVersionLabelKey) toUpdate = true } // kernel version if _, ok := labels[common.NodeKernelVersionLabelKey]; ok { delete(labels, common.NodeKernelVersionLabelKey) toUpdate = true } // external csi-provisioner label // TODO https://github.com/dell/csi-baremetal/issues/319 Rework after operator implementation if _, ok := labels[common.NodeIDTopologyLabelKey]; ok { delete(labels, common.NodeIDTopologyLabelKey) toUpdate = true } if toUpdate { k8sNode.Annotations = annotations k8sNode.Labels = labels return bmc.k8sClient.UpdateCR(context.Background(), k8sNode) } return nil } // matchedAddressesCount return amount of k8s node addresses that has corresponding address in bmNodeCR.Spec.Addresses map func (bmc *Controller) matchedAddressesCount(bmNodeCR *nodecrd.Node, k8sNode *coreV1.Node) int { matchedCount := 0 for _, addr := range k8sNode.Status.Addresses { crAddr, ok := bmNodeCR.Spec.Addresses[string(addr.Type)] if ok && crAddr == addr.Address { matchedCount++ } } return matchedCount } // constructAddresses converts k8sNode.Status.Addresses into the the map[string]string, key - address type, value - address func (bmc *Controller) constructAddresses(k8sNode *coreV1.Node) map[string]string { res := make(map[string]string, len(k8sNode.Status.Addresses)) for _, addr := range k8sNode.Status.Addresses { res[string(addr.Type)] = addr.Address } return res } func (bmc *Controller) c
onstructNodeID(
identifier_name
svh_visitor.rs
// hash leads to unstable SVH, because ident.name is just an index // into intern table (i.e. essentially a random address), not // computed from the name content. // // With the below enums, the SVH computation is not sensitive to // artifacts of how rustc was invoked nor of how the source code // was laid out. (Or at least it is *less* sensitive.) // This enum represents the different potential bits of code the // visitor could encounter that could affect the ABI for the crate, // and assigns each a distinct tag to feed into the hash computation. #[derive(Hash)] enum SawAbiComponent<'a> { // FIXME (#14132): should we include (some function of) // ident.ctxt as well? SawIdent(token::InternedString), SawStructDef(token::InternedString), SawLifetime(token::InternedString), SawLifetimeDef(token::InternedString), SawMod, SawForeignItem, SawItem,
SawGenerics, SawFn, SawTraitItem, SawImplItem, SawStructField, SawVariant, SawPath, SawBlock, SawPat, SawLocal, SawArm, SawExpr(SawExprComponent<'a>), SawStmt(SawStmtComponent), } /// SawExprComponent carries all of the information that we want /// to include in the hash that *won't* be covered by the /// subsequent recursive traversal of the expression's /// substructure by the visitor. /// /// We know every Expr_ variant is covered by a variant because /// `fn saw_expr` maps each to some case below. Ensuring that /// each variant carries an appropriate payload has to be verified /// by hand. /// /// (However, getting that *exactly* right is not so important /// because the SVH is just a developer convenience; there is no /// guarantee of collision-freedom, hash collisions are just /// (hopefully) unlikely.) #[derive(Hash)] pub enum SawExprComponent<'a> { SawExprLoop(Option<token::InternedString>), SawExprField(token::InternedString), SawExprTupField(usize), SawExprBreak(Option<token::InternedString>), SawExprAgain(Option<token::InternedString>), SawExprBox, SawExprVec, SawExprCall, SawExprMethodCall, SawExprTup, SawExprBinary(hir::BinOp_), SawExprUnary(hir::UnOp), SawExprLit(ast::LitKind), SawExprCast, SawExprType, SawExprIf, SawExprWhile, SawExprMatch, SawExprClosure, SawExprBlock, SawExprAssign, SawExprAssignOp(hir::BinOp_), SawExprIndex, SawExprPath(Option<usize>), SawExprAddrOf(hir::Mutability), SawExprRet, SawExprInlineAsm(&'a hir::InlineAsm), SawExprStruct, SawExprRepeat, } fn saw_expr<'a>(node: &'a Expr_) -> SawExprComponent<'a> { match *node { ExprBox(..) => SawExprBox, ExprVec(..) => SawExprVec, ExprCall(..) => SawExprCall, ExprMethodCall(..) => SawExprMethodCall, ExprTup(..) => SawExprTup, ExprBinary(op, _, _) => SawExprBinary(op.node), ExprUnary(op, _) => SawExprUnary(op), ExprLit(ref lit) => SawExprLit(lit.node.clone()), ExprCast(..) => SawExprCast, ExprType(..) => SawExprType, ExprIf(..) => SawExprIf, ExprWhile(..) => SawExprWhile, ExprLoop(_, id) => SawExprLoop(id.map(|id| id.node.as_str())), ExprMatch(..) => SawExprMatch, ExprClosure(..) => SawExprClosure, ExprBlock(..) => SawExprBlock, ExprAssign(..) => SawExprAssign, ExprAssignOp(op, _, _) => SawExprAssignOp(op.node), ExprField(_, name) => SawExprField(name.node.as_str()), ExprTupField(_, id) => SawExprTupField(id.node), ExprIndex(..) => SawExprIndex, ExprPath(ref qself, _) => SawExprPath(qself.as_ref().map(|q| q.position)), ExprAddrOf(m, _) => SawExprAddrOf(m), ExprBreak(id) => SawExprBreak(id.map(|id| id.node.as_str())), ExprAgain(id) => SawExprAgain(id.map(|id| id.node.as_str())), ExprRet(..) => SawExprRet, ExprInlineAsm(ref a,_,_) => SawExprInlineAsm(a), ExprStruct(..) => SawExprStruct, ExprRepeat(..) => SawExprRepeat, } } /// SawStmtComponent is analogous to SawExprComponent, but for statements. #[derive(Hash)] pub enum SawStmtComponent { SawStmtExpr, SawStmtSemi, } impl<'a, 'hash, 'tcx> Visitor<'tcx> for StrictVersionHashVisitor<'a, 'hash, 'tcx> { fn visit_nested_item(&mut self, _: ItemId) { // Each item is hashed independently; ignore nested items. } fn visit_variant_data(&mut self, s: &'tcx VariantData, name: Name, g: &'tcx Generics, _: NodeId, _: Span) { debug!("visit_variant_data: st={:?}", self.st); SawStructDef(name.as_str()).hash(self.st); visit::walk_generics(self, g); visit::walk_struct_def(self, s) } fn visit_variant(&mut self, v: &'tcx Variant, g: &'tcx Generics, item_id: NodeId) { debug!("visit_variant: st={:?}", self.st); SawVariant.hash(self.st); // walk_variant does not call walk_generics, so do it here. visit::walk_generics(self, g); visit::walk_variant(self, v, g, item_id) } // All of the remaining methods just record (in the hash // SipHasher) that the visitor saw that particular variant // (with its payload), and continue walking as the default // visitor would. // // Some of the implementations have some notes as to how one // might try to make their SVH computation less discerning // (e.g. by incorporating reachability analysis). But // currently all of their implementations are uniform and // uninteresting. // // (If you edit a method such that it deviates from the // pattern, please move that method up above this comment.) fn visit_name(&mut self, _: Span, name: Name) { debug!("visit_name: st={:?}", self.st); SawIdent(name.as_str()).hash(self.st); } fn visit_lifetime(&mut self, l: &'tcx Lifetime) { debug!("visit_lifetime: st={:?}", self.st); SawLifetime(l.name.as_str()).hash(self.st); } fn visit_lifetime_def(&mut self, l: &'tcx LifetimeDef) { debug!("visit_lifetime_def: st={:?}", self.st); SawLifetimeDef(l.lifetime.name.as_str()).hash(self.st); } // We do recursively walk the bodies of functions/methods // (rather than omitting their bodies from the hash) since // monomorphization and cross-crate inlining generally implies // that a change to a crate body will require downstream // crates to be recompiled. fn visit_expr(&mut self, ex: &'tcx Expr) { debug!("visit_expr: st={:?}", self.st); SawExpr(saw_expr(&ex.node)).hash(self.st); visit::walk_expr(self, ex) } fn visit_stmt(&mut self, s: &'tcx Stmt) { debug!("visit_stmt: st={:?}", self.st); // We don't want to modify the hash for decls, because // they might be item decls (if they are local decls, // we'll hash that fact in visit_local); but we do want to // remember if this was a StmtExpr or StmtSemi (the later // had an explicit semi-colon; this affects the typing // rules). match s.node { StmtDecl(..) => (), StmtExpr(..) => SawStmt(SawStmtExpr).hash(self.st), StmtSemi(..) => SawStmt(SawStmtSemi).hash(self.st), } visit::walk_stmt(self, s) } fn visit_foreign_item(&mut self, i: &'tcx ForeignItem) { debug!("visit_foreign_item: st={:?}", self.st); // FIXME (#14132) ideally we would incorporate privacy (or // perhaps reachability) somewhere here, so foreign items // that do not leak into downstream crates would not be // part of the ABI. SawForeignItem.hash(self.st); visit::walk_foreign_item(self, i
SawTy,
random_line_split
svh_visitor.rs
Option<token::InternedString>), SawExprBox, SawExprVec, SawExprCall, SawExprMethodCall, SawExprTup, SawExprBinary(hir::BinOp_), SawExprUnary(hir::UnOp), SawExprLit(ast::LitKind), SawExprCast, SawExprType, SawExprIf, SawExprWhile, SawExprMatch, SawExprClosure, SawExprBlock, SawExprAssign, SawExprAssignOp(hir::BinOp_), SawExprIndex, SawExprPath(Option<usize>), SawExprAddrOf(hir::Mutability), SawExprRet, SawExprInlineAsm(&'a hir::InlineAsm), SawExprStruct, SawExprRepeat, } fn saw_expr<'a>(node: &'a Expr_) -> SawExprComponent<'a> { match *node { ExprBox(..) => SawExprBox, ExprVec(..) => SawExprVec, ExprCall(..) => SawExprCall, ExprMethodCall(..) => SawExprMethodCall, ExprTup(..) => SawExprTup, ExprBinary(op, _, _) => SawExprBinary(op.node), ExprUnary(op, _) => SawExprUnary(op), ExprLit(ref lit) => SawExprLit(lit.node.clone()), ExprCast(..) => SawExprCast, ExprType(..) => SawExprType, ExprIf(..) => SawExprIf, ExprWhile(..) => SawExprWhile, ExprLoop(_, id) => SawExprLoop(id.map(|id| id.node.as_str())), ExprMatch(..) => SawExprMatch, ExprClosure(..) => SawExprClosure, ExprBlock(..) => SawExprBlock, ExprAssign(..) => SawExprAssign, ExprAssignOp(op, _, _) => SawExprAssignOp(op.node), ExprField(_, name) => SawExprField(name.node.as_str()), ExprTupField(_, id) => SawExprTupField(id.node), ExprIndex(..) => SawExprIndex, ExprPath(ref qself, _) => SawExprPath(qself.as_ref().map(|q| q.position)), ExprAddrOf(m, _) => SawExprAddrOf(m), ExprBreak(id) => SawExprBreak(id.map(|id| id.node.as_str())), ExprAgain(id) => SawExprAgain(id.map(|id| id.node.as_str())), ExprRet(..) => SawExprRet, ExprInlineAsm(ref a,_,_) => SawExprInlineAsm(a), ExprStruct(..) => SawExprStruct, ExprRepeat(..) => SawExprRepeat, } } /// SawStmtComponent is analogous to SawExprComponent, but for statements. #[derive(Hash)] pub enum SawStmtComponent { SawStmtExpr, SawStmtSemi, } impl<'a, 'hash, 'tcx> Visitor<'tcx> for StrictVersionHashVisitor<'a, 'hash, 'tcx> { fn visit_nested_item(&mut self, _: ItemId) { // Each item is hashed independently; ignore nested items. } fn visit_variant_data(&mut self, s: &'tcx VariantData, name: Name, g: &'tcx Generics, _: NodeId, _: Span) { debug!("visit_variant_data: st={:?}", self.st); SawStructDef(name.as_str()).hash(self.st); visit::walk_generics(self, g); visit::walk_struct_def(self, s) } fn visit_variant(&mut self, v: &'tcx Variant, g: &'tcx Generics, item_id: NodeId) { debug!("visit_variant: st={:?}", self.st); SawVariant.hash(self.st); // walk_variant does not call walk_generics, so do it here. visit::walk_generics(self, g); visit::walk_variant(self, v, g, item_id) } // All of the remaining methods just record (in the hash // SipHasher) that the visitor saw that particular variant // (with its payload), and continue walking as the default // visitor would. // // Some of the implementations have some notes as to how one // might try to make their SVH computation less discerning // (e.g. by incorporating reachability analysis). But // currently all of their implementations are uniform and // uninteresting. // // (If you edit a method such that it deviates from the // pattern, please move that method up above this comment.) fn visit_name(&mut self, _: Span, name: Name) { debug!("visit_name: st={:?}", self.st); SawIdent(name.as_str()).hash(self.st); } fn visit_lifetime(&mut self, l: &'tcx Lifetime) { debug!("visit_lifetime: st={:?}", self.st); SawLifetime(l.name.as_str()).hash(self.st); } fn visit_lifetime_def(&mut self, l: &'tcx LifetimeDef) { debug!("visit_lifetime_def: st={:?}", self.st); SawLifetimeDef(l.lifetime.name.as_str()).hash(self.st); } // We do recursively walk the bodies of functions/methods // (rather than omitting their bodies from the hash) since // monomorphization and cross-crate inlining generally implies // that a change to a crate body will require downstream // crates to be recompiled. fn visit_expr(&mut self, ex: &'tcx Expr) { debug!("visit_expr: st={:?}", self.st); SawExpr(saw_expr(&ex.node)).hash(self.st); visit::walk_expr(self, ex) } fn visit_stmt(&mut self, s: &'tcx Stmt) { debug!("visit_stmt: st={:?}", self.st); // We don't want to modify the hash for decls, because // they might be item decls (if they are local decls, // we'll hash that fact in visit_local); but we do want to // remember if this was a StmtExpr or StmtSemi (the later // had an explicit semi-colon; this affects the typing // rules). match s.node { StmtDecl(..) => (), StmtExpr(..) => SawStmt(SawStmtExpr).hash(self.st), StmtSemi(..) => SawStmt(SawStmtSemi).hash(self.st), } visit::walk_stmt(self, s) } fn visit_foreign_item(&mut self, i: &'tcx ForeignItem) { debug!("visit_foreign_item: st={:?}", self.st); // FIXME (#14132) ideally we would incorporate privacy (or // perhaps reachability) somewhere here, so foreign items // that do not leak into downstream crates would not be // part of the ABI. SawForeignItem.hash(self.st); visit::walk_foreign_item(self, i) } fn visit_item(&mut self, i: &'tcx Item) { debug!("visit_item: {:?} st={:?}", i, self.st); // FIXME (#14132) ideally would incorporate reachability // analysis somewhere here, so items that never leak into // downstream crates (e.g. via monomorphisation or // inlining) would not be part of the ABI. SawItem.hash(self.st); visit::walk_item(self, i) } fn visit_mod(&mut self, m: &'tcx Mod, _s: Span, n: NodeId) { debug!("visit_mod: st={:?}", self.st); SawMod.hash(self.st); visit::walk_mod(self, m, n) } fn visit_ty(&mut self, t: &'tcx Ty) { debug!("visit_ty: st={:?}", self.st); SawTy.hash(self.st); visit::walk_ty(self, t) } fn visit_generics(&mut self, g: &'tcx Generics) { debug!("visit_generics: st={:?}", self.st); SawGenerics.hash(self.st); visit::walk_generics(self, g) } fn visit_fn(&mut self, fk: FnKind<'tcx>, fd: &'tcx FnDecl, b: &'tcx Block, s: Span, n: NodeId) { debug!("visit_fn: st={:?}", self.st); SawFn.hash(self.st); visit::walk_fn(self, fk, fd, b, s, n) } fn visit_trait_item(&mut self, ti: &'tcx TraitItem) { debug!("visit_trait_item: st={:?}", self.st); SawTraitItem.hash(self.st); visit::walk_trait_item(self, ti) } fn visit_impl_item(&mut self, ii: &'tcx ImplItem) { debug!("visit_impl_item: st={:?}", self.st); SawImplItem.hash(self.st); visit::walk_impl_item(self, ii) } fn visit_struct_field(&mut self, s: &'tcx StructField)
{ debug!("visit_struct_field: st={:?}", self.st); SawStructField.hash(self.st); visit::walk_struct_field(self, s) }
identifier_body
svh_visitor.rs
// hash leads to unstable SVH, because ident.name is just an index // into intern table (i.e. essentially a random address), not // computed from the name content. // // With the below enums, the SVH computation is not sensitive to // artifacts of how rustc was invoked nor of how the source code // was laid out. (Or at least it is *less* sensitive.) // This enum represents the different potential bits of code the // visitor could encounter that could affect the ABI for the crate, // and assigns each a distinct tag to feed into the hash computation. #[derive(Hash)] enum SawAbiComponent<'a> { // FIXME (#14132): should we include (some function of) // ident.ctxt as well? SawIdent(token::InternedString), SawStructDef(token::InternedString), SawLifetime(token::InternedString), SawLifetimeDef(token::InternedString), SawMod, SawForeignItem, SawItem, SawTy, SawGenerics, SawFn, SawTraitItem, SawImplItem, SawStructField, SawVariant, SawPath, SawBlock, SawPat, SawLocal, SawArm, SawExpr(SawExprComponent<'a>), SawStmt(SawStmtComponent), } /// SawExprComponent carries all of the information that we want /// to include in the hash that *won't* be covered by the /// subsequent recursive traversal of the expression's /// substructure by the visitor. /// /// We know every Expr_ variant is covered by a variant because /// `fn saw_expr` maps each to some case below. Ensuring that /// each variant carries an appropriate payload has to be verified /// by hand. /// /// (However, getting that *exactly* right is not so important /// because the SVH is just a developer convenience; there is no /// guarantee of collision-freedom, hash collisions are just /// (hopefully) unlikely.) #[derive(Hash)] pub enum SawExprComponent<'a> { SawExprLoop(Option<token::InternedString>), SawExprField(token::InternedString), SawExprTupField(usize), SawExprBreak(Option<token::InternedString>), SawExprAgain(Option<token::InternedString>), SawExprBox, SawExprVec, SawExprCall, SawExprMethodCall, SawExprTup, SawExprBinary(hir::BinOp_), SawExprUnary(hir::UnOp), SawExprLit(ast::LitKind), SawExprCast, SawExprType, SawExprIf, SawExprWhile, SawExprMatch, SawExprClosure, SawExprBlock, SawExprAssign, SawExprAssignOp(hir::BinOp_), SawExprIndex, SawExprPath(Option<usize>), SawExprAddrOf(hir::Mutability), SawExprRet, SawExprInlineAsm(&'a hir::InlineAsm), SawExprStruct, SawExprRepeat, } fn saw_expr<'a>(node: &'a Expr_) -> SawExprComponent<'a> { match *node { ExprBox(..) => SawExprBox, ExprVec(..) => SawExprVec, ExprCall(..) => SawExprCall, ExprMethodCall(..) => SawExprMethodCall, ExprTup(..) => SawExprTup, ExprBinary(op, _, _) => SawExprBinary(op.node), ExprUnary(op, _) => SawExprUnary(op), ExprLit(ref lit) => SawExprLit(lit.node.clone()), ExprCast(..) => SawExprCast, ExprType(..) => SawExprType, ExprIf(..) => SawExprIf, ExprWhile(..) => SawExprWhile, ExprLoop(_, id) => SawExprLoop(id.map(|id| id.node.as_str())), ExprMatch(..) => SawExprMatch, ExprClosure(..) => SawExprClosure, ExprBlock(..) => SawExprBlock, ExprAssign(..) => SawExprAssign, ExprAssignOp(op, _, _) => SawExprAssignOp(op.node), ExprField(_, name) => SawExprField(name.node.as_str()), ExprTupField(_, id) => SawExprTupField(id.node), ExprIndex(..) => SawExprIndex, ExprPath(ref qself, _) => SawExprPath(qself.as_ref().map(|q| q.position)), ExprAddrOf(m, _) => SawExprAddrOf(m), ExprBreak(id) => SawExprBreak(id.map(|id| id.node.as_str())), ExprAgain(id) => SawExprAgain(id.map(|id| id.node.as_str())), ExprRet(..) => SawExprRet, ExprInlineAsm(ref a,_,_) => SawExprInlineAsm(a), ExprStruct(..) => SawExprStruct, ExprRepeat(..) => SawExprRepeat, } } /// SawStmtComponent is analogous to SawExprComponent, but for statements. #[derive(Hash)] pub enum SawStmtComponent { SawStmtExpr, SawStmtSemi, } impl<'a, 'hash, 'tcx> Visitor<'tcx> for StrictVersionHashVisitor<'a, 'hash, 'tcx> { fn visit_nested_item(&mut self, _: ItemId) { // Each item is hashed independently; ignore nested items. } fn visit_variant_data(&mut self, s: &'tcx VariantData, name: Name, g: &'tcx Generics, _: NodeId, _: Span) { debug!("visit_variant_data: st={:?}", self.st); SawStructDef(name.as_str()).hash(self.st); visit::walk_generics(self, g); visit::walk_struct_def(self, s) } fn
(&mut self, v: &'tcx Variant, g: &'tcx Generics, item_id: NodeId) { debug!("visit_variant: st={:?}", self.st); SawVariant.hash(self.st); // walk_variant does not call walk_generics, so do it here. visit::walk_generics(self, g); visit::walk_variant(self, v, g, item_id) } // All of the remaining methods just record (in the hash // SipHasher) that the visitor saw that particular variant // (with its payload), and continue walking as the default // visitor would. // // Some of the implementations have some notes as to how one // might try to make their SVH computation less discerning // (e.g. by incorporating reachability analysis). But // currently all of their implementations are uniform and // uninteresting. // // (If you edit a method such that it deviates from the // pattern, please move that method up above this comment.) fn visit_name(&mut self, _: Span, name: Name) { debug!("visit_name: st={:?}", self.st); SawIdent(name.as_str()).hash(self.st); } fn visit_lifetime(&mut self, l: &'tcx Lifetime) { debug!("visit_lifetime: st={:?}", self.st); SawLifetime(l.name.as_str()).hash(self.st); } fn visit_lifetime_def(&mut self, l: &'tcx LifetimeDef) { debug!("visit_lifetime_def: st={:?}", self.st); SawLifetimeDef(l.lifetime.name.as_str()).hash(self.st); } // We do recursively walk the bodies of functions/methods // (rather than omitting their bodies from the hash) since // monomorphization and cross-crate inlining generally implies // that a change to a crate body will require downstream // crates to be recompiled. fn visit_expr(&mut self, ex: &'tcx Expr) { debug!("visit_expr: st={:?}", self.st); SawExpr(saw_expr(&ex.node)).hash(self.st); visit::walk_expr(self, ex) } fn visit_stmt(&mut self, s: &'tcx Stmt) { debug!("visit_stmt: st={:?}", self.st); // We don't want to modify the hash for decls, because // they might be item decls (if they are local decls, // we'll hash that fact in visit_local); but we do want to // remember if this was a StmtExpr or StmtSemi (the later // had an explicit semi-colon; this affects the typing // rules). match s.node { StmtDecl(..) => (), StmtExpr(..) => SawStmt(SawStmtExpr).hash(self.st), StmtSemi(..) => SawStmt(SawStmtSemi).hash(self.st), } visit::walk_stmt(self, s) } fn visit_foreign_item(&mut self, i: &'tcx ForeignItem) { debug!("visit_foreign_item: st={:?}", self.st); // FIXME (#14132) ideally we would incorporate privacy (or // perhaps reachability) somewhere here, so foreign items // that do not leak into downstream crates would not be // part of the ABI. SawForeignItem.hash(self.st); visit::walk_foreign_item(self,
visit_variant
identifier_name
config.rs
.config.toml"; /// ENV_PREFIX should be used along side the config field name to set a config field using /// environment variables /// For example, `IROH_PATH=/path/to/config` would set the value of the `Config.path` field pub const ENV_PREFIX: &str = "IROH"; /// Paths to files or directory within the [`iroh_data_root`] used by Iroh. #[derive(Debug, Clone, Eq, PartialEq)] pub enum IrohPaths { /// Path to the node's private key for the [`iroh_net::PeerId`]. Keypair, /// Path to the node's [flat-file store](iroh::baomap::flat) for complete blobs. BaoFlatStoreComplete, /// Path to the node's [flat-file store](iroh::baomap::flat) for partial blobs. BaoFlatStorePartial, } impl From<&IrohPaths> for &'static str { fn from(value: &IrohPaths) -> Self { match value { IrohPaths::Keypair => "keypair", IrohPaths::BaoFlatStoreComplete => "blobs.v0", IrohPaths::BaoFlatStorePartial => "blobs-partial.v0", } } } impl FromStr for IrohPaths { type Err = anyhow::Error; fn from_str(s: &str) -> Result<Self> { Ok(match s { "keypair" => Self::Keypair, "blobs.v0" => Self::BaoFlatStoreComplete, "blobs-partial.v0" => Self::BaoFlatStorePartial, _ => bail!("unknown file or directory"), }) } } impl fmt::Display for IrohPaths { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s: &str = self.into(); write!(f, "{s}") } } impl AsRef<Path> for IrohPaths { fn as_ref(&self) -> &Path { let s: &str = self.into(); Path::new(s) } } impl IrohPaths { /// Get the path for this [`IrohPath`] by joining the name to `IROH_DATA_DIR` environment variable. pub fn with_env(self) -> Result<PathBuf> { let mut root = iroh_data_root()?; if !root.is_absolute() { root = std::env::current_dir()?.join(root); } Ok(self.with_root(root)) } /// Get the path for this [`IrohPath`] by joining the name to a root directory. pub fn with_root(self, root: impl AsRef<Path>) -> PathBuf { let path = root.as_ref().join(self); path } } /// The configuration for the iroh cli. #[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)] #[serde(default)] pub struct Config { /// The regions for DERP to use. pub derp_regions: Vec<DerpRegion>, } impl Default for Config { fn
() -> Self { Self { // TODO(ramfox): this should probably just be a derp map derp_regions: [default_na_derp_region(), default_eu_derp_region()].into(), } } } impl Config { /// Make a config using a default, files, environment variables, and commandline flags. /// /// Later items in the *file_paths* slice will have a higher priority than earlier ones. /// /// Environment variables are expected to start with the *env_prefix*. Nested fields can be /// accessed using `.`, if your environment allows env vars with `.` /// /// Note: For the metrics configuration env vars, it is recommended to use the metrics /// specific prefix `IROH_METRICS` to set a field in the metrics config. You can use the /// above dot notation to set a metrics field, eg, `IROH_CONFIG_METRICS.SERVICE_NAME`, but /// only if your environment allows it pub fn load<S, V>( file_paths: &[Option<&Path>], env_prefix: &str, flag_overrides: HashMap<S, V>, ) -> Result<Config> where S: AsRef<str>, V: Into<Value>, { let mut builder = config::Config::builder(); // layer on config options from files for path in file_paths.iter().flatten() { if path.exists() { let p = path.to_str().ok_or_else(|| anyhow::anyhow!("empty path"))?; builder = builder.add_source(File::with_name(p)); } } // next, add any environment variables builder = builder.add_source( Environment::with_prefix(env_prefix) .separator("__") .try_parsing(true), ); // finally, override any values for (flag, val) in flag_overrides.into_iter() { builder = builder.set_override(flag, val)?; } let cfg = builder.build()?; debug!("make_config:\n{:#?}\n", cfg); let cfg = cfg.try_deserialize()?; Ok(cfg) } /// Constructs a `DerpMap` based on the current configuration. pub fn derp_map(&self) -> Option<DerpMap> { if self.derp_regions.is_empty() { return None; } let dm: DerpMap = self.derp_regions.iter().cloned().into(); Some(dm) } } /// Name of directory that wraps all iroh files in a given application directory const IROH_DIR: &str = "iroh"; /// Returns the path to the user's iroh config directory. /// /// If the `IROH_CONFIG_DIR` environment variable is set it will be used unconditionally. /// Otherwise the returned value depends on the operating system according to the following /// table. /// /// | Platform | Value | Example | /// | -------- | ------------------------------------- | -------------------------------- | /// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config/iroh | /home/alice/.config/iroh | /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh | /// | Windows | `{FOLDERID_RoamingAppData}`/iroh | C:\Users\Alice\AppData\Roaming\iroh | pub fn iroh_config_root() -> Result<PathBuf> { if let Some(val) = env::var_os("IROH_CONFIG_DIR") { return Ok(PathBuf::from(val)); } let cfg = dirs_next::config_dir() .ok_or_else(|| anyhow!("operating environment provides no directory for configuration"))?; Ok(cfg.join(IROH_DIR)) } /// Path that leads to a file in the iroh config directory. pub fn iroh_config_path(file_name: impl AsRef<Path>) -> Result<PathBuf> { let path = iroh_config_root()?.join(file_name); Ok(path) } /// Returns the path to the user's iroh data directory. /// /// If the `IROH_DATA_DIR` environment variable is set it will be used unconditionally. /// Otherwise the returned value depends on the operating system according to the following /// table. /// /// | Platform | Value | Example | /// | -------- | --------------------------------------------- | ---------------------------------------- | /// | Linux | `$XDG_DATA_HOME`/iroh or `$HOME`/.local/share/iroh | /home/alice/.local/share/iroh | /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh | /// | Windows | `{FOLDERID_RoamingAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh | pub fn iroh_data_root() -> Result<PathBuf> { if let Some(val) = env::var_os("IROH_DATA_DIR") { return Ok(PathBuf::from(val)); } let path = dirs_next::data_dir().ok_or_else(|| { anyhow!("operating environment provides no directory for application data") })?; Ok(path.join(IROH_DIR)) } /// Path that leads to a file in the iroh data directory. #[allow(dead_code)] pub fn iroh_data_path(file_name: &Path) -> Result<PathBuf> { let path = iroh_data_root()?.join(file_name); Ok(path) } /// Returns the path to the user's iroh cache directory. /// /// If the `IROH_CACHE_DIR` environment variable is set it will be used unconditionally. /// Otherwise the returned value depends on the operating system according to the following /// table. /// /// | Platform | Value | Example | /// | -------- | --------------------------------------------- | ---------------------------------------- | /// | Linux | `$XDG_CACHE_HOME`/iroh or `$HOME`/.cache/iroh | /home/.cache/iroh | /// | macOS | `$HOME`/Library/Caches/iroh | /Users/Alice/Library/Caches/iroh | /// | Windows | `{FOLDERID_LocalAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh | #[allow(dead_code)] pub fn iroh_cache_root() -> Result<PathBuf> { if let Some(val) = env::var_os
default
identifier_name
config.rs
.config.toml"; /// ENV_PREFIX should be used along side the config field name to set a config field using /// environment variables /// For example, `IROH_PATH=/path/to/config` would set the value of the `Config.path` field pub const ENV_PREFIX: &str = "IROH"; /// Paths to files or directory within the [`iroh_data_root`] used by Iroh. #[derive(Debug, Clone, Eq, PartialEq)] pub enum IrohPaths { /// Path to the node's private key for the [`iroh_net::PeerId`]. Keypair, /// Path to the node's [flat-file store](iroh::baomap::flat) for complete blobs. BaoFlatStoreComplete, /// Path to the node's [flat-file store](iroh::baomap::flat) for partial blobs. BaoFlatStorePartial, } impl From<&IrohPaths> for &'static str { fn from(value: &IrohPaths) -> Self { match value { IrohPaths::Keypair => "keypair", IrohPaths::BaoFlatStoreComplete => "blobs.v0", IrohPaths::BaoFlatStorePartial => "blobs-partial.v0", } } } impl FromStr for IrohPaths { type Err = anyhow::Error; fn from_str(s: &str) -> Result<Self> { Ok(match s { "keypair" => Self::Keypair, "blobs.v0" => Self::BaoFlatStoreComplete, "blobs-partial.v0" => Self::BaoFlatStorePartial, _ => bail!("unknown file or directory"), }) } } impl fmt::Display for IrohPaths { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s: &str = self.into(); write!(f, "{s}") } } impl AsRef<Path> for IrohPaths { fn as_ref(&self) -> &Path { let s: &str = self.into(); Path::new(s) } } impl IrohPaths { /// Get the path for this [`IrohPath`] by joining the name to `IROH_DATA_DIR` environment variable. pub fn with_env(self) -> Result<PathBuf>
/// Get the path for this [`IrohPath`] by joining the name to a root directory. pub fn with_root(self, root: impl AsRef<Path>) -> PathBuf { let path = root.as_ref().join(self); path } } /// The configuration for the iroh cli. #[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)] #[serde(default)] pub struct Config { /// The regions for DERP to use. pub derp_regions: Vec<DerpRegion>, } impl Default for Config { fn default() -> Self { Self { // TODO(ramfox): this should probably just be a derp map derp_regions: [default_na_derp_region(), default_eu_derp_region()].into(), } } } impl Config { /// Make a config using a default, files, environment variables, and commandline flags. /// /// Later items in the *file_paths* slice will have a higher priority than earlier ones. /// /// Environment variables are expected to start with the *env_prefix*. Nested fields can be /// accessed using `.`, if your environment allows env vars with `.` /// /// Note: For the metrics configuration env vars, it is recommended to use the metrics /// specific prefix `IROH_METRICS` to set a field in the metrics config. You can use the /// above dot notation to set a metrics field, eg, `IROH_CONFIG_METRICS.SERVICE_NAME`, but /// only if your environment allows it pub fn load<S, V>( file_paths: &[Option<&Path>], env_prefix: &str, flag_overrides: HashMap<S, V>, ) -> Result<Config> where S: AsRef<str>, V: Into<Value>, { let mut builder = config::Config::builder(); // layer on config options from files for path in file_paths.iter().flatten() { if path.exists() { let p = path.to_str().ok_or_else(|| anyhow::anyhow!("empty path"))?; builder = builder.add_source(File::with_name(p)); } } // next, add any environment variables builder = builder.add_source( Environment::with_prefix(env_prefix) .separator("__") .try_parsing(true), ); // finally, override any values for (flag, val) in flag_overrides.into_iter() { builder = builder.set_override(flag, val)?; } let cfg = builder.build()?; debug!("make_config:\n{:#?}\n", cfg); let cfg = cfg.try_deserialize()?; Ok(cfg) } /// Constructs a `DerpMap` based on the current configuration. pub fn derp_map(&self) -> Option<DerpMap> { if self.derp_regions.is_empty() { return None; } let dm: DerpMap = self.derp_regions.iter().cloned().into(); Some(dm) } } /// Name of directory that wraps all iroh files in a given application directory const IROH_DIR: &str = "iroh"; /// Returns the path to the user's iroh config directory. /// /// If the `IROH_CONFIG_DIR` environment variable is set it will be used unconditionally. /// Otherwise the returned value depends on the operating system according to the following /// table. /// /// | Platform | Value | Example | /// | -------- | ------------------------------------- | -------------------------------- | /// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config/iroh | /home/alice/.config/iroh | /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh | /// | Windows | `{FOLDERID_RoamingAppData}`/iroh | C:\Users\Alice\AppData\Roaming\iroh | pub fn iroh_config_root() -> Result<PathBuf> { if let Some(val) = env::var_os("IROH_CONFIG_DIR") { return Ok(PathBuf::from(val)); } let cfg = dirs_next::config_dir() .ok_or_else(|| anyhow!("operating environment provides no directory for configuration"))?; Ok(cfg.join(IROH_DIR)) } /// Path that leads to a file in the iroh config directory. pub fn iroh_config_path(file_name: impl AsRef<Path>) -> Result<PathBuf> { let path = iroh_config_root()?.join(file_name); Ok(path) } /// Returns the path to the user's iroh data directory. /// /// If the `IROH_DATA_DIR` environment variable is set it will be used unconditionally. /// Otherwise the returned value depends on the operating system according to the following /// table. /// /// | Platform | Value | Example | /// | -------- | --------------------------------------------- | ---------------------------------------- | /// | Linux | `$XDG_DATA_HOME`/iroh or `$HOME`/.local/share/iroh | /home/alice/.local/share/iroh | /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh | /// | Windows | `{FOLDERID_RoamingAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh | pub fn iroh_data_root() -> Result<PathBuf> { if let Some(val) = env::var_os("IROH_DATA_DIR") { return Ok(PathBuf::from(val)); } let path = dirs_next::data_dir().ok_or_else(|| { anyhow!("operating environment provides no directory for application data") })?; Ok(path.join(IROH_DIR)) } /// Path that leads to a file in the iroh data directory. #[allow(dead_code)] pub fn iroh_data_path(file_name: &Path) -> Result<PathBuf> { let path = iroh_data_root()?.join(file_name); Ok(path) } /// Returns the path to the user's iroh cache directory. /// /// If the `IROH_CACHE_DIR` environment variable is set it will be used unconditionally. /// Otherwise the returned value depends on the operating system according to the following /// table. /// /// | Platform | Value | Example | /// | -------- | --------------------------------------------- | ---------------------------------------- | /// | Linux | `$XDG_CACHE_HOME`/iroh or `$HOME`/.cache/iroh | /home/.cache/iroh | /// | macOS | `$HOME`/Library/Caches/iroh | /Users/Alice/Library/Caches/iroh | /// | Windows | `{FOLDERID_LocalAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh | #[allow(dead_code)] pub fn iroh_cache_root() -> Result<PathBuf> { if let Some(val) = env::var
{ let mut root = iroh_data_root()?; if !root.is_absolute() { root = std::env::current_dir()?.join(root); } Ok(self.with_root(root)) }
identifier_body
config.rs
h.config.toml"; /// ENV_PREFIX should be used along side the config field name to set a config field using /// environment variables /// For example, `IROH_PATH=/path/to/config` would set the value of the `Config.path` field pub const ENV_PREFIX: &str = "IROH"; /// Paths to files or directory within the [`iroh_data_root`] used by Iroh. #[derive(Debug, Clone, Eq, PartialEq)] pub enum IrohPaths { /// Path to the node's private key for the [`iroh_net::PeerId`]. Keypair, /// Path to the node's [flat-file store](iroh::baomap::flat) for complete blobs. BaoFlatStoreComplete, /// Path to the node's [flat-file store](iroh::baomap::flat) for partial blobs. BaoFlatStorePartial, } impl From<&IrohPaths> for &'static str { fn from(value: &IrohPaths) -> Self { match value {
} } impl FromStr for IrohPaths { type Err = anyhow::Error; fn from_str(s: &str) -> Result<Self> { Ok(match s { "keypair" => Self::Keypair, "blobs.v0" => Self::BaoFlatStoreComplete, "blobs-partial.v0" => Self::BaoFlatStorePartial, _ => bail!("unknown file or directory"), }) } } impl fmt::Display for IrohPaths { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s: &str = self.into(); write!(f, "{s}") } } impl AsRef<Path> for IrohPaths { fn as_ref(&self) -> &Path { let s: &str = self.into(); Path::new(s) } } impl IrohPaths { /// Get the path for this [`IrohPath`] by joining the name to `IROH_DATA_DIR` environment variable. pub fn with_env(self) -> Result<PathBuf> { let mut root = iroh_data_root()?; if !root.is_absolute() { root = std::env::current_dir()?.join(root); } Ok(self.with_root(root)) } /// Get the path for this [`IrohPath`] by joining the name to a root directory. pub fn with_root(self, root: impl AsRef<Path>) -> PathBuf { let path = root.as_ref().join(self); path } } /// The configuration for the iroh cli. #[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)] #[serde(default)] pub struct Config { /// The regions for DERP to use. pub derp_regions: Vec<DerpRegion>, } impl Default for Config { fn default() -> Self { Self { // TODO(ramfox): this should probably just be a derp map derp_regions: [default_na_derp_region(), default_eu_derp_region()].into(), } } } impl Config { /// Make a config using a default, files, environment variables, and commandline flags. /// /// Later items in the *file_paths* slice will have a higher priority than earlier ones. /// /// Environment variables are expected to start with the *env_prefix*. Nested fields can be /// accessed using `.`, if your environment allows env vars with `.` /// /// Note: For the metrics configuration env vars, it is recommended to use the metrics /// specific prefix `IROH_METRICS` to set a field in the metrics config. You can use the /// above dot notation to set a metrics field, eg, `IROH_CONFIG_METRICS.SERVICE_NAME`, but /// only if your environment allows it pub fn load<S, V>( file_paths: &[Option<&Path>], env_prefix: &str, flag_overrides: HashMap<S, V>, ) -> Result<Config> where S: AsRef<str>, V: Into<Value>, { let mut builder = config::Config::builder(); // layer on config options from files for path in file_paths.iter().flatten() { if path.exists() { let p = path.to_str().ok_or_else(|| anyhow::anyhow!("empty path"))?; builder = builder.add_source(File::with_name(p)); } } // next, add any environment variables builder = builder.add_source( Environment::with_prefix(env_prefix) .separator("__") .try_parsing(true), ); // finally, override any values for (flag, val) in flag_overrides.into_iter() { builder = builder.set_override(flag, val)?; } let cfg = builder.build()?; debug!("make_config:\n{:#?}\n", cfg); let cfg = cfg.try_deserialize()?; Ok(cfg) } /// Constructs a `DerpMap` based on the current configuration. pub fn derp_map(&self) -> Option<DerpMap> { if self.derp_regions.is_empty() { return None; } let dm: DerpMap = self.derp_regions.iter().cloned().into(); Some(dm) } } /// Name of directory that wraps all iroh files in a given application directory const IROH_DIR: &str = "iroh"; /// Returns the path to the user's iroh config directory. /// /// If the `IROH_CONFIG_DIR` environment variable is set it will be used unconditionally. /// Otherwise the returned value depends on the operating system according to the following /// table. /// /// | Platform | Value | Example | /// | -------- | ------------------------------------- | -------------------------------- | /// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config/iroh | /home/alice/.config/iroh | /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh | /// | Windows | `{FOLDERID_RoamingAppData}`/iroh | C:\Users\Alice\AppData\Roaming\iroh | pub fn iroh_config_root() -> Result<PathBuf> { if let Some(val) = env::var_os("IROH_CONFIG_DIR") { return Ok(PathBuf::from(val)); } let cfg = dirs_next::config_dir() .ok_or_else(|| anyhow!("operating environment provides no directory for configuration"))?; Ok(cfg.join(IROH_DIR)) } /// Path that leads to a file in the iroh config directory. pub fn iroh_config_path(file_name: impl AsRef<Path>) -> Result<PathBuf> { let path = iroh_config_root()?.join(file_name); Ok(path) } /// Returns the path to the user's iroh data directory. /// /// If the `IROH_DATA_DIR` environment variable is set it will be used unconditionally. /// Otherwise the returned value depends on the operating system according to the following /// table. /// /// | Platform | Value | Example | /// | -------- | --------------------------------------------- | ---------------------------------------- | /// | Linux | `$XDG_DATA_HOME`/iroh or `$HOME`/.local/share/iroh | /home/alice/.local/share/iroh | /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh | /// | Windows | `{FOLDERID_RoamingAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh | pub fn iroh_data_root() -> Result<PathBuf> { if let Some(val) = env::var_os("IROH_DATA_DIR") { return Ok(PathBuf::from(val)); } let path = dirs_next::data_dir().ok_or_else(|| { anyhow!("operating environment provides no directory for application data") })?; Ok(path.join(IROH_DIR)) } /// Path that leads to a file in the iroh data directory. #[allow(dead_code)] pub fn iroh_data_path(file_name: &Path) -> Result<PathBuf> { let path = iroh_data_root()?.join(file_name); Ok(path) } /// Returns the path to the user's iroh cache directory. /// /// If the `IROH_CACHE_DIR` environment variable is set it will be used unconditionally. /// Otherwise the returned value depends on the operating system according to the following /// table. /// /// | Platform | Value | Example | /// | -------- | --------------------------------------------- | ---------------------------------------- | /// | Linux | `$XDG_CACHE_HOME`/iroh or `$HOME`/.cache/iroh | /home/.cache/iroh | /// | macOS | `$HOME`/Library/Caches/iroh | /Users/Alice/Library/Caches/iroh | /// | Windows | `{FOLDERID_LocalAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh | #[allow(dead_code)] pub fn iroh_cache_root() -> Result<PathBuf> { if let Some(val) = env::var_os
IrohPaths::Keypair => "keypair", IrohPaths::BaoFlatStoreComplete => "blobs.v0", IrohPaths::BaoFlatStorePartial => "blobs-partial.v0", }
random_line_split
UAGS.py
HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' def disable(self): self.HEADER = '' self.OKBLUE = '' self.OKGREEN = '' self.WARNING = '' self.FAIL = '' self.ENDC = '' ## main section starting here... print() print(bcolors.BOLD + bcolors.OKBLUE + "HoraceAndTheSpider" + bcolors.ENDC + "'s " + "openretro.org " + bcolors.BOLD + "UAE4Arm Amiga Game Scraper" + bcolors.ENDC + " | " + "" + bcolors.FAIL + "www.ultimateamiga.co.uk" + bcolors.ENDC) print() ## check for overwrite of existing entries NewScrapes = input("Scrape existing game entries? " + bcolors.OKBLUE + "(y/n) " + bcolors.ENDC) if NewScrapes != "yes" and NewScrapes != "y" and NewScrapes != "Y" and NewScrapes != "YES": NewScrapes = "n" print("Existing game entries will " + bcolors.BOLD + "not" + bcolors.ENDC + " be scraped.") else: NewScrapes = "y" print("All found game entries will be scraped.") print() ## all a filter to be used ScanFilter = input("Limit scanned files to a specific pattern match? " + bcolors.OKBLUE + "(Enter pattern or leave blank) " + bcolors.ENDC) print() ## check for overwrite of existing images NewImages = input("Overwrite existing images, such as in " + bcolors.BOLD + "boxarts/" + bcolors.ENDC + " ? " + bcolors.OKBLUE + "(y/n) " + bcolors.ENDC) if NewImages != "yes" and NewImages != "y" and NewImages != "Y" and NewImages != "YES": NewImages = "n" print("Existing images will " + bcolors.BOLD + "not" + bcolors.ENDC + " be overwritten.") else: NewImages = "y" print("Existing images will be overwritten.") print() # Check for saving bonus material images AllImages = input ("Store additional images to " + bcolors.BOLD + "snap/" + bcolors.ENDC + "and" + bcolors.BOLD + " wheel/" + bcolors.ENDC + " ? " + bcolors.OKBLUE + "(y/n) "+ bcolors.ENDC ) if AllImages != "yes" and AllImages != "y" and AllImages != "Y" and AllImages != "YES": AllImages = "n" print ("Additional images will " + bcolors.BOLD + "not"+ bcolors.ENDC + " be stored.") else: AllImages = "y" print ("Additional images will be stored.") print() ## initialisations ScannedGames = 0 LimitResults = 0 ##ScanFilter = "Five" XML = "" ExitButton = False KeyRead = 0 ErrorMessage = "" try: ssl._create_default_https_context = ssl._create_unverified_context except: pass ## -------- input dir if platform.system()=="Darwin": inputdir="/Volumes/roms-1/amiga/" ## inputdir = "/Users/horaceandthespider/Documents/Gaming/AmigaWHD/WorkingFolder2/ECS Pack/" ## -------- I SEE YOU AINGER! o_o elif platform.node()=="RAVEN": inputdir="C:\\Users\\oaing\\Desktop\\whdload\\" else: inputdir="//home/pi/RetroPie/roms/amiga/" # paths/folders if needed os.makedirs(inputdir + "boxart", exist_ok=True) if AllImages == "y": os.makedirs (inputdir + "wheel", exist_ok=True) os.makedirs (inputdir + "snap", exist_ok=True) # here, we will open an existing XML, *or* create one. XML_File = inputdir + "gamelist.xml" if (os.path.isfile(XML_File)) == False: XML = '<?xml version="1.0"?>\n<gameList>\n' XML = XML + "</gameList>" text_file = open(XML_File, "w") text_file.write(XML) text_file.close() text_file = open(XML_File, "r") XML = text_file.read() text_file.close() ## check XML validity if XML.find("?xml version=")<0 or XML.find("<gameList>")<0 or XML.find("</gameList>")<0: print (bcolors.FAIL + ">> XML File "+ bcolors.BOLD + XML_File + bcolors.ENDC + bcolors.FAIL + " is malformed." + bcolors.ENDC ) KillXML = input ("Delete file prior to restart? (y/n) "+ bcolors.ENDC ) if KillXML != "yes" and KillXML != "y" and KillXML != "Y" and KillXML != "YES": raise SystemExit else: print("Deleting 'gamelist.xml'") os.remove(XML_File) raise SystemExit ## ======== MAIN FILE READING LOOP for filename in glob.glob(inputdir+'*.uae'): ScannedGames = ScannedGames + 1 ## filename = "Bloodwych (& Extended Levels).uae" GameVariant = "" GameEntry = "" # Get the name, and remove extension and path GameName = filename GameName = GameName.replace ('.uae','') GameName = GameName.replace (inputdir,'') RealName = filename RealName = RealName.replace (inputdir,'') # finally, this filter should work... if GameName == "_Config Maker": print("Scraping data for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " from external source.") GameEntry = MakeGameEntry(RealName,'','','',AllImages) ErrorMessage = ErrorMessage + GetPictures(RealName,"",AllImages,NewImages,inputdir) elif GameName.find(ScanFilter) == -1 and ScanFilter != '': print("Filter applied. Entry for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " skipped.") # so, if it's aready in there, and we are *not* scraping everything elif NewScrapes=="n" and XML.find("<path>./" + RealName + "</path>") > -1: print("Existing entry for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " - skipping.") ## OMG i cannot believe i just 'tabbed' *everything in the loop for this... else: # Find the game type ## i think i can remove lots of these now.... (1,2,4 and 5) if GameName.find(' [AGA]')>0: GameType = 'AGA' SearchType = 'amiga' GameName = GameName.replace (' [AGA]','') elif GameName.find(' [CD32]')>0: GameType = 'CD32' SearchType = 'cd32' GameName = GameName.replace (' [CD32]','') elif GameName.find(' [CDTV]')>0: GameType = 'CDTV' SearchType = 'cdtv' GameName = GameName.replace (' [CDTV]','') elif GameName.find(' [CD]')>0: GameType = 'AGA' SearchType = 'amiga' GameName = GameName.replace (' [CD]','') else: GameType = 'ECS' SearchType = 'amiga' # Tidy the spaces etc for the search string ParseName = GameName.replace('-','') ParseName = ParseName.replace('&','%26') ParseName = ParseName.replace('+','%2B') ParseName = ParseName.replace(' ','+') ParseName = ParseName.replace('++','+') ## print ("Searched for " + ParseName) print () print (bcolors.OKBLUE + str(ScannedGames) + bcolors.ENDC + ": Searching for: " + bcolors.BOLD + GameName + bcolors.ENDC + " (" + ParseName+") " + GameType) print (bcolors.HEADER + " " + filename + bcolors.ENDC) print () ## lets search the database! FindLink = "" NewParseName = "" ### Here , we can do a loop ## we will 'break' if we find a link though ## pass 1, search as normal ## pass 2, search with brackets as '[' ']' ... for games where a name is shared ## pass 3, search with brackets as '' ... for games like Cannon Fodder (New Campaign) ## pass 4, search with brackets omitted completely and the extra bits stored for later use .. in GameVariant ## ... ## pass
olors:
identifier_name
UAGS.py
else: NewScrapes = "y" print("All found game entries will be scraped.") print() ## all a filter to be used ScanFilter = input("Limit scanned files to a specific pattern match? " + bcolors.OKBLUE + "(Enter pattern or leave blank) " + bcolors.ENDC) print() ## check for overwrite of existing images NewImages = input("Overwrite existing images, such as in " + bcolors.BOLD + "boxarts/" + bcolors.ENDC + " ? " + bcolors.OKBLUE + "(y/n) " + bcolors.ENDC) if NewImages != "yes" and NewImages != "y" and NewImages != "Y" and NewImages != "YES": NewImages = "n" print("Existing images will " + bcolors.BOLD + "not" + bcolors.ENDC + " be overwritten.") else: NewImages = "y" print("Existing images will be overwritten.") print() # Check for saving bonus material images AllImages = input ("Store additional images to " + bcolors.BOLD + "snap/" + bcolors.ENDC + "and" + bcolors.BOLD + " wheel/" + bcolors.ENDC + " ? " + bcolors.OKBLUE + "(y/n) "+ bcolors.ENDC ) if AllImages != "yes" and AllImages != "y" and AllImages != "Y" and AllImages != "YES": AllImages = "n" print ("Additional images will " + bcolors.BOLD + "not"+ bcolors.ENDC + " be stored.") else: AllImages = "y" print ("Additional images will be stored.") print() ## initialisations ScannedGames = 0 LimitResults = 0 ##ScanFilter = "Five" XML = "" ExitButton = False KeyRead = 0 ErrorMessage = "" try: ssl._create_default_https_context = ssl._create_unverified_context except: pass ## -------- input dir if platform.system()=="Darwin": inputdir="/Volumes/roms-1/amiga/" ## inputdir = "/Users/horaceandthespider/Documents/Gaming/AmigaWHD/WorkingFolder2/ECS Pack/" ## -------- I SEE YOU AINGER! o_o elif platform.node()=="RAVEN": inputdir="C:\\Users\\oaing\\Desktop\\whdload\\" else: inputdir="//home/pi/RetroPie/roms/amiga/" # paths/folders if needed os.makedirs(inputdir + "boxart", exist_ok=True) if AllImages == "y": os.makedirs (inputdir + "wheel", exist_ok=True) os.makedirs (inputdir + "snap", exist_ok=True) # here, we will open an existing XML, *or* create one. XML_File = inputdir + "gamelist.xml" if (os.path.isfile(XML_File)) == False: XML = '<?xml version="1.0"?>\n<gameList>\n' XML = XML + "</gameList>" text_file = open(XML_File, "w") text_file.write(XML) text_file.close()
## check XML validity if XML.find("?xml version=")<0 or XML.find("<gameList>")<0 or XML.find("</gameList>")<0: print (bcolors.FAIL + ">> XML File "+ bcolors.BOLD + XML_File + bcolors.ENDC + bcolors.FAIL + " is malformed." + bcolors.ENDC ) KillXML = input ("Delete file prior to restart? (y/n) "+ bcolors.ENDC ) if KillXML != "yes" and KillXML != "y" and KillXML != "Y" and KillXML != "YES": raise SystemExit else: print("Deleting 'gamelist.xml'") os.remove(XML_File) raise SystemExit ## ======== MAIN FILE READING LOOP for filename in glob.glob(inputdir+'*.uae'): ScannedGames = ScannedGames + 1 ## filename = "Bloodwych (& Extended Levels).uae" GameVariant = "" GameEntry = "" # Get the name, and remove extension and path GameName = filename GameName = GameName.replace ('.uae','') GameName = GameName.replace (inputdir,'') RealName = filename RealName = RealName.replace (inputdir,'') # finally, this filter should work... if GameName == "_Config Maker": print("Scraping data for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " from external source.") GameEntry = MakeGameEntry(RealName,'','','',AllImages) ErrorMessage = ErrorMessage + GetPictures(RealName,"",AllImages,NewImages,inputdir) elif GameName.find(ScanFilter) == -1 and ScanFilter != '': print("Filter applied. Entry for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " skipped.") # so, if it's aready in there, and we are *not* scraping everything elif NewScrapes=="n" and XML.find("<path>./" + RealName + "</path>") > -1: print("Existing entry for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " - skipping.") ## OMG i cannot believe i just 'tabbed' *everything in the loop for this... else: # Find the game type ## i think i can remove lots of these now.... (1,2,4 and 5) if GameName.find(' [AGA]')>0: GameType = 'AGA' SearchType = 'amiga' GameName = GameName.replace (' [AGA]','') elif GameName.find(' [CD32]')>0: GameType = 'CD32' SearchType = 'cd32' GameName = GameName.replace (' [CD32]','') elif GameName.find(' [CDTV]')>0: GameType = 'CDTV' SearchType = 'cdtv' GameName = GameName.replace (' [CDTV]','') elif GameName.find(' [CD]')>0: GameType = 'AGA' SearchType = 'amiga' GameName = GameName.replace (' [CD]','') else: GameType = 'ECS' SearchType = 'amiga' # Tidy the spaces etc for the search string ParseName = GameName.replace('-','') ParseName = ParseName.replace('&','%26') ParseName = ParseName.replace('+','%2B') ParseName = ParseName.replace(' ','+') ParseName = ParseName.replace('++','+') ## print ("Searched for " + ParseName) print () print (bcolors.OKBLUE + str(ScannedGames) + bcolors.ENDC + ": Searching for: " + bcolors.BOLD + GameName + bcolors.ENDC + " (" + ParseName+") " + GameType) print (bcolors.HEADER + " " + filename + bcolors.ENDC) print () ## lets search the database! FindLink = "" NewParseName = "" ### Here , we can do a loop ## we will 'break' if we find a link though ## pass 1, search as normal ## pass 2, search with brackets as '[' ']' ... for games where a name is shared ## pass 3, search with brackets as '' ... for games like Cannon Fodder (New Campaign) ## pass 4, search with brackets omitted completely and the extra bits stored for later use .. in GameVariant ## ... ## pass 9, ??? ## pass 10, profit! ## if we *didnt find anything, we re-try, with anything in brackets removed (alternative versions etc) ## -- we will also store the brackets information to put in the XML description (to show different versions apart) ##import re ##re.sub(r'\s\(.*\)', '', "Lemmings (2 Disk)") for Pass in range(1,5): # special 'pass' rules if Pass==1: NewParseName = ParseName elif Pass==2: NewParseName = ParseName NewParseName = NewParseName.replace(')','') NewParseName = NewParseName.replace('(','') elif Pass==3: NewParseName = ParseName NewParseName = NewParseName.replace('[','') NewParseName = NewParseName.replace(']','') elif Pass==4: NewParseName = ParseName NewParseName = NewParseName.replace(GetBrackets(ParseName),'').strip() GameVariant = GetBrackets(GameName) ## here we do the actual searches ## first of all, we have a special rule for AGA games, because they are pain in the b*m if GameType == 'AGA': SearchString = 'https://openretro.org/browse/'+SearchType+'?q=' + NewParseName + "+[AGA]"+"&disabled=1&unpublished=1" FindLink,FindImage,FindGame = WebSearchResult(Search
text_file = open(XML_File, "r") XML = text_file.read() text_file.close()
random_line_split
UAGS.py
Ne
print() ## all a filter to be used ScanFilter = input("Limit scanned files to a specific pattern match? " + bcolors.OKBLUE + "(Enter pattern or leave blank) " + bcolors.ENDC) print() ## check for overwrite of existing images NewImages = input("Overwrite existing images, such as in " + bcolors.BOLD + "boxarts/" + bcolors.ENDC + " ? " + bcolors.OKBLUE + "(y/n) " + bcolors.ENDC) if NewImages != "yes" and NewImages != "y" and NewImages != "Y" and NewImages != "YES": NewImages = "n" print("Existing images will " + bcolors.BOLD + "not" + bcolors.ENDC + " be overwritten.") else: NewImages = "y" print("Existing images will be overwritten.") print() # Check for saving bonus material images AllImages = input ("Store additional images to " + bcolors.BOLD + "snap/" + bcolors.ENDC + "and" + bcolors.BOLD + " wheel/" + bcolors.ENDC + " ? " + bcolors.OKBLUE + "(y/n) "+ bcolors.ENDC ) if AllImages != "yes" and AllImages != "y" and AllImages != "Y" and AllImages != "YES": AllImages = "n" print ("Additional images will " + bcolors.BOLD + "not"+ bcolors.ENDC + " be stored.") else: AllImages = "y" print ("Additional images will be stored.") print() ## initialisations ScannedGames = 0 LimitResults = 0 ##ScanFilter = "Five" XML = "" ExitButton = False KeyRead = 0 ErrorMessage = "" try: ssl._create_default_https_context = ssl._create_unverified_context except: pass ## -------- input dir if platform.system()=="Darwin": inputdir="/Volumes/roms-1/amiga/" ## inputdir = "/Users/horaceandthespider/Documents/Gaming/AmigaWHD/WorkingFolder2/ECS Pack/" ## -------- I SEE YOU AINGER! o_o elif platform.node()=="RAVEN": inputdir="C:\\Users\\oaing\\Desktop\\whdload\\" else: inputdir="//home/pi/RetroPie/roms/amiga/" # paths/folders if needed os.makedirs(inputdir + "boxart", exist_ok=True) if AllImages == "y": os.makedirs (inputdir + "wheel", exist_ok=True) os.makedirs (inputdir + "snap", exist_ok=True) # here, we will open an existing XML, *or* create one. XML_File = inputdir + "gamelist.xml" if (os.path.isfile(XML_File)) == False: XML = '<?xml version="1.0"?>\n<gameList>\n' XML = XML + "</gameList>" text_file = open(XML_File, "w") text_file.write(XML) text_file.close() text_file = open(XML_File, "r") XML = text_file.read() text_file.close() ## check XML validity if XML.find("?xml version=")<0 or XML.find("<gameList>")<0 or XML.find("</gameList>")<0: print (bcolors.FAIL + ">> XML File "+ bcolors.BOLD + XML_File + bcolors.ENDC + bcolors.FAIL + " is malformed." + bcolors.ENDC ) KillXML = input ("Delete file prior to restart? (y/n) "+ bcolors.ENDC ) if KillXML != "yes" and KillXML != "y" and KillXML != "Y" and KillXML != "YES": raise SystemExit else: print("Deleting 'gamelist.xml'") os.remove(XML_File) raise SystemExit ## ======== MAIN FILE READING LOOP for filename in glob.glob(inputdir+'*.uae'): ScannedGames = ScannedGames + 1 ## filename = "Bloodwych (& Extended Levels).uae" GameVariant = "" GameEntry = "" # Get the name, and remove extension and path GameName = filename GameName = GameName.replace ('.uae','') GameName = GameName.replace (inputdir,'') RealName = filename RealName = RealName.replace (inputdir,'') # finally, this filter should work... if GameName == "_Config Maker": print("Scraping data for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " from external source.") GameEntry = MakeGameEntry(RealName,'','','',AllImages) ErrorMessage = ErrorMessage + GetPictures(RealName,"",AllImages,NewImages,inputdir) elif GameName.find(ScanFilter) == -1 and ScanFilter != '': print("Filter applied. Entry for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " skipped.") # so, if it's aready in there, and we are *not* scraping everything elif NewScrapes=="n" and XML.find("<path>./" + RealName + "</path>") > -1: print("Existing entry for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " - skipping.") ## OMG i cannot believe i just 'tabbed' *everything in the loop for this... else: # Find the game type ## i think i can remove lots of these now.... (1,2,4 and 5) if GameName.find(' [AGA]')>0: GameType = 'AGA' SearchType = 'amiga' GameName = GameName.replace (' [AGA]','') elif GameName.find(' [CD32]')>0: GameType = 'CD32' SearchType = 'cd32' GameName = GameName.replace (' [CD32]','') elif GameName.find(' [CDTV]')>0: GameType = 'CDTV' SearchType = 'cdtv' GameName = GameName.replace (' [CDTV]','') elif GameName.find(' [CD]')>0: GameType = 'AGA' SearchType = 'amiga' GameName = GameName.replace (' [CD]','') else: GameType = 'ECS' SearchType = 'amiga' # Tidy the spaces etc for the search string ParseName = GameName.replace('-','') ParseName = ParseName.replace('&','%26') ParseName = ParseName.replace('+','%2B') ParseName = ParseName.replace(' ','+') ParseName = ParseName.replace('++','+') ## print ("Searched for " + ParseName) print () print (bcolors.OKBLUE + str(ScannedGames) + bcolors.ENDC + ": Searching for: " + bcolors.BOLD + GameName + bcolors.ENDC + " (" + ParseName+") " + GameType) print (bcolors.HEADER + " " + filename + bcolors.ENDC) print () ## lets search the database! FindLink = "" NewParseName = "" ### Here , we can do a loop ## we will 'break' if we find a link though ## pass 1, search as normal ## pass 2, search with brackets as '[' ']' ... for games where a name is shared ## pass 3, search with brackets as '' ... for games like Cannon Fodder (New Campaign) ## pass 4, search with brackets omitted completely and the extra bits stored for later use .. in GameVariant ## ... ## pass 9, ??? ## pass 10, profit! ## if we *didnt find anything, we re-try, with anything in brackets removed (alternative versions etc) ## -- we will also store the brackets information to put in the XML description (to show different versions apart) ##import re ##re.sub(r'\s\(.*\)', '', "Lemmings (2 Disk)") for Pass in range(1,5): # special 'pass' rules if Pass==1: NewParseName = ParseName elif Pass==2: NewParseName = ParseName NewParseName = NewParseName.replace(')','') NewParseName = NewParseName.replace('(','') elif Pass==3: NewParseName = ParseName NewParseName = NewParseName.replace('[','') NewParseName = NewParseName.replace(']','') elif Pass==4: NewParseName = ParseName NewParseName = NewParseName.replace(GetBrackets(ParseName),'').strip() GameVariant = GetBrackets(GameName) ## here we do the actual searches ## first of all, we have a special rule for AGA games, because they are pain in the b*m if GameType == 'AGA': SearchString = 'https://openretro.org/browse/'+SearchType+'?q=' + NewParseName + "+[AGA]"+"&disabled=1&unpublished=1" FindLink,FindImage,FindGame = WebSearchResult(Search
wScrapes = "y" print("All found game entries will be scraped.")
conditional_block
UAGS.py
## main section starting here... print() print(bcolors.BOLD + bcolors.OKBLUE + "HoraceAndTheSpider" + bcolors.ENDC + "'s " + "openretro.org " + bcolors.BOLD + "UAE4Arm Amiga Game Scraper" + bcolors.ENDC + " | " + "" + bcolors.FAIL + "www.ultimateamiga.co.uk" + bcolors.ENDC) print() ## check for overwrite of existing entries NewScrapes = input("Scrape existing game entries? " + bcolors.OKBLUE + "(y/n) " + bcolors.ENDC) if NewScrapes != "yes" and NewScrapes != "y" and NewScrapes != "Y" and NewScrapes != "YES": NewScrapes = "n" print("Existing game entries will " + bcolors.BOLD + "not" + bcolors.ENDC + " be scraped.") else: NewScrapes = "y" print("All found game entries will be scraped.") print() ## all a filter to be used ScanFilter = input("Limit scanned files to a specific pattern match? " + bcolors.OKBLUE + "(Enter pattern or leave blank) " + bcolors.ENDC) print() ## check for overwrite of existing images NewImages = input("Overwrite existing images, such as in " + bcolors.BOLD + "boxarts/" + bcolors.ENDC + " ? " + bcolors.OKBLUE + "(y/n) " + bcolors.ENDC) if NewImages != "yes" and NewImages != "y" and NewImages != "Y" and NewImages != "YES": NewImages = "n" print("Existing images will " + bcolors.BOLD + "not" + bcolors.ENDC + " be overwritten.") else: NewImages = "y" print("Existing images will be overwritten.") print() # Check for saving bonus material images AllImages = input ("Store additional images to " + bcolors.BOLD + "snap/" + bcolors.ENDC + "and" + bcolors.BOLD + " wheel/" + bcolors.ENDC + " ? " + bcolors.OKBLUE + "(y/n) "+ bcolors.ENDC ) if AllImages != "yes" and AllImages != "y" and AllImages != "Y" and AllImages != "YES": AllImages = "n" print ("Additional images will " + bcolors.BOLD + "not"+ bcolors.ENDC + " be stored.") else: AllImages = "y" print ("Additional images will be stored.") print() ## initialisations ScannedGames = 0 LimitResults = 0 ##ScanFilter = "Five" XML = "" ExitButton = False KeyRead = 0 ErrorMessage = "" try: ssl._create_default_https_context = ssl._create_unverified_context except: pass ## -------- input dir if platform.system()=="Darwin": inputdir="/Volumes/roms-1/amiga/" ## inputdir = "/Users/horaceandthespider/Documents/Gaming/AmigaWHD/WorkingFolder2/ECS Pack/" ## -------- I SEE YOU AINGER! o_o elif platform.node()=="RAVEN": inputdir="C:\\Users\\oaing\\Desktop\\whdload\\" else: inputdir="//home/pi/RetroPie/roms/amiga/" # paths/folders if needed os.makedirs(inputdir + "boxart", exist_ok=True) if AllImages == "y": os.makedirs (inputdir + "wheel", exist_ok=True) os.makedirs (inputdir + "snap", exist_ok=True) # here, we will open an existing XML, *or* create one. XML_File = inputdir + "gamelist.xml" if (os.path.isfile(XML_File)) == False: XML = '<?xml version="1.0"?>\n<gameList>\n' XML = XML + "</gameList>" text_file = open(XML_File, "w") text_file.write(XML) text_file.close() text_file = open(XML_File, "r") XML = text_file.read() text_file.close() ## check XML validity if XML.find("?xml version=")<0 or XML.find("<gameList>")<0 or XML.find("</gameList>")<0: print (bcolors.FAIL + ">> XML File "+ bcolors.BOLD + XML_File + bcolors.ENDC + bcolors.FAIL + " is malformed." + bcolors.ENDC ) KillXML = input ("Delete file prior to restart? (y/n) "+ bcolors.ENDC ) if KillXML != "yes" and KillXML != "y" and KillXML != "Y" and KillXML != "YES": raise SystemExit else: print("Deleting 'gamelist.xml'") os.remove(XML_File) raise SystemExit ## ======== MAIN FILE READING LOOP for filename in glob.glob(inputdir+'*.uae'): ScannedGames = ScannedGames + 1 ## filename = "Bloodwych (& Extended Levels).uae" GameVariant = "" GameEntry = "" # Get the name, and remove extension and path GameName = filename GameName = GameName.replace ('.uae','') GameName = GameName.replace (inputdir,'') RealName = filename RealName = RealName.replace (inputdir,'') # finally, this filter should work... if GameName == "_Config Maker": print("Scraping data for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " from external source.") GameEntry = MakeGameEntry(RealName,'','','',AllImages) ErrorMessage = ErrorMessage + GetPictures(RealName,"",AllImages,NewImages,inputdir) elif GameName.find(ScanFilter) == -1 and ScanFilter != '': print("Filter applied. Entry for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " skipped.") # so, if it's aready in there, and we are *not* scraping everything elif NewScrapes=="n" and XML.find("<path>./" + RealName + "</path>") > -1: print("Existing entry for " + bcolors.OKGREEN + GameName + bcolors.ENDC + " - skipping.") ## OMG i cannot believe i just 'tabbed' *everything in the loop for this... else: # Find the game type ## i think i can remove lots of these now.... (1,2,4 and 5) if GameName.find(' [AGA]')>0: GameType = 'AGA' SearchType = 'amiga' GameName = GameName.replace (' [AGA]','') elif GameName.find(' [CD32]')>0: GameType = 'CD32' SearchType = 'cd32' GameName = GameName.replace (' [CD32]','') elif GameName.find(' [CDTV]')>0: GameType = 'CDTV' SearchType = 'cdtv' GameName = GameName.replace (' [CDTV]','') elif GameName.find(' [CD]')>0: GameType = 'AGA' SearchType = 'amiga' GameName = GameName.replace (' [CD]','') else: GameType = 'ECS' SearchType = 'amiga' # Tidy the spaces etc for the search string ParseName = GameName.replace('-','') ParseName = ParseName.replace('&','%26') ParseName = ParseName.replace('+','%2B') ParseName = ParseName.replace(' ','+') ParseName = ParseName.replace('++','+') ## print ("Searched for " + ParseName) print () print (bcolors.OKBLUE + str(ScannedGames) + bcolors.ENDC + ": Searching for: " + bcolors.BOLD + GameName + bcolors.ENDC + " (" + ParseName+") " + GameType) print (bcolors.HEADER + " " + filename + bcolors.ENDC) print () ## lets search the database! FindLink = "" NewParseName = "" ### Here , we can do a loop ## we will 'break' if we find a link though ## pass 1, search as normal ## pass 2, search with brackets as '[' ']' ... for games where a name is shared ## pass 3, search with brackets as '' ... for games like Cannon Fodder (New Campaign) ## pass 4, search with brackets omitted completely and the extra bits stored for later use .. in GameVariant ## ... ## pass 9, ???
ADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' def disable(self): self.HEADER = '' self.OKBLUE = '' self.OKGREEN = '' self.WARNING = '' self.FAIL = '' self.ENDC = ''
identifier_body
databaseDemo.go
(dbfile string) *sql.DB { database, err := sql.Open("sqlite3", dbfile) if err != nil { log.Fatal(err) } return database } func getMinGPA() float64 { fmt.Print("What is the minimum GPA for good standing:") reader := bufio.NewReader(os.Stdin) value, err := reader.ReadString('\n') if err != nil { log.Fatal("How did we fail to read from standard in!?!?") } value = strings.TrimSpace(value) min_gpa, err := strconv.ParseFloat(value, 32) if err != nil { log.Fatal("oooops you typed that wrong", err) } return min_gpa } func findProbationStudents(database *sql.DB) { var firstName, lastName string var gpa float64 minGpa := getMinGPA() selectStatement := "SELECT first_name, last_name, gpa FROM STUDENTS WHERE gpa < ?" resultSet, err := database.Query(selectStatement, minGpa) if err != nil { log.Fatal("Bad Query", err) } defer resultSet.Close() for resultSet.Next() { err = resultSet.Scan(&firstName, &lastName, &gpa) if err != nil { log.Fatal(err) } fmt.Printf("%s %s is on probation with a GPA of %f\n", firstName, lastName, gpa) } } func registerForClasses(database *sql.DB) { insertStatement := "INSERT INTO CLASS_LIST (banner_id, course_prefix, course_number, registration_date)" + "VALUES(?, 'Comp', 510, DATE('now'))" preppedStatement, err := database.Prepare(insertStatement) if err != nil { log.Fatal("Hey prof you goofed it trying to type live", err) } for i := 1001; i <= 1008; i++ { preppedStatement.Exec(i) } } func addCourses(database *sql.DB) { var sampleData = map[string]string{ "comp502": "Research\n(3 credits)\nPrerequisite: Consent of the department; formal application required\nOriginal research is undertaken by the graduate student in their field. This course culminates in a capstone project. For details, consult the paragraph titled “Directed or Independent Study” in the “College of Graduate Studies” section of this catalog. Offered fall and spring semesters.", "comp503": "Directed Study\n(1-3 credits)\nPrerequisite: Consent of the department; formal application required\nDirected study is designed for the graduate student who desires to study selected topics in a specific field. For details, consult the paragraph titled “Directed or Independent Study” in the “College of Graduate Studies” section of this catalog. Repeatable: may earn a maximum of six credits. Offered fall and spring semesters.", "comp510": "Topics in Programming Languages\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course investigates programming language development from designer’s, user’s and implementer’s point of view. Topics include formal syntax and semantics, language system, extensible languages and control structures. There is also a survey of intralanguage features, covering ALGOL-60, ALGOL-68, Ada, Pascal, LISP, SNOBOL-4 APL, SIMULA-67, CLU, MODULA, and others. Offered periodically.", "comp520": "Operating Systems Principles\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course examines design principles such as optimal scheduling; file systems, system integrity and security, as well as the mathematical analysis of selected aspects of operating system design. Topics include queuing theory, disk scheduling, storage management and the working set model. Design and implementation of an operating system nucleus is also studied. Offered periodically.", "comp525": "Design and Construction of Compilers\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics will include lexical and syntactic analysis; code generation; error detection and correction; optimization techniques; models of code generators; and incremental and interactive compiling. Students will design and implement a compiler. Offered periodically.", "comp530": "Software Engineering\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nTopics in this course will include construction of reliable software, software tools, software testing methodologies, structured design, structured programming, software characteristics and quality and formal proofs of program correctness. Chief programmer teams and structure walk-throughs will be employed. Offered periodically.\n", "comp540": "Automata, Computability and Formal Languages\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nTopics in this course will include finite automata and regular languages, context- free languages, Turing machines and their variants, partial recursive functions and grammars, Church’s thesis, undecidable problems, complexity of algorithms and completeness. Offered periodically.", "comp545": "Analysis of Algorithms\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course deals with techniques in the analysis of algorithms. Topics to be chosen from among the following: dynamic programming, search and traverse techniques, backtracking, numerical techniques, NP-hard and NP-complete problems, approximation algorithms and other topics in the analysis and design of algorithms. Offered fall semester.\n", "comp560": "Artificial Intelligence\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course is an introduction to LISP or another AI programming language. Topics are chosen from pattern recognition, theorem proving, learning, cognitive science and vision. It also presents introduction to the basic techniques of AI such as heuristic search, semantic nets, production systems, frames, planning and other AI topics. Offered periodically.\n", "comp570": "Robotics\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis is a project-oriented course in robotics. Topics are chosen from manipulator motion and control, motion planning, legged-motion, vision, touch sensing, grasping, programming languages for robots and automated factory design. Offered periodically.", "comp580": "Database Systems\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics will include relational, hierarchical and network data models; design theory for relational databases and query optimization; classification of data models, data languages; concurrency, integrity, privacy; modeling and measurement of access strategies; and dedicated processors, information retrieval and real time applications. Offered periodically.", "comp590": "Computer Architecture\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course is an introduction to the internal structure of digital computers including design of gates, flip-fops, registers and memories to perform operations on numerical and other data represented in binary form; computer system analysis and design; organizational dependence on computations to be performed; and theoretical aspects of parallel and pipeline computation. Offered periodically.", "comp594": "Computer Networks\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course provides an introduction to fundamental concepts in computer networks, including their design and implementation. Topics include network architectures and protocols, placing emphasis on protocol used in the Internet; routing; data link layer issues; multimedia networking; network security; and network management. Offered periodically.\n", "comp596": "Topics in Computer Science\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics are chosen from program verification, formal semantics, formal language theory, concurrent programming, complexity or algorithms, programming language theory, graphics and other computer science topics. Repeatable for different topics. Offered as topics arise.", "comp598": " Computer Science Graduate Internship\n(3 credits)\nPrerequisite: Matriculation in the computer science master’s program; at least six credits of graduate-level course work in computer science (COMP); formal application required\nAn internship provides an opportunity to apply what has been learned in the classroom and allows the student to further professional skills. Faculty supervision allows for reflection on the internship experience and connects the applied portion of the academic study to other courses. Repeatable; may earn a maximum of six credits, however, only three credits can be used toward the degree. Graded on (P) Pass/(N) No Pass basis. Offered fall and spring semesters.\n", } insertStatement := "INSERT INTO COURSE (course_prefix, course_number, description) VALUES (?,?,?);" preppedStatement, err := database.Prepare(insertStatement) if err != nil { log.Fatal(err) } for course, desc := range sampleData { prefix := course[0:4] numVal := course[4:7] courseNum, err := strconv.Atoi(numVal) if err != nil { log.Fatal("ooops we must have mistyped", err) } preppedStatement.Exec(prefix, courseNum, desc) } } func addSampleStudents(database *sql.DB) { sampleNames := map[string]string{"John": "Santore", "Enping": "Li
OpenDataBase
identifier_name
databaseDemo.go
) value, err := reader.ReadString('\n') if err != nil { log.Fatal("How did we fail to read from standard in!?!?") } value = strings.TrimSpace(value) min_gpa, err := strconv.ParseFloat(value, 32) if err != nil { log.Fatal("oooops you typed that wrong", err) } return min_gpa } func findProbationStudents(database *sql.DB) { var firstName, lastName string var gpa float64 minGpa := getMinGPA() selectStatement := "SELECT first_name, last_name, gpa FROM STUDENTS WHERE gpa < ?" resultSet, err := database.Query(selectStatement, minGpa) if err != nil { log.Fatal("Bad Query", err) } defer resultSet.Close() for resultSet.Next() { err = resultSet.Scan(&firstName, &lastName, &gpa) if err != nil {
} } func registerForClasses(database *sql.DB) { insertStatement := "INSERT INTO CLASS_LIST (banner_id, course_prefix, course_number, registration_date)" + "VALUES(?, 'Comp', 510, DATE('now'))" preppedStatement, err := database.Prepare(insertStatement) if err != nil { log.Fatal("Hey prof you goofed it trying to type live", err) } for i := 1001; i <= 1008; i++ { preppedStatement.Exec(i) } } func addCourses(database *sql.DB) { var sampleData = map[string]string{ "comp502": "Research\n(3 credits)\nPrerequisite: Consent of the department; formal application required\nOriginal research is undertaken by the graduate student in their field. This course culminates in a capstone project. For details, consult the paragraph titled “Directed or Independent Study” in the “College of Graduate Studies” section of this catalog. Offered fall and spring semesters.", "comp503": "Directed Study\n(1-3 credits)\nPrerequisite: Consent of the department; formal application required\nDirected study is designed for the graduate student who desires to study selected topics in a specific field. For details, consult the paragraph titled “Directed or Independent Study” in the “College of Graduate Studies” section of this catalog. Repeatable: may earn a maximum of six credits. Offered fall and spring semesters.", "comp510": "Topics in Programming Languages\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course investigates programming language development from designer’s, user’s and implementer’s point of view. Topics include formal syntax and semantics, language system, extensible languages and control structures. There is also a survey of intralanguage features, covering ALGOL-60, ALGOL-68, Ada, Pascal, LISP, SNOBOL-4 APL, SIMULA-67, CLU, MODULA, and others. Offered periodically.", "comp520": "Operating Systems Principles\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course examines design principles such as optimal scheduling; file systems, system integrity and security, as well as the mathematical analysis of selected aspects of operating system design. Topics include queuing theory, disk scheduling, storage management and the working set model. Design and implementation of an operating system nucleus is also studied. Offered periodically.", "comp525": "Design and Construction of Compilers\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics will include lexical and syntactic analysis; code generation; error detection and correction; optimization techniques; models of code generators; and incremental and interactive compiling. Students will design and implement a compiler. Offered periodically.", "comp530": "Software Engineering\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nTopics in this course will include construction of reliable software, software tools, software testing methodologies, structured design, structured programming, software characteristics and quality and formal proofs of program correctness. Chief programmer teams and structure walk-throughs will be employed. Offered periodically.\n", "comp540": "Automata, Computability and Formal Languages\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nTopics in this course will include finite automata and regular languages, context- free languages, Turing machines and their variants, partial recursive functions and grammars, Church’s thesis, undecidable problems, complexity of algorithms and completeness. Offered periodically.", "comp545": "Analysis of Algorithms\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course deals with techniques in the analysis of algorithms. Topics to be chosen from among the following: dynamic programming, search and traverse techniques, backtracking, numerical techniques, NP-hard and NP-complete problems, approximation algorithms and other topics in the analysis and design of algorithms. Offered fall semester.\n", "comp560": "Artificial Intelligence\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course is an introduction to LISP or another AI programming language. Topics are chosen from pattern recognition, theorem proving, learning, cognitive science and vision. It also presents introduction to the basic techniques of AI such as heuristic search, semantic nets, production systems, frames, planning and other AI topics. Offered periodically.\n", "comp570": "Robotics\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis is a project-oriented course in robotics. Topics are chosen from manipulator motion and control, motion planning, legged-motion, vision, touch sensing, grasping, programming languages for robots and automated factory design. Offered periodically.", "comp580": "Database Systems\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics will include relational, hierarchical and network data models; design theory for relational databases and query optimization; classification of data models, data languages; concurrency, integrity, privacy; modeling and measurement of access strategies; and dedicated processors, information retrieval and real time applications. Offered periodically.", "comp590": "Computer Architecture\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course is an introduction to the internal structure of digital computers including design of gates, flip-fops, registers and memories to perform operations on numerical and other data represented in binary form; computer system analysis and design; organizational dependence on computations to be performed; and theoretical aspects of parallel and pipeline computation. Offered periodically.", "comp594": "Computer Networks\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course provides an introduction to fundamental concepts in computer networks, including their design and implementation. Topics include network architectures and protocols, placing emphasis on protocol used in the Internet; routing; data link layer issues; multimedia networking; network security; and network management. Offered periodically.\n", "comp596": "Topics in Computer Science\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics are chosen from program verification, formal semantics, formal language theory, concurrent programming, complexity or algorithms, programming language theory, graphics and other computer science topics. Repeatable for different topics. Offered as topics arise.", "comp598": " Computer Science Graduate Internship\n(3 credits)\nPrerequisite: Matriculation in the computer science master’s program; at least six credits of graduate-level course work in computer science (COMP); formal application required\nAn internship provides an opportunity to apply what has been learned in the classroom and allows the student to further professional skills. Faculty supervision allows for reflection on the internship experience and connects the applied portion of the academic study to other courses. Repeatable; may earn a maximum of six credits, however, only three credits can be used toward the degree. Graded on (P) Pass/(N) No Pass basis. Offered fall and spring semesters.\n", } insertStatement := "INSERT INTO COURSE (course_prefix, course_number, description) VALUES (?,?,?);" preppedStatement, err := database.Prepare(insertStatement) if err != nil { log.Fatal(err) } for course, desc := range sampleData { prefix := course[0:4] numVal := course[4:7] courseNum, err := strconv.Atoi(numVal) if err != nil { log.Fatal("ooops we must have mistyped", err) } preppedStatement.Exec(prefix, courseNum, desc) } } func addSampleStudents(database *sql.DB) { sampleNames := map[string]string{"John": "Santore", "Enping": "Li", "Michael": "Black", "Seikyung": "Jung", "Haleh": "Khojasteh", "Abdul": "Sattar", "Paul": "Kim", "Yiheng": "Liang"} statement := "INSERT INTO STUDENTS (banner_id, first_name
log.Fatal(err) } fmt.Printf("%s %s is on probation with a GPA of %f\n", firstName, lastName, gpa)
random_line_split
databaseDemo.go
value, err := reader.ReadString('\n') if err != nil { log.Fatal("How did we fail to read from standard in!?!?") } value = strings.TrimSpace(value) min_gpa, err := strconv.ParseFloat(value, 32) if err != nil { log.Fatal("oooops you typed that wrong", err) } return min_gpa } func findProbationStudents(database *sql.DB) { var firstName, lastName string var gpa float64 minGpa := getMinGPA() selectStatement := "SELECT first_name, last_name, gpa FROM STUDENTS WHERE gpa < ?" resultSet, err := database.Query(selectStatement, minGpa) if err != nil { log.Fatal("Bad Query", err) } defer resultSet.Close() for resultSet.Next() { err = resultSet.Scan(&firstName, &lastName, &gpa) if err != nil { log.Fatal(err) } fmt.Printf("%s %s is on probation with a GPA of %f\n", firstName, lastName, gpa) } } func registerForClasses(database *sql.DB) { insertStatement := "INSERT INTO CLASS_LIST (banner_id, course_prefix, course_number, registration_date)" + "VALUES(?, 'Comp', 510, DATE('now'))" preppedStatement, err := database.Prepare(insertStatement) if err != nil { log.Fatal("Hey prof you goofed it trying to type live", err) } for i := 1001; i <= 1008; i++ { preppedStatement.Exec(i) } } func addCourses(database *sql.DB)
if err != nil { log.Fatal(err) } for course, desc := range sampleData { prefix := course[0:4] numVal := course[4:7] courseNum, err := strconv.Atoi(numVal) if err != nil { log.Fatal("ooops we must have mistyped", err) } preppedStatement.Exec(prefix, courseNum, desc) } } func addSampleStudents(d atabase *sql.DB) { sampleNames := map[string]string{"John": "Santore", "Enping": "Li", "Michael": "Black", "Seikyung": "Jung", "Haleh": "Khojasteh", "Abdul": "Sattar", "Paul": "Kim", "Yiheng": "Liang"} statement := "INSERT INTO STUDENTS (banner_id,
{ var sampleData = map[string]string{ "comp502": "Research\n(3 credits)\nPrerequisite: Consent of the department; formal application required\nOriginal research is undertaken by the graduate student in their field. This course culminates in a capstone project. For details, consult the paragraph titled “Directed or Independent Study” in the “College of Graduate Studies” section of this catalog. Offered fall and spring semesters.", "comp503": "Directed Study\n(1-3 credits)\nPrerequisite: Consent of the department; formal application required\nDirected study is designed for the graduate student who desires to study selected topics in a specific field. For details, consult the paragraph titled “Directed or Independent Study” in the “College of Graduate Studies” section of this catalog. Repeatable: may earn a maximum of six credits. Offered fall and spring semesters.", "comp510": "Topics in Programming Languages\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course investigates programming language development from designer’s, user’s and implementer’s point of view. Topics include formal syntax and semantics, language system, extensible languages and control structures. There is also a survey of intralanguage features, covering ALGOL-60, ALGOL-68, Ada, Pascal, LISP, SNOBOL-4 APL, SIMULA-67, CLU, MODULA, and others. Offered periodically.", "comp520": "Operating Systems Principles\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course examines design principles such as optimal scheduling; file systems, system integrity and security, as well as the mathematical analysis of selected aspects of operating system design. Topics include queuing theory, disk scheduling, storage management and the working set model. Design and implementation of an operating system nucleus is also studied. Offered periodically.", "comp525": "Design and Construction of Compilers\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics will include lexical and syntactic analysis; code generation; error detection and correction; optimization techniques; models of code generators; and incremental and interactive compiling. Students will design and implement a compiler. Offered periodically.", "comp530": "Software Engineering\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nTopics in this course will include construction of reliable software, software tools, software testing methodologies, structured design, structured programming, software characteristics and quality and formal proofs of program correctness. Chief programmer teams and structure walk-throughs will be employed. Offered periodically.\n", "comp540": "Automata, Computability and Formal Languages\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nTopics in this course will include finite automata and regular languages, context- free languages, Turing machines and their variants, partial recursive functions and grammars, Church’s thesis, undecidable problems, complexity of algorithms and completeness. Offered periodically.", "comp545": "Analysis of Algorithms\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course deals with techniques in the analysis of algorithms. Topics to be chosen from among the following: dynamic programming, search and traverse techniques, backtracking, numerical techniques, NP-hard and NP-complete problems, approximation algorithms and other topics in the analysis and design of algorithms. Offered fall semester.\n", "comp560": "Artificial Intelligence\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course is an introduction to LISP or another AI programming language. Topics are chosen from pattern recognition, theorem proving, learning, cognitive science and vision. It also presents introduction to the basic techniques of AI such as heuristic search, semantic nets, production systems, frames, planning and other AI topics. Offered periodically.\n", "comp570": "Robotics\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis is a project-oriented course in robotics. Topics are chosen from manipulator motion and control, motion planning, legged-motion, vision, touch sensing, grasping, programming languages for robots and automated factory design. Offered periodically.", "comp580": "Database Systems\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics will include relational, hierarchical and network data models; design theory for relational databases and query optimization; classification of data models, data languages; concurrency, integrity, privacy; modeling and measurement of access strategies; and dedicated processors, information retrieval and real time applications. Offered periodically.", "comp590": "Computer Architecture\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course is an introduction to the internal structure of digital computers including design of gates, flip-fops, registers and memories to perform operations on numerical and other data represented in binary form; computer system analysis and design; organizational dependence on computations to be performed; and theoretical aspects of parallel and pipeline computation. Offered periodically.", "comp594": "Computer Networks\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course provides an introduction to fundamental concepts in computer networks, including their design and implementation. Topics include network architectures and protocols, placing emphasis on protocol used in the Internet; routing; data link layer issues; multimedia networking; network security; and network management. Offered periodically.\n", "comp596": "Topics in Computer Science\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics are chosen from program verification, formal semantics, formal language theory, concurrent programming, complexity or algorithms, programming language theory, graphics and other computer science topics. Repeatable for different topics. Offered as topics arise.", "comp598": " Computer Science Graduate Internship\n(3 credits)\nPrerequisite: Matriculation in the computer science master’s program; at least six credits of graduate-level course work in computer science (COMP); formal application required\nAn internship provides an opportunity to apply what has been learned in the classroom and allows the student to further professional skills. Faculty supervision allows for reflection on the internship experience and connects the applied portion of the academic study to other courses. Repeatable; may earn a maximum of six credits, however, only three credits can be used toward the degree. Graded on (P) Pass/(N) No Pass basis. Offered fall and spring semesters.\n", } insertStatement := "INSERT INTO COURSE (course_prefix, course_number, description) VALUES (?,?,?);" preppedStatement, err := database.Prepare(insertStatement)
identifier_body
databaseDemo.go
) value, err := reader.ReadString('\n') if err != nil { log.Fatal("How did we fail to read from standard in!?!?") } value = strings.TrimSpace(value) min_gpa, err := strconv.ParseFloat(value, 32) if err != nil { log.Fatal("oooops you typed that wrong", err) } return min_gpa } func findProbationStudents(database *sql.DB) { var firstName, lastName string var gpa float64 minGpa := getMinGPA() selectStatement := "SELECT first_name, last_name, gpa FROM STUDENTS WHERE gpa < ?" resultSet, err := database.Query(selectStatement, minGpa) if err != nil { log.Fatal("Bad Query", err) } defer resultSet.Close() for resultSet.Next() { err = resultSet.Scan(&firstName, &lastName, &gpa) if err != nil { log.Fatal(err) } fmt.Printf("%s %s is on probation with a GPA of %f\n", firstName, lastName, gpa) } } func registerForClasses(database *sql.DB) { insertStatement := "INSERT INTO CLASS_LIST (banner_id, course_prefix, course_number, registration_date)" + "VALUES(?, 'Comp', 510, DATE('now'))" preppedStatement, err := database.Prepare(insertStatement) if err != nil { log.Fatal("Hey prof you goofed it trying to type live", err) } for i := 1001; i <= 1008; i++ { preppedStatement.Exec(i) } } func addCourses(database *sql.DB) { var sampleData = map[string]string{ "comp502": "Research\n(3 credits)\nPrerequisite: Consent of the department; formal application required\nOriginal research is undertaken by the graduate student in their field. This course culminates in a capstone project. For details, consult the paragraph titled “Directed or Independent Study” in the “College of Graduate Studies” section of this catalog. Offered fall and spring semesters.", "comp503": "Directed Study\n(1-3 credits)\nPrerequisite: Consent of the department; formal application required\nDirected study is designed for the graduate student who desires to study selected topics in a specific field. For details, consult the paragraph titled “Directed or Independent Study” in the “College of Graduate Studies” section of this catalog. Repeatable: may earn a maximum of six credits. Offered fall and spring semesters.", "comp510": "Topics in Programming Languages\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course investigates programming language development from designer’s, user’s and implementer’s point of view. Topics include formal syntax and semantics, language system, extensible languages and control structures. There is also a survey of intralanguage features, covering ALGOL-60, ALGOL-68, Ada, Pascal, LISP, SNOBOL-4 APL, SIMULA-67, CLU, MODULA, and others. Offered periodically.", "comp520": "Operating Systems Principles\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course examines design principles such as optimal scheduling; file systems, system integrity and security, as well as the mathematical analysis of selected aspects of operating system design. Topics include queuing theory, disk scheduling, storage management and the working set model. Design and implementation of an operating system nucleus is also studied. Offered periodically.", "comp525": "Design and Construction of Compilers\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics will include lexical and syntactic analysis; code generation; error detection and correction; optimization techniques; models of code generators; and incremental and interactive compiling. Students will design and implement a compiler. Offered periodically.", "comp530": "Software Engineering\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nTopics in this course will include construction of reliable software, software tools, software testing methodologies, structured design, structured programming, software characteristics and quality and formal proofs of program correctness. Chief programmer teams and structure walk-throughs will be employed. Offered periodically.\n", "comp540": "Automata, Computability and Formal Languages\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nTopics in this course will include finite automata and regular languages, context- free languages, Turing machines and their variants, partial recursive functions and grammars, Church’s thesis, undecidable problems, complexity of algorithms and completeness. Offered periodically.", "comp545": "Analysis of Algorithms\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course deals with techniques in the analysis of algorithms. Topics to be chosen from among the following: dynamic programming, search and traverse techniques, backtracking, numerical techniques, NP-hard and NP-complete problems, approximation algorithms and other topics in the analysis and design of algorithms. Offered fall semester.\n", "comp560": "Artificial Intelligence\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course is an introduction to LISP or another AI programming language. Topics are chosen from pattern recognition, theorem proving, learning, cognitive science and vision. It also presents introduction to the basic techniques of AI such as heuristic search, semantic nets, production systems, frames, planning and other AI topics. Offered periodically.\n", "comp570": "Robotics\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis is a project-oriented course in robotics. Topics are chosen from manipulator motion and control, motion planning, legged-motion, vision, touch sensing, grasping, programming languages for robots and automated factory design. Offered periodically.", "comp580": "Database Systems\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics will include relational, hierarchical and network data models; design theory for relational databases and query optimization; classification of data models, data languages; concurrency, integrity, privacy; modeling and measurement of access strategies; and dedicated processors, information retrieval and real time applications. Offered periodically.", "comp590": "Computer Architecture\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course is an introduction to the internal structure of digital computers including design of gates, flip-fops, registers and memories to perform operations on numerical and other data represented in binary form; computer system analysis and design; organizational dependence on computations to be performed; and theoretical aspects of parallel and pipeline computation. Offered periodically.", "comp594": "Computer Networks\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nThis course provides an introduction to fundamental concepts in computer networks, including their design and implementation. Topics include network architectures and protocols, placing emphasis on protocol used in the Internet; routing; data link layer issues; multimedia networking; network security; and network management. Offered periodically.\n", "comp596": "Topics in Computer Science\n(3 credits)\nPrerequisite: Admission to the MS program in Computer Science or consent of instructor\nIn this course, topics are chosen from program verification, formal semantics, formal language theory, concurrent programming, complexity or algorithms, programming language theory, graphics and other computer science topics. Repeatable for different topics. Offered as topics arise.", "comp598": " Computer Science Graduate Internship\n(3 credits)\nPrerequisite: Matriculation in the computer science master’s program; at least six credits of graduate-level course work in computer science (COMP); formal application required\nAn internship provides an opportunity to apply what has been learned in the classroom and allows the student to further professional skills. Faculty supervision allows for reflection on the internship experience and connects the applied portion of the academic study to other courses. Repeatable; may earn a maximum of six credits, however, only three credits can be used toward the degree. Graded on (P) Pass/(N) No Pass basis. Offered fall and spring semesters.\n", } insertStatement := "INSERT INTO COURSE (course_prefix, course_number, description) VALUES (?,?,?);" preppedStatement, err := database.Prepare(insertStatement) if err != nil { log.Fatal(err) } for course, desc := range sampleData { prefix := course[0:4]
s(database *sql.DB) { sampleNames := map[string]string{"John": "Santore", "Enping": "Li", "Michael": "Black", "Seikyung": "Jung", "Haleh": "Khojasteh", "Abdul": "Sattar", "Paul": "Kim", "Yiheng": "Liang"} statement := "INSERT INTO STUDENTS (banner_id, first
numVal := course[4:7] courseNum, err := strconv.Atoi(numVal) if err != nil { log.Fatal("ooops we must have mistyped", err) } preppedStatement.Exec(prefix, courseNum, desc) } } func addSampleStudent
conditional_block
utils.rs
the given channel. // FIXME: Workaround for TrustedLen / TrustedRandomAccess being unstable // and because of that we wouldn't get nice optimizations fn foreach_sample_zipped<U>( &self, channel: usize, iter: impl Iterator<Item = U>, func: impl FnMut(&'a S, U), ); fn foreach_frame<F: Frame<Sample = S>>(&self, func: impl FnMut(F)); /// Number of frames. fn frames(&self) -> usize; /// Number of channels. fn channels(&self) -> usize; /// Split into two at the given sample. fn split_at(self, sample: usize) -> (Self, Self); } /// Struct representing interleaved samples. pub struct Interleaved<'a, S> { /// Interleaved sample data. data: &'a [S], /// Number of channels. channels: usize, } impl<'a, S> Interleaved<'a, S> { /// Create a new wrapper around the interleaved channels and do a sanity check. pub fn new(data: &'a [S], channels: usize) -> Result<Self, crate::Error> { if channels == 0 { return Err(crate::Error::NoMem); } if data.len() % channels != 0 { return Err(crate::Error::NoMem); } Ok(Interleaved { data, channels }) } } impl<'a, S: Sample> Samples<'a, S> for Interleaved<'a, S> { #[inline] fn foreach_sample(&self, channel: usize, mut func: impl FnMut(&'a S)) { assert!(channel < self.channels); for v in self.data.chunks_exact(self.channels) { func(&v[channel]) } } #[inline] fn fo
>( &self, channel: usize, iter: impl Iterator<Item = U>, mut func: impl FnMut(&'a S, U), ) { assert!(channel < self.channels); for (v, u) in Iterator::zip(self.data.chunks_exact(self.channels), iter) { func(&v[channel], u) } } #[inline] fn foreach_frame<F: Frame<Sample = S>>(&self, mut func: impl FnMut(F)) { assert_eq!(F::CHANNELS, self.channels); for f in self.data.chunks_exact(self.channels) { func(F::from_samples(&mut f.iter().copied()).unwrap()); } } #[inline] fn frames(&self) -> usize { self.data.len() / self.channels } #[inline] fn channels(&self) -> usize { self.channels } #[inline] fn split_at(self, sample: usize) -> (Self, Self) { assert!(sample * self.channels <= self.data.len()); let (fst, snd) = self.data.split_at(sample * self.channels); ( Interleaved { data: fst, channels: self.channels, }, Interleaved { data: snd, channels: self.channels, }, ) } } /// Struct representing interleaved samples. pub struct Planar<'a, S> { data: &'a [&'a [S]], start: usize, end: usize, } impl<'a, S> Planar<'a, S> { /// Create a new wrapper around the planar channels and do a sanity check. pub fn new(data: &'a [&'a [S]]) -> Result<Self, crate::Error> { if data.is_empty() { return Err(crate::Error::NoMem); } if data.iter().any(|d| data[0].len() != d.len()) { return Err(crate::Error::NoMem); } Ok(Planar { data, start: 0, end: data[0].len(), }) } } impl<'a, S: Sample> Samples<'a, S> for Planar<'a, S> { #[inline] fn foreach_sample(&self, channel: usize, mut func: impl FnMut(&'a S)) { assert!(channel < self.data.len()); for v in &self.data[channel][self.start..self.end] { func(v) } } #[inline] fn foreach_sample_zipped<U>( &self, channel: usize, iter: impl Iterator<Item = U>, mut func: impl FnMut(&'a S, U), ) { assert!(channel < self.data.len()); for (v, u) in Iterator::zip(self.data[channel][self.start..self.end].iter(), iter) { func(v, u) } } #[inline] fn foreach_frame<F: Frame<Sample = S>>(&self, mut func: impl FnMut(F)) { let channels = self.data.len(); assert_eq!(F::CHANNELS, channels); for f in self.start..self.end { func(F::from_fn(|c| self.data[c][f])); } } #[inline] fn frames(&self) -> usize { self.end - self.start } #[inline] fn channels(&self) -> usize { self.data.len() } #[inline] fn split_at(self, sample: usize) -> (Self, Self) { assert!(self.start + sample <= self.end); ( Planar { data: self.data, start: self.start, end: self.start + sample, }, Planar { data: self.data, start: self.start + sample, end: self.end, }, ) } } pub trait Sample: dasp_sample::Sample + dasp_sample::Duplex<f32> + dasp_sample::Duplex<f64> { const MAX_AMPLITUDE: f64; fn as_f64_raw(self) -> f64; } impl Sample for f32 { const MAX_AMPLITUDE: f64 = 1.0; #[inline(always)] fn as_f64_raw(self) -> f64 { self as f64 } } impl Sample for f64 { const MAX_AMPLITUDE: f64 = 1.0; #[inline(always)] fn as_f64_raw(self) -> f64 { self } } impl Sample for i16 { const MAX_AMPLITUDE: f64 = -(Self::MIN as f64); #[inline(always)] fn as_f64_raw(self) -> f64 { self as f64 } } impl Sample for i32 { const MAX_AMPLITUDE: f64 = -(Self::MIN as f64); #[inline(always)] fn as_f64_raw(self) -> f64 { self as f64 } } /// An extension-trait to accumulate samples into a frame pub trait FrameAccumulator: Frame { fn scale_add(&mut self, other: &Self, coeff: f32); fn retain_max_samples(&mut self, other: &Self); } impl<F: Frame, S> FrameAccumulator for F where S: SampleAccumulator + std::fmt::Debug, F: IndexMut<Target = S>, { #[inline(always)] fn scale_add(&mut self, other: &Self, coeff: f32) { for i in 0..Self::CHANNELS { self.index_mut(i).scale_add(*other.index(i), coeff); } } fn retain_max_samples(&mut self, other: &Self) { for i in 0..Self::CHANNELS { let this = self.index_mut(i); let other = other.index(i); if *other > *this { *this = *other; } } } } // Required since std::ops::IndexMut seem to not be implemented for arrays // making FrameAcc hard to implement for auto-vectorization // IndexMut seems to be coming to stdlib, when https://github.com/rust-lang/rust/pull/74989 // implemented, this trait can be removed pub trait IndexMut { type Target; fn index_mut(&mut self, i: usize) -> &mut Self::Target; fn index(&self, i: usize) -> &Self::Target; } macro_rules! index_mut_impl { ( $channels:expr ) => { impl<T: SampleAccumulator> IndexMut for [T; $channels] { type Target = T; #[inline(always)] fn index_mut(&mut self, i: usize) -> &mut Self::Target { &mut self[i] } #[inline(always)] fn index(&self, i: usize) -> &Self::Target { &self[i] } } }; } index_mut_impl!(1); index_mut_impl!(2); index_mut_impl!(4); index_mut_impl!(6); index_mut_impl!(8); pub trait SampleAccumulator: Sample { fn scale_add(&mut self, other: Self, coeff: f32); } impl SampleAccumulator for f32 { #[inline(always)] fn scale_add(&mut self, other: Self, coeff: f32) {
reach_sample_zipped<U
identifier_name
utils.rs
the given channel. // FIXME: Workaround for TrustedLen / TrustedRandomAccess being unstable // and because of that we wouldn't get nice optimizations fn foreach_sample_zipped<U>( &self, channel: usize, iter: impl Iterator<Item = U>, func: impl FnMut(&'a S, U), ); fn foreach_frame<F: Frame<Sample = S>>(&self, func: impl FnMut(F)); /// Number of frames. fn frames(&self) -> usize; /// Number of channels. fn channels(&self) -> usize; /// Split into two at the given sample. fn split_at(self, sample: usize) -> (Self, Self); } /// Struct representing interleaved samples. pub struct Interleaved<'a, S> { /// Interleaved sample data. data: &'a [S], /// Number of channels. channels: usize, } impl<'a, S> Interleaved<'a, S> { /// Create a new wrapper around the interleaved channels and do a sanity check. pub fn new(data: &'a [S], channels: usize) -> Result<Self, crate::Error> { if channels == 0 { return Err(crate::Error::NoMem); } if data.len() % channels != 0 { return Err(crate::Error::NoMem); } Ok(Interleaved { data, channels }) } } impl<'a, S: Sample> Samples<'a, S> for Interleaved<'a, S> { #[inline] fn foreach_sample(&self, channel: usize, mut func: impl FnMut(&'a S)) { assert!(channel < self.channels); for v in self.data.chunks_exact(self.channels) { func(&v[channel]) } } #[inline] fn foreach_sample_zipped<U>( &self, channel: usize, iter: impl Iterator<Item = U>, mut func: impl FnMut(&'a S, U), ) { assert!(channel < self.channels); for (v, u) in Iterator::zip(self.data.chunks_exact(self.channels), iter) { func(&v[channel], u) } } #[inline] fn foreach_frame<F: Frame<Sample = S>>(&self, mut func: impl FnMut(F)) { assert_eq!(F::CHANNELS, self.channels); for f in self.data.chunks_exact(self.channels) { func(F::from_samples(&mut f.iter().copied()).unwrap()); } } #[inline] fn frames(&self) -> usize { self.data.len() / self.channels } #[inline] fn channels(&self) -> usize { self.channels } #[inline] fn split_at(self, sample: usize) -> (Self, Self) { assert!(sample * self.channels <= self.data.len()); let (fst, snd) = self.data.split_at(sample * self.channels); ( Interleaved { data: fst, channels: self.channels, }, Interleaved { data: snd, channels: self.channels, }, ) } } /// Struct representing interleaved samples. pub struct Planar<'a, S> { data: &'a [&'a [S]], start: usize, end: usize, } impl<'a, S> Planar<'a, S> { /// Create a new wrapper around the planar channels and do a sanity check. pub fn new(data: &'a [&'a [S]]) -> Result<Self, crate::Error> { if data.is_empty() { return Err(crate::Error::NoMem); } if data.iter().any(|d| data[0].len() != d.len()) { return Err(crate::Error::NoMem); } Ok(Planar { data, start: 0, end: data[0].len(), }) } } impl<'a, S: Sample> Samples<'a, S> for Planar<'a, S> { #[inline] fn foreach_sample(&self, channel: usize, mut func: impl FnMut(&'a S)) { assert!(channel < self.data.len()); for v in &self.data[channel][self.start..self.end] { func(v) } } #[inline] fn foreach_sample_zipped<U>( &self, channel: usize, iter: impl Iterator<Item = U>, mut func: impl FnMut(&'a S, U), ) { assert!(channel < self.data.len()); for (v, u) in Iterator::zip(self.data[channel][self.start..self.end].iter(), iter) { func(v, u) } } #[inline] fn foreach_frame<F: Frame<Sample = S>>(&self, mut func: impl FnMut(F)) { let channels = self.data.len(); assert_eq!(F::CHANNELS, channels); for f in self.start..self.end { func(F::from_fn(|c| self.data[c][f])); } } #[inline] fn frames(&self) -> usize { self.end - self.start } #[inline] fn channels(&self) -> usize { self.data.len() } #[inline] fn split_at(self, sample: usize) -> (Self, Self) { assert!(self.start + sample <= self.end); ( Planar { data: self.data, start: self.start, end: self.start + sample, }, Planar { data: self.data, start: self.start + sample, end: self.end, }, ) } } pub trait Sample: dasp_sample::Sample + dasp_sample::Duplex<f32> + dasp_sample::Duplex<f64> { const MAX_AMPLITUDE: f64; fn as_f64_raw(self) -> f64; } impl Sample for f32 { const MAX_AMPLITUDE: f64 = 1.0; #[inline(always)] fn as_f64_raw(self) -> f64 { self as f64 } } impl Sample for f64 { const MAX_AMPLITUDE: f64 = 1.0; #[inline(always)] fn as_f64_raw(self) -> f64 { self
const MAX_AMPLITUDE: f64 = -(Self::MIN as f64); #[inline(always)] fn as_f64_raw(self) -> f64 { self as f64 } } impl Sample for i32 { const MAX_AMPLITUDE: f64 = -(Self::MIN as f64); #[inline(always)] fn as_f64_raw(self) -> f64 { self as f64 } } /// An extension-trait to accumulate samples into a frame pub trait FrameAccumulator: Frame { fn scale_add(&mut self, other: &Self, coeff: f32); fn retain_max_samples(&mut self, other: &Self); } impl<F: Frame, S> FrameAccumulator for F where S: SampleAccumulator + std::fmt::Debug, F: IndexMut<Target = S>, { #[inline(always)] fn scale_add(&mut self, other: &Self, coeff: f32) { for i in 0..Self::CHANNELS { self.index_mut(i).scale_add(*other.index(i), coeff); } } fn retain_max_samples(&mut self, other: &Self) { for i in 0..Self::CHANNELS { let this = self.index_mut(i); let other = other.index(i); if *other > *this { *this = *other; } } } } // Required since std::ops::IndexMut seem to not be implemented for arrays // making FrameAcc hard to implement for auto-vectorization // IndexMut seems to be coming to stdlib, when https://github.com/rust-lang/rust/pull/74989 // implemented, this trait can be removed pub trait IndexMut { type Target; fn index_mut(&mut self, i: usize) -> &mut Self::Target; fn index(&self, i: usize) -> &Self::Target; } macro_rules! index_mut_impl { ( $channels:expr ) => { impl<T: SampleAccumulator> IndexMut for [T; $channels] { type Target = T; #[inline(always)] fn index_mut(&mut self, i: usize) -> &mut Self::Target { &mut self[i] } #[inline(always)] fn index(&self, i: usize) -> &Self::Target { &self[i] } } }; } index_mut_impl!(1); index_mut_impl!(2); index_mut_impl!(4); index_mut_impl!(6); index_mut_impl!(8); pub trait SampleAccumulator: Sample { fn scale_add(&mut self, other: Self, coeff: f32); } impl SampleAccumulator for f32 { #[inline(always)] fn scale_add(&mut self, other: Self, coeff: f32) { #[
} } impl Sample for i16 {
random_line_split
utils.rs
the given channel. // FIXME: Workaround for TrustedLen / TrustedRandomAccess being unstable // and because of that we wouldn't get nice optimizations fn foreach_sample_zipped<U>( &self, channel: usize, iter: impl Iterator<Item = U>, func: impl FnMut(&'a S, U), ); fn foreach_frame<F: Frame<Sample = S>>(&self, func: impl FnMut(F)); /// Number of frames. fn frames(&self) -> usize; /// Number of channels. fn channels(&self) -> usize; /// Split into two at the given sample. fn split_at(self, sample: usize) -> (Self, Self); } /// Struct representing interleaved samples. pub struct Interleaved<'a, S> { /// Interleaved sample data. data: &'a [S], /// Number of channels. channels: usize, } impl<'a, S> Interleaved<'a, S> { /// Create a new wrapper around the interleaved channels and do a sanity check. pub fn new(data: &'a [S], channels: usize) -> Result<Self, crate::Error> { if channels == 0 { return Err(crate::Error::NoMem); } if data.len() % channels != 0 { return Err(crate::Error::NoMem); } Ok(Interleaved { data, channels }) } } impl<'a, S: Sample> Samples<'a, S> for Interleaved<'a, S> { #[inline] fn foreach_sample(&self, channel: usize, mut func: impl FnMut(&'a S)) { assert!(channel < self.channels); for v in self.data.chunks_exact(self.channels) { func(&v[channel]) } } #[inline] fn foreach_sample_zipped<U>( &self, channel: usize, iter: impl Iterator<Item = U>, mut func: impl FnMut(&'a S, U), ) { assert!(channel < self.channels); for (v, u) in Iterator::zip(self.data.chunks_exact(self.channels), iter) { func(&v[channel], u) } } #[inline] fn foreach_frame<F: Frame<Sample = S>>(&self, mut func: impl FnMut(F)) { assert_eq!(F::CHANNELS, self.channels); for f in self.data.chunks_exact(self.channels) { func(F::from_samples(&mut f.iter().copied()).unwrap()); } } #[inline] fn frames(&self) -> usize { self.data.len() / self.channels } #[inline] fn channels(&self) -> usize { self.channels } #[inline] fn split_at(self, sample: usize) -> (Self, Self) { assert!(sample * self.channels <= self.data.len()); let (fst, snd) = self.data.split_at(sample * self.channels); ( Interleaved { data: fst, channels: self.channels, }, Interleaved { data: snd, channels: self.channels, }, ) } } /// Struct representing interleaved samples. pub struct Planar<'a, S> { data: &'a [&'a [S]], start: usize, end: usize, } impl<'a, S> Planar<'a, S> { /// Create a new wrapper around the planar channels and do a sanity check. pub fn new(data: &'a [&'a [S]]) -> Result<Self, crate::Error> { if data.is_empty() { return Err(crate::Error::NoMem); } if data.iter().any(|d| data[0].len() != d.len()) { return Err(crate::Error::NoMem); } Ok(Planar { data, start: 0, end: data[0].len(), }) } } impl<'a, S: Sample> Samples<'a, S> for Planar<'a, S> { #[inline] fn foreach_sample(&self, channel: usize, mut func: impl FnMut(&'a S)) { assert!(channel < self.data.len()); for v in &self.data[channel][self.start..self.end] { func(v) } } #[inline] fn foreach_sample_zipped<U>( &self, channel: usize, iter: impl Iterator<Item = U>, mut func: impl FnMut(&'a S, U), ) { assert!(channel < self.data.len()); for (v, u) in Iterator::zip(self.data[channel][self.start..self.end].iter(), iter) { func(v, u) } } #[inline] fn foreach_frame<F: Frame<Sample = S>>(&self, mut func: impl FnMut(F)) { let channels = self.data.len(); assert_eq!(F::CHANNELS, channels); for f in self.start..self.end { func(F::from_fn(|c| self.data[c][f])); } } #[inline] fn frames(&self) -> usize { self.end - self.start } #[inline] fn channels(&self) -> usize { self.data.len() } #[inline] fn split_at(self, sample: usize) -> (Self, Self) { assert!(self.start + sample <= self.end); ( Planar { data: self.data, start: self.start, end: self.start + sample, }, Planar { data: self.data, start: self.start + sample, end: self.end, }, ) } } pub trait Sample: dasp_sample::Sample + dasp_sample::Duplex<f32> + dasp_sample::Duplex<f64> { const MAX_AMPLITUDE: f64; fn as_f64_raw(self) -> f64; } impl Sample for f32 { const MAX_AMPLITUDE: f64 = 1.0; #[inline(always)] fn as_f64_raw(self) -> f64 { self as f64 } } impl Sample for f64 { const MAX_AMPLITUDE: f64 = 1.0; #[inline(always)] fn as_f64_raw(self) -> f64 {
impl Sample for i16 { const MAX_AMPLITUDE: f64 = -(Self::MIN as f64); #[inline(always)] fn as_f64_raw(self) -> f64 { self as f64 } } impl Sample for i32 { const MAX_AMPLITUDE: f64 = -(Self::MIN as f64); #[inline(always)] fn as_f64_raw(self) -> f64 { self as f64 } } /// An extension-trait to accumulate samples into a frame pub trait FrameAccumulator: Frame { fn scale_add(&mut self, other: &Self, coeff: f32); fn retain_max_samples(&mut self, other: &Self); } impl<F: Frame, S> FrameAccumulator for F where S: SampleAccumulator + std::fmt::Debug, F: IndexMut<Target = S>, { #[inline(always)] fn scale_add(&mut self, other: &Self, coeff: f32) { for i in 0..Self::CHANNELS { self.index_mut(i).scale_add(*other.index(i), coeff); } } fn retain_max_samples(&mut self, other: &Self) { for i in 0..Self::CHANNELS { let this = self.index_mut(i); let other = other.index(i); if *other > *this { *this = *other; } } } } // Required since std::ops::IndexMut seem to not be implemented for arrays // making FrameAcc hard to implement for auto-vectorization // IndexMut seems to be coming to stdlib, when https://github.com/rust-lang/rust/pull/74989 // implemented, this trait can be removed pub trait IndexMut { type Target; fn index_mut(&mut self, i: usize) -> &mut Self::Target; fn index(&self, i: usize) -> &Self::Target; } macro_rules! index_mut_impl { ( $channels:expr ) => { impl<T: SampleAccumulator> IndexMut for [T; $channels] { type Target = T; #[inline(always)] fn index_mut(&mut self, i: usize) -> &mut Self::Target { &mut self[i] } #[inline(always)] fn index(&self, i: usize) -> &Self::Target { &self[i] } } }; } index_mut_impl!(1); index_mut_impl!(2); index_mut_impl!(4); index_mut_impl!(6); index_mut_impl!(8); pub trait SampleAccumulator: Sample { fn scale_add(&mut self, other: Self, coeff: f32); } impl SampleAccumulator for f32 { #[inline(always)] fn scale_add(&mut self, other: Self, coeff: f32) {
self } }
identifier_body
narwhal.js
$('head').append($script); }) .then(() => (new Promise((resolve, reject) => { setTimeout(resolve, waitFor) }))) ) $(document).ready(() => { // // Configurations, selectors, ... // const selector = { editButton: { id: 'n-edit-button', selector: '#n-edit-button' }, clickable: { class: 'n-clickable', selector: '.n-clickable' }, jinja2UrlTemplate: { id: 'n-jinja2-urlTemplate', selector: '#n-jinja2-urlTemplate' }, metaData: { id: 'n-meta', selector: '#n-meta' }, metaDataType: { id: 'n-meta-type', selector: '#n-meta-type' }, artifactItem: { class: 'n-artifact-item', selector: '.n-artifact-item' }, artifactItemId: { id: 'n-artifact-id', selector: '#n-artifact-id-' }, } const selectors = [ { title: 'edit button', selector: 'a[href^="/edit"]:first', attr: { id: selector.editButton.id } }, { title: 'artifact item', selector: '.tbl-artifact tr', attr: { class: selector.artifactItem.class } }, { title: 'artifact item id', selector: '.tbl-artifact tr', attr: { id: selector.artifactItemId.id, data: 'id' } }, // clickable content { title: 'description row', selector: 'tr:contains("description"):first > td:first', attr: { class: selector.clickable.class } }, { title: 'template row', selector: 'tr:contains("url template"):first > td:first', attr: { class: selector.clickable.class } }, // jinja { title: 'jinja2 urlTemplate', selector: 'input[name="url_template"]', attr: { id: selector.jinja2UrlTemplate.id } }, // metadata { title: 'meta - type', selector: 'tr:contains("Meta") th small', attr: { id: selector.metaDataType.id } }, { title: 'meta', selector: 'tr:contains("Meta") textarea[name="meta"]', attr: { id: selector.metaData.id } }, ] // distribute attributes to elements by selectors above selectors.forEach(({ selector, attr }) => { ifExistsDo($(selector), ($item) => { if (attr.hasOwnProperty('class')) { $item.addClass(attr.class) } else if (attr.hasOwnProperty('id') && attr.hasOwnProperty('data')) { $item.each((_k, singleItem) => { const $singleItem = $(singleItem) const id = `${attr.id}-${$singleItem.data(attr.data)}` $singleItem.attr('id', id) }) } else if (attr.hasOwnProperty('id')) { $item.attr('id', attr.id) } }) }) // // Events, handlers, helpers ... // // edit on `E` keyup $(document).on('keyup', ({ target: { tagName }, key }) => { const tag = tagName.toLocaleLowerCase() if (tag !== 'input' && tag !== 'textarea' && key === 'e') { ifExistsDo($(selector.editButton.selector), ($edit) => { window.location.href = $edit.first().attr('href') }) } }) // make links in clickable class clickable ifExistsDo($(selector.clickable.selector), ($clickable) => { $clickable.each((_, item) => { const $item = $(item) const content = $item .html() .split(' ') .map((word) => word.startsWith('http') ? `<a href='${word}' target="_blank">${word}</a>` : word) .join(' ') $item.html(content) }) }) // show jinja2 syntax in textarea with jinja2 const highlightJinjaSyntax = (type, id) => { // https://codemirror.net/doc/manual.html return window.CodeMirror.fromTextArea(document.getElementById(id), { mode: { name: type, htmlMode: true }, lineNumbers: true, tabSize: 2, extraKeys: { Tab: (cm) => cm.execCommand("indentMore"), "Shift-Tab": (cm) => cm.execCommand("indentLess"), }, }); } // transform urlTemplate input to textArea if (window.location.href.indexOf('/artifacts/') > 0) { ifExistsDo($(selector.jinja2UrlTemplate.selector), ($urlTemplate) => { const $textArea = $('<textarea>') .attr('name', $urlTemplate.attr('name')) .attr('id', $urlTemplate.attr('id')) .addClass($urlTemplate.attr('class')) .val($urlTemplate.val()) $urlTemplate.replaceWith($textArea) }) } // load script and enable highlighting if ($(selector.jinja2UrlTemplate.selector).length > 0 || $(selector.metaData.selector).length > 0) { Promise .all([ loadStyle('https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/codemirror.min.css'), loadScriptAndWait('https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/codemirror.min.js', 300), ]) .then(() => { if (!window.hasOwnProperty('CodeMirror')) { throw Error('CodeMirror was not loaded correctly ...') } }) .then(() => loadScriptAndWait('https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/mode/jinja2/jinja2.min.js', 300)) .then(() => loadScriptAndWait('https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/mode/yaml/yaml.min.js', 300)) .then(() => { ifExistsDo($(selector.jinja2UrlTemplate.selector), ($_) => { highlightJinjaSyntax('jinja2', selector.jinja2UrlTemplate.id).setSize('', '60px') }) ifExistsDo($(selector.metaData.selector), ($_) => { const typeText = $(selector.metaDataType.id).text() const type = typeText.indexOf('jinja2') > 0 ? 'jinja2' : typeText.indexOf('yaml') ? 'yaml' : '' highlightJinjaSyntax(type, selector.metaData.id) }) }) .then(() => { $('.CodeMirror').css({ border: '1px solid #ccc', borderRadius: '4px' }) }) .catch((e) => console.error(e)) } // add dashboard overview if (window.location.href.indexOf('/artifacts') > 0 && $(selector.artifactItem).length > 0) { $('.n-overview').remove() const example = [ { domain: "consents", list: [ { id: 923, configurationId: 953, alias: 'thor (router)' }, { id: 962, configurationId: 963, alias: 'rogue (person-filter)' }, { id: 924, configurationId: 952, alias: 'ant-man (aggregator)' }, { id: 965, configurationId: 966, alias: 'quicksilver (deriver)' }, { id: 958, configurationId: 959, alias: 'hulk (data_processing-filter)' }, { id: 960, configurationId: 961, alias: 'she-hulk (all-intent-filter)' }, { id: 904, configurationId: 968, alias: 'loki (stator)' }, ] } ] const dashboardArtifactsOverview = localStorage.hasOwnProperty('dashboardArtifactsOverview') ? JSON.parse(localStorage.getItem('dashboardArtifactsOverview')) : [] const $createDomainTitle = (domain) => domain.length > 0 ? $('<h4></h4>') .text(domain) .css({ clear: 'both' }) : '' const $getArtifact = (id) => { const $artifact = $(`${selector.artifactItemId.selector}${id}`) return $artifact.length ? $artifact : $('<tr><td>id</td></tr>') // not all artifacts are shown on the page, but we can create a link .append($(`<td><a href="/artifacts/${id}">artefact id: ${id}</a></td>`)) } const $createAlias = (alias) => alias.length > 0 ? $('<h3></h3>') .text(alias) .css({ fontSize: 12, margin: 0, textAlign: 'right' }) : '' const $createArtifact = ($artifact) => $('<h4></h4>') .append($artifact.clone().find('td:nth-child(2)')) .css({ fontSize: 12 })
const $createConfiguration = ($configuration) => $configuration ? $('<h5></h5>') .append($configuration.clone().find('td:nth-child(2)')) .css({ fontSize: 11 })
random_line_split
narwhal.js
Promise((resolve, reject) => { const $script = $('<script type="text/javascript">') .attr('src', url) .ready(resolve) $('head').append($script); }) .then(() => (new Promise((resolve, reject) => { setTimeout(resolve, waitFor) }))) ) $(document).ready(() => { // // Configurations, selectors, ... // const selector = { editButton: { id: 'n-edit-button', selector: '#n-edit-button' }, clickable: { class: 'n-clickable', selector: '.n-clickable' }, jinja2UrlTemplate: { id: 'n-jinja2-urlTemplate', selector: '#n-jinja2-urlTemplate' }, metaData: { id: 'n-meta', selector: '#n-meta' }, metaDataType: { id: 'n-meta-type', selector: '#n-meta-type' }, artifactItem: { class: 'n-artifact-item', selector: '.n-artifact-item' }, artifactItemId: { id: 'n-artifact-id', selector: '#n-artifact-id-' }, } const selectors = [ { title: 'edit button', selector: 'a[href^="/edit"]:first', attr: { id: selector.editButton.id } }, { title: 'artifact item', selector: '.tbl-artifact tr', attr: { class: selector.artifactItem.class } }, { title: 'artifact item id', selector: '.tbl-artifact tr', attr: { id: selector.artifactItemId.id, data: 'id' } }, // clickable content { title: 'description row', selector: 'tr:contains("description"):first > td:first', attr: { class: selector.clickable.class } }, { title: 'template row', selector: 'tr:contains("url template"):first > td:first', attr: { class: selector.clickable.class } }, // jinja { title: 'jinja2 urlTemplate', selector: 'input[name="url_template"]', attr: { id: selector.jinja2UrlTemplate.id } }, // metadata { title: 'meta - type', selector: 'tr:contains("Meta") th small', attr: { id: selector.metaDataType.id } }, { title: 'meta', selector: 'tr:contains("Meta") textarea[name="meta"]', attr: { id: selector.metaData.id } }, ] // distribute attributes to elements by selectors above selectors.forEach(({ selector, attr }) => { ifExistsDo($(selector), ($item) => { if (attr.hasOwnProperty('class')) { $item.addClass(attr.class) } else if (attr.hasOwnProperty('id') && attr.hasOwnProperty('data')) { $item.each((_k, singleItem) => { const $singleItem = $(singleItem) const id = `${attr.id}-${$singleItem.data(attr.data)}` $singleItem.attr('id', id) }) } else if (attr.hasOwnProperty('id')) { $item.attr('id', attr.id) } }) }) // // Events, handlers, helpers ... // // edit on `E` keyup $(document).on('keyup', ({ target: { tagName }, key }) => { const tag = tagName.toLocaleLowerCase() if (tag !== 'input' && tag !== 'textarea' && key === 'e') { ifExistsDo($(selector.editButton.selector), ($edit) => { window.location.href = $edit.first().attr('href') }) } }) // make links in clickable class clickable ifExistsDo($(selector.clickable.selector), ($clickable) => { $clickable.each((_, item) => { const $item = $(item) const content = $item .html() .split(' ') .map((word) => word.startsWith('http') ? `<a href='${word}' target="_blank">${word}</a>` : word) .join(' ') $item.html(content) }) }) // show jinja2 syntax in textarea with jinja2 const highlightJinjaSyntax = (type, id) => { // https://codemirror.net/doc/manual.html return window.CodeMirror.fromTextArea(document.getElementById(id), { mode: { name: type, htmlMode: true }, lineNumbers: true, tabSize: 2, extraKeys: { Tab: (cm) => cm.execCommand("indentMore"), "Shift-Tab": (cm) => cm.execCommand("indentLess"), }, }); } // transform urlTemplate input to textArea if (window.location.href.indexOf('/artifacts/') > 0) { ifExistsDo($(selector.jinja2UrlTemplate.selector), ($urlTemplate) => { const $textArea = $('<textarea>') .attr('name', $urlTemplate.attr('name')) .attr('id', $urlTemplate.attr('id')) .addClass($urlTemplate.attr('class')) .val($urlTemplate.val()) $urlTemplate.replaceWith($textArea) }) } // load script and enable highlighting if ($(selector.jinja2UrlTemplate.selector).length > 0 || $(selector.metaData.selector).length > 0) { Promise .all([ loadStyle('https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/codemirror.min.css'), loadScriptAndWait('https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/codemirror.min.js', 300), ]) .then(() => { if (!window.hasOwnProperty('CodeMirror'))
}) .then(() => loadScriptAndWait('https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/mode/jinja2/jinja2.min.js', 300)) .then(() => loadScriptAndWait('https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.44.0/mode/yaml/yaml.min.js', 300)) .then(() => { ifExistsDo($(selector.jinja2UrlTemplate.selector), ($_) => { highlightJinjaSyntax('jinja2', selector.jinja2UrlTemplate.id).setSize('', '60px') }) ifExistsDo($(selector.metaData.selector), ($_) => { const typeText = $(selector.metaDataType.id).text() const type = typeText.indexOf('jinja2') > 0 ? 'jinja2' : typeText.indexOf('yaml') ? 'yaml' : '' highlightJinjaSyntax(type, selector.metaData.id) }) }) .then(() => { $('.CodeMirror').css({ border: '1px solid #ccc', borderRadius: '4px' }) }) .catch((e) => console.error(e)) } // add dashboard overview if (window.location.href.indexOf('/artifacts') > 0 && $(selector.artifactItem).length > 0) { $('.n-overview').remove() const example = [ { domain: "consents", list: [ { id: 923, configurationId: 953, alias: 'thor (router)' }, { id: 962, configurationId: 963, alias: 'rogue (person-filter)' }, { id: 924, configurationId: 952, alias: 'ant-man (aggregator)' }, { id: 965, configurationId: 966, alias: 'quicksilver (deriver)' }, { id: 958, configurationId: 959, alias: 'hulk (data_processing-filter)' }, { id: 960, configurationId: 961, alias: 'she-hulk (all-intent-filter)' }, { id: 904, configurationId: 968, alias: 'loki (stator)' }, ] } ] const dashboardArtifactsOverview = localStorage.hasOwnProperty('dashboardArtifactsOverview') ? JSON.parse(localStorage.getItem('dashboardArtifactsOverview')) : [] const $createDomainTitle = (domain) => domain.length > 0 ? $('<h4></h4>') .text(domain) .css({ clear: 'both' }) : '' const $getArtifact = (id) => { const $artifact = $(`${selector.artifactItemId.selector}${id}`) return $artifact.length ? $artifact : $('<tr><td>id</td></tr>') // not all artifacts are shown on the page, but we can create a link .append($(`<td><a href="/artifacts/${id}">artefact id: ${id}</a></td>`)) } const $createAlias = (alias) => alias.length > 0 ? $('<h3></h3>') .text(alias) .css({ fontSize: 12, margin: 0, textAlign: 'right' }) : '' const $createArtifact = ($artifact) => $('<h4></h4>') .append($artifact.clone().find('td:nth-child(2)')) .css({ fontSize: 12 }) const $createConfiguration = ($configuration) => $configuration
{ throw Error('CodeMirror was not loaded correctly ...') }
conditional_block
mul_fixed.rs
`. assert_eq!( config.add_config.x_p, config.add_incomplete_config.x_p, "add and add_incomplete are used internally in mul_fixed." ); assert_eq!( config.add_config.y_p, config.add_incomplete_config.y_p, "add and add_incomplete are used internally in mul_fixed." ); for advice in [config.window, config.u].iter() { assert_ne!( *advice, config.add_config.x_qr, "Do not overlap with output columns of add." ); assert_ne!( *advice, config.add_config.y_qr, "Do not overlap with output columns of add." );
config.running_sum_coords_gate(meta); config } /// Check that each window in the running sum decomposition uses the correct y_p /// and interpolated x_p. /// /// This gate is used both in the mul_fixed::base_field_elem and mul_fixed::short /// helpers, which decompose the scalar using a running sum. /// /// This gate is not used in the mul_fixed::full_width helper, since the full-width /// scalar is witnessed directly as three-bit windows instead of being decomposed /// via a running sum. fn running_sum_coords_gate(&self, meta: &mut ConstraintSystem<pallas::Base>) { meta.create_gate("Running sum coordinates check", |meta| { let q_mul_fixed_running_sum = meta.query_selector(self.running_sum_config.q_range_check()); let z_cur = meta.query_advice(self.window, Rotation::cur()); let z_next = meta.query_advice(self.window, Rotation::next()); // z_{i+1} = (z_i - a_i) / 2^3 // => a_i = z_i - z_{i+1} * 2^3 let word = z_cur - z_next * pallas::Base::from(H as u64); Constraints::with_selector(q_mul_fixed_running_sum, self.coords_check(meta, word)) }); } /// [Specification](https://p.z.cash/halo2-0.1:ecc-fixed-mul-coordinates). #[allow(clippy::op_ref)] fn coords_check( &self, meta: &mut VirtualCells<'_, pallas::Base>, window: Expression<pallas::Base>, ) -> Vec<(&'static str, Expression<pallas::Base>)> { let y_p = meta.query_advice(self.add_config.y_p, Rotation::cur()); let x_p = meta.query_advice(self.add_config.x_p, Rotation::cur()); let z = meta.query_fixed(self.fixed_z); let u = meta.query_advice(self.u, Rotation::cur()); let window_pow: Vec<Expression<pallas::Base>> = (0..H) .map(|pow| { (0..pow).fold(Expression::Constant(pallas::Base::one()), |acc, _| { acc * window.clone() }) }) .collect(); let interpolated_x = window_pow.iter().zip(self.lagrange_coeffs.iter()).fold( Expression::Constant(pallas::Base::zero()), |acc, (window_pow, coeff)| acc + (window_pow.clone() * meta.query_fixed(*coeff)), ); // Check interpolation of x-coordinate let x_check = interpolated_x - x_p.clone(); // Check that `y + z = u^2`, where `z` is fixed and `u`, `y` are witnessed let y_check = u.square() - y_p.clone() - z; // Check that (x, y) is on the curve let on_curve = y_p.square() - x_p.clone().square() * x_p - Expression::Constant(pallas::Affine::b()); vec![ ("check x", x_check), ("check y", y_check), ("on-curve", on_curve), ] } #[allow(clippy::type_complexity)] fn assign_region_inner<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, scalar: &ScalarFixed, base: &F, coords_check_toggle: Selector, ) -> Result<(NonIdentityEccPoint, NonIdentityEccPoint), Error> { // Assign fixed columns for given fixed base self.assign_fixed_constants::<F, NUM_WINDOWS>(region, offset, base, coords_check_toggle)?; // Initialize accumulator let acc = self.initialize_accumulator::<F, NUM_WINDOWS>(region, offset, base, scalar)?; // Process all windows excluding least and most significant windows let acc = self.add_incomplete::<F, NUM_WINDOWS>(region, offset, acc, base, scalar)?; // Process most significant window let mul_b = self.process_msb::<F, NUM_WINDOWS>(region, offset, base, scalar)?; Ok((acc, mul_b)) } /// [Specification](https://p.z.cash/halo2-0.1:ecc-fixed-mul-load-base). fn assign_fixed_constants<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, base: &F, coords_check_toggle: Selector, ) -> Result<(), Error> { let mut constants = None; let build_constants = || { let lagrange_coeffs = base.lagrange_coeffs(); assert_eq!(lagrange_coeffs.len(), NUM_WINDOWS); let z = base.z(); assert_eq!(z.len(), NUM_WINDOWS); (lagrange_coeffs, z) }; // Assign fixed columns for given fixed base for window in 0..NUM_WINDOWS { coords_check_toggle.enable(region, window + offset)?; // Assign x-coordinate Lagrange interpolation coefficients for k in 0..H { region.assign_fixed( || { format!( "Lagrange interpolation coeff for window: {:?}, k: {:?}", window, k ) }, self.lagrange_coeffs[k], window + offset, || { if constants.as_ref().is_none() { constants = Some(build_constants()); } let lagrange_coeffs = &constants.as_ref().unwrap().0; Value::known(lagrange_coeffs[window][k]) }, )?; } // Assign z-values for each window region.assign_fixed( || format!("z-value for window: {:?}", window), self.fixed_z, window + offset, || { let z = &constants.as_ref().unwrap().1; Value::known(pallas::Base::from(z[window])) }, )?; } Ok(()) } /// Assigns the values used to process a window. fn process_window<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, w: usize, k_usize: Value<usize>, window_scalar: Value<pallas::Scalar>, base: &F, ) -> Result<NonIdentityEccPoint, Error> { let base_value = base.generator(); let base_u = base.u(); assert_eq!(base_u.len(), NUM_WINDOWS); // Compute [window_scalar]B let mul_b = { let mul_b = window_scalar.map(|scalar| base_value * scalar); let mul_b = mul_b.map(|mul_b| mul_b.to_affine().coordinates().unwrap()); let x = mul_b.map(|mul_b| { let x = *mul_b.x(); assert!(x != pallas::Base::zero()); x.into() }); let x = region.assign_advice( || format!("mul_b_x, window {}", w), self.add_config.x_p, offset + w, || x, )?; let y = mul_b.map(|mul_b| { let y = *mul_b.y(); assert!(y != pallas::Base::zero()); y.into() }); let y = region.assign_advice( || format!("mul_b_y, window {}", w), self.add_config.y_p, offset + w, || y, )?; NonIdentityEccPoint::from_coordinates_unchecked(x, y) }; // Assign u = (y_p + z_w).sqrt() let u_val = k_usize.map(|k| pallas::Base::from_repr(base_u[w][k]).unwrap()); region.assign_advice(|| "u", self.u, offset + w, || u_val)?; Ok(mul_b) } fn initialize_accumulator<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, base: &F, scalar: &ScalarFixed, ) -> Result<NonIdentityEccPoint, Error> { // Recall that the message at each window `w` is represented as // `m_w = [(k_w + 2) ⋅ 8^w]B`. // When `w = 0`, we have `m_0 = [(k_0 + 2)]B`. let w = 0; let k0 = scalar.windows_field()[0]; let k0_usize =
}
random_line_split
mul_fixed.rs
_toggle)?; // Initialize accumulator let acc = self.initialize_accumulator::<F, NUM_WINDOWS>(region, offset, base, scalar)?; // Process all windows excluding least and most significant windows let acc = self.add_incomplete::<F, NUM_WINDOWS>(region, offset, acc, base, scalar)?; // Process most significant window let mul_b = self.process_msb::<F, NUM_WINDOWS>(region, offset, base, scalar)?; Ok((acc, mul_b)) } /// [Specification](https://p.z.cash/halo2-0.1:ecc-fixed-mul-load-base). fn assign_fixed_constants<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, base: &F, coords_check_toggle: Selector, ) -> Result<(), Error> { let mut constants = None; let build_constants = || { let lagrange_coeffs = base.lagrange_coeffs(); assert_eq!(lagrange_coeffs.len(), NUM_WINDOWS); let z = base.z(); assert_eq!(z.len(), NUM_WINDOWS); (lagrange_coeffs, z) }; // Assign fixed columns for given fixed base for window in 0..NUM_WINDOWS { coords_check_toggle.enable(region, window + offset)?; // Assign x-coordinate Lagrange interpolation coefficients for k in 0..H { region.assign_fixed( || { format!( "Lagrange interpolation coeff for window: {:?}, k: {:?}", window, k ) }, self.lagrange_coeffs[k], window + offset, || { if constants.as_ref().is_none() { constants = Some(build_constants()); } let lagrange_coeffs = &constants.as_ref().unwrap().0; Value::known(lagrange_coeffs[window][k]) }, )?; } // Assign z-values for each window region.assign_fixed( || format!("z-value for window: {:?}", window), self.fixed_z, window + offset, || { let z = &constants.as_ref().unwrap().1; Value::known(pallas::Base::from(z[window])) }, )?; } Ok(()) } /// Assigns the values used to process a window. fn process_window<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, w: usize, k_usize: Value<usize>, window_scalar: Value<pallas::Scalar>, base: &F, ) -> Result<NonIdentityEccPoint, Error> { let base_value = base.generator(); let base_u = base.u(); assert_eq!(base_u.len(), NUM_WINDOWS); // Compute [window_scalar]B let mul_b = { let mul_b = window_scalar.map(|scalar| base_value * scalar); let mul_b = mul_b.map(|mul_b| mul_b.to_affine().coordinates().unwrap()); let x = mul_b.map(|mul_b| { let x = *mul_b.x(); assert!(x != pallas::Base::zero()); x.into() }); let x = region.assign_advice( || format!("mul_b_x, window {}", w), self.add_config.x_p, offset + w, || x, )?; let y = mul_b.map(|mul_b| { let y = *mul_b.y(); assert!(y != pallas::Base::zero()); y.into() }); let y = region.assign_advice( || format!("mul_b_y, window {}", w), self.add_config.y_p, offset + w, || y, )?; NonIdentityEccPoint::from_coordinates_unchecked(x, y) }; // Assign u = (y_p + z_w).sqrt() let u_val = k_usize.map(|k| pallas::Base::from_repr(base_u[w][k]).unwrap()); region.assign_advice(|| "u", self.u, offset + w, || u_val)?; Ok(mul_b) } fn initialize_accumulator<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, base: &F, scalar: &ScalarFixed, ) -> Result<NonIdentityEccPoint, Error> { // Recall that the message at each window `w` is represented as // `m_w = [(k_w + 2) ⋅ 8^w]B`. // When `w = 0`, we have `m_0 = [(k_0 + 2)]B`. let w = 0; let k0 = scalar.windows_field()[0]; let k0_usize = scalar.windows_usize()[0]; self.process_lower_bits::<_, NUM_WINDOWS>(region, offset, w, k0, k0_usize, base) } fn add_incomplete<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, mut acc: NonIdentityEccPoint, base: &F, scalar: &ScalarFixed, ) -> Result<NonIdentityEccPoint, Error> { let scalar_windows_field = scalar.windows_field(); let scalar_windows_usize = scalar.windows_usize(); assert_eq!(scalar_windows_field.len(), NUM_WINDOWS); for (w, (k, k_usize)) in scalar_windows_field .into_iter() .zip(scalar_windows_usize) .enumerate() // The MSB is processed separately. .take(NUM_WINDOWS - 1) // Skip k_0 (already processed). .skip(1) { // Compute [(k_w + 2) ⋅ 8^w]B // // This assigns the coordinates of the returned point into the input cells for // the incomplete addition gate, which will then copy them into themselves. let mul_b = self.process_lower_bits::<_, NUM_WINDOWS>(region, offset, w, k, k_usize, base)?; // Add to the accumulator. // // After the first loop, the accumulator will already be in the input cells // for the incomplete addition gate, and will be copied into themselves. acc = self .add_incomplete_config .assign_region(&mul_b, &acc, offset + w, region)?; } Ok(acc) } /// Assigns the values used to process a window that does not contain the MSB. fn process_lower_bits<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, w: usize, k: Value<pallas::Scalar>, k_usize: Value<usize>, base: &F, ) -> Result<NonIdentityEccPoint, Error> { // `scalar = [(k_w + 2) ⋅ 8^w] let scalar = k.map(|k| (k + *TWO_SCALAR) * (*H_SCALAR).pow(&[w as u64, 0, 0, 0])); self.process_window::<_, NUM_WINDOWS>(region, offset, w, k_usize, scalar, base) } /// Assigns the values used to process the window containing the MSB. fn process_msb<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, base: &F, scalar: &ScalarFixed, ) -> Result<NonIdentityEccPoint, Error> { let k_usize = scalar.windows_usize()[NUM_WINDOWS - 1]; // offset_acc = \sum_{j = 0}^{NUM_WINDOWS - 2} 2^{FIXED_BASE_WINDOW_SIZE*j + 1} let offset_acc = (0..(NUM_WINDOWS - 1)).fold(pallas::Scalar::zero(), |acc, w| { acc + (*TWO_SCALAR).pow(&[FIXED_BASE_WINDOW_SIZE as u64 * w as u64 + 1, 0, 0, 0]) }); // `scalar = [k * 8^(NUM_WINDOWS - 1) - offset_acc]`. let scalar = scalar.windows_field()[scalar.windows_field().len() - 1] .map(|k| k * (*H_SCALAR).pow(&[(NUM_WINDOWS - 1) as u64, 0, 0, 0]) - offset_acc); self.process_window::<_, NUM_WINDOWS>( region, offset, NUM_WINDOWS - 1, k_usize, scalar, base, ) } } enum ScalarFixed { FullWidth(EccScalarFixed), Short(EccScalarFixedShort), BaseFieldElem(EccBaseFieldElemFixed), } impl From<&EccScalarFixed> for ScalarFixed { fn from(scalar_fixed: &EccScalarFixed) -> Self {
Self::FullWidth(scalar_fixed.clone()) } } im
identifier_body
mul_fixed.rs
: &mut Region<'_, pallas::Base>, offset: usize, base: &F, coords_check_toggle: Selector, ) -> Result<(), Error> { let mut constants = None; let build_constants = || { let lagrange_coeffs = base.lagrange_coeffs(); assert_eq!(lagrange_coeffs.len(), NUM_WINDOWS); let z = base.z(); assert_eq!(z.len(), NUM_WINDOWS); (lagrange_coeffs, z) }; // Assign fixed columns for given fixed base for window in 0..NUM_WINDOWS { coords_check_toggle.enable(region, window + offset)?; // Assign x-coordinate Lagrange interpolation coefficients for k in 0..H { region.assign_fixed( || { format!( "Lagrange interpolation coeff for window: {:?}, k: {:?}", window, k ) }, self.lagrange_coeffs[k], window + offset, || { if constants.as_ref().is_none() { constants = Some(build_constants()); } let lagrange_coeffs = &constants.as_ref().unwrap().0; Value::known(lagrange_coeffs[window][k]) }, )?; } // Assign z-values for each window region.assign_fixed( || format!("z-value for window: {:?}", window), self.fixed_z, window + offset, || { let z = &constants.as_ref().unwrap().1; Value::known(pallas::Base::from(z[window])) }, )?; } Ok(()) } /// Assigns the values used to process a window. fn process_window<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, w: usize, k_usize: Value<usize>, window_scalar: Value<pallas::Scalar>, base: &F, ) -> Result<NonIdentityEccPoint, Error> { let base_value = base.generator(); let base_u = base.u(); assert_eq!(base_u.len(), NUM_WINDOWS); // Compute [window_scalar]B let mul_b = { let mul_b = window_scalar.map(|scalar| base_value * scalar); let mul_b = mul_b.map(|mul_b| mul_b.to_affine().coordinates().unwrap()); let x = mul_b.map(|mul_b| { let x = *mul_b.x(); assert!(x != pallas::Base::zero()); x.into() }); let x = region.assign_advice( || format!("mul_b_x, window {}", w), self.add_config.x_p, offset + w, || x, )?; let y = mul_b.map(|mul_b| { let y = *mul_b.y(); assert!(y != pallas::Base::zero()); y.into() }); let y = region.assign_advice( || format!("mul_b_y, window {}", w), self.add_config.y_p, offset + w, || y, )?; NonIdentityEccPoint::from_coordinates_unchecked(x, y) }; // Assign u = (y_p + z_w).sqrt() let u_val = k_usize.map(|k| pallas::Base::from_repr(base_u[w][k]).unwrap()); region.assign_advice(|| "u", self.u, offset + w, || u_val)?; Ok(mul_b) } fn initialize_accumulator<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, base: &F, scalar: &ScalarFixed, ) -> Result<NonIdentityEccPoint, Error> { // Recall that the message at each window `w` is represented as // `m_w = [(k_w + 2) ⋅ 8^w]B`. // When `w = 0`, we have `m_0 = [(k_0 + 2)]B`. let w = 0; let k0 = scalar.windows_field()[0]; let k0_usize = scalar.windows_usize()[0]; self.process_lower_bits::<_, NUM_WINDOWS>(region, offset, w, k0, k0_usize, base) } fn add_incomplete<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, mut acc: NonIdentityEccPoint, base: &F, scalar: &ScalarFixed, ) -> Result<NonIdentityEccPoint, Error> { let scalar_windows_field = scalar.windows_field(); let scalar_windows_usize = scalar.windows_usize(); assert_eq!(scalar_windows_field.len(), NUM_WINDOWS); for (w, (k, k_usize)) in scalar_windows_field .into_iter() .zip(scalar_windows_usize) .enumerate() // The MSB is processed separately. .take(NUM_WINDOWS - 1) // Skip k_0 (already processed). .skip(1) { // Compute [(k_w + 2) ⋅ 8^w]B // // This assigns the coordinates of the returned point into the input cells for // the incomplete addition gate, which will then copy them into themselves. let mul_b = self.process_lower_bits::<_, NUM_WINDOWS>(region, offset, w, k, k_usize, base)?; // Add to the accumulator. // // After the first loop, the accumulator will already be in the input cells // for the incomplete addition gate, and will be copied into themselves. acc = self .add_incomplete_config .assign_region(&mul_b, &acc, offset + w, region)?; } Ok(acc) } /// Assigns the values used to process a window that does not contain the MSB. fn process_lower_bits<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, w: usize, k: Value<pallas::Scalar>, k_usize: Value<usize>, base: &F, ) -> Result<NonIdentityEccPoint, Error> { // `scalar = [(k_w + 2) ⋅ 8^w] let scalar = k.map(|k| (k + *TWO_SCALAR) * (*H_SCALAR).pow(&[w as u64, 0, 0, 0])); self.process_window::<_, NUM_WINDOWS>(region, offset, w, k_usize, scalar, base) } /// Assigns the values used to process the window containing the MSB. fn process_msb<F: FixedPoint<pallas::Affine>, const NUM_WINDOWS: usize>( &self, region: &mut Region<'_, pallas::Base>, offset: usize, base: &F, scalar: &ScalarFixed, ) -> Result<NonIdentityEccPoint, Error> { let k_usize = scalar.windows_usize()[NUM_WINDOWS - 1]; // offset_acc = \sum_{j = 0}^{NUM_WINDOWS - 2} 2^{FIXED_BASE_WINDOW_SIZE*j + 1} let offset_acc = (0..(NUM_WINDOWS - 1)).fold(pallas::Scalar::zero(), |acc, w| { acc + (*TWO_SCALAR).pow(&[FIXED_BASE_WINDOW_SIZE as u64 * w as u64 + 1, 0, 0, 0]) }); // `scalar = [k * 8^(NUM_WINDOWS - 1) - offset_acc]`. let scalar = scalar.windows_field()[scalar.windows_field().len() - 1] .map(|k| k * (*H_SCALAR).pow(&[(NUM_WINDOWS - 1) as u64, 0, 0, 0]) - offset_acc); self.process_window::<_, NUM_WINDOWS>( region, offset, NUM_WINDOWS - 1, k_usize, scalar, base, ) } } enum ScalarFixed { FullWidth(EccScalarFixed), Short(EccScalarFixedShort), BaseFieldElem(EccBaseFieldElemFixed), } impl From<&EccScalarFixed> for ScalarFixed { fn from(scalar_fixed: &EccScalarFixed) -> Self { Self::FullWidth(scalar_fixed.clone()) } } impl From<&EccScalarFixedShort> for ScalarFixed { fn from(scalar_fixed: &EccScalarFixedShort) -> Self { Self::Short(scalar_fixed.clone()) } } impl From<&EccBaseFieldElemFixed> for ScalarFixed { fn from(base_field_elem: &EccBaseFieldElemFixed) -> Self { Self::BaseFieldElem(base_field_elem.clone()) } } impl ScalarFixed { /// The scalar decomposition was done in the base field. For computation /// outside the circuit, we now convert them back into the scalar field. /// /// This function does not require that the base field fits inside the scalar field, /// because the window size fits into either field. fn window
s_field(&self
identifier_name
RhinoResponse.ts
{ private _headersSent: boolean = false; private STATUS: number = 200; private COOKIES: HTTPServer.Response = {}; private readonly RESPONSE_HEADERS = new Headers(); constructor(private readonly HTTP_REQUEST: HTTPServer.ServerRequest) {} /** * Appends a value to an already existing HTTP header field, or * adds the field if it has not been created. * @param field The HTTP header field name * @param value The value for the header field. * @note Should follow the standard from https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ public append(field: string, value: string | string[]) { this.appendHeader(field, value); return this; } /** * Appends a value to an already existing HTTP header field, or * adds the field if it has not been created. * @param field The HTTP header field name * @param value The value for the header field. * @note Should follow the standard from https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ public appendHeader(field: string, value: string | string[]): RhinoResponse { if (Array.isArray(value)) { // Joins the values with a comma, if they are an array of values value.forEach((val) => this.RESPONSE_HEADERS.append(field, val)); } else { // Otherwise, just adds the value to the header this.RESPONSE_HEADERS.append(field, value); } return this; } /** * Sets the HTTP response Content-Disposition header field to “attachment”. * If a filename is given, then it sets the Content-Type based on the extension name via res.type(), * and sets the Content-Disposition “filename=” parameter. * @param filename The name that will be set to the downloaded file */ public asAttachment(filename?: string): RhinoResponse { const _filename = filename ? `filename=${filename};` : ""; this.setHeader(HeaderField.ContentDisposition, `attachment; ${_filename}`); const ext = Path.extname(filename || ""); this.contentType(ext); return this; } /** * Clears a cookie from the response by setting * its expiration date to a date before now. * @param name The name of the cookie */ public clearCookie(name: string) { // Sets the cookie to be appended to the response Cookies.delCookie(this.COOKIES, name); } /** * Sends a cookie to the client with the specified name and value * @param name The name of the cookie * @param value The value of the cookie * @param options Optional parameters for the cookie */ public cookie(name: string, value: string | number | object, options?: CookieOptions) { if (typeof value === "string") value = value; if (typeof value === "number") value = value.toString(); if (typeof value === "object") value = JSON.stringify(value); // Form the cookie data const c: Cookies.Cookie = { name, value, ...options }; // Sets the cookie to be appended to the response Cookies.setCookie(this.COOKIES, c); } /** * Sets the content-type header. * @param mime The MIME type, or file extension * * @example * ``` * // file extension * this.res.contentType('.css') * // file type * this.res.contentType('html') * // Mime type from the MIMEType Enumerable * this.res.contentType(MIMEType.TextMarkdown) * // Direct MIME type * this.res.contentType('application/json') * ``` */ public contentType(mime: string): RhinoResponse { let _mime: string = MIMEType.TextPlain; if (mime.includes("/")) { // If the passed mime contains a slash, we // will assume it to be a mime type directly. _mime = mime; } else { // For anything else, we assume it to be a file extension or file type. // If the file extension could not be understood, we leave it as text/plain const foundMime = Object.keys(ExtMIMEType).filter((key) => { const keyName = (mime.startsWith(".") ? "." : "") + key; return keyName === mime; }); let mimeFileExt = ExtMIMEType[foundMime[0]]; if (mimeFileExt) { _mime = mimeFileExt; } else { Utils.logPassiveError( "The passed file extension could not be understood. Please specify the mime type directly." ); } } this.set(HeaderField.ContentType, _mime); return this; } /** * Send the contents of a file in the HTTP response's body for automatic download by the client. * @param filepath The relative or absolute path to the file. If the * path is relative to the file, then the entry point of the application should * be the base of the file's path. * @note Requires the --allow-read flag */ public async download(filepath: string, filename?: string): Promise<any> { const file_n = filename ?? filepath.split("/").pop(); return new Promise(async (resolve, reject) => { try { this.asAttachment(file_n); this.sendFile(filepath) .then(() => resolve(true)) .catch((err) => { this.removeHeader(HeaderField.ContentDisposition); reject(err); }); } catch (err) { reject(err); } }); } /** * Sends the response without any data. */ public end() { this.HTTP_REQUEST.finalize(); } /** * Returns wether the headers have been sent or not */ public get headersSent(): boolean { return this._headersSent; } /** * Removes a header field from the the response headers. * @param field The header field to be removed */ public removeHeader(field: string) { this.RESPONSE_HEADERS.delete(field); } /** * Sends the passed resData as a response to the client * @param resData The data to send to the client */ public send(resData: any) { let d = resData; // If a response was already sent to the client, we log an error if (this._headersSent) Utils.logPassiveError("A response has already been sent!"); // If the data is an object, we convert it to a string if (typeof d === "object") d = JSON.stringify(d); this.HTTP_REQUEST.done.then(() => { // Prevents any other responses from being // sent after this response has been sent. this._headersSent = true; }); // If no response has been sent to the client, the data is sent to the client if (!this._headersSent) this.HTTP_REQUEST.respond({ status: this.STATUS, headers: this.RESPONSE_HEADERS, body: d, // converts any data into a string ...this.COOKIES, }); } /** * Transfers the file at the given path in the response body, * and sets the content-type header based on the file's extension. * @param filepath The relative or absolute path to the file. If the * path is relative to the file, then the entry point of the application should * be the base of the file's path. */ public async sendFile(filepath: any): Promise<any> { // If a response was already sent to the client, we log an error if (this._headersSent) Utils.logPassiveError("A response has already been sent!"); const p = Path.resolve(filepath); const ext = Path.extname(filepath); return new Promise(async (resolve, reject) => { try { const [file, fileInfo] = await Promise.all([Deno.open(p), Deno.stat(p)]); this.contentType(ext); this.setHeader("content-length", fileInfo.size.toString()); this.HTTP_REQUEST.done.then(() => { // closes the file after the request is done file.close(); // Prevents any other responses from being // sent after this response has been sent. this._headersSent = true; resolve(true); }); // If no response has been sent to the client, the data is sent to the client if (!this._headersSent) this.HTTP_REQUEST.respond({ status: this.STATUS, headers: this.RESPONSE_HEADERS, body: file, ...this.COOKIES, }); } catch (err) { reject(err); } }); } /** * Sends a status code and its string name to the client. * @param code The status code */ public sendStatus(code: StatusCode) { const n = StatusCodeName[Object.keys(StatusCode)[Object.values(StatusCode).indexOf(code)]]; this.status(code).send(n); } /** * Creates an HTTP header field with the passed value. Resets the field if * it already exists, or creates it if it does not exist. * @param field The HTTP header field name. * @param value The value for the header field
RhinoResponse
identifier_name
RhinoResponse.ts
ends a value to an already existing HTTP header field, or * adds the field if it has not been created. * @param field The HTTP header field name * @param value The value for the header field. * @note Should follow the standard from https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ public append(field: string, value: string | string[]) { this.appendHeader(field, value); return this; } /** * Appends a value to an already existing HTTP header field, or * adds the field if it has not been created. * @param field The HTTP header field name * @param value The value for the header field. * @note Should follow the standard from https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ public appendHeader(field: string, value: string | string[]): RhinoResponse { if (Array.isArray(value)) { // Joins the values with a comma, if they are an array of values value.forEach((val) => this.RESPONSE_HEADERS.append(field, val)); } else { // Otherwise, just adds the value to the header this.RESPONSE_HEADERS.append(field, value); } return this; } /** * Sets the HTTP response Content-Disposition header field to “attachment”. * If a filename is given, then it sets the Content-Type based on the extension name via res.type(), * and sets the Content-Disposition “filename=” parameter. * @param filename The name that will be set to the downloaded file */ public asAttachment(filename?: string): RhinoResponse {
* * Clears a cookie from the response by setting * its expiration date to a date before now. * @param name The name of the cookie */ public clearCookie(name: string) { // Sets the cookie to be appended to the response Cookies.delCookie(this.COOKIES, name); } /** * Sends a cookie to the client with the specified name and value * @param name The name of the cookie * @param value The value of the cookie * @param options Optional parameters for the cookie */ public cookie(name: string, value: string | number | object, options?: CookieOptions) { if (typeof value === "string") value = value; if (typeof value === "number") value = value.toString(); if (typeof value === "object") value = JSON.stringify(value); // Form the cookie data const c: Cookies.Cookie = { name, value, ...options }; // Sets the cookie to be appended to the response Cookies.setCookie(this.COOKIES, c); } /** * Sets the content-type header. * @param mime The MIME type, or file extension * * @example * ``` * // file extension * this.res.contentType('.css') * // file type * this.res.contentType('html') * // Mime type from the MIMEType Enumerable * this.res.contentType(MIMEType.TextMarkdown) * // Direct MIME type * this.res.contentType('application/json') * ``` */ public contentType(mime: string): RhinoResponse { let _mime: string = MIMEType.TextPlain; if (mime.includes("/")) { // If the passed mime contains a slash, we // will assume it to be a mime type directly. _mime = mime; } else { // For anything else, we assume it to be a file extension or file type. // If the file extension could not be understood, we leave it as text/plain const foundMime = Object.keys(ExtMIMEType).filter((key) => { const keyName = (mime.startsWith(".") ? "." : "") + key; return keyName === mime; }); let mimeFileExt = ExtMIMEType[foundMime[0]]; if (mimeFileExt) { _mime = mimeFileExt; } else { Utils.logPassiveError( "The passed file extension could not be understood. Please specify the mime type directly." ); } } this.set(HeaderField.ContentType, _mime); return this; } /** * Send the contents of a file in the HTTP response's body for automatic download by the client. * @param filepath The relative or absolute path to the file. If the * path is relative to the file, then the entry point of the application should * be the base of the file's path. * @note Requires the --allow-read flag */ public async download(filepath: string, filename?: string): Promise<any> { const file_n = filename ?? filepath.split("/").pop(); return new Promise(async (resolve, reject) => { try { this.asAttachment(file_n); this.sendFile(filepath) .then(() => resolve(true)) .catch((err) => { this.removeHeader(HeaderField.ContentDisposition); reject(err); }); } catch (err) { reject(err); } }); } /** * Sends the response without any data. */ public end() { this.HTTP_REQUEST.finalize(); } /** * Returns wether the headers have been sent or not */ public get headersSent(): boolean { return this._headersSent; } /** * Removes a header field from the the response headers. * @param field The header field to be removed */ public removeHeader(field: string) { this.RESPONSE_HEADERS.delete(field); } /** * Sends the passed resData as a response to the client * @param resData The data to send to the client */ public send(resData: any) { let d = resData; // If a response was already sent to the client, we log an error if (this._headersSent) Utils.logPassiveError("A response has already been sent!"); // If the data is an object, we convert it to a string if (typeof d === "object") d = JSON.stringify(d); this.HTTP_REQUEST.done.then(() => { // Prevents any other responses from being // sent after this response has been sent. this._headersSent = true; }); // If no response has been sent to the client, the data is sent to the client if (!this._headersSent) this.HTTP_REQUEST.respond({ status: this.STATUS, headers: this.RESPONSE_HEADERS, body: d, // converts any data into a string ...this.COOKIES, }); } /** * Transfers the file at the given path in the response body, * and sets the content-type header based on the file's extension. * @param filepath The relative or absolute path to the file. If the * path is relative to the file, then the entry point of the application should * be the base of the file's path. */ public async sendFile(filepath: any): Promise<any> { // If a response was already sent to the client, we log an error if (this._headersSent) Utils.logPassiveError("A response has already been sent!"); const p = Path.resolve(filepath); const ext = Path.extname(filepath); return new Promise(async (resolve, reject) => { try { const [file, fileInfo] = await Promise.all([Deno.open(p), Deno.stat(p)]); this.contentType(ext); this.setHeader("content-length", fileInfo.size.toString()); this.HTTP_REQUEST.done.then(() => { // closes the file after the request is done file.close(); // Prevents any other responses from being // sent after this response has been sent. this._headersSent = true; resolve(true); }); // If no response has been sent to the client, the data is sent to the client if (!this._headersSent) this.HTTP_REQUEST.respond({ status: this.STATUS, headers: this.RESPONSE_HEADERS, body: file, ...this.COOKIES, }); } catch (err) { reject(err); } }); } /** * Sends a status code and its string name to the client. * @param code The status code */ public sendStatus(code: StatusCode) { const n = StatusCodeName[Object.keys(StatusCode)[Object.values(StatusCode).indexOf(code)]]; this.status(code).send(n); } /** * Creates an HTTP header field with the passed value. Resets the field if * it already exists, or creates it if it does not exist. * @param field The HTTP header field name. * @param value The value for the header field. * @note Should follow the standard from https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ public set(field: HeaderField, value: string | string[]): RhinoResponse { this.setHeader(field, value);
const _filename = filename ? `filename=${filename};` : ""; this.setHeader(HeaderField.ContentDisposition, `attachment; ${_filename}`); const ext = Path.extname(filename || ""); this.contentType(ext); return this; } /*
identifier_body
RhinoResponse.ts
val) => this.RESPONSE_HEADERS.append(field, val)); } else { // Otherwise, just adds the value to the header this.RESPONSE_HEADERS.append(field, value); } return this; } /** * Sets the HTTP response Content-Disposition header field to “attachment”. * If a filename is given, then it sets the Content-Type based on the extension name via res.type(), * and sets the Content-Disposition “filename=” parameter. * @param filename The name that will be set to the downloaded file */ public asAttachment(filename?: string): RhinoResponse { const _filename = filename ? `filename=${filename};` : ""; this.setHeader(HeaderField.ContentDisposition, `attachment; ${_filename}`); const ext = Path.extname(filename || ""); this.contentType(ext); return this; } /** * Clears a cookie from the response by setting * its expiration date to a date before now. * @param name The name of the cookie */ public clearCookie(name: string) { // Sets the cookie to be appended to the response Cookies.delCookie(this.COOKIES, name); } /** * Sends a cookie to the client with the specified name and value * @param name The name of the cookie * @param value The value of the cookie * @param options Optional parameters for the cookie */ public cookie(name: string, value: string | number | object, options?: CookieOptions) { if (typeof value === "string") value = value; if (typeof value === "number") value = value.toString(); if (typeof value === "object") value = JSON.stringify(value); // Form the cookie data const c: Cookies.Cookie = { name, value, ...options }; // Sets the cookie to be appended to the response Cookies.setCookie(this.COOKIES, c); } /** * Sets the content-type header. * @param mime The MIME type, or file extension * * @example * ``` * // file extension * this.res.contentType('.css') * // file type * this.res.contentType('html') * // Mime type from the MIMEType Enumerable * this.res.contentType(MIMEType.TextMarkdown) * // Direct MIME type * this.res.contentType('application/json') * ``` */ public contentType(mime: string): RhinoResponse { let _mime: string = MIMEType.TextPlain; if (mime.includes("/")) { // If the passed mime contains a slash, we // will assume it to be a mime type directly. _mime = mime; } else { // For anything else, we assume it to be a file extension or file type. // If the file extension could not be understood, we leave it as text/plain const foundMime = Object.keys(ExtMIMEType).filter((key) => { const keyName = (mime.startsWith(".") ? "." : "") + key; return keyName === mime; }); let mimeFileExt = ExtMIMEType[foundMime[0]]; if (mimeFileExt) { _mime = mimeFileExt; } else { Utils.logPassiveError( "The passed file extension could not be understood. Please specify the mime type directly." ); } } this.set(HeaderField.ContentType, _mime); return this; } /** * Send the contents of a file in the HTTP response's body for automatic download by the client. * @param filepath The relative or absolute path to the file. If the * path is relative to the file, then the entry point of the application should * be the base of the file's path. * @note Requires the --allow-read flag */ public async download(filepath: string, filename?: string): Promise<any> { const file_n = filename ?? filepath.split("/").pop(); return new Promise(async (resolve, reject) => { try { this.asAttachment(file_n); this.sendFile(filepath) .then(() => resolve(true)) .catch((err) => { this.removeHeader(HeaderField.ContentDisposition); reject(err); }); } catch (err) { reject(err); } }); } /** * Sends the response without any data. */ public end() { this.HTTP_REQUEST.finalize(); } /** * Returns wether the headers have been sent or not */ public get headersSent(): boolean { return this._headersSent; } /** * Removes a header field from the the response headers. * @param field The header field to be removed */ public removeHeader(field: string) { this.RESPONSE_HEADERS.delete(field); } /** * Sends the passed resData as a response to the client * @param resData The data to send to the client */ public send(resData: any) { let d = resData; // If a response was already sent to the client, we log an error if (this._headersSent) Utils.logPassiveError("A response has already been sent!"); // If the data is an object, we convert it to a string if (typeof d === "object") d = JSON.stringify(d); this.HTTP_REQUEST.done.then(() => { // Prevents any other responses from being // sent after this response has been sent. this._headersSent = true; }); // If no response has been sent to the client, the data is sent to the client if (!this._headersSent) this.HTTP_REQUEST.respond({ status: this.STATUS, headers: this.RESPONSE_HEADERS, body: d, // converts any data into a string ...this.COOKIES, }); } /** * Transfers the file at the given path in the response body, * and sets the content-type header based on the file's extension. * @param filepath The relative or absolute path to the file. If the * path is relative to the file, then the entry point of the application should * be the base of the file's path. */ public async sendFile(filepath: any): Promise<any> { // If a response was already sent to the client, we log an error if (this._headersSent) Utils.logPassiveError("A response has already been sent!"); const p = Path.resolve(filepath); const ext = Path.extname(filepath); return new Promise(async (resolve, reject) => { try { const [file, fileInfo] = await Promise.all([Deno.open(p), Deno.stat(p)]); this.contentType(ext); this.setHeader("content-length", fileInfo.size.toString()); this.HTTP_REQUEST.done.then(() => { // closes the file after the request is done file.close(); // Prevents any other responses from being // sent after this response has been sent. this._headersSent = true; resolve(true); }); // If no response has been sent to the client, the data is sent to the client if (!this._headersSent) this.HTTP_REQUEST.respond({ status: this.STATUS, headers: this.RESPONSE_HEADERS, body: file, ...this.COOKIES, }); } catch (err) { reject(err); } }); } /** * Sends a status code and its string name to the client. * @param code The status code */ public sendStatus(code: StatusCode) { const n = StatusCodeName[Object.keys(StatusCode)[Object.values(StatusCode).indexOf(code)]]; this.status(code).send(n); } /** * Creates an HTTP header field with the passed value. Resets the field if * it already exists, or creates it if it does not exist. * @param field The HTTP header field name. * @param value The value for the header field. * @note Should follow the standard from https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ public set(field: HeaderField, value: string | string[]): RhinoResponse { this.setHeader(field, value); return this; } /** * Creates an HTTP header field with the passed value. Resets the field if * it already exists, or creates it if it does not exist. * @note Should follow the standard from https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ public setHeader(field: string, value: string | string[]): RhinoResponse { if (Array.isArray(value)) { // If the header field already exists, we delete it. if (this.RESPONSE_HEADERS.get(field)) this.RESPONSE_HEADERS.delete(field); // NOTE: calling Header.append() here was not a mistake. We cannot call // Headers.set() in this case because that would reset the value // of the field on every call of the loop. Instead, we delete the field // (in case it already existed) and append to a new empty field. value.forEach((val) => this.RESPONSE_HEADERS.append(field, val)); } else {
// Otherwise, just adds the value to the header this.RESPONSE_HEADERS.set(field, value); }
conditional_block
RhinoResponse.ts
// Derives the optional values of a cookie export type CookieOptions = Omit<Omit<Cookies.Cookie, "name">, "value">; export class RhinoResponse { private _headersSent: boolean = false; private STATUS: number = 200; private COOKIES: HTTPServer.Response = {}; private readonly RESPONSE_HEADERS = new Headers(); constructor(private readonly HTTP_REQUEST: HTTPServer.ServerRequest) {} /** * Appends a value to an already existing HTTP header field, or * adds the field if it has not been created. * @param field The HTTP header field name * @param value The value for the header field. * @note Should follow the standard from https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ public append(field: string, value: string | string[]) { this.appendHeader(field, value); return this; } /** * Appends a value to an already existing HTTP header field, or * adds the field if it has not been created. * @param field The HTTP header field name * @param value The value for the header field. * @note Should follow the standard from https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ public appendHeader(field: string, value: string | string[]): RhinoResponse { if (Array.isArray(value)) { // Joins the values with a comma, if they are an array of values value.forEach((val) => this.RESPONSE_HEADERS.append(field, val)); } else { // Otherwise, just adds the value to the header this.RESPONSE_HEADERS.append(field, value); } return this; } /** * Sets the HTTP response Content-Disposition header field to “attachment”. * If a filename is given, then it sets the Content-Type based on the extension name via res.type(), * and sets the Content-Disposition “filename=” parameter. * @param filename The name that will be set to the downloaded file */ public asAttachment(filename?: string): RhinoResponse { const _filename = filename ? `filename=${filename};` : ""; this.setHeader(HeaderField.ContentDisposition, `attachment; ${_filename}`); const ext = Path.extname(filename || ""); this.contentType(ext); return this; } /** * Clears a cookie from the response by setting * its expiration date to a date before now. * @param name The name of the cookie */ public clearCookie(name: string) { // Sets the cookie to be appended to the response Cookies.delCookie(this.COOKIES, name); } /** * Sends a cookie to the client with the specified name and value * @param name The name of the cookie * @param value The value of the cookie * @param options Optional parameters for the cookie */ public cookie(name: string, value: string | number | object, options?: CookieOptions) { if (typeof value === "string") value = value; if (typeof value === "number") value = value.toString(); if (typeof value === "object") value = JSON.stringify(value); // Form the cookie data const c: Cookies.Cookie = { name, value, ...options }; // Sets the cookie to be appended to the response Cookies.setCookie(this.COOKIES, c); } /** * Sets the content-type header. * @param mime The MIME type, or file extension * * @example * ``` * // file extension * this.res.contentType('.css') * // file type * this.res.contentType('html') * // Mime type from the MIMEType Enumerable * this.res.contentType(MIMEType.TextMarkdown) * // Direct MIME type * this.res.contentType('application/json') * ``` */ public contentType(mime: string): RhinoResponse { let _mime: string = MIMEType.TextPlain; if (mime.includes("/")) { // If the passed mime contains a slash, we // will assume it to be a mime type directly. _mime = mime; } else { // For anything else, we assume it to be a file extension or file type. // If the file extension could not be understood, we leave it as text/plain const foundMime = Object.keys(ExtMIMEType).filter((key) => { const keyName = (mime.startsWith(".") ? "." : "") + key; return keyName === mime; }); let mimeFileExt = ExtMIMEType[foundMime[0]]; if (mimeFileExt) { _mime = mimeFileExt; } else { Utils.logPassiveError( "The passed file extension could not be understood. Please specify the mime type directly." ); } } this.set(HeaderField.ContentType, _mime); return this; } /** * Send the contents of a file in the HTTP response's body for automatic download by the client. * @param filepath The relative or absolute path to the file. If the * path is relative to the file, then the entry point of the application should * be the base of the file's path. * @note Requires the --allow-read flag */ public async download(filepath: string, filename?: string): Promise<any> { const file_n = filename ?? filepath.split("/").pop(); return new Promise(async (resolve, reject) => { try { this.asAttachment(file_n); this.sendFile(filepath) .then(() => resolve(true)) .catch((err) => { this.removeHeader(HeaderField.ContentDisposition); reject(err); }); } catch (err) { reject(err); } }); } /** * Sends the response without any data. */ public end() { this.HTTP_REQUEST.finalize(); } /** * Returns wether the headers have been sent or not */ public get headersSent(): boolean { return this._headersSent; } /** * Removes a header field from the the response headers. * @param field The header field to be removed */ public removeHeader(field: string) { this.RESPONSE_HEADERS.delete(field); } /** * Sends the passed resData as a response to the client * @param resData The data to send to the client */ public send(resData: any) { let d = resData; // If a response was already sent to the client, we log an error if (this._headersSent) Utils.logPassiveError("A response has already been sent!"); // If the data is an object, we convert it to a string if (typeof d === "object") d = JSON.stringify(d); this.HTTP_REQUEST.done.then(() => { // Prevents any other responses from being // sent after this response has been sent. this._headersSent = true; }); // If no response has been sent to the client, the data is sent to the client if (!this._headersSent) this.HTTP_REQUEST.respond({ status: this.STATUS, headers: this.RESPONSE_HEADERS, body: d, // converts any data into a string ...this.COOKIES, }); } /** * Transfers the file at the given path in the response body, * and sets the content-type header based on the file's extension. * @param filepath The relative or absolute path to the file. If the * path is relative to the file, then the entry point of the application should * be the base of the file's path. */ public async sendFile(filepath: any): Promise<any> { // If a response was already sent to the client, we log an error if (this._headersSent) Utils.logPassiveError("A response has already been sent!"); const p = Path.resolve(filepath); const ext = Path.extname(filepath); return new Promise(async (resolve, reject) => { try { const [file, fileInfo] = await Promise.all([Deno.open(p), Deno.stat(p)]); this.contentType(ext); this.setHeader("content-length", fileInfo.size.toString()); this.HTTP_REQUEST.done.then(() => { // closes the file after the request is done file.close(); // Prevents any other responses from being // sent after this response has been sent. this._headersSent = true; resolve(true); }); // If no response has been sent to the client, the data is sent to the client if (!this._headersSent) this.HTTP_REQUEST.respond({ status: this.STATUS, headers: this.RESPONSE_HEADERS, body: file, ...this.COOKIES, }); } catch (err) { reject(err); } }); } /** * Sends a status code and its string name to the client. * @param code The status code */ public sendStatus(code: StatusCode) { const n = StatusCodeName[Object.keys(StatusCode)[Object.values(StatusCode).indexOf(code)]]; this.status(code).send(n); } /** * Creates an HTTP header field with the passed value. Resets the field if * it already exists
random_line_split
node.go
(row, "|") + "]" } return fmt.Sprintf("%s [page=%d, all<=%s]", c.node.typ.String(), c.pageNumber, key) } type Record struct { rowid int columns []Column } func (r Record) String() string { var row []string for _, c := range r.columns { row = append(row, c.String()) } return fmt.Sprintf("rowid=%+v columns=[%s]", r.rowid, strings.Join(row, "|")) } func (r Record) GetColumn(idx int) Column { if idx < len(r.columns) { return r.columns[idx] } // NULL return Column{ typ: 0, } } type Column struct { typ int content []byte } func (c Column) String() string { v := c.Value() switch vt := v.(type) { case string: return vt case []byte: return "\"" + string(vt) + "\"" case float64: return fmt.Sprintf("%f", vt) case int64: return fmt.Sprintf("%d", vt) case time.Time: return vt.String() case bool: return fmt.Sprintf("%v", vt) case nil: return "nil" default: return fmt.Sprintf("<unknown column type = %s>", reflect.TypeOf(v)) } } func (c Column) AsInt() (int, bool) { // [1, 6] are the various int64 types if c.typ >= 1 && c.typ <= 6 { i64 := c.Value().(int64) return int(i64), true } return 0, false } func (c Column) Value() driver.Value { // TODO: validate this works with negative integers (2's complement) switch c.typ { case 0: return nil case 1: return int64(c.content[0]) case 2: return int64(binary.BigEndian.Uint16(c.content)) case 3: // stdlib binary does not have a 24-bit option b := c.content u := uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16 return int64(u) case 4: return int64(binary.BigEndian.Uint32(c.content)) case 5: // stdlib binary does not have a 48-bit option b := c.content u := uint64(b[5]) | uint64(b[4])<<8 | uint64(b[3])<<16 | uint64(b[2])<<24 | uint64(b[1])<<32 | uint64(b[0])<<40 return int64(u) case 6: return int64(binary.BigEndian.Uint64(c.content)) case 7: b := binary.BigEndian.Uint64(c.content) return math.Float64frombits(b) case 8: return int64(0) case 9: return int64(1) default: if c.typ%2 == 0 { // blob return c.content } else { // string return string(c.content) } } } // https://www.sqlite.org/fileformat2.html#serialtype func
(typ int) int { switch typ { case 0, 8, 9: return 0 case 1: return 1 case 2: return 2 case 3: return 3 case 4: return 4 case 5: return 6 case 6, 7: return 8 case 10, 11: // https://github.com/sqlite/sqlite/blob/96e3c39bd58ede59150c00e4f8609cbac674ffae/tool/offsets.c#L216 // return 0 panic(fmt.Errorf("cannot support columns of type=%d", typ)) default: if typ%2 == 0 { return (typ - 12) / 2 } else { // https://github.com/sqlite/sqlite/blob/96e3c39bd58ede59150c00e4f8609cbac674ffae/tool/offsets.c#L216 // should this be 12? return (typ - 13) / 2 } } } func newNode(pageNumber int, pgr *pager.Pager, parent *node) (n *node, err error) { page, err := pgr.Get(pageNumber) if err != nil { return nil, err } defer func() { if rerr := pgr.ReleasePage(); rerr != nil { // TODO: multi errors err = rerr } }() offset := 0 if pageNumber == 1 { // the root table (aka sqlite_schema) // Note: page 1 is an exception, since it includes the database header which // is contained in the first 100 bytes of this page. offset += 100 } // Read the tree header, which is stored in the either the first 8 bytes (leaf pages) // or 12 bytes (interior pages) of the page. typ := ToTreeType(page[offset]) if typ == TreeTypeUnknown { return nil, fmt.Errorf("unknown tree page type for page=%d: %+v", pageNumber, page[offset]) } offset += 1 // Assert that tree pages are of the same type (table vs. index) and that // parents are always of type interior. if parent != nil { if typ == TreeTypeTableInterior || typ == TreeTypeTableLeaf { if parent.typ != TreeTypeTableInterior { return nil, fmt.Errorf("invalid node type: parent=%s child=%s", parent.typ.String(), typ.String()) } } else if typ == TreeTypeIndexInterior || typ == TreeTypeIndexLeaf { if parent.typ != TreeTypeIndexInterior { return nil, fmt.Errorf("invalid node type: parent=%s child=%s", parent.typ.String(), typ.String()) } } } freeblockOffset := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 numCells := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 contentOffset := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 numFreeBytes := int(page[offset]) offset += 1 // If this is an interior page, then the header is 4 bytes longer because the next four bytes // store the pointer of the right-most node in this page. nextPageNumber := 0 if typ == TreeTypeTableInterior || typ == TreeTypeIndexInterior { nextPageNumber = int(binary.BigEndian.Uint32(page[offset : offset+4])) offset += 4 } // TODO: validate long keys (>25% of the page size) which will test overflow pages, // however this only happens on index trees. // Read all cell pointers into memory. ptrs := []int{} for i := 0; i < numCells; i++ { // read the cell pointer b := page[offset : offset+2] v := binary.BigEndian.Uint16(b) ptrs = append(ptrs, int(v)) offset += 2 } // read the cell contents records := []Record{} children := []*child{} for _, ptr := range ptrs { var numBytesPayload int var rowid int var childPageNumber int var columns []Column // Left child pointer, if interior if typ == TreeTypeTableInterior || typ == TreeTypeIndexInterior { // A 4-byte big-endian page number which is the left child pointer. childPageNumber = int(binary.BigEndian.Uint32(page[ptr : ptr+4])) ptr += 4 } // Number of data bytes, if not zerodata if typ != TreeTypeTableInterior { // A varint which is the total number of bytes of payload, including any overflow ptr += internal.PutVarint(page[ptr:], &numBytesPayload) } // Integer key itself if intkey if typ == TreeTypeTableInterior || typ == TreeTypeTableLeaf { // A varint which is the integer key. ptr += internal.PutVarint(page[ptr:], &rowid) } // Record Payload if typ != TreeTypeTableInterior { // The initial portion of the payload that does not spill to overflow pages. // TODO: support overflowing payloads. columns, err = readColumns(page[ptr : ptr+numBytesPayload]) if err != nil { return nil, err } if typ != TreeTypeTableLeaf { // Extract the rowid from the last column: idx := len(columns) - 1 var ok bool rowid, ok = columns
columnContentSize
identifier_name
node.go
(row, "|") + "]" } return fmt.Sprintf("%s [page=%d, all<=%s]", c.node.typ.String(), c.pageNumber, key) } type Record struct { rowid int columns []Column } func (r Record) String() string { var row []string for _, c := range r.columns { row = append(row, c.String()) } return fmt.Sprintf("rowid=%+v columns=[%s]", r.rowid, strings.Join(row, "|")) } func (r Record) GetColumn(idx int) Column { if idx < len(r.columns) { return r.columns[idx] } // NULL return Column{ typ: 0, } } type Column struct { typ int content []byte } func (c Column) String() string { v := c.Value() switch vt := v.(type) { case string: return vt case []byte: return "\"" + string(vt) + "\"" case float64: return fmt.Sprintf("%f", vt) case int64: return fmt.Sprintf("%d", vt) case time.Time: return vt.String() case bool: return fmt.Sprintf("%v", vt) case nil: return "nil" default: return fmt.Sprintf("<unknown column type = %s>", reflect.TypeOf(v)) } } func (c Column) AsInt() (int, bool) { // [1, 6] are the various int64 types if c.typ >= 1 && c.typ <= 6 { i64 := c.Value().(int64) return int(i64), true } return 0, false } func (c Column) Value() driver.Value
u := uint64(b[5]) | uint64(b[4])<<8 | uint64(b[3])<<16 | uint64(b[2])<<24 | uint64(b[1])<<32 | uint64(b[0])<<40 return int64(u) case 6: return int64(binary.BigEndian.Uint64(c.content)) case 7: b := binary.BigEndian.Uint64(c.content) return math.Float64frombits(b) case 8: return int64(0) case 9: return int64(1) default: if c.typ%2 == 0 { // blob return c.content } else { // string return string(c.content) } } } // https://www.sqlite.org/fileformat2.html#serialtype func columnContentSize(typ int) int { switch typ { case 0, 8, 9: return 0 case 1: return 1 case 2: return 2 case 3: return 3 case 4: return 4 case 5: return 6 case 6, 7: return 8 case 10, 11: // https://github.com/sqlite/sqlite/blob/96e3c39bd58ede59150c00e4f8609cbac674ffae/tool/offsets.c#L216 // return 0 panic(fmt.Errorf("cannot support columns of type=%d", typ)) default: if typ%2 == 0 { return (typ - 12) / 2 } else { // https://github.com/sqlite/sqlite/blob/96e3c39bd58ede59150c00e4f8609cbac674ffae/tool/offsets.c#L216 // should this be 12? return (typ - 13) / 2 } } } func newNode(pageNumber int, pgr *pager.Pager, parent *node) (n *node, err error) { page, err := pgr.Get(pageNumber) if err != nil { return nil, err } defer func() { if rerr := pgr.ReleasePage(); rerr != nil { // TODO: multi errors err = rerr } }() offset := 0 if pageNumber == 1 { // the root table (aka sqlite_schema) // Note: page 1 is an exception, since it includes the database header which // is contained in the first 100 bytes of this page. offset += 100 } // Read the tree header, which is stored in the either the first 8 bytes (leaf pages) // or 12 bytes (interior pages) of the page. typ := ToTreeType(page[offset]) if typ == TreeTypeUnknown { return nil, fmt.Errorf("unknown tree page type for page=%d: %+v", pageNumber, page[offset]) } offset += 1 // Assert that tree pages are of the same type (table vs. index) and that // parents are always of type interior. if parent != nil { if typ == TreeTypeTableInterior || typ == TreeTypeTableLeaf { if parent.typ != TreeTypeTableInterior { return nil, fmt.Errorf("invalid node type: parent=%s child=%s", parent.typ.String(), typ.String()) } } else if typ == TreeTypeIndexInterior || typ == TreeTypeIndexLeaf { if parent.typ != TreeTypeIndexInterior { return nil, fmt.Errorf("invalid node type: parent=%s child=%s", parent.typ.String(), typ.String()) } } } freeblockOffset := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 numCells := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 contentOffset := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 numFreeBytes := int(page[offset]) offset += 1 // If this is an interior page, then the header is 4 bytes longer because the next four bytes // store the pointer of the right-most node in this page. nextPageNumber := 0 if typ == TreeTypeTableInterior || typ == TreeTypeIndexInterior { nextPageNumber = int(binary.BigEndian.Uint32(page[offset : offset+4])) offset += 4 } // TODO: validate long keys (>25% of the page size) which will test overflow pages, // however this only happens on index trees. // Read all cell pointers into memory. ptrs := []int{} for i := 0; i < numCells; i++ { // read the cell pointer b := page[offset : offset+2] v := binary.BigEndian.Uint16(b) ptrs = append(ptrs, int(v)) offset += 2 } // read the cell contents records := []Record{} children := []*child{} for _, ptr := range ptrs { var numBytesPayload int var rowid int var childPageNumber int var columns []Column // Left child pointer, if interior if typ == TreeTypeTableInterior || typ == TreeTypeIndexInterior { // A 4-byte big-endian page number which is the left child pointer. childPageNumber = int(binary.BigEndian.Uint32(page[ptr : ptr+4])) ptr += 4 } // Number of data bytes, if not zerodata if typ != TreeTypeTableInterior { // A varint which is the total number of bytes of payload, including any overflow ptr += internal.PutVarint(page[ptr:], &numBytesPayload) } // Integer key itself if intkey if typ == TreeTypeTableInterior || typ == TreeTypeTableLeaf { // A varint which is the integer key. ptr += internal.PutVarint(page[ptr:], &rowid) } // Record Payload if typ != TreeTypeTableInterior { // The initial portion of the payload that does not spill to overflow pages. // TODO: support overflowing payloads. columns, err = readColumns(page[ptr : ptr+numBytesPayload]) if err != nil { return nil, err } if typ != TreeTypeTableLeaf { // Extract the rowid from the last column: idx := len(columns) - 1 var ok bool rowid, ok = columns[idx
{ // TODO: validate this works with negative integers (2's complement) switch c.typ { case 0: return nil case 1: return int64(c.content[0]) case 2: return int64(binary.BigEndian.Uint16(c.content)) case 3: // stdlib binary does not have a 24-bit option b := c.content u := uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16 return int64(u) case 4: return int64(binary.BigEndian.Uint32(c.content)) case 5: // stdlib binary does not have a 48-bit option b := c.content
identifier_body
node.go
(row, "|") + "]" } return fmt.Sprintf("%s [page=%d, all<=%s]", c.node.typ.String(), c.pageNumber, key) } type Record struct { rowid int columns []Column } func (r Record) String() string { var row []string for _, c := range r.columns { row = append(row, c.String()) } return fmt.Sprintf("rowid=%+v columns=[%s]", r.rowid, strings.Join(row, "|")) } func (r Record) GetColumn(idx int) Column { if idx < len(r.columns) { return r.columns[idx] } // NULL return Column{ typ: 0, } } type Column struct { typ int content []byte } func (c Column) String() string { v := c.Value() switch vt := v.(type) { case string: return vt case []byte: return "\"" + string(vt) + "\"" case float64: return fmt.Sprintf("%f", vt) case int64: return fmt.Sprintf("%d", vt) case time.Time: return vt.String() case bool: return fmt.Sprintf("%v", vt) case nil: return "nil" default: return fmt.Sprintf("<unknown column type = %s>", reflect.TypeOf(v)) } }
func (c Column) AsInt() (int, bool) { // [1, 6] are the various int64 types if c.typ >= 1 && c.typ <= 6 { i64 := c.Value().(int64) return int(i64), true } return 0, false } func (c Column) Value() driver.Value { // TODO: validate this works with negative integers (2's complement) switch c.typ { case 0: return nil case 1: return int64(c.content[0]) case 2: return int64(binary.BigEndian.Uint16(c.content)) case 3: // stdlib binary does not have a 24-bit option b := c.content u := uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16 return int64(u) case 4: return int64(binary.BigEndian.Uint32(c.content)) case 5: // stdlib binary does not have a 48-bit option b := c.content u := uint64(b[5]) | uint64(b[4])<<8 | uint64(b[3])<<16 | uint64(b[2])<<24 | uint64(b[1])<<32 | uint64(b[0])<<40 return int64(u) case 6: return int64(binary.BigEndian.Uint64(c.content)) case 7: b := binary.BigEndian.Uint64(c.content) return math.Float64frombits(b) case 8: return int64(0) case 9: return int64(1) default: if c.typ%2 == 0 { // blob return c.content } else { // string return string(c.content) } } } // https://www.sqlite.org/fileformat2.html#serialtype func columnContentSize(typ int) int { switch typ { case 0, 8, 9: return 0 case 1: return 1 case 2: return 2 case 3: return 3 case 4: return 4 case 5: return 6 case 6, 7: return 8 case 10, 11: // https://github.com/sqlite/sqlite/blob/96e3c39bd58ede59150c00e4f8609cbac674ffae/tool/offsets.c#L216 // return 0 panic(fmt.Errorf("cannot support columns of type=%d", typ)) default: if typ%2 == 0 { return (typ - 12) / 2 } else { // https://github.com/sqlite/sqlite/blob/96e3c39bd58ede59150c00e4f8609cbac674ffae/tool/offsets.c#L216 // should this be 12? return (typ - 13) / 2 } } } func newNode(pageNumber int, pgr *pager.Pager, parent *node) (n *node, err error) { page, err := pgr.Get(pageNumber) if err != nil { return nil, err } defer func() { if rerr := pgr.ReleasePage(); rerr != nil { // TODO: multi errors err = rerr } }() offset := 0 if pageNumber == 1 { // the root table (aka sqlite_schema) // Note: page 1 is an exception, since it includes the database header which // is contained in the first 100 bytes of this page. offset += 100 } // Read the tree header, which is stored in the either the first 8 bytes (leaf pages) // or 12 bytes (interior pages) of the page. typ := ToTreeType(page[offset]) if typ == TreeTypeUnknown { return nil, fmt.Errorf("unknown tree page type for page=%d: %+v", pageNumber, page[offset]) } offset += 1 // Assert that tree pages are of the same type (table vs. index) and that // parents are always of type interior. if parent != nil { if typ == TreeTypeTableInterior || typ == TreeTypeTableLeaf { if parent.typ != TreeTypeTableInterior { return nil, fmt.Errorf("invalid node type: parent=%s child=%s", parent.typ.String(), typ.String()) } } else if typ == TreeTypeIndexInterior || typ == TreeTypeIndexLeaf { if parent.typ != TreeTypeIndexInterior { return nil, fmt.Errorf("invalid node type: parent=%s child=%s", parent.typ.String(), typ.String()) } } } freeblockOffset := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 numCells := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 contentOffset := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 numFreeBytes := int(page[offset]) offset += 1 // If this is an interior page, then the header is 4 bytes longer because the next four bytes // store the pointer of the right-most node in this page. nextPageNumber := 0 if typ == TreeTypeTableInterior || typ == TreeTypeIndexInterior { nextPageNumber = int(binary.BigEndian.Uint32(page[offset : offset+4])) offset += 4 } // TODO: validate long keys (>25% of the page size) which will test overflow pages, // however this only happens on index trees. // Read all cell pointers into memory. ptrs := []int{} for i := 0; i < numCells; i++ { // read the cell pointer b := page[offset : offset+2] v := binary.BigEndian.Uint16(b) ptrs = append(ptrs, int(v)) offset += 2 } // read the cell contents records := []Record{} children := []*child{} for _, ptr := range ptrs { var numBytesPayload int var rowid int var childPageNumber int var columns []Column // Left child pointer, if interior if typ == TreeTypeTableInterior || typ == TreeTypeIndexInterior { // A 4-byte big-endian page number which is the left child pointer. childPageNumber = int(binary.BigEndian.Uint32(page[ptr : ptr+4])) ptr += 4 } // Number of data bytes, if not zerodata if typ != TreeTypeTableInterior { // A varint which is the total number of bytes of payload, including any overflow ptr += internal.PutVarint(page[ptr:], &numBytesPayload) } // Integer key itself if intkey if typ == TreeTypeTableInterior || typ == TreeTypeTableLeaf { // A varint which is the integer key. ptr += internal.PutVarint(page[ptr:], &rowid) } // Record Payload if typ != TreeTypeTableInterior { // The initial portion of the payload that does not spill to overflow pages. // TODO: support overflowing payloads. columns, err = readColumns(page[ptr : ptr+numBytesPayload]) if err != nil { return nil, err } if typ != TreeTypeTableLeaf { // Extract the rowid from the last column: idx := len(columns) - 1 var ok bool rowid, ok = columns[idx
random_line_split
node.go
(row, "|") + "]" } return fmt.Sprintf("%s [page=%d, all<=%s]", c.node.typ.String(), c.pageNumber, key) } type Record struct { rowid int columns []Column } func (r Record) String() string { var row []string for _, c := range r.columns { row = append(row, c.String()) } return fmt.Sprintf("rowid=%+v columns=[%s]", r.rowid, strings.Join(row, "|")) } func (r Record) GetColumn(idx int) Column { if idx < len(r.columns) { return r.columns[idx] } // NULL return Column{ typ: 0, } } type Column struct { typ int content []byte } func (c Column) String() string { v := c.Value() switch vt := v.(type) { case string: return vt case []byte: return "\"" + string(vt) + "\"" case float64: return fmt.Sprintf("%f", vt) case int64: return fmt.Sprintf("%d", vt) case time.Time: return vt.String() case bool: return fmt.Sprintf("%v", vt) case nil: return "nil" default: return fmt.Sprintf("<unknown column type = %s>", reflect.TypeOf(v)) } } func (c Column) AsInt() (int, bool) { // [1, 6] are the various int64 types if c.typ >= 1 && c.typ <= 6 { i64 := c.Value().(int64) return int(i64), true } return 0, false } func (c Column) Value() driver.Value { // TODO: validate this works with negative integers (2's complement) switch c.typ { case 0: return nil case 1: return int64(c.content[0]) case 2: return int64(binary.BigEndian.Uint16(c.content)) case 3: // stdlib binary does not have a 24-bit option b := c.content u := uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16 return int64(u) case 4: return int64(binary.BigEndian.Uint32(c.content)) case 5: // stdlib binary does not have a 48-bit option b := c.content u := uint64(b[5]) | uint64(b[4])<<8 | uint64(b[3])<<16 | uint64(b[2])<<24 | uint64(b[1])<<32 | uint64(b[0])<<40 return int64(u) case 6: return int64(binary.BigEndian.Uint64(c.content)) case 7: b := binary.BigEndian.Uint64(c.content) return math.Float64frombits(b) case 8: return int64(0) case 9: return int64(1) default: if c.typ%2 == 0 { // blob return c.content } else { // string return string(c.content) } } } // https://www.sqlite.org/fileformat2.html#serialtype func columnContentSize(typ int) int { switch typ { case 0, 8, 9: return 0 case 1: return 1 case 2: return 2 case 3: return 3 case 4: return 4 case 5: return 6 case 6, 7: return 8 case 10, 11: // https://github.com/sqlite/sqlite/blob/96e3c39bd58ede59150c00e4f8609cbac674ffae/tool/offsets.c#L216 // return 0 panic(fmt.Errorf("cannot support columns of type=%d", typ)) default: if typ%2 == 0 { return (typ - 12) / 2 } else { // https://github.com/sqlite/sqlite/blob/96e3c39bd58ede59150c00e4f8609cbac674ffae/tool/offsets.c#L216 // should this be 12? return (typ - 13) / 2 } } } func newNode(pageNumber int, pgr *pager.Pager, parent *node) (n *node, err error) { page, err := pgr.Get(pageNumber) if err != nil { return nil, err } defer func() { if rerr := pgr.ReleasePage(); rerr != nil { // TODO: multi errors err = rerr } }() offset := 0 if pageNumber == 1 { // the root table (aka sqlite_schema) // Note: page 1 is an exception, since it includes the database header which // is contained in the first 100 bytes of this page. offset += 100 } // Read the tree header, which is stored in the either the first 8 bytes (leaf pages) // or 12 bytes (interior pages) of the page. typ := ToTreeType(page[offset]) if typ == TreeTypeUnknown { return nil, fmt.Errorf("unknown tree page type for page=%d: %+v", pageNumber, page[offset]) } offset += 1 // Assert that tree pages are of the same type (table vs. index) and that // parents are always of type interior. if parent != nil { if typ == TreeTypeTableInterior || typ == TreeTypeTableLeaf { if parent.typ != TreeTypeTableInterior { return nil, fmt.Errorf("invalid node type: parent=%s child=%s", parent.typ.String(), typ.String()) } } else if typ == TreeTypeIndexInterior || typ == TreeTypeIndexLeaf { if parent.typ != TreeTypeIndexInterior { return nil, fmt.Errorf("invalid node type: parent=%s child=%s", parent.typ.String(), typ.String()) } } } freeblockOffset := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 numCells := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 contentOffset := int(binary.BigEndian.Uint16(page[offset : offset+2])) offset += 2 numFreeBytes := int(page[offset]) offset += 1 // If this is an interior page, then the header is 4 bytes longer because the next four bytes // store the pointer of the right-most node in this page. nextPageNumber := 0 if typ == TreeTypeTableInterior || typ == TreeTypeIndexInterior { nextPageNumber = int(binary.BigEndian.Uint32(page[offset : offset+4])) offset += 4 } // TODO: validate long keys (>25% of the page size) which will test overflow pages, // however this only happens on index trees. // Read all cell pointers into memory. ptrs := []int{} for i := 0; i < numCells; i++ { // read the cell pointer b := page[offset : offset+2] v := binary.BigEndian.Uint16(b) ptrs = append(ptrs, int(v)) offset += 2 } // read the cell contents records := []Record{} children := []*child{} for _, ptr := range ptrs { var numBytesPayload int var rowid int var childPageNumber int var columns []Column // Left child pointer, if interior if typ == TreeTypeTableInterior || typ == TreeTypeIndexInterior { // A 4-byte big-endian page number which is the left child pointer. childPageNumber = int(binary.BigEndian.Uint32(page[ptr : ptr+4])) ptr += 4 } // Number of data bytes, if not zerodata if typ != TreeTypeTableInterior { // A varint which is the total number of bytes of payload, including any overflow ptr += internal.PutVarint(page[ptr:], &numBytesPayload) } // Integer key itself if intkey if typ == TreeTypeTableInterior || typ == TreeTypeTableLeaf { // A varint which is the integer key. ptr += internal.PutVarint(page[ptr:], &rowid) } // Record Payload if typ != TreeTypeTableInterior { // The initial portion of the payload that does not spill to overflow pages. // TODO: support overflowing payloads. columns, err = readColumns(page[ptr : ptr+numBytesPayload]) if err != nil
if typ != TreeTypeTableLeaf { // Extract the rowid from the last column: idx := len(columns) - 1 var ok bool rowid, ok =
{ return nil, err }
conditional_block
terminal.rs
fn name(&self) -> &str; /// Acquires a lock on terminal read operations and returns a value holding /// that lock and granting access to such operations. /// /// The lock must not be released until the returned value is dropped. fn lock_read<'a>(&'a self) -> Box<dyn TerminalReader<Self> + 'a>; /// Acquires a lock on terminal write operations and returns a value holding /// that lock and granting access to such operations. /// /// The lock must not be released until the returned value is dropped. fn lock_write<'a>(&'a self) -> Box<dyn TerminalWriter<Self> + 'a>; } /// Holds a lock on `Terminal` read operations pub trait TerminalReader<Term: Terminal> { /// Prepares the terminal for line reading and editing operations. /// /// If `block_signals` is `true`, the terminal will be configured to treat /// special characters that would otherwise be interpreted as signals as /// their literal value. /// /// If `block_signals` is `false`, a signal contained in the `report_signals` /// set may be returned. /// /// # Notes /// /// This method may be called more than once. However, if the state values /// are not restored in reverse order in which they were created, /// the state of the underlying terminal device becomes undefined. fn prepare(&mut self, block_signals: bool, report_signals: SignalSet) -> io::Result<Term::PrepareState>; /// Like `prepare`, but called when the write lock is already held. /// /// # Safety /// /// This method must be called with a `TerminalWriter` instance returned /// by the same `Terminal` instance to which this `TerminalReader` belongs. unsafe fn prepare_with_lock(&mut self, lock: &mut dyn TerminalWriter<Term>, block_signals: bool, report_signals: SignalSet) -> io::Result<Term::PrepareState>; /// Restores the terminal state using the given state data. fn restore(&mut self, state: Term::PrepareState) -> io::Result<()>; /// Like `restore`, but called when the write lock is already held. /// /// # Safety /// /// This method must be called with a `TerminalWriter` instance returned /// by the same `Terminal` instance to which this `TerminalReader` belongs. unsafe fn restore_with_lock(&mut self, lock: &mut dyn TerminalWriter<Term>, state: Term::PrepareState) -> io::Result<()>; /// Reads some input from the terminal and appends it to the given buffer. fn read(&mut self, buf: &mut Vec<u8>) -> io::Result<RawRead>; /// Waits `timeout` for user input. If `timeout` is `None`, waits indefinitely. /// /// Returns `Ok(true)` if input becomes available within the given timeout /// or if a signal is received. /// /// Returns `Ok(false)` if the timeout expires before input becomes available. fn wait_for_input(&mut self, timeout: Option<Duration>) -> io::Result<bool>; } /// Holds a lock on `Terminal` write operations pub trait TerminalWriter<Term: Terminal> { /// Returns the size of the terminal window fn size(&self) -> io::Result<Size>; /// Presents a clear terminal screen, with cursor at first row, first column. /// /// If the terminal possesses a scrolling window over a buffer, this shall /// have the effect of moving the visible window down such that it shows /// an empty view of the buffer, preserving some or all of existing buffer /// contents, where possible. fn clear_screen(&mut self) -> io::Result<()>; /// Clears characters on the line occupied by the cursor, beginning with the /// cursor and ending at the end of the line. Also clears all characters on /// all lines after the cursor. fn clear_to_screen_end(&mut self) -> io::Result<()>; /// Moves the cursor up `n` cells; `n` may be zero. fn move_up(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor down `n` cells; `n` may be zero. fn move_down(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor left `n` cells; `n` may be zero. fn move_left(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor right `n` cells; `n` may be zero. fn move_right(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor to the first column of the current line fn move_to_first_column(&mut self) -> io::Result<()>; /// Set the current cursor mode fn set_cursor_mode(&mut self, mode: CursorMode) -> io::Result<()>; /// Writes output to the terminal. /// /// For each carriage return `'\r'` written to the terminal, the cursor /// should be moved to the first column of the current line. /// /// For each newline `'\n'` written to the terminal, the cursor should /// be moved to the first column of the following line. /// /// The terminal interface shall not automatically move the cursor to the next /// line when `write` causes a character to be written to the final column. fn write(&mut self, s: &str) -> io::Result<()>; /// Flushes any currently buffered output data. /// /// `TerminalWriter` instances may not buffer data on all systems. /// /// Data must be flushed when the `TerminalWriter` instance is dropped. fn flush(&mut self) -> io::Result<()>; } impl DefaultTerminal { /// Opens access to the terminal device associated with standard output. pub fn new() -> io::Result<DefaultTerminal> { mortal::Terminal::new().map(DefaultTerminal) } /// Opens access to the terminal device associated with standard error. pub fn stderr() -> io::Result<DefaultTerminal> { mortal::Terminal::stderr().map(DefaultTerminal) } unsafe fn cast_writer<'a>(writer: &'a mut dyn TerminalWriter<Self>) -> &'a mut TerminalWriteGuard<'a> { &mut *(writer as *mut _ as *mut TerminalWriteGuard) } } impl Terminal for DefaultTerminal { type PrepareState = PrepareState; fn name(&self) -> &str { self.0.name() } fn lock_read<'a>(&'a self) -> Box<dyn TerminalReader<Self> + 'a> { Box::new(self.0.lock_read().unwrap()) } fn lock_write<'a>(&'a self) -> Box<dyn TerminalWriter<Self> + 'a> { Box::new(self.0.lock_write().unwrap()) } } impl<'a> TerminalReader<DefaultTerminal> for TerminalReadGuard<'a> { fn prepare(&mut self, block_signals: bool, report_signals: SignalSet) -> io::Result<PrepareState> { self.prepare(PrepareConfig{ block_signals, enable_control_flow: !block_signals, enable_keypad: false, report_signals, .. PrepareConfig::default() }) } unsafe fn prepare_with_lock(&mut self, lock: &mut dyn TerminalWriter<DefaultTerminal>, block_signals: bool, report_signals: SignalSet) -> io::Result<PrepareState> { let lock = DefaultTerminal::cast_writer(lock); self.prepare_with_lock(lock, PrepareConfig{ block_signals, enable_control_flow: !block_signals, enable_keypad: false, report_signals, .. PrepareConfig::default() }) } fn restore(&mut self, state: PrepareState) -> io::Result<()> { self.restore(state) } unsafe fn restore_with_lock(&mut self, lock: &mut dyn TerminalWriter<DefaultTerminal>, state: PrepareState) -> io::Result<()> { let lock = DefaultTerminal::cast_writer(lock); self.restore_with_lock(lock, state) } fn read(&mut self, buf: &mut Vec<u8>) -> io::Result<RawRead> { sys::terminal_read(self, buf) } fn wait_for_input(&mut self, timeout: Option<Duration>) -> io::Result<bool> { self.wait_event(timeout) } } impl<'a> TerminalWriter<DefaultTerminal> for TerminalWriteGuard<'a> { fn size(&self) -> io::Result<Size> { self.size() } fn clear_screen(&mut self) -> io::Result<()> { self.clear_screen() } fn clear_to_screen_end(&mut self) -> io::Result<()> { self.clear_to_screen_end() } fn move_up(&mut self, n: usize) -> io::Result<()> { self.move_up(n) } fn move_down(&mut self, n: usize) -> io::Result<()> { self.move_down(n) } fn move_left(&mut self, n: usize) -> io::Result<()> { self.move_left(n) } fn move_right(&mut self, n: usize) -> io::Result<()> { self.move_right(n) } fn
move_to_first_column
identifier_name
terminal.rs
. type PrepareState; /* /// Holds an exclusive read lock and provides read operations type Reader: TerminalReader; /// Holds an exclusive write lock and provides write operations type Writer: TerminalWriter; */ /// Returns the name of the terminal. fn name(&self) -> &str; /// Acquires a lock on terminal read operations and returns a value holding /// that lock and granting access to such operations. /// /// The lock must not be released until the returned value is dropped. fn lock_read<'a>(&'a self) -> Box<dyn TerminalReader<Self> + 'a>; /// Acquires a lock on terminal write operations and returns a value holding /// that lock and granting access to such operations. /// /// The lock must not be released until the returned value is dropped. fn lock_write<'a>(&'a self) -> Box<dyn TerminalWriter<Self> + 'a>; } /// Holds a lock on `Terminal` read operations pub trait TerminalReader<Term: Terminal> { /// Prepares the terminal for line reading and editing operations. /// /// If `block_signals` is `true`, the terminal will be configured to treat /// special characters that would otherwise be interpreted as signals as /// their literal value. /// /// If `block_signals` is `false`, a signal contained in the `report_signals` /// set may be returned. /// /// # Notes /// /// This method may be called more than once. However, if the state values /// are not restored in reverse order in which they were created, /// the state of the underlying terminal device becomes undefined. fn prepare(&mut self, block_signals: bool, report_signals: SignalSet) -> io::Result<Term::PrepareState>; /// Like `prepare`, but called when the write lock is already held. /// /// # Safety /// /// This method must be called with a `TerminalWriter` instance returned /// by the same `Terminal` instance to which this `TerminalReader` belongs. unsafe fn prepare_with_lock(&mut self, lock: &mut dyn TerminalWriter<Term>, block_signals: bool, report_signals: SignalSet) -> io::Result<Term::PrepareState>; /// Restores the terminal state using the given state data. fn restore(&mut self, state: Term::PrepareState) -> io::Result<()>; /// Like `restore`, but called when the write lock is already held. /// /// # Safety /// /// This method must be called with a `TerminalWriter` instance returned /// by the same `Terminal` instance to which this `TerminalReader` belongs. unsafe fn restore_with_lock(&mut self, lock: &mut dyn TerminalWriter<Term>, state: Term::PrepareState) -> io::Result<()>; /// Reads some input from the terminal and appends it to the given buffer. fn read(&mut self, buf: &mut Vec<u8>) -> io::Result<RawRead>; /// Waits `timeout` for user input. If `timeout` is `None`, waits indefinitely. /// /// Returns `Ok(true)` if input becomes available within the given timeout /// or if a signal is received. /// /// Returns `Ok(false)` if the timeout expires before input becomes available. fn wait_for_input(&mut self, timeout: Option<Duration>) -> io::Result<bool>; } /// Holds a lock on `Terminal` write operations pub trait TerminalWriter<Term: Terminal> { /// Returns the size of the terminal window fn size(&self) -> io::Result<Size>; /// Presents a clear terminal screen, with cursor at first row, first column. /// /// If the terminal possesses a scrolling window over a buffer, this shall /// have the effect of moving the visible window down such that it shows /// an empty view of the buffer, preserving some or all of existing buffer /// contents, where possible. fn clear_screen(&mut self) -> io::Result<()>; /// Clears characters on the line occupied by the cursor, beginning with the /// cursor and ending at the end of the line. Also clears all characters on /// all lines after the cursor. fn clear_to_screen_end(&mut self) -> io::Result<()>; /// Moves the cursor up `n` cells; `n` may be zero. fn move_up(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor down `n` cells; `n` may be zero. fn move_down(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor left `n` cells; `n` may be zero. fn move_left(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor right `n` cells; `n` may be zero. fn move_right(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor to the first column of the current line fn move_to_first_column(&mut self) -> io::Result<()>; /// Set the current cursor mode fn set_cursor_mode(&mut self, mode: CursorMode) -> io::Result<()>; /// Writes output to the terminal. /// /// For each carriage return `'\r'` written to the terminal, the cursor /// should be moved to the first column of the current line. /// /// For each newline `'\n'` written to the terminal, the cursor should /// be moved to the first column of the following line. /// /// The terminal interface shall not automatically move the cursor to the next /// line when `write` causes a character to be written to the final column. fn write(&mut self, s: &str) -> io::Result<()>; /// Flushes any currently buffered output data. /// /// `TerminalWriter` instances may not buffer data on all systems. /// /// Data must be flushed when the `TerminalWriter` instance is dropped. fn flush(&mut self) -> io::Result<()>; } impl DefaultTerminal { /// Opens access to the terminal device associated with standard output. pub fn new() -> io::Result<DefaultTerminal> { mortal::Terminal::new().map(DefaultTerminal) } /// Opens access to the terminal device associated with standard error. pub fn stderr() -> io::Result<DefaultTerminal> { mortal::Terminal::stderr().map(DefaultTerminal) } unsafe fn cast_writer<'a>(writer: &'a mut dyn TerminalWriter<Self>) -> &'a mut TerminalWriteGuard<'a> { &mut *(writer as *mut _ as *mut TerminalWriteGuard) } } impl Terminal for DefaultTerminal { type PrepareState = PrepareState; fn name(&self) -> &str { self.0.name() } fn lock_read<'a>(&'a self) -> Box<dyn TerminalReader<Self> + 'a> { Box::new(self.0.lock_read().unwrap()) } fn lock_write<'a>(&'a self) -> Box<dyn TerminalWriter<Self> + 'a> { Box::new(self.0.lock_write().unwrap()) } } impl<'a> TerminalReader<DefaultTerminal> for TerminalReadGuard<'a> { fn prepare(&mut self, block_signals: bool, report_signals: SignalSet) -> io::Result<PrepareState> { self.prepare(PrepareConfig{ block_signals, enable_control_flow: !block_signals, enable_keypad: false, report_signals, .. PrepareConfig::default() }) } unsafe fn prepare_with_lock(&mut self, lock: &mut dyn TerminalWriter<DefaultTerminal>, block_signals: bool, report_signals: SignalSet) -> io::Result<PrepareState> { let lock = DefaultTerminal::cast_writer(lock); self.prepare_with_lock(lock, PrepareConfig{ block_signals, enable_control_flow: !block_signals, enable_keypad: false, report_signals, .. PrepareConfig::default() }) } fn restore(&mut self, state: PrepareState) -> io::Result<()> { self.restore(state) } unsafe fn restore_with_lock(&mut self, lock: &mut dyn TerminalWriter<DefaultTerminal>, state: PrepareState) -> io::Result<()> { let lock = DefaultTerminal::cast_writer(lock); self.restore_with_lock(lock, state) } fn read(&mut self, buf: &mut Vec<u8>) -> io::Result<RawRead> { sys::terminal_read(self, buf) } fn wait_for_input(&mut self, timeout: Option<Duration>) -> io::Result<bool> { self.wait_event(timeout) } } impl<'a> TerminalWriter<DefaultTerminal> for TerminalWriteGuard<'a> { fn size(&self) -> io::Result<Size> { self.size() } fn clear_screen(&mut self) -> io::Result<()> { self.clear_screen() } fn clear_to_screen_end(&mut self) -> io::Result<()> { self.clear_to_screen_end() } fn move_up(&mut self, n: usize) -> io::Result<()> { self.move_up(n) } fn move_down(&mut self, n: usize) -> io::Result<()>
{ self.move_down(n) }
identifier_body
terminal.rs
(usize), /// The terminal window was resized Resize(Size), /// A signal was received while waiting for input Signal(Signal), } /// Defines a low-level interface to the terminal pub trait Terminal: Sized + Send + Sync { // TODO: When generic associated types are implemented (and stabilized), // boxed trait objects may be replaced by `Reader` and `Writer`. /// Returned by `prepare`; passed to `restore` to restore state. type PrepareState; /* /// Holds an exclusive read lock and provides read operations type Reader: TerminalReader; /// Holds an exclusive write lock and provides write operations type Writer: TerminalWriter; */ /// Returns the name of the terminal. fn name(&self) -> &str; /// Acquires a lock on terminal read operations and returns a value holding /// that lock and granting access to such operations. /// /// The lock must not be released until the returned value is dropped. fn lock_read<'a>(&'a self) -> Box<dyn TerminalReader<Self> + 'a>; /// Acquires a lock on terminal write operations and returns a value holding /// that lock and granting access to such operations. /// /// The lock must not be released until the returned value is dropped. fn lock_write<'a>(&'a self) -> Box<dyn TerminalWriter<Self> + 'a>; } /// Holds a lock on `Terminal` read operations pub trait TerminalReader<Term: Terminal> { /// Prepares the terminal for line reading and editing operations. /// /// If `block_signals` is `true`, the terminal will be configured to treat /// special characters that would otherwise be interpreted as signals as /// their literal value. /// /// If `block_signals` is `false`, a signal contained in the `report_signals` /// set may be returned. /// /// # Notes /// /// This method may be called more than once. However, if the state values /// are not restored in reverse order in which they were created, /// the state of the underlying terminal device becomes undefined. fn prepare(&mut self, block_signals: bool, report_signals: SignalSet) -> io::Result<Term::PrepareState>; /// Like `prepare`, but called when the write lock is already held. /// /// # Safety /// /// This method must be called with a `TerminalWriter` instance returned /// by the same `Terminal` instance to which this `TerminalReader` belongs. unsafe fn prepare_with_lock(&mut self, lock: &mut dyn TerminalWriter<Term>, block_signals: bool, report_signals: SignalSet) -> io::Result<Term::PrepareState>; /// Restores the terminal state using the given state data. fn restore(&mut self, state: Term::PrepareState) -> io::Result<()>; /// Like `restore`, but called when the write lock is already held. /// /// # Safety /// /// This method must be called with a `TerminalWriter` instance returned /// by the same `Terminal` instance to which this `TerminalReader` belongs. unsafe fn restore_with_lock(&mut self, lock: &mut dyn TerminalWriter<Term>, state: Term::PrepareState) -> io::Result<()>; /// Reads some input from the terminal and appends it to the given buffer. fn read(&mut self, buf: &mut Vec<u8>) -> io::Result<RawRead>; /// Waits `timeout` for user input. If `timeout` is `None`, waits indefinitely. /// /// Returns `Ok(true)` if input becomes available within the given timeout /// or if a signal is received. /// /// Returns `Ok(false)` if the timeout expires before input becomes available. fn wait_for_input(&mut self, timeout: Option<Duration>) -> io::Result<bool>; } /// Holds a lock on `Terminal` write operations pub trait TerminalWriter<Term: Terminal> { /// Returns the size of the terminal window fn size(&self) -> io::Result<Size>; /// Presents a clear terminal screen, with cursor at first row, first column. /// /// If the terminal possesses a scrolling window over a buffer, this shall /// have the effect of moving the visible window down such that it shows /// an empty view of the buffer, preserving some or all of existing buffer /// contents, where possible. fn clear_screen(&mut self) -> io::Result<()>; /// Clears characters on the line occupied by the cursor, beginning with the /// cursor and ending at the end of the line. Also clears all characters on /// all lines after the cursor. fn clear_to_screen_end(&mut self) -> io::Result<()>; /// Moves the cursor up `n` cells; `n` may be zero. fn move_up(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor down `n` cells; `n` may be zero. fn move_down(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor left `n` cells; `n` may be zero. fn move_left(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor right `n` cells; `n` may be zero. fn move_right(&mut self, n: usize) -> io::Result<()>; /// Moves the cursor to the first column of the current line fn move_to_first_column(&mut self) -> io::Result<()>; /// Set the current cursor mode fn set_cursor_mode(&mut self, mode: CursorMode) -> io::Result<()>; /// Writes output to the terminal. /// /// For each carriage return `'\r'` written to the terminal, the cursor /// should be moved to the first column of the current line. /// /// For each newline `'\n'` written to the terminal, the cursor should /// be moved to the first column of the following line.
/// Flushes any currently buffered output data. /// /// `TerminalWriter` instances may not buffer data on all systems. /// /// Data must be flushed when the `TerminalWriter` instance is dropped. fn flush(&mut self) -> io::Result<()>; } impl DefaultTerminal { /// Opens access to the terminal device associated with standard output. pub fn new() -> io::Result<DefaultTerminal> { mortal::Terminal::new().map(DefaultTerminal) } /// Opens access to the terminal device associated with standard error. pub fn stderr() -> io::Result<DefaultTerminal> { mortal::Terminal::stderr().map(DefaultTerminal) } unsafe fn cast_writer<'a>(writer: &'a mut dyn TerminalWriter<Self>) -> &'a mut TerminalWriteGuard<'a> { &mut *(writer as *mut _ as *mut TerminalWriteGuard) } } impl Terminal for DefaultTerminal { type PrepareState = PrepareState; fn name(&self) -> &str { self.0.name() } fn lock_read<'a>(&'a self) -> Box<dyn TerminalReader<Self> + 'a> { Box::new(self.0.lock_read().unwrap()) } fn lock_write<'a>(&'a self) -> Box<dyn TerminalWriter<Self> + 'a> { Box::new(self.0.lock_write().unwrap()) } } impl<'a> TerminalReader<DefaultTerminal> for TerminalReadGuard<'a> { fn prepare(&mut self, block_signals: bool, report_signals: SignalSet) -> io::Result<PrepareState> { self.prepare(PrepareConfig{ block_signals, enable_control_flow: !block_signals, enable_keypad: false, report_signals, .. PrepareConfig::default() }) } unsafe fn prepare_with_lock(&mut self, lock: &mut dyn TerminalWriter<DefaultTerminal>, block_signals: bool, report_signals: SignalSet) -> io::Result<PrepareState> { let lock = DefaultTerminal::cast_writer(lock); self.prepare_with_lock(lock, PrepareConfig{ block_signals, enable_control_flow: !block_signals, enable_keypad: false, report_signals, .. PrepareConfig::default() }) } fn restore(&mut self, state: PrepareState) -> io::Result<()> { self.restore(state) } unsafe fn restore_with_lock(&mut self, lock: &mut dyn TerminalWriter<DefaultTerminal>, state: PrepareState) -> io::Result<()> { let lock = DefaultTerminal::cast_writer(lock); self.restore_with_lock(lock, state) } fn read(&mut self, buf: &mut Vec<u8>) -> io::Result<RawRead> { sys::terminal_read(self, buf) } fn wait_for_input(&mut self, timeout: Option<Duration>) -> io::Result<bool> { self.wait_event(timeout) } } impl<'a> TerminalWriter<DefaultTerminal> for TerminalWriteGuard<'a> { fn size(&self) -> io::Result<Size> { self.size() } fn
/// /// The terminal interface shall not automatically move the cursor to the next /// line when `write` causes a character to be written to the final column. fn write(&mut self, s: &str) -> io::Result<()>;
random_line_split
passThrough.go
doing the I/O // operation and performs this one on behalf of it. // // Currently, this handler serves non-emulated resources within the /proc/sys // subtree, but there's nothing specific to this path in this handler's // implementation (see that the Path attribute is set to "*"), so this one could // be utilized for pass-through operations in other subtrees too. // type PassThrough struct { domain.HandlerBase } var PassThrough_Handler = &PassThrough{ domain.HandlerBase{ Name: "PassThrough", Path: "*", Enabled: true, }, } func (h *PassThrough) Lookup( n domain.IOnodeIface, req *domain.HandlerRequest) (os.FileInfo, error) { logrus.Debugf("Executing Lookup() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.LookupRequest, Payload: &domain.LookupPayload{ Entry: n.Path(), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return nil, err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return nil, responseMsg.Payload.(error) } info := responseMsg.Payload.(domain.FileInfo) return info, nil } func (h *PassThrough) Open( n domain.IOnodeIface, req *domain.HandlerRequest) error { logrus.Debugf("Executing Open() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.OpenFileRequest, Payload: &domain.OpenFilePayload{ File: n.Path(), Flags: strconv.Itoa(n.OpenFlags()), Mode: strconv.Itoa(int(n.OpenMode())), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return responseMsg.Payload.(error) } return nil } func (h *PassThrough) Read( n domain.IOnodeIface, req *domain.HandlerRequest) (int, error) { var resource = n.Name() logrus.Debugf("Executing Read() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, resource) if req.Offset > 0 { return 0, io.EOF } var ( data string ok bool err error ) path := n.Path() prs := h.Service.ProcessService() process := prs.ProcessCreate(req.Pid, req.Uid, req.Gid) cntr := req.Container // // Caching here improves performance by avoiding dispatching the nsenter agent. But // note that caching is only helping processes at the sys container level, not in inner // containers or unshared namespaces. To enable caching for those, we would need to // have a cache per each namespace and this is expensive; plus we would also need to // know when the namespace ceases to exist in order to destroy the cache associated // with it. // if domain.ProcessNsMatch(process, cntr.InitProc()) { // If this resource is cached, return it's data; otherwise fetch its data from the // host FS and store it in the cache. cntr.Lock() data, ok = cntr.Data(path, resource) if !ok { data, err = h.fetchFile(n, process) if err != nil
cntr.SetData(path, resource, data) } cntr.Unlock() } else { data, err = h.fetchFile(n, process) if err != nil { return 0, err } } data += "\n" return copyResultBuffer(req.Data, []byte(data)) } func (h *PassThrough) Write( n domain.IOnodeIface, req *domain.HandlerRequest) (int, error) { var resource = n.Name() logrus.Debugf("Executing Write() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, resource) path := n.Path() cntr := req.Container newContent := strings.TrimSpace(string(req.Data)) prs := h.Service.ProcessService() process := prs.ProcessCreate(req.Pid, req.Uid, req.Gid) // If write op is originated by a process within a registered sys-container // (it fully matches its namespaces) then store the data in the cache and do // a write-through to the host FS. Otherwise just do the write-through. if domain.ProcessNsMatch(process, cntr.InitProc()) { cntr.Lock() if err := h.pushFile(n, process, newContent); err != nil { cntr.Unlock() return 0, err } cntr.SetData(path, resource, newContent) cntr.Unlock() } else { if err := h.pushFile(n, process, newContent); err != nil { return 0, err } } return len(req.Data), nil } func (h *PassThrough) ReadDirAll( n domain.IOnodeIface, req *domain.HandlerRequest) ([]os.FileInfo, error) { logrus.Debugf("Executing ReadDirAll() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.ReadDirRequest, Payload: &domain.ReadDirPayload{ Dir: n.Path(), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return nil, err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return nil, responseMsg.Payload.(error) } var osFileEntries = make([]os.FileInfo, 0) // Transform event-response payload into a FileInfo slice. Notice that to // convert []T1 struct to a []T2 one, we must iterate through each element // and do the conversion one element at a time. dirEntries := responseMsg.Payload.([]domain.FileInfo) for _, v := range dirEntries { osFileEntries = append(osFileEntries, v) } return osFileEntries, nil } func (h *PassThrough) Setattr( n domain.IOnodeIface, req *domain.HandlerRequest) error { logrus.Debugf("Executing Setattr() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.OpenFileRequest, Payload: &domain.OpenFilePayload{ File: n.Path(), Flags: strconv.Itoa(n.OpenFlags()), Mode: strconv.Itoa(int(n.OpenMode())), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return responseMsg.Payload.(error) } return nil } // Auxiliary method to fetch the content of any given file within a container. func (h *PassThrough) fetchFile( n domain.IOnodeIface, process domain.ProcessIface) (string, error) { // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( process.Pid(), &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.ReadFileRequest, Payload: &domain.ReadFilePayload{ File: n.Path(), }, }, nil, false, ) // Launch nsenter-event to obtain file state within container // namespaces. err := nss.SendRequestEvent(event) if
{ cntr.Unlock() return 0, err }
conditional_block
passThrough.go
doing the I/O // operation and performs this one on behalf of it. // // Currently, this handler serves non-emulated resources within the /proc/sys // subtree, but there's nothing specific to this path in this handler's // implementation (see that the Path attribute is set to "*"), so this one could // be utilized for pass-through operations in other subtrees too. // type PassThrough struct { domain.HandlerBase } var PassThrough_Handler = &PassThrough{ domain.HandlerBase{ Name: "PassThrough", Path: "*", Enabled: true, }, } func (h *PassThrough) Lookup( n domain.IOnodeIface, req *domain.HandlerRequest) (os.FileInfo, error) { logrus.Debugf("Executing Lookup() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.LookupRequest, Payload: &domain.LookupPayload{ Entry: n.Path(), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return nil, err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return nil, responseMsg.Payload.(error) } info := responseMsg.Payload.(domain.FileInfo) return info, nil } func (h *PassThrough) Open( n domain.IOnodeIface, req *domain.HandlerRequest) error { logrus.Debugf("Executing Open() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.OpenFileRequest, Payload: &domain.OpenFilePayload{ File: n.Path(), Flags: strconv.Itoa(n.OpenFlags()), Mode: strconv.Itoa(int(n.OpenMode())), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return responseMsg.Payload.(error) } return nil } func (h *PassThrough) Read( n domain.IOnodeIface, req *domain.HandlerRequest) (int, error) { var resource = n.Name() logrus.Debugf("Executing Read() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, resource)
return 0, io.EOF } var ( data string ok bool err error ) path := n.Path() prs := h.Service.ProcessService() process := prs.ProcessCreate(req.Pid, req.Uid, req.Gid) cntr := req.Container // // Caching here improves performance by avoiding dispatching the nsenter agent. But // note that caching is only helping processes at the sys container level, not in inner // containers or unshared namespaces. To enable caching for those, we would need to // have a cache per each namespace and this is expensive; plus we would also need to // know when the namespace ceases to exist in order to destroy the cache associated // with it. // if domain.ProcessNsMatch(process, cntr.InitProc()) { // If this resource is cached, return it's data; otherwise fetch its data from the // host FS and store it in the cache. cntr.Lock() data, ok = cntr.Data(path, resource) if !ok { data, err = h.fetchFile(n, process) if err != nil { cntr.Unlock() return 0, err } cntr.SetData(path, resource, data) } cntr.Unlock() } else { data, err = h.fetchFile(n, process) if err != nil { return 0, err } } data += "\n" return copyResultBuffer(req.Data, []byte(data)) } func (h *PassThrough) Write( n domain.IOnodeIface, req *domain.HandlerRequest) (int, error) { var resource = n.Name() logrus.Debugf("Executing Write() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, resource) path := n.Path() cntr := req.Container newContent := strings.TrimSpace(string(req.Data)) prs := h.Service.ProcessService() process := prs.ProcessCreate(req.Pid, req.Uid, req.Gid) // If write op is originated by a process within a registered sys-container // (it fully matches its namespaces) then store the data in the cache and do // a write-through to the host FS. Otherwise just do the write-through. if domain.ProcessNsMatch(process, cntr.InitProc()) { cntr.Lock() if err := h.pushFile(n, process, newContent); err != nil { cntr.Unlock() return 0, err } cntr.SetData(path, resource, newContent) cntr.Unlock() } else { if err := h.pushFile(n, process, newContent); err != nil { return 0, err } } return len(req.Data), nil } func (h *PassThrough) ReadDirAll( n domain.IOnodeIface, req *domain.HandlerRequest) ([]os.FileInfo, error) { logrus.Debugf("Executing ReadDirAll() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.ReadDirRequest, Payload: &domain.ReadDirPayload{ Dir: n.Path(), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return nil, err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return nil, responseMsg.Payload.(error) } var osFileEntries = make([]os.FileInfo, 0) // Transform event-response payload into a FileInfo slice. Notice that to // convert []T1 struct to a []T2 one, we must iterate through each element // and do the conversion one element at a time. dirEntries := responseMsg.Payload.([]domain.FileInfo) for _, v := range dirEntries { osFileEntries = append(osFileEntries, v) } return osFileEntries, nil } func (h *PassThrough) Setattr( n domain.IOnodeIface, req *domain.HandlerRequest) error { logrus.Debugf("Executing Setattr() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.OpenFileRequest, Payload: &domain.OpenFilePayload{ File: n.Path(), Flags: strconv.Itoa(n.OpenFlags()), Mode: strconv.Itoa(int(n.OpenMode())), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return responseMsg.Payload.(error) } return nil } // Auxiliary method to fetch the content of any given file within a container. func (h *PassThrough) fetchFile( n domain.IOnodeIface, process domain.ProcessIface) (string, error) { // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( process.Pid(), &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.ReadFileRequest, Payload: &domain.ReadFilePayload{ File: n.Path(), }, }, nil, false, ) // Launch nsenter-event to obtain file state within container // namespaces. err := nss.SendRequestEvent(event) if err
if req.Offset > 0 {
random_line_split
passThrough.go
doing the I/O // operation and performs this one on behalf of it. // // Currently, this handler serves non-emulated resources within the /proc/sys // subtree, but there's nothing specific to this path in this handler's // implementation (see that the Path attribute is set to "*"), so this one could // be utilized for pass-through operations in other subtrees too. // type PassThrough struct { domain.HandlerBase } var PassThrough_Handler = &PassThrough{ domain.HandlerBase{ Name: "PassThrough", Path: "*", Enabled: true, }, } func (h *PassThrough) Lookup( n domain.IOnodeIface, req *domain.HandlerRequest) (os.FileInfo, error) { logrus.Debugf("Executing Lookup() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.LookupRequest, Payload: &domain.LookupPayload{ Entry: n.Path(), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return nil, err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return nil, responseMsg.Payload.(error) } info := responseMsg.Payload.(domain.FileInfo) return info, nil } func (h *PassThrough)
( n domain.IOnodeIface, req *domain.HandlerRequest) error { logrus.Debugf("Executing Open() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.OpenFileRequest, Payload: &domain.OpenFilePayload{ File: n.Path(), Flags: strconv.Itoa(n.OpenFlags()), Mode: strconv.Itoa(int(n.OpenMode())), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return responseMsg.Payload.(error) } return nil } func (h *PassThrough) Read( n domain.IOnodeIface, req *domain.HandlerRequest) (int, error) { var resource = n.Name() logrus.Debugf("Executing Read() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, resource) if req.Offset > 0 { return 0, io.EOF } var ( data string ok bool err error ) path := n.Path() prs := h.Service.ProcessService() process := prs.ProcessCreate(req.Pid, req.Uid, req.Gid) cntr := req.Container // // Caching here improves performance by avoiding dispatching the nsenter agent. But // note that caching is only helping processes at the sys container level, not in inner // containers or unshared namespaces. To enable caching for those, we would need to // have a cache per each namespace and this is expensive; plus we would also need to // know when the namespace ceases to exist in order to destroy the cache associated // with it. // if domain.ProcessNsMatch(process, cntr.InitProc()) { // If this resource is cached, return it's data; otherwise fetch its data from the // host FS and store it in the cache. cntr.Lock() data, ok = cntr.Data(path, resource) if !ok { data, err = h.fetchFile(n, process) if err != nil { cntr.Unlock() return 0, err } cntr.SetData(path, resource, data) } cntr.Unlock() } else { data, err = h.fetchFile(n, process) if err != nil { return 0, err } } data += "\n" return copyResultBuffer(req.Data, []byte(data)) } func (h *PassThrough) Write( n domain.IOnodeIface, req *domain.HandlerRequest) (int, error) { var resource = n.Name() logrus.Debugf("Executing Write() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, resource) path := n.Path() cntr := req.Container newContent := strings.TrimSpace(string(req.Data)) prs := h.Service.ProcessService() process := prs.ProcessCreate(req.Pid, req.Uid, req.Gid) // If write op is originated by a process within a registered sys-container // (it fully matches its namespaces) then store the data in the cache and do // a write-through to the host FS. Otherwise just do the write-through. if domain.ProcessNsMatch(process, cntr.InitProc()) { cntr.Lock() if err := h.pushFile(n, process, newContent); err != nil { cntr.Unlock() return 0, err } cntr.SetData(path, resource, newContent) cntr.Unlock() } else { if err := h.pushFile(n, process, newContent); err != nil { return 0, err } } return len(req.Data), nil } func (h *PassThrough) ReadDirAll( n domain.IOnodeIface, req *domain.HandlerRequest) ([]os.FileInfo, error) { logrus.Debugf("Executing ReadDirAll() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.ReadDirRequest, Payload: &domain.ReadDirPayload{ Dir: n.Path(), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return nil, err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return nil, responseMsg.Payload.(error) } var osFileEntries = make([]os.FileInfo, 0) // Transform event-response payload into a FileInfo slice. Notice that to // convert []T1 struct to a []T2 one, we must iterate through each element // and do the conversion one element at a time. dirEntries := responseMsg.Payload.([]domain.FileInfo) for _, v := range dirEntries { osFileEntries = append(osFileEntries, v) } return osFileEntries, nil } func (h *PassThrough) Setattr( n domain.IOnodeIface, req *domain.HandlerRequest) error { logrus.Debugf("Executing Setattr() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.OpenFileRequest, Payload: &domain.OpenFilePayload{ File: n.Path(), Flags: strconv.Itoa(n.OpenFlags()), Mode: strconv.Itoa(int(n.OpenMode())), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return responseMsg.Payload.(error) } return nil } // Auxiliary method to fetch the content of any given file within a container. func (h *PassThrough) fetchFile( n domain.IOnodeIface, process domain.ProcessIface) (string, error) { // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( process.Pid(), &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.ReadFileRequest, Payload: &domain.ReadFilePayload{ File: n.Path(), }, }, nil, false, ) // Launch nsenter-event to obtain file state within container // namespaces. err := nss.SendRequestEvent(event) if err
Open
identifier_name
passThrough.go
domain.ErrorResponse { return nil, responseMsg.Payload.(error) } info := responseMsg.Payload.(domain.FileInfo) return info, nil } func (h *PassThrough) Open( n domain.IOnodeIface, req *domain.HandlerRequest) error { logrus.Debugf("Executing Open() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.OpenFileRequest, Payload: &domain.OpenFilePayload{ File: n.Path(), Flags: strconv.Itoa(n.OpenFlags()), Mode: strconv.Itoa(int(n.OpenMode())), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return responseMsg.Payload.(error) } return nil } func (h *PassThrough) Read( n domain.IOnodeIface, req *domain.HandlerRequest) (int, error) { var resource = n.Name() logrus.Debugf("Executing Read() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, resource) if req.Offset > 0 { return 0, io.EOF } var ( data string ok bool err error ) path := n.Path() prs := h.Service.ProcessService() process := prs.ProcessCreate(req.Pid, req.Uid, req.Gid) cntr := req.Container // // Caching here improves performance by avoiding dispatching the nsenter agent. But // note that caching is only helping processes at the sys container level, not in inner // containers or unshared namespaces. To enable caching for those, we would need to // have a cache per each namespace and this is expensive; plus we would also need to // know when the namespace ceases to exist in order to destroy the cache associated // with it. // if domain.ProcessNsMatch(process, cntr.InitProc()) { // If this resource is cached, return it's data; otherwise fetch its data from the // host FS and store it in the cache. cntr.Lock() data, ok = cntr.Data(path, resource) if !ok { data, err = h.fetchFile(n, process) if err != nil { cntr.Unlock() return 0, err } cntr.SetData(path, resource, data) } cntr.Unlock() } else { data, err = h.fetchFile(n, process) if err != nil { return 0, err } } data += "\n" return copyResultBuffer(req.Data, []byte(data)) } func (h *PassThrough) Write( n domain.IOnodeIface, req *domain.HandlerRequest) (int, error) { var resource = n.Name() logrus.Debugf("Executing Write() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, resource) path := n.Path() cntr := req.Container newContent := strings.TrimSpace(string(req.Data)) prs := h.Service.ProcessService() process := prs.ProcessCreate(req.Pid, req.Uid, req.Gid) // If write op is originated by a process within a registered sys-container // (it fully matches its namespaces) then store the data in the cache and do // a write-through to the host FS. Otherwise just do the write-through. if domain.ProcessNsMatch(process, cntr.InitProc()) { cntr.Lock() if err := h.pushFile(n, process, newContent); err != nil { cntr.Unlock() return 0, err } cntr.SetData(path, resource, newContent) cntr.Unlock() } else { if err := h.pushFile(n, process, newContent); err != nil { return 0, err } } return len(req.Data), nil } func (h *PassThrough) ReadDirAll( n domain.IOnodeIface, req *domain.HandlerRequest) ([]os.FileInfo, error) { logrus.Debugf("Executing ReadDirAll() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.ReadDirRequest, Payload: &domain.ReadDirPayload{ Dir: n.Path(), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return nil, err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return nil, responseMsg.Payload.(error) } var osFileEntries = make([]os.FileInfo, 0) // Transform event-response payload into a FileInfo slice. Notice that to // convert []T1 struct to a []T2 one, we must iterate through each element // and do the conversion one element at a time. dirEntries := responseMsg.Payload.([]domain.FileInfo) for _, v := range dirEntries { osFileEntries = append(osFileEntries, v) } return osFileEntries, nil } func (h *PassThrough) Setattr( n domain.IOnodeIface, req *domain.HandlerRequest) error { logrus.Debugf("Executing Setattr() for req-id: %#x, handler: %s, resource: %s", req.ID, h.Name, n.Name()) // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( req.Pid, &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.OpenFileRequest, Payload: &domain.OpenFilePayload{ File: n.Path(), Flags: strconv.Itoa(n.OpenFlags()), Mode: strconv.Itoa(int(n.OpenMode())), }, }, nil, false, ) // Launch nsenter-event. err := nss.SendRequestEvent(event) if err != nil { return err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return responseMsg.Payload.(error) } return nil } // Auxiliary method to fetch the content of any given file within a container. func (h *PassThrough) fetchFile( n domain.IOnodeIface, process domain.ProcessIface) (string, error) { // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( process.Pid(), &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.ReadFileRequest, Payload: &domain.ReadFilePayload{ File: n.Path(), }, }, nil, false, ) // Launch nsenter-event to obtain file state within container // namespaces. err := nss.SendRequestEvent(event) if err != nil { return "", err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return "", responseMsg.Payload.(error) } info := responseMsg.Payload.(string) return info, nil } // Auxiliary method to inject content into any given file within a container. func (h *PassThrough) pushFile( n domain.IOnodeIface, process domain.ProcessIface, s string) error { // Create nsenterEvent to initiate interaction with container namespaces. nss := h.Service.NSenterService() event := nss.NewEvent( process.Pid(), &domain.AllNSsButMount, &domain.NSenterMessage{ Type: domain.WriteFileRequest, Payload: &domain.WriteFilePayload{ File: n.Path(), Content: s, }, }, nil, false, ) // Launch nsenter-event to write file state within container // namespaces. err := nss.SendRequestEvent(event) if err != nil { return err } // Obtain nsenter-event response. responseMsg := nss.ReceiveResponseEvent(event) if responseMsg.Type == domain.ErrorResponse { return responseMsg.Payload.(error) } return nil } func (h *PassThrough) GetName() string { return h.Name } func (h *PassThrough) GetPath() string { return h.Path } func (h *PassThrough) GetService() domain.HandlerServiceIface
{ return h.Service }
identifier_body
utils.go
" "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger" ) const ( // DefaultQuerySnapshotLimit constant is already present in pkg/csi/service/common/constants.go // However, using that constant creates an import cycle. // TODO: Refactor to move all the constants into a top level directory. DefaultQuerySnapshotLimit = int64(128) ) // QueryVolumeUtil helps to invoke query volume API based on the feature // state set for using query async volume. If useQueryVolumeAsync is set to // true, the function invokes CNS QueryVolumeAsync, otherwise it invokes // synchronous QueryVolume API. The function also take volume manager instance, // query filters, query selection as params. Returns queryResult when query // volume succeeds, otherwise returns appropriate errors. func QueryVolumeUtil(ctx context.Context, m cnsvolume.Manager, queryFilter cnstypes.CnsQueryFilter, querySelection *cnstypes.CnsQuerySelection, useQueryVolumeAsync bool) (*cnstypes.CnsQueryResult, error)
if err != nil { return nil, logger.LogNewErrorCodef(log, codes.Internal, "queryVolume failed for queryFilter: %+v. Err=%+v", queryFilter, err.Error()) } } return queryResult, nil } // QuerySnapshotsUtil helps invoke CNS QuerySnapshot API. The method takes in a snapshotQueryFilter that represents // the criteria to retrieve the snapshots. The maxEntries represents the max number of results that the caller of this // method can handle. func QuerySnapshotsUtil(ctx context.Context, m cnsvolume.Manager, snapshotQueryFilter cnstypes.CnsSnapshotQueryFilter, maxEntries int64) ([]cnstypes.CnsSnapshotQueryResultEntry, string, error) { log := logger.GetLogger(ctx) var allQuerySnapshotResults []cnstypes.CnsSnapshotQueryResultEntry var snapshotQuerySpec cnstypes.CnsSnapshotQuerySpec var batchSize int64 maxIteration := int64(1) isMaxIterationSet := false if snapshotQueryFilter.SnapshotQuerySpecs == nil { log.Infof("Attempting to retrieve all the Snapshots available in the vCenter inventory.") } else { snapshotQuerySpec = snapshotQueryFilter.SnapshotQuerySpecs[0] log.Infof("Invoking QuerySnapshots with spec: %+v", snapshotQuerySpec) } // Check if cursor is specified, if not set a default cursor. if snapshotQueryFilter.Cursor == nil { // Setting the default limit(128) explicitly. snapshotQueryFilter = cnstypes.CnsSnapshotQueryFilter{ Cursor: &cnstypes.CnsCursor{ Offset: 0, Limit: DefaultQuerySnapshotLimit, }, } batchSize = DefaultQuerySnapshotLimit } else { batchSize = snapshotQueryFilter.Cursor.Limit } iteration := int64(1) for { if iteration > maxIteration { // Exceeds the max number of results that can be handled by callers. nextToken := strconv.FormatInt(snapshotQueryFilter.Cursor.Offset, 10) log.Infof("the number of results: %d approached max-entries: %d for "+ "limit: %d in iteration: %d, returning with next-token: %s", len(allQuerySnapshotResults), maxEntries, batchSize, iteration, nextToken) return allQuerySnapshotResults, nextToken, nil } log.Infof("invoking QuerySnapshots in iteration: %d with offset: %d and limit: %d, current total "+ "results: %d", iteration, snapshotQueryFilter.Cursor.Offset, snapshotQueryFilter.Cursor.Limit, len(allQuerySnapshotResults)) snapshotQueryResult, err := m.QuerySnapshots(ctx, snapshotQueryFilter) if err != nil { log.Errorf("querySnapshots failed for snapshotQueryFilter: %v. Err=%+v", snapshotQueryFilter, err) return nil, "", err } if snapshotQueryResult == nil { log.Infof("Observed empty SnapshotQueryResult") break } if len(snapshotQueryResult.Entries) == 0 { log.Infof("QuerySnapshots retrieved no results for the spec: %+v", snapshotQuerySpec) } // Update the max iteration. // isMaxIterationSet ensures that the max iterations are set only once, this is to ensure that the number of // results are lower than the max entries supported by caller in a busy system which has increasing number // total records. if !isMaxIterationSet { if snapshotQueryResult.Cursor.TotalRecords < maxEntries { // If the total number of records is less than max entries supported by caller then // all results can be retrieved in a loop, when the results are returned no next-token is expected to be set. // Example: // maxEntries=200, totalRecords=150, batchSize=128 // maxIteration=2 // iteration-1: 128 results, iteration-2: 22 results // total results returned: 150 // offset=0 maxRecords := snapshotQueryResult.Cursor.TotalRecords numOfIterationsForAllResults := float64(maxRecords) / float64(batchSize) maxIteration = int64(math.Ceil(numOfIterationsForAllResults)) log.Infof("setting max iteration to %d for total records count: %d", maxIteration, maxRecords) } else { // All results cannot be returned to caller, in this case the expectation is return as many results with a // nextToken. // Example: // maxEntries=150, totalRecords=200, batchSize=128 // maxIteration=1 // iteration-1: 128 results // total results returned: 128 // offset= 1, callers are expected to call with new offset as next token. maxRecords := maxEntries numOfIterationsForAllResults := float64(maxRecords) / float64(batchSize) maxIteration = int64(math.Floor(numOfIterationsForAllResults)) log.Infof("setting max iteration to %d for total records count: %d and max limit: %d", maxIteration, snapshotQueryResult.Cursor.TotalRecords, maxRecords) } isMaxIterationSet = true } allQuerySnapshotResults = append(allQuerySnapshotResults, snapshotQueryResult.Entries...) log.Infof("%d more snapshots to be queried", snapshotQueryResult.Cursor.TotalRecords-snapshotQueryResult.Cursor.Offset) if snapshotQueryResult.Cursor.Offset == snapshotQueryResult.Cursor.TotalRecords { log.Infof("QuerySnapshots retrieved all records (%d) for the SnapshotQuerySpec: %+v in %d iterations", snapshotQueryResult.Cursor.TotalRecords, snapshotQuerySpec, iteration) break } iteration++ snapshotQueryFilter.Cursor = &snapshotQueryResult.Cursor } return allQuerySnapshotResults, "", nil } type CnsVolumeDetails struct { VolumeID string SizeInMB int64 DatastoreUrl string VolumeType string } // QueryVolumeDetailsUtil queries Capacity in MB and datastore URL for the source volume with expected volume type. func QueryVolumeDetailsUtil(ctx context.Context, m cnsvolume.Manager, volumeIds []cnstypes.CnsVolumeId) ( map[string]*CnsVolumeDetails, error) { log := logger.GetLogger(ctx) volumeDetailsMap := make(map[string]*CnsVolumeDetails) // Select only the backing object details, volume type and datastore. querySelection := &cnstypes.CnsQuerySelection{ Names: []string{ string(cnstypes.QuerySelectionNameTypeBackingObjectDetails), string(cnstypes.QuerySelectionNameTypeVolumeType), string(cnstypes.QuerySelectionNameTypeDataStoreUrl), }, } queryFilter := cnstypes.CnsQueryFilter{ VolumeIds: volumeIds, } log.Infof("Invoking QueryAllVolumeUtil with Filter: %+v, Selection: %+v", queryFilter, *querySelection) allQueryResults, err := m.QueryAllVolume(ctx, queryFilter, *querySelection) if err != nil { log.Errorf("failed to retrieve the volume size and datastore, err: %+v", err) return volumeDetailsMap, logger.LogNewErrorCodef(log,
{ log := logger.GetLogger(ctx) var queryAsyncNotSupported bool var queryResult *cnstypes.CnsQueryResult var err error if useQueryVolumeAsync { // AsyncQueryVolume feature switch is enabled. queryResult, err = m.QueryVolumeAsync(ctx, queryFilter, querySelection) if err != nil { if err.Error() == cnsvsphere.ErrNotSupported.Error() { log.Warn("QueryVolumeAsync is not supported. Invoking QueryVolume API") queryAsyncNotSupported = true } else { // Return for any other failures. return nil, logger.LogNewErrorCodef(log, codes.Internal, "queryVolumeAsync failed for queryFilter: %v. Err=%+v", queryFilter, err.Error()) } } } if !useQueryVolumeAsync || queryAsyncNotSupported { queryResult, err = m.QueryVolume(ctx, queryFilter)
identifier_body
utils.go
" "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger" ) const ( // DefaultQuerySnapshotLimit constant is already present in pkg/csi/service/common/constants.go // However, using that constant creates an import cycle. // TODO: Refactor to move all the constants into a top level directory. DefaultQuerySnapshotLimit = int64(128) ) // QueryVolumeUtil helps to invoke query volume API based on the feature // state set for using query async volume. If useQueryVolumeAsync is set to // true, the function invokes CNS QueryVolumeAsync, otherwise it invokes // synchronous QueryVolume API. The function also take volume manager instance, // query filters, query selection as params. Returns queryResult when query // volume succeeds, otherwise returns appropriate errors. func QueryVolumeUtil(ctx context.Context, m cnsvolume.Manager, queryFilter cnstypes.CnsQueryFilter, querySelection *cnstypes.CnsQuerySelection, useQueryVolumeAsync bool) (*cnstypes.CnsQueryResult, error) { log := logger.GetLogger(ctx) var queryAsyncNotSupported bool var queryResult *cnstypes.CnsQueryResult var err error if useQueryVolumeAsync { // AsyncQueryVolume feature switch is enabled. queryResult, err = m.QueryVolumeAsync(ctx, queryFilter, querySelection) if err != nil { if err.Error() == cnsvsphere.ErrNotSupported.Error() { log.Warn("QueryVolumeAsync is not supported. Invoking QueryVolume API") queryAsyncNotSupported = true } else { // Return for any other failures. return nil, logger.LogNewErrorCodef(log, codes.Internal, "queryVolumeAsync failed for queryFilter: %v. Err=%+v", queryFilter, err.Error()) } } } if !useQueryVolumeAsync || queryAsyncNotSupported { queryResult, err = m.QueryVolume(ctx, queryFilter) if err != nil { return nil, logger.LogNewErrorCodef(log, codes.Internal, "queryVolume failed for queryFilter: %+v. Err=%+v", queryFilter, err.Error()) } } return queryResult, nil } // QuerySnapshotsUtil helps invoke CNS QuerySnapshot API. The method takes in a snapshotQueryFilter that represents // the criteria to retrieve the snapshots. The maxEntries represents the max number of results that the caller of this // method can handle. func
(ctx context.Context, m cnsvolume.Manager, snapshotQueryFilter cnstypes.CnsSnapshotQueryFilter, maxEntries int64) ([]cnstypes.CnsSnapshotQueryResultEntry, string, error) { log := logger.GetLogger(ctx) var allQuerySnapshotResults []cnstypes.CnsSnapshotQueryResultEntry var snapshotQuerySpec cnstypes.CnsSnapshotQuerySpec var batchSize int64 maxIteration := int64(1) isMaxIterationSet := false if snapshotQueryFilter.SnapshotQuerySpecs == nil { log.Infof("Attempting to retrieve all the Snapshots available in the vCenter inventory.") } else { snapshotQuerySpec = snapshotQueryFilter.SnapshotQuerySpecs[0] log.Infof("Invoking QuerySnapshots with spec: %+v", snapshotQuerySpec) } // Check if cursor is specified, if not set a default cursor. if snapshotQueryFilter.Cursor == nil { // Setting the default limit(128) explicitly. snapshotQueryFilter = cnstypes.CnsSnapshotQueryFilter{ Cursor: &cnstypes.CnsCursor{ Offset: 0, Limit: DefaultQuerySnapshotLimit, }, } batchSize = DefaultQuerySnapshotLimit } else { batchSize = snapshotQueryFilter.Cursor.Limit } iteration := int64(1) for { if iteration > maxIteration { // Exceeds the max number of results that can be handled by callers. nextToken := strconv.FormatInt(snapshotQueryFilter.Cursor.Offset, 10) log.Infof("the number of results: %d approached max-entries: %d for "+ "limit: %d in iteration: %d, returning with next-token: %s", len(allQuerySnapshotResults), maxEntries, batchSize, iteration, nextToken) return allQuerySnapshotResults, nextToken, nil } log.Infof("invoking QuerySnapshots in iteration: %d with offset: %d and limit: %d, current total "+ "results: %d", iteration, snapshotQueryFilter.Cursor.Offset, snapshotQueryFilter.Cursor.Limit, len(allQuerySnapshotResults)) snapshotQueryResult, err := m.QuerySnapshots(ctx, snapshotQueryFilter) if err != nil { log.Errorf("querySnapshots failed for snapshotQueryFilter: %v. Err=%+v", snapshotQueryFilter, err) return nil, "", err } if snapshotQueryResult == nil { log.Infof("Observed empty SnapshotQueryResult") break } if len(snapshotQueryResult.Entries) == 0 { log.Infof("QuerySnapshots retrieved no results for the spec: %+v", snapshotQuerySpec) } // Update the max iteration. // isMaxIterationSet ensures that the max iterations are set only once, this is to ensure that the number of // results are lower than the max entries supported by caller in a busy system which has increasing number // total records. if !isMaxIterationSet { if snapshotQueryResult.Cursor.TotalRecords < maxEntries { // If the total number of records is less than max entries supported by caller then // all results can be retrieved in a loop, when the results are returned no next-token is expected to be set. // Example: // maxEntries=200, totalRecords=150, batchSize=128 // maxIteration=2 // iteration-1: 128 results, iteration-2: 22 results // total results returned: 150 // offset=0 maxRecords := snapshotQueryResult.Cursor.TotalRecords numOfIterationsForAllResults := float64(maxRecords) / float64(batchSize) maxIteration = int64(math.Ceil(numOfIterationsForAllResults)) log.Infof("setting max iteration to %d for total records count: %d", maxIteration, maxRecords) } else { // All results cannot be returned to caller, in this case the expectation is return as many results with a // nextToken. // Example: // maxEntries=150, totalRecords=200, batchSize=128 // maxIteration=1 // iteration-1: 128 results // total results returned: 128 // offset= 1, callers are expected to call with new offset as next token. maxRecords := maxEntries numOfIterationsForAllResults := float64(maxRecords) / float64(batchSize) maxIteration = int64(math.Floor(numOfIterationsForAllResults)) log.Infof("setting max iteration to %d for total records count: %d and max limit: %d", maxIteration, snapshotQueryResult.Cursor.TotalRecords, maxRecords) } isMaxIterationSet = true } allQuerySnapshotResults = append(allQuerySnapshotResults, snapshotQueryResult.Entries...) log.Infof("%d more snapshots to be queried", snapshotQueryResult.Cursor.TotalRecords-snapshotQueryResult.Cursor.Offset) if snapshotQueryResult.Cursor.Offset == snapshotQueryResult.Cursor.TotalRecords { log.Infof("QuerySnapshots retrieved all records (%d) for the SnapshotQuerySpec: %+v in %d iterations", snapshotQueryResult.Cursor.TotalRecords, snapshotQuerySpec, iteration) break } iteration++ snapshotQueryFilter.Cursor = &snapshotQueryResult.Cursor } return allQuerySnapshotResults, "", nil } type CnsVolumeDetails struct { VolumeID string SizeInMB int64 DatastoreUrl string VolumeType string } // QueryVolumeDetailsUtil queries Capacity in MB and datastore URL for the source volume with expected volume type. func QueryVolumeDetailsUtil(ctx context.Context, m cnsvolume.Manager, volumeIds []cnstypes.CnsVolumeId) ( map[string]*CnsVolumeDetails, error) { log := logger.GetLogger(ctx) volumeDetailsMap := make(map[string]*CnsVolumeDetails) // Select only the backing object details, volume type and datastore. querySelection := &cnstypes.CnsQuerySelection{ Names: []string{ string(cnstypes.QuerySelectionNameTypeBackingObjectDetails), string(cnstypes.QuerySelectionNameTypeVolumeType), string(cnstypes.QuerySelectionNameTypeDataStoreUrl), }, } queryFilter := cnstypes.CnsQueryFilter{ VolumeIds: volumeIds, } log.Infof("Invoking QueryAllVolumeUtil with Filter: %+v, Selection: %+v", queryFilter, *querySelection) allQueryResults, err := m.QueryAllVolume(ctx, queryFilter, *querySelection) if err != nil { log.Errorf("failed to retrieve the volume size and datastore, err: %+v", err) return volumeDetailsMap, logger.LogNewErrorCodef(log,
QuerySnapshotsUtil
identifier_name
utils.go
) if err != nil { return nil, logger.LogNewErrorCodef(log, codes.Internal, "queryVolume failed for queryFilter: %+v. Err=%+v", queryFilter, err.Error()) } } return queryResult, nil } // QuerySnapshotsUtil helps invoke CNS QuerySnapshot API. The method takes in a snapshotQueryFilter that represents // the criteria to retrieve the snapshots. The maxEntries represents the max number of results that the caller of this // method can handle. func QuerySnapshotsUtil(ctx context.Context, m cnsvolume.Manager, snapshotQueryFilter cnstypes.CnsSnapshotQueryFilter, maxEntries int64) ([]cnstypes.CnsSnapshotQueryResultEntry, string, error) { log := logger.GetLogger(ctx) var allQuerySnapshotResults []cnstypes.CnsSnapshotQueryResultEntry var snapshotQuerySpec cnstypes.CnsSnapshotQuerySpec var batchSize int64 maxIteration := int64(1) isMaxIterationSet := false if snapshotQueryFilter.SnapshotQuerySpecs == nil { log.Infof("Attempting to retrieve all the Snapshots available in the vCenter inventory.") } else { snapshotQuerySpec = snapshotQueryFilter.SnapshotQuerySpecs[0] log.Infof("Invoking QuerySnapshots with spec: %+v", snapshotQuerySpec) } // Check if cursor is specified, if not set a default cursor. if snapshotQueryFilter.Cursor == nil { // Setting the default limit(128) explicitly. snapshotQueryFilter = cnstypes.CnsSnapshotQueryFilter{ Cursor: &cnstypes.CnsCursor{ Offset: 0, Limit: DefaultQuerySnapshotLimit, }, } batchSize = DefaultQuerySnapshotLimit } else { batchSize = snapshotQueryFilter.Cursor.Limit } iteration := int64(1) for { if iteration > maxIteration { // Exceeds the max number of results that can be handled by callers. nextToken := strconv.FormatInt(snapshotQueryFilter.Cursor.Offset, 10) log.Infof("the number of results: %d approached max-entries: %d for "+ "limit: %d in iteration: %d, returning with next-token: %s", len(allQuerySnapshotResults), maxEntries, batchSize, iteration, nextToken) return allQuerySnapshotResults, nextToken, nil } log.Infof("invoking QuerySnapshots in iteration: %d with offset: %d and limit: %d, current total "+ "results: %d", iteration, snapshotQueryFilter.Cursor.Offset, snapshotQueryFilter.Cursor.Limit, len(allQuerySnapshotResults)) snapshotQueryResult, err := m.QuerySnapshots(ctx, snapshotQueryFilter) if err != nil { log.Errorf("querySnapshots failed for snapshotQueryFilter: %v. Err=%+v", snapshotQueryFilter, err) return nil, "", err } if snapshotQueryResult == nil { log.Infof("Observed empty SnapshotQueryResult") break } if len(snapshotQueryResult.Entries) == 0 { log.Infof("QuerySnapshots retrieved no results for the spec: %+v", snapshotQuerySpec) } // Update the max iteration. // isMaxIterationSet ensures that the max iterations are set only once, this is to ensure that the number of // results are lower than the max entries supported by caller in a busy system which has increasing number // total records. if !isMaxIterationSet { if snapshotQueryResult.Cursor.TotalRecords < maxEntries { // If the total number of records is less than max entries supported by caller then // all results can be retrieved in a loop, when the results are returned no next-token is expected to be set. // Example: // maxEntries=200, totalRecords=150, batchSize=128 // maxIteration=2 // iteration-1: 128 results, iteration-2: 22 results // total results returned: 150 // offset=0 maxRecords := snapshotQueryResult.Cursor.TotalRecords numOfIterationsForAllResults := float64(maxRecords) / float64(batchSize) maxIteration = int64(math.Ceil(numOfIterationsForAllResults)) log.Infof("setting max iteration to %d for total records count: %d", maxIteration, maxRecords) } else { // All results cannot be returned to caller, in this case the expectation is return as many results with a // nextToken. // Example: // maxEntries=150, totalRecords=200, batchSize=128 // maxIteration=1 // iteration-1: 128 results // total results returned: 128 // offset= 1, callers are expected to call with new offset as next token. maxRecords := maxEntries numOfIterationsForAllResults := float64(maxRecords) / float64(batchSize) maxIteration = int64(math.Floor(numOfIterationsForAllResults)) log.Infof("setting max iteration to %d for total records count: %d and max limit: %d", maxIteration, snapshotQueryResult.Cursor.TotalRecords, maxRecords) } isMaxIterationSet = true } allQuerySnapshotResults = append(allQuerySnapshotResults, snapshotQueryResult.Entries...) log.Infof("%d more snapshots to be queried", snapshotQueryResult.Cursor.TotalRecords-snapshotQueryResult.Cursor.Offset) if snapshotQueryResult.Cursor.Offset == snapshotQueryResult.Cursor.TotalRecords { log.Infof("QuerySnapshots retrieved all records (%d) for the SnapshotQuerySpec: %+v in %d iterations", snapshotQueryResult.Cursor.TotalRecords, snapshotQuerySpec, iteration) break } iteration++ snapshotQueryFilter.Cursor = &snapshotQueryResult.Cursor } return allQuerySnapshotResults, "", nil } type CnsVolumeDetails struct { VolumeID string SizeInMB int64 DatastoreUrl string VolumeType string } // QueryVolumeDetailsUtil queries Capacity in MB and datastore URL for the source volume with expected volume type. func QueryVolumeDetailsUtil(ctx context.Context, m cnsvolume.Manager, volumeIds []cnstypes.CnsVolumeId) ( map[string]*CnsVolumeDetails, error) { log := logger.GetLogger(ctx) volumeDetailsMap := make(map[string]*CnsVolumeDetails) // Select only the backing object details, volume type and datastore. querySelection := &cnstypes.CnsQuerySelection{ Names: []string{ string(cnstypes.QuerySelectionNameTypeBackingObjectDetails), string(cnstypes.QuerySelectionNameTypeVolumeType), string(cnstypes.QuerySelectionNameTypeDataStoreUrl), }, } queryFilter := cnstypes.CnsQueryFilter{ VolumeIds: volumeIds, } log.Infof("Invoking QueryAllVolumeUtil with Filter: %+v, Selection: %+v", queryFilter, *querySelection) allQueryResults, err := m.QueryAllVolume(ctx, queryFilter, *querySelection) if err != nil { log.Errorf("failed to retrieve the volume size and datastore, err: %+v", err) return volumeDetailsMap, logger.LogNewErrorCodef(log, codes.Internal, "failed to retrieve the volume sizes: %+v", err) } log.Infof("Number of results from QueryAllVolumeUtil: %d", len(allQueryResults.Volumes)) for _, res := range allQueryResults.Volumes { volumeId := res.VolumeId datastoreUrl := res.DatastoreUrl volumeCapacityInMB := res.BackingObjectDetails.GetCnsBackingObjectDetails().CapacityInMb volumeType := res.VolumeType log.Debugf("VOLUME: %s, TYPE: %s, DATASTORE: %s, CAPACITY: %d", volumeId, volumeType, datastoreUrl, volumeCapacityInMB) volumeDetails := &CnsVolumeDetails{ VolumeID: volumeId.Id, SizeInMB: volumeCapacityInMB, DatastoreUrl: datastoreUrl, VolumeType: volumeType, } volumeDetailsMap[volumeId.Id] = volumeDetails } return volumeDetailsMap, nil } // LogoutAllSessions will logout all vCenter sessions and disconnect vCenter client func LogoutAllvCenterSessions(ctx context.Context) { log := logger.GetLogger(ctx) log.Info("Logging out all vCenter sessions") virtualcentermanager := cnsvsphere.GetVirtualCenterManager(ctx) vCenters := virtualcentermanager.GetAllVirtualCenters() managerInstanceMap := cnsvolume.GetAllManagerInstances(ctx) for _, vc := range vCenters { if vc.Client == nil { continue } log.Info("Closing idle vCenter session") vc.Client.CloseIdleConnections() // logout vCenter session for list-view mgr, ok := managerInstanceMap[vc.Config.Host] if ok && mgr != nil { err := mgr.LogoutListViewVCSession(ctx)
if err != nil { continue } } log.Infof("Disconnecting vCenter client for host %s", vc.Config.Host)
random_line_split
utils.go
" "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger" ) const ( // DefaultQuerySnapshotLimit constant is already present in pkg/csi/service/common/constants.go // However, using that constant creates an import cycle. // TODO: Refactor to move all the constants into a top level directory. DefaultQuerySnapshotLimit = int64(128) ) // QueryVolumeUtil helps to invoke query volume API based on the feature // state set for using query async volume. If useQueryVolumeAsync is set to // true, the function invokes CNS QueryVolumeAsync, otherwise it invokes // synchronous QueryVolume API. The function also take volume manager instance, // query filters, query selection as params. Returns queryResult when query // volume succeeds, otherwise returns appropriate errors. func QueryVolumeUtil(ctx context.Context, m cnsvolume.Manager, queryFilter cnstypes.CnsQueryFilter, querySelection *cnstypes.CnsQuerySelection, useQueryVolumeAsync bool) (*cnstypes.CnsQueryResult, error) { log := logger.GetLogger(ctx) var queryAsyncNotSupported bool var queryResult *cnstypes.CnsQueryResult var err error if useQueryVolumeAsync { // AsyncQueryVolume feature switch is enabled. queryResult, err = m.QueryVolumeAsync(ctx, queryFilter, querySelection) if err != nil { if err.Error() == cnsvsphere.ErrNotSupported.Error() { log.Warn("QueryVolumeAsync is not supported. Invoking QueryVolume API") queryAsyncNotSupported = true } else { // Return for any other failures. return nil, logger.LogNewErrorCodef(log, codes.Internal, "queryVolumeAsync failed for queryFilter: %v. Err=%+v", queryFilter, err.Error()) } } } if !useQueryVolumeAsync || queryAsyncNotSupported { queryResult, err = m.QueryVolume(ctx, queryFilter) if err != nil { return nil, logger.LogNewErrorCodef(log, codes.Internal, "queryVolume failed for queryFilter: %+v. Err=%+v", queryFilter, err.Error()) } } return queryResult, nil } // QuerySnapshotsUtil helps invoke CNS QuerySnapshot API. The method takes in a snapshotQueryFilter that represents // the criteria to retrieve the snapshots. The maxEntries represents the max number of results that the caller of this // method can handle. func QuerySnapshotsUtil(ctx context.Context, m cnsvolume.Manager, snapshotQueryFilter cnstypes.CnsSnapshotQueryFilter, maxEntries int64) ([]cnstypes.CnsSnapshotQueryResultEntry, string, error) { log := logger.GetLogger(ctx) var allQuerySnapshotResults []cnstypes.CnsSnapshotQueryResultEntry var snapshotQuerySpec cnstypes.CnsSnapshotQuerySpec var batchSize int64 maxIteration := int64(1) isMaxIterationSet := false if snapshotQueryFilter.SnapshotQuerySpecs == nil { log.Infof("Attempting to retrieve all the Snapshots available in the vCenter inventory.") } else { snapshotQuerySpec = snapshotQueryFilter.SnapshotQuerySpecs[0] log.Infof("Invoking QuerySnapshots with spec: %+v", snapshotQuerySpec) } // Check if cursor is specified, if not set a default cursor. if snapshotQueryFilter.Cursor == nil
else { batchSize = snapshotQueryFilter.Cursor.Limit } iteration := int64(1) for { if iteration > maxIteration { // Exceeds the max number of results that can be handled by callers. nextToken := strconv.FormatInt(snapshotQueryFilter.Cursor.Offset, 10) log.Infof("the number of results: %d approached max-entries: %d for "+ "limit: %d in iteration: %d, returning with next-token: %s", len(allQuerySnapshotResults), maxEntries, batchSize, iteration, nextToken) return allQuerySnapshotResults, nextToken, nil } log.Infof("invoking QuerySnapshots in iteration: %d with offset: %d and limit: %d, current total "+ "results: %d", iteration, snapshotQueryFilter.Cursor.Offset, snapshotQueryFilter.Cursor.Limit, len(allQuerySnapshotResults)) snapshotQueryResult, err := m.QuerySnapshots(ctx, snapshotQueryFilter) if err != nil { log.Errorf("querySnapshots failed for snapshotQueryFilter: %v. Err=%+v", snapshotQueryFilter, err) return nil, "", err } if snapshotQueryResult == nil { log.Infof("Observed empty SnapshotQueryResult") break } if len(snapshotQueryResult.Entries) == 0 { log.Infof("QuerySnapshots retrieved no results for the spec: %+v", snapshotQuerySpec) } // Update the max iteration. // isMaxIterationSet ensures that the max iterations are set only once, this is to ensure that the number of // results are lower than the max entries supported by caller in a busy system which has increasing number // total records. if !isMaxIterationSet { if snapshotQueryResult.Cursor.TotalRecords < maxEntries { // If the total number of records is less than max entries supported by caller then // all results can be retrieved in a loop, when the results are returned no next-token is expected to be set. // Example: // maxEntries=200, totalRecords=150, batchSize=128 // maxIteration=2 // iteration-1: 128 results, iteration-2: 22 results // total results returned: 150 // offset=0 maxRecords := snapshotQueryResult.Cursor.TotalRecords numOfIterationsForAllResults := float64(maxRecords) / float64(batchSize) maxIteration = int64(math.Ceil(numOfIterationsForAllResults)) log.Infof("setting max iteration to %d for total records count: %d", maxIteration, maxRecords) } else { // All results cannot be returned to caller, in this case the expectation is return as many results with a // nextToken. // Example: // maxEntries=150, totalRecords=200, batchSize=128 // maxIteration=1 // iteration-1: 128 results // total results returned: 128 // offset= 1, callers are expected to call with new offset as next token. maxRecords := maxEntries numOfIterationsForAllResults := float64(maxRecords) / float64(batchSize) maxIteration = int64(math.Floor(numOfIterationsForAllResults)) log.Infof("setting max iteration to %d for total records count: %d and max limit: %d", maxIteration, snapshotQueryResult.Cursor.TotalRecords, maxRecords) } isMaxIterationSet = true } allQuerySnapshotResults = append(allQuerySnapshotResults, snapshotQueryResult.Entries...) log.Infof("%d more snapshots to be queried", snapshotQueryResult.Cursor.TotalRecords-snapshotQueryResult.Cursor.Offset) if snapshotQueryResult.Cursor.Offset == snapshotQueryResult.Cursor.TotalRecords { log.Infof("QuerySnapshots retrieved all records (%d) for the SnapshotQuerySpec: %+v in %d iterations", snapshotQueryResult.Cursor.TotalRecords, snapshotQuerySpec, iteration) break } iteration++ snapshotQueryFilter.Cursor = &snapshotQueryResult.Cursor } return allQuerySnapshotResults, "", nil } type CnsVolumeDetails struct { VolumeID string SizeInMB int64 DatastoreUrl string VolumeType string } // QueryVolumeDetailsUtil queries Capacity in MB and datastore URL for the source volume with expected volume type. func QueryVolumeDetailsUtil(ctx context.Context, m cnsvolume.Manager, volumeIds []cnstypes.CnsVolumeId) ( map[string]*CnsVolumeDetails, error) { log := logger.GetLogger(ctx) volumeDetailsMap := make(map[string]*CnsVolumeDetails) // Select only the backing object details, volume type and datastore. querySelection := &cnstypes.CnsQuerySelection{ Names: []string{ string(cnstypes.QuerySelectionNameTypeBackingObjectDetails), string(cnstypes.QuerySelectionNameTypeVolumeType), string(cnstypes.QuerySelectionNameTypeDataStoreUrl), }, } queryFilter := cnstypes.CnsQueryFilter{ VolumeIds: volumeIds, } log.Infof("Invoking QueryAllVolumeUtil with Filter: %+v, Selection: %+v", queryFilter, *querySelection) allQueryResults, err := m.QueryAllVolume(ctx, queryFilter, *querySelection) if err != nil { log.Errorf("failed to retrieve the volume size and datastore, err: %+v", err) return volumeDetailsMap, logger.LogNewErrorCodef(log,
{ // Setting the default limit(128) explicitly. snapshotQueryFilter = cnstypes.CnsSnapshotQueryFilter{ Cursor: &cnstypes.CnsCursor{ Offset: 0, Limit: DefaultQuerySnapshotLimit, }, } batchSize = DefaultQuerySnapshotLimit }
conditional_block
block.go
Flag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode)) return cmd } func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) { // get the node node, err := cliCtx.GetNode() if err != nil { return nil, err } // header -> BlockchainInfo // header, tx -> Block // results -> BlockResults res, err := node.Block(height) if err != nil { return nil, err } if !cliCtx.TrustNode { check, err := cliCtx.Verify(res.Block.Height) if err != nil { return nil, err } err = tmliteProxy.ValidateBlockMeta(res.BlockMeta, check) if err != nil { return nil, err } err = tmliteProxy.ValidateBlock(res.Block, check) if err != nil { return nil, err } } if cliCtx.Indent { return cdc.MarshalJSONIndent(res, "", " ") } return cdc.MarshalJSON(res) } // get the current blockchain height func GetChainHeight(cliCtx context.CLIContext) (int64, error) { node, err := cliCtx.GetNode() if err != nil { return -1, err } status, err := node.Status() if err != nil { return -1, err } height := status.SyncInfo.LatestBlockHeight return height, nil } // CMD func printBlock(cmd *cobra.Command, args []string) error { var height *int64 // optional height if len(args) > 0 { h, err := strconv.Atoi(args[0]) if err != nil { return err } if h > 0 { tmp := int64(h) height = &tmp } } output, err := getBlock(context.NewCLIContext(), height) if err != nil { return err } fmt.Println(string(output)) return nil } // REST // REST handler to get a block func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, "ERROR: Couldn't parse block height. Assumed format is '/block/{height}'.") return } chainHeight, err := GetChainHeight(cliCtx) if height > chainHeight { rest.WriteErrorResponse(w, http.StatusNotFound, "ERROR: Requested block height is bigger then the chain length.") return } output, err := getBlock(cliCtx, &height) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, output, cliCtx.Indent) } } // REST handler to get the latest block func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { height, err := GetChainHeight(cliCtx) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } output, err := getBlock(cliCtx, &height) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, output, cliCtx.Indent) } } //BlockDetails struct type DisplayTx struct { From sdk.AccAddress To sdk.AccAddress Amount []sdk.BigCoin Hash string Memo string Data string } type DisplayBlock struct { Txs []DisplayTx `json:"txs"` Evidence tmTypes.EvidenceData `json:"evidence"` LastCommit *tmTypes.Commit `json:"last_commit"` } type BlockInfo struct { BlockMeta *tmTypes.BlockMeta `json:"block_meta"` Block DisplayBlock `json:"block"` Time string `json:"time"` } type DisplayFee struct { // Amount []sdk.BigCoin `json:"amount"` // junying-todo, 2019-12-07 GasWanted string `json:"gas_wanted"` GasPrice string `json:"gas_price"` } type StdTx struct { Msgs []DisplayTx `json:"msg"` Fee DisplayFee `json:"fee"` Signatures []auth.StdSignature `json:"signatures"` Memo string `json:"memo"` } type GetTxResponse struct { Height int64 `json:"height"` TxHash string `json:"txhash"` Code uint32 `json:"code,omitempty"` Log sdk.ABCIMessageLogs `json:"log,omitempty"` Info string `json:"info,omitempty"` GasWanted int64 `json:"gas_wanted,omitempty"` GasUsed int64 `json:"gas_used,omitempty"` Tags sdk.StringTags `json:"tags,omitempty"` Codespace string `json:"codespace,omitempty"` Tx StdTx `json:"tx,omitempty"` // Data string `json:"data,omitempty"` } // GetBlockDetailFn func GetBlockDetailFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/block/{height}'.")) return } chainHeight, err := GetChainHeight(cliCtx) if height > chainHeight { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("ERROR: Requested block height is bigger then the chain length.")) return } // get the node node, err := cliCtx.GetNode() if err != nil { fmt.Printf("getNode error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } var blockInfo BlockInfo //get Block info resultBlock, err := node.Block(&height) if err != nil { fmt.Printf("get block error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } blockInfo.BlockMeta = resultBlock.BlockMeta blockInfo.Block.Evidence = resultBlock.Block.Evidence blockInfo.Block.LastCommit = resultBlock.Block.LastCommit blockInfo.Time = resultBlock.BlockMeta.Header.Time.Local().Format("2006-01-02 15:04:05") for _, tx := range resultBlock.Block.Data.Txs { sdkTx, err := parseTx(cdc, tx) if err != nil { fmt.Printf("parseTx error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } switch currTx := sdkTx.(type) { case auth.StdTx: var displayTx DisplayTx for _, msg := range currTx.GetMsgs() { //fmt.Printf("msg|route=%s|type=%s\n", msg.Route(), msg.Type()) switch msg := msg.(type) { case htdfservice.MsgSend: displayTx.From = msg.From displayTx.To = msg.To displayTx.Hash = hex.EncodeToString(tx.Hash()) displayTx.Amount = unit_convert.DefaultCoinsToBigCoins(msg.Amount) displayTx.Memo = currTx.Memo blockInfo.Block.Txs = append(blockInfo.Block.Txs, displayTx) //fmt.Printf("msg|from=%s|to=%s\n", msg.From, msg.To) default: fmt.Printf("ignore type|type=%s|route=%s\n", msg.Type(), msg.Route()) continue } } default: fmt.Printf("unknown type: %+v\n", currTx) } } sdkRest.PostProcessResponse(w, cdc, &blockInfo, cliCtx.Indent) } } func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) { var tx auth.StdTx err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return nil, err }
func formatTxResult(cdc *codec.Codec, res *ctypes.ResultTx, resBlock *ctypes.ResultBlock) (sdk.TxResponse, error) { tx, err := parseTx(cdc, res.Tx) if err != nil { return sdk.TxResponse{}, err } return sdk.NewResponseResultTx(res, tx, resBlock.Block.Time.Format(time.RFC3339)), nil } // func GetTxFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r
return tx, nil }
random_line_split
block.go
(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode)) return cmd } func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) { // get the node node, err := cliCtx.GetNode() if err != nil { return nil, err } // header -> BlockchainInfo // header, tx -> Block // results -> BlockResults res, err := node.Block(height) if err != nil { return nil, err } if !cliCtx.TrustNode { check, err := cliCtx.Verify(res.Block.Height) if err != nil { return nil, err } err = tmliteProxy.ValidateBlockMeta(res.BlockMeta, check) if err != nil { return nil, err } err = tmliteProxy.ValidateBlock(res.Block, check) if err != nil { return nil, err } } if cliCtx.Indent { return cdc.MarshalJSONIndent(res, "", " ") } return cdc.MarshalJSON(res) } // get the current blockchain height func GetChainHeight(cliCtx context.CLIContext) (int64, error)
// CMD func printBlock(cmd *cobra.Command, args []string) error { var height *int64 // optional height if len(args) > 0 { h, err := strconv.Atoi(args[0]) if err != nil { return err } if h > 0 { tmp := int64(h) height = &tmp } } output, err := getBlock(context.NewCLIContext(), height) if err != nil { return err } fmt.Println(string(output)) return nil } // REST // REST handler to get a block func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, "ERROR: Couldn't parse block height. Assumed format is '/block/{height}'.") return } chainHeight, err := GetChainHeight(cliCtx) if height > chainHeight { rest.WriteErrorResponse(w, http.StatusNotFound, "ERROR: Requested block height is bigger then the chain length.") return } output, err := getBlock(cliCtx, &height) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, output, cliCtx.Indent) } } // REST handler to get the latest block func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { height, err := GetChainHeight(cliCtx) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } output, err := getBlock(cliCtx, &height) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, output, cliCtx.Indent) } } //BlockDetails struct type DisplayTx struct { From sdk.AccAddress To sdk.AccAddress Amount []sdk.BigCoin Hash string Memo string Data string } type DisplayBlock struct { Txs []DisplayTx `json:"txs"` Evidence tmTypes.EvidenceData `json:"evidence"` LastCommit *tmTypes.Commit `json:"last_commit"` } type BlockInfo struct { BlockMeta *tmTypes.BlockMeta `json:"block_meta"` Block DisplayBlock `json:"block"` Time string `json:"time"` } type DisplayFee struct { // Amount []sdk.BigCoin `json:"amount"` // junying-todo, 2019-12-07 GasWanted string `json:"gas_wanted"` GasPrice string `json:"gas_price"` } type StdTx struct { Msgs []DisplayTx `json:"msg"` Fee DisplayFee `json:"fee"` Signatures []auth.StdSignature `json:"signatures"` Memo string `json:"memo"` } type GetTxResponse struct { Height int64 `json:"height"` TxHash string `json:"txhash"` Code uint32 `json:"code,omitempty"` Log sdk.ABCIMessageLogs `json:"log,omitempty"` Info string `json:"info,omitempty"` GasWanted int64 `json:"gas_wanted,omitempty"` GasUsed int64 `json:"gas_used,omitempty"` Tags sdk.StringTags `json:"tags,omitempty"` Codespace string `json:"codespace,omitempty"` Tx StdTx `json:"tx,omitempty"` // Data string `json:"data,omitempty"` } // GetBlockDetailFn func GetBlockDetailFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/block/{height}'.")) return } chainHeight, err := GetChainHeight(cliCtx) if height > chainHeight { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("ERROR: Requested block height is bigger then the chain length.")) return } // get the node node, err := cliCtx.GetNode() if err != nil { fmt.Printf("getNode error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } var blockInfo BlockInfo //get Block info resultBlock, err := node.Block(&height) if err != nil { fmt.Printf("get block error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } blockInfo.BlockMeta = resultBlock.BlockMeta blockInfo.Block.Evidence = resultBlock.Block.Evidence blockInfo.Block.LastCommit = resultBlock.Block.LastCommit blockInfo.Time = resultBlock.BlockMeta.Header.Time.Local().Format("2006-01-02 15:04:05") for _, tx := range resultBlock.Block.Data.Txs { sdkTx, err := parseTx(cdc, tx) if err != nil { fmt.Printf("parseTx error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } switch currTx := sdkTx.(type) { case auth.StdTx: var displayTx DisplayTx for _, msg := range currTx.GetMsgs() { //fmt.Printf("msg|route=%s|type=%s\n", msg.Route(), msg.Type()) switch msg := msg.(type) { case htdfservice.MsgSend: displayTx.From = msg.From displayTx.To = msg.To displayTx.Hash = hex.EncodeToString(tx.Hash()) displayTx.Amount = unit_convert.DefaultCoinsToBigCoins(msg.Amount) displayTx.Memo = currTx.Memo blockInfo.Block.Txs = append(blockInfo.Block.Txs, displayTx) //fmt.Printf("msg|from=%s|to=%s\n", msg.From, msg.To) default: fmt.Printf("ignore type|type=%s|route=%s\n", msg.Type(), msg.Route()) continue } } default: fmt.Printf("unknown type: %+v\n", currTx) } } sdkRest.PostProcessResponse(w, cdc, &blockInfo, cliCtx.Indent) } } func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) { var tx auth.StdTx err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return nil, err } return tx, nil } func formatTxResult(cdc *codec.Codec, res *ctypes.ResultTx, resBlock *ctypes.ResultBlock) (sdk.TxResponse, error) { tx, err := parseTx(cdc, res.Tx) if err != nil { return sdk.TxResponse{}, err } return sdk.NewResponseResultTx(res, tx, resBlock.Block.Time.Format(time.RFC3339)), nil } // func GetTxFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r
{ node, err := cliCtx.GetNode() if err != nil { return -1, err } status, err := node.Status() if err != nil { return -1, err } height := status.SyncInfo.LatestBlockHeight return height, nil }
identifier_body
block.go
Flag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode)) return cmd } func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) { // get the node node, err := cliCtx.GetNode() if err != nil { return nil, err } // header -> BlockchainInfo // header, tx -> Block // results -> BlockResults res, err := node.Block(height) if err != nil { return nil, err } if !cliCtx.TrustNode { check, err := cliCtx.Verify(res.Block.Height) if err != nil { return nil, err } err = tmliteProxy.ValidateBlockMeta(res.BlockMeta, check) if err != nil { return nil, err } err = tmliteProxy.ValidateBlock(res.Block, check) if err != nil { return nil, err } } if cliCtx.Indent { return cdc.MarshalJSONIndent(res, "", " ") } return cdc.MarshalJSON(res) } // get the current blockchain height func GetChainHeight(cliCtx context.CLIContext) (int64, error) { node, err := cliCtx.GetNode() if err != nil { return -1, err } status, err := node.Status() if err != nil { return -1, err } height := status.SyncInfo.LatestBlockHeight return height, nil } // CMD func printBlock(cmd *cobra.Command, args []string) error { var height *int64 // optional height if len(args) > 0 { h, err := strconv.Atoi(args[0]) if err != nil { return err } if h > 0 { tmp := int64(h) height = &tmp } } output, err := getBlock(context.NewCLIContext(), height) if err != nil { return err } fmt.Println(string(output)) return nil } // REST // REST handler to get a block func
(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, "ERROR: Couldn't parse block height. Assumed format is '/block/{height}'.") return } chainHeight, err := GetChainHeight(cliCtx) if height > chainHeight { rest.WriteErrorResponse(w, http.StatusNotFound, "ERROR: Requested block height is bigger then the chain length.") return } output, err := getBlock(cliCtx, &height) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, output, cliCtx.Indent) } } // REST handler to get the latest block func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { height, err := GetChainHeight(cliCtx) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } output, err := getBlock(cliCtx, &height) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, output, cliCtx.Indent) } } //BlockDetails struct type DisplayTx struct { From sdk.AccAddress To sdk.AccAddress Amount []sdk.BigCoin Hash string Memo string Data string } type DisplayBlock struct { Txs []DisplayTx `json:"txs"` Evidence tmTypes.EvidenceData `json:"evidence"` LastCommit *tmTypes.Commit `json:"last_commit"` } type BlockInfo struct { BlockMeta *tmTypes.BlockMeta `json:"block_meta"` Block DisplayBlock `json:"block"` Time string `json:"time"` } type DisplayFee struct { // Amount []sdk.BigCoin `json:"amount"` // junying-todo, 2019-12-07 GasWanted string `json:"gas_wanted"` GasPrice string `json:"gas_price"` } type StdTx struct { Msgs []DisplayTx `json:"msg"` Fee DisplayFee `json:"fee"` Signatures []auth.StdSignature `json:"signatures"` Memo string `json:"memo"` } type GetTxResponse struct { Height int64 `json:"height"` TxHash string `json:"txhash"` Code uint32 `json:"code,omitempty"` Log sdk.ABCIMessageLogs `json:"log,omitempty"` Info string `json:"info,omitempty"` GasWanted int64 `json:"gas_wanted,omitempty"` GasUsed int64 `json:"gas_used,omitempty"` Tags sdk.StringTags `json:"tags,omitempty"` Codespace string `json:"codespace,omitempty"` Tx StdTx `json:"tx,omitempty"` // Data string `json:"data,omitempty"` } // GetBlockDetailFn func GetBlockDetailFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/block/{height}'.")) return } chainHeight, err := GetChainHeight(cliCtx) if height > chainHeight { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("ERROR: Requested block height is bigger then the chain length.")) return } // get the node node, err := cliCtx.GetNode() if err != nil { fmt.Printf("getNode error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } var blockInfo BlockInfo //get Block info resultBlock, err := node.Block(&height) if err != nil { fmt.Printf("get block error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } blockInfo.BlockMeta = resultBlock.BlockMeta blockInfo.Block.Evidence = resultBlock.Block.Evidence blockInfo.Block.LastCommit = resultBlock.Block.LastCommit blockInfo.Time = resultBlock.BlockMeta.Header.Time.Local().Format("2006-01-02 15:04:05") for _, tx := range resultBlock.Block.Data.Txs { sdkTx, err := parseTx(cdc, tx) if err != nil { fmt.Printf("parseTx error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } switch currTx := sdkTx.(type) { case auth.StdTx: var displayTx DisplayTx for _, msg := range currTx.GetMsgs() { //fmt.Printf("msg|route=%s|type=%s\n", msg.Route(), msg.Type()) switch msg := msg.(type) { case htdfservice.MsgSend: displayTx.From = msg.From displayTx.To = msg.To displayTx.Hash = hex.EncodeToString(tx.Hash()) displayTx.Amount = unit_convert.DefaultCoinsToBigCoins(msg.Amount) displayTx.Memo = currTx.Memo blockInfo.Block.Txs = append(blockInfo.Block.Txs, displayTx) //fmt.Printf("msg|from=%s|to=%s\n", msg.From, msg.To) default: fmt.Printf("ignore type|type=%s|route=%s\n", msg.Type(), msg.Route()) continue } } default: fmt.Printf("unknown type: %+v\n", currTx) } } sdkRest.PostProcessResponse(w, cdc, &blockInfo, cliCtx.Indent) } } func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) { var tx auth.StdTx err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return nil, err } return tx, nil } func formatTxResult(cdc *codec.Codec, res *ctypes.ResultTx, resBlock *ctypes.ResultBlock) (sdk.TxResponse, error) { tx, err := parseTx(cdc, res.Tx) if err != nil { return sdk.TxResponse{}, err } return sdk.NewResponseResultTx(res, tx, resBlock.Block.Time.Format(time.RFC3339)), nil } // func GetTxFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r
BlockRequestHandlerFn
identifier_name
block.go
(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode)) return cmd } func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) { // get the node node, err := cliCtx.GetNode() if err != nil { return nil, err } // header -> BlockchainInfo // header, tx -> Block // results -> BlockResults res, err := node.Block(height) if err != nil { return nil, err } if !cliCtx.TrustNode { check, err := cliCtx.Verify(res.Block.Height) if err != nil { return nil, err } err = tmliteProxy.ValidateBlockMeta(res.BlockMeta, check) if err != nil { return nil, err } err = tmliteProxy.ValidateBlock(res.Block, check) if err != nil { return nil, err } } if cliCtx.Indent { return cdc.MarshalJSONIndent(res, "", " ") } return cdc.MarshalJSON(res) } // get the current blockchain height func GetChainHeight(cliCtx context.CLIContext) (int64, error) { node, err := cliCtx.GetNode() if err != nil { return -1, err } status, err := node.Status() if err != nil { return -1, err } height := status.SyncInfo.LatestBlockHeight return height, nil } // CMD func printBlock(cmd *cobra.Command, args []string) error { var height *int64 // optional height if len(args) > 0 { h, err := strconv.Atoi(args[0]) if err != nil { return err } if h > 0 { tmp := int64(h) height = &tmp } } output, err := getBlock(context.NewCLIContext(), height) if err != nil { return err } fmt.Println(string(output)) return nil } // REST // REST handler to get a block func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, "ERROR: Couldn't parse block height. Assumed format is '/block/{height}'.") return } chainHeight, err := GetChainHeight(cliCtx) if height > chainHeight { rest.WriteErrorResponse(w, http.StatusNotFound, "ERROR: Requested block height is bigger then the chain length.") return } output, err := getBlock(cliCtx, &height) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, output, cliCtx.Indent) } } // REST handler to get the latest block func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { height, err := GetChainHeight(cliCtx) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } output, err := getBlock(cliCtx, &height) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } rest.PostProcessResponse(w, cdc, output, cliCtx.Indent) } } //BlockDetails struct type DisplayTx struct { From sdk.AccAddress To sdk.AccAddress Amount []sdk.BigCoin Hash string Memo string Data string } type DisplayBlock struct { Txs []DisplayTx `json:"txs"` Evidence tmTypes.EvidenceData `json:"evidence"` LastCommit *tmTypes.Commit `json:"last_commit"` } type BlockInfo struct { BlockMeta *tmTypes.BlockMeta `json:"block_meta"` Block DisplayBlock `json:"block"` Time string `json:"time"` } type DisplayFee struct { // Amount []sdk.BigCoin `json:"amount"` // junying-todo, 2019-12-07 GasWanted string `json:"gas_wanted"` GasPrice string `json:"gas_price"` } type StdTx struct { Msgs []DisplayTx `json:"msg"` Fee DisplayFee `json:"fee"` Signatures []auth.StdSignature `json:"signatures"` Memo string `json:"memo"` } type GetTxResponse struct { Height int64 `json:"height"` TxHash string `json:"txhash"` Code uint32 `json:"code,omitempty"` Log sdk.ABCIMessageLogs `json:"log,omitempty"` Info string `json:"info,omitempty"` GasWanted int64 `json:"gas_wanted,omitempty"` GasUsed int64 `json:"gas_used,omitempty"` Tags sdk.StringTags `json:"tags,omitempty"` Codespace string `json:"codespace,omitempty"` Tx StdTx `json:"tx,omitempty"` // Data string `json:"data,omitempty"` } // GetBlockDetailFn func GetBlockDetailFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/block/{height}'.")) return } chainHeight, err := GetChainHeight(cliCtx) if height > chainHeight { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("ERROR: Requested block height is bigger then the chain length.")) return } // get the node node, err := cliCtx.GetNode() if err != nil { fmt.Printf("getNode error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } var blockInfo BlockInfo //get Block info resultBlock, err := node.Block(&height) if err != nil { fmt.Printf("get block error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } blockInfo.BlockMeta = resultBlock.BlockMeta blockInfo.Block.Evidence = resultBlock.Block.Evidence blockInfo.Block.LastCommit = resultBlock.Block.LastCommit blockInfo.Time = resultBlock.BlockMeta.Header.Time.Local().Format("2006-01-02 15:04:05") for _, tx := range resultBlock.Block.Data.Txs { sdkTx, err := parseTx(cdc, tx) if err != nil { fmt.Printf("parseTx error|err=%s\n", err) sdkRest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } switch currTx := sdkTx.(type) { case auth.StdTx: var displayTx DisplayTx for _, msg := range currTx.GetMsgs()
default: fmt.Printf("unknown type: %+v\n", currTx) } } sdkRest.PostProcessResponse(w, cdc, &blockInfo, cliCtx.Indent) } } func parseTx(cdc *codec.Codec, txBytes []byte) (sdk.Tx, error) { var tx auth.StdTx err := cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx) if err != nil { return nil, err } return tx, nil } func formatTxResult(cdc *codec.Codec, res *ctypes.ResultTx, resBlock *ctypes.ResultBlock) (sdk.TxResponse, error) { tx, err := parseTx(cdc, res.Tx) if err != nil { return sdk.TxResponse{}, err } return sdk.NewResponseResultTx(res, tx, resBlock.Block.Time.Format(time.RFC3339)), nil } // func GetTxFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r
{ //fmt.Printf("msg|route=%s|type=%s\n", msg.Route(), msg.Type()) switch msg := msg.(type) { case htdfservice.MsgSend: displayTx.From = msg.From displayTx.To = msg.To displayTx.Hash = hex.EncodeToString(tx.Hash()) displayTx.Amount = unit_convert.DefaultCoinsToBigCoins(msg.Amount) displayTx.Memo = currTx.Memo blockInfo.Block.Txs = append(blockInfo.Block.Txs, displayTx) //fmt.Printf("msg|from=%s|to=%s\n", msg.From, msg.To) default: fmt.Printf("ignore type|type=%s|route=%s\n", msg.Type(), msg.Route()) continue } }
conditional_block
benchmarking.py
_nets('trained_networks/final*') full_net = load_nets('trained_networks/full*') with open('trained_networks/decision_tree.pkl', 'rb') as pkl: dtr_full = pickle.load(pkl)[0] with open('ligpy_benchmarking_files/ligpy_args.txt', 'rb') as args: ligpy_args = args.readlines() ligpy_args = ligpy_args[1:] # reset the random seed generator np.random.seed() rand_sample = np.random.randint(0, 199999) # the row of input data to use in tests rand_input = x_train[rand_sample:rand_sample+1, :] def predict_full_net(input_data=rand_input, net=full_net['all']): """ Predict the output measures using the network trained on all 30 output measures at once. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. net : nolearn.lasagne.base.NeuralNet, optional the trained neural net for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ return y_scaler.inverse_transform(net.predict(input_data)) def predict_single_net(input_data=rand_input, net=nets[5]): """ Predict the value for a single output measure. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. net : nolearn.lasagne.base.NeuralNet, optional a trained neural net for a single output measure Returns ------- predicted : numpy.ndarray an array of the predicted values """ pred = net.predict(input_data) transform_mat[:, 5] = pred.ravel() return y_scaler.inverse_transform(transform_mat)[:, 5] def predict_30_single_nets(input_data=rand_input, nets=nets): """ Predict the output measures using 30 individually trained neural nets. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. nets : dict, optional dictionary with the trained neural nets for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ predicted = np.zeros((input_data.shape[0], 30)) for i in nets.keys(): predicted[:, i] = nets[i].predict(input_data).ravel() return y_scaler.inverse_transform(predicted) def predict_decision_tree(input_data=rand_input, tree=dtr_full): """ Predict the output measures using a decision tree trained on all 30 output measures at once. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. tree : sklearn.tree.tree.DecisionTreeRegressor, optional the trained decision tree for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ return y_scaler.inverse_transform(tree.predict(input_data)) def get_random_ligpy_args(): """ Get the arguments from a random row of ligpy_args to pass to predict_ligpy. Returns ------- end_time output_time_step cool_time initial_T heat_rate maximum_T plant """ rand_index = np.random.randint(0, 249999) args = ligpy_args[rand_index] end_time = float(args.split(' ')[0]) output_time_step = float(args.split(' ')[1]) cool_time = int(args.split(' ')[2]) initial_T = float(args.split(' ')[3]) heat_rate = float(args.split(' ')[4]) maximum_T = float(args.split(' ')[5]) plant = str(args.split(' ')[8]).rstrip() return (end_time, output_time_step, cool_time, initial_T, heat_rate, maximum_T, plant) # these need to be defined before the next function, but they will be # overwritten (end_time, output_time_step, cool_time, initial_T, heat_rate, maximum_T, plant) = get_random_ligpy_args() def setup_predict_ligpy(end_time=end_time, output_time_step=output_time_step, cool_time=cool_time, initial_T=initial_T, heat_rate=heat_rate, maximum_T=maximum_T, plant=plant):
(file_completereactionlist, file_completerateconstantlist, file_compositionlist) = utils.set_paths() working_directory = 'results_dir' if not os.path.exists(working_directory): os.makedirs(working_directory) # pickle the arguments used for this program to reference during analysis prog_params = [end_time, output_time_step, initial_T, heat_rate, maximum_T, absolute_tolerance, relative_tolerance, plant, cool_time] with open('%s/prog_params.pkl' % working_directory, 'wb') as pkl: pickle.dump(prog_params, pkl) # Get lists of all the species in the kinetic scheme and their indices specieslist = utils.get_specieslist(file_completereactionlist) # Get kmatrix kmatrix = utils.build_k_matrix(file_completerateconstantlist) # Set the initial composition of the lignin polymer PLIGC_0, PLIGH_0, PLIGO_0 = utils.define_initial_composition( file_compositionlist, plant) # Set the initial conditions for the DDASAC solver y0_ddasac = np.zeros(len(specieslist)) y0_ddasac[:3] = [PLIGC_0, PLIGH_0, PLIGO_0] return (file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) def teardown_predict_ligpy(): """ Clean up after running predict_ligpy(). Parameters ---------- None Returns ------- None """ call('rm -rf bsub.c bsub.o ddat.in fort.11 f.out greg10.in jacobian.c ' 'jacobian.o model.c model.o net_rates.def parest rates.def ' 'results_dir/', shell=True) # These must be defined to load the module (file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) = setup_predict_ligpy() def predict_ligpy(file_completereactionlist=file_completereactionlist, kmatrix=kmatrix, working_directory=working_directory, y0_ddasac=y0_ddasac, specieslist=specieslist, absolute_tolerance=absolute_tolerance, relative_tolerance=relative_tolerance, initial_T=initial_T, heat_rate=heat_rate, end_time=end_time, maximum_T=maximum_T, output_time_step=output_time_step, cool_time=cool_time): """ This function is a modified version of `ligpy.py` in the `ligpy` package. It sets up and solves the ODE model for lignin pyrolysis, then calculates the set of outputs that are predicted by the machine learning models developed in `lignet`. Parameters ---------- standard arguments passed to `ligpy.py` Returns ------- None """ # Solve the model with DDASAC ddasac.run_ddasac(file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) # Load the program parameters and results from the selected folder (end_time, output_time_step, initial_T, heating_rate, max_T, atol, rtol, plant, cool_time, y, t, T, specieslist, speciesindices, indices_to_species) = load_results('.') # create a new matrix of mass fractions (instead of concentrations) m = copy.deepcopy(y) for species in specieslist: # make an array of mass concentration (g/L) m[:, speciesindices[species]] = (y[:, speciesindices[species]] * MW[species][0]) generate_report(spec
""" Create the proper environment to run predict_ligpy() and set up the kinetic model. Parameters ---------- standard arguments passed to `ligpy.py` Returns ------- standard arguments for `ddasac.run_ddasac()` """ call('cp ligpy_benchmarking_files/sa_compositionlist.dat ' '../../../ligpy/ligpy/data/compositionlist.dat;', shell=True ) absolute_tolerance = float(1e-10) relative_tolerance = float(1e-8) # These are the files and paths that will be referenced in this program:
identifier_body
benchmarking.py
ets('trained_networks/final*') full_net = load_nets('trained_networks/full*') with open('trained_networks/decision_tree.pkl', 'rb') as pkl: dtr_full = pickle.load(pkl)[0] with open('ligpy_benchmarking_files/ligpy_args.txt', 'rb') as args: ligpy_args = args.readlines() ligpy_args = ligpy_args[1:] # reset the random seed generator np.random.seed() rand_sample = np.random.randint(0, 199999) # the row of input data to use in tests rand_input = x_train[rand_sample:rand_sample+1, :] def predict_full_net(input_data=rand_input, net=full_net['all']): """ Predict the output measures using the network trained on all 30 output measures at once. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. net : nolearn.lasagne.base.NeuralNet, optional the trained neural net for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ return y_scaler.inverse_transform(net.predict(input_data)) def predict_single_net(input_data=rand_input, net=nets[5]): """ Predict the value for a single output measure. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. net : nolearn.lasagne.base.NeuralNet, optional a trained neural net for a single output measure Returns ------- predicted : numpy.ndarray an array of the predicted values """ pred = net.predict(input_data) transform_mat[:, 5] = pred.ravel() return y_scaler.inverse_transform(transform_mat)[:, 5] def predict_30_single_nets(input_data=rand_input, nets=nets): """ Predict the output measures using 30 individually trained neural nets. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. nets : dict, optional dictionary with the trained neural nets for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ predicted = np.zeros((input_data.shape[0], 30)) for i in nets.keys():
return y_scaler.inverse_transform(predicted) def predict_decision_tree(input_data=rand_input, tree=dtr_full): """ Predict the output measures using a decision tree trained on all 30 output measures at once. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. tree : sklearn.tree.tree.DecisionTreeRegressor, optional the trained decision tree for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ return y_scaler.inverse_transform(tree.predict(input_data)) def get_random_ligpy_args(): """ Get the arguments from a random row of ligpy_args to pass to predict_ligpy. Returns ------- end_time output_time_step cool_time initial_T heat_rate maximum_T plant """ rand_index = np.random.randint(0, 249999) args = ligpy_args[rand_index] end_time = float(args.split(' ')[0]) output_time_step = float(args.split(' ')[1]) cool_time = int(args.split(' ')[2]) initial_T = float(args.split(' ')[3]) heat_rate = float(args.split(' ')[4]) maximum_T = float(args.split(' ')[5]) plant = str(args.split(' ')[8]).rstrip() return (end_time, output_time_step, cool_time, initial_T, heat_rate, maximum_T, plant) # these need to be defined before the next function, but they will be # overwritten (end_time, output_time_step, cool_time, initial_T, heat_rate, maximum_T, plant) = get_random_ligpy_args() def setup_predict_ligpy(end_time=end_time, output_time_step=output_time_step, cool_time=cool_time, initial_T=initial_T, heat_rate=heat_rate, maximum_T=maximum_T, plant=plant): """ Create the proper environment to run predict_ligpy() and set up the kinetic model. Parameters ---------- standard arguments passed to `ligpy.py` Returns ------- standard arguments for `ddasac.run_ddasac()` """ call('cp ligpy_benchmarking_files/sa_compositionlist.dat ' '../../../ligpy/ligpy/data/compositionlist.dat;', shell=True ) absolute_tolerance = float(1e-10) relative_tolerance = float(1e-8) # These are the files and paths that will be referenced in this program: (file_completereactionlist, file_completerateconstantlist, file_compositionlist) = utils.set_paths() working_directory = 'results_dir' if not os.path.exists(working_directory): os.makedirs(working_directory) # pickle the arguments used for this program to reference during analysis prog_params = [end_time, output_time_step, initial_T, heat_rate, maximum_T, absolute_tolerance, relative_tolerance, plant, cool_time] with open('%s/prog_params.pkl' % working_directory, 'wb') as pkl: pickle.dump(prog_params, pkl) # Get lists of all the species in the kinetic scheme and their indices specieslist = utils.get_specieslist(file_completereactionlist) # Get kmatrix kmatrix = utils.build_k_matrix(file_completerateconstantlist) # Set the initial composition of the lignin polymer PLIGC_0, PLIGH_0, PLIGO_0 = utils.define_initial_composition( file_compositionlist, plant) # Set the initial conditions for the DDASAC solver y0_ddasac = np.zeros(len(specieslist)) y0_ddasac[:3] = [PLIGC_0, PLIGH_0, PLIGO_0] return (file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) def teardown_predict_ligpy(): """ Clean up after running predict_ligpy(). Parameters ---------- None Returns ------- None """ call('rm -rf bsub.c bsub.o ddat.in fort.11 f.out greg10.in jacobian.c ' 'jacobian.o model.c model.o net_rates.def parest rates.def ' 'results_dir/', shell=True) # These must be defined to load the module (file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) = setup_predict_ligpy() def predict_ligpy(file_completereactionlist=file_completereactionlist, kmatrix=kmatrix, working_directory=working_directory, y0_ddasac=y0_ddasac, specieslist=specieslist, absolute_tolerance=absolute_tolerance, relative_tolerance=relative_tolerance, initial_T=initial_T, heat_rate=heat_rate, end_time=end_time, maximum_T=maximum_T, output_time_step=output_time_step, cool_time=cool_time): """ This function is a modified version of `ligpy.py` in the `ligpy` package. It sets up and solves the ODE model for lignin pyrolysis, then calculates the set of outputs that are predicted by the machine learning models developed in `lignet`. Parameters ---------- standard arguments passed to `ligpy.py` Returns ------- None """ # Solve the model with DDASAC ddasac.run_ddasac(file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) # Load the program parameters and results from the selected folder (end_time, output_time_step, initial_T, heating_rate, max_T, atol, rtol, plant, cool_time, y, t, T, specieslist, speciesindices, indices_to_species) = load_results('.') # create a new matrix of mass fractions (instead of concentrations) m = copy.deepcopy(y) for species in specieslist: # make an array of mass concentration (g/L) m[:, speciesindices[species]] = (y[:, speciesindices[species]] * MW[species][0]) generate_report(spec
predicted[:, i] = nets[i].predict(input_data).ravel()
conditional_block
benchmarking.py
ets('trained_networks/final*') full_net = load_nets('trained_networks/full*') with open('trained_networks/decision_tree.pkl', 'rb') as pkl: dtr_full = pickle.load(pkl)[0] with open('ligpy_benchmarking_files/ligpy_args.txt', 'rb') as args: ligpy_args = args.readlines() ligpy_args = ligpy_args[1:] # reset the random seed generator np.random.seed() rand_sample = np.random.randint(0, 199999) # the row of input data to use in tests rand_input = x_train[rand_sample:rand_sample+1, :] def predict_full_net(input_data=rand_input, net=full_net['all']): """ Predict the output measures using the network trained on all 30 output measures at once. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. net : nolearn.lasagne.base.NeuralNet, optional the trained neural net for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ return y_scaler.inverse_transform(net.predict(input_data)) def predict_single_net(input_data=rand_input, net=nets[5]): """ Predict the value for a single output measure. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. net : nolearn.lasagne.base.NeuralNet, optional a trained neural net for a single output measure Returns ------- predicted : numpy.ndarray an array of the predicted values """ pred = net.predict(input_data) transform_mat[:, 5] = pred.ravel() return y_scaler.inverse_transform(transform_mat)[:, 5] def predict_30_single_nets(input_data=rand_input, nets=nets): """ Predict the output measures using 30 individually trained neural nets. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. nets : dict, optional dictionary with the trained neural nets for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ predicted = np.zeros((input_data.shape[0], 30)) for i in nets.keys(): predicted[:, i] = nets[i].predict(input_data).ravel() return y_scaler.inverse_transform(predicted) def predict_decision_tree(input_data=rand_input, tree=dtr_full): """ Predict the output measures using a decision tree trained on all 30 output measures at once. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. tree : sklearn.tree.tree.DecisionTreeRegressor, optional the trained decision tree for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ return y_scaler.inverse_transform(tree.predict(input_data)) def get_random_ligpy_args(): """ Get the arguments from a random row of ligpy_args to pass to predict_ligpy. Returns ------- end_time output_time_step cool_time initial_T heat_rate maximum_T plant """ rand_index = np.random.randint(0, 249999) args = ligpy_args[rand_index] end_time = float(args.split(' ')[0]) output_time_step = float(args.split(' ')[1]) cool_time = int(args.split(' ')[2]) initial_T = float(args.split(' ')[3]) heat_rate = float(args.split(' ')[4]) maximum_T = float(args.split(' ')[5]) plant = str(args.split(' ')[8]).rstrip() return (end_time, output_time_step, cool_time, initial_T, heat_rate, maximum_T, plant) # these need to be defined before the next function, but they will be # overwritten (end_time, output_time_step, cool_time, initial_T, heat_rate, maximum_T, plant) = get_random_ligpy_args() def setup_predict_ligpy(end_time=end_time, output_time_step=output_time_step, cool_time=cool_time, initial_T=initial_T, heat_rate=heat_rate, maximum_T=maximum_T, plant=plant): """ Create the proper environment to run predict_ligpy() and set up the kinetic model. Parameters ---------- standard arguments passed to `ligpy.py` Returns ------- standard arguments for `ddasac.run_ddasac()` """ call('cp ligpy_benchmarking_files/sa_compositionlist.dat ' '../../../ligpy/ligpy/data/compositionlist.dat;', shell=True ) absolute_tolerance = float(1e-10) relative_tolerance = float(1e-8) # These are the files and paths that will be referenced in this program: (file_completereactionlist, file_completerateconstantlist, file_compositionlist) = utils.set_paths() working_directory = 'results_dir' if not os.path.exists(working_directory): os.makedirs(working_directory) # pickle the arguments used for this program to reference during analysis prog_params = [end_time, output_time_step, initial_T, heat_rate, maximum_T, absolute_tolerance, relative_tolerance, plant, cool_time] with open('%s/prog_params.pkl' % working_directory, 'wb') as pkl: pickle.dump(prog_params, pkl) # Get lists of all the species in the kinetic scheme and their indices specieslist = utils.get_specieslist(file_completereactionlist) # Get kmatrix kmatrix = utils.build_k_matrix(file_completerateconstantlist) # Set the initial composition of the lignin polymer PLIGC_0, PLIGH_0, PLIGO_0 = utils.define_initial_composition( file_compositionlist, plant) # Set the initial conditions for the DDASAC solver y0_ddasac = np.zeros(len(specieslist)) y0_ddasac[:3] = [PLIGC_0, PLIGH_0, PLIGO_0] return (file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) def
(): """ Clean up after running predict_ligpy(). Parameters ---------- None Returns ------- None """ call('rm -rf bsub.c bsub.o ddat.in fort.11 f.out greg10.in jacobian.c ' 'jacobian.o model.c model.o net_rates.def parest rates.def ' 'results_dir/', shell=True) # These must be defined to load the module (file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) = setup_predict_ligpy() def predict_ligpy(file_completereactionlist=file_completereactionlist, kmatrix=kmatrix, working_directory=working_directory, y0_ddasac=y0_ddasac, specieslist=specieslist, absolute_tolerance=absolute_tolerance, relative_tolerance=relative_tolerance, initial_T=initial_T, heat_rate=heat_rate, end_time=end_time, maximum_T=maximum_T, output_time_step=output_time_step, cool_time=cool_time): """ This function is a modified version of `ligpy.py` in the `ligpy` package. It sets up and solves the ODE model for lignin pyrolysis, then calculates the set of outputs that are predicted by the machine learning models developed in `lignet`. Parameters ---------- standard arguments passed to `ligpy.py` Returns ------- None """ # Solve the model with DDASAC ddasac.run_ddasac(file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) # Load the program parameters and results from the selected folder (end_time, output_time_step, initial_T, heating_rate, max_T, atol, rtol, plant, cool_time, y, t, T, specieslist, speciesindices, indices_to_species) = load_results('.') # create a new matrix of mass fractions (instead of concentrations) m = copy.deepcopy(y) for species in specieslist: # make an array of mass concentration (g/L) m[:, speciesindices[species]] = (y[:, speciesindices[species]] * MW[species][0]) generate_report
teardown_predict_ligpy
identifier_name
benchmarking.py
_nets('trained_networks/final*') full_net = load_nets('trained_networks/full*') with open('trained_networks/decision_tree.pkl', 'rb') as pkl: dtr_full = pickle.load(pkl)[0] with open('ligpy_benchmarking_files/ligpy_args.txt', 'rb') as args: ligpy_args = args.readlines() ligpy_args = ligpy_args[1:] # reset the random seed generator np.random.seed() rand_sample = np.random.randint(0, 199999) # the row of input data to use in tests rand_input = x_train[rand_sample:rand_sample+1, :] def predict_full_net(input_data=rand_input, net=full_net['all']): """ Predict the output measures using the network trained on all 30 output measures at once. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. net : nolearn.lasagne.base.NeuralNet, optional the trained neural net for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ return y_scaler.inverse_transform(net.predict(input_data)) def predict_single_net(input_data=rand_input, net=nets[5]): """ Predict the value for a single output measure. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. net : nolearn.lasagne.base.NeuralNet, optional a trained neural net for a single output measure Returns ------- predicted : numpy.ndarray an array of the predicted values """ pred = net.predict(input_data) transform_mat[:, 5] = pred.ravel() return y_scaler.inverse_transform(transform_mat)[:, 5] def predict_30_single_nets(input_data=rand_input, nets=nets): """ Predict the output measures using 30 individually trained neural nets. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. nets : dict, optional dictionary with the trained neural nets for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ predicted = np.zeros((input_data.shape[0], 30)) for i in nets.keys(): predicted[:, i] = nets[i].predict(input_data).ravel() return y_scaler.inverse_transform(predicted) def predict_decision_tree(input_data=rand_input, tree=dtr_full): """ Predict the output measures using a decision tree trained on all 30 output measures at once. Parameters ---------- input_data : numpy.ndarray, optional an array of input values to predict. This can be a single row or many rows. tree : sklearn.tree.tree.DecisionTreeRegressor, optional the trained decision tree for all 30 output measures Returns ------- predicted : numpy.ndarray an array of the predicted values """ return y_scaler.inverse_transform(tree.predict(input_data)) def get_random_ligpy_args(): """ Get the arguments from a random row of ligpy_args to pass to predict_ligpy. Returns ------- end_time output_time_step cool_time initial_T heat_rate maximum_T plant """ rand_index = np.random.randint(0, 249999) args = ligpy_args[rand_index] end_time = float(args.split(' ')[0]) output_time_step = float(args.split(' ')[1]) cool_time = int(args.split(' ')[2]) initial_T = float(args.split(' ')[3]) heat_rate = float(args.split(' ')[4]) maximum_T = float(args.split(' ')[5]) plant = str(args.split(' ')[8]).rstrip() return (end_time, output_time_step, cool_time, initial_T, heat_rate, maximum_T, plant) # these need to be defined before the next function, but they will be # overwritten (end_time, output_time_step, cool_time, initial_T, heat_rate, maximum_T, plant) = get_random_ligpy_args() def setup_predict_ligpy(end_time=end_time, output_time_step=output_time_step, cool_time=cool_time, initial_T=initial_T, heat_rate=heat_rate, maximum_T=maximum_T, plant=plant): """ Create the proper environment to run predict_ligpy() and set up the kinetic model. Parameters ---------- standard arguments passed to `ligpy.py` Returns ------- standard arguments for `ddasac.run_ddasac()` """ call('cp ligpy_benchmarking_files/sa_compositionlist.dat '
relative_tolerance = float(1e-8) # These are the files and paths that will be referenced in this program: (file_completereactionlist, file_completerateconstantlist, file_compositionlist) = utils.set_paths() working_directory = 'results_dir' if not os.path.exists(working_directory): os.makedirs(working_directory) # pickle the arguments used for this program to reference during analysis prog_params = [end_time, output_time_step, initial_T, heat_rate, maximum_T, absolute_tolerance, relative_tolerance, plant, cool_time] with open('%s/prog_params.pkl' % working_directory, 'wb') as pkl: pickle.dump(prog_params, pkl) # Get lists of all the species in the kinetic scheme and their indices specieslist = utils.get_specieslist(file_completereactionlist) # Get kmatrix kmatrix = utils.build_k_matrix(file_completerateconstantlist) # Set the initial composition of the lignin polymer PLIGC_0, PLIGH_0, PLIGO_0 = utils.define_initial_composition( file_compositionlist, plant) # Set the initial conditions for the DDASAC solver y0_ddasac = np.zeros(len(specieslist)) y0_ddasac[:3] = [PLIGC_0, PLIGH_0, PLIGO_0] return (file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) def teardown_predict_ligpy(): """ Clean up after running predict_ligpy(). Parameters ---------- None Returns ------- None """ call('rm -rf bsub.c bsub.o ddat.in fort.11 f.out greg10.in jacobian.c ' 'jacobian.o model.c model.o net_rates.def parest rates.def ' 'results_dir/', shell=True) # These must be defined to load the module (file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) = setup_predict_ligpy() def predict_ligpy(file_completereactionlist=file_completereactionlist, kmatrix=kmatrix, working_directory=working_directory, y0_ddasac=y0_ddasac, specieslist=specieslist, absolute_tolerance=absolute_tolerance, relative_tolerance=relative_tolerance, initial_T=initial_T, heat_rate=heat_rate, end_time=end_time, maximum_T=maximum_T, output_time_step=output_time_step, cool_time=cool_time): """ This function is a modified version of `ligpy.py` in the `ligpy` package. It sets up and solves the ODE model for lignin pyrolysis, then calculates the set of outputs that are predicted by the machine learning models developed in `lignet`. Parameters ---------- standard arguments passed to `ligpy.py` Returns ------- None """ # Solve the model with DDASAC ddasac.run_ddasac(file_completereactionlist, kmatrix, working_directory, y0_ddasac, specieslist, absolute_tolerance, relative_tolerance, initial_T, heat_rate, end_time, maximum_T, output_time_step, cool_time) # Load the program parameters and results from the selected folder (end_time, output_time_step, initial_T, heating_rate, max_T, atol, rtol, plant, cool_time, y, t, T, specieslist, speciesindices, indices_to_species) = load_results('.') # create a new matrix of mass fractions (instead of concentrations) m = copy.deepcopy(y) for species in specieslist: # make an array of mass concentration (g/L) m[:, speciesindices[species]] = (y[:, speciesindices[species]] * MW[species][0]) generate_report(spec
'../../../ligpy/ligpy/data/compositionlist.dat;', shell=True ) absolute_tolerance = float(1e-10)
random_line_split
main.py
_at.y # np.arctan(-1) / np.pi return np.arctan(delta_y / delta_x) def __repr__(self): return f'Track: {self.begins_at} -> {self.ends_at}' def geom(self): geom = rendering.Line(self.begins_at.arr(), self.ends_at.arr()) geom.set_color(0, 0, 0) return geom def __contains__(self, station): return station in (self.begins_at, self.ends_at) class Node: def __init__(self, x, y, name=None): self.name = name # self.p = pos self.x = x self.y = y self._tracks = None self._nodes = None def __eq__(self, other): return np.isclose(self.x, other.x) and np.isclose(self.y, other.y) def __hash__(self): return hash((self.x, self.y)) def tracks(self): if not self._tracks: self._tracks = {x for x in World.tracks if x.begins_at == self or x.ends_at == self} return self._tracks def arr(self): return self.x, self.y def geom(self): geom = rendering.make_capsule(length=20, width=20) geom.set_color(1, 0, 0) geom.add_attr(rendering.Transform(translation=self.arr(), rotation=0.0, scale=(1, 1))) geom.add_attr(rendering.Transform(translation=(-10.0, 0), rotation=0.0, scale=(1, 1))) # center the pill return geom def is_next_to(self, other_track): for t in self.tracks(): if other_track in (t.begins_at, t.ends_at): return True return False def __repr__(self): return f'Node: {self.name} ({self.x}, {self.y})' class Train: def __init__(self, track, dist, direction, name): self.on_track = track self.dist_on_track = dist self.direction = direction # -1 = from end to beginning, 1 = from beginning to end self.name = name self.speed = 0.1 width = 45.0 height = 15.0 lef, rig, top, bot = -width / 2, width / 2, height / 2, -height / 2 self.geom = rendering.FilledPolygon([(lef, bot), (lef, top), (rig, top), (rig, bot)]) self.geom.set_color(0, 1, 0) self.translation = rendering.Transform() self.geom.add_attr(self.translation) def go_to(self, station): pass def step(self, direction, next_track, speed): if next_track in self.on_track.begins_at.tracks() or next_track in self.on_track.ends_at.tracks(): if self.pos() == next_track.begins_at: self.dist_on_track = 0 self.on_track = next_track if self.pos() == next_track.ends_at: self.dist_on_track = next_track.track_length self.on_track = next_track self.dist_on_track += direction * speed if self.dist_on_track < 0.0: self.dist_on_track = 0.0 if self.dist_on_track > self.on_track.track_length: self.dist_on_track = self.on_track.track_length # if self.pos() == self.on_track.begins_at: # if next_track in self.on_track.begins_at.tracks(): # # if self.pos() == next_track.begins_at: # self.dist_on_track = 0 # else: # self.dist_on_track = next_track.track_length # self.on_track = next_track # if self.pos() == self.on_track.ends_at: # if next_track in self.on_track.ends_at.tracks(): # if self.pos() == next_track.begins_at: # self.dist_on_track = 0 # else: # self.dist_on_track = next_track.track_length # self.on_track = next_track def
(self) -> Node: delta = self.on_track.get_delta() progress = self.dist_on_track / self.on_track.track_length x = self.on_track.begins_at.x + progress * delta.x y = self.on_track.begins_at.y + progress * delta.y return Node(x=x, y=y) def curr_station(self): stations = [x for x in World.nodes if x == self.pos()] if stations: return stations[0] else: return None def __eq__(self, other): return self.name == other.name def __hash__(self): return self.name.__hash__() class World(gym.Env): metadata = { 'render.modes': ['human', 'rgb_array'], 'video.frames_per_second': 1 } from typing import List, Set tracks: Set[Track] = set() nodes: List[Node] = [] points = set() def __init__(self): self.age = 0 self.viewer = None self.state = None self.done = False self.scr_wid = 900 self.scr_hgt = 700 self.train = None self.train_trans = None self.origin = None self.destination = None def step(self, next_station: Node): if self.done: raise Exception('test is done.') curr_station = [x for x in World.nodes if x == self.train.pos()][0] if curr_station.is_next_to(next_station): track = [x for x in World.tracks if curr_station in x and next_station in x][0] if track.ends_at == next_station: dir = 1 else: dir = -1 self.train.step(dir, track, track.track_length) self.state = { 'current_station': next_station } # self.train.step(direction=action['direction'], next_track=action['next_track'], speed=action['speed']) self.age += 1 self.done = self.destination == self.train.pos() if self.done: reward = 0.0 else: # reward = 1.0 / self.age reward = -1.0 return self.state['current_station'], reward, self.done, {} def reset(self): track = list(self.origin.tracks())[0] if track.ends_at == self.origin: self.train = Train(track=track, dist=track.track_length, direction=1, name='bob') else: self.train = Train(track=track, dist=0.0, direction=1, name='bob') self.state = { 'current_station': self.origin } self.done = False self.close() self.render() return self.state def render(self, mode='human'): if self.viewer is None: self.viewer = rendering.Viewer(self.scr_wid, self.scr_hgt) for track in World.tracks: self.viewer.add_geom(track.geom()) for n in World.nodes: self.viewer.add_geom(n.geom()) self.viewer.add_geom(self.train.geom) self.train.translation.set_rotation(self.train.on_track.get_angle()) self.train.translation.set_translation(self.train.pos().x, self.train.pos().y) return self.viewer.render(return_rgb_array=mode == 'rgb_array') def close(self): if self.viewer: self.viewer.close() self.viewer = None gym.envs.registration.register( id='world-v0', entry_point='main:World', ) if __name__ == '__main__': from main import * import gym import numpy as np from torch.optim.rmsprop import RMSprop na = Node(name='A', x=304.0, y=256.0) nb = Node(name='B', x=539.0, y=365.0) tb = Node(name='Bravo', x=841.0, y=154.0) tc = Node(name='Charlie', x=204.0, y=526.0) td = Node(name='Dingo', x=786.0, y=617.0) tf = Node(name='Foxtrot', x=56.0, y=285.0) tt = Node(name='Tango', x=89.0, y=66.0) tw = Node(name='Whiskey', x=249.0, y=64.0) ra = Track(tc, td, 5) rb = Track(td, tb, 5) rc = Track(tb, nb, 3) rd = Track(td, nb, 3) re = Track(tc, nb, 3) rf = Track(na, nb, 3) rg = Track(na,
pos
identifier_name
main.py
def reset(self): track = list(self.origin.tracks())[0] if track.ends_at == self.origin: self.train = Train(track=track, dist=track.track_length, direction=1, name='bob') else: self.train = Train(track=track, dist=0.0, direction=1, name='bob') self.state = { 'current_station': self.origin } self.done = False self.close() self.render() return self.state def render(self, mode='human'): if self.viewer is None: self.viewer = rendering.Viewer(self.scr_wid, self.scr_hgt) for track in World.tracks: self.viewer.add_geom(track.geom()) for n in World.nodes: self.viewer.add_geom(n.geom()) self.viewer.add_geom(self.train.geom) self.train.translation.set_rotation(self.train.on_track.get_angle()) self.train.translation.set_translation(self.train.pos().x, self.train.pos().y) return self.viewer.render(return_rgb_array=mode == 'rgb_array') def close(self): if self.viewer: self.viewer.close() self.viewer = None gym.envs.registration.register( id='world-v0', entry_point='main:World', ) if __name__ == '__main__': from main import * import gym import numpy as np from torch.optim.rmsprop import RMSprop na = Node(name='A', x=304.0, y=256.0) nb = Node(name='B', x=539.0, y=365.0) tb = Node(name='Bravo', x=841.0, y=154.0) tc = Node(name='Charlie', x=204.0, y=526.0) td = Node(name='Dingo', x=786.0, y=617.0) tf = Node(name='Foxtrot', x=56.0, y=285.0) tt = Node(name='Tango', x=89.0, y=66.0) tw = Node(name='Whiskey', x=249.0, y=64.0) ra = Track(tc, td, 5) rb = Track(td, tb, 5) rc = Track(tb, nb, 3) rd = Track(td, nb, 3) re = Track(tc, nb, 3) rf = Track(na, nb, 3) rg = Track(na, tf, 2) rh = Track(na, tt, 2) ri = Track(na, tw, 2) World.nodes = [na, nb, tb, tc, td, tf, tt, tw] World.tracks = {ra, rb, rc, rd, re, rf, rg, rh, ri} world: World = gym.make('world-v0') world.origin = tw world.destination = td world.reset() world.render() # ================================================= RENDER_FPS = 20 N_INPUTS = len(World.nodes) * 2 # which station now N_OUTPUTS = len(World.nodes) # which station next LEARNING_RATE = 0.1 N_EPISODES = 20 # number of training iterations N_MAX_STEPS = 1000 # max steps per episode N_GAMES_PER_EPISODE = 3 # train the policy every 10 episodes # BATCH_SIZE = 128 # GAMMA = 0.999 EPS_START = 0.9 EPS_END = 0.05 EPS_DECAY = 200 # TARGET_UPDATE = 10 # save_iterations = 10 # save the model every 10 training iterations DISCOUNT_RATE = 0.95 BATCH_SIZE = 128 GAMMA = 0.999 # 2. Build the neural network class NextStationNet(nn.Module): def __init__(self): super(NextStationNet, self).__init__() self.input_layer = nn.Linear(N_INPUTS, 32) self.hidden_layer = nn.Linear(32, 32) # self.hidden_layer.weight = torch.nn.Parameter(torch.tensor([[1.58]])) # self.hidden_layer.bias = torch.nn.Parameter(torch.tensor([-0.14])) self.output_layer = nn.Linear(32, N_OUTPUTS) # self.output_layer.weight = torch.nn.Parameter(torch.tensor([[2.45]])) # self.output_layer.bias = torch.nn.Parameter(torch.tensor([-0.11])) def forward(self, x): x = torch.sigmoid(self.input_layer(x)) x = torch.sigmoid(self.hidden_layer(x)) x = torch.sigmoid(self.output_layer(x)) return x policy_net: NextStationNet = NextStationNet() target_net: NextStationNet = NextStationNet() # DQN(screen_height, screen_width, n_actions).to(device) target_net.load_state_dict(policy_net.state_dict()) target_net.eval() optimizer: RMSprop = RMSprop(policy_net.parameters()) steps_done = 0 # super global variable def select_action(policy_net, from_station, to_station): global steps_done # track instance to one hot in_arr = torch.tensor([0.0] * N_INPUTS) in_arr[World.nodes.index(from_station)] = 1.0 in_arr[len(World.nodes) + World.nodes.index(to_station)] = 1.0 # global steps_done eps_threshold = EPS_END + (EPS_START - EPS_END) * math.exp(-1. * steps_done / EPS_DECAY) steps_done += 1 with torch.no_grad(): prediction = policy_net(in_arr) if random.random() > eps_threshold: max_idx = prediction.max(0)[1] else: max_idx = random.randrange(N_OUTPUTS) target = torch.tensor([0.0] * N_OUTPUTS) target[max_idx] = 1.0 station = World.nodes[max_idx] return station, prediction, target print(f"network topology: {policy_net}") # # run input data forward through network # # track instance to one hot # input_data = torch.tensor([0.0] * N_INPUTS) # input_data[World.nodes.index(world.train.curr_station())] = 1.0 # start at node A # # output = policy_net(input_data) # # # # backpropagate gradient # # target = torch.tensor([0] * N_OUTPUTS) # target[World.nodes.index(td)] = 1 # I want to go to station dingo # # target = torch.tensor([0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]) # # criterion = nn.MSELoss() # loss = criterion(output, target) # policy_net.zero_grad() # loss.backward() # # # update weights and biases # optimizer = optim.SGD(policy_net.parameters(), lr=0.1) # optimizer.step() # # output = policy_net(input_data) # print(f"updated_a_l2 = {round(output.item(), 4)}") def discount_rewards(reward, discount_rate): discounted_rewards = np.empty(len(reward)) cumulative_rewards = 0 for step in reversed(range(len(reward))): cumulative_rewards = reward[step] + cumulative_rewards * discount_rate discounted_rewards[step] = cumulative_rewards return discounted_rewards def discount_and_normalize_rewards(all_rewards, discount_rate): all_discounted_rewards = [discount_rewards(reward, discount_rate) for reward in all_rewards] flat_rewards = np.concatenate(all_discounted_rewards) reward_mean = flat_rewards.mean() reward_std = flat_rewards.std() return [(discounted_rewards - reward_mean) / reward_std for discounted_rewards in all_discounted_rewards] # exploration policy. Try something new or stick with the known. for episode in range(N_EPISODES): print(f"episode: {episode}") all_rewards = [] # all sequences of raw rewards for each episode all_gradients = [] # gradients saved at each step of each episode # all_entropy = [] for game in range(N_GAMES_PER_EPISODE):
print(f" game: {game}") curr_rewards = [] # all raw rewards from the current episode curr_gradients = [] # all gradients from the current episode world.reset() # world.render() # time.sleep(1 / RENDER_FPS) for step in range(N_MAX_STEPS): next_station, prediction, target = select_action(policy_net, from_station=world.train.curr_station(), to_station=world.destination) print(next_station) curr_station, reward, done, info = world.step(next_station=next_station) world.render() time.sleep(1 / RENDER_FPS) curr_rewards += [reward] # curr_gradients += [val_grads] if done:
conditional_block
main.py
return Node(x=delta_x, y=delta_y) def get_angle(self): delta_x = self.ends_at.x - self.begins_at.x delta_y = self.ends_at.y - self.begins_at.y # np.arctan(-1) / np.pi return np.arctan(delta_y / delta_x) def __repr__(self): return f'Track: {self.begins_at} -> {self.ends_at}' def geom(self): geom = rendering.Line(self.begins_at.arr(), self.ends_at.arr()) geom.set_color(0, 0, 0) return geom def __contains__(self, station): return station in (self.begins_at, self.ends_at) class Node: def __init__(self, x, y, name=None): self.name = name # self.p = pos self.x = x self.y = y self._tracks = None self._nodes = None def __eq__(self, other): return np.isclose(self.x, other.x) and np.isclose(self.y, other.y) def __hash__(self): return hash((self.x, self.y)) def tracks(self): if not self._tracks: self._tracks = {x for x in World.tracks if x.begins_at == self or x.ends_at == self} return self._tracks def arr(self): return self.x, self.y def geom(self): geom = rendering.make_capsule(length=20, width=20) geom.set_color(1, 0, 0) geom.add_attr(rendering.Transform(translation=self.arr(), rotation=0.0, scale=(1, 1))) geom.add_attr(rendering.Transform(translation=(-10.0, 0), rotation=0.0, scale=(1, 1))) # center the pill return geom def is_next_to(self, other_track): for t in self.tracks(): if other_track in (t.begins_at, t.ends_at): return True return False def __repr__(self): return f'Node: {self.name} ({self.x}, {self.y})' class Train: def __init__(self, track, dist, direction, name): self.on_track = track self.dist_on_track = dist self.direction = direction # -1 = from end to beginning, 1 = from beginning to end self.name = name self.speed = 0.1 width = 45.0 height = 15.0 lef, rig, top, bot = -width / 2, width / 2, height / 2, -height / 2 self.geom = rendering.FilledPolygon([(lef, bot), (lef, top), (rig, top), (rig, bot)]) self.geom.set_color(0, 1, 0) self.translation = rendering.Transform() self.geom.add_attr(self.translation) def go_to(self, station): pass def step(self, direction, next_track, speed): if next_track in self.on_track.begins_at.tracks() or next_track in self.on_track.ends_at.tracks(): if self.pos() == next_track.begins_at: self.dist_on_track = 0 self.on_track = next_track if self.pos() == next_track.ends_at: self.dist_on_track = next_track.track_length self.on_track = next_track self.dist_on_track += direction * speed if self.dist_on_track < 0.0: self.dist_on_track = 0.0 if self.dist_on_track > self.on_track.track_length: self.dist_on_track = self.on_track.track_length # if self.pos() == self.on_track.begins_at: # if next_track in self.on_track.begins_at.tracks(): # # if self.pos() == next_track.begins_at: # self.dist_on_track = 0 # else: # self.dist_on_track = next_track.track_length # self.on_track = next_track # if self.pos() == self.on_track.ends_at: # if next_track in self.on_track.ends_at.tracks(): # if self.pos() == next_track.begins_at: # self.dist_on_track = 0 # else: # self.dist_on_track = next_track.track_length # self.on_track = next_track def pos(self) -> Node: delta = self.on_track.get_delta() progress = self.dist_on_track / self.on_track.track_length x = self.on_track.begins_at.x + progress * delta.x y = self.on_track.begins_at.y + progress * delta.y return Node(x=x, y=y) def curr_station(self): stations = [x for x in World.nodes if x == self.pos()] if stations: return stations[0] else: return None def __eq__(self, other): return self.name == other.name def __hash__(self): return self.name.__hash__() class World(gym.Env): metadata = { 'render.modes': ['human', 'rgb_array'], 'video.frames_per_second': 1 } from typing import List, Set tracks: Set[Track] = set() nodes: List[Node] = [] points = set() def __init__(self): self.age = 0 self.viewer = None self.state = None self.done = False self.scr_wid = 900 self.scr_hgt = 700 self.train = None self.train_trans = None self.origin = None self.destination = None def step(self, next_station: Node): if self.done: raise Exception('test is done.') curr_station = [x for x in World.nodes if x == self.train.pos()][0] if curr_station.is_next_to(next_station): track = [x for x in World.tracks if curr_station in x and next_station in x][0] if track.ends_at == next_station: dir = 1 else: dir = -1 self.train.step(dir, track, track.track_length) self.state = { 'current_station': next_station } # self.train.step(direction=action['direction'], next_track=action['next_track'], speed=action['speed']) self.age += 1 self.done = self.destination == self.train.pos() if self.done: reward = 0.0 else: # reward = 1.0 / self.age reward = -1.0 return self.state['current_station'], reward, self.done, {} def reset(self): track = list(self.origin.tracks())[0] if track.ends_at == self.origin: self.train = Train(track=track, dist=track.track_length, direction=1, name='bob') else: self.train = Train(track=track, dist=0.0, direction=1, name='bob') self.state = { 'current_station': self.origin } self.done = False self.close() self.render() return self.state def render(self, mode='human'): if self.viewer is None: self.viewer = rendering.Viewer(self.scr_wid, self.scr_hgt) for track in World.tracks: self.viewer.add_geom(track.geom()) for n in World.nodes: self.viewer.add_geom(n.geom()) self.viewer.add_geom(self.train.geom) self.train.translation.set_rotation(self.train.on_track.get_angle()) self.train.translation.set_translation(self.train.pos().x, self.train.pos().y) return self.viewer.render(return_rgb_array=mode == 'rgb_array') def close(self): if self.viewer: self.viewer.close() self.viewer = None gym.envs.registration.register( id='world-v0', entry_point='main:World', ) if __name__ == '__main__': from main import * import gym import numpy as np from torch.optim.rmsprop import RMSprop na = Node(name='A', x=304.0, y=256.0) nb = Node(name='B', x=539.0, y=365.0) tb = Node(name='Bravo', x=841.0, y=154.0) tc = Node(name='Charlie', x=204.0, y=526.0) td = Node(name='Dingo', x=786.0, y=617.0) tf = Node(name='Foxtrot', x=56.0, y=285.0) tt = Node(name='Tango', x=89.0, y=66.0) tw = Node(name='Whiskey', x=249.0, y=64.0) ra = Track(tc, td, 5)
delta_y = self.ends_at.y - self.begins_at.y
random_line_split
main.py
# Called with either one element to determine next action, or a batch # during optimization. Returns tensor([[left0exp,right0exp]...]). def forward(self, x): x = F.relu(self.bn1(self.conv1(x))) x = F.relu(self.bn2(self.conv2(x))) x = F.relu(self.bn3(self.conv3(x))) return self.head(x.view(x.size(0), -1)) Transition = namedtuple('Transition', ('state', 'action', 'next_state', 'reward')) class ReplayMemory(object): def __init__(self, capacity): self.capacity = capacity self.memory = [] self.position = 0 def push(self, *args): """Saves a transition.""" if len(self.memory) < self.capacity: self.memory.append(None) self.memory[self.position] = Transition(*args) self.position = (self.position + 1) % self.capacity def sample(self, batch_size): return random.sample(self.memory, batch_size) def __len__(self): return len(self.memory) class Track: def __init__(self, a, b, track_length): self.begins_at = a self.ends_at = b self.track_length = track_length def __eq__(self, other): return self.begins_at == other.begins_at and self.ends_at == other.ends_at def __hash__(self): return hash((self.begins_at.__hash__(), self.ends_at.__hash__())) def get_delta(self): delta_x = self.ends_at.x - self.begins_at.x delta_y = self.ends_at.y - self.begins_at.y return Node(x=delta_x, y=delta_y) def get_angle(self): delta_x = self.ends_at.x - self.begins_at.x delta_y = self.ends_at.y - self.begins_at.y # np.arctan(-1) / np.pi return np.arctan(delta_y / delta_x) def __repr__(self): return f'Track: {self.begins_at} -> {self.ends_at}' def geom(self): geom = rendering.Line(self.begins_at.arr(), self.ends_at.arr()) geom.set_color(0, 0, 0) return geom def __contains__(self, station): return station in (self.begins_at, self.ends_at) class Node: def __init__(self, x, y, name=None): self.name = name # self.p = pos self.x = x self.y = y self._tracks = None self._nodes = None def __eq__(self, other): return np.isclose(self.x, other.x) and np.isclose(self.y, other.y) def __hash__(self): return hash((self.x, self.y)) def tracks(self): if not self._tracks: self._tracks = {x for x in World.tracks if x.begins_at == self or x.ends_at == self} return self._tracks def arr(self): return self.x, self.y def geom(self): geom = rendering.make_capsule(length=20, width=20) geom.set_color(1, 0, 0) geom.add_attr(rendering.Transform(translation=self.arr(), rotation=0.0, scale=(1, 1))) geom.add_attr(rendering.Transform(translation=(-10.0, 0), rotation=0.0, scale=(1, 1))) # center the pill return geom def is_next_to(self, other_track): for t in self.tracks(): if other_track in (t.begins_at, t.ends_at): return True return False def __repr__(self): return f'Node: {self.name} ({self.x}, {self.y})' class Train: def __init__(self, track, dist, direction, name): self.on_track = track self.dist_on_track = dist self.direction = direction # -1 = from end to beginning, 1 = from beginning to end self.name = name self.speed = 0.1 width = 45.0 height = 15.0 lef, rig, top, bot = -width / 2, width / 2, height / 2, -height / 2 self.geom = rendering.FilledPolygon([(lef, bot), (lef, top), (rig, top), (rig, bot)]) self.geom.set_color(0, 1, 0) self.translation = rendering.Transform() self.geom.add_attr(self.translation) def go_to(self, station): pass def step(self, direction, next_track, speed): if next_track in self.on_track.begins_at.tracks() or next_track in self.on_track.ends_at.tracks(): if self.pos() == next_track.begins_at: self.dist_on_track = 0 self.on_track = next_track if self.pos() == next_track.ends_at: self.dist_on_track = next_track.track_length self.on_track = next_track self.dist_on_track += direction * speed if self.dist_on_track < 0.0: self.dist_on_track = 0.0 if self.dist_on_track > self.on_track.track_length: self.dist_on_track = self.on_track.track_length # if self.pos() == self.on_track.begins_at: # if next_track in self.on_track.begins_at.tracks(): # # if self.pos() == next_track.begins_at: # self.dist_on_track = 0 # else: # self.dist_on_track = next_track.track_length # self.on_track = next_track # if self.pos() == self.on_track.ends_at: # if next_track in self.on_track.ends_at.tracks(): # if self.pos() == next_track.begins_at: # self.dist_on_track = 0 # else: # self.dist_on_track = next_track.track_length # self.on_track = next_track def pos(self) -> Node: delta = self.on_track.get_delta() progress = self.dist_on_track / self.on_track.track_length x = self.on_track.begins_at.x + progress * delta.x y = self.on_track.begins_at.y + progress * delta.y return Node(x=x, y=y) def curr_station(self): stations = [x for x in World.nodes if x == self.pos()] if stations: return stations[0] else: return None def __eq__(self, other): return self.name == other.name def __hash__(self): return self.name.__hash__() class World(gym.Env): metadata = { 'render.modes': ['human', 'rgb_array'], 'video.frames_per_second': 1 } from typing import List, Set tracks: Set[Track] = set() nodes: List[Node] = [] points = set() def __init__(self): self.age = 0 self.viewer = None self.state = None self.done = False self.scr_wid = 900 self.scr_hgt = 700 self.train = None self.train_trans = None self.origin = None self.destination = None def step(self, next_station: Node): if self.done: raise Exception('test is done.') curr_station = [x for x in World.nodes if x == self.train.pos()][0] if curr_station.is_next_to(next_station): track = [x for x in World.tracks if curr_station in x and next_station in x][0] if track.ends_at == next_station: dir = 1 else: dir = -1 self.train.step(dir, track, track.track_length)
super(DQN, self).__init__() self.conv1 = nn.Conv2d(3, 16, kernel_size=5, stride=2) self.bn1 = nn.BatchNorm2d(16) self.conv2 = nn.Conv2d(16, 32, kernel_size=5, stride=2) self.bn2 = nn.BatchNorm2d(32) self.conv3 = nn.Conv2d(32, 32, kernel_size=5, stride=2) self.bn3 = nn.BatchNorm2d(32) # Number of Linear input connections depends on output of conv2d layers # and therefore the input image size, so compute it. def conv2d_size_out(size, kernel_size = 5, stride = 2): return (size - (kernel_size - 1) - 1) // stride + 1 convw = conv2d_size_out(conv2d_size_out(conv2d_size_out(w))) convh = conv2d_size_out(conv2d_size_out(conv2d_size_out(h))) linear_input_size = convw * convh * 32 self.head = nn.Linear(linear_input_size, outputs)
identifier_body
test_expand.rs
], ); let keys = hex!("6920e299a5202a6d656e636869746f2a"); check( unsafe { &expand_key(&keys) }, &[ [0x6920e299a5202a6d, 0x656e636869746f2a], [0xfa8807605fa82d0d, 0x3ac64e6553b2214f], [0xcf75838d90ddae80, 0xaa1be0e5f9a9c1aa], [0x180d2f1488d08194, 0x22cb6171db62a0db], [0xbaed96ad323d1739, 0x10f67648cb94d693], [0x881b4ab2ba265d8b, 0xaad02bc36144fd50], [0xb34f195d096944d6, 0xa3b96f15c2fd9245], [0xa7007778ae6933ae, 0x0dd05cbbcf2dcefe], [0xff8bccf251e2ff5c, 0x5c32a3e7931f6d19], [0x24b7182e7555e772, 0x29674495ba78298c], [0xae127cdadb479ba8, 0xf220df3d4858f6b1], ], ); let keys = hex!("2b7e151628aed2a6abf7158809cf4f3c"); check( unsafe { &expand_key(&keys) }, &[ [0x2b7e151628aed2a6, 0xabf7158809cf4f3c], [0xa0fafe1788542cb1, 0x23a339392a6c7605], [0xf2c295f27a96b943, 0x5935807a7359f67f], [0x3d80477d4716fe3e, 0x1e237e446d7a883b], [0xef44a541a8525b7f, 0xb671253bdb0bad00], [0xd4d1c6f87c839d87, 0xcaf2b8bc11f915bc], [0x6d88a37a110b3efd, 0xdbf98641ca0093fd], [0x4e54f70e5f5fc9f3, 0x84a64fb24ea6dc4f], [0xead27321b58dbad2, 0x312bf5607f8d292f], [0xac7766f319fadc21, 0x28d12941575c006e], [0xd014f9a8c9ee2589, 0xe13f0cc8b6630ca6], ], ); } #[test] fn aes192_expand_key_test() { use super::aes192::expand_key; let keys = [0x00; 24]; check( unsafe { &expand_key(&keys) }, &[ [0x0000000000000000, 0x0000000000000000], [0x0000000000000000, 0x6263636362636363], [0x6263636362636363, 0x6263636362636363], [0x9b9898c9f9fbfbaa, 0x9b9898c9f9fbfbaa], [0x9b9898c9f9fbfbaa, 0x90973450696ccffa], [0xf2f457330b0fac99, 0x90973450696ccffa], [0xc81d19a9a171d653, 0x53858160588a2df9], [0xc81d19a9a171d653, 0x7bebf49bda9a22c8], [0x891fa3a8d1958e51, 0x198897f8b8f941ab], [0xc26896f718f2b43f, 0x91ed1797407899c6], [0x59f00e3ee1094f95, 0x83ecbc0f9b1e0830], [0x0af31fa74a8b8661, 0x137b885ff272c7ca], [0x432ac886d834c0b6, 0xd2c7df11984c5970], ], ); let keys = [0xff; 24]; check( unsafe { &expand_key(&keys) }, &[ [0xffffffffffffffff, 0xffffffffffffffff], [0xffffffffffffffff, 0xe8e9e9e917161616], [0xe8e9e9e917161616, 0xe8e9e9e917161616], [0xadaeae19bab8b80f, 0x525151e6454747f0], [0xadaeae19bab8b80f, 0xc5c2d8ed7f7a60e2], [0x2d2b3104686c76f4, 0xc5c2d8ed7f7a60e2], [0x1712403f686820dd, 0x454311d92d2f672d], [0xe8edbfc09797df22, 0x8f8cd3b7e7e4f36a], [0xa2a7e2b38f88859e, 0x67653a5ef0f2e57c], [0x2655c33bc1b13051, 0x6316d2e2ec9e577c], [0x8bfb6d227b09885e, 0x67919b1aa620ab4b], [0xc53679a929a82ed5, 0xa25343f7d95acba9], [0x598e482fffaee364, 0x3a989acd1330b418], ], ); let keys = hex!("000102030405060708090a0b0c0d0e0f1011121314151617"); check( unsafe { &expand_key(&keys) }, &[ [0x00010203040506
random_line_split
test_expand.rs
6869746f2a], [0xfa8807605fa82d0d, 0x3ac64e6553b2214f], [0xcf75838d90ddae80, 0xaa1be0e5f9a9c1aa], [0x180d2f1488d08194, 0x22cb6171db62a0db], [0xbaed96ad323d1739, 0x10f67648cb94d693], [0x881b4ab2ba265d8b, 0xaad02bc36144fd50], [0xb34f195d096944d6, 0xa3b96f15c2fd9245], [0xa7007778ae6933ae, 0x0dd05cbbcf2dcefe], [0xff8bccf251e2ff5c, 0x5c32a3e7931f6d19], [0x24b7182e7555e772, 0x29674495ba78298c], [0xae127cdadb479ba8, 0xf220df3d4858f6b1], ], ); let keys = hex!("2b7e151628aed2a6abf7158809cf4f3c"); check( unsafe { &expand_key(&keys) }, &[ [0x2b7e151628aed2a6, 0xabf7158809cf4f3c], [0xa0fafe1788542cb1, 0x23a339392a6c7605], [0xf2c295f27a96b943, 0x5935807a7359f67f], [0x3d80477d4716fe3e, 0x1e237e446d7a883b], [0xef44a541a8525b7f, 0xb671253bdb0bad00], [0xd4d1c6f87c839d87, 0xcaf2b8bc11f915bc], [0x6d88a37a110b3efd, 0xdbf98641ca0093fd], [0x4e54f70e5f5fc9f3, 0x84a64fb24ea6dc4f], [0xead27321b58dbad2, 0x312bf5607f8d292f], [0xac7766f319fadc21, 0x28d12941575c006e], [0xd014f9a8c9ee2589, 0xe13f0cc8b6630ca6], ], ); } #[test] fn aes192_expand_key_test()
], ); let keys = [0xff; 24]; check( unsafe { &expand_key(&keys) }, &[ [0xffffffffffffffff, 0xffffffffffffffff], [0xffffffffffffffff, 0xe8e9e9e917161616], [0xe8e9e9e917161616, 0xe8e9e9e917161616], [0xadaeae19bab8b80f, 0x525151e6454747f0], [0xadaeae19bab8b80f, 0xc5c2d8ed7f7a60e2], [0x2d2b3104686c76f4, 0xc5c2d8ed7f7a60e2], [0x1712403f686820dd, 0x454311d92d2f672d], [0xe8edbfc09797df22, 0x8f8cd3b7e7e4f36a], [0xa2a7e2b38f88859e, 0x67653a5ef0f2e57c], [0x2655c33bc1b13051, 0x6316d2e2ec9e577c], [0x8bfb6d227b09885e, 0x67919b1aa620ab4b], [0xc53679a929a82ed5, 0xa25343f7d95acba9], [0x598e482fffaee364, 0x3a989acd1330b418], ], ); let keys = hex!("000102030405060708090a0b0c0d0e0f1011121314151617"); check( unsafe { &expand_key(&keys) }, &[ [0x0001020304050607, 0x08090a0b0c0d0e0f], [0x1011121314151617, 0x5846f2f95c43f4fe], [0x544afef55847f0fa, 0x4856
{ use super::aes192::expand_key; let keys = [0x00; 24]; check( unsafe { &expand_key(&keys) }, &[ [0x0000000000000000, 0x0000000000000000], [0x0000000000000000, 0x6263636362636363], [0x6263636362636363, 0x6263636362636363], [0x9b9898c9f9fbfbaa, 0x9b9898c9f9fbfbaa], [0x9b9898c9f9fbfbaa, 0x90973450696ccffa], [0xf2f457330b0fac99, 0x90973450696ccffa], [0xc81d19a9a171d653, 0x53858160588a2df9], [0xc81d19a9a171d653, 0x7bebf49bda9a22c8], [0x891fa3a8d1958e51, 0x198897f8b8f941ab], [0xc26896f718f2b43f, 0x91ed1797407899c6], [0x59f00e3ee1094f95, 0x83ecbc0f9b1e0830], [0x0af31fa74a8b8661, 0x137b885ff272c7ca], [0x432ac886d834c0b6, 0xd2c7df11984c5970],
identifier_body
test_expand.rs
x6263636362636363], [0x6263636362636363, 0x6263636362636363], [0x9b9898c9f9fbfbaa, 0x9b9898c9f9fbfbaa], [0x9b9898c9f9fbfbaa, 0x90973450696ccffa], [0xf2f457330b0fac99, 0x90973450696ccffa], [0xc81d19a9a171d653, 0x53858160588a2df9], [0xc81d19a9a171d653, 0x7bebf49bda9a22c8], [0x891fa3a8d1958e51, 0x198897f8b8f941ab], [0xc26896f718f2b43f, 0x91ed1797407899c6], [0x59f00e3ee1094f95, 0x83ecbc0f9b1e0830], [0x0af31fa74a8b8661, 0x137b885ff272c7ca], [0x432ac886d834c0b6, 0xd2c7df11984c5970], ], ); let keys = [0xff; 24]; check( unsafe { &expand_key(&keys) }, &[ [0xffffffffffffffff, 0xffffffffffffffff], [0xffffffffffffffff, 0xe8e9e9e917161616], [0xe8e9e9e917161616, 0xe8e9e9e917161616], [0xadaeae19bab8b80f, 0x525151e6454747f0], [0xadaeae19bab8b80f, 0xc5c2d8ed7f7a60e2], [0x2d2b3104686c76f4, 0xc5c2d8ed7f7a60e2], [0x1712403f686820dd, 0x454311d92d2f672d], [0xe8edbfc09797df22, 0x8f8cd3b7e7e4f36a], [0xa2a7e2b38f88859e, 0x67653a5ef0f2e57c], [0x2655c33bc1b13051, 0x6316d2e2ec9e577c], [0x8bfb6d227b09885e, 0x67919b1aa620ab4b], [0xc53679a929a82ed5, 0xa25343f7d95acba9], [0x598e482fffaee364, 0x3a989acd1330b418], ], ); let keys = hex!("000102030405060708090a0b0c0d0e0f1011121314151617"); check( unsafe { &expand_key(&keys) }, &[ [0x0001020304050607, 0x08090a0b0c0d0e0f], [0x1011121314151617, 0x5846f2f95c43f4fe], [0x544afef55847f0fa, 0x4856e2e95c43f4fe], [0x40f949b31cbabd4d, 0x48f043b810b7b342], [0x58e151ab04a2a555, 0x7effb5416245080c], [0x2ab54bb43a02f8f6, 0x62e3a95d66410c08], [0xf501857297448d7e, 0xbdf1c6ca87f33e3c], [0xe510976183519b69, 0x34157c9ea351f1e0], [0x1ea0372a99530916, 0x7c439e77ff12051e], [0xdd7e0e887e2fff68, 0x608fc842f9dcc154], [0x859f5f237a8d5a3d, 0xc0c02952beefd63a], [0xde601e7827bcdf2c, 0xa223800fd8aeda32], [0xa4970a331a78dc09, 0xc418c271e3a41d5d], ], ); let keys = hex!("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b"); check( unsafe { &expand_key(&keys) }, &[ [0x8e73b0f7da0e6452, 0xc810f32b809079e5], [0x62f8ead2522c6b7b, 0xfe0c91f72402f5a5], [0xec12068e6c827f6b, 0x0e7a95b95c56fec2], [0x4db7b4bd69b54118, 0x85a74796e92538fd], [0xe75fad44bb095386, 0x485af05721efb14f], [0xa448f6d94d6dce24, 0xaa326360113b30e6], [0xa25e7ed583b1cf9a, 0x27f939436a94f767], [0xc0a69407d19da4e1, 0xec1786eb6fa64971], [0x485f703222cb8755, 0xe26d135233f0b7b3], [0x40beeb282f18a259, 0x6747d26b458c553e], [0xa7e1466c9411f1df, 0x821f750aad07d753], [0xca4005388fcc5006, 0x282d166abc3ce7b5], [0xe98ba06f448c773c, 0x8ecc720401002202], ], ); } #[test] fn
aes256_expand_key_test
identifier_name
day11.rs
FromStr for Item { type Err = Error; fn from_str(s: &str) -> Result<Self>
} impl std::fmt::Debug for Item { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Item::Chip(id) => write!(f, "{}M", id), Item::Generator(id) => write!(f, "{}G", id), Item::ChipAndGenerator => write!(f, "<>"), } } } #[derive(Debug, PartialEq, Eq, Clone)] struct State { elevator: usize, floors: Vec<HashSet<Item>>, } impl std::fmt::Display for State { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for (i, floor) in self.floors.iter().enumerate() { write!( f, "{} {}: {:?}\n", i, if self.elevator == i { "E" } else { " " }, floor )?; } Ok(()) } } fn would_fry(items: &HashSet<Item>) -> bool { for item in items { match item { Item::Chip(id) => { if items.contains(&Item::Generator(*id)) { // chip is protected by generator continue; } for other_item in items { if let Item::Generator(other_id) = other_item { // TODO: we might not need this if if other_id != id { // chip gets fried by another generator return true; } } } } Item::Generator(_) => {} Item::ChipAndGenerator => {} } } false } impl State { fn score(&self) -> usize { self.floors .iter() .enumerate() .map(|(i, f)| f.len() * (i + 1) * 10) .sum::<usize>() } fn is_success(&self) -> bool { for floor in &self.floors[..self.floors.len() - 1] { if !floor.is_empty() { return false; } } true } fn get_neighbors(&self) -> Vec<State> { let mut out = Vec::new(); // calculate valid floors that the elevator can move to let mut valid_destinations = Vec::new(); if self.elevator > 0 { valid_destinations.push(self.elevator - 1); }; if self.elevator < self.floors.len() - 1 { valid_destinations.push(self.elevator + 1); } for num_items in 1..=2 { // generate sets of items that can be taken from current floor - none, one, or two for moved_items in self.floors[self.elevator].iter().combinations(num_items) { let moved_items: HashSet<Item> = moved_items.into_iter().cloned().collect(); for destination in &valid_destinations { // take moved_items from self.elevator to destination let current_floor: HashSet<Item> = self.floors[self.elevator] .difference(&moved_items) .cloned() .collect(); let destination_floor: HashSet<Item> = self.floors[*destination] .union(&moved_items) .cloned() .collect(); // do not perform invalid moves if would_fry(&current_floor) || would_fry(&destination_floor) { continue; } let mut new_state: State = self.clone(); new_state.floors[self.elevator] = current_floor; new_state.floors[*destination] = destination_floor; new_state.elevator = *destination; out.push(new_state); } } } out } } impl std::hash::Hash for State { fn hash<H: std::hash::Hasher>(&self, state: &mut H) { self.elevator.hash(state); let floors: Vec<Vec<Item>> = self .floors .iter() .map(|items| { // build list of ids for which both Chip and Generator are seen let merge: Vec<char> = items .iter() .filter_map(|i| { if let Item::Chip(id) = i { if items.contains(&Item::Generator(*id)) { return Some(*id); } } None }) .collect(); let mut items = items.clone(); for id in &merge { items.remove(&Item::Chip(*id)); items.remove(&Item::Generator(*id)); } let mut items: Vec<Item> = items.into_iter().collect(); for _ in &merge { items.push(Item::ChipAndGenerator); } items.sort(); items }) .collect(); floors.hash(state); } } #[derive(Debug, Snafu)] enum Error { #[snafu(display("I/O error: {}", source))] Io { source: std::io::Error }, #[snafu(display("Int format error for '{}': {}", data, source))] ParseInt { data: String, source: std::num::ParseIntError, }, #[snafu(display("Invalid item: '{}'", data))] ParseItem { data: String }, } fn solve(input: &[&str]) -> Result<usize> { // -> Result<Vec<State>> { let start = State { elevator: 0, floors: input .iter() .map(|l| { if l.trim().is_empty() { Ok(HashSet::new()) } else { l.split(",") .map(|i| i.parse()) .collect::<Result<HashSet<Item>>>() } }) .collect::<Result<_>>()?, }; //let mut queue = vec![(0, Vec::new(), start.clone())]; let mut queue = vec![(0, start.clone())]; let mut seen: HashSet<State> = HashSet::new(); let mut max_steps = 0; let mut best_score = 0; while !queue.is_empty() { //let (steps, path, state) = queue.remove(0); let (steps, state) = queue.remove(0); //println!("{}\n{}", steps, state); if max_steps < steps { max_steps = steps; print!("."); std::io::stdout().flush().unwrap(); } for next_state in state.get_neighbors() { if seen.contains(&next_state) { continue; } let score = next_state.score(); if score > best_score { best_score = score; } // dirty heuristic: don't explore very bad states if score < best_score - 40 { continue; } //let mut next_path = path.clone(); //next_path.push(next_state.clone()); seen.insert(next_state.clone()); //queue.push((steps + 1, next_path, next_state)); queue.push((steps + 1, next_state)); } if state.is_success() { //return Ok(path); return Ok(steps); } } panic!("No solution") } fn main() -> Result<()> { //let input = vec![ // "HM,LM", // The first floor contains a hydrogen-compatible microchip and a lithium-compatible microchip. // "HG", // The second floor contains a hydrogen generator. // "LG", // The third floor contains a lithium generator. // "", // The fourth floor contains nothing relevant. //]; // p plutonium // P promethium // r ruthenium // s strontium // t thulium let input1 = vec![ "tG,tM,pG,sG", // The first floor contains a thulium generator, a thulium-compatible microchip, a plutonium generator, and a strontium generator. "pM,sM", // The second floor contains a plutonium-compatible microchip and a strontium-compatible microchip. "PG,PM,rG,rM", // The third floor contains a promethium generator, a promethium-compatible microchip, a ruthenium generator, and a ruthenium-compatible microchip. "", // The fourth floor contains nothing relevant. ]; if let Ok(path) = solve(&input1[..]) { //println!("\npart 1: solution in {} steps", path.len()); println!("\npart 1: solution in {} steps", path); //for (i, step) in path.into_iter().enumerate() { // println!("STEP {}:\n{}\n", i, step); //} } // d dilithium // e elerium // p plutonium // P prometh
{ let s: Vec<char> = s.chars().collect(); if s.len() != 2 { return Err(Error::ParseItem { data: s[0].to_string(), }); } match s[1] { 'M' => Ok(Item::Chip(s[0])), 'G' => Ok(Item::Generator(s[0])), _ => Err(Error::ParseItem { data: s[1].to_string(), }), } }
identifier_body
day11.rs
FromStr for Item { type Err = Error; fn from_str(s: &str) -> Result<Self> { let s: Vec<char> = s.chars().collect(); if s.len() != 2 { return Err(Error::ParseItem { data: s[0].to_string(), }); } match s[1] { 'M' => Ok(Item::Chip(s[0])), 'G' => Ok(Item::Generator(s[0])), _ => Err(Error::ParseItem { data: s[1].to_string(), }), } } } impl std::fmt::Debug for Item { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Item::Chip(id) => write!(f, "{}M", id), Item::Generator(id) => write!(f, "{}G", id), Item::ChipAndGenerator => write!(f, "<>"), } } } #[derive(Debug, PartialEq, Eq, Clone)] struct State { elevator: usize, floors: Vec<HashSet<Item>>, } impl std::fmt::Display for State { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for (i, floor) in self.floors.iter().enumerate() { write!( f, "{} {}: {:?}\n", i, if self.elevator == i { "E" } else { " " }, floor )?; } Ok(()) } } fn would_fry(items: &HashSet<Item>) -> bool { for item in items { match item { Item::Chip(id) => { if items.contains(&Item::Generator(*id)) { // chip is protected by generator continue; } for other_item in items { if let Item::Generator(other_id) = other_item { // TODO: we might not need this if if other_id != id { // chip gets fried by another generator return true; } } } } Item::Generator(_) => {} Item::ChipAndGenerator => {} } } false } impl State { fn
(&self) -> usize { self.floors .iter() .enumerate() .map(|(i, f)| f.len() * (i + 1) * 10) .sum::<usize>() } fn is_success(&self) -> bool { for floor in &self.floors[..self.floors.len() - 1] { if !floor.is_empty() { return false; } } true } fn get_neighbors(&self) -> Vec<State> { let mut out = Vec::new(); // calculate valid floors that the elevator can move to let mut valid_destinations = Vec::new(); if self.elevator > 0 { valid_destinations.push(self.elevator - 1); }; if self.elevator < self.floors.len() - 1 { valid_destinations.push(self.elevator + 1); } for num_items in 1..=2 { // generate sets of items that can be taken from current floor - none, one, or two for moved_items in self.floors[self.elevator].iter().combinations(num_items) { let moved_items: HashSet<Item> = moved_items.into_iter().cloned().collect(); for destination in &valid_destinations { // take moved_items from self.elevator to destination let current_floor: HashSet<Item> = self.floors[self.elevator] .difference(&moved_items) .cloned() .collect(); let destination_floor: HashSet<Item> = self.floors[*destination] .union(&moved_items) .cloned() .collect(); // do not perform invalid moves if would_fry(&current_floor) || would_fry(&destination_floor) { continue; } let mut new_state: State = self.clone(); new_state.floors[self.elevator] = current_floor; new_state.floors[*destination] = destination_floor; new_state.elevator = *destination; out.push(new_state); } } } out } } impl std::hash::Hash for State { fn hash<H: std::hash::Hasher>(&self, state: &mut H) { self.elevator.hash(state); let floors: Vec<Vec<Item>> = self .floors .iter() .map(|items| { // build list of ids for which both Chip and Generator are seen let merge: Vec<char> = items .iter() .filter_map(|i| { if let Item::Chip(id) = i { if items.contains(&Item::Generator(*id)) { return Some(*id); } } None }) .collect(); let mut items = items.clone(); for id in &merge { items.remove(&Item::Chip(*id)); items.remove(&Item::Generator(*id)); } let mut items: Vec<Item> = items.into_iter().collect(); for _ in &merge { items.push(Item::ChipAndGenerator); } items.sort(); items }) .collect(); floors.hash(state); } } #[derive(Debug, Snafu)] enum Error { #[snafu(display("I/O error: {}", source))] Io { source: std::io::Error }, #[snafu(display("Int format error for '{}': {}", data, source))] ParseInt { data: String, source: std::num::ParseIntError, }, #[snafu(display("Invalid item: '{}'", data))] ParseItem { data: String }, } fn solve(input: &[&str]) -> Result<usize> { // -> Result<Vec<State>> { let start = State { elevator: 0, floors: input .iter() .map(|l| { if l.trim().is_empty() { Ok(HashSet::new()) } else { l.split(",") .map(|i| i.parse()) .collect::<Result<HashSet<Item>>>() } }) .collect::<Result<_>>()?, }; //let mut queue = vec![(0, Vec::new(), start.clone())]; let mut queue = vec![(0, start.clone())]; let mut seen: HashSet<State> = HashSet::new(); let mut max_steps = 0; let mut best_score = 0; while !queue.is_empty() { //let (steps, path, state) = queue.remove(0); let (steps, state) = queue.remove(0); //println!("{}\n{}", steps, state); if max_steps < steps { max_steps = steps; print!("."); std::io::stdout().flush().unwrap(); } for next_state in state.get_neighbors() { if seen.contains(&next_state) { continue; } let score = next_state.score(); if score > best_score { best_score = score; } // dirty heuristic: don't explore very bad states if score < best_score - 40 { continue; } //let mut next_path = path.clone(); //next_path.push(next_state.clone()); seen.insert(next_state.clone()); //queue.push((steps + 1, next_path, next_state)); queue.push((steps + 1, next_state)); } if state.is_success() { //return Ok(path); return Ok(steps); } } panic!("No solution") } fn main() -> Result<()> { //let input = vec![ // "HM,LM", // The first floor contains a hydrogen-compatible microchip and a lithium-compatible microchip. // "HG", // The second floor contains a hydrogen generator. // "LG", // The third floor contains a lithium generator. // "", // The fourth floor contains nothing relevant. //]; // p plutonium // P promethium // r ruthenium // s strontium // t thulium let input1 = vec![ "tG,tM,pG,sG", // The first floor contains a thulium generator, a thulium-compatible microchip, a plutonium generator, and a strontium generator. "pM,sM", // The second floor contains a plutonium-compatible microchip and a strontium-compatible microchip. "PG,PM,rG,rM", // The third floor contains a promethium generator, a promethium-compatible microchip, a ruthenium generator, and a ruthenium-compatible microchip. "", // The fourth floor contains nothing relevant. ]; if let Ok(path) = solve(&input1[..]) { //println!("\npart 1: solution in {} steps", path.len()); println!("\npart 1: solution in {} steps", path); //for (i, step) in path.into_iter().enumerate() { // println!("STEP {}:\n{}\n", i, step); //} } // d dilithium // e elerium // p plutonium // P promethium
score
identifier_name
day11.rs
FromStr for Item { type Err = Error; fn from_str(s: &str) -> Result<Self> { let s: Vec<char> = s.chars().collect(); if s.len() != 2 { return Err(Error::ParseItem { data: s[0].to_string(), }); } match s[1] { 'M' => Ok(Item::Chip(s[0])), 'G' => Ok(Item::Generator(s[0])), _ => Err(Error::ParseItem { data: s[1].to_string(), }), } } } impl std::fmt::Debug for Item { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Item::Chip(id) => write!(f, "{}M", id), Item::Generator(id) => write!(f, "{}G", id), Item::ChipAndGenerator => write!(f, "<>"), } } } #[derive(Debug, PartialEq, Eq, Clone)] struct State { elevator: usize, floors: Vec<HashSet<Item>>, } impl std::fmt::Display for State { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for (i, floor) in self.floors.iter().enumerate() { write!( f, "{} {}: {:?}\n", i, if self.elevator == i { "E" } else { " " }, floor )?; } Ok(()) } } fn would_fry(items: &HashSet<Item>) -> bool { for item in items { match item { Item::Chip(id) => { if items.contains(&Item::Generator(*id)) { // chip is protected by generator continue; } for other_item in items { if let Item::Generator(other_id) = other_item { // TODO: we might not need this if if other_id != id { // chip gets fried by another generator return true; } } } } Item::Generator(_) => {} Item::ChipAndGenerator => {} } } false } impl State { fn score(&self) -> usize { self.floors .iter() .enumerate()
for floor in &self.floors[..self.floors.len() - 1] { if !floor.is_empty() { return false; } } true } fn get_neighbors(&self) -> Vec<State> { let mut out = Vec::new(); // calculate valid floors that the elevator can move to let mut valid_destinations = Vec::new(); if self.elevator > 0 { valid_destinations.push(self.elevator - 1); }; if self.elevator < self.floors.len() - 1 { valid_destinations.push(self.elevator + 1); } for num_items in 1..=2 { // generate sets of items that can be taken from current floor - none, one, or two for moved_items in self.floors[self.elevator].iter().combinations(num_items) { let moved_items: HashSet<Item> = moved_items.into_iter().cloned().collect(); for destination in &valid_destinations { // take moved_items from self.elevator to destination let current_floor: HashSet<Item> = self.floors[self.elevator] .difference(&moved_items) .cloned() .collect(); let destination_floor: HashSet<Item> = self.floors[*destination] .union(&moved_items) .cloned() .collect(); // do not perform invalid moves if would_fry(&current_floor) || would_fry(&destination_floor) { continue; } let mut new_state: State = self.clone(); new_state.floors[self.elevator] = current_floor; new_state.floors[*destination] = destination_floor; new_state.elevator = *destination; out.push(new_state); } } } out } } impl std::hash::Hash for State { fn hash<H: std::hash::Hasher>(&self, state: &mut H) { self.elevator.hash(state); let floors: Vec<Vec<Item>> = self .floors .iter() .map(|items| { // build list of ids for which both Chip and Generator are seen let merge: Vec<char> = items .iter() .filter_map(|i| { if let Item::Chip(id) = i { if items.contains(&Item::Generator(*id)) { return Some(*id); } } None }) .collect(); let mut items = items.clone(); for id in &merge { items.remove(&Item::Chip(*id)); items.remove(&Item::Generator(*id)); } let mut items: Vec<Item> = items.into_iter().collect(); for _ in &merge { items.push(Item::ChipAndGenerator); } items.sort(); items }) .collect(); floors.hash(state); } } #[derive(Debug, Snafu)] enum Error { #[snafu(display("I/O error: {}", source))] Io { source: std::io::Error }, #[snafu(display("Int format error for '{}': {}", data, source))] ParseInt { data: String, source: std::num::ParseIntError, }, #[snafu(display("Invalid item: '{}'", data))] ParseItem { data: String }, } fn solve(input: &[&str]) -> Result<usize> { // -> Result<Vec<State>> { let start = State { elevator: 0, floors: input .iter() .map(|l| { if l.trim().is_empty() { Ok(HashSet::new()) } else { l.split(",") .map(|i| i.parse()) .collect::<Result<HashSet<Item>>>() } }) .collect::<Result<_>>()?, }; //let mut queue = vec![(0, Vec::new(), start.clone())]; let mut queue = vec![(0, start.clone())]; let mut seen: HashSet<State> = HashSet::new(); let mut max_steps = 0; let mut best_score = 0; while !queue.is_empty() { //let (steps, path, state) = queue.remove(0); let (steps, state) = queue.remove(0); //println!("{}\n{}", steps, state); if max_steps < steps { max_steps = steps; print!("."); std::io::stdout().flush().unwrap(); } for next_state in state.get_neighbors() { if seen.contains(&next_state) { continue; } let score = next_state.score(); if score > best_score { best_score = score; } // dirty heuristic: don't explore very bad states if score < best_score - 40 { continue; } //let mut next_path = path.clone(); //next_path.push(next_state.clone()); seen.insert(next_state.clone()); //queue.push((steps + 1, next_path, next_state)); queue.push((steps + 1, next_state)); } if state.is_success() { //return Ok(path); return Ok(steps); } } panic!("No solution") } fn main() -> Result<()> { //let input = vec![ // "HM,LM", // The first floor contains a hydrogen-compatible microchip and a lithium-compatible microchip. // "HG", // The second floor contains a hydrogen generator. // "LG", // The third floor contains a lithium generator. // "", // The fourth floor contains nothing relevant. //]; // p plutonium // P promethium // r ruthenium // s strontium // t thulium let input1 = vec![ "tG,tM,pG,sG", // The first floor contains a thulium generator, a thulium-compatible microchip, a plutonium generator, and a strontium generator. "pM,sM", // The second floor contains a plutonium-compatible microchip and a strontium-compatible microchip. "PG,PM,rG,rM", // The third floor contains a promethium generator, a promethium-compatible microchip, a ruthenium generator, and a ruthenium-compatible microchip. "", // The fourth floor contains nothing relevant. ]; if let Ok(path) = solve(&input1[..]) { //println!("\npart 1: solution in {} steps", path.len()); println!("\npart 1: solution in {} steps", path); //for (i, step) in path.into_iter().enumerate() { // println!("STEP {}:\n{}\n", i, step); //} } // d dilithium // e elerium // p plutonium // P promethium
.map(|(i, f)| f.len() * (i + 1) * 10) .sum::<usize>() } fn is_success(&self) -> bool {
random_line_split
smpl.tpl.js
Id = blkId || smpl.tpl.utils.MAIN; var blk = this.__parsedBlocks[blkId]; if (blk) { delete this.__parsedBlocks[blkId]; return blk.join(''); } else { return ''; } }; smpl.tpl.Template.prototype.reset = function() { if (!this.__data) { this.init(); } this.__data = {}; this.__data[smpl.tpl.utils.MAIN] = { me: this.__globalKey }; this.__parsedBlocks = {}; return this; }; smpl.tpl.Template.prototype.parseBlock = function(blkId, config) { if (this.__blocks[blkId]) { var str = this.__blocks[blkId].call(this, smpl, this.getData(blkId)); delete this.__data[blkId]; var widget = this.__widgets[blkId]; if (widget) { var lib = smpl.tpl.utils.libraries[widget.lib]; widget = lib.retrieveWidget(widget.widget, config, str); str = widget.html; } this.__parsedBlocks[blkId] = this.__parsedBlocks[blkId] || []; this.__parsedBlocks[blkId].push(str); return widget && widget.widget; } else { throw new Error(smpl.string.supplant(MESSAGES.wrongParsed, [this.__name, 'block', blkId])); } }; smpl.tpl.Template.prototype.parseWidget = function(widgetId, config) { if (this.__widgets[widgetId]) { var widget = this.__widgets[widgetId]; config = config || {}; if (widget.args) { for (var key in widget.args) { config[key] = widget.args[key]; } //TODO: Support token in config } return this.parseBlock(widgetId, config); } else { throw new Error(smpl.string.supplant(MESSAGES.wrongParsed, [this.__name, 'widget', widgetId])); } }; smpl.tpl.Template.prototype.parse = function() { this.reset(); this.onParse.apply(this, arguments); this.parseBlock(smpl.tpl.utils.MAIN); return this; }; smpl.tpl.Template.prototype.load = function(container, display) { /* jshint browser: true */ if (typeof container === 'string') { container = document.getElementById(container); } if (container) { display = display || container.style.display; container.style.display = 'none'; container.innerHTML = this.retrieve(); if (this.onLoad) { this.onLoad(container, display); } container.style.display = display; } }; smpl.tpl.Template.prototype.onParse = function(pfx, obj) { this.set(pfx, obj); }; smpl.tpl.globalRepo = {}; smpl.tpl.enableGlobal = function(key, obj) { if (key) { smpl.tpl.globalKey = key; smpl.tpl.globalObj = obj; } else if (smpl.global.smpl === smpl) { smpl.tpl.globalKey = 'smpl.tpl.globalRepo'; smpl.tpl.globalObj = smpl.tpl.globalRepo; } return !!smpl.tpl.globalKey; }; smpl.tpl.utils = {}; smpl.tpl.utils.libraries = {}; smpl.tpl.utils.registerLibrary = function(libName, library) { smpl.tpl.utils.libraries[libName] = library; }; smpl.tpl.utils.MAIN = '_main'; smpl.tpl.utils.make = function(tpl, txt) { var l = txt.length, pos = 0, startPos = 0, stack = [], newpos; tpl.__blocks = {}; tpl.__parents = {}; this.processToken(tpl, stack, {type: 'block', name: this.MAIN}); while (pos < l) { var chr = txt.charAt(pos++); var nextChar = txt.charAt(pos); if (chr === '\\' && (nextChar === '\\' || nextChar === '{')) { // skip escaped \ and { ++pos; } else if (chr === '<' && nextChar === '!' && txt.charAt(pos + 1) === '-' && txt.charAt(pos + 2) === '-') { // html comment. search for block or widget newpos = txt.indexOf('-->', pos + 3); if (newpos !== -1) { var m = REGEX.block.exec(txt.slice(pos + 3, newpos)); if (!m) { m = REGEX.widget.exec(txt.slice(pos + 3, newpos)); } if (m) { this.processToken(tpl, stack, {type: 'html', txt: txt.slice(startPos, pos - 1)}); this.processToken(tpl, stack, { type: m[1].toLowerCase(), name: m[2], lib: m[3], widget: m[4], args: m[5] && this.parseArgs(m[5]) }); if (m[6]) { //Autoclose widget (`/` at the end of the block) this.processToken(tpl, stack, {type: '/' + m[1].toLowerCase(), name: m[2]}); } pos = newpos + 3; startPos = pos; } } } else if (chr === '{') { newpos = this.jsTokenize(txt, pos); if (newpos !== pos) { var tokenTxt = txt.slice(startPos, pos - 1); this.processToken(tpl, stack, {type: 'html', txt: tokenTxt, beforeJs: true}); startPos = pos; this.processToken(tpl, stack, {type: 'js', txt: txt.slice(pos, newpos)}); startPos = pos = newpos + 1; //skip closing } } } } this.processToken(tpl, stack, {type: 'html', txt: txt.slice(startPos, pos)}); this.processToken(tpl, stack, {type: '/block', name: this.MAIN}); }; smpl.tpl.utils.parseArgs = function(argsText) { var args = {}; if (argsText) { var re = /([-\w]+)="(.*?)"/g; var arg; while ((arg = re.exec(argsText)) !== null) { args[arg[1]] = smpl.string.unescapeHTML(arg[2]); } } return args; }; smpl.tpl.utils.processToken = function(tpl, stack, token) { var processed; switch (token.type) { case 'block': case 'widget': stack.push(token); if (tpl.__blocks[token.name]) { throw new Error(smpl.string.supplant(MESSAGES.duplicateBlock, [tpl.__name, token.name])); } tpl.__blocks[token.name] = []; if (token.type === 'widget') { tpl.__widgets[token.name] = { lib: token.lib, widget: token.widget, args: token.args }; } break; case '/widget': case '/block': var closed = stack.pop(); if (closed.name !== token.name || closed.type !== token.type.slice(1)) { throw new Error(smpl.string.supplant(MESSAGES.wrongClosed, [token.type.slice(1), token.name, closed.type, closed.name])); } tpl.__blocks[closed.name] = 'return ' + (tpl.__blocks[closed.name].join(' + ') || '""'); var parent = stack[stack.length - 1]; if (parent) { tpl.__parents[closed.name] = parent.name; } processed = "(this.retrieve('" + closed.name + "') || '')"; break; case 'html': token.txt = token.txt.replace(token.beforeJs ? /(\\*)\1\\(\{|$)/g : /(\\*)\1\\(\{)/g, '$1$2'); processed = "'" + smpl.string.escapeJs(token.txt) + "'"; break; case 'js': processed = this.compileJs(token.txt); break; } if (processed && stack.length) { var activeToken = stack[stack.length - 1]; tpl.__blocks[activeToken.name].push(processed); } }; smpl.tpl.utils.jsTokenize = function(input, pos) { var l = input.length, context = [], closingChar = { '(': ')', '[': ']', '{': '}' }, lastchar = '', newLine = /[\u000A\u000D\u2028\u2029]/, at = input.charAt(pos) === '@', initialPos = pos; while (pos < l) { var chr = input.charAt(pos++); if (newLine.test(chr)) {
if (!at) return initialPos; } else if (chr === '/' && input.charAt(pos) === '/') { //Single line comment if (!at) return initialPos; while (!newLine.test(input.charAt(++pos))); ++pos;
random_line_split
smpl.tpl.js
'string')
} } if (smpl.tpl.globalObj) { this.__globalKey = this.__name + '_' + smpl.utils.uniq(); smpl.tpl.globalObj[this.__globalKey] = this; this.__globalKey = smpl.tpl.globalKey + '["' + this.__globalKey + '"]'; } }; smpl.tpl.Template.prototype.set = function(block, key, value) { if (arguments.length === 2) { value = key; key = block; block = smpl.tpl.utils.MAIN; } this.getData(block)[key] = value; }; smpl.tpl.Template.prototype.getData = function(blockId) { if (this.__data[blockId]) return this.__data[blockId]; var parent = this.getData(this.__parents[blockId]); var data = Object.create(parent); this.__data[blockId] = data; return data; }; smpl.tpl.Template.prototype.retrieve = function(blkId) { blkId = blkId || smpl.tpl.utils.MAIN; var blk = this.__parsedBlocks[blkId]; if (blk) { delete this.__parsedBlocks[blkId]; return blk.join(''); } else { return ''; } }; smpl.tpl.Template.prototype.reset = function() { if (!this.__data) { this.init(); } this.__data = {}; this.__data[smpl.tpl.utils.MAIN] = { me: this.__globalKey }; this.__parsedBlocks = {}; return this; }; smpl.tpl.Template.prototype.parseBlock = function(blkId, config) { if (this.__blocks[blkId]) { var str = this.__blocks[blkId].call(this, smpl, this.getData(blkId)); delete this.__data[blkId]; var widget = this.__widgets[blkId]; if (widget) { var lib = smpl.tpl.utils.libraries[widget.lib]; widget = lib.retrieveWidget(widget.widget, config, str); str = widget.html; } this.__parsedBlocks[blkId] = this.__parsedBlocks[blkId] || []; this.__parsedBlocks[blkId].push(str); return widget && widget.widget; } else { throw new Error(smpl.string.supplant(MESSAGES.wrongParsed, [this.__name, 'block', blkId])); } }; smpl.tpl.Template.prototype.parseWidget = function(widgetId, config) { if (this.__widgets[widgetId]) { var widget = this.__widgets[widgetId]; config = config || {}; if (widget.args) { for (var key in widget.args) { config[key] = widget.args[key]; } //TODO: Support token in config } return this.parseBlock(widgetId, config); } else { throw new Error(smpl.string.supplant(MESSAGES.wrongParsed, [this.__name, 'widget', widgetId])); } }; smpl.tpl.Template.prototype.parse = function() { this.reset(); this.onParse.apply(this, arguments); this.parseBlock(smpl.tpl.utils.MAIN); return this; }; smpl.tpl.Template.prototype.load = function(container, display) { /* jshint browser: true */ if (typeof container === 'string') { container = document.getElementById(container); } if (container) { display = display || container.style.display; container.style.display = 'none'; container.innerHTML = this.retrieve(); if (this.onLoad) { this.onLoad(container, display); } container.style.display = display; } }; smpl.tpl.Template.prototype.onParse = function(pfx, obj) { this.set(pfx, obj); }; smpl.tpl.globalRepo = {}; smpl.tpl.enableGlobal = function(key, obj) { if (key) { smpl.tpl.globalKey = key; smpl.tpl.globalObj = obj; } else if (smpl.global.smpl === smpl) { smpl.tpl.globalKey = 'smpl.tpl.globalRepo'; smpl.tpl.globalObj = smpl.tpl.globalRepo; } return !!smpl.tpl.globalKey; }; smpl.tpl.utils = {}; smpl.tpl.utils.libraries = {}; smpl.tpl.utils.registerLibrary = function(libName, library) { smpl.tpl.utils.libraries[libName] = library; }; smpl.tpl.utils.MAIN = '_main'; smpl.tpl.utils.make = function(tpl, txt) { var l = txt.length, pos = 0, startPos = 0, stack = [], newpos; tpl.__blocks = {}; tpl.__parents = {}; this.processToken(tpl, stack, {type: 'block', name: this.MAIN}); while (pos < l) { var chr = txt.charAt(pos++); var nextChar = txt.charAt(pos); if (chr === '\\' && (nextChar === '\\' || nextChar === '{')) { // skip escaped \ and { ++pos; } else if (chr === '<' && nextChar === '!' && txt.charAt(pos + 1) === '-' && txt.charAt(pos + 2) === '-') { // html comment. search for block or widget newpos = txt.indexOf('-->', pos + 3); if (newpos !== -1) { var m = REGEX.block.exec(txt.slice(pos + 3, newpos)); if (!m) { m = REGEX.widget.exec(txt.slice(pos + 3, newpos)); } if (m) { this.processToken(tpl, stack, {type: 'html', txt: txt.slice(startPos, pos - 1)}); this.processToken(tpl, stack, { type: m[1].toLowerCase(), name: m[2], lib: m[3], widget: m[4], args: m[5] && this.parseArgs(m[5]) }); if (m[6]) { //Autoclose widget (`/` at the end of the block) this.processToken(tpl, stack, {type: '/' + m[1].toLowerCase(), name: m[2]}); } pos = newpos + 3; startPos = pos; } } } else if (chr === '{') { newpos = this.jsTokenize(txt, pos); if (newpos !== pos) { var tokenTxt = txt.slice(startPos, pos - 1); this.processToken(tpl, stack, {type: 'html', txt: tokenTxt, beforeJs: true}); startPos = pos; this.processToken(tpl, stack, {type: 'js', txt: txt.slice(pos, newpos)}); startPos = pos = newpos + 1; //skip closing } } } } this.processToken(tpl, stack, {type: 'html', txt: txt.slice(startPos, pos)}); this.processToken(tpl, stack, {type: '/block', name: this.MAIN}); }; smpl.tpl.utils.parseArgs = function(argsText) { var args = {}; if (argsText) { var re = /([-\w]+)="(.*?)"/g; var arg; while ((arg = re.exec(argsText)) !== null) { args[arg[1]] = smpl.string.unescapeHTML(arg[2]); } } return args; }; smpl.tpl.utils.processToken = function(tpl, stack, token) { var processed; switch (token.type) { case 'block': case 'widget': stack.push(token); if (tpl.__blocks[token.name]) { throw new Error(smpl.string.supplant(MESSAGES.duplicateBlock, [tpl.__name, token.name])); } tpl.__blocks[token.name] = []; if (token.type === 'widget') { tpl.__widgets[token.name] = { lib: token.lib, widget: token.widget, args: token.args }; } break; case '/widget': case '/block': var closed = stack.pop(); if (closed.name !== token.name || closed.type !== token.type.slice(1)) { throw new Error(smpl.string.supplant(MESSAGES.wrongClosed, [token.type.slice(1), token.name, closed.type, closed.name])); } tpl.__blocks[closed.name] = 'return ' + (tpl.__blocks[closed.name].join(' + ') || '""'); var parent = stack[stack.length - 1]; if (parent) { tpl.__parents[closed.name] = parent.name; } processed = "(this.retrieve('" + closed.name + "') || '')"; break; case 'html': token.txt = token.txt.replace(token.beforeJs ? /(\\*)\1\\(\{|$)/g : /(\\*)\1\\(\{)/g, '$1$2'); processed = "'" + smpl
{ this.__blocks[blkId] = new Function('smpl', '$', '"use strict";' + this.__blocks[blkId]); }
conditional_block
cart.component.ts
ToastrService, private router:Router, private cartService:OrderService, private sanitizer: DomSanitizer, private countriesService: CountriesService, private cService: CartService) { } async ngOnInit() { // this.getCart(); //this.getItems() this.userinfo = this.auth.getLoginData(); this.buyerId = this.userinfo['id']; await this.getCountries(); await this.getPreparation(); await this.validateCart(); this.getTotal(); } //VALIDATING CART async validateCart(){ await new Promise((resolve, reject) => { this.cartService.validateCart(this.userinfo['id']).subscribe(val =>{ console.log("Cart Validation", val); if(val['items'].length > 0){ this.itemsDeleted = val['items']; this.showSnackBar = true; } resolve(); }, error =>{ reject(); }) }); } getTotal(){ this.cartService.getCart( this.buyerId ) .subscribe( cart=> { console.log("Cart", cart); if(cart && cart.hasOwnProperty('items')){ console.log("Si existe"); if(cart['items'].length > 0){ console.log("Si es mayor a cero"); this.cart = cart; this.shoppingCartId=cart['id'] this.products=cart['items']; this.lastMilteCost = cart['lastMileCost']; this.firstMileCost = cart['firstMileCosts']; this.sfsMargin = cart['sfsMargin']; this.uaeTaxes = cart['uaeTaxes']; this.customs = cart['customs']; this.total= cart['subTotal']; this.shipping = cart['shipping']; this.taxesPer = cart['currentCharges']['uaeTaxes']; this.totalOtherFees = cart['totalOtherFees']; this.totalWithShipping = cart['total']; this.products.forEach((data, index) => { if (data.fish.imagePrimary && data.fish.imagePrimary != '') { this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle(`url(${this.API}${data.fish.imagePrimary})`); } else if (data.images && data.images.length > 0) { let src = data['images'][0].src ? data['images'][0].src : data['images'][0]; this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle(`url(${this.API}${src})`); } else { this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle('url(../../assets/default-img-product.jpg)'); } }); this.hideLoader(); this.empty = false; }else
} else{ this.hideLoader(); } }, error=> { console.log( error ); } ) } getTextWidth(text, font) { // re-use canvas object for better performance var canvas = document.createElement("canvas"); var context = canvas.getContext("2d"); context.font = font; var metrics = context.measureText(text); return metrics.width; } public isVacio(id, idSuffix) { let element = document.querySelector('#' + id) as HTMLInputElement; if (element === null) return true; //unit measure const suffixElement = document.getElementById(idSuffix); const width = this.getTextWidth(element.value, 'Josefin Sans, sans-serif'); if (suffixElement !== null) suffixElement.style.left = ($(element).width() / 2) + width + 'px'; return element.value === ''; } public getTag(perBox, id, unitOfSale){ let element = document.querySelector('#' + id) as HTMLInputElement; if (element === null) return ''; try{ let val = Number(element.value); if(val <= 1 && perBox === true) { return 'box'; } return perBox === true ? 'boxes' : (unitOfSale).toLowerCase(); } catch(e){ console.log(e); return ''; } } //GET COUNTRIES async getCountries() { await new Promise((resolve, reject) => { this.countriesService.getCountries().subscribe( result => { this.countries = result; resolve(); }, error => { console.log( error ); reject(); } ); }) } //FUNCTION TO GET ONLY THE TOTALS WHEN CHANGING QTY OF A PRODUCT getTotalPricing(){ //this.showLoading=true; this.cartService.getCart( this.userinfo['id'] ) .subscribe( cart=> { this.showLoading = false; if(cart && cart.hasOwnProperty('items')){ if(cart['items'].length > 0){ this.products=cart['items']; this.products.forEach((data, index) => { setTimeout(() => { console.log( jQuery('#range-' + data.fish.id), data.quantity.value); jQuery('#range-' + data.fish.id).val(data.quantity.value); this.moveBubble(data.fish.id); }, 100); }); this.lastMilteCost = cart['lastMileCost']; this.firstMileCost = cart['firstMileCosts']; this.sfsMargin = cart['sfsMargin']; this.uaeTaxes = cart['totalUAETaxes']; this.customs = cart['customs']; this.total= cart['subTotal']; this.shipping = cart['shipping']; this.totalOtherFees = cart['totalOtherFees']+cart['totalUAETaxes']; this.totalWithShipping = cart['total']; } } }, error=> { //this.showLoading = false; console.log( error ); } ) } hideLoader(){ this.showLoading=false; this.empty = true; } getItems(){ let cart = { "buyer": this.userinfo['id'] } this.productService.saveData("shoppingcart", cart).subscribe(result => { this.cService.setCart(result); console.log(' calcular totales', result ); },e=>{console.log(e)}) } getTotalxItem(count, price){ return count*price; } deleteItem(i, id){ this.productService.deleteData(`itemshopping/${id}`).subscribe( result=>{ this.products.splice(i, 1); console.log("Borrando item..", result, this.products); this.getItems(); jQuery('#confirmDelete').modal('hide'); this.getAllProductsCount(); if(this.products.length == 0){ this.empty = true; } }, e=>{ this.toast.error("Error deleting item!", "Error",{positionClass:"toast-top-right"} ); console.log(e) } ) } getAllProductsCount(){ var items:any = {"items": []}; this.products.forEach((element, index) => { let item = { "id": element['id'], "quantity": { "type": element['quantity'].type, "value": element['quantity'].value } } items['items'].push(item); console.log( 'get Product Counts', item ); if (items['items'].length == this.products.length){ this.updatecart(items); } }); } updatecart(items){ this.productService.updateData(this.shoppingEnpoint, items).subscribe(result => { console.log( 'result', result ); this.getTotalPricing(); }, error => { this.toast.error("Error updating cart!", "Error",{positionClass:"toast-top-right"} ); }) } checkout(){ localStorage.setItem('shippingCost', this.shipping); localStorage.setItem('shoppingTotal', this.totalWithShipping); localStorage.setItem('shoppingCartId', this.shoppingCartId); localStorage.setItem('totalOtherFees', this.totalOtherFees); //this.router.navigate(['/checkout'], {queryParams: {shoppingCartId: this.shoppingCartId}}); this.router.navigate(['/reviewcart'], {queryParams: {shoppingCartId: this.shoppingCartId}}); } findCountryName(value) { for (var i = 0; i < this.countries.length; i++) { if (this.countries[i]['code'] === value) { return this.countries[i].name; } } return null; } //FIND NAME OF PREPARATION findPreparationName(id){ for (var i = 0; i < this.preparataion.length; i++) { if (this.preparataion[i]['id'] === id) { return this.preparataion[i].name; } } return null; } showConfirmModal(itemID:string, index){ console.log("Product modal ID", itemID, index); this.itemToDelete = itemID; this.index = index; jQuery('#confirmDelete').modal('show'); } validateMax(i){ console.log(this.products[i].quantity.value); if(this.products[i].quantity.value > this.products[i].fish.maximumOrder){ this.products[i].quantity.value = this.products[i].fish.maximumOrder; this.getAllProductsCount(); }else{ this.getAllProductsCount(); } } //Function to hide span hideMe
{ this.hideLoader(); }
conditional_block
cart.component.ts
:ToastrService, private router:Router, private cartService:OrderService, private sanitizer: DomSanitizer, private countriesService: CountriesService, private cService: CartService) { } async ngOnInit() { // this.getCart(); //this.getItems() this.userinfo = this.auth.getLoginData(); this.buyerId = this.userinfo['id']; await this.getCountries(); await this.getPreparation(); await this.validateCart(); this.getTotal(); } //VALIDATING CART async validateCart(){ await new Promise((resolve, reject) => { this.cartService.validateCart(this.userinfo['id']).subscribe(val =>{ console.log("Cart Validation", val); if(val['items'].length > 0){ this.itemsDeleted = val['items']; this.showSnackBar = true; } resolve(); }, error =>{ reject(); }) }); } getTotal(){ this.cartService.getCart( this.buyerId ) .subscribe( cart=> { console.log("Cart", cart); if(cart && cart.hasOwnProperty('items')){ console.log("Si existe"); if(cart['items'].length > 0){ console.log("Si es mayor a cero"); this.cart = cart; this.shoppingCartId=cart['id'] this.products=cart['items']; this.lastMilteCost = cart['lastMileCost']; this.firstMileCost = cart['firstMileCosts']; this.sfsMargin = cart['sfsMargin']; this.uaeTaxes = cart['uaeTaxes']; this.customs = cart['customs']; this.total= cart['subTotal']; this.shipping = cart['shipping']; this.taxesPer = cart['currentCharges']['uaeTaxes']; this.totalOtherFees = cart['totalOtherFees']; this.totalWithShipping = cart['total']; this.products.forEach((data, index) => { if (data.fish.imagePrimary && data.fish.imagePrimary != '') { this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle(`url(${this.API}${data.fish.imagePrimary})`); } else if (data.images && data.images.length > 0) { let src = data['images'][0].src ? data['images'][0].src : data['images'][0]; this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle(`url(${this.API}${src})`); } else { this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle('url(../../assets/default-img-product.jpg)'); } }); this.hideLoader(); this.empty = false; }else{ this.hideLoader(); } } else{ this.hideLoader(); } }, error=> { console.log( error ); } ) } getTextWidth(text, font) { // re-use canvas object for better performance var canvas = document.createElement("canvas"); var context = canvas.getContext("2d"); context.font = font; var metrics = context.measureText(text); return metrics.width; } public isVacio(id, idSuffix) { let element = document.querySelector('#' + id) as HTMLInputElement; if (element === null) return true; //unit measure const suffixElement = document.getElementById(idSuffix); const width = this.getTextWidth(element.value, 'Josefin Sans, sans-serif'); if (suffixElement !== null) suffixElement.style.left = ($(element).width() / 2) + width + 'px'; return element.value === ''; } public getTag(perBox, id, unitOfSale){ let element = document.querySelector('#' + id) as HTMLInputElement; if (element === null) return ''; try{ let val = Number(element.value); if(val <= 1 && perBox === true) { return 'box'; } return perBox === true ? 'boxes' : (unitOfSale).toLowerCase(); } catch(e){ console.log(e); return ''; } } //GET COUNTRIES async getCountries() { await new Promise((resolve, reject) => { this.countriesService.getCountries().subscribe( result => { this.countries = result; resolve(); }, error => { console.log( error ); reject(); } ); }) } //FUNCTION TO GET ONLY THE TOTALS WHEN CHANGING QTY OF A PRODUCT getTotalPricing(){ //this.showLoading=true; this.cartService.getCart( this.userinfo['id'] ) .subscribe( cart=> { this.showLoading = false; if(cart && cart.hasOwnProperty('items')){ if(cart['items'].length > 0){ this.products=cart['items']; this.products.forEach((data, index) => { setTimeout(() => { console.log( jQuery('#range-' + data.fish.id), data.quantity.value); jQuery('#range-' + data.fish.id).val(data.quantity.value); this.moveBubble(data.fish.id); }, 100); }); this.lastMilteCost = cart['lastMileCost']; this.firstMileCost = cart['firstMileCosts']; this.sfsMargin = cart['sfsMargin']; this.uaeTaxes = cart['totalUAETaxes']; this.customs = cart['customs']; this.total= cart['subTotal']; this.shipping = cart['shipping']; this.totalOtherFees = cart['totalOtherFees']+cart['totalUAETaxes']; this.totalWithShipping = cart['total']; } } }, error=> { //this.showLoading = false; console.log( error ); } ) } hideLoader(){ this.showLoading=false; this.empty = true; } getItems(){ let cart = { "buyer": this.userinfo['id'] } this.productService.saveData("shoppingcart", cart).subscribe(result => { this.cService.setCart(result); console.log(' calcular totales', result ); },e=>{console.log(e)}) } getTotalxItem(count, price){ return count*price; } deleteItem(i, id){ this.productService.deleteData(`itemshopping/${id}`).subscribe( result=>{ this.products.splice(i, 1); console.log("Borrando item..", result, this.products); this.getItems(); jQuery('#confirmDelete').modal('hide'); this.getAllProductsCount(); if(this.products.length == 0){ this.empty = true; } }, e=>{ this.toast.error("Error deleting item!", "Error",{positionClass:"toast-top-right"} ); console.log(e) } ) } getAllProductsCount(){ var items:any = {"items": []}; this.products.forEach((element, index) => { let item = { "id": element['id'], "quantity": { "type": element['quantity'].type, "value": element['quantity'].value } } items['items'].push(item); console.log( 'get Product Counts', item ); if (items['items'].length == this.products.length){ this.updatecart(items); } }); }
(items){ this.productService.updateData(this.shoppingEnpoint, items).subscribe(result => { console.log( 'result', result ); this.getTotalPricing(); }, error => { this.toast.error("Error updating cart!", "Error",{positionClass:"toast-top-right"} ); }) } checkout(){ localStorage.setItem('shippingCost', this.shipping); localStorage.setItem('shoppingTotal', this.totalWithShipping); localStorage.setItem('shoppingCartId', this.shoppingCartId); localStorage.setItem('totalOtherFees', this.totalOtherFees); //this.router.navigate(['/checkout'], {queryParams: {shoppingCartId: this.shoppingCartId}}); this.router.navigate(['/reviewcart'], {queryParams: {shoppingCartId: this.shoppingCartId}}); } findCountryName(value) { for (var i = 0; i < this.countries.length; i++) { if (this.countries[i]['code'] === value) { return this.countries[i].name; } } return null; } //FIND NAME OF PREPARATION findPreparationName(id){ for (var i = 0; i < this.preparataion.length; i++) { if (this.preparataion[i]['id'] === id) { return this.preparataion[i].name; } } return null; } showConfirmModal(itemID:string, index){ console.log("Product modal ID", itemID, index); this.itemToDelete = itemID; this.index = index; jQuery('#confirmDelete').modal('show'); } validateMax(i){ console.log(this.products[i].quantity.value); if(this.products[i].quantity.value > this.products[i].fish.maximumOrder){ this.products[i].quantity.value = this.products[i].fish.maximumOrder; this.getAllProductsCount(); }else{ this.getAllProductsCount(); } } //Function to hide span hideMe
updatecart
identifier_name
cart.component.ts
ToastrService, private router:Router, private cartService:OrderService, private sanitizer: DomSanitizer, private countriesService: CountriesService, private cService: CartService) { } async ngOnInit() { // this.getCart(); //this.getItems() this.userinfo = this.auth.getLoginData(); this.buyerId = this.userinfo['id']; await this.getCountries(); await this.getPreparation(); await this.validateCart(); this.getTotal(); } //VALIDATING CART async validateCart(){ await new Promise((resolve, reject) => { this.cartService.validateCart(this.userinfo['id']).subscribe(val =>{ console.log("Cart Validation", val); if(val['items'].length > 0){ this.itemsDeleted = val['items']; this.showSnackBar = true; } resolve(); }, error =>{ reject(); }) }); } getTotal(){ this.cartService.getCart( this.buyerId ) .subscribe( cart=> { console.log("Cart", cart); if(cart && cart.hasOwnProperty('items')){ console.log("Si existe"); if(cart['items'].length > 0){ console.log("Si es mayor a cero"); this.cart = cart; this.shoppingCartId=cart['id'] this.products=cart['items']; this.lastMilteCost = cart['lastMileCost']; this.firstMileCost = cart['firstMileCosts']; this.sfsMargin = cart['sfsMargin']; this.uaeTaxes = cart['uaeTaxes']; this.customs = cart['customs']; this.total= cart['subTotal']; this.shipping = cart['shipping']; this.taxesPer = cart['currentCharges']['uaeTaxes']; this.totalOtherFees = cart['totalOtherFees']; this.totalWithShipping = cart['total']; this.products.forEach((data, index) => { if (data.fish.imagePrimary && data.fish.imagePrimary != '') { this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle(`url(${this.API}${data.fish.imagePrimary})`); } else if (data.images && data.images.length > 0) { let src = data['images'][0].src ? data['images'][0].src : data['images'][0]; this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle(`url(${this.API}${src})`); } else { this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle('url(../../assets/default-img-product.jpg)'); } }); this.hideLoader(); this.empty = false; }else{ this.hideLoader(); } } else{ this.hideLoader(); } }, error=> { console.log( error ); } ) } getTextWidth(text, font) { // re-use canvas object for better performance var canvas = document.createElement("canvas"); var context = canvas.getContext("2d"); context.font = font; var metrics = context.measureText(text); return metrics.width; } public isVacio(id, idSuffix) { let element = document.querySelector('#' + id) as HTMLInputElement; if (element === null) return true; //unit measure const suffixElement = document.getElementById(idSuffix); const width = this.getTextWidth(element.value, 'Josefin Sans, sans-serif'); if (suffixElement !== null) suffixElement.style.left = ($(element).width() / 2) + width + 'px'; return element.value === ''; } public getTag(perBox, id, unitOfSale){ let element = document.querySelector('#' + id) as HTMLInputElement; if (element === null) return ''; try{ let val = Number(element.value); if(val <= 1 && perBox === true) { return 'box'; } return perBox === true ? 'boxes' : (unitOfSale).toLowerCase(); } catch(e){ console.log(e); return ''; } } //GET COUNTRIES async getCountries() { await new Promise((resolve, reject) => { this.countriesService.getCountries().subscribe( result => { this.countries = result; resolve(); }, error => { console.log( error ); reject(); } ); }) } //FUNCTION TO GET ONLY THE TOTALS WHEN CHANGING QTY OF A PRODUCT getTotalPricing(){ //this.showLoading=true; this.cartService.getCart( this.userinfo['id'] ) .subscribe( cart=> { this.showLoading = false; if(cart && cart.hasOwnProperty('items')){ if(cart['items'].length > 0){ this.products=cart['items']; this.products.forEach((data, index) => { setTimeout(() => { console.log( jQuery('#range-' + data.fish.id), data.quantity.value); jQuery('#range-' + data.fish.id).val(data.quantity.value); this.moveBubble(data.fish.id); }, 100); }); this.lastMilteCost = cart['lastMileCost']; this.firstMileCost = cart['firstMileCosts']; this.sfsMargin = cart['sfsMargin']; this.uaeTaxes = cart['totalUAETaxes']; this.customs = cart['customs']; this.total= cart['subTotal']; this.shipping = cart['shipping']; this.totalOtherFees = cart['totalOtherFees']+cart['totalUAETaxes']; this.totalWithShipping = cart['total']; } } }, error=> { //this.showLoading = false; console.log( error ); } ) } hideLoader(){ this.showLoading=false; this.empty = true; } getItems(){ let cart = { "buyer": this.userinfo['id'] } this.productService.saveData("shoppingcart", cart).subscribe(result => { this.cService.setCart(result); console.log(' calcular totales', result ); },e=>{console.log(e)}) } getTotalxItem(count, price){ return count*price; } deleteItem(i, id){ this.productService.deleteData(`itemshopping/${id}`).subscribe( result=>{ this.products.splice(i, 1); console.log("Borrando item..", result, this.products); this.getItems(); jQuery('#confirmDelete').modal('hide'); this.getAllProductsCount(); if(this.products.length == 0){ this.empty = true; } }, e=>{ this.toast.error("Error deleting item!", "Error",{positionClass:"toast-top-right"} ); console.log(e) } ) } getAllProductsCount(){ var items:any = {"items": []}; this.products.forEach((element, index) => { let item = { "id": element['id'], "quantity": { "type": element['quantity'].type, "value": element['quantity'].value } } items['items'].push(item); console.log( 'get Product Counts', item ); if (items['items'].length == this.products.length){ this.updatecart(items); } }); } updatecart(items){ this.productService.updateData(this.shoppingEnpoint, items).subscribe(result => { console.log( 'result', result ); this.getTotalPricing(); }, error => { this.toast.error("Error updating cart!", "Error",{positionClass:"toast-top-right"} ); }) } checkout(){ localStorage.setItem('shippingCost', this.shipping); localStorage.setItem('shoppingTotal', this.totalWithShipping); localStorage.setItem('shoppingCartId', this.shoppingCartId); localStorage.setItem('totalOtherFees', this.totalOtherFees); //this.router.navigate(['/checkout'], {queryParams: {shoppingCartId: this.shoppingCartId}}); this.router.navigate(['/reviewcart'], {queryParams: {shoppingCartId: this.shoppingCartId}}); } findCountryName(value) { for (var i = 0; i < this.countries.length; i++) { if (this.countries[i]['code'] === value) { return this.countries[i].name; } } return null; } //FIND NAME OF PREPARATION findPreparationName(id){ for (var i = 0; i < this.preparataion.length; i++) { if (this.preparataion[i]['id'] === id) { return this.preparataion[i].name; } } return null; } showConfirmModal(itemID:string, index)
validateMax(i){ console.log(this.products[i].quantity.value); if(this.products[i].quantity.value > this.products[i].fish.maximumOrder){ this.products[i].quantity.value = this.products[i].fish.maximumOrder; this.getAllProductsCount(); }else{ this.getAllProductsCount(); } } //Function to hide span hide
{ console.log("Product modal ID", itemID, index); this.itemToDelete = itemID; this.index = index; jQuery('#confirmDelete').modal('show'); }
identifier_body
cart.component.ts
:ToastrService, private router:Router, private cartService:OrderService, private sanitizer: DomSanitizer, private countriesService: CountriesService, private cService: CartService) { } async ngOnInit() { // this.getCart(); //this.getItems() this.userinfo = this.auth.getLoginData(); this.buyerId = this.userinfo['id']; await this.getCountries(); await this.getPreparation(); await this.validateCart(); this.getTotal(); } //VALIDATING CART async validateCart(){ await new Promise((resolve, reject) => { this.cartService.validateCart(this.userinfo['id']).subscribe(val =>{ console.log("Cart Validation", val); if(val['items'].length > 0){ this.itemsDeleted = val['items']; this.showSnackBar = true; } resolve(); }, error =>{ reject(); }) }); } getTotal(){ this.cartService.getCart( this.buyerId ) .subscribe( cart=> { console.log("Cart", cart); if(cart && cart.hasOwnProperty('items')){ console.log("Si existe"); if(cart['items'].length > 0){ console.log("Si es mayor a cero"); this.cart = cart; this.shoppingCartId=cart['id'] this.products=cart['items']; this.lastMilteCost = cart['lastMileCost']; this.firstMileCost = cart['firstMileCosts']; this.sfsMargin = cart['sfsMargin']; this.uaeTaxes = cart['uaeTaxes']; this.customs = cart['customs']; this.total= cart['subTotal']; this.shipping = cart['shipping']; this.taxesPer = cart['currentCharges']['uaeTaxes']; this.totalOtherFees = cart['totalOtherFees']; this.totalWithShipping = cart['total']; this.products.forEach((data, index) => { if (data.fish.imagePrimary && data.fish.imagePrimary != '') { this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle(`url(${this.API}${data.fish.imagePrimary})`); } else if (data.images && data.images.length > 0) { let src = data['images'][0].src ? data['images'][0].src : data['images'][0]; this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle(`url(${this.API}${src})`); } else { this.imageCart[index] = this.sanitizer.bypassSecurityTrustStyle('url(../../assets/default-img-product.jpg)'); } }); this.hideLoader(); this.empty = false; }else{ this.hideLoader(); } } else{ this.hideLoader(); } }, error=> { console.log( error ); } ) } getTextWidth(text, font) { // re-use canvas object for better performance
var context = canvas.getContext("2d"); context.font = font; var metrics = context.measureText(text); return metrics.width; } public isVacio(id, idSuffix) { let element = document.querySelector('#' + id) as HTMLInputElement; if (element === null) return true; //unit measure const suffixElement = document.getElementById(idSuffix); const width = this.getTextWidth(element.value, 'Josefin Sans, sans-serif'); if (suffixElement !== null) suffixElement.style.left = ($(element).width() / 2) + width + 'px'; return element.value === ''; } public getTag(perBox, id, unitOfSale){ let element = document.querySelector('#' + id) as HTMLInputElement; if (element === null) return ''; try{ let val = Number(element.value); if(val <= 1 && perBox === true) { return 'box'; } return perBox === true ? 'boxes' : (unitOfSale).toLowerCase(); } catch(e){ console.log(e); return ''; } } //GET COUNTRIES async getCountries() { await new Promise((resolve, reject) => { this.countriesService.getCountries().subscribe( result => { this.countries = result; resolve(); }, error => { console.log( error ); reject(); } ); }) } //FUNCTION TO GET ONLY THE TOTALS WHEN CHANGING QTY OF A PRODUCT getTotalPricing(){ //this.showLoading=true; this.cartService.getCart( this.userinfo['id'] ) .subscribe( cart=> { this.showLoading = false; if(cart && cart.hasOwnProperty('items')){ if(cart['items'].length > 0){ this.products=cart['items']; this.products.forEach((data, index) => { setTimeout(() => { console.log( jQuery('#range-' + data.fish.id), data.quantity.value); jQuery('#range-' + data.fish.id).val(data.quantity.value); this.moveBubble(data.fish.id); }, 100); }); this.lastMilteCost = cart['lastMileCost']; this.firstMileCost = cart['firstMileCosts']; this.sfsMargin = cart['sfsMargin']; this.uaeTaxes = cart['totalUAETaxes']; this.customs = cart['customs']; this.total= cart['subTotal']; this.shipping = cart['shipping']; this.totalOtherFees = cart['totalOtherFees']+cart['totalUAETaxes']; this.totalWithShipping = cart['total']; } } }, error=> { //this.showLoading = false; console.log( error ); } ) } hideLoader(){ this.showLoading=false; this.empty = true; } getItems(){ let cart = { "buyer": this.userinfo['id'] } this.productService.saveData("shoppingcart", cart).subscribe(result => { this.cService.setCart(result); console.log(' calcular totales', result ); },e=>{console.log(e)}) } getTotalxItem(count, price){ return count*price; } deleteItem(i, id){ this.productService.deleteData(`itemshopping/${id}`).subscribe( result=>{ this.products.splice(i, 1); console.log("Borrando item..", result, this.products); this.getItems(); jQuery('#confirmDelete').modal('hide'); this.getAllProductsCount(); if(this.products.length == 0){ this.empty = true; } }, e=>{ this.toast.error("Error deleting item!", "Error",{positionClass:"toast-top-right"} ); console.log(e) } ) } getAllProductsCount(){ var items:any = {"items": []}; this.products.forEach((element, index) => { let item = { "id": element['id'], "quantity": { "type": element['quantity'].type, "value": element['quantity'].value } } items['items'].push(item); console.log( 'get Product Counts', item ); if (items['items'].length == this.products.length){ this.updatecart(items); } }); } updatecart(items){ this.productService.updateData(this.shoppingEnpoint, items).subscribe(result => { console.log( 'result', result ); this.getTotalPricing(); }, error => { this.toast.error("Error updating cart!", "Error",{positionClass:"toast-top-right"} ); }) } checkout(){ localStorage.setItem('shippingCost', this.shipping); localStorage.setItem('shoppingTotal', this.totalWithShipping); localStorage.setItem('shoppingCartId', this.shoppingCartId); localStorage.setItem('totalOtherFees', this.totalOtherFees); //this.router.navigate(['/checkout'], {queryParams: {shoppingCartId: this.shoppingCartId}}); this.router.navigate(['/reviewcart'], {queryParams: {shoppingCartId: this.shoppingCartId}}); } findCountryName(value) { for (var i = 0; i < this.countries.length; i++) { if (this.countries[i]['code'] === value) { return this.countries[i].name; } } return null; } //FIND NAME OF PREPARATION findPreparationName(id){ for (var i = 0; i < this.preparataion.length; i++) { if (this.preparataion[i]['id'] === id) { return this.preparataion[i].name; } } return null; } showConfirmModal(itemID:string, index){ console.log("Product modal ID", itemID, index); this.itemToDelete = itemID; this.index = index; jQuery('#confirmDelete').modal('show'); } validateMax(i){ console.log(this.products[i].quantity.value); if(this.products[i].quantity.value > this.products[i].fish.maximumOrder){ this.products[i].quantity.value = this.products[i].fish.maximumOrder; this.getAllProductsCount(); }else{ this.getAllProductsCount(); } } //Function to hide span hideMe(id
var canvas = document.createElement("canvas");
random_line_split
sink.rs
open(&input_file_path)?; // Replace the @@ marker in the args with the actual file path (if input type is File). if input_channel == InputChannel::File { if let Some(elem) = args.iter_mut().find(|e| **e == "@@") { *elem = input_file_path.to_str().unwrap().to_owned(); } else { return Err(anyhow!(format!("No @@ marker in args, even though the input channel is defined as file. args: {:#?}", args))); } } let mut stdout_file = None; if log_stdout { // Setup file for stdout logging. let mut path = workdir.clone(); path.push("stdout"); let file = OpenOptions::new() .read(true) .write(true) .create(true) .truncate(true) .open(&path) .unwrap(); stdout_file = Some((file, path)); } let mut stderr_file = None; if log_stderr { // Setup file for stdout logging. let mut path = workdir.clone(); path.push("stderr"); let file = OpenOptions::new() .read(true) .write(true) .create(true) .truncate(true) .open(&path) .unwrap(); stderr_file = Some((file, path)); } Ok(AflSink { path, args, workdir, input_channel, input_file: (input_file, input_file_path), forkserver_sid: None, bitmap: Bitmap::new_in_shm(BITMAP_DEFAULT_MAP_SIZE, 0x00), send_fd: None, receive_fd: None, log_stdout, log_stderr, stdout_file, stderr_file, config: config.cloned(), bitmap_was_resize: false, }) } pub fn from_config(config: &Config, id: Option<usize>) -> Result<AflSink> { let config_new = config.clone(); let mut workdir = config_new.general.work_dir.clone(); workdir.push( id.map(|id| id.to_string()) .unwrap_or_else(|| "0".to_owned()), ); let sink = AflSink::new( config_new.sink.bin_path, config_new.sink.arguments, workdir, config_new.sink.input_type, Some(config), config.sink.log_stdout, config.sink.log_stderr, )?; Ok(sink) } /// Wait for the given duration for the forkserver read fd to become ready. /// Returns Ok(true) if data becomes ready during the given `timeout`, else /// Ok(false). /// /// # Error /// /// Returns an Error if an unexpected error occurs. fn wait_for_data(&self, timeout: Duration) -> Result<()> { let pollfd = filedescriptor::pollfd { fd: self.receive_fd.unwrap(), events: filedescriptor::POLLIN, revents: 0, }; let mut pollfds = [pollfd]; let nready = filedescriptor::poll(&mut pollfds, Some(timeout)); match nready { Ok(1) => Ok(()), Ok(0) => Err(SinkError::CommunicationTimeoutError(format!( "Did not received data after {:?}", timeout )) .into()), Ok(n) => { unreachable!("Unexpected return value: {}", n); } Err(ref err) => { if let filedescriptor::Error::Poll(err) = err { if err.kind() == io::ErrorKind::Interrupted { return self.wait_for_data(timeout); } } Err(SinkError::FatalError(format!("Failed to poll fd: {:#?}", err)).into()) } } } pub fn start(&mut self) -> Result<()> { // send_pipe[1](we) -> send_pipe[0](forkserver). let send_pipe = [0i32; 2]; // receive_pipe[1](forkserver) -> receive_pipe[0](we). let receive_pipe = [0i32; 2]; // Create pipe for communicating with the forkserver. unsafe { let ret = libc::pipe(send_pipe.as_ptr() as *mut i32); assert_eq!(ret, 0); let ret = libc::pipe(receive_pipe.as_ptr() as *mut i32); assert_eq!(ret, 0); } self.send_fd = Some(send_pipe[1]); let child_receive_fd = send_pipe[0]; self.receive_fd = Some(receive_pipe[0]); let child_send_fd = receive_pipe[1]; let child_pid = unsafe { libc::fork() }; match child_pid { -1 => return Err(anyhow!("Fork failed!")), 0 => { /* Child Be aware that we are forking a potentially multithreaded application here. Since fork() only copies the calling thread, the environment might be left in a dirty state because of, e.g., mutexs that where locked at the time fork was called. Because of this it is only save to call async-signal-safe functions (https://man7.org/linux/man-pages/man7/signal-safety.7.html). Note that loggin function (debug!...) often internally use mutexes to lock the output buffer, thus using logging here is forbidden and likely causes deadlocks. */ let map_shm_id = self.bitmap.shm_id(); unsafe { let ret = libc::setsid(); assert!(ret >= 0); } // Setup args let path = self.path.to_str().map(|s| s.to_owned()).ok_or_else(|| { SinkError::Other(anyhow!("Invalid UTF-8 character in path")) })?; let mut args = self.args.clone(); args.insert(0, path.clone()); let argv_nonref: Vec<CString> = args .iter() .map(|arg| CString::new(arg.as_bytes()).unwrap()) .collect(); let mut argv: Vec<*const c_char> = argv_nonref.iter().map(|arg| arg.as_ptr()).collect(); argv.push(std::ptr::null()); // Setup environment let mut envp: Vec<*const c_char> = Vec::new(); let shm_env_var = CString::new(format!("{}={}", AFL_SHM_ENV_VAR_NAME, map_shm_id)).unwrap(); envp.push(shm_env_var.as_ptr()); let mut env_from_config = Vec::new(); if let Some(cfg) = self.config.as_ref() { cfg.sink.env.iter().for_each(|var| { env_from_config .push(CString::new(format!("{}={}", var.0, var.1).as_bytes()).unwrap()) }) } let afl_maps_size = CString::new(format!("AFL_MAP_SIZE={}", self.bitmap().size())).unwrap(); envp.push(afl_maps_size.as_bytes().as_ptr() as *const i8); env_from_config.iter().for_each(|e| { envp.push(e.as_bytes().as_ptr() as *const i8); }); envp.push(std::ptr::null()); let dev_null_fd = unsafe { let path = CString::new("/dev/null".as_bytes()).unwrap(); libc::open(path.as_ptr(), libc::O_RDONLY) }; if dev_null_fd < 0 { panic!("Failed to open /dev/null"); } match self.input_channel { InputChannel::Stdin => unsafe { libc::dup2(self.input_file.0.as_raw_fd(), 0); }, _ => unsafe { libc::dup2(dev_null_fd, 0); }, } if self.log_stdout { // unsafe { // let fd = self.stdout_file.as_ref().unwrap().0.as_raw_fd(); // libc::dup2(fd, libc::STDOUT_FILENO); // libc::close(fd); // } } else { unsafe { libc::dup2(dev_null_fd, libc::STDOUT_FILENO); } } if self.log_stderr
else { unsafe { libc::dup2(dev_null_fd, libc::STDERR_FILENO); } } unsafe { libc::close(dev_null_fd); } unsafe { // Close the pipe ends used by our parent. libc::close(self.receive_fd.unwrap()); libc::close(self.send_fd.unwrap()); // Remap fds to the ones used by the forkserver. // The fds might have by chance the correct value, in this case // dup2 & close would actually cause us to close the fd we intended to pass. if child_receive_fd != AFL_READ_FROM_PARENT_FD { let ret = libc::dup2(child_receive_fd, AFL_READ_FROM_PARENT_FD); assert!(ret >= 0); libc::close(child_receive_fd); } if child_send_fd != AFL_WRITE_TO_PARENT_FD { let ret = libc::dup2(child_send_fd, AFL_WRITE_TO_PARENT_FD); assert!(ret >= 0); libc::close(child_send_fd); } } unsafe { if !self.log_stdout
{ //unsafe { // let fd = self.stderr_file.as_ref().unwrap().0.as_raw_fd(); // libc::dup2(fd, libc::STDERR_FILENO); // libc::close(fd); //} }
conditional_block
sink.rs
)).unwrap(); envp.push(shm_env_var.as_ptr()); let mut env_from_config = Vec::new(); if let Some(cfg) = self.config.as_ref() { cfg.sink.env.iter().for_each(|var| { env_from_config .push(CString::new(format!("{}={}", var.0, var.1).as_bytes()).unwrap()) }) } let afl_maps_size = CString::new(format!("AFL_MAP_SIZE={}", self.bitmap().size())).unwrap(); envp.push(afl_maps_size.as_bytes().as_ptr() as *const i8); env_from_config.iter().for_each(|e| { envp.push(e.as_bytes().as_ptr() as *const i8); }); envp.push(std::ptr::null()); let dev_null_fd = unsafe { let path = CString::new("/dev/null".as_bytes()).unwrap(); libc::open(path.as_ptr(), libc::O_RDONLY) }; if dev_null_fd < 0 { panic!("Failed to open /dev/null"); } match self.input_channel { InputChannel::Stdin => unsafe { libc::dup2(self.input_file.0.as_raw_fd(), 0); }, _ => unsafe { libc::dup2(dev_null_fd, 0); }, } if self.log_stdout { // unsafe { // let fd = self.stdout_file.as_ref().unwrap().0.as_raw_fd(); // libc::dup2(fd, libc::STDOUT_FILENO); // libc::close(fd); // } } else { unsafe { libc::dup2(dev_null_fd, libc::STDOUT_FILENO); } } if self.log_stderr { //unsafe { // let fd = self.stderr_file.as_ref().unwrap().0.as_raw_fd(); // libc::dup2(fd, libc::STDERR_FILENO); // libc::close(fd); //} } else { unsafe { libc::dup2(dev_null_fd, libc::STDERR_FILENO); } } unsafe { libc::close(dev_null_fd); } unsafe { // Close the pipe ends used by our parent. libc::close(self.receive_fd.unwrap()); libc::close(self.send_fd.unwrap()); // Remap fds to the ones used by the forkserver. // The fds might have by chance the correct value, in this case // dup2 & close would actually cause us to close the fd we intended to pass. if child_receive_fd != AFL_READ_FROM_PARENT_FD { let ret = libc::dup2(child_receive_fd, AFL_READ_FROM_PARENT_FD); assert!(ret >= 0); libc::close(child_receive_fd); } if child_send_fd != AFL_WRITE_TO_PARENT_FD { let ret = libc::dup2(child_send_fd, AFL_WRITE_TO_PARENT_FD); assert!(ret >= 0); libc::close(child_send_fd); } } unsafe { if !self.log_stdout && !self.log_stderr { // if we log stderr or stdout, the limit will cause our // fuzzer to fail after some time. let mut rlim: libc::rlimit = std::mem::zeroed(); rlim.rlim_cur = n_mib_bytes!(512).try_into().unwrap(); rlim.rlim_max = n_mib_bytes!(512).try_into().unwrap(); let ret = libc::setrlimit(libc::RLIMIT_FSIZE, &rlim as *const libc::rlimit); assert_eq!(ret, 0); } // Disable core dumps let limit_val: libc::rlimit = std::mem::zeroed(); let ret = libc::setrlimit(libc::RLIMIT_CORE, &limit_val); assert_eq!(ret, 0); // Max AS size. let mut rlim: libc::rlimit = std::mem::zeroed(); rlim.rlim_cur = n_mib_bytes!(512).try_into().unwrap(); rlim.rlim_max = n_mib_bytes!(512).try_into().unwrap(); let ret = libc::setrlimit(libc::RLIMIT_AS, &rlim as *const libc::rlimit); assert_eq!(ret, 0); let ret = libc::personality(libc::ADDR_NO_RANDOMIZE as u64); assert_eq!(ret, 0); } if let Err(err) = self.drop_privileges() { log::error!("Failed to drop privileges: {:#?}", err); panic!(); } // Make sure that UID == EUID, since if this is not the case, // ld will ignore LD_PRELOAD which we need to use for targets // that normally load instrumented libraries during runtime. assert_eq!(nix::unistd::getuid(), nix::unistd::geteuid()); assert_eq!(nix::unistd::getegid(), nix::unistd::getegid()); let prog = CString::new(path.as_bytes()).unwrap(); unsafe { libc::execve(prog.as_ptr(), argv.as_ptr(), envp.as_ptr()); } unreachable!("Failed to call execve on '{}'", path); } _ => { /* The parent */ } } /* The parent */ log::info!("Forkserver has pid {}", child_pid); // Note th sid, thus we can kill the child later. // This is a sid since the child calls setsid(). self.forkserver_sid = Some(child_pid); // Close the pipe ends used by the child. unsafe { libc::close(child_receive_fd); libc::close(child_send_fd); } unsafe { libc::fcntl(self.send_fd.unwrap(), libc::F_SETFD, libc::FD_CLOEXEC); libc::fcntl(self.receive_fd.unwrap(), libc::F_SETFD, libc::FD_CLOEXEC); } // Wait for for hello from the child. self.wait_for_data(AFL_DEFAULT_TIMEOUT) .context("Timeout while waiting for forkserver to come up.")?; // Read the available data. let buffer = [0u8; 4]; unsafe { let ret = libc::read( self.receive_fd.unwrap(), buffer.as_ptr() as *mut libc::c_void, 4, ); if ret != 4 { return Err(anyhow!(format!( "Failed to do handshake with forkserver. ret={}", ret ))); } // Process extended attributes used by AFL++. // Sett src/afl-forkserver.c:689 (afl_fsrv_start) let status = u32::from_ne_bytes(buffer); log::info!("Forkserver status: 0x{:x}", status); if status & FS_OPT_MAPSIZE == FS_OPT_MAPSIZE { log::info!("Got extended option FS_OPT_MAPSIZE from forkserver"); let new_map_size = ((status & 0x00fffffe) >> 1) + 1; log::info!("Target requests a map of size {} bytes", new_map_size); log::info!("Current map size is {} bytes", self.bitmap().size()); if self.bitmap_was_resize { log::info!("Already resized, skipping...."); return Ok(()); } let new_map_size = new_map_size.next_power_of_two() as usize; if new_map_size > self.bitmap().size() { log::info!("Resizing bitmap to {} bytes", new_map_size); self.stop(); let new_map = Bitmap::new_in_shm(new_map_size, 0x00); let _ = mem::replace(self.bitmap(), new_map); self.bitmap_was_resize = true; return self.start(); } } } // if self.stdout_file.is_some() { // // Take the the stdout file thus its fd gets dropped. // self.stdout_file.take(); // } // if self.stderr_file.is_some() { // // Take the the stderr file thus its fd gets dropped. // self.stderr_file.take(); // } // We are ready to fuzz! Ok(()) } fn drop_privileges(&mut self) -> Result<()> { let uid_gid = self .config .as_ref() .map(|config| config.general.jail_uid_gid()) .unwrap_or(None); if uid_gid.is_some() { jail::acquire_privileges()?; } if let Some((uid, gid)) = uid_gid { jail::drop_privileges(uid, gid, true)?; } Ok(()) } /// Stops the forksever. Must be called before calling start() again. /// It is save to call this function multiple times. pub fn stop(&mut self) { if let Some(sid) = self.forkserver_sid.take() { unsafe { libc::close(self.send_fd.unwrap()); libc::close(self.receive_fd.unwrap()); let ret = libc::killpg(sid, SIGKILL); assert!(ret == 0); // reap it libc::waitpid(sid, std::ptr::null_mut() as *mut libc::c_int, 0); } } } /// Write the given bytes into the sinks input channel. This function /// is only allowed to be called on sinks with InputChannel::Stdin or InputChannel::File /// input channel. pub fn
write
identifier_name
sink.rs
::RLIMIT_FSIZE, &rlim as *const libc::rlimit); assert_eq!(ret, 0); } // Disable core dumps let limit_val: libc::rlimit = std::mem::zeroed(); let ret = libc::setrlimit(libc::RLIMIT_CORE, &limit_val); assert_eq!(ret, 0); // Max AS size. let mut rlim: libc::rlimit = std::mem::zeroed(); rlim.rlim_cur = n_mib_bytes!(512).try_into().unwrap(); rlim.rlim_max = n_mib_bytes!(512).try_into().unwrap(); let ret = libc::setrlimit(libc::RLIMIT_AS, &rlim as *const libc::rlimit); assert_eq!(ret, 0); let ret = libc::personality(libc::ADDR_NO_RANDOMIZE as u64); assert_eq!(ret, 0); } if let Err(err) = self.drop_privileges() { log::error!("Failed to drop privileges: {:#?}", err); panic!(); } // Make sure that UID == EUID, since if this is not the case, // ld will ignore LD_PRELOAD which we need to use for targets // that normally load instrumented libraries during runtime. assert_eq!(nix::unistd::getuid(), nix::unistd::geteuid()); assert_eq!(nix::unistd::getegid(), nix::unistd::getegid()); let prog = CString::new(path.as_bytes()).unwrap(); unsafe { libc::execve(prog.as_ptr(), argv.as_ptr(), envp.as_ptr()); } unreachable!("Failed to call execve on '{}'", path); } _ => { /* The parent */ } } /* The parent */ log::info!("Forkserver has pid {}", child_pid); // Note th sid, thus we can kill the child later. // This is a sid since the child calls setsid(). self.forkserver_sid = Some(child_pid); // Close the pipe ends used by the child. unsafe { libc::close(child_receive_fd); libc::close(child_send_fd); } unsafe { libc::fcntl(self.send_fd.unwrap(), libc::F_SETFD, libc::FD_CLOEXEC); libc::fcntl(self.receive_fd.unwrap(), libc::F_SETFD, libc::FD_CLOEXEC); } // Wait for for hello from the child. self.wait_for_data(AFL_DEFAULT_TIMEOUT) .context("Timeout while waiting for forkserver to come up.")?; // Read the available data. let buffer = [0u8; 4]; unsafe { let ret = libc::read( self.receive_fd.unwrap(), buffer.as_ptr() as *mut libc::c_void, 4, ); if ret != 4 { return Err(anyhow!(format!( "Failed to do handshake with forkserver. ret={}", ret ))); } // Process extended attributes used by AFL++. // Sett src/afl-forkserver.c:689 (afl_fsrv_start) let status = u32::from_ne_bytes(buffer); log::info!("Forkserver status: 0x{:x}", status); if status & FS_OPT_MAPSIZE == FS_OPT_MAPSIZE { log::info!("Got extended option FS_OPT_MAPSIZE from forkserver"); let new_map_size = ((status & 0x00fffffe) >> 1) + 1; log::info!("Target requests a map of size {} bytes", new_map_size); log::info!("Current map size is {} bytes", self.bitmap().size()); if self.bitmap_was_resize { log::info!("Already resized, skipping...."); return Ok(()); } let new_map_size = new_map_size.next_power_of_two() as usize; if new_map_size > self.bitmap().size() { log::info!("Resizing bitmap to {} bytes", new_map_size); self.stop(); let new_map = Bitmap::new_in_shm(new_map_size, 0x00); let _ = mem::replace(self.bitmap(), new_map); self.bitmap_was_resize = true; return self.start(); } } } // if self.stdout_file.is_some() { // // Take the the stdout file thus its fd gets dropped. // self.stdout_file.take(); // } // if self.stderr_file.is_some() { // // Take the the stderr file thus its fd gets dropped. // self.stderr_file.take(); // } // We are ready to fuzz! Ok(()) } fn drop_privileges(&mut self) -> Result<()> { let uid_gid = self .config .as_ref() .map(|config| config.general.jail_uid_gid()) .unwrap_or(None); if uid_gid.is_some() { jail::acquire_privileges()?; } if let Some((uid, gid)) = uid_gid { jail::drop_privileges(uid, gid, true)?; } Ok(()) } /// Stops the forksever. Must be called before calling start() again. /// It is save to call this function multiple times. pub fn stop(&mut self) { if let Some(sid) = self.forkserver_sid.take() { unsafe { libc::close(self.send_fd.unwrap()); libc::close(self.receive_fd.unwrap()); let ret = libc::killpg(sid, SIGKILL); assert!(ret == 0); // reap it libc::waitpid(sid, std::ptr::null_mut() as *mut libc::c_int, 0); } } } /// Write the given bytes into the sinks input channel. This function /// is only allowed to be called on sinks with InputChannel::Stdin or InputChannel::File /// input channel. pub fn write(&mut self, data: &[u8]) { debug_assert!( self.input_channel == InputChannel::Stdin || self.input_channel == InputChannel::File ); self.input_file.0.seek(SeekFrom::Start(0)).unwrap(); self.input_file.0.set_len(0).unwrap(); self.input_file.0.write_all(data).unwrap(); self.input_file.0.seek(SeekFrom::Start(0)).unwrap(); self.input_file.0.sync_all().unwrap(); } pub fn run(&mut self, timeout: Duration) -> Result<RunResult> { self.bitmap().reset(); let buffer = [0u8; 4]; let buf_ptr = buffer.as_ptr() as *mut libc::c_void; // Tell the forkserver to fork. log::trace!("Requesting fork"); let ret = repeat_on_interrupt(|| unsafe { libc::write(self.send_fd.unwrap(), buf_ptr, 4) }); if ret != 4 { error!("Fork request failed"); return Err(anyhow!("Failed to write to send_fd: {}", ret)); } log::trace!("Waiting for child pid"); self.wait_for_data(AFL_DEFAULT_TIMEOUT) .context("Failed to retrive child pid from forkserver")?; let ret = repeat_on_interrupt(|| unsafe { libc::read(self.receive_fd.unwrap(), buf_ptr, 4) }); if ret != 4 { error!("Failed to retrive child pid"); return Err(anyhow!("Failed to read from receive_non_blocking_fd")); } let child_pid = i32::from_le_bytes(buffer); log::trace!("Got child pid {}", child_pid); if child_pid <= 0 { log::error!("Child pid '{}' is invalid", child_pid); return Err(anyhow!( "Failed to parse child_pid. child_pid={}, bytes={:?}", child_pid, buffer )); } log::trace!("Waiting for child termination"); match self.wait_for_data(timeout) { Ok(_) => (), Err(err) => { log::trace!("Child timed out: {:#?}", err); // Kill the child since it appears to have timed out. let kill_ret = nix::sys::signal::kill( nix::unistd::Pid::from_raw(child_pid), nix::sys::signal::SIGKILL, ); if let Err(ref err) = kill_ret { // This might just be caused by the fact that the child won the race // and terminated before we killed it. log::trace!("Failed to kill child: {:#?}", err); } if let Err(err) = self .wait_for_data(AFL_DEFAULT_TIMEOUT) .context("Child did not acknowledge termination request") { let reason = try_get_child_exit_reason(self.forkserver_sid.unwrap()); log::error!( "Exit reason: {:#?}, child_pid={:?}, kill_ret={:?}", reason, child_pid, kill_ret ); return Err(err.context(format!("child_exit_reason={:#?}", reason))); } // Consume exit status. let ret = unsafe { libc::read(self.receive_fd.unwrap(), buf_ptr, 4) }; if ret != 4 { log::error!("Expected {} != 4", ret); } return Ok(RunResult::TimedOut); } }
log::trace!("Child terminated, getting exit status");
random_line_split
app_store_connect.py
managing Debug Information Files from Apple App Store Connect. Users can instruct Sentry to download dSYM from App Store Connect and put them into Sentry's debug files. These tasks enable this functionality. """ import logging import pathlib import tempfile from typing import List, Mapping, Tuple import requests import sentry_sdk from django.utils import timezone from sentry.lang.native import appconnect from sentry.models import ( AppConnectBuild, LatestAppConnectBuildsCheck, Project, ProjectOption, debugfile, ) from sentry.tasks.base import instrumented_task from sentry.utils import json, metrics, sdk from sentry.utils.appleconnect import appstore_connect as appstoreconnect_api logger = logging.getLogger(__name__)
# Sadly this decorator makes this entire function untyped for now as it does not itself have # typing annotations. So we do all the work outside of the decorated task function to work # around this. # Since all these args must be pickled we keep them to built-in types as well. @instrumented_task(name="sentry.tasks.app_store_connect.dsym_download", queue="appstoreconnect", ignore_result=True) # type: ignore def dsym_download(project_id: int, config_id: str) -> None: inner_dsym_download(project_id=project_id, config_id=config_id) def inner_dsym_download(project_id: int, config_id: str) -> None: """Downloads the dSYMs from App Store Connect and stores them in the Project's debug files.""" with sdk.configure_scope() as scope: scope.set_tag("project", project_id) scope.set_tag("config_id", config_id) project = Project.objects.get(pk=project_id) config = appconnect.AppStoreConnectConfig.from_project_config(project, config_id) client = appconnect.AppConnectClient.from_config(config) listed_builds = client.list_builds() builds = process_builds(project=project, config=config, to_process=listed_builds) if not builds: return for i, (build, build_state) in enumerate(builds): with sdk.configure_scope() as scope: scope.set_context("dsym_downloads", {"total": len(builds), "completed": i}) with tempfile.NamedTemporaryFile() as dsyms_zip: try: client.download_dsyms(build, pathlib.Path(dsyms_zip.name)) # For no dSYMs, let the build be marked as fetched so they're not # repeatedly re-checked every time this task is run. except appconnect.NoDsymsError: logger.debug("No dSYMs for build %s", build) # Moves on to the next build so we don't check off fetched. This url will # eventuallyTM be populated, so revisit it at a later time. except appconnect.PendingDsymsError: logger.debug("dSYM url currently unavailable for build %s", build) continue # early-return in unauthorized and forbidden to avoid trying all the other builds # as well, since an expired token will error for all of them. # the error is also swallowed unreported because this is an expected and actionable # error. except appstoreconnect_api.UnauthorizedError: sentry_sdk.capture_message( "Not authorized to download dSYM using current App Store Connect credentials", level="info", ) return except appstoreconnect_api.ForbiddenError: sentry_sdk.capture_message( "Forbidden from downloading dSYM using current App Store Connect credentials", level="info", ) return # Don't let malformed URLs abort all pending downloads in case it's an isolated instance except ValueError as e: sdk.capture_exception(e) continue # Assume request errors are a server side issue and do not abort all the # pending downloads. except appstoreconnect_api.RequestError as e: sdk.capture_exception(e) continue except requests.RequestException as e: sdk.capture_exception(e) continue else: create_difs_from_dsyms_zip(dsyms_zip.name, project) logger.debug("Uploaded dSYMs for build %s", build) metrics.incr("tasks.app_store_connect.builds_ingested", sample_rate=1) build_state.fetched = True build_state.save() def create_difs_from_dsyms_zip(dsyms_zip: str, project: Project) -> None: with sentry_sdk.start_span(op="dsym-difs", description="Extract difs dSYM zip"): with open(dsyms_zip, "rb") as fp: created = debugfile.create_files_from_dif_zip(fp, project, accept_unknown=True) for proj_debug_file in created: logger.debug("Created %r for project %s", proj_debug_file, project.id) def get_or_create_persisted_build( project: Project, config: appconnect.AppStoreConnectConfig, build: appconnect.BuildInfo ) -> AppConnectBuild: """Fetches the sentry-internal :class:`AppConnectBuild`. The build corresponds to the :class:`appconnect.BuildInfo` as returned by the AppStore Connect API. If no build exists yet, a new "pending" build is created. """ try: build_state = AppConnectBuild.objects.get( project=project, app_id=build.app_id, platform=build.platform, bundle_short_version=build.version, bundle_version=build.build_number, ) except AppConnectBuild.DoesNotExist: build_state = AppConnectBuild( project=project, app_id=build.app_id, bundle_id=config.bundleId, platform=build.platform, bundle_short_version=build.version, bundle_version=build.build_number, uploaded_to_appstore=build.uploaded_date, first_seen=timezone.now(), fetched=False, ) build_state.save() return build_state def process_builds( project: Project, config: appconnect.AppStoreConnectConfig, to_process: List[appconnect.BuildInfo], ) -> List[Tuple[appconnect.BuildInfo, AppConnectBuild]]: """Returns a list of builds whose dSYMs need to be updated or fetched. This will create a new "pending" :class:`AppConnectBuild` for any :class:`appconnect.BuildInfo` that cannot be found in the DB. These pending :class:`AppConnectBuild`s are immediately saved upon creation. """ pending_builds = [] with sentry_sdk.start_span( op="appconnect-update-builds", description="Update AppStoreConnect builds in database" ): for build in to_process: build_state = get_or_create_persisted_build(project, config, build) if not build_state.fetched: pending_builds.append((build, build_state)) LatestAppConnectBuildsCheck.objects.create_or_update( project=project, source_id=config.id, values={"last_checked": timezone.now()} ) return pending_builds # Untyped decorator would stop type-checking of entire function, split into an inner # function instead which can be type checked. @instrumented_task( # type: ignore name="sentry.tasks.app_store_connect.refresh_all_builds", queue="appstoreconnect", ignore_result=True, ) def refresh_all_builds() -> None: inner_refresh_all_builds() def inner_refresh_all_builds() -> None: """Refreshes all AppStoreConnect builds for all projects. This iterates over all the projects configured in Sentry and for any which has an AppStoreConnect symbol source configured will poll the AppStoreConnect API to check if there are new builds. """ # We have no way to query for AppStore Connect symbol sources directly, but # getting all of the project options that have custom symbol sources # configured is a reasonable compromise, as the number of those should be # low enough to traverse every hour. # Another alternative would be to get a list of projects that have had a # previous successful import, as indicated by existing `AppConnectBuild` # objects. But that would miss projects that have a valid AppStore Connect # setup, but have not yet published any kind of build to AppStore. options = ProjectOption.objects.filter(key=appconnect.SYMBOL_SOURCES_PROP_NAME) count = 0 for option in options: with sdk.push_scope() as scope: scope.set_tag("project", option.project_id) try: if not option.value: # An empty string set as option value, the UI does this when deleting # all sources. This is not valid JSON. continue # We are parsing JSON thus all types are Any, so give the type-checker some # extra help. We are maybe slightly lying about the type, but the # attributes we do access are all string values. all_sources: List[Mapping[str, str]] = json.loads(option.value) for source in all_sources: try: source_id = source["id"] source_type = source["type"] except KeyError: logger.exception("Malformed symbol source") continue if source_type == appconnect.SYMBOL_SOURCE_TYPE_NAME: dsym_download.apply_async( kwargs={ "project_id": option.project_id, "config_id": source_id, } ) count += 1 except Exception: logger.exception("Failed to refresh AppStoreConnect builds") metrics.gauge("tasks.app_store_connect.refresh
random_line_split
app_store_connect.py
managing Debug Information Files from Apple App Store Connect. Users can instruct Sentry to download dSYM from App Store Connect and put them into Sentry's debug files. These tasks enable this functionality. """ import logging import pathlib import tempfile from typing import List, Mapping, Tuple import requests import sentry_sdk from django.utils import timezone from sentry.lang.native import appconnect from sentry.models import ( AppConnectBuild, LatestAppConnectBuildsCheck, Project, ProjectOption, debugfile, ) from sentry.tasks.base import instrumented_task from sentry.utils import json, metrics, sdk from sentry.utils.appleconnect import appstore_connect as appstoreconnect_api logger = logging.getLogger(__name__) # Sadly this decorator makes this entire function untyped for now as it does not itself have # typing annotations. So we do all the work outside of the decorated task function to work # around this. # Since all these args must be pickled we keep them to built-in types as well. @instrumented_task(name="sentry.tasks.app_store_connect.dsym_download", queue="appstoreconnect", ignore_result=True) # type: ignore def dsym_download(project_id: int, config_id: str) -> None: inner_dsym_download(project_id=project_id, config_id=config_id) def inner_dsym_download(project_id: int, config_id: str) -> None: """Downloads the dSYMs from App Store Connect and stores them in the Project's debug files.""" with sdk.configure_scope() as scope: scope.set_tag("project", project_id) scope.set_tag("config_id", config_id) project = Project.objects.get(pk=project_id) config = appconnect.AppStoreConnectConfig.from_project_config(project, config_id) client = appconnect.AppConnectClient.from_config(config) listed_builds = client.list_builds() builds = process_builds(project=project, config=config, to_process=listed_builds) if not builds: return for i, (build, build_state) in enumerate(builds):
"Not authorized to download dSYM using current App Store Connect credentials", level="info", ) return except appstoreconnect_api.ForbiddenError: sentry_sdk.capture_message( "Forbidden from downloading dSYM using current App Store Connect credentials", level="info", ) return # Don't let malformed URLs abort all pending downloads in case it's an isolated instance except ValueError as e: sdk.capture_exception(e) continue # Assume request errors are a server side issue and do not abort all the # pending downloads. except appstoreconnect_api.RequestError as e: sdk.capture_exception(e) continue except requests.RequestException as e: sdk.capture_exception(e) continue else: create_difs_from_dsyms_zip(dsyms_zip.name, project) logger.debug("Uploaded dSYMs for build %s", build) metrics.incr("tasks.app_store_connect.builds_ingested", sample_rate=1) build_state.fetched = True build_state.save() def create_difs_from_dsyms_zip(dsyms_zip: str, project: Project) -> None: with sentry_sdk.start_span(op="dsym-difs", description="Extract difs dSYM zip"): with open(dsyms_zip, "rb") as fp: created = debugfile.create_files_from_dif_zip(fp, project, accept_unknown=True) for proj_debug_file in created: logger.debug("Created %r for project %s", proj_debug_file, project.id) def get_or_create_persisted_build( project: Project, config: appconnect.AppStoreConnectConfig, build: appconnect.BuildInfo ) -> AppConnectBuild: """Fetches the sentry-internal :class:`AppConnectBuild`. The build corresponds to the :class:`appconnect.BuildInfo` as returned by the AppStore Connect API. If no build exists yet, a new "pending" build is created. """ try: build_state = AppConnectBuild.objects.get( project=project, app_id=build.app_id, platform=build.platform, bundle_short_version=build.version, bundle_version=build.build_number, ) except AppConnectBuild.DoesNotExist: build_state = AppConnectBuild( project=project, app_id=build.app_id, bundle_id=config.bundleId, platform=build.platform, bundle_short_version=build.version, bundle_version=build.build_number, uploaded_to_appstore=build.uploaded_date, first_seen=timezone.now(), fetched=False, ) build_state.save() return build_state def process_builds( project: Project, config: appconnect.AppStoreConnectConfig, to_process: List[appconnect.BuildInfo], ) -> List[Tuple[appconnect.BuildInfo, AppConnectBuild]]: """Returns a list of builds whose dSYMs need to be updated or fetched. This will create a new "pending" :class:`AppConnectBuild` for any :class:`appconnect.BuildInfo` that cannot be found in the DB. These pending :class:`AppConnectBuild`s are immediately saved upon creation. """ pending_builds = [] with sentry_sdk.start_span( op="appconnect-update-builds", description="Update AppStoreConnect builds in database" ): for build in to_process: build_state = get_or_create_persisted_build(project, config, build) if not build_state.fetched: pending_builds.append((build, build_state)) LatestAppConnectBuildsCheck.objects.create_or_update( project=project, source_id=config.id, values={"last_checked": timezone.now()} ) return pending_builds # Untyped decorator would stop type-checking of entire function, split into an inner # function instead which can be type checked. @instrumented_task( # type: ignore name="sentry.tasks.app_store_connect.refresh_all_builds", queue="appstoreconnect", ignore_result=True, ) def refresh_all_builds() -> None: inner_refresh_all_builds() def inner_refresh_all_builds() -> None: """Refreshes all AppStoreConnect builds for all projects. This iterates over all the projects configured in Sentry and for any which has an AppStoreConnect symbol source configured will poll the AppStoreConnect API to check if there are new builds. """ # We have no way to query for AppStore Connect symbol sources directly, but # getting all of the project options that have custom symbol sources # configured is a reasonable compromise, as the number of those should be # low enough to traverse every hour. # Another alternative would be to get a list of projects that have had a # previous successful import, as indicated by existing `AppConnectBuild` # objects. But that would miss projects that have a valid AppStore Connect # setup, but have not yet published any kind of build to AppStore. options = ProjectOption.objects.filter(key=appconnect.SYMBOL_SOURCES_PROP_NAME) count = 0 for option in options: with sdk.push_scope() as scope: scope.set_tag("project", option.project_id) try: if not option.value: # An empty string set as option value, the UI does this when deleting # all sources. This is not valid JSON. continue # We are parsing JSON thus all types are Any, so give the type-checker some # extra help. We are maybe slightly lying about the type, but the # attributes we do access are all string values. all_sources: List[Mapping[str, str]] = json.loads(option.value) for source in all_sources: try: source_id = source["id"] source_type = source["type"] except KeyError: logger.exception("Malformed symbol source") continue if source_type == appconnect.SYMBOL_SOURCE_TYPE_NAME: dsym_download.apply_async( kwargs={ "project_id": option.project_id, "config_id": source_id, } ) count += 1 except Exception: logger.exception("Failed to refresh AppStoreConnect builds") metrics.gauge("tasks.app_store_connect.refreshed
with sdk.configure_scope() as scope: scope.set_context("dsym_downloads", {"total": len(builds), "completed": i}) with tempfile.NamedTemporaryFile() as dsyms_zip: try: client.download_dsyms(build, pathlib.Path(dsyms_zip.name)) # For no dSYMs, let the build be marked as fetched so they're not # repeatedly re-checked every time this task is run. except appconnect.NoDsymsError: logger.debug("No dSYMs for build %s", build) # Moves on to the next build so we don't check off fetched. This url will # eventuallyTM be populated, so revisit it at a later time. except appconnect.PendingDsymsError: logger.debug("dSYM url currently unavailable for build %s", build) continue # early-return in unauthorized and forbidden to avoid trying all the other builds # as well, since an expired token will error for all of them. # the error is also swallowed unreported because this is an expected and actionable # error. except appstoreconnect_api.UnauthorizedError: sentry_sdk.capture_message(
conditional_block
app_store_connect.py
managing Debug Information Files from Apple App Store Connect. Users can instruct Sentry to download dSYM from App Store Connect and put them into Sentry's debug files. These tasks enable this functionality. """ import logging import pathlib import tempfile from typing import List, Mapping, Tuple import requests import sentry_sdk from django.utils import timezone from sentry.lang.native import appconnect from sentry.models import ( AppConnectBuild, LatestAppConnectBuildsCheck, Project, ProjectOption, debugfile, ) from sentry.tasks.base import instrumented_task from sentry.utils import json, metrics, sdk from sentry.utils.appleconnect import appstore_connect as appstoreconnect_api logger = logging.getLogger(__name__) # Sadly this decorator makes this entire function untyped for now as it does not itself have # typing annotations. So we do all the work outside of the decorated task function to work # around this. # Since all these args must be pickled we keep them to built-in types as well. @instrumented_task(name="sentry.tasks.app_store_connect.dsym_download", queue="appstoreconnect", ignore_result=True) # type: ignore def dsym_download(project_id: int, config_id: str) -> None: inner_dsym_download(project_id=project_id, config_id=config_id) def inner_dsym_download(project_id: int, config_id: str) -> None: """Downloads the dSYMs from App Store Connect and stores them in the Project's debug files.""" with sdk.configure_scope() as scope: scope.set_tag("project", project_id) scope.set_tag("config_id", config_id) project = Project.objects.get(pk=project_id) config = appconnect.AppStoreConnectConfig.from_project_config(project, config_id) client = appconnect.AppConnectClient.from_config(config) listed_builds = client.list_builds() builds = process_builds(project=project, config=config, to_process=listed_builds) if not builds: return for i, (build, build_state) in enumerate(builds): with sdk.configure_scope() as scope: scope.set_context("dsym_downloads", {"total": len(builds), "completed": i}) with tempfile.NamedTemporaryFile() as dsyms_zip: try: client.download_dsyms(build, pathlib.Path(dsyms_zip.name)) # For no dSYMs, let the build be marked as fetched so they're not # repeatedly re-checked every time this task is run. except appconnect.NoDsymsError: logger.debug("No dSYMs for build %s", build) # Moves on to the next build so we don't check off fetched. This url will # eventuallyTM be populated, so revisit it at a later time. except appconnect.PendingDsymsError: logger.debug("dSYM url currently unavailable for build %s", build) continue # early-return in unauthorized and forbidden to avoid trying all the other builds # as well, since an expired token will error for all of them. # the error is also swallowed unreported because this is an expected and actionable # error. except appstoreconnect_api.UnauthorizedError: sentry_sdk.capture_message( "Not authorized to download dSYM using current App Store Connect credentials", level="info", ) return except appstoreconnect_api.ForbiddenError: sentry_sdk.capture_message( "Forbidden from downloading dSYM using current App Store Connect credentials", level="info", ) return # Don't let malformed URLs abort all pending downloads in case it's an isolated instance except ValueError as e: sdk.capture_exception(e) continue # Assume request errors are a server side issue and do not abort all the # pending downloads. except appstoreconnect_api.RequestError as e: sdk.capture_exception(e) continue except requests.RequestException as e: sdk.capture_exception(e) continue else: create_difs_from_dsyms_zip(dsyms_zip.name, project) logger.debug("Uploaded dSYMs for build %s", build) metrics.incr("tasks.app_store_connect.builds_ingested", sample_rate=1) build_state.fetched = True build_state.save() def create_difs_from_dsyms_zip(dsyms_zip: str, project: Project) -> None:
def get_or_create_persisted_build( project: Project, config: appconnect.AppStoreConnectConfig, build: appconnect.BuildInfo ) -> AppConnectBuild: """Fetches the sentry-internal :class:`AppConnectBuild`. The build corresponds to the :class:`appconnect.BuildInfo` as returned by the AppStore Connect API. If no build exists yet, a new "pending" build is created. """ try: build_state = AppConnectBuild.objects.get( project=project, app_id=build.app_id, platform=build.platform, bundle_short_version=build.version, bundle_version=build.build_number, ) except AppConnectBuild.DoesNotExist: build_state = AppConnectBuild( project=project, app_id=build.app_id, bundle_id=config.bundleId, platform=build.platform, bundle_short_version=build.version, bundle_version=build.build_number, uploaded_to_appstore=build.uploaded_date, first_seen=timezone.now(), fetched=False, ) build_state.save() return build_state def process_builds( project: Project, config: appconnect.AppStoreConnectConfig, to_process: List[appconnect.BuildInfo], ) -> List[Tuple[appconnect.BuildInfo, AppConnectBuild]]: """Returns a list of builds whose dSYMs need to be updated or fetched. This will create a new "pending" :class:`AppConnectBuild` for any :class:`appconnect.BuildInfo` that cannot be found in the DB. These pending :class:`AppConnectBuild`s are immediately saved upon creation. """ pending_builds = [] with sentry_sdk.start_span( op="appconnect-update-builds", description="Update AppStoreConnect builds in database" ): for build in to_process: build_state = get_or_create_persisted_build(project, config, build) if not build_state.fetched: pending_builds.append((build, build_state)) LatestAppConnectBuildsCheck.objects.create_or_update( project=project, source_id=config.id, values={"last_checked": timezone.now()} ) return pending_builds # Untyped decorator would stop type-checking of entire function, split into an inner # function instead which can be type checked. @instrumented_task( # type: ignore name="sentry.tasks.app_store_connect.refresh_all_builds", queue="appstoreconnect", ignore_result=True, ) def refresh_all_builds() -> None: inner_refresh_all_builds() def inner_refresh_all_builds() -> None: """Refreshes all AppStoreConnect builds for all projects. This iterates over all the projects configured in Sentry and for any which has an AppStoreConnect symbol source configured will poll the AppStoreConnect API to check if there are new builds. """ # We have no way to query for AppStore Connect symbol sources directly, but # getting all of the project options that have custom symbol sources # configured is a reasonable compromise, as the number of those should be # low enough to traverse every hour. # Another alternative would be to get a list of projects that have had a # previous successful import, as indicated by existing `AppConnectBuild` # objects. But that would miss projects that have a valid AppStore Connect # setup, but have not yet published any kind of build to AppStore. options = ProjectOption.objects.filter(key=appconnect.SYMBOL_SOURCES_PROP_NAME) count = 0 for option in options: with sdk.push_scope() as scope: scope.set_tag("project", option.project_id) try: if not option.value: # An empty string set as option value, the UI does this when deleting # all sources. This is not valid JSON. continue # We are parsing JSON thus all types are Any, so give the type-checker some # extra help. We are maybe slightly lying about the type, but the # attributes we do access are all string values. all_sources: List[Mapping[str, str]] = json.loads(option.value) for source in all_sources: try: source_id = source["id"] source_type = source["type"] except KeyError: logger.exception("Malformed symbol source") continue if source_type == appconnect.SYMBOL_SOURCE_TYPE_NAME: dsym_download.apply_async( kwargs={ "project_id": option.project_id, "config_id": source_id, } ) count += 1 except Exception: logger.exception("Failed to refresh AppStoreConnect builds") metrics.gauge("tasks.app_store_connect.refresh
with sentry_sdk.start_span(op="dsym-difs", description="Extract difs dSYM zip"): with open(dsyms_zip, "rb") as fp: created = debugfile.create_files_from_dif_zip(fp, project, accept_unknown=True) for proj_debug_file in created: logger.debug("Created %r for project %s", proj_debug_file, project.id)
identifier_body
app_store_connect.py
managing Debug Information Files from Apple App Store Connect. Users can instruct Sentry to download dSYM from App Store Connect and put them into Sentry's debug files. These tasks enable this functionality. """ import logging import pathlib import tempfile from typing import List, Mapping, Tuple import requests import sentry_sdk from django.utils import timezone from sentry.lang.native import appconnect from sentry.models import ( AppConnectBuild, LatestAppConnectBuildsCheck, Project, ProjectOption, debugfile, ) from sentry.tasks.base import instrumented_task from sentry.utils import json, metrics, sdk from sentry.utils.appleconnect import appstore_connect as appstoreconnect_api logger = logging.getLogger(__name__) # Sadly this decorator makes this entire function untyped for now as it does not itself have # typing annotations. So we do all the work outside of the decorated task function to work # around this. # Since all these args must be pickled we keep them to built-in types as well. @instrumented_task(name="sentry.tasks.app_store_connect.dsym_download", queue="appstoreconnect", ignore_result=True) # type: ignore def dsym_download(project_id: int, config_id: str) -> None: inner_dsym_download(project_id=project_id, config_id=config_id) def inner_dsym_download(project_id: int, config_id: str) -> None: """Downloads the dSYMs from App Store Connect and stores them in the Project's debug files.""" with sdk.configure_scope() as scope: scope.set_tag("project", project_id) scope.set_tag("config_id", config_id) project = Project.objects.get(pk=project_id) config = appconnect.AppStoreConnectConfig.from_project_config(project, config_id) client = appconnect.AppConnectClient.from_config(config) listed_builds = client.list_builds() builds = process_builds(project=project, config=config, to_process=listed_builds) if not builds: return for i, (build, build_state) in enumerate(builds): with sdk.configure_scope() as scope: scope.set_context("dsym_downloads", {"total": len(builds), "completed": i}) with tempfile.NamedTemporaryFile() as dsyms_zip: try: client.download_dsyms(build, pathlib.Path(dsyms_zip.name)) # For no dSYMs, let the build be marked as fetched so they're not # repeatedly re-checked every time this task is run. except appconnect.NoDsymsError: logger.debug("No dSYMs for build %s", build) # Moves on to the next build so we don't check off fetched. This url will # eventuallyTM be populated, so revisit it at a later time. except appconnect.PendingDsymsError: logger.debug("dSYM url currently unavailable for build %s", build) continue # early-return in unauthorized and forbidden to avoid trying all the other builds # as well, since an expired token will error for all of them. # the error is also swallowed unreported because this is an expected and actionable # error. except appstoreconnect_api.UnauthorizedError: sentry_sdk.capture_message( "Not authorized to download dSYM using current App Store Connect credentials", level="info", ) return except appstoreconnect_api.ForbiddenError: sentry_sdk.capture_message( "Forbidden from downloading dSYM using current App Store Connect credentials", level="info", ) return # Don't let malformed URLs abort all pending downloads in case it's an isolated instance except ValueError as e: sdk.capture_exception(e) continue # Assume request errors are a server side issue and do not abort all the # pending downloads. except appstoreconnect_api.RequestError as e: sdk.capture_exception(e) continue except requests.RequestException as e: sdk.capture_exception(e) continue else: create_difs_from_dsyms_zip(dsyms_zip.name, project) logger.debug("Uploaded dSYMs for build %s", build) metrics.incr("tasks.app_store_connect.builds_ingested", sample_rate=1) build_state.fetched = True build_state.save() def create_difs_from_dsyms_zip(dsyms_zip: str, project: Project) -> None: with sentry_sdk.start_span(op="dsym-difs", description="Extract difs dSYM zip"): with open(dsyms_zip, "rb") as fp: created = debugfile.create_files_from_dif_zip(fp, project, accept_unknown=True) for proj_debug_file in created: logger.debug("Created %r for project %s", proj_debug_file, project.id) def get_or_create_persisted_build( project: Project, config: appconnect.AppStoreConnectConfig, build: appconnect.BuildInfo ) -> AppConnectBuild: """Fetches the sentry-internal :class:`AppConnectBuild`. The build corresponds to the :class:`appconnect.BuildInfo` as returned by the AppStore Connect API. If no build exists yet, a new "pending" build is created. """ try: build_state = AppConnectBuild.objects.get( project=project, app_id=build.app_id, platform=build.platform, bundle_short_version=build.version, bundle_version=build.build_number, ) except AppConnectBuild.DoesNotExist: build_state = AppConnectBuild( project=project, app_id=build.app_id, bundle_id=config.bundleId, platform=build.platform, bundle_short_version=build.version, bundle_version=build.build_number, uploaded_to_appstore=build.uploaded_date, first_seen=timezone.now(), fetched=False, ) build_state.save() return build_state def process_builds( project: Project, config: appconnect.AppStoreConnectConfig, to_process: List[appconnect.BuildInfo], ) -> List[Tuple[appconnect.BuildInfo, AppConnectBuild]]: """Returns a list of builds whose dSYMs need to be updated or fetched. This will create a new "pending" :class:`AppConnectBuild` for any :class:`appconnect.BuildInfo` that cannot be found in the DB. These pending :class:`AppConnectBuild`s are immediately saved upon creation. """ pending_builds = [] with sentry_sdk.start_span( op="appconnect-update-builds", description="Update AppStoreConnect builds in database" ): for build in to_process: build_state = get_or_create_persisted_build(project, config, build) if not build_state.fetched: pending_builds.append((build, build_state)) LatestAppConnectBuildsCheck.objects.create_or_update( project=project, source_id=config.id, values={"last_checked": timezone.now()} ) return pending_builds # Untyped decorator would stop type-checking of entire function, split into an inner # function instead which can be type checked. @instrumented_task( # type: ignore name="sentry.tasks.app_store_connect.refresh_all_builds", queue="appstoreconnect", ignore_result=True, ) def refresh_all_builds() -> None: inner_refresh_all_builds() def
() -> None: """Refreshes all AppStoreConnect builds for all projects. This iterates over all the projects configured in Sentry and for any which has an AppStoreConnect symbol source configured will poll the AppStoreConnect API to check if there are new builds. """ # We have no way to query for AppStore Connect symbol sources directly, but # getting all of the project options that have custom symbol sources # configured is a reasonable compromise, as the number of those should be # low enough to traverse every hour. # Another alternative would be to get a list of projects that have had a # previous successful import, as indicated by existing `AppConnectBuild` # objects. But that would miss projects that have a valid AppStore Connect # setup, but have not yet published any kind of build to AppStore. options = ProjectOption.objects.filter(key=appconnect.SYMBOL_SOURCES_PROP_NAME) count = 0 for option in options: with sdk.push_scope() as scope: scope.set_tag("project", option.project_id) try: if not option.value: # An empty string set as option value, the UI does this when deleting # all sources. This is not valid JSON. continue # We are parsing JSON thus all types are Any, so give the type-checker some # extra help. We are maybe slightly lying about the type, but the # attributes we do access are all string values. all_sources: List[Mapping[str, str]] = json.loads(option.value) for source in all_sources: try: source_id = source["id"] source_type = source["type"] except KeyError: logger.exception("Malformed symbol source") continue if source_type == appconnect.SYMBOL_SOURCE_TYPE_NAME: dsym_download.apply_async( kwargs={ "project_id": option.project_id, "config_id": source_id, } ) count += 1 except Exception: logger.exception("Failed to refresh AppStoreConnect builds") metrics.gauge("tasks.app_store_connect.refresh
inner_refresh_all_builds
identifier_name
player.rs
(&self) -> i64 { self.length } } #[derive(Debug)] pub enum Event { PlayerShutDown, PlaybackStatusChange(PlaybackStatus), Seeked { position: Duration }, MetadataChange(Option<Metadata>), } #[derive(Debug)] pub struct Progress { /// If player is stopped, metadata will be None metadata: Option<Metadata>, playback_status: PlaybackStatus, /// When this Progress was constructed, in order to calculate how old it is. instant: Instant, /// Position at the time of construction position: Duration, } impl Progress { pub fn new( playback_status: PlaybackStatus, position: Duration, metadata: Option<Metadata>, ) -> Progress { Progress { metadata, playback_status, instant: Instant::now(), position, } } pub fn metadata(&self) -> &Option<Metadata> { &self.metadata } pub fn take_metadata(self) -> Option<Metadata> { self.metadata } pub fn playback_status(&self) -> PlaybackStatus { self.playback_status } pub fn instant(&self) -> Instant { self.instant } pub fn position(&self) -> Duration { self.position } } fn query_player_property<T>(p: &ConnectionProxy, name: &str) -> Result<T, String> where for<'b> T: dbus::arg::Get<'b>, { p.get("org.mpris.MediaPlayer2.Player", name) .map_err(|e| e.to_string()) } pub fn query_player_position(p: &ConnectionProxy) -> Result<Duration, String> { let v = query_player_property::<i64>(p, "Position")?; if v < 0 { panic!("Wrong position value"); } Ok(Duration::from_micros(v.try_into().unwrap())) } fn query_player_playback_status(p: &ConnectionProxy) -> Result<PlaybackStatus, String> { query_player_property::<String>(p, "PlaybackStatus").map(|v| parse_playback_status(&v)) } fn parse_player_metadata<T: arg::RefArg>( metadata_map: HashMap<String, T>, ) -> Result<Option<Metadata>, String> { debug!("metadata_map = {:?}", metadata_map); let file_path_encoded = match metadata_map.get("xesam:url") { Some(url) => url .as_str() .ok_or("url metadata should be string")? .to_string(), // If playlist has reached end, new metadata event is sent, // but it doesn't contain any of the following keys None => return Ok(None), }; let file_path_url = Url::parse(&file_path_encoded) .map_err(|e| format!("invalid format of url metadata: {}", e.to_string()))?; let file_path = file_path_url .to_file_path() .map_err(|_| format!("invalid format of url metadata: {}", file_path_url))?; let album = metadata_map .get("xesam:album") .map(|v| { v.as_str() .ok_or("album metadata should be string") .map(|x| x.to_string()) }) .transpose()?; let title = metadata_map["xesam:title"] .as_str() .ok_or("title metadata should be string")? .to_string(); let length = metadata_map["mpris:length"] .as_i64() .ok_or("length metadata should be i64")?; let artists = metadata_map .get("xesam:artist") .map(|v| { v.as_iter() .ok_or("artist metadata should be iterator")? .next() .ok_or("artist metadata should contain at least one entry")? .as_iter() .ok_or("artist metadata should have nested iterator")? .map(|x| { Ok(x.as_str() .ok_or("artist metadata values should be string")? .to_string()) }) .collect::<Result<Vec<String>, &'static str>>() }) .transpose()?; Ok(Some(Metadata { album, title, artists, file_path, length, })) } fn query_player_metadata(p: &ConnectionProxy) -> Result<Option<Metadata>, String> { query_player_property::<DbusStringMap>(p, "Metadata").and_then(parse_player_metadata) } pub fn query_progress(p: &ConnectionProxy) -> Result<Progress, String> { let playback_status = query_player_playback_status(p)?; let position = query_player_position(p)?; let instant = Instant::now(); let metadata = if playback_status != PlaybackStatus::Stopped { query_player_metadata(p)? } else { None }; Ok(Progress { metadata, playback_status, instant, position, }) } fn parse_playback_status(playback_status: &str) -> PlaybackStatus { match playback_status { "Playing" => PlaybackStatus::Playing, "Paused" => PlaybackStatus::Paused, "Stopped" => PlaybackStatus::Stopped, _ => panic!(""), } } fn query_unique_owner_name<S: Into<String>>(c: &Connection, bus_name: S) -> Result<String, String> { let get_name_owner = Message::new_method_call( "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", ) .map_err(|e| e.to_string())? .append1(bus_name.into());
.map(|reply| { reply .get1() .expect("GetNameOwner must have name as first member") }) } fn query_all_player_buses(c: &Connection) -> Result<Vec<String>, String> { let list_names = Message::new_method_call( "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListNames", )?; let reply = c .send_with_reply_and_block(list_names, Duration::from_millis(500)) .map_err(|e| e.to_string())?; let names: arg::Array<&str, _> = reply.read1().map_err(|e| e.to_string())?; Ok(names .filter(|name| name.starts_with(MPRIS2_PREFIX)) .map(|str_ref| str_ref.to_owned()) .collect()) } fn get_message_item_dict( a: &arg::Variant<Box<dyn arg::RefArg>>, ) -> HashMap<String, Box<&dyn arg::RefArg>> { let mut it = a.as_iter().unwrap(); let d_variant = it.next().unwrap(); let d_it = d_variant.as_iter().unwrap(); let v = d_it.collect::<Vec<_>>(); v.chunks(2) .map(|c| { let key = c[0].as_str().unwrap(); (key.to_string(), Box::new(c[1])) }) .collect() } #[derive(Debug)] pub struct DbusPropertiesChangedHappened { pub interface_name: String, pub changed_properties: DbusStringMap, pub invalidated_properties: Vec<String>, } impl dbus::message::SignalArgs for DbusPropertiesChangedHappened { const NAME: &'static str = "PropertiesChanged"; const INTERFACE: &'static str = "org.freedesktop.DBus.Properties"; } impl arg::ReadAll for DbusPropertiesChangedHappened { fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { Ok(Self { interface_name: i.read()?, changed_properties: i.read()?, invalidated_properties: i.read()?, }) } } #[derive(Debug)] pub struct MediaPlayer2SeekedHappened { pub position_us: i64, } impl dbus::message::SignalArgs for MediaPlayer2SeekedHappened { const NAME: &'static str = "Seeked"; const INTERFACE: &'static str = "org.mpris.MediaPlayer2.Player"; } impl arg::ReadAll for MediaPlayer2SeekedHappened { fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { Ok(Self { position_us: i.read()?, }) } } #[derive(Debug)] pub struct DbusNameOwnedChanged { pub name: String, pub new_owner: String, pub old_owner: String, } impl dbus::message::SignalArgs for DbusNameOwnedChanged { const NAME: &'static str = "NameOwnerChanged"; const INTERFACE: &'static str = "org.freedesktop.DBus"; } impl arg::ReadAll for DbusNameOwnedChanged { fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { Ok(Self { name: i.read()?, new_owner: i.read()?, old_owner: i.read()?, }) } } pub fn get_connection_proxy<'a>( c: &'a Connection, player_owner_name: &'a str, ) -> ConnectionProxy<'a> { c.with_proxy(player_owner_name, MPRIS2_PATH, Duration::from_millis(5000)) } fn get_mediaplayer2_seeked_handler(
c.send_with_reply_and_block(get_name_owner, Duration::from_millis(100)) .map_err(|e| e.to_string())
random_line_split
player.rs
self) -> i64 { self.length } } #[derive(Debug)] pub enum Event { PlayerShutDown, PlaybackStatusChange(PlaybackStatus), Seeked { position: Duration }, MetadataChange(Option<Metadata>), } #[derive(Debug)] pub struct Progress { /// If player is stopped, metadata will be None metadata: Option<Metadata>, playback_status: PlaybackStatus, /// When this Progress was constructed, in order to calculate how old it is. instant: Instant, /// Position at the time of construction position: Duration, } impl Progress { pub fn new( playback_status: PlaybackStatus, position: Duration, metadata: Option<Metadata>, ) -> Progress { Progress { metadata, playback_status, instant: Instant::now(), position, } } pub fn metadata(&self) -> &Option<Metadata> { &self.metadata } pub fn take_metadata(self) -> Option<Metadata> { self.metadata } pub fn playback_status(&self) -> PlaybackStatus { self.playback_status } pub fn instant(&self) -> Instant { self.instant } pub fn position(&self) -> Duration { self.position } } fn query_player_property<T>(p: &ConnectionProxy, name: &str) -> Result<T, String> where for<'b> T: dbus::arg::Get<'b>, { p.get("org.mpris.MediaPlayer2.Player", name) .map_err(|e| e.to_string()) } pub fn query_player_position(p: &ConnectionProxy) -> Result<Duration, String> { let v = query_player_property::<i64>(p, "Position")?; if v < 0 { panic!("Wrong position value"); } Ok(Duration::from_micros(v.try_into().unwrap())) } fn query_player_playback_status(p: &ConnectionProxy) -> Result<PlaybackStatus, String>
fn parse_player_metadata<T: arg::RefArg>( metadata_map: HashMap<String, T>, ) -> Result<Option<Metadata>, String> { debug!("metadata_map = {:?}", metadata_map); let file_path_encoded = match metadata_map.get("xesam:url") { Some(url) => url .as_str() .ok_or("url metadata should be string")? .to_string(), // If playlist has reached end, new metadata event is sent, // but it doesn't contain any of the following keys None => return Ok(None), }; let file_path_url = Url::parse(&file_path_encoded) .map_err(|e| format!("invalid format of url metadata: {}", e.to_string()))?; let file_path = file_path_url .to_file_path() .map_err(|_| format!("invalid format of url metadata: {}", file_path_url))?; let album = metadata_map .get("xesam:album") .map(|v| { v.as_str() .ok_or("album metadata should be string") .map(|x| x.to_string()) }) .transpose()?; let title = metadata_map["xesam:title"] .as_str() .ok_or("title metadata should be string")? .to_string(); let length = metadata_map["mpris:length"] .as_i64() .ok_or("length metadata should be i64")?; let artists = metadata_map .get("xesam:artist") .map(|v| { v.as_iter() .ok_or("artist metadata should be iterator")? .next() .ok_or("artist metadata should contain at least one entry")? .as_iter() .ok_or("artist metadata should have nested iterator")? .map(|x| { Ok(x.as_str() .ok_or("artist metadata values should be string")? .to_string()) }) .collect::<Result<Vec<String>, &'static str>>() }) .transpose()?; Ok(Some(Metadata { album, title, artists, file_path, length, })) } fn query_player_metadata(p: &ConnectionProxy) -> Result<Option<Metadata>, String> { query_player_property::<DbusStringMap>(p, "Metadata").and_then(parse_player_metadata) } pub fn query_progress(p: &ConnectionProxy) -> Result<Progress, String> { let playback_status = query_player_playback_status(p)?; let position = query_player_position(p)?; let instant = Instant::now(); let metadata = if playback_status != PlaybackStatus::Stopped { query_player_metadata(p)? } else { None }; Ok(Progress { metadata, playback_status, instant, position, }) } fn parse_playback_status(playback_status: &str) -> PlaybackStatus { match playback_status { "Playing" => PlaybackStatus::Playing, "Paused" => PlaybackStatus::Paused, "Stopped" => PlaybackStatus::Stopped, _ => panic!(""), } } fn query_unique_owner_name<S: Into<String>>(c: &Connection, bus_name: S) -> Result<String, String> { let get_name_owner = Message::new_method_call( "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", ) .map_err(|e| e.to_string())? .append1(bus_name.into()); c.send_with_reply_and_block(get_name_owner, Duration::from_millis(100)) .map_err(|e| e.to_string()) .map(|reply| { reply .get1() .expect("GetNameOwner must have name as first member") }) } fn query_all_player_buses(c: &Connection) -> Result<Vec<String>, String> { let list_names = Message::new_method_call( "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListNames", )?; let reply = c .send_with_reply_and_block(list_names, Duration::from_millis(500)) .map_err(|e| e.to_string())?; let names: arg::Array<&str, _> = reply.read1().map_err(|e| e.to_string())?; Ok(names .filter(|name| name.starts_with(MPRIS2_PREFIX)) .map(|str_ref| str_ref.to_owned()) .collect()) } fn get_message_item_dict( a: &arg::Variant<Box<dyn arg::RefArg>>, ) -> HashMap<String, Box<&dyn arg::RefArg>> { let mut it = a.as_iter().unwrap(); let d_variant = it.next().unwrap(); let d_it = d_variant.as_iter().unwrap(); let v = d_it.collect::<Vec<_>>(); v.chunks(2) .map(|c| { let key = c[0].as_str().unwrap(); (key.to_string(), Box::new(c[1])) }) .collect() } #[derive(Debug)] pub struct DbusPropertiesChangedHappened { pub interface_name: String, pub changed_properties: DbusStringMap, pub invalidated_properties: Vec<String>, } impl dbus::message::SignalArgs for DbusPropertiesChangedHappened { const NAME: &'static str = "PropertiesChanged"; const INTERFACE: &'static str = "org.freedesktop.DBus.Properties"; } impl arg::ReadAll for DbusPropertiesChangedHappened { fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { Ok(Self { interface_name: i.read()?, changed_properties: i.read()?, invalidated_properties: i.read()?, }) } } #[derive(Debug)] pub struct MediaPlayer2SeekedHappened { pub position_us: i64, } impl dbus::message::SignalArgs for MediaPlayer2SeekedHappened { const NAME: &'static str = "Seeked"; const INTERFACE: &'static str = "org.mpris.MediaPlayer2.Player"; } impl arg::ReadAll for MediaPlayer2SeekedHappened { fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { Ok(Self { position_us: i.read()?, }) } } #[derive(Debug)] pub struct DbusNameOwnedChanged { pub name: String, pub new_owner: String, pub old_owner: String, } impl dbus::message::SignalArgs for DbusNameOwnedChanged { const NAME: &'static str = "NameOwnerChanged"; const INTERFACE: &'static str = "org.freedesktop.DBus"; } impl arg::ReadAll for DbusNameOwnedChanged { fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { Ok(Self { name: i.read()?, new_owner: i.read()?, old_owner: i.read()?, }) } } pub fn get_connection_proxy<'a>( c: &'a Connection, player_owner_name: &'a str, ) -> ConnectionProxy<'a> { c.with_proxy(player_owner_name, MPRIS2_PATH, Duration::from_millis(5000)) } fn get_mediaplayer2_seeked_handler(
{ query_player_property::<String>(p, "PlaybackStatus").map(|v| parse_playback_status(&v)) }
identifier_body
player.rs
(&self) -> i64 { self.length } } #[derive(Debug)] pub enum Event { PlayerShutDown, PlaybackStatusChange(PlaybackStatus), Seeked { position: Duration }, MetadataChange(Option<Metadata>), } #[derive(Debug)] pub struct Progress { /// If player is stopped, metadata will be None metadata: Option<Metadata>, playback_status: PlaybackStatus, /// When this Progress was constructed, in order to calculate how old it is. instant: Instant, /// Position at the time of construction position: Duration, } impl Progress { pub fn new( playback_status: PlaybackStatus, position: Duration, metadata: Option<Metadata>, ) -> Progress { Progress { metadata, playback_status, instant: Instant::now(), position, } } pub fn metadata(&self) -> &Option<Metadata> { &self.metadata } pub fn take_metadata(self) -> Option<Metadata> { self.metadata } pub fn playback_status(&self) -> PlaybackStatus { self.playback_status } pub fn instant(&self) -> Instant { self.instant } pub fn
(&self) -> Duration { self.position } } fn query_player_property<T>(p: &ConnectionProxy, name: &str) -> Result<T, String> where for<'b> T: dbus::arg::Get<'b>, { p.get("org.mpris.MediaPlayer2.Player", name) .map_err(|e| e.to_string()) } pub fn query_player_position(p: &ConnectionProxy) -> Result<Duration, String> { let v = query_player_property::<i64>(p, "Position")?; if v < 0 { panic!("Wrong position value"); } Ok(Duration::from_micros(v.try_into().unwrap())) } fn query_player_playback_status(p: &ConnectionProxy) -> Result<PlaybackStatus, String> { query_player_property::<String>(p, "PlaybackStatus").map(|v| parse_playback_status(&v)) } fn parse_player_metadata<T: arg::RefArg>( metadata_map: HashMap<String, T>, ) -> Result<Option<Metadata>, String> { debug!("metadata_map = {:?}", metadata_map); let file_path_encoded = match metadata_map.get("xesam:url") { Some(url) => url .as_str() .ok_or("url metadata should be string")? .to_string(), // If playlist has reached end, new metadata event is sent, // but it doesn't contain any of the following keys None => return Ok(None), }; let file_path_url = Url::parse(&file_path_encoded) .map_err(|e| format!("invalid format of url metadata: {}", e.to_string()))?; let file_path = file_path_url .to_file_path() .map_err(|_| format!("invalid format of url metadata: {}", file_path_url))?; let album = metadata_map .get("xesam:album") .map(|v| { v.as_str() .ok_or("album metadata should be string") .map(|x| x.to_string()) }) .transpose()?; let title = metadata_map["xesam:title"] .as_str() .ok_or("title metadata should be string")? .to_string(); let length = metadata_map["mpris:length"] .as_i64() .ok_or("length metadata should be i64")?; let artists = metadata_map .get("xesam:artist") .map(|v| { v.as_iter() .ok_or("artist metadata should be iterator")? .next() .ok_or("artist metadata should contain at least one entry")? .as_iter() .ok_or("artist metadata should have nested iterator")? .map(|x| { Ok(x.as_str() .ok_or("artist metadata values should be string")? .to_string()) }) .collect::<Result<Vec<String>, &'static str>>() }) .transpose()?; Ok(Some(Metadata { album, title, artists, file_path, length, })) } fn query_player_metadata(p: &ConnectionProxy) -> Result<Option<Metadata>, String> { query_player_property::<DbusStringMap>(p, "Metadata").and_then(parse_player_metadata) } pub fn query_progress(p: &ConnectionProxy) -> Result<Progress, String> { let playback_status = query_player_playback_status(p)?; let position = query_player_position(p)?; let instant = Instant::now(); let metadata = if playback_status != PlaybackStatus::Stopped { query_player_metadata(p)? } else { None }; Ok(Progress { metadata, playback_status, instant, position, }) } fn parse_playback_status(playback_status: &str) -> PlaybackStatus { match playback_status { "Playing" => PlaybackStatus::Playing, "Paused" => PlaybackStatus::Paused, "Stopped" => PlaybackStatus::Stopped, _ => panic!(""), } } fn query_unique_owner_name<S: Into<String>>(c: &Connection, bus_name: S) -> Result<String, String> { let get_name_owner = Message::new_method_call( "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", ) .map_err(|e| e.to_string())? .append1(bus_name.into()); c.send_with_reply_and_block(get_name_owner, Duration::from_millis(100)) .map_err(|e| e.to_string()) .map(|reply| { reply .get1() .expect("GetNameOwner must have name as first member") }) } fn query_all_player_buses(c: &Connection) -> Result<Vec<String>, String> { let list_names = Message::new_method_call( "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListNames", )?; let reply = c .send_with_reply_and_block(list_names, Duration::from_millis(500)) .map_err(|e| e.to_string())?; let names: arg::Array<&str, _> = reply.read1().map_err(|e| e.to_string())?; Ok(names .filter(|name| name.starts_with(MPRIS2_PREFIX)) .map(|str_ref| str_ref.to_owned()) .collect()) } fn get_message_item_dict( a: &arg::Variant<Box<dyn arg::RefArg>>, ) -> HashMap<String, Box<&dyn arg::RefArg>> { let mut it = a.as_iter().unwrap(); let d_variant = it.next().unwrap(); let d_it = d_variant.as_iter().unwrap(); let v = d_it.collect::<Vec<_>>(); v.chunks(2) .map(|c| { let key = c[0].as_str().unwrap(); (key.to_string(), Box::new(c[1])) }) .collect() } #[derive(Debug)] pub struct DbusPropertiesChangedHappened { pub interface_name: String, pub changed_properties: DbusStringMap, pub invalidated_properties: Vec<String>, } impl dbus::message::SignalArgs for DbusPropertiesChangedHappened { const NAME: &'static str = "PropertiesChanged"; const INTERFACE: &'static str = "org.freedesktop.DBus.Properties"; } impl arg::ReadAll for DbusPropertiesChangedHappened { fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { Ok(Self { interface_name: i.read()?, changed_properties: i.read()?, invalidated_properties: i.read()?, }) } } #[derive(Debug)] pub struct MediaPlayer2SeekedHappened { pub position_us: i64, } impl dbus::message::SignalArgs for MediaPlayer2SeekedHappened { const NAME: &'static str = "Seeked"; const INTERFACE: &'static str = "org.mpris.MediaPlayer2.Player"; } impl arg::ReadAll for MediaPlayer2SeekedHappened { fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { Ok(Self { position_us: i.read()?, }) } } #[derive(Debug)] pub struct DbusNameOwnedChanged { pub name: String, pub new_owner: String, pub old_owner: String, } impl dbus::message::SignalArgs for DbusNameOwnedChanged { const NAME: &'static str = "NameOwnerChanged"; const INTERFACE: &'static str = "org.freedesktop.DBus"; } impl arg::ReadAll for DbusNameOwnedChanged { fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { Ok(Self { name: i.read()?, new_owner: i.read()?, old_owner: i.read()?, }) } } pub fn get_connection_proxy<'a>( c: &'a Connection, player_owner_name: &'a str, ) -> ConnectionProxy<'a> { c.with_proxy(player_owner_name, MPRIS2_PATH, Duration::from_millis(5000)) } fn get_mediaplayer2_seeked_handler(
position
identifier_name
initialize.py
username':'sarahbro1', } ]: user = mod.User() for key in data: setattr(user, key, data[key]) user.set_password('password') user.save() group.user_set.add(user) ############################################################################# ################################ DUMMY DATA ################################# ############################################################################# ################################## VENUES ################################### venue = mod.Venue() venue.name = "The Park" venue.address = address2 venue.save() ################################## EVENTS ################################### event_template = mod.PublicEvent() event_template.name = "Fun Event" event_template.description = "A roudy get together for families in the community. We will do things, participate in things, experience things, and generally have a good time. Come one, come all to see the amount of fun you can have when you gather for no real reason." event_template.save() event = mod.Event() event.venue = venue event.event_template = event_template event.start_date = "2016-01-01" event.end_date = "2016-01-03" event.event_map = "Map goes here" event.save() ################################## AREAS #################################### area = mod.Area() area.name = "Vendors" area.event = event area.description = "This is where vendors will be located." area.place_number = 1 area.coordinator = user area.supervisor = user area.save() ################################ INVENTORY ################################## # Bulk product - musket balls photo = mod.Photograph() photo.place_taken = "Colonial Heritage Festival" photo.image = "products/media/product_images/musket_balls.jpg" photo.description = "Musket balls for sale!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Musket Balls' specs.price = 2.50 specs.description = 'Made to the exact specifications to match what was shot during the Revolutionary War!' specs.manufacturer = 'Test manufacturer' specs.average_cost = 2.50 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() inventory = mod.Inventory() inventory.quantity_on_hand = 400 inventory.shelf_location = 'Corner' inventory.order_file = 'Test File' inventory.condition = 'Old' inventory.specs = specs inventory.save() sale_item = mod.ExpectedSaleItem() sale_item.product_specification = specs sale_item.high_price = '25.00' sale_item.low_price = '1.00' sale_item.area = area sale_item.save() ############################ SERIALIZED PRODUCT ############################# # Broom photo = mod.Photograph() photo.place_taken = "Colonial Heritage Fsetival" photo.image = "products/media/product_images/broom.jpg" photo.description = "Broom made by one of our very own artisans!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Broom' specs.price = 23.99 specs.description = 'Bring a colonial flair to your normal chores!' specs.manufacturer = 'Artisan Allen' specs.average_cost = 23.99 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() product = mod.SerializedProduct() product.quantity_on_hand = 1 product.shelf_location = 'Back Corner' product.order_file = 'Test File' product.condition = 'Ancient' product.specs = specs product.serial_number = '2222222' product.cost = 20.00 product.status = 'Good' product.save() ################################## ITEM ##################################### # Item for rent photo = mod.Photograph() photo.place_taken = "Colonial Heritage Fsetival" photo.image = "rentals/media/canon.jpg" photo.description = "A cannon that really fires!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Cannon' specs.price = 9.99 specs.description = 'Cannon lent to us for rent by the Smithsonian' specs.manufacturer = 'Test manufacturer' specs.average_cost = 9.99 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() rental_item = mod.Item() rental_item.quantity_on_hand= 1 rental_item.shelf_location = 'Front Corner' rental_item.order_file = 'Test File' rental_item.condition = 'New' rental_item.specs = specs rental_item.standard_rental_price= 9.99 rental_item.times_rented = 2 rental_item.price_per_day = 9.99 rental_item.replacement_price = 190.00 rental_item.save() # Item not for rent specs = mod.ProductSpecification() specs.name = 'Full-Sized Replica of the Liberty Bell' specs.price = 20000.0 specs.description = 'Made to the exact dimensions as the actual Liberty Bell. For display only.' specs.manufacturer = 'Test manufacturer' specs.average_cost = 2.0 specs.sku = '111' specs.order_form_name= 'Test order form' specs.production_time= 'Test production time' specs.save() item = mod.Item() item.quantity_on_hand= 1 item.shelf_location = 'Front Corner' item.order_file = 'Test File' item.condition = 'New' item.specs = specs item.save() ############################## WARDROBE ITEM ################################ # Man's Jacket photo = mod.Photograph() photo.place_taken = "Colonial Heritage Fsetival" photo.image = "rentals/media/mans_jacket.jpg" photo.description = "Colonial era man's jacket!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Jacket' specs.price = 4.90 specs.description = "Man's jacket from the 1600's" specs.manufacturer = 'H&M' specs.average_cost = 4.90 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() wardrobe_item = mod.WardrobeItem() wardrobe_item.quantity_on_hand= 1 wardrobe_item.shelf_location = 'Front Right Corner' wardrobe_item.order_file = 'Test File' wardrobe_item.condition = 'Newest' wardrobe_item.specs = specs wardrobe_item.standard_rental_price= 4.90 wardrobe_item.times_rented = 2 wardrobe_item.price_per_day = 4.90 wardrobe_item.replacement_price = 27.99 wardrobe_item.size = 38 wardrobe_item.size_modifier= 'S' wardrobe_item.gender = 'M' wardrobe_item.color = 'White' wardrobe_item.pattern = 'Paisely' wardrobe_item.start_year = '1677-1-1' wardrobe_item.end_year = '1678-1-1' wardrobe_item.save() # Man's shirt photo = mod.Photograph() photo.place_taken = "Colonial Heritage Fsetival" photo.image = "rentals/media/mans_shirt.jpg" photo.description = "Colonial era man's shirt!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Shirt' specs.price = 2.98 specs.description = "Man's dress shirt from the 1600's" specs.manufacturer = 'Banana Republic' specs.average_cost = 2.98 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() wardrobe_item = mod.WardrobeItem() wardrobe_item.quantity_on_hand= 1 wardrobe_item.shelf_location = 'Front Right Corner' wardrobe_item.order_file = 'Test File' wardrobe_item.condition = 'Newest' wardrobe_item.specs = specs wardrobe_item.standard_rental_price= 2.98 wardrobe_item.times_rented = 3 wardrobe_item.price_per_day = 2.98 wardrobe_item.replacement_price = 32.50 wardrobe_item.size = 38 wardrobe_item.size_modifier= 'L' wardrobe_item.gender = 'M' wardrobe_item.color = 'White' wardrobe_item.pattern = 'Paisely' wardrobe_item.start_year = '1677-1-1' wardrobe_item.end_year = '1678-1-1' wardrobe_item.save() ############################################################################# ############################## TRANSACTIONS ################################# ############################################################################# for data in [ {'customer': user} ]: transaction = mod.Transaction()
random_line_split
initialize.py
= mod.Area() area.name = "Vendors" area.event = event area.description = "This is where vendors will be located." area.place_number = 1 area.coordinator = user area.supervisor = user area.save() ################################ INVENTORY ################################## # Bulk product - musket balls photo = mod.Photograph() photo.place_taken = "Colonial Heritage Festival" photo.image = "products/media/product_images/musket_balls.jpg" photo.description = "Musket balls for sale!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Musket Balls' specs.price = 2.50 specs.description = 'Made to the exact specifications to match what was shot during the Revolutionary War!' specs.manufacturer = 'Test manufacturer' specs.average_cost = 2.50 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() inventory = mod.Inventory() inventory.quantity_on_hand = 400 inventory.shelf_location = 'Corner' inventory.order_file = 'Test File' inventory.condition = 'Old' inventory.specs = specs inventory.save() sale_item = mod.ExpectedSaleItem() sale_item.product_specification = specs sale_item.high_price = '25.00' sale_item.low_price = '1.00' sale_item.area = area sale_item.save() ############################ SERIALIZED PRODUCT ############################# # Broom photo = mod.Photograph() photo.place_taken = "Colonial Heritage Fsetival" photo.image = "products/media/product_images/broom.jpg" photo.description = "Broom made by one of our very own artisans!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Broom' specs.price = 23.99 specs.description = 'Bring a colonial flair to your normal chores!' specs.manufacturer = 'Artisan Allen' specs.average_cost = 23.99 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() product = mod.SerializedProduct() product.quantity_on_hand = 1 product.shelf_location = 'Back Corner' product.order_file = 'Test File' product.condition = 'Ancient' product.specs = specs product.serial_number = '2222222' product.cost = 20.00 product.status = 'Good' product.save() ################################## ITEM ##################################### # Item for rent photo = mod.Photograph() photo.place_taken = "Colonial Heritage Fsetival" photo.image = "rentals/media/canon.jpg" photo.description = "A cannon that really fires!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Cannon' specs.price = 9.99 specs.description = 'Cannon lent to us for rent by the Smithsonian' specs.manufacturer = 'Test manufacturer' specs.average_cost = 9.99 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() rental_item = mod.Item() rental_item.quantity_on_hand= 1 rental_item.shelf_location = 'Front Corner' rental_item.order_file = 'Test File' rental_item.condition = 'New' rental_item.specs = specs rental_item.standard_rental_price= 9.99 rental_item.times_rented = 2 rental_item.price_per_day = 9.99 rental_item.replacement_price = 190.00 rental_item.save() # Item not for rent specs = mod.ProductSpecification() specs.name = 'Full-Sized Replica of the Liberty Bell' specs.price = 20000.0 specs.description = 'Made to the exact dimensions as the actual Liberty Bell. For display only.' specs.manufacturer = 'Test manufacturer' specs.average_cost = 2.0 specs.sku = '111' specs.order_form_name= 'Test order form' specs.production_time= 'Test production time' specs.save() item = mod.Item() item.quantity_on_hand= 1 item.shelf_location = 'Front Corner' item.order_file = 'Test File' item.condition = 'New' item.specs = specs item.save() ############################## WARDROBE ITEM ################################ # Man's Jacket photo = mod.Photograph() photo.place_taken = "Colonial Heritage Fsetival" photo.image = "rentals/media/mans_jacket.jpg" photo.description = "Colonial era man's jacket!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Jacket' specs.price = 4.90 specs.description = "Man's jacket from the 1600's" specs.manufacturer = 'H&M' specs.average_cost = 4.90 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() wardrobe_item = mod.WardrobeItem() wardrobe_item.quantity_on_hand= 1 wardrobe_item.shelf_location = 'Front Right Corner' wardrobe_item.order_file = 'Test File' wardrobe_item.condition = 'Newest' wardrobe_item.specs = specs wardrobe_item.standard_rental_price= 4.90 wardrobe_item.times_rented = 2 wardrobe_item.price_per_day = 4.90 wardrobe_item.replacement_price = 27.99 wardrobe_item.size = 38 wardrobe_item.size_modifier= 'S' wardrobe_item.gender = 'M' wardrobe_item.color = 'White' wardrobe_item.pattern = 'Paisely' wardrobe_item.start_year = '1677-1-1' wardrobe_item.end_year = '1678-1-1' wardrobe_item.save() # Man's shirt photo = mod.Photograph() photo.place_taken = "Colonial Heritage Fsetival" photo.image = "rentals/media/mans_shirt.jpg" photo.description = "Colonial era man's shirt!" photo.photographer = user photo.save() specs = mod.ProductSpecification() specs.name = 'Shirt' specs.price = 2.98 specs.description = "Man's dress shirt from the 1600's" specs.manufacturer = 'Banana Republic' specs.average_cost = 2.98 specs.sku = '111' specs.order_form_name = 'Test order form' specs.production_time = 'Test production time' specs.photograph = photo specs.save() wardrobe_item = mod.WardrobeItem() wardrobe_item.quantity_on_hand= 1 wardrobe_item.shelf_location = 'Front Right Corner' wardrobe_item.order_file = 'Test File' wardrobe_item.condition = 'Newest' wardrobe_item.specs = specs wardrobe_item.standard_rental_price= 2.98 wardrobe_item.times_rented = 3 wardrobe_item.price_per_day = 2.98 wardrobe_item.replacement_price = 32.50 wardrobe_item.size = 38 wardrobe_item.size_modifier= 'L' wardrobe_item.gender = 'M' wardrobe_item.color = 'White' wardrobe_item.pattern = 'Paisely' wardrobe_item.start_year = '1677-1-1' wardrobe_item.end_year = '1678-1-1' wardrobe_item.save() ############################################################################# ############################## TRANSACTIONS ################################# ############################################################################# for data in [ {'customer': user} ]: transaction = mod.Transaction() for key in data: setattr(transaction, key, data[key]) transaction.save() ################################# RENTAL ##################################### for data in [ {'date_out':'2000-01-01 00:00:00', 'due_date': '2001-01-01', 'item':item, 'transaction':transaction, 'amount':40.87}, {'date_out':'2014-01-01 00:00:00', 'due_date': '2015-02-19', 'item':wardrobe_item, 'transaction':transaction, 'amount':27.29}, {'date_out':'2014-01-01 00:00:00', 'due_date': '2015-01-20', 'item':wardrobe_item, 'transaction':transaction, 'amount':280.14}, ]: rental = mod.RentalItem() for key in data:
setattr(rental, key, data[key])
conditional_block
pharmacie-details.ts
import {Pharmacie} from '../../models/pharmacie'; // Importation de la page de détail d'une pharmacie import {OpinionPage} from '../opinion/opinion'; // Importation de la page de saisie des horaires de la pharmacie import {HoursPage} from '../hours/hours'; declare var google; declare var $; declare var _; declare var moment; /* Generated class for the PharmacieDetailsPage page. See http://ionicframework.com/docs/v2/components/#navigation for more info on Ionic pages and navigation. */ @Component({ templateUrl: 'build/pages/pharmacie-details/pharmacie-details.html', // Ajout du provider Pharmacies et Opinions providers: [PharmaciesProvider, OpinionsProvider, SubscriberProvider] }) export class PharmacieDetailsPage { @ViewChild('map') mapElement: ElementRef; map: any; id: string; rs: string; pharmacie: Pharmacie = new Pharmacie; loader: any; options: string; moment: any; hours: { mo: string, tu: string, we: string, th: string, fr: string, sa: string, su: string}; constructor( public modalCtrl: ModalController, private platform: Platform, public nav: NavController, private loadingController: LoadingController, private navParams: NavParams, private pharmaciesProvider: PharmaciesProvider, private opinionsProvider: OpinionsProvider, private subscriberProvider: SubscriberProvider, private applicationRef: ApplicationRef, private zone: NgZone, private toastController : ToastController) { this.moment = moment; this.platform = platform; this.platform.ready().then(() => { GoogleAnalytics.trackView('pharmacie-details'); }); // Récupération des paramètres id et raison sociale depuis les paramètres de navigation this.id = navParams.get('id'); this.rs = navParams.get('rs'); // Affichage de la mire de chargement this.loader = this.loadingController.create({ content: 'Chargement en cours...' }); this.loader.present(); // Définition du segment Carte par défaut pour afficher la carte (au lieu des commentaires) this.options = 'map'; // Récupère les détails de la pharmacie depuis l'API. pharmaciesProvider.loadDetails(this.id) .then( pharmacie => { this.pharmacie = pharmacie; this.pharmacie.isFavorite = this.isFavorite(); this.pharmacie.favoriteIcon = this.isFavorite() ? 'star' : 'star-outline' ; this.fetchOpinions(); // Récupère les avis pour avoir la note moyenne de la pharmacie this.formatHours(); this.loader.dismiss(); // On efface la mire de chargement this.displayMap(); // On affiche la carte avec la position de la pharmacie }) } // Formate les horaires d'ouverture en une heure au format HH:mm formatHours() { if (this.pharmacie.hours) { function addZero(i) { if (i < 10) { i = "0" + i; } return i; } function formatDay(amo, amc, pmo, pmc) { if (amo == 0 && pmc == 1440) return 'Ouvert 24h/24'; // Pas d'horaire le matin if (amo == 0 && amc == 0) { // Pas d'horaire l'après midi if (pmo == 0 || pmc == 0) return 'Fermé'; else return `${addZero(Math.trunc(pmo/60))}:${addZero(pmo%60)}-${addZero(Math.trunc(pmc/60))}:${addZero(pmc%60)}`; // Horaires le matin } else { let hours = `${addZero(Math.trunc(amo/60))}:${addZero(amo%60)}-${addZero(Math.trunc(amc/60))}:${addZero(amc%60)}`; // Horaires l'après midi if (pmo != 0 && pmc != 0) hours += ` ${addZero(Math.trunc(pmo/60))}:${addZero(pmo%60)}-${addZero(Math.trunc(pmc/60))}:${addZero(pmc%60)}`; return hours; } }; this.hours = { mo: formatDay(this.pharmacie.hours.mo.amo, this.pharmacie.hours.mo.amc, this.pharmacie.hours.mo.pmo, this.pharmacie.hours.mo.pmc), tu: formatDay(this.pharmacie.hours.tu.amo, this.pharmacie.hours.tu.amc, this.pharmacie.hours.tu.pmo, this.pharmacie.hours.tu.pmc), we: formatDay(this.pharmacie.hours.we.amo, this.pharmacie.hours.we.amc, this.pharmacie.hours.we.pmo, this.pharmacie.hours.we.pmc), th: formatDay(this.pharmacie.hours.th.amo, this.pharmacie.hours.th.amc, this.pharmacie.hours.th.pmo, this.pharmacie.hours.th.pmc), fr: formatDay(this.pharmacie.hours.fr.amo, this.pharmacie.hours.fr.amc, this.pharmacie.hours.fr.pmo, this.pharmacie.hours.fr.pmc), sa: formatDay(this.pharmacie.hours.sa.amo, this.pharmacie.hours.sa.amc, this.pharmacie.hours.sa.pmo, this.pharmacie.hours.sa.pmc), su: formatDay(this.pharmacie.hours.su.amo, this.pharmacie.hours.su.amc, this.pharmacie.hours.su.pmo, this.pharmacie.hours.su.pmc) }; } } // Regarde dans le localStorage si la pharmacie fait partie des pharmacies favorites isFavorite(){ if (!localStorage.getItem('favorites')) return false; let favorites = JSON.parse(localStorage.getItem('favorites')); return (favorites.indexOf(this.pharmacie._id) !== -1); } // Ajoute la pharmacie à la liste des pharmacies favorites toggleFavorite() { this.platform.ready().then(() => { GoogleAnalytics.trackEvent('pharmacie-details', 'toggleFavorite', 'Ajout / Retirer la pharmacie des favoris', 1); }); let $favorites = $('ion-icon#favorites'); let isFavorites = $favorites.attr('class') === 'ion-md-star', classIcon, msgToast, favorites; // Si la pharmacie fait partie des pharmacies favorites if (isFavorites) { // On désabonne l'utilisateur aux commentaires de la pharmacie this.subscriberProvider.unsubscribe(this.pharmacie._id); classIcon = 'ion-md-star-outline'; msgToast = 'Pharmacie retirée des favoris'; // Mise a jour de la liste des pharmacies favorites favorites = JSON.parse(localStorage.getItem('favorites')); favorites = favorites.filter((item) => item != this.pharmacie._id); // Si la pharmacie ne fait pas partie des pharmacies favorites } else { // On abonne l'utilisateur aux commentaires de la pharmacie this.subscriberProvider.subscribe(this.pharmacie._id); classIcon = 'ion-md-star'; msgToast = 'Pharmacie ajoutée aux favoris'; if (localStorage.getItem('favorites')) { favorites = JSON.parse(localStorage.getItem('favorites')); favorites.push(this.pharmacie._id); } else { favorites = [this.pharmacie._id]; } } // Sauvegarde de la liste des pharmacies favorites dans le localeStorage localStorage.setItem('favorites', JSON.stringify(favorites)); $favorites.attr('class', classIcon); let toast = this.toastController.create({ message: msgToast, duration: 3000, position: 'middle' }); toast.present(); } // Ouverture d'une application GPS du téléphone (google maps, waze) pour afficher l'itinéraire jusqu'à la pharmacie. navigateToPharmacie() { let destination = `${this.pharmacie.loc[1]},${this.pharmacie.loc[0]}`; if (this.platform.is('ios')){ window.open('maps://?q=' + destination, '_system'); } else { let label = encodeURI(`${this.pharmacie.numvoie} ${this.pharmacie.typvoie} ${this.pharmacie.voie}, ${this.pharmacie.cpville}`); window.open('geo:0,0?q=' + destination + '(' + label + ')', '_system'); } } // Partage les infos de la pharmacie avec une application externe share() { SocialSharing.share( `${this.pharmacie.rs} Adresse : ${this.pharmacie.numvoie} ${this.pharmacie.typvoie} ${this.pharmacie.voie}, ${this.pharmacie.cpville} Tél: ${this.pharmacie.telephone} Fax: ${this.pharmacie.fax}`, this.pharmacie.rs, '',
import {SubscriberProvider} from '../../providers/subscriber/subscriber'; // Importation du modèle de données Pharmacie
random_line_split
pharmacie-details.ts
: 'build/pages/pharmacie-details/pharmacie-details.html', // Ajout du provider Pharmacies et Opinions providers: [PharmaciesProvider, OpinionsProvider, SubscriberProvider] }) export class PharmacieDetailsPage { @ViewChild('map') mapElement: ElementRef; map: any; id: string; rs: string; pharmacie: Pharmacie = new Pharmacie; loader: any; options: string; moment: any; hours: { mo: string, tu: string, we: string, th: string, fr: string, sa: string, su: string}; constructor( public modalCtrl: ModalController, private platform: Platform, public nav: NavController, private loadingController: LoadingController, private navParams: NavParams, private pharmaciesProvider: PharmaciesProvider, private opinionsProvider: OpinionsProvider, private subscriberProvider: SubscriberProvider, private applicationRef: ApplicationRef, private zone: NgZone, private toastController : ToastController) { this.moment = moment; this.platform = platform; this.platform.ready().then(() => { GoogleAnalytics.trackView('pharmacie-details'); }); // Récupération des paramètres id et raison sociale depuis les paramètres de navigation this.id = navParams.get('id'); this.rs = navParams.get('rs'); // Affichage de la mire de chargement this.loader = this.loadingController.create({ content: 'Chargement en cours...' }); this.loader.present(); // Définition du segment Carte par défaut pour afficher la carte (au lieu des commentaires) this.options = 'map'; // Récupère les détails de la pharmacie depuis l'API. pharmaciesProvider.loadDetails(this.id) .then( pharmacie => { this.pharmacie = pharmacie; this.pharmacie.isFavorite = this.isFavorite(); this.pharmacie.favoriteIcon = this.isFavorite() ? 'star' : 'star-outline' ; this.fetchOpinions(); // Récupère les avis pour avoir la note moyenne de la pharmacie this.formatHours(); this.loader.dismiss(); // On efface la mire de chargement this.displayMap(); // On affiche la carte avec la position de la pharmacie }) } // Formate les horaires d'ouverture en une heure au format HH:mm formatHours() { if (this.pharmacie.hours) { function addZero(i) { if (i < 10) { i = "0" + i; } return i; } function formatDay(amo, amc, pmo, pmc) { if (amo == 0 && pmc == 1440) return 'Ouvert 24h/24'; // Pas d'horaire le matin if (amo == 0 && amc == 0) { // Pas d'horaire l'après midi if (pmo == 0 || pmc == 0) return 'Fermé'; else return `${addZero(Math.trunc(pmo/60))}:${addZero(pmo%60)}-${addZero(Math.trunc(pmc/60))}:${addZero(pmc%60)}`; // Horaires le matin } else { let hours = `${addZero(Math.trunc(amo/60))}:${addZero(amo%60)}-${addZero(Math.trunc(amc/60))}:${addZero(amc%60)}`; // Horaires l'après midi if (pmo != 0 && pmc != 0) hours += ` ${addZero(Math.trunc(pmo/60))}:${addZero(pmo%60)}-${addZero(Math.trunc(pmc/60))}:${addZero(pmc%60)}`; return hours; } }; this.hours = { mo: formatDay(this.pharmacie.hours.mo.amo, this.pharmacie.hours.mo.amc, this.pharmacie.hours.mo.pmo, this.pharmacie.hours.mo.pmc), tu: formatDay(this.pharmacie.hours.tu.amo, this.pharmacie.hours.tu.amc, this.pharmacie.hours.tu.pmo, this.pharmacie.hours.tu.pmc), we: formatDay(this.pharmacie.hours.we.amo, this.pharmacie.hours.we.amc, this.pharmacie.hours.we.pmo, this.pharmacie.hours.we.pmc), th: formatDay(this.pharmacie.hours.th.amo, this.pharmacie.hours.th.amc, this.pharmacie.hours.th.pmo, this.pharmacie.hours.th.pmc), fr: formatDay(this.pharmacie.hours.fr.amo, this.pharmacie.hours.fr.amc, this.pharmacie.hours.fr.pmo, this.pharmacie.hours.fr.pmc), sa: formatDay(this.pharmacie.hours.sa.amo, this.pharmacie.hours.sa.amc, this.pharmacie.hours.sa.pmo, this.pharmacie.hours.sa.pmc), su: formatDay(this.pharmacie.hours.su.amo, this.pharmacie.hours.su.amc, this.pharmacie.hours.su.pmo, this.pharmacie.hours.su.pmc) }; } } // Regarde dans le localStorage si la pharmacie fait partie des pharmacies favorites isFavorite(){ if (!localStorage.getItem('favorites')) return false; let favorites = JSON.parse(localStorage.getItem('favorites')); return (favorites.indexOf(this.pharmacie._id) !== -1); } // Ajoute la pharmacie à la liste des pharmacies favorites toggleFavorite() { this.platform.ready().then(() => { GoogleAnalytics.trackEvent('pharmacie-details', 'toggleFavorite', 'Ajout / Retirer la pharmacie des favoris', 1); }); let $favorites = $('ion-icon#favorites'); let isFavorites = $favorites.attr('class') === 'ion-md-star', classIcon, msgToast, favorites; // Si la pharmacie fait partie des pharmacies favorites if (isFavorites) { // On désa
abonne l'utilisateur aux commentaires de la pharmacie this.subscriberProvider.subscribe(this.pharmacie._id); classIcon = 'ion-md-star'; msgToast = 'Pharmacie ajoutée aux favoris'; if (localStorage.getItem('favorites')) { favorites = JSON.parse(localStorage.getItem('favorites')); favorites.push(this.pharmacie._id); } else { favorites = [this.pharmacie._id]; } } // Sauvegarde de la liste des pharmacies favorites dans le localeStorage localStorage.setItem('favorites', JSON.stringify(favorites)); $favorites.attr('class', classIcon); let toast = this.toastController.create({ message: msgToast, duration: 3000, position: 'middle' }); toast.present(); } // Ouverture d'une application GPS du téléphone (google maps, waze) pour afficher l'itinéraire jusqu'à la pharmacie. navigateToPharmacie() { let destination = `${this.pharmacie.loc[1]},${this.pharmacie.loc[0]}`; if (this.platform.is('ios')){ window.open('maps://?q=' + destination, '_system'); } else { let label = encodeURI(`${this.pharmacie.numvoie} ${this.pharmacie.typvoie} ${this.pharmacie.voie}, ${this.pharmacie.cpville}`); window.open('geo:0,0?q=' + destination + '(' + label + ')', '_system'); } } // Partage les infos de la pharmacie avec une application externe share() { SocialSharing.share( `${this.pharmacie.rs} Adresse : ${this.pharmacie.numvoie} ${this.pharmacie.typvoie} ${this.pharmacie.voie}, ${this.pharmacie.cpville} Tél: ${this.pharmacie.telephone} Fax: ${this.pharmacie.fax}`, this.pharmacie.rs, '', '' ).then( result => { console.log(result); }).catch(err => { console.log(err); }); } // Chargement de la carte google map centré sur la pharmacie displayMap() { this.platform.ready().then(() => { GoogleAnalytics.trackEvent('pharmacie-details', 'displayMap', 'Affichage de la carte', 1); }); setTimeout(function() { // Coordonnées géographique de la pharmacie let latLng = new google.maps.LatLng(this.pharmacie.loc[1], this.pharmacie.loc[0]); // Options de la carte let mapOptions = { center
bonne l'utilisateur aux commentaires de la pharmacie this.subscriberProvider.unsubscribe(this.pharmacie._id); classIcon = 'ion-md-star-outline'; msgToast = 'Pharmacie retirée des favoris'; // Mise a jour de la liste des pharmacies favorites favorites = JSON.parse(localStorage.getItem('favorites')); favorites = favorites.filter((item) => item != this.pharmacie._id); // Si la pharmacie ne fait pas partie des pharmacies favorites } else { // On
conditional_block
pharmacie-details.ts
'build/pages/pharmacie-details/pharmacie-details.html', // Ajout du provider Pharmacies et Opinions providers: [PharmaciesProvider, OpinionsProvider, SubscriberProvider] }) export class PharmacieDetailsPage { @ViewChild('map') mapElement: ElementRef; map: any; id: string; rs: string; pharmacie: Pharmacie = new Pharmacie; loader: any; options: string; moment: any; hours: { mo: string, tu: string, we: string, th: string, fr: string, sa: string, su: string}; constructor( public modalCtrl: ModalController, private platform: Platform, public nav: NavController, private loadingController: LoadingController, private navParams: NavParams, private pharmaciesProvider: PharmaciesProvider, private opinionsProvider: OpinionsProvider, private subscriberProvider: SubscriberProvider, private applicationRef: ApplicationRef, private zone: NgZone, private toastController : ToastController) { this.moment = moment; this.platform = platform; this.platform.ready().then(() => { GoogleAnalytics.trackView('pharmacie-details'); }); // Récupération des paramètres id et raison sociale depuis les paramètres de navigation this.id = navParams.get('id'); this.rs = navParams.get('rs'); // Affichage de la mire de chargement this.loader = this.loadingController.create({ content: 'Chargement en cours...' }); this.loader.present(); // Définition du segment Carte par défaut pour afficher la carte (au lieu des commentaires) this.options = 'map'; // Récupère les détails de la pharmacie depuis l'API. pharmaciesProvider.loadDetails(this.id) .then( pharmacie => { this.pharmacie = pharmacie; this.pharmacie.isFavorite = this.isFavorite(); this.pharmacie.favoriteIcon = this.isFavorite() ? 'star' : 'star-outline' ; this.fetchOpinions(); // Récupère les avis pour avoir la note moyenne de la pharmacie this.formatHours(); this.loader.dismiss(); // On efface la mire de chargement this.displayMap(); // On affiche la carte avec la position de la pharmacie }) } // Formate les horaires d'ouverture en une heure au format HH:mm formatHours()
his.pharmacie.hours) { function addZero(i) { if (i < 10) { i = "0" + i; } return i; } function formatDay(amo, amc, pmo, pmc) { if (amo == 0 && pmc == 1440) return 'Ouvert 24h/24'; // Pas d'horaire le matin if (amo == 0 && amc == 0) { // Pas d'horaire l'après midi if (pmo == 0 || pmc == 0) return 'Fermé'; else return `${addZero(Math.trunc(pmo/60))}:${addZero(pmo%60)}-${addZero(Math.trunc(pmc/60))}:${addZero(pmc%60)}`; // Horaires le matin } else { let hours = `${addZero(Math.trunc(amo/60))}:${addZero(amo%60)}-${addZero(Math.trunc(amc/60))}:${addZero(amc%60)}`; // Horaires l'après midi if (pmo != 0 && pmc != 0) hours += ` ${addZero(Math.trunc(pmo/60))}:${addZero(pmo%60)}-${addZero(Math.trunc(pmc/60))}:${addZero(pmc%60)}`; return hours; } }; this.hours = { mo: formatDay(this.pharmacie.hours.mo.amo, this.pharmacie.hours.mo.amc, this.pharmacie.hours.mo.pmo, this.pharmacie.hours.mo.pmc), tu: formatDay(this.pharmacie.hours.tu.amo, this.pharmacie.hours.tu.amc, this.pharmacie.hours.tu.pmo, this.pharmacie.hours.tu.pmc), we: formatDay(this.pharmacie.hours.we.amo, this.pharmacie.hours.we.amc, this.pharmacie.hours.we.pmo, this.pharmacie.hours.we.pmc), th: formatDay(this.pharmacie.hours.th.amo, this.pharmacie.hours.th.amc, this.pharmacie.hours.th.pmo, this.pharmacie.hours.th.pmc), fr: formatDay(this.pharmacie.hours.fr.amo, this.pharmacie.hours.fr.amc, this.pharmacie.hours.fr.pmo, this.pharmacie.hours.fr.pmc), sa: formatDay(this.pharmacie.hours.sa.amo, this.pharmacie.hours.sa.amc, this.pharmacie.hours.sa.pmo, this.pharmacie.hours.sa.pmc), su: formatDay(this.pharmacie.hours.su.amo, this.pharmacie.hours.su.amc, this.pharmacie.hours.su.pmo, this.pharmacie.hours.su.pmc) }; } } // Regarde dans le localStorage si la pharmacie fait partie des pharmacies favorites isFavorite(){ if (!localStorage.getItem('favorites')) return false; let favorites = JSON.parse(localStorage.getItem('favorites')); return (favorites.indexOf(this.pharmacie._id) !== -1); } // Ajoute la pharmacie à la liste des pharmacies favorites toggleFavorite() { this.platform.ready().then(() => { GoogleAnalytics.trackEvent('pharmacie-details', 'toggleFavorite', 'Ajout / Retirer la pharmacie des favoris', 1); }); let $favorites = $('ion-icon#favorites'); let isFavorites = $favorites.attr('class') === 'ion-md-star', classIcon, msgToast, favorites; // Si la pharmacie fait partie des pharmacies favorites if (isFavorites) { // On désabonne l'utilisateur aux commentaires de la pharmacie this.subscriberProvider.unsubscribe(this.pharmacie._id); classIcon = 'ion-md-star-outline'; msgToast = 'Pharmacie retirée des favoris'; // Mise a jour de la liste des pharmacies favorites favorites = JSON.parse(localStorage.getItem('favorites')); favorites = favorites.filter((item) => item != this.pharmacie._id); // Si la pharmacie ne fait pas partie des pharmacies favorites } else { // On abonne l'utilisateur aux commentaires de la pharmacie this.subscriberProvider.subscribe(this.pharmacie._id); classIcon = 'ion-md-star'; msgToast = 'Pharmacie ajoutée aux favoris'; if (localStorage.getItem('favorites')) { favorites = JSON.parse(localStorage.getItem('favorites')); favorites.push(this.pharmacie._id); } else { favorites = [this.pharmacie._id]; } } // Sauvegarde de la liste des pharmacies favorites dans le localeStorage localStorage.setItem('favorites', JSON.stringify(favorites)); $favorites.attr('class', classIcon); let toast = this.toastController.create({ message: msgToast, duration: 3000, position: 'middle' }); toast.present(); } // Ouverture d'une application GPS du téléphone (google maps, waze) pour afficher l'itinéraire jusqu'à la pharmacie. navigateToPharmacie() { let destination = `${this.pharmacie.loc[1]},${this.pharmacie.loc[0]}`; if (this.platform.is('ios')){ window.open('maps://?q=' + destination, '_system'); } else { let label = encodeURI(`${this.pharmacie.numvoie} ${this.pharmacie.typvoie} ${this.pharmacie.voie}, ${this.pharmacie.cpville}`); window.open('geo:0,0?q=' + destination + '(' + label + ')', '_system'); } } // Partage les infos de la pharmacie avec une application externe share() { SocialSharing.share( `${this.pharmacie.rs} Adresse : ${this.pharmacie.numvoie} ${this.pharmacie.typvoie} ${this.pharmacie.voie}, ${this.pharmacie.cpville} Tél: ${this.pharmacie.telephone} Fax: ${this.pharmacie.fax}`, this.pharmacie.rs, '', '' ).then( result => { console.log(result); }).catch(err => { console.log(err); }); } // Chargement de la carte google map centré sur la pharmacie displayMap() { this.platform.ready().then(() => { GoogleAnalytics.trackEvent('pharmacie-details', 'displayMap', 'Affichage de la carte', 1); }); setTimeout(function() { // Coordonnées géographique de la pharmacie let latLng = new google.maps.LatLng(this.pharmacie.loc[1], this.pharmacie.loc[0]); // Options de la carte let mapOptions = { center
{ if (t
identifier_name
did.rs
DID`] query and returns an iterator of (key, value) pairs. #[inline] pub fn query_pairs(&self) -> form_urlencoded::Parse { self.core.query_pairs(self.as_str()) } /// Change the method of the [`DID`]. #[inline] pub fn set_method(&mut self, value: impl AsRef<str>) { self.core.set_method(&mut self.data, value.as_ref()); } /// Change the method-specific-id of the [`DID`]. #[inline] pub fn set_method_id(&mut self, value: impl AsRef<str>) { self.core.set_method_id(&mut self.data, value.as_ref()); } /// Change the path of the [`DID`]. #[inline] pub fn set_path(&mut self, value: impl AsRef<str>) { self.core.set_path(&mut self.data, value.as_ref()); } /// Change the query of the [`DID`]. /// /// No serialization is performed. #[inline] pub fn set_query(&mut self, value: Option<&str>) { self.core.set_query(&mut self.data, value); } /// Change the fragment of the [`DID`]. /// /// No serialization is performed. #[inline] pub fn set_fragment(&mut self, value: Option<&str>) { self.core.set_fragment(&mut self.data, value); } /// Creates a new [`DID`] by joining `self` with the relative DID `other`. /// /// # Errors /// /// Returns `Err` if any base or relative DID segments are invalid. #[cfg(feature = "alloc")] pub fn join(&self, other: impl AsRef<str>) -> Result<Self> { let data: &str = other.as_ref(); let core: Core = Core::parse_relative(data)?; resolution::transform_references(self, (data, &core)) } } impl Hash for DID { fn hash<H>(&self, hasher: &mut H) where H: Hasher, { self.as_str().hash(hasher) } } impl PartialEq for DID { fn eq(&self, other: &Self) -> bool { self.as_str() == other.as_str() } } impl Eq for DID {} impl PartialOrd for DID { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.as_str().partial_cmp(other.as_str()) } } impl Ord for DID { fn cmp(&self, other: &Self) -> Ordering { self.as_str().cmp(other.as_str()) } } impl PartialEq<str> for DID { fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl PartialEq<&'_ str> for DID { fn eq(&self, other: &&'_ str) -> bool { self == *other } } impl Debug for DID { fn fmt(&self, f: &mut Formatter) -> FmtResult { f.write_fmt(format_args!("{:?}", self.as_str())) } } impl Display for DID { fn fmt(&self, f: &mut Formatter) -> FmtResult { f.write_fmt(format_args!("{}", self.as_str())) } } impl AsRef<str> for DID { fn as_ref(&self) -> &str { self.data.as_ref() } } impl FromStr for DID { type Err = Error; fn from_str(string: &str) -> Result<Self, Self::Err> { Self::parse(string) } } #[cfg(feature = "alloc")] impl TryFrom<String> for DID { type Error = Error; fn try_from(other: String) -> Result<Self, Self::Error> { Self::parse(other) } } #[cfg(feature = "alloc")] impl From<DID> for String { fn from(other: DID) -> Self { other.into_string() } } // ============================================================================= // Reference Resolution // See RFC 3986 - https://tools.ietf.org/html/rfc3986#section-5 // ============================================================================= #[cfg(feature = "alloc")] mod resolution { use alloc::borrow::Cow; use core::fmt::Display; use core::fmt::Formatter; use core::fmt::Result as FmtResult; use core::str::from_utf8_unchecked; use crate::core::Core; use crate::did::DID; use crate::error::Error; use crate::error::Result; #[derive(Debug)] #[repr(transparent)] pub struct Path<'a>(Cow<'a, str>); impl<'a> Path<'a> { pub const fn new() -> Self { Self(Cow::Borrowed("")) } pub fn push(&mut self, value: impl AsRef<[u8]>) { self .0 .to_mut() .push_str(unsafe { from_utf8_unchecked(value.as_ref()) }); } pub fn pop(&mut self) { if self.0.is_empty() { return; } if let Some(index) = self.0.rfind('/') { self.0.to_mut().replace_range(index.., ""); } } } impl<'a> From<Path<'a>> for Cow<'a, str> { fn from(other: Path<'a>) -> Self { other.0 } } impl Display for Path<'_> { fn fmt(&self, f: &mut Formatter) -> FmtResult { Display::fmt(&self.0, f) } } /// Transform References. /// /// Transforms a DID reference into its target DID. /// /// [More Info](https://tools.ietf.org/html/rfc3986#section-5.2.2) #[allow(non_snake_case)] pub fn transform_references(base: &DID, (data, core): (&str, &Core)) -> Result<DID> { let P: &str = core.path(data); let Q: Option<&str> = core.query(data); let mut T: DID = base.clone(); if P.is_empty() { T.set_path(base.path()); T.set_query(Q.or_else(|| base.query())); } else { if P.starts_with('/') { T.set_path(remove_dot_segments(P)); } else { T.set_path(remove_dot_segments(&merge_paths(base, P)?)); } T.set_query(Q); } T.set_method(base.method()); // TODO: Remove? This in inherited via clone T.set_method_id(base.method_id()); // TODO: Remove? This in inherited via clone T.set_fragment(core.fragment(data)); Ok(T) } /// Merge Paths. /// /// Merges a relative-path reference with the path of the base DID. /// /// [More Info](https://tools.ietf.org/html/rfc3986#section-5.2.3) pub fn merge_paths<'a>(base: &'a DID, data: &'a str) -> Result<Cow<'a, str>> { // Ensure the base DID has an authority component. // // The DID authority is `<method>:<method-specific-id>` so it should always // be present for non-relative DIDs. if base.method().is_empty() || base.method_id().is_empty() { return Err(Error::InvalidAuthority); } // 1. If the base URI has a defined authority component and an empty // path, then return a string consisting of "/" concatenated with the // reference's path. if base.path().is_empty() { return Ok(data.into()); } // 2. Return a string consisting of the reference's path component // appended to all but the last segment of the base URI's path (i.e., // excluding any characters after the right-most "/" in the base URI // path, or excluding the entire base URI path if it does not contain // any "/" characters). let mut path: &str = base.path(); if let Some(index) = path.rfind('/') { path = &path[..=index]; } Ok([path, data].join("").into()) } /// Remove Dot Segments. /// /// [More Info](https://tools.ietf.org/html/rfc3986#section-5.2.4) pub fn remove_dot_segments(path: &str) -> Cow<str> { fn next_segment(input: impl AsRef<[u8]>) -> Option<usize> { match input.as_ref() { [b'/', input @ ..] => next_segment(input).map(|index| index + 1), input => input.iter().position(|byte| *byte == b'/'), } } let mut output: Path = Path::new(); let mut input: &[u8] = path.as_bytes(); loop { match input { // Remove prefix ../ [b'.', b'.', b'/', ..] => { input = &input[3..]; } // Remove prefix ./ [b'.', b'/', ..] => { input = &input[2..]; } // Replace prefix /./ [b'/', b'.', b'/', ..] =>
{ input = &input[2..]; }
conditional_block
did.rs
DID { data: String, core: Core, } impl DID { /// The URL scheme for Decentralized Identifiers. pub const SCHEME: &'static str = "did"; /// Parses a [`DID`] from the provided `input`. /// /// # Errors /// /// Returns `Err` if any DID segments are invalid. pub fn parse(input: impl AsRef<str>) -> Result<Self> { Ok(Self { data: input.as_ref().to_string(), core: Core::parse(input)?, }) } /// Returns a wrapped `DID` with a more detailed `Debug` implementation. #[inline] pub const fn inspect(&self) -> Inspect { Inspect(self) } /// Returns the serialized [`DID`]. /// /// This is fast since the serialized value is stored in the [`DID`]. #[inline] pub fn as_str(&self) -> &str { &*self.data } /// Consumes the [`DID`] and returns the serialization. #[cfg(feature = "alloc")] #[inline] pub fn into_string(self) -> String { self.data } /// Returns the [`DID`] scheme. See [`DID::SCHEME`]. #[inline] pub const fn scheme(&self) -> &'static str { DID::SCHEME } /// Returns the [`DID`] authority. #[inline] pub fn authority(&self) -> &str { self.core.authority(self.as_str()) } /// Returns the [`DID`] method name. #[inline] pub fn method(&self) -> &str { self.core.method(self.as_str()) } /// Returns the [`DID`] method-specific ID. #[inline] pub fn method_id(&self) -> &str { self.core.method_id(self.as_str()) } /// Returns the [`DID`] path. #[inline] pub fn path(&self) -> &str { self.core.path(self.as_str()) } /// Returns the [`DID`] method query, if any. #[inline] pub fn query(&self) -> Option<&str> { self.core.query(self.as_str()) } /// Returns the [`DID`] method fragment, if any. #[inline] pub fn fragment(&self) -> Option<&str> { self.core.fragment(self.as_str()) } /// Parses the [`DID`] query and returns an iterator of (key, value) pairs. #[inline] pub fn query_pairs(&self) -> form_urlencoded::Parse { self.core.query_pairs(self.as_str()) } /// Change the method of the [`DID`]. #[inline] pub fn set_method(&mut self, value: impl AsRef<str>) { self.core.set_method(&mut self.data, value.as_ref()); } /// Change the method-specific-id of the [`DID`]. #[inline] pub fn set_method_id(&mut self, value: impl AsRef<str>) { self.core.set_method_id(&mut self.data, value.as_ref()); } /// Change the path of the [`DID`]. #[inline] pub fn set_path(&mut self, value: impl AsRef<str>) { self.core.set_path(&mut self.data, value.as_ref()); } /// Change the query of the [`DID`]. /// /// No serialization is performed. #[inline] pub fn set_query(&mut self, value: Option<&str>) { self.core.set_query(&mut self.data, value); } /// Change the fragment of the [`DID`]. /// /// No serialization is performed. #[inline] pub fn set_fragment(&mut self, value: Option<&str>) { self.core.set_fragment(&mut self.data, value); } /// Creates a new [`DID`] by joining `self` with the relative DID `other`. /// /// # Errors /// /// Returns `Err` if any base or relative DID segments are invalid. #[cfg(feature = "alloc")] pub fn join(&self, other: impl AsRef<str>) -> Result<Self> { let data: &str = other.as_ref(); let core: Core = Core::parse_relative(data)?; resolution::transform_references(self, (data, &core)) } } impl Hash for DID { fn hash<H>(&self, hasher: &mut H) where H: Hasher, { self.as_str().hash(hasher) } } impl PartialEq for DID { fn eq(&self, other: &Self) -> bool { self.as_str() == other.as_str() } } impl Eq for DID {} impl PartialOrd for DID { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.as_str().partial_cmp(other.as_str()) } } impl Ord for DID { fn cmp(&self, other: &Self) -> Ordering { self.as_str().cmp(other.as_str()) } } impl PartialEq<str> for DID { fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl PartialEq<&'_ str> for DID { fn eq(&self, other: &&'_ str) -> bool { self == *other } } impl Debug for DID { fn fmt(&self, f: &mut Formatter) -> FmtResult
} impl Display for DID { fn fmt(&self, f: &mut Formatter) -> FmtResult { f.write_fmt(format_args!("{}", self.as_str())) } } impl AsRef<str> for DID { fn as_ref(&self) -> &str { self.data.as_ref() } } impl FromStr for DID { type Err = Error; fn from_str(string: &str) -> Result<Self, Self::Err> { Self::parse(string) } } #[cfg(feature = "alloc")] impl TryFrom<String> for DID { type Error = Error; fn try_from(other: String) -> Result<Self, Self::Error> { Self::parse(other) } } #[cfg(feature = "alloc")] impl From<DID> for String { fn from(other: DID) -> Self { other.into_string() } } // ============================================================================= // Reference Resolution // See RFC 3986 - https://tools.ietf.org/html/rfc3986#section-5 // ============================================================================= #[cfg(feature = "alloc")] mod resolution { use alloc::borrow::Cow; use core::fmt::Display; use core::fmt::Formatter; use core::fmt::Result as FmtResult; use core::str::from_utf8_unchecked; use crate::core::Core; use crate::did::DID; use crate::error::Error; use crate::error::Result; #[derive(Debug)] #[repr(transparent)] pub struct Path<'a>(Cow<'a, str>); impl<'a> Path<'a> { pub const fn new() -> Self { Self(Cow::Borrowed("")) } pub fn push(&mut self, value: impl AsRef<[u8]>) { self .0 .to_mut() .push_str(unsafe { from_utf8_unchecked(value.as_ref()) }); } pub fn pop(&mut self) { if self.0.is_empty() { return; } if let Some(index) = self.0.rfind('/') { self.0.to_mut().replace_range(index.., ""); } } } impl<'a> From<Path<'a>> for Cow<'a, str> { fn from(other: Path<'a>) -> Self { other.0 } } impl Display for Path<'_> { fn fmt(&self, f: &mut Formatter) -> FmtResult { Display::fmt(&self.0, f) } } /// Transform References. /// /// Transforms a DID reference into its target DID. /// /// [More Info](https://tools.ietf.org/html/rfc3986#section-5.2.2) #[allow(non_snake_case)] pub fn transform_references(base: &DID, (data, core): (&str, &Core)) -> Result<DID> { let P: &str = core.path(data); let Q: Option<&str> = core.query(data); let mut T: DID = base.clone(); if P.is_empty() { T.set_path(base.path()); T.set_query(Q.or_else(|| base.query())); } else { if P.starts_with('/') { T.set_path(remove_dot_segments(P)); } else { T.set_path(remove_dot_segments(&merge_paths(base, P)?)); } T.set_query(Q); } T.set_method(base.method()); // TODO: Remove? This in inherited via clone T.set_method_id(base.method_id()); // TODO: Remove? This in inherited via clone T.set_fragment(core.fragment(data)); Ok(T) } /// Merge Paths. /// /// Merges a relative-path reference with the path of the base DID. /// /// [More Info](https://tools.ietf.org/html/rfc
{ f.write_fmt(format_args!("{:?}", self.as_str())) }
identifier_body
did.rs
struct DID { data: String, core: Core, } impl DID { /// The URL scheme for Decentralized Identifiers. pub const SCHEME: &'static str = "did"; /// Parses a [`DID`] from the provided `input`. /// /// # Errors /// /// Returns `Err` if any DID segments are invalid. pub fn parse(input: impl AsRef<str>) -> Result<Self> { Ok(Self { data: input.as_ref().to_string(), core: Core::parse(input)?, }) } /// Returns a wrapped `DID` with a more detailed `Debug` implementation. #[inline] pub const fn inspect(&self) -> Inspect { Inspect(self) } /// Returns the serialized [`DID`]. /// /// This is fast since the serialized value is stored in the [`DID`]. #[inline] pub fn as_str(&self) -> &str { &*self.data } /// Consumes the [`DID`] and returns the serialization. #[cfg(feature = "alloc")] #[inline] pub fn into_string(self) -> String { self.data } /// Returns the [`DID`] scheme. See [`DID::SCHEME`]. #[inline] pub const fn scheme(&self) -> &'static str { DID::SCHEME } /// Returns the [`DID`] authority. #[inline] pub fn authority(&self) -> &str { self.core.authority(self.as_str()) } /// Returns the [`DID`] method name. #[inline] pub fn method(&self) -> &str { self.core.method(self.as_str()) } /// Returns the [`DID`] method-specific ID. #[inline] pub fn method_id(&self) -> &str { self.core.method_id(self.as_str()) } /// Returns the [`DID`] path.
self.core.path(self.as_str()) } /// Returns the [`DID`] method query, if any. #[inline] pub fn query(&self) -> Option<&str> { self.core.query(self.as_str()) } /// Returns the [`DID`] method fragment, if any. #[inline] pub fn fragment(&self) -> Option<&str> { self.core.fragment(self.as_str()) } /// Parses the [`DID`] query and returns an iterator of (key, value) pairs. #[inline] pub fn query_pairs(&self) -> form_urlencoded::Parse { self.core.query_pairs(self.as_str()) } /// Change the method of the [`DID`]. #[inline] pub fn set_method(&mut self, value: impl AsRef<str>) { self.core.set_method(&mut self.data, value.as_ref()); } /// Change the method-specific-id of the [`DID`]. #[inline] pub fn set_method_id(&mut self, value: impl AsRef<str>) { self.core.set_method_id(&mut self.data, value.as_ref()); } /// Change the path of the [`DID`]. #[inline] pub fn set_path(&mut self, value: impl AsRef<str>) { self.core.set_path(&mut self.data, value.as_ref()); } /// Change the query of the [`DID`]. /// /// No serialization is performed. #[inline] pub fn set_query(&mut self, value: Option<&str>) { self.core.set_query(&mut self.data, value); } /// Change the fragment of the [`DID`]. /// /// No serialization is performed. #[inline] pub fn set_fragment(&mut self, value: Option<&str>) { self.core.set_fragment(&mut self.data, value); } /// Creates a new [`DID`] by joining `self` with the relative DID `other`. /// /// # Errors /// /// Returns `Err` if any base or relative DID segments are invalid. #[cfg(feature = "alloc")] pub fn join(&self, other: impl AsRef<str>) -> Result<Self> { let data: &str = other.as_ref(); let core: Core = Core::parse_relative(data)?; resolution::transform_references(self, (data, &core)) } } impl Hash for DID { fn hash<H>(&self, hasher: &mut H) where H: Hasher, { self.as_str().hash(hasher) } } impl PartialEq for DID { fn eq(&self, other: &Self) -> bool { self.as_str() == other.as_str() } } impl Eq for DID {} impl PartialOrd for DID { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.as_str().partial_cmp(other.as_str()) } } impl Ord for DID { fn cmp(&self, other: &Self) -> Ordering { self.as_str().cmp(other.as_str()) } } impl PartialEq<str> for DID { fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl PartialEq<&'_ str> for DID { fn eq(&self, other: &&'_ str) -> bool { self == *other } } impl Debug for DID { fn fmt(&self, f: &mut Formatter) -> FmtResult { f.write_fmt(format_args!("{:?}", self.as_str())) } } impl Display for DID { fn fmt(&self, f: &mut Formatter) -> FmtResult { f.write_fmt(format_args!("{}", self.as_str())) } } impl AsRef<str> for DID { fn as_ref(&self) -> &str { self.data.as_ref() } } impl FromStr for DID { type Err = Error; fn from_str(string: &str) -> Result<Self, Self::Err> { Self::parse(string) } } #[cfg(feature = "alloc")] impl TryFrom<String> for DID { type Error = Error; fn try_from(other: String) -> Result<Self, Self::Error> { Self::parse(other) } } #[cfg(feature = "alloc")] impl From<DID> for String { fn from(other: DID) -> Self { other.into_string() } } // ============================================================================= // Reference Resolution // See RFC 3986 - https://tools.ietf.org/html/rfc3986#section-5 // ============================================================================= #[cfg(feature = "alloc")] mod resolution { use alloc::borrow::Cow; use core::fmt::Display; use core::fmt::Formatter; use core::fmt::Result as FmtResult; use core::str::from_utf8_unchecked; use crate::core::Core; use crate::did::DID; use crate::error::Error; use crate::error::Result; #[derive(Debug)] #[repr(transparent)] pub struct Path<'a>(Cow<'a, str>); impl<'a> Path<'a> { pub const fn new() -> Self { Self(Cow::Borrowed("")) } pub fn push(&mut self, value: impl AsRef<[u8]>) { self .0 .to_mut() .push_str(unsafe { from_utf8_unchecked(value.as_ref()) }); } pub fn pop(&mut self) { if self.0.is_empty() { return; } if let Some(index) = self.0.rfind('/') { self.0.to_mut().replace_range(index.., ""); } } } impl<'a> From<Path<'a>> for Cow<'a, str> { fn from(other: Path<'a>) -> Self { other.0 } } impl Display for Path<'_> { fn fmt(&self, f: &mut Formatter) -> FmtResult { Display::fmt(&self.0, f) } } /// Transform References. /// /// Transforms a DID reference into its target DID. /// /// [More Info](https://tools.ietf.org/html/rfc3986#section-5.2.2) #[allow(non_snake_case)] pub fn transform_references(base: &DID, (data, core): (&str, &Core)) -> Result<DID> { let P: &str = core.path(data); let Q: Option<&str> = core.query(data); let mut T: DID = base.clone(); if P.is_empty() { T.set_path(base.path()); T.set_query(Q.or_else(|| base.query())); } else { if P.starts_with('/') { T.set_path(remove_dot_segments(P)); } else { T.set_path(remove_dot_segments(&merge_paths(base, P)?)); } T.set_query(Q); } T.set_method(base.method()); // TODO: Remove? This in inherited via clone T.set_method_id(base.method_id()); // TODO: Remove? This in inherited via clone T.set_fragment(core.fragment(data)); Ok(T) } /// Merge Paths. /// /// Merges a relative-path reference with the path of the base DID. /// /// [More Info](https://tools.ietf.org/html/rfc3
#[inline] pub fn path(&self) -> &str {
random_line_split
did.rs
.core.authority(self.as_str()) } /// Returns the [`DID`] method name. #[inline] pub fn method(&self) -> &str { self.core.method(self.as_str()) } /// Returns the [`DID`] method-specific ID. #[inline] pub fn method_id(&self) -> &str { self.core.method_id(self.as_str()) } /// Returns the [`DID`] path. #[inline] pub fn path(&self) -> &str { self.core.path(self.as_str()) } /// Returns the [`DID`] method query, if any. #[inline] pub fn query(&self) -> Option<&str> { self.core.query(self.as_str()) } /// Returns the [`DID`] method fragment, if any. #[inline] pub fn fragment(&self) -> Option<&str> { self.core.fragment(self.as_str()) } /// Parses the [`DID`] query and returns an iterator of (key, value) pairs. #[inline] pub fn query_pairs(&self) -> form_urlencoded::Parse { self.core.query_pairs(self.as_str()) } /// Change the method of the [`DID`]. #[inline] pub fn set_method(&mut self, value: impl AsRef<str>) { self.core.set_method(&mut self.data, value.as_ref()); } /// Change the method-specific-id of the [`DID`]. #[inline] pub fn set_method_id(&mut self, value: impl AsRef<str>) { self.core.set_method_id(&mut self.data, value.as_ref()); } /// Change the path of the [`DID`]. #[inline] pub fn set_path(&mut self, value: impl AsRef<str>) { self.core.set_path(&mut self.data, value.as_ref()); } /// Change the query of the [`DID`]. /// /// No serialization is performed. #[inline] pub fn set_query(&mut self, value: Option<&str>) { self.core.set_query(&mut self.data, value); } /// Change the fragment of the [`DID`]. /// /// No serialization is performed. #[inline] pub fn set_fragment(&mut self, value: Option<&str>) { self.core.set_fragment(&mut self.data, value); } /// Creates a new [`DID`] by joining `self` with the relative DID `other`. /// /// # Errors /// /// Returns `Err` if any base or relative DID segments are invalid. #[cfg(feature = "alloc")] pub fn join(&self, other: impl AsRef<str>) -> Result<Self> { let data: &str = other.as_ref(); let core: Core = Core::parse_relative(data)?; resolution::transform_references(self, (data, &core)) } } impl Hash for DID { fn hash<H>(&self, hasher: &mut H) where H: Hasher, { self.as_str().hash(hasher) } } impl PartialEq for DID { fn eq(&self, other: &Self) -> bool { self.as_str() == other.as_str() } } impl Eq for DID {} impl PartialOrd for DID { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { self.as_str().partial_cmp(other.as_str()) } } impl Ord for DID { fn cmp(&self, other: &Self) -> Ordering { self.as_str().cmp(other.as_str()) } } impl PartialEq<str> for DID { fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl PartialEq<&'_ str> for DID { fn eq(&self, other: &&'_ str) -> bool { self == *other } } impl Debug for DID { fn fmt(&self, f: &mut Formatter) -> FmtResult { f.write_fmt(format_args!("{:?}", self.as_str())) } } impl Display for DID { fn fmt(&self, f: &mut Formatter) -> FmtResult { f.write_fmt(format_args!("{}", self.as_str())) } } impl AsRef<str> for DID { fn as_ref(&self) -> &str { self.data.as_ref() } } impl FromStr for DID { type Err = Error; fn from_str(string: &str) -> Result<Self, Self::Err> { Self::parse(string) } } #[cfg(feature = "alloc")] impl TryFrom<String> for DID { type Error = Error; fn try_from(other: String) -> Result<Self, Self::Error> { Self::parse(other) } } #[cfg(feature = "alloc")] impl From<DID> for String { fn from(other: DID) -> Self { other.into_string() } } // ============================================================================= // Reference Resolution // See RFC 3986 - https://tools.ietf.org/html/rfc3986#section-5 // ============================================================================= #[cfg(feature = "alloc")] mod resolution { use alloc::borrow::Cow; use core::fmt::Display; use core::fmt::Formatter; use core::fmt::Result as FmtResult; use core::str::from_utf8_unchecked; use crate::core::Core; use crate::did::DID; use crate::error::Error; use crate::error::Result; #[derive(Debug)] #[repr(transparent)] pub struct Path<'a>(Cow<'a, str>); impl<'a> Path<'a> { pub const fn new() -> Self { Self(Cow::Borrowed("")) } pub fn push(&mut self, value: impl AsRef<[u8]>) { self .0 .to_mut() .push_str(unsafe { from_utf8_unchecked(value.as_ref()) }); } pub fn pop(&mut self) { if self.0.is_empty() { return; } if let Some(index) = self.0.rfind('/') { self.0.to_mut().replace_range(index.., ""); } } } impl<'a> From<Path<'a>> for Cow<'a, str> { fn from(other: Path<'a>) -> Self { other.0 } } impl Display for Path<'_> { fn fmt(&self, f: &mut Formatter) -> FmtResult { Display::fmt(&self.0, f) } } /// Transform References. /// /// Transforms a DID reference into its target DID. /// /// [More Info](https://tools.ietf.org/html/rfc3986#section-5.2.2) #[allow(non_snake_case)] pub fn transform_references(base: &DID, (data, core): (&str, &Core)) -> Result<DID> { let P: &str = core.path(data); let Q: Option<&str> = core.query(data); let mut T: DID = base.clone(); if P.is_empty() { T.set_path(base.path()); T.set_query(Q.or_else(|| base.query())); } else { if P.starts_with('/') { T.set_path(remove_dot_segments(P)); } else { T.set_path(remove_dot_segments(&merge_paths(base, P)?)); } T.set_query(Q); } T.set_method(base.method()); // TODO: Remove? This in inherited via clone T.set_method_id(base.method_id()); // TODO: Remove? This in inherited via clone T.set_fragment(core.fragment(data)); Ok(T) } /// Merge Paths. /// /// Merges a relative-path reference with the path of the base DID. /// /// [More Info](https://tools.ietf.org/html/rfc3986#section-5.2.3) pub fn merge_paths<'a>(base: &'a DID, data: &'a str) -> Result<Cow<'a, str>> { // Ensure the base DID has an authority component. // // The DID authority is `<method>:<method-specific-id>` so it should always // be present for non-relative DIDs. if base.method().is_empty() || base.method_id().is_empty() { return Err(Error::InvalidAuthority); } // 1. If the base URI has a defined authority component and an empty // path, then return a string consisting of "/" concatenated with the // reference's path. if base.path().is_empty() { return Ok(data.into()); } // 2. Return a string consisting of the reference's path component // appended to all but the last segment of the base URI's path (i.e., // excluding any characters after the right-most "/" in the base URI // path, or excluding the entire base URI path if it does not contain // any "/" characters). let mut path: &str = base.path(); if let Some(index) = path.rfind('/') { path = &path[..=index]; } Ok([path, data].join("").into()) } /// Remove Dot Segments. /// /// [More Info](https://tools.ietf.org/html/rfc3986#section-5.2.4) pub fn
remove_dot_segments
identifier_name