def myMain(): x=input('Enter x:') if argv[1] == 'zen': log.level('info') log.d('debug') log.i('info x={}'.format(x)) log.w('warn') log.e('error') log.c('critical') else: if argv[1] == 'basic': # see Python Docs logging.html 16.6.7. LogRecord attributes logging.basicConfig(style='{',format="{asctime:s}:{filename:s}:{lineno:d}:{msg:s}",level='DEBUG') logging.debug('our debug message x={}'.format(x)) logging.warn('our warn message x={}'.format(x)) else: print("If we don't want to type logging.debug() etc, we have to create our own logger lg=logging.getLogger({})".format(__name__)) lg=logging.getLogger(__name__) if argv[1]=='easy': logging.basicConfig(level='INFO') else: lg.setLevel('WARN') # optional: defaults to all levels lg.addHandler(logging.StreamHandler()) lg.handlers[0].setFormatter(logging.Formatter(style='{',fmt="{filename:s}:{funcName:s}():{lineno:d}:{msg:s}")) lg.debug('our debug mesage') lg.info('our info here')
def get_web_page(url): ''' # Let the user know we are trying to download a webpage # Construct a request with a User-Agent header # Send the request and read the webpage from the response # Convert the webpage from bytes to a string, and return it ''' url = clean_link(url) log.i('getting: ' + url) handle = Request(url) handle.add_header('User-Agent', 'SPD/1.0') try: webpage = urlopen(handle).read() except HTTPError as err: log.w('a problem occured when getting ' + url) if err.code == 404: message = 'the requested page could not be found' else: message = 'the server returned a code of ' + str(err.code) log.w(message) webpage = str(err.code) # pass the return code as a string webpage = str(webpage) # convert from bytes to string log.d(len(webpage)) soup = BeautifulSoup(webpage, 'html.parser') return soup
def download_image_gallery(link): ''' # Fetch the HTML page at the given link # If it's a gfycat link, alter the url to point at the gif and download it # Otherwise, find all '//i.imgur.com' links and download each one ''' global args if 'gfycat.com/' in link: log.d('page is gfycat') if not re.search(r'\.(gif|webm)', link): link = link.replace('gfycat', 'fat.gfycat') + '.webm' download_image(link) elif 'imgur.com/' in link: webpage = get_web_page(link) if str(webpage) == '404' and re.search(r'layout/grid', link): log.w('grid layout not found, trying again') webpage = get_web_page(link.replace('/layout/grid', '')) log.d('page is imgur gallery (limited to first 20 images!)') urls = webpage.find_all('a') for url in urls: if re.search(args.imgur_gallery_image_regex, url.get('href')): log.d('matched imgur_gallery_image_regex in %s' % url) download_image(url.get('href')) else: log.d('page is imgur grid') urls = webpage.find_all('img') for url in urls: if url.get('data-src'): if re.search(args.imgur_grid_image_regex, url.get('data-src')): log.d('matched data-src in %s, grid image link' % url) download_image(url.get('data-src')) elif url.get('src'): if re.search(args.imgur_gallery_image_regex, url.get('src')): log.d('matched src in %s, gallery image link' % url) download_image(url.get('src')) else: log.d('no src, data-src in %s' % url)
def download_image(link): ''' # Let the user know we are trying to download an image at the given link # Prepare the command (wget) to download the image # If wget doesn't exist, we raise a FileNotFound error # Otherwise, if we're on Windows, modify the wget command to avoid an issue # with GnuWin wget and SSL # Finally, we run the constructed command to download the image ''' global args # --no-check-certificate is used on windows because GnuWin wget fails to # verify all certificates for some reason link = clean_link(link) log.i('downloading: ' + link) wget_command = [which('wget'), '-b', '-N', '-o', '/dev/null', link] if (which('wget') is None): raise WGetNotFoundException('Could not find wget') elif operating_system() == 'Windows': log.d('on windows, adjusting wget_command') wget_command = [ which('wget'), '-b', '-N', '-o', 'NUL', '--no-check-certificate', link ] try: while call(wget_command) != 0: time.sleep(.05) log.d('call is not 0') log.i('retrying...') except BlockingIOError: time.sleep(.1) log.d('BlockingIOError!') log.w('retrying...') download_image(link)
def download_image(link): ''' # Let the user know we are trying to download an image at the given link # Prepare the command (wget) to download the image # If wget doesn't exist, we raise a FileNotFound error # Otherwise, if we're on Windows, modify the wget command to avoid an issue # with GnuWin wget and SSL # Finally, we run the constructed command to download the image ''' global args # --no-check-certificate is used on windows because GnuWin wget fails to # verify all certificates for some reason link = clean_link(link) log.i('downloading: ' + link) wget_command = [which('wget'), '-b', '-N', '-o', '/dev/null', link] if (which('wget') is None): raise WGetNotFoundException('Could not find wget') elif operating_system() == 'Windows': log.d('on windows, adjusting wget_command') wget_command = [which('wget'), '-b', '-N', '-o', 'NUL', '--no-check-certificate', link] try: while call(wget_command) != 0: time.sleep(.05) log.d('call is not 0') log.i('retrying...') except BlockingIOError: time.sleep(.1) log.d('BlockingIOError!') log.w('retrying...') download_image(link)
def print_energies_on_file(self, energy_dict, folder_names, args): time = datetime.datetime.now() file_name = (f'{USER} - {time.strftime("%d-%m-%y")}.txt') x = PrettyTable() folder_names.insert(0, 'Molecule Name') x.field_names = folder_names temp_table = list(energy_dict.values()) temp_table2 = list(energy_dict.keys()) err_cont = 0 for j in temp_table: for i in temp_table2: cont = 0 lista1 = [i] while cont < len(j): try: valor = list(j[cont].values())[0] except IndexError: valor = ' ' lista1.append(valor) cont += 1 try: x.add_row(lista1) except Exception: if err_cont == 0: log.w( 'There is not the same number of files in all the directories. Some results might be missing from the table...' ) err_cont += 1 temp_table2.remove(i) break x.sortby = 'Molecule Name' table_title = ( f'{USER} - {time.strftime("%d-%m-%y")} - Gaussian Energy Calculation Results' ) x.hrules = prettytable.ALL print(x.get_string(title=table_title)) with open(file_name, 'w+') as f: f.write(str(x.get_string(title=table_title))) image_name = f'{time.strftime("%d-%m-%y")}-molecules.svg' subprocess.call([f'obabel -i pdb *.pdb -O {image_name} -d -xC'], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) log.info( f'Data correctly saved as \'{file_name}\' in \'{os.getcwd()}\'') log.info( f'Molecule structure drawn in \'{time.strftime("%d-%m-%y")}-molecules.svg\' in \'{os.getcwd()}\'' ) if args.mail != ' ': full_path = os.getcwd() folder_name = os.path.basename(full_path) email = args.mail subprocess.call([ 'echo "Calculations from folder \'{}\' done, see results attached..." | mail -s "Results - {}" -A "{}" -A "{}" {}' .format(folder_name, folder_name, file_name, image_name, email) ], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) log.info('Results delivery attempted in \'{}\''.format(email)) return str(x)
def gjf_settings(self): config_dict = {} file_list = [file for file in os.listdir() if file.endswith('.pdb')] if len(file_list) == 0: log.e( f'\'.pdb\' files not found in directory \'{os.path.basename(os.getcwd())}\'. Aborting...' ) quit() log.i(f'{len(file_list)} files found. Gathering settings...') while True: try: log.d('How many calculations do you wish to perform?') ncalc = int(input()) log.d( 'Do you want to set number of processors used and memory size available? Default is 8 processors and 1gb of memory. (Y/N) ' ) query = input() nproc, mem = 8, 1 if query.lower() == 'y': log.d('How many procesors do you wish to use? ') nproc = int(input()) log.d('How much memory do you want to use? ') mem = float(input()) break except: log.w('You have not provided a valid input, please try again.') config_dict['ncalc'] = ncalc config_dict['nproc'] = nproc config_dict['mem'] = mem config_dict['chk'] = 'chkfile.chk' for calc_number in range(ncalc): type_list = [ 'DFTmin', 'MP2min', 'MP2max', 'M062Xmin', 'M062Xmax', 'Custom' ] opt_list = [ 'Local minimum', 'Transition State', 'Restart from Checkpoint' ] freq_list = ['Complete', 'no Raman'] method_list = ['B3LYP', 'MP2', 'M062X'] basis_list = ['6-31G(d)', '6-311+G(d,p)'] log.d( f'For calculation number {calc_number}, choose a calculation type preset by its number or type \'custom\' for custom settings...' ) for val, name in enumerate(type_list): print(f'{val:15}) {name}') calc_type = input() log.d( f'For calculation number {calc_number}, do you wish optimization (Y/N):' ) query = input() if query.lower() == 'y': log.d(f'Which optimization type do you wish :') for val, name in enumerate(opt_list): print(f'{val:15}) {name}') opt_type = input() log.d( f'For calculation number {calc_number}, do you wish frequency calculations (Y/N):' ) query = input() if query.lower() == 'y': log.d(f'Which frequency calculation do you wish:') for val, name in enumerate(freq_list): print(f'{val:15}) {name}') opt_type = input() if calc_type.lower() == 'custom': log.d( f'For calculation number {calc_number}, select calculation method:' ) for val, name in enumerate(method_list): print(f'{val:15}) {name}') calc_method = input() log.d( f'For calculation number {calc_number}, select basis set:') for val, name in enumerate(basis_list): print(f'{val:15}) {name}') basis_set = input() log.d( f'For calculation number {calc_number}, do you wish to specify a solvent (Y/N):' ) query = input() if query.lower() == 'y': query_text1 = 'Please input solvent name:' solvent = input(f'{query_text1:15}') pp.pprint(config_dict) return config_dict