def init_repo(args={}, debug=False, init=False): ''' initilize a repo as coolkit repo with default configuration if any parent repo is already initilized then we will just copy the config from there ''' cwd = abs_path(os.getcwd()) home_loc = abs_path('~') now = cwd while (now != home_loc): if ('.coolkit' in os.listdir(now) and os.path.isdir(os.path.join(now, '.coolkit'))): if (debug): print('got .coolkit at ', now) break now = abs_path(os.path.join(now, os.pardir)) if (now == home_loc): verify_folder(cwd + '/.coolkit/') srbjson.create_file(cwd + '/.coolkit/config', srbjson.local_template) print(Colour.GREEN + 'initialized empty CoolKit repository in ' + cwd + '/.coolkit/' + Colour.END) elif (now != cwd): verify_folder(cwd + '/.coolkit/') shutil.copy(now + '/.coolkit/config', cwd + '/.coolkit/config') print(Colour.GREEN + 'initialized empty CoolKit repository in ' + cwd + '/.coolkit/' + Colour.END) else: if (init): print(Colour.YELLOW + 'Already a coolkit repo' + Colour.END) if (not 'c_name' in args or not args['c_name']): contest_name = get_contest_name(cwd.split('/')[-1]) if (not contest_name): args['c_name'] = None else: args['c_name'] = contest_name srbjson.dump_data(args, cwd + '/.coolkit/config', srbjson.local_template)
def check_init(): ''' set config to global config file. ''' cwd = abs_path(os.getcwd()) home_loc = abs_path('~') now = cwd while (now != home_loc): if ('.coolkit' in os.listdir(now) and os.path.isdir(os.path.join(now, '.coolkit'))): return True now = abs_path(os.path.join(now, os.pardir)) return False
def fetch_data_from_local_config(): verify_init() cwd = abs_path(os.getcwd()) home_loc = abs_path('~') now = cwd while (now != home_loc): if ('.coolkit' in os.listdir(now) and os.path.isdir(os.path.join(now, '.coolkit'))): break now = abs_path(os.path.join(now, os.pardir)) data = srbjson.extract_data(now + '/.coolkit/config', srbjson.local_template) return data
def _write_data(data, file_name): """ Write RAW data into a json file """ fille = abs_path(file_name) jfile = open(fille, 'w') data = {'coolkit': data} json.dump(data, jfile, indent=4, ensure_ascii=False) jfile.close()
class Const: cache_dir = abs_path('~/.config/coolkit') mult_soln_words = [ 'multiple possible', 'multiple answers', 'output any', 'any', 'multiple' ] def __init__(): pass
def verify_folder(folder,debug=False): ''' similar to mkdir -p ''' folder = abs_path(folder) if not os.path.exists(folder): if(debug): print('creating folder '+ folder) os.makedirs(folder) elif os.path.isfile(folder): if(debug): print('there exists file of same name')
def verify_file(file_path,debug=False): file_path = abs_path(file_path) parent_dir = os.path.join(file_path,os.pardir) verify_folder(parent_dir) if not os.path.exists(file_path): if(debug): print('creating file '+ file_path) file_ = open(file_path, 'w') file_.close() elif os.path.isdir(file_path): if(debug): print('there exists folder of same name')
def set_local_config(args={}, debug=False): ''' set config to config file. parent config if found creates init if not ''' if (not check_init()): init_repo() cwd = abs_path(os.getcwd()) home_loc = abs_path('~') now = cwd while (now != home_loc): if ('.coolkit' in os.listdir(now) and os.path.isdir(os.path.join(now, '.coolkit'))): if (debug): print('got .coolkit at ', now) break now = abs_path(os.path.join(now, os.pardir)) srbjson.dump_data(args, now + '/.coolkit/config', srbjson.local_template)
def dump_data(data, file_name, template): """ create RAW data from LIST uses _write_data """ fille = abs_path(file_name) dictt = srbjson.extract_data(fille, template) for key in data: if (key in dictt): dictt[key] = data[key] srbjson._write_data(dictt, file_name)
def extract_data(file_name, template): """ Extracts json data from the given file if there is no such file it will create one if there is currupt file it will create new if file is ok it will return its content """ fille = abs_path(file_name) try: jfile = open(fille) except FileNotFoundError: srbjson.create_file(fille, template) jfile = open(fille) data = json.load(jfile) if (not 'coolkit' in data.keys()): srbjson.create_file(fille, template) jfile = open(fille) data = json.load(jfile) return data['coolkit']
def set_global_config(args={}): ''' set config to global config file. ''' srbjson.dump_data(args, abs_path(Const.cache_dir + '/config'), srbjson.global_template)
def fetch_data_from_global_config(): data = srbjson.extract_data(abs_path('~/.config/coolkit/config'), srbjson.global_template) return data
def display_ques(contest_num=920, prob_num='A'): pwd = str(os.getcwd()) print(pwd) file_name = pwd + '/.config' file_name = abs_path(file_name) contest_num = str(contest_num) req = requests.get('http://codeforces.com/contest/' + contest_num + '/problem/' + prob_num) soup = BeautifulSoup(req.text, 'html.parser') a = soup.find_all(id='body') b = a[0].find_all('div', recursive=False) #inside body c = b[3].find_all('div', recursive=False) d = c[1].find_all('div', recursive=False) e = d[1].find_all('div', recursive=False) f = e[0].find_all('div', recursive=False) g = f[0].find_all('div', recursive=False) #reached to content h0 = g[0].find_all('div', recursive=False) #went under header print('Problem name : ', h0[0].get_text()[3:]) print('') #problem name printed h0[1] = h0[1].get_text() print('time limit per test :', h0[1][19:]) print('') #time printed print('memory limit per test :', h0[2].get_text()[21:]) print('') #memory printed print('Input :', h0[3].get_text()[5:]) print('') #input printed print('Output :', h0[4].get_text()[6:]) print('') #Output printed print('') print('Question :') ######## image = g[1].find_all('img') if len(image) > 0: print( "Image", ":", 'http://codeforces.com/contest/' + contest_num + '/problem/' + prob_num) ####### h1 = g[1].find_all('p', recursive=False) for i in range(0, len(h1)): print(h1[i].get_text()) #question printed print('') print('INPUT:') h2 = g[2].find_all('p', recursive=False) for i in range(0, len(h2)): print(h2[i].get_text()) #input printed print('') print('OUTPUT:') h3 = g[3].find_all('p', recursive=False) for i in range(0, len(h3)): print(h3[i].get_text(), end=('\n\n')) #output printed h4 = g[4].find_all('div', recursive=False) j = h4[1].find_all('div', recursive=False) #inside sample test var = [] print('EXAMPLES') for k in range(0, len(j)): if (k + 1) % 2 == 0: print('Output') else: print('Input') l = j[k].find_all('pre', recursive=False) var.insert(k, l[0].get_text('\n')) print(l[0].get_text('\n')) if k != (len(j) - 1): print('') #Examples printed and variables stored ina list but it has \n character<F5> print("TESTING") print(var)