def save_array(file_name, array_list): string = "" for i in range(len(array_list)): string += str(array_list[i]) if i < len(array_list) - 1: string += "\n" file_util.write_to_file(file_name, string)
def save_state(state, final=False): contents = state_to_contents(state) if kwargs['inplace']: do_backup = kwargs['suffix'] is not None and len(kwargs['suffix']) > 0 write_to_file(filename, contents, do_backup=do_backup, backup_ext=kwargs['suffix']) elif final: print(contents)
def rollback_losses_csv(file_name, current_itter): file_util.copy_as_backup(file_name) lines = file_util.read_all_lines(file_name) new_file = "" for i in range(len(lines)): line_split = lines[i].split(',') if int(line_split[0]) < current_itter: new_file += lines[i] + "\n" file_util.write_to_file(file_name, new_file)
def absolutize_imports(infile, **kwargs): filename = infile.name if kwargs['verbose']: kwargs['log']('Processing %s...' % filename) absolutized_contents = get_file(filename, update_globs=True, **kwargs) infile.close() if kwargs['inplace']: do_backup = kwargs['suffix'] is not None and len(kwargs['suffix']) > 0 write_to_file(filename, absolutized_contents, do_backup=do_backup, backup_ext=kwargs['suffix']) else: print(absolutized_contents)
def absolutize_imports(filename, **kwargs): if kwargs['verbose']: kwargs['log']('Processing %s...' % filename) absolutized_contents = get_file(filename, update_globs=True, **kwargs) if kwargs['inplace']: do_backup = kwargs['suffix'] is not None and len(kwargs['suffix']) > 0 write_to_file(filename, absolutized_contents, do_backup=do_backup, backup_ext=kwargs['suffix']) else: print(absolutized_contents)
def move_from_proof(filename, **kwargs): if kwargs['verbose']: kwargs['log']('Processing %s...' % filename) try: with open(filename, 'r') as f: contents = f.read() except IOError as e: if kwargs['verbose']: kwargs['log']('Failed to process %s' % filename) if kwargs['verbose'] >= 2: kwargs['log'](repr(e)) return ret = [] cur_statements = [] cur_statement = [] deferred_copied_statements = [] deferred_statements = [] orig_space_count = 0 cur_diff_space_count = 0 if ''.join(split_coq_file_contents_with_comments(contents)) != contents: kwargs['log']('WARNING: Could not split %s' % filename) return for i in split_coq_file_contents_with_comments(contents): is_definition_full = (ALL_DEFINITIONS_REG.match(i) is not None and (':=' in strip_parens(strip_comments(i)) or ONELINE_DEFINITIONS_REG.match(i))) is_definition_start = (ALL_DEFINITIONS_REG.match(i) is not None and ':=' not in strip_parens(strip_comments(i)) and not ONELINE_DEFINITIONS_REG.match(i)) #print((is_definition_start, ONELINE_DEFINITIONS_REG.match(i), ALL_DEFINITIONS_REG.match(i), i)) is_definition_end = ALL_ENDINGS.match(i) is not None #if 'not_reachable_iff' in i: # print((ALL_DEFINITIONS_REG.match(i), strip_parens(strip_comments(i)), ONELINE_DEFINITIONS_REG.match(i))) if not is_definition_start and not cur_statements and not cur_statement: if kwargs['verbose'] >= 3: kwargs['log'](repr(i)) ret.append(i) elif is_definition_start: if kwargs['verbose'] >= 2: kwargs['log']('Starting definition (%d): %s' % (len(deferred_statements), repr(i))) if not cur_statement and not deferred_statements: orig_space_count = len(get_leading_space(i)) if cur_statement: deferred_statements.append( (cur_diff_space_count, cur_statement)) cur_diff_space_count = max( 0, len(get_leading_space(i)) - orig_space_count) cur_statement = [remove_leading_space(i, cur_diff_space_count)] elif (SAFE_REG.match(i) or not i.strip()) and cur_statement: if kwargs['verbose'] >= 3: kwargs['log'](repr(i)) cur_statement.append(remove_leading_space(i, cur_diff_space_count)) elif is_definition_end and cur_statement: if kwargs['verbose'] >= 2: kwargs['log']('Ending definition: ' + repr(i)) cur_statement.append(remove_leading_space(i, cur_diff_space_count)) cur_statements.append(''.join(cur_statement)) #print(''.join(preminimize_lifted_statements(cur_statements))) if deferred_statements: cur_diff_space_count, cur_statement = deferred_statements.pop() deferred_copied_statements = list( preminimize_lifted_statements(deferred_copied_statements)) cur_statement.extend( deferred_copied_statements) # todo: fix indentation else: #print('extending') #print(''.join(minimize_lifted_statements(cur_statements))) ret.extend(list(minimize_lifted_statements(cur_statements))) cur_statement = [] cur_statements = [] deferred_copied_statements = [] elif MOVE_UP_REG.match(i) or is_definition_full: if kwargs['verbose'] >= 2: kwargs['log']('Lifting: ' + repr(i)) cur_statements.append(set_leading_space(i, orig_space_count)) elif COPY_UP_REG.match(i) and cur_statement: if kwargs['verbose'] >= 2: kwargs['log']('Lift-copying: ' + repr(i)) cur_statement.append(remove_leading_space(i, cur_diff_space_count)) cur_statements.append(set_leading_space(i, orig_space_count)) deferred_copied_statements.append( remove_leading_space(i, cur_diff_space_count)) else: raw_input('WARNING: Unrecognized: %s' % repr(i)) #print(cur_diff_space_count) #print(remove_leading_space(i, cur_diff_space_count)) if cur_statements or deferred_statements or cur_statement: raw_input('WARNING: extra statements: %s' % repr( (cur_statements, cur_statement, deferred_statements))) cur_statements.append(''.join(cur_statement)) for i in deferred_statements: cur_statements.append(''.join(i)) ret.extend(list(minimize_lifted_statements(cur_statements))) ret = ''.join(ret) if ret == contents: return if kwargs['inplace']: do_backup = kwargs['suffix'] is not None and len(kwargs['suffix']) > 0 write_to_file(filename, ret, do_backup=do_backup, backup_ext=kwargs['suffix']) else: print(ret)
def mwrite_to_file(file_name, contents, do_backup=False): return write_to_file(file_name, contents, do_backup=do_backup, memoize=True)
else: raw_input('WARNING: Unrecognized: %s' % repr(i)) #print(cur_diff_space_count) #print(remove_leading_space(i, cur_diff_space_count)) if cur_statements or deferred_statements or cur_statement: raw_input('WARNING: extra statements: %s' % repr((cur_statements, cur_statement, deferred_statements))) cur_statements.append(''.join(cur_statement)) for i in deferred_statements: cur_statements.append(''.join(i)) ret.extend(list(minimize_lifted_statements(cur_statements))) ret = ''.join(ret) if ret == contents: return if kwargs['inplace']: do_backup = kwargs['suffix'] is not None and len(kwargs['suffix']) > 0 write_to_file(filename, ret, do_backup=do_backup, backup_ext=kwargs['suffix']) else: print(ret) if __name__ == '__main__': args = process_logging_arguments(parser.parse_args()) env = { 'verbose': args.verbose, 'log': args.log, 'inplace': args.suffix != '', # it's None if they passed no argument, and '' if they didn't pass -i 'suffix': args.suffix, } for f in args.input_files: move_from_proof(f, **env)
#print(cur_diff_space_count) #print(remove_leading_space(i, cur_diff_space_count)) if cur_statements or deferred_statements or cur_statement: raw_input('WARNING: extra statements: %s' % repr( (cur_statements, cur_statement, deferred_statements))) cur_statements.append(''.join(cur_statement)) for i in deferred_statements: cur_statements.append(''.join(i)) ret.extend(list(minimize_lifted_statements(cur_statements))) ret = ''.join(ret) if ret == contents: return if kwargs['inplace']: do_backup = kwargs['suffix'] is not None and len(kwargs['suffix']) > 0 write_to_file(filename, ret, do_backup=do_backup, backup_ext=kwargs['suffix']) else: print(ret) if __name__ == '__main__': args = process_logging_arguments(parser.parse_args()) env = { 'verbose': args.verbose, 'log': args.log, 'inplace': args.suffix != '', # it's None if they passed no argument, and '' if they didn't pass -i 'suffix': args.suffix, }