def scrap(words, *, reference=True, force=False): """ scrap list of words argument: ([word1, word2, ...], reference=True, force=False) reference (bool): scrap other wordforms of a word force (bool): force redownload word info. Used in debugging """ for word in words: print('scraping ' + GREEN + quote(word) + RESET + '...') if word in DOWNLOADED_WORDS and force is False: print(GREEN + quote(word) + RESET + 'has been ' + YELLOW + 'downloaded' + RESET + '. Skipping to next word') continue elif word in NOT_FOUND_WORDS and force is False: print(GREEN + quote(word) + YELLOW + ' not found' + RESET + '. Skipping to next word') continue else: # valid word. Downloading... print('valid word: ' + GREEN + quote(word) + RESET + '. Ready to ' + BLUE + 'download' + RESET) exitcode, others = extract_data(word) if exitcode == 1: # Word not found time.sleep(0.5) elif exitcode == 2: # Connection error time.sleep(10) elif exitcode == 3: # Word data is corrupted time.sleep(2) else: # success if others and reference is True: print('scrap ' + BLUE + 'reference' + RESET + ' words: ' + GREEN + quote(others) + RESET) scrap(others, reference=False) print(MAGENTA + 'cooldown...' + RESET) # cooldown time: 2s if extract_data.elapsed < 2: time.sleep(2 - extract_data.elapsed)
def newgrouptarget(self,**kwargs): #THIS IS A HACK TO PREVENT UNICODE STRINGS IN PY2.XX FROM GETTING THROUGH #BECAUSE QUOTE() IS USUALLY WHERE THEY CRASH. #AWFULHACK quote(kwargs['groupname']) pages.require("/admin/users.edit") #create the new user auth.addGroup(kwargs['groupname']) #Take the user back to the users page raise cherrypy.HTTPRedirect("/auth/")
def newgrouptarget(self, **kwargs): #THIS IS A HACK TO PREVENT UNICODE STRINGS IN PY2.XX FROM GETTING THROUGH #BECAUSE QUOTE() IS USUALLY WHERE THEY CRASH. #AWFULHACK quote(kwargs['groupname']) pages.require("/admin/users.edit") #create the new user auth.addGroup(kwargs['groupname']) #Take the user back to the users page raise cherrypy.HTTPRedirect("/auth/")
def newusertarget(self,**kwargs): #THIS IS A HACK TO PREVENT UNICODE STRINGS IN PY2.XX FROM GETTING THROUGH #BECAUSE QUOTE() IS USUALLY WHERE THEY CRASH. #AWFULHACK quote(kwargs['username']) pages.require("/admin/users.edit") #create the new user auth.addUser(kwargs['username'],kwargs['password']) #Take the user back to the users page messagebus.postMessage('/system/notifications','New user "'+kwargs['username']+'" added') raise cherrypy.HTTPRedirect("/auth/")
def newusertarget(self, **kwargs): #THIS IS A HACK TO PREVENT UNICODE STRINGS IN PY2.XX FROM GETTING THROUGH #BECAUSE QUOTE() IS USUALLY WHERE THEY CRASH. #AWFULHACK quote(kwargs['username']) pages.require("/admin/users.edit") #create the new user auth.addUser(kwargs['username'], kwargs['password']) #Take the user back to the users page messagebus.postMessage('/system/notifications', 'New user "' + kwargs['username'] + '" added') raise cherrypy.HTTPRedirect("/auth/")
def pytest_configure(config): global PYTHON if config.option.pyexe: PYTHON = config.option.pyexe if config.option.exe: util.set_run_cmd(util.quote(EXE)) _build_exe_if_old() else: util.set_run_cmd('%s %s'%(util.quote(PYTHON), util.quote(SCRIPT))) if config.option.m_verbose: util.TestInfo.verbose = True
def recreate_exception(self): last_dot = self._exception_type_name.rfind('.') assert last_dot > 0, self._exception_type_name module_name = self._exception_type_name[:last_dot] exec 'import %s' % module_name klass = eval(self._exception_type_name) return eval('%s(%s)' % (self._exception_type_name, util.quote(str(self))))
def test_mode_bit(tmp_path, bit, name): path = tmp_path / "file" path.touch() quoted_path = util.quote(str(path)) query = f"<path({quoted_path}).<file.<mode.<{name}" assert util.sq(query) == False path.chmod(stat.S_IMODE(path.stat().st_mode) | bit) assert util.sq(query) == True
def test_canonical(tmp_path, path_info): path = path_info["path"] quoted_path = util.quote(path) canonical_path = (tmp_path / path).resolve() canonical_path.parent.mkdir(parents=True, exist_ok=True) canonical_path.touch(exist_ok=True) result = util.sq(f"<path({quoted_path}).<canonical", cwd=tmp_path) assert result == str(canonical_path)
def test_permissions(tmp_path): path = tmp_path / "file" path.touch() quoted_path = util.quote(str(path)) expected = path.stat().st_mode & (stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) assert util.sq( f"<path({quoted_path}).<file.<mode.<permissions") == expected
def test_absolute(path_info): path = path_info["path"] quoted_path = util.quote(path) absolute_path = path_info["path"] if not path_info["is_absolute"]: absolute_path = f"{pathlib.Path.cwd()}/{path}" result = util.sq(f"<path({quoted_path}).<absolute") assert result == absolute_path
def test_time(tmp_path): quoted_path = util.quote(str(tmp_path)) result = util.sq(f"<path({quoted_path}).<file {{ atime mtime ctime }}") stat = tmp_path.stat() # We can't check for an exact atime value because the value might change # between accesses. Also, try not to underflow if atime is zero. assert result["atime"] + 10 > stat.st_atime assert result["atime"] < stat.st_atime + 10 assert result["mtime"] == math.floor(stat.st_mtime) assert result["ctime"] == math.floor(stat.st_ctime)
def updateuser(self,user,**kwargs): #THIS IS A HACK TO PREVENT UNICODE STRINGS IN PY2.XX FROM GETTING THROUGH #BECAUSE QUOTE() IS USUALLY WHERE THEY CRASH. #AWFULHACK quote(kwargs['username']) #Remove the user from all groups that the checkbox was not checked for for i in auth.Users[user]['groups']: if not ('Group'+i) in kwargs: auth.removeUserFromGroup(user,i) #Add the user to all checked groups for i in kwargs: if i[:5] == 'Group': if kwargs[i] == 'true': auth.addUserToGroup(user,i[5:]) auth.changePassword(user,kwargs['password']) auth.changeUsername(user,kwargs['username']) #Take the user back to the users page raise cherrypy.HTTPRedirect("/auth")
def updateuser(self, user, **kwargs): #THIS IS A HACK TO PREVENT UNICODE STRINGS IN PY2.XX FROM GETTING THROUGH #BECAUSE QUOTE() IS USUALLY WHERE THEY CRASH. #AWFULHACK quote(kwargs['username']) #Remove the user from all groups that the checkbox was not checked for for i in auth.Users[user]['groups']: if not ('Group' + i) in kwargs: auth.removeUserFromGroup(user, i) #Add the user to all checked groups for i in kwargs: if i[:5] == 'Group': if kwargs[i] == 'true': auth.addUserToGroup(user, i[5:]) auth.changePassword(user, kwargs['password']) auth.changeUsername(user, kwargs['username']) #Take the user back to the users page raise cherrypy.HTTPRedirect("/auth")
def test_exists(tmp_path, symlink, follow_symlinks, exists): path = tmp_path / "file" if exists: path.touch() if symlink: link = tmp_path / "link" link.symlink_to(path) path = link follow_symlinks_param = "true" if follow_symlinks else "false" quoted_path = util.quote(str(path)) query = f"<path({quoted_path}).<exists({follow_symlinks_param})" assert util.sq(query) == (exists or (symlink and not follow_symlinks))
def test_path(path_info): for attr in ( "parent", "filename", "stem", "extension", "parts", "is_absolute", ): quoted_path = util.quote(path_info["path"]) result = util.sq(f"<path({quoted_path}).<{attr}") assert result == path_info[attr]
def _send_file(self, filename, is_text, as_name=None): hp_name = self.get_hp_name(filename, as_name, log) f = open(filename, 'r') bytes = f.read() f.close() if is_text: bytes = util.filter_text(util.quote(bytes)) log.i('Sending file {} as {}'.format(filename, hp_name)) self._send_bytes(hp_name, bytes)
def test_file(tmp_path, symlink, follow_symlinks): path = tmp_path / "file" path.touch() expected = path.stat().st_ino if symlink: link = tmp_path / "link" link.symlink_to(path) path = link if not follow_symlinks: expected = path.lstat().st_ino follow_symlinks_param = "true" if follow_symlinks else "false" quoted_path = util.quote(str(path)) query = f"<path({quoted_path}).<file({follow_symlinks_param})" assert util.sq(query) == expected
def test_hard_link_count(tmp_path): path = tmp_path / "file" quoted_path = util.quote(str(path)) path.touch() assert util.sq(f"<path({quoted_path}).<file.<hard_link_count") == 1 # Note: pathlib.Path.link_to() will be deprecated in Python 3.9 due to the # strange argument order (opposite to pathlib.Path.symlink_to()) but we # won't have the replacement, pathlib.Path.hardlink_to() until Python 3.9. # See https://bugs.python.org/issue39291 and # https://bugs.python.org/issue39950 path.link_to(tmp_path / "file2") assert util.sq(f"<path({quoted_path}).<file.<hard_link_count") == 2 path.link_to(tmp_path / "file3") assert util.sq(f"<path({quoted_path}).<file.<hard_link_count") == 3
def notify(self, thing, redditor, link, body, author): """ Notifies the specified redditor that they have been mentioned. """ if self.quiet or util.is_ignored(redditor): return quote = util.quote(body) msg = self.NOTIFICATION_BODY % (thing, link, author, quote) while msg.__len__() > 10000: # Check message size quote_len = quote.__len__() # Get the quote length quote = quote[:quote_len - 2] # Chop off a character msg = self.NOTIFICATION_BODY % (permalink, author, quote) # Reassign the message username = redditor.name print('Sending message to ' + username + '...', end="") self.reddit.send_message(username, 'You have been mentioned in a comment.', msg) print('[DONE]')
def test_shell(tmp_path): quoted_path = util.quote(str(tmp_path)) expected = pwd.getpwuid(tmp_path.stat().st_uid).pw_shell result = util.sq(f"<path({quoted_path}).<file.<user.<shell") assert result == expected
def test_name(tmp_path): quoted_path = util.quote(str(tmp_path)) expected = pwd.getpwuid(tmp_path.stat().st_uid).pw_gecos.split(',')[0] result = util.sq(f"<path({quoted_path}).<file.<user.<name") assert result == expected
def test_uid(tmp_path): quoted_path = util.quote(str(tmp_path)) expected = tmp_path.stat().st_uid result = util.sq(f"<path({quoted_path}).<file.<user.<uid") assert result == expected
def test_user(tmp_path): quoted_path = util.quote(str(tmp_path)) result = util.sq(f"<path({quoted_path}).<file.<user") assert result == tmp_path.stat().st_uid
def test_group(tmp_path): quoted_path = util.quote(str(tmp_path)) result = util.sq(f"<path({quoted_path}).<file.<group") assert result == tmp_path.stat().st_gid
def test_type(tmp_path, file_type): path = tmp_path / "file" create_file_of_type(path, file_type) quoted_path = util.quote(str(path)) query = f"<path({quoted_path}).<file(false).<type" assert util.sq(query) == file_type
import pytest from scheme import translate from scheme.model import * from util import quote, procedure, t_ @pytest.mark.parametrize( "text,expected", [ ('16', 16), ('"hello world"', "hello world"), (r'#\\t', Character("t")), (r'#t', True), (r'#f', False), ("'17", quote(17)), #(",17", 17), #(",@17", 17), #("#(17)", Vector((17,))), ("(quote 17)", quote(17)), (r"'(1 2 3)", quote((1, 2, 3))), (r"""'#(0 (2 2 2 2) "Anna")""", quote(Vector( (0, (2, 2, 2, 2), "Anna")))), (r"'()", quote(())), (r"'nil", quote(Symbol("nil"))), (r"''a", quote(quote(Symbol("a")))), (r"(quote (+ 1 2))", quote((Symbol("+"), 1, 2))), ]) def test_literal(text, expected): result = translate(text).commands[0] assert expected == result
def test_mode(tmp_path): path = tmp_path / "file" path.touch() quoted_path = util.quote(str(path)) assert util.sq(f"<path({quoted_path}).<file.<mode") == stat.S_IMODE( path.stat().st_mode)
def test_inode(tmp_path): path = tmp_path / "file" path.touch() quoted_path = util.quote(str(path)) assert util.sq(f"<path({quoted_path}).<file.<inode") == path.stat().st_ino
def test_block_count(tmp_path): quoted_path = util.quote(str(tmp_path)) result = util.sq(f"<path({quoted_path}).<file.<block_count") assert result == tmp_path.stat().st_blocks
def test_members(tmp_path): quoted_path = util.quote(str(tmp_path)) expected = grp.getgrgid(tmp_path.stat().st_gid).gr_mem result = util.sq(f"<path({quoted_path}).<file.<group.<members") assert result == expected