def split_file(clazz, filename, chunk_size, zfill_length=None, output_directory=None): check.check_string(filename) check.check_int(chunk_size) check.check_int(zfill_length, allow_none=True) file_size = file_util.size(filename) clazz._log.log_method_d() num_total = int(math.ceil(float(file_size) / float(chunk_size))) result_file_list = [] zfill_length = zfill_length or len(str(num_total)) output_directory = output_directory or path.dirname(filename) with open(filename, 'rb') as fin: index = 0 while True: data = fin.read(chunk_size) if not data: break next_filename = clazz._make_split_filename( filename, output_directory, index + 1, zfill_length) with open(next_filename, 'wb') as fout: fout.write(data) result_file_list.append(next_filename) index += 1 return result_file_list
def lsof(clazz, pid=None): 'Return a list of lsof items for all process or a specific process' check.check_int(pid, allow_none=True) args = [] if pid: args.extend(['-p', str(pid)]) rv = lsof_command.call_command(args) return lsof_output_parser.parse_lsof_output(rv.stdout)
def reindent_files(clazz, files, indent, backup): check.check_int(indent) check.check_bool(backup) clazz._log.log_method_d() python_files = clazz.resolve_python_files(files) for filename in python_files: refactor_reindent.reindent_file(filename, indent, backup)
def check_value_types(self): 'Check the type of each option.' check.check_bool(self.verbose) check.check_bool(self.debug) check.check_string(self.username, allow_none=True) check.check_string(self.password, allow_none=True) check.check_int(self.port, allow_none=True) check.check_int(self.num_tries) check.check_float(self.sleep_time)
def reindent_file(clazz, filename, indent, backup): check.check_string(filename) check.check_int(indent) check.check_bool(backup) file_check.check_file(filename) clazz._log.log_method_d() backup_args = [] if backup else ['--nobackup'] args = ['--indent', str(indent)] + backup_args + [filename] reindent_main(args)
def insert(clazz, text, position, insert_text): 'Insert insert_text into text at position.' check.check_string(text) check.check_int(position) check.check_string(insert_text) buf = StringIO() left = text[0:position] right = text[position:] buf.write(left) buf.write(insert_text) buf.write(right) return buf.getvalue()
def save_file(clazz, filename, o, indent=None, sort_keys=False, codec=None): check.check_string(filename) check.check_int(indent, allow_none=True) check.check_bool(sort_keys) check.check_string(codec, allow_none=True) content = clazz.to_json(o, indent=indent, sort_keys=sort_keys) codec = codec or 'utf-8' with open(filename, 'w', encoding=codec) as f: f.write(content)
def to_json(clazz, o, indent=None, sort_keys=False): check.check_int(indent, allow_none=True) check.check_bool(sort_keys) ''' Like json.dumps plus the following: - same white space results on both python 2 and 3 - __dict__ is used when object is not json encodable ''' def default(o): return o.__dict__ return json.dumps(o, indent=indent, default=default, sort_keys=sort_keys, separators=(', ', ': '))
def word_has_boundary(clazz, text, start, end, boundary_chars=None): check.check_string(text) check.check_int(start) check.check_int(end) check.check_set(boundary_chars, allow_none=True) boundary_chars = boundary_chars or clazz.CHARS if start >= 1: prev_char = text[start - 1] prev_char_is_boundary = prev_char in boundary_chars #print(f'prev_char={prev_char} prev_char_is_boundary={prev_char_is_boundary}') if not prev_char_is_boundary: return False if end < (len(text) - 1): next_char = text[end + 1] next_char_is_boundary = next_char in boundary_chars #print(f'next_char={next_char} next_char_is_boundary={next_char_is_boundary}') if not next_char_is_boundary: return False return True
def search(clazz, root_dir, text, relative=True, min_depth=None, max_depth=None): check.check_string(root_dir) check.check_string(text) check.check_bool(relative) check.check_int(min_depth, allow_none=True) check.check_int(max_depth, allow_none=True) files = file_find.find(root_dir, relative=relative, min_depth=min_depth, max_depth=max_depth) items = [] for f in files: fpath = path.join(root_dir, f) next_items = clazz.search_file(fpath, text) items += next_items if relative: return [item.become_relative(root_dir) for item in items] return items
def num_iterations(self, n): check.check_int(n) if not n in range(1, 110): raise ValueError('Iterations needs to be between 1 and 10: %d' % (n)) self._num_iterations = n
def open_handles(clazz, pid): check.check_int(pid) args = [ '-p', str(pid) ] rv = handle_exe.call_handle(args) return handle_output_parser.parse_handle_output(rv.stdout)