partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
test
Tigre._get_local_files
Returns a dictionary of all the files under a path.
tigre/tigre.py
def _get_local_files(self, path): """Returns a dictionary of all the files under a path.""" if not path: raise ValueError("No path specified") files = defaultdict(lambda: None) path_len = len(path) + 1 for root, dirs, filenames in os.walk(path): for name in filenames: full_path = join(root, name) files[full_path[path_len:]] = compute_md5(full_path) return files
def _get_local_files(self, path): """Returns a dictionary of all the files under a path.""" if not path: raise ValueError("No path specified") files = defaultdict(lambda: None) path_len = len(path) + 1 for root, dirs, filenames in os.walk(path): for name in filenames: full_path = join(root, name) files[full_path[path_len:]] = compute_md5(full_path) return files
[ "Returns", "a", "dictionary", "of", "all", "the", "files", "under", "a", "path", "." ]
varikin/Tigre
python
https://github.com/varikin/Tigre/blob/6ffac1de52f087cf92cbf368997b336c35a0e3c0/tigre/tigre.py#L64-L74
[ "def", "_get_local_files", "(", "self", ",", "path", ")", ":", "if", "not", "path", ":", "raise", "ValueError", "(", "\"No path specified\"", ")", "files", "=", "defaultdict", "(", "lambda", ":", "None", ")", "path_len", "=", "len", "(", "path", ")", "+", "1", "for", "root", ",", "dirs", ",", "filenames", "in", "os", ".", "walk", "(", "path", ")", ":", "for", "name", "in", "filenames", ":", "full_path", "=", "join", "(", "root", ",", "name", ")", "files", "[", "full_path", "[", "path_len", ":", "]", "]", "=", "compute_md5", "(", "full_path", ")", "return", "files" ]
6ffac1de52f087cf92cbf368997b336c35a0e3c0
test
Tigre.sync_folder
Syncs a local directory with an S3 bucket. Currently does not delete files from S3 that are not in the local directory. path: The path to the directory to sync to S3 bucket: The name of the bucket on S3
tigre/tigre.py
def sync_folder(self, path, bucket): """Syncs a local directory with an S3 bucket. Currently does not delete files from S3 that are not in the local directory. path: The path to the directory to sync to S3 bucket: The name of the bucket on S3 """ bucket = self.conn.get_bucket(bucket) local_files = self._get_local_files(path) s3_files = self._get_s3_files(bucket) for filename, hash in local_files.iteritems(): s3_key = s3_files[filename] if s3_key is None: s3_key = Key(bucket) s3_key.key = filename s3_key.etag = '"!"' if s3_key.etag[1:-1] != hash[0]: s3_key.set_contents_from_filename(join(path, filename), md5=hash)
def sync_folder(self, path, bucket): """Syncs a local directory with an S3 bucket. Currently does not delete files from S3 that are not in the local directory. path: The path to the directory to sync to S3 bucket: The name of the bucket on S3 """ bucket = self.conn.get_bucket(bucket) local_files = self._get_local_files(path) s3_files = self._get_s3_files(bucket) for filename, hash in local_files.iteritems(): s3_key = s3_files[filename] if s3_key is None: s3_key = Key(bucket) s3_key.key = filename s3_key.etag = '"!"' if s3_key.etag[1:-1] != hash[0]: s3_key.set_contents_from_filename(join(path, filename), md5=hash)
[ "Syncs", "a", "local", "directory", "with", "an", "S3", "bucket", ".", "Currently", "does", "not", "delete", "files", "from", "S3", "that", "are", "not", "in", "the", "local", "directory", "." ]
varikin/Tigre
python
https://github.com/varikin/Tigre/blob/6ffac1de52f087cf92cbf368997b336c35a0e3c0/tigre/tigre.py#L87-L106
[ "def", "sync_folder", "(", "self", ",", "path", ",", "bucket", ")", ":", "bucket", "=", "self", ".", "conn", ".", "get_bucket", "(", "bucket", ")", "local_files", "=", "self", ".", "_get_local_files", "(", "path", ")", "s3_files", "=", "self", ".", "_get_s3_files", "(", "bucket", ")", "for", "filename", ",", "hash", "in", "local_files", ".", "iteritems", "(", ")", ":", "s3_key", "=", "s3_files", "[", "filename", "]", "if", "s3_key", "is", "None", ":", "s3_key", "=", "Key", "(", "bucket", ")", "s3_key", ".", "key", "=", "filename", "s3_key", ".", "etag", "=", "'\"!\"'", "if", "s3_key", ".", "etag", "[", "1", ":", "-", "1", "]", "!=", "hash", "[", "0", "]", ":", "s3_key", ".", "set_contents_from_filename", "(", "join", "(", "path", ",", "filename", ")", ",", "md5", "=", "hash", ")" ]
6ffac1de52f087cf92cbf368997b336c35a0e3c0
test
Tigre.sync
Syncs a list of folders to their assicated buckets. folders: A list of 2-tuples in the form (folder, bucket)
tigre/tigre.py
def sync(self, folders): """Syncs a list of folders to their assicated buckets. folders: A list of 2-tuples in the form (folder, bucket) """ if not folders: raise ValueError("No folders to sync given") for folder in folders: self.sync_folder(*folder)
def sync(self, folders): """Syncs a list of folders to their assicated buckets. folders: A list of 2-tuples in the form (folder, bucket) """ if not folders: raise ValueError("No folders to sync given") for folder in folders: self.sync_folder(*folder)
[ "Syncs", "a", "list", "of", "folders", "to", "their", "assicated", "buckets", ".", "folders", ":", "A", "list", "of", "2", "-", "tuples", "in", "the", "form", "(", "folder", "bucket", ")" ]
varikin/Tigre
python
https://github.com/varikin/Tigre/blob/6ffac1de52f087cf92cbf368997b336c35a0e3c0/tigre/tigre.py#L108-L116
[ "def", "sync", "(", "self", ",", "folders", ")", ":", "if", "not", "folders", ":", "raise", "ValueError", "(", "\"No folders to sync given\"", ")", "for", "folder", "in", "folders", ":", "self", ".", "sync_folder", "(", "*", "folder", ")" ]
6ffac1de52f087cf92cbf368997b336c35a0e3c0
test
login_required
Decorator for views that checks that the user is logged in, redirecting to the log-in page if necessary.
ci/views.py
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None): """ Decorator for views that checks that the user is logged in, redirecting to the log-in page if necessary. """ actual_decorator = request_passes_test( lambda r: r.session.get('user_token'), login_url=login_url, redirect_field_name=redirect_field_name ) if function: return actual_decorator(function) return actual_decorator
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None): """ Decorator for views that checks that the user is logged in, redirecting to the log-in page if necessary. """ actual_decorator = request_passes_test( lambda r: r.session.get('user_token'), login_url=login_url, redirect_field_name=redirect_field_name ) if function: return actual_decorator(function) return actual_decorator
[ "Decorator", "for", "views", "that", "checks", "that", "the", "user", "is", "logged", "in", "redirecting", "to", "the", "log", "-", "in", "page", "if", "necessary", "." ]
praekeltfoundation/seed-control-interface
python
https://github.com/praekeltfoundation/seed-control-interface/blob/32ddad88b5bc2f8f4d80b848361899da2e081636/ci/views.py#L111-L124
[ "def", "login_required", "(", "function", "=", "None", ",", "redirect_field_name", "=", "REDIRECT_FIELD_NAME", ",", "login_url", "=", "None", ")", ":", "actual_decorator", "=", "request_passes_test", "(", "lambda", "r", ":", "r", ".", "session", ".", "get", "(", "'user_token'", ")", ",", "login_url", "=", "login_url", ",", "redirect_field_name", "=", "redirect_field_name", ")", "if", "function", ":", "return", "actual_decorator", "(", "function", ")", "return", "actual_decorator" ]
32ddad88b5bc2f8f4d80b848361899da2e081636
test
permission_required
Decorator for views that checks that the user is logged in, redirecting to the log-in page if necessary.
ci/views.py
def permission_required(function=None, permission=None, object_id=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None): """ Decorator for views that checks that the user is logged in, redirecting to the log-in page if necessary. """ actual_decorator = request_passes_test( lambda r: has_permission(r.session.get('user_permissions'), permission, object_id), # noqa login_url=login_url, redirect_field_name=redirect_field_name ) if function: return actual_decorator(function) return actual_decorator
def permission_required(function=None, permission=None, object_id=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None): """ Decorator for views that checks that the user is logged in, redirecting to the log-in page if necessary. """ actual_decorator = request_passes_test( lambda r: has_permission(r.session.get('user_permissions'), permission, object_id), # noqa login_url=login_url, redirect_field_name=redirect_field_name ) if function: return actual_decorator(function) return actual_decorator
[ "Decorator", "for", "views", "that", "checks", "that", "the", "user", "is", "logged", "in", "redirecting", "to", "the", "log", "-", "in", "page", "if", "necessary", "." ]
praekeltfoundation/seed-control-interface
python
https://github.com/praekeltfoundation/seed-control-interface/blob/32ddad88b5bc2f8f4d80b848361899da2e081636/ci/views.py#L136-L150
[ "def", "permission_required", "(", "function", "=", "None", ",", "permission", "=", "None", ",", "object_id", "=", "None", ",", "redirect_field_name", "=", "REDIRECT_FIELD_NAME", ",", "login_url", "=", "None", ")", ":", "actual_decorator", "=", "request_passes_test", "(", "lambda", "r", ":", "has_permission", "(", "r", ".", "session", ".", "get", "(", "'user_permissions'", ")", ",", "permission", ",", "object_id", ")", ",", "# noqa", "login_url", "=", "login_url", ",", "redirect_field_name", "=", "redirect_field_name", ")", "if", "function", ":", "return", "actual_decorator", "(", "function", ")", "return", "actual_decorator" ]
32ddad88b5bc2f8f4d80b848361899da2e081636
test
tokens_required
Ensure the user has the necessary tokens for the specified services
ci/views.py
def tokens_required(service_list): """ Ensure the user has the necessary tokens for the specified services """ def decorator(func): @wraps(func) def inner(request, *args, **kwargs): for service in service_list: if service not in request.session["user_tokens"]: return redirect('denied') return func(request, *args, **kwargs) return inner return decorator
def tokens_required(service_list): """ Ensure the user has the necessary tokens for the specified services """ def decorator(func): @wraps(func) def inner(request, *args, **kwargs): for service in service_list: if service not in request.session["user_tokens"]: return redirect('denied') return func(request, *args, **kwargs) return inner return decorator
[ "Ensure", "the", "user", "has", "the", "necessary", "tokens", "for", "the", "specified", "services" ]
praekeltfoundation/seed-control-interface
python
https://github.com/praekeltfoundation/seed-control-interface/blob/32ddad88b5bc2f8f4d80b848361899da2e081636/ci/views.py#L153-L165
[ "def", "tokens_required", "(", "service_list", ")", ":", "def", "decorator", "(", "func", ")", ":", "@", "wraps", "(", "func", ")", "def", "inner", "(", "request", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "for", "service", "in", "service_list", ":", "if", "service", "not", "in", "request", ".", "session", "[", "\"user_tokens\"", "]", ":", "return", "redirect", "(", "'denied'", ")", "return", "func", "(", "request", ",", "*", "args", ",", "*", "*", "kwargs", ")", "return", "inner", "return", "decorator" ]
32ddad88b5bc2f8f4d80b848361899da2e081636
test
login
Displays the login form and handles the login action.
ci/views.py
def login(request, template_name='ci/login.html', redirect_field_name=REDIRECT_FIELD_NAME, authentication_form=AuthenticationForm): """ Displays the login form and handles the login action. """ redirect_to = request.POST.get(redirect_field_name, request.GET.get(redirect_field_name, '')) if request.method == "POST": form = authentication_form(request, data=request.POST) if form.is_valid(): # Ensure the user-originating redirection url is safe. if not is_safe_url(url=redirect_to, host=request.get_host()): redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL) # Okay, security check complete. Get the user object from auth api. user = form.get_user() request.session['user_token'] = user["token"] request.session['user_email'] = user["email"] request.session['user_permissions'] = user["permissions"] request.session['user_id'] = user["id"] request.session['user_list'] = user["user_list"] if not settings.HIDE_DASHBOARDS: # Set user dashboards because they are slow to change dashboards = ciApi.get_user_dashboards(user["id"]) dashboard_list = list(dashboards['results']) if len(dashboard_list) > 0: request.session['user_dashboards'] = \ dashboard_list[0]["dashboards"] request.session['user_default_dashboard'] = \ dashboard_list[0]["default_dashboard"]["id"] else: request.session['user_dashboards'] = [] request.session['user_default_dashboard'] = None # Get the user access tokens too and format for easy access tokens = ciApi.get_user_service_tokens( params={"user_id": user["id"]}) token_list = list(tokens['results']) user_tokens = {} if len(token_list) > 0: for token in token_list: user_tokens[token["service"]["name"]] = { "token": token["token"], "url": token["service"]["url"] + "/api/v1" } request.session['user_tokens'] = user_tokens return HttpResponseRedirect(redirect_to) else: form = authentication_form(request) current_site = get_current_site(request) context = { 'form': form, redirect_field_name: redirect_to, 'site': current_site, 'site_name': current_site.name, } return TemplateResponse(request, template_name, context)
def login(request, template_name='ci/login.html', redirect_field_name=REDIRECT_FIELD_NAME, authentication_form=AuthenticationForm): """ Displays the login form and handles the login action. """ redirect_to = request.POST.get(redirect_field_name, request.GET.get(redirect_field_name, '')) if request.method == "POST": form = authentication_form(request, data=request.POST) if form.is_valid(): # Ensure the user-originating redirection url is safe. if not is_safe_url(url=redirect_to, host=request.get_host()): redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL) # Okay, security check complete. Get the user object from auth api. user = form.get_user() request.session['user_token'] = user["token"] request.session['user_email'] = user["email"] request.session['user_permissions'] = user["permissions"] request.session['user_id'] = user["id"] request.session['user_list'] = user["user_list"] if not settings.HIDE_DASHBOARDS: # Set user dashboards because they are slow to change dashboards = ciApi.get_user_dashboards(user["id"]) dashboard_list = list(dashboards['results']) if len(dashboard_list) > 0: request.session['user_dashboards'] = \ dashboard_list[0]["dashboards"] request.session['user_default_dashboard'] = \ dashboard_list[0]["default_dashboard"]["id"] else: request.session['user_dashboards'] = [] request.session['user_default_dashboard'] = None # Get the user access tokens too and format for easy access tokens = ciApi.get_user_service_tokens( params={"user_id": user["id"]}) token_list = list(tokens['results']) user_tokens = {} if len(token_list) > 0: for token in token_list: user_tokens[token["service"]["name"]] = { "token": token["token"], "url": token["service"]["url"] + "/api/v1" } request.session['user_tokens'] = user_tokens return HttpResponseRedirect(redirect_to) else: form = authentication_form(request) current_site = get_current_site(request) context = { 'form': form, redirect_field_name: redirect_to, 'site': current_site, 'site_name': current_site.name, } return TemplateResponse(request, template_name, context)
[ "Displays", "the", "login", "form", "and", "handles", "the", "login", "action", "." ]
praekeltfoundation/seed-control-interface
python
https://github.com/praekeltfoundation/seed-control-interface/blob/32ddad88b5bc2f8f4d80b848361899da2e081636/ci/views.py#L168-L232
[ "def", "login", "(", "request", ",", "template_name", "=", "'ci/login.html'", ",", "redirect_field_name", "=", "REDIRECT_FIELD_NAME", ",", "authentication_form", "=", "AuthenticationForm", ")", ":", "redirect_to", "=", "request", ".", "POST", ".", "get", "(", "redirect_field_name", ",", "request", ".", "GET", ".", "get", "(", "redirect_field_name", ",", "''", ")", ")", "if", "request", ".", "method", "==", "\"POST\"", ":", "form", "=", "authentication_form", "(", "request", ",", "data", "=", "request", ".", "POST", ")", "if", "form", ".", "is_valid", "(", ")", ":", "# Ensure the user-originating redirection url is safe.", "if", "not", "is_safe_url", "(", "url", "=", "redirect_to", ",", "host", "=", "request", ".", "get_host", "(", ")", ")", ":", "redirect_to", "=", "resolve_url", "(", "settings", ".", "LOGIN_REDIRECT_URL", ")", "# Okay, security check complete. Get the user object from auth api.", "user", "=", "form", ".", "get_user", "(", ")", "request", ".", "session", "[", "'user_token'", "]", "=", "user", "[", "\"token\"", "]", "request", ".", "session", "[", "'user_email'", "]", "=", "user", "[", "\"email\"", "]", "request", ".", "session", "[", "'user_permissions'", "]", "=", "user", "[", "\"permissions\"", "]", "request", ".", "session", "[", "'user_id'", "]", "=", "user", "[", "\"id\"", "]", "request", ".", "session", "[", "'user_list'", "]", "=", "user", "[", "\"user_list\"", "]", "if", "not", "settings", ".", "HIDE_DASHBOARDS", ":", "# Set user dashboards because they are slow to change", "dashboards", "=", "ciApi", ".", "get_user_dashboards", "(", "user", "[", "\"id\"", "]", ")", "dashboard_list", "=", "list", "(", "dashboards", "[", "'results'", "]", ")", "if", "len", "(", "dashboard_list", ")", ">", "0", ":", "request", ".", "session", "[", "'user_dashboards'", "]", "=", "dashboard_list", "[", "0", "]", "[", "\"dashboards\"", "]", "request", ".", "session", "[", "'user_default_dashboard'", "]", "=", "dashboard_list", "[", "0", "]", "[", "\"default_dashboard\"", "]", "[", "\"id\"", "]", "else", ":", "request", ".", "session", "[", "'user_dashboards'", "]", "=", "[", "]", "request", ".", "session", "[", "'user_default_dashboard'", "]", "=", "None", "# Get the user access tokens too and format for easy access", "tokens", "=", "ciApi", ".", "get_user_service_tokens", "(", "params", "=", "{", "\"user_id\"", ":", "user", "[", "\"id\"", "]", "}", ")", "token_list", "=", "list", "(", "tokens", "[", "'results'", "]", ")", "user_tokens", "=", "{", "}", "if", "len", "(", "token_list", ")", ">", "0", ":", "for", "token", "in", "token_list", ":", "user_tokens", "[", "token", "[", "\"service\"", "]", "[", "\"name\"", "]", "]", "=", "{", "\"token\"", ":", "token", "[", "\"token\"", "]", ",", "\"url\"", ":", "token", "[", "\"service\"", "]", "[", "\"url\"", "]", "+", "\"/api/v1\"", "}", "request", ".", "session", "[", "'user_tokens'", "]", "=", "user_tokens", "return", "HttpResponseRedirect", "(", "redirect_to", ")", "else", ":", "form", "=", "authentication_form", "(", "request", ")", "current_site", "=", "get_current_site", "(", "request", ")", "context", "=", "{", "'form'", ":", "form", ",", "redirect_field_name", ":", "redirect_to", ",", "'site'", ":", "current_site", ",", "'site_name'", ":", "current_site", ".", "name", ",", "}", "return", "TemplateResponse", "(", "request", ",", "template_name", ",", "context", ")" ]
32ddad88b5bc2f8f4d80b848361899da2e081636
test
build
Build CLI dynamically based on the package structure.
yhy/commands/__init__.py
def build(cli, path, package): """Build CLI dynamically based on the package structure. """ for _, name, ispkg in iter_modules(path): module = import_module(f'.{name}', package) if ispkg: build(cli.group(name)(module.group), module.__path__, module.__package__) else: cli.command(name)(module.command)
def build(cli, path, package): """Build CLI dynamically based on the package structure. """ for _, name, ispkg in iter_modules(path): module = import_module(f'.{name}', package) if ispkg: build(cli.group(name)(module.group), module.__path__, module.__package__) else: cli.command(name)(module.command)
[ "Build", "CLI", "dynamically", "based", "on", "the", "package", "structure", "." ]
yeonghoey/yhy
python
https://github.com/yeonghoey/yhy/blob/4bce1482c31aeeccff96c4cfd1803b83932604e7/yhy/commands/__init__.py#L5-L15
[ "def", "build", "(", "cli", ",", "path", ",", "package", ")", ":", "for", "_", ",", "name", ",", "ispkg", "in", "iter_modules", "(", "path", ")", ":", "module", "=", "import_module", "(", "f'.{name}'", ",", "package", ")", "if", "ispkg", ":", "build", "(", "cli", ".", "group", "(", "name", ")", "(", "module", ".", "group", ")", ",", "module", ".", "__path__", ",", "module", ".", "__package__", ")", "else", ":", "cli", ".", "command", "(", "name", ")", "(", "module", ".", "command", ")" ]
4bce1482c31aeeccff96c4cfd1803b83932604e7
test
Fridge.readonly
Return an already closed read-only instance of Fridge. Arguments are the same as for the constructor.
fridge.py
def readonly(cls, *args, **kwargs): """ Return an already closed read-only instance of Fridge. Arguments are the same as for the constructor. """ fridge = cls(*args, **kwargs) fridge.close() return fridge
def readonly(cls, *args, **kwargs): """ Return an already closed read-only instance of Fridge. Arguments are the same as for the constructor. """ fridge = cls(*args, **kwargs) fridge.close() return fridge
[ "Return", "an", "already", "closed", "read", "-", "only", "instance", "of", "Fridge", ".", "Arguments", "are", "the", "same", "as", "for", "the", "constructor", "." ]
swarmer/fridge
python
https://github.com/swarmer/fridge/blob/fcf6481307ce268c40c22f5e0062d01334f6cd95/fridge.py#L30-L37
[ "def", "readonly", "(", "cls", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "fridge", "=", "cls", "(", "*", "args", ",", "*", "*", "kwargs", ")", "fridge", ".", "close", "(", ")", "return", "fridge" ]
fcf6481307ce268c40c22f5e0062d01334f6cd95
test
Fridge.load
Force reloading the data from the file. All data in the in-memory dictionary is discarded. This method is called automatically by the constructor, normally you don't need to call it.
fridge.py
def load(self): """ Force reloading the data from the file. All data in the in-memory dictionary is discarded. This method is called automatically by the constructor, normally you don't need to call it. """ self._check_open() try: data = json.load(self.file, **self.load_args) except ValueError: data = {} if not isinstance(data, dict): raise ValueError('Root JSON type must be dictionary') self.clear() self.update(data)
def load(self): """ Force reloading the data from the file. All data in the in-memory dictionary is discarded. This method is called automatically by the constructor, normally you don't need to call it. """ self._check_open() try: data = json.load(self.file, **self.load_args) except ValueError: data = {} if not isinstance(data, dict): raise ValueError('Root JSON type must be dictionary') self.clear() self.update(data)
[ "Force", "reloading", "the", "data", "from", "the", "file", ".", "All", "data", "in", "the", "in", "-", "memory", "dictionary", "is", "discarded", ".", "This", "method", "is", "called", "automatically", "by", "the", "constructor", "normally", "you", "don", "t", "need", "to", "call", "it", "." ]
swarmer/fridge
python
https://github.com/swarmer/fridge/blob/fcf6481307ce268c40c22f5e0062d01334f6cd95/fridge.py#L90-L105
[ "def", "load", "(", "self", ")", ":", "self", ".", "_check_open", "(", ")", "try", ":", "data", "=", "json", ".", "load", "(", "self", ".", "file", ",", "*", "*", "self", ".", "load_args", ")", "except", "ValueError", ":", "data", "=", "{", "}", "if", "not", "isinstance", "(", "data", ",", "dict", ")", ":", "raise", "ValueError", "(", "'Root JSON type must be dictionary'", ")", "self", ".", "clear", "(", ")", "self", ".", "update", "(", "data", ")" ]
fcf6481307ce268c40c22f5e0062d01334f6cd95
test
Fridge.save
Force saving the dictionary to the file. All data in the file is discarded. This method is called automatically by :meth:`close`.
fridge.py
def save(self): """ Force saving the dictionary to the file. All data in the file is discarded. This method is called automatically by :meth:`close`. """ self._check_open() self.file.truncate(0) self.file.seek(0) json.dump(self, self.file, **self.dump_args)
def save(self): """ Force saving the dictionary to the file. All data in the file is discarded. This method is called automatically by :meth:`close`. """ self._check_open() self.file.truncate(0) self.file.seek(0) json.dump(self, self.file, **self.dump_args)
[ "Force", "saving", "the", "dictionary", "to", "the", "file", ".", "All", "data", "in", "the", "file", "is", "discarded", ".", "This", "method", "is", "called", "automatically", "by", ":", "meth", ":", "close", "." ]
swarmer/fridge
python
https://github.com/swarmer/fridge/blob/fcf6481307ce268c40c22f5e0062d01334f6cd95/fridge.py#L107-L116
[ "def", "save", "(", "self", ")", ":", "self", ".", "_check_open", "(", ")", "self", ".", "file", ".", "truncate", "(", "0", ")", "self", ".", "file", ".", "seek", "(", "0", ")", "json", ".", "dump", "(", "self", ",", "self", ".", "file", ",", "*", "*", "self", ".", "dump_args", ")" ]
fcf6481307ce268c40c22f5e0062d01334f6cd95
test
Fridge.close
Close the fridge. Calls :meth:`save` and closes the underlying file object unless an already open file was passed to the constructor. This method has no effect if the object is already closed. After the fridge is closed :meth:`save` and :meth:`load` will raise an exception but you will still be able to use it as an ordinary dictionary.
fridge.py
def close(self): """ Close the fridge. Calls :meth:`save` and closes the underlying file object unless an already open file was passed to the constructor. This method has no effect if the object is already closed. After the fridge is closed :meth:`save` and :meth:`load` will raise an exception but you will still be able to use it as an ordinary dictionary. """ if not self.closed: self.save() if self.close_file: self.file.close() self.closed = True
def close(self): """ Close the fridge. Calls :meth:`save` and closes the underlying file object unless an already open file was passed to the constructor. This method has no effect if the object is already closed. After the fridge is closed :meth:`save` and :meth:`load` will raise an exception but you will still be able to use it as an ordinary dictionary. """ if not self.closed: self.save() if self.close_file: self.file.close() self.closed = True
[ "Close", "the", "fridge", ".", "Calls", ":", "meth", ":", "save", "and", "closes", "the", "underlying", "file", "object", "unless", "an", "already", "open", "file", "was", "passed", "to", "the", "constructor", ".", "This", "method", "has", "no", "effect", "if", "the", "object", "is", "already", "closed", "." ]
swarmer/fridge
python
https://github.com/swarmer/fridge/blob/fcf6481307ce268c40c22f5e0062d01334f6cd95/fridge.py#L118-L132
[ "def", "close", "(", "self", ")", ":", "if", "not", "self", ".", "closed", ":", "self", ".", "save", "(", ")", "if", "self", ".", "close_file", ":", "self", ".", "file", ".", "close", "(", ")", "self", ".", "closed", "=", "True" ]
fcf6481307ce268c40c22f5e0062d01334f6cd95
test
self_sign_jwks
Create a signed JWT containing a JWKS. The JWT is signed by one of the keys in the JWKS. :param keyjar: A KeyJar instance with at least one private signing key :param iss: issuer of the JWT, should be the owner of the keys :param kid: A key ID if a special key should be used otherwise one is picked at random. :param lifetime: The lifetime of the signed JWT :return: A signed JWT
src/fedoidcmsg/utils.py
def self_sign_jwks(keyjar, iss, kid='', lifetime=3600): """ Create a signed JWT containing a JWKS. The JWT is signed by one of the keys in the JWKS. :param keyjar: A KeyJar instance with at least one private signing key :param iss: issuer of the JWT, should be the owner of the keys :param kid: A key ID if a special key should be used otherwise one is picked at random. :param lifetime: The lifetime of the signed JWT :return: A signed JWT """ # _json = json.dumps(jwks) _jwt = JWT(keyjar, iss=iss, lifetime=lifetime) jwks = keyjar.export_jwks(issuer=iss) return _jwt.pack(payload={'jwks': jwks}, owner=iss, kid=kid)
def self_sign_jwks(keyjar, iss, kid='', lifetime=3600): """ Create a signed JWT containing a JWKS. The JWT is signed by one of the keys in the JWKS. :param keyjar: A KeyJar instance with at least one private signing key :param iss: issuer of the JWT, should be the owner of the keys :param kid: A key ID if a special key should be used otherwise one is picked at random. :param lifetime: The lifetime of the signed JWT :return: A signed JWT """ # _json = json.dumps(jwks) _jwt = JWT(keyjar, iss=iss, lifetime=lifetime) jwks = keyjar.export_jwks(issuer=iss) return _jwt.pack(payload={'jwks': jwks}, owner=iss, kid=kid)
[ "Create", "a", "signed", "JWT", "containing", "a", "JWKS", ".", "The", "JWT", "is", "signed", "by", "one", "of", "the", "keys", "in", "the", "JWKS", "." ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/utils.py#L15-L33
[ "def", "self_sign_jwks", "(", "keyjar", ",", "iss", ",", "kid", "=", "''", ",", "lifetime", "=", "3600", ")", ":", "# _json = json.dumps(jwks)", "_jwt", "=", "JWT", "(", "keyjar", ",", "iss", "=", "iss", ",", "lifetime", "=", "lifetime", ")", "jwks", "=", "keyjar", ".", "export_jwks", "(", "issuer", "=", "iss", ")", "return", "_jwt", ".", "pack", "(", "payload", "=", "{", "'jwks'", ":", "jwks", "}", ",", "owner", "=", "iss", ",", "kid", "=", "kid", ")" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
verify_self_signed_jwks
Verify the signature of a signed JWT containing a JWKS. The JWT is signed by one of the keys in the JWKS. In the JWT the JWKS is stored using this format :: 'jwks': { 'keys': [ ] } :param sjwt: Signed Jason Web Token :return: Dictionary containing 'jwks' (the JWKS) and 'iss' (the issuer of the JWT)
src/fedoidcmsg/utils.py
def verify_self_signed_jwks(sjwt): """ Verify the signature of a signed JWT containing a JWKS. The JWT is signed by one of the keys in the JWKS. In the JWT the JWKS is stored using this format :: 'jwks': { 'keys': [ ] } :param sjwt: Signed Jason Web Token :return: Dictionary containing 'jwks' (the JWKS) and 'iss' (the issuer of the JWT) """ _jws = factory(sjwt) _json = _jws.jwt.part[1] _body = json.loads(as_unicode(_json)) iss = _body['iss'] _jwks = _body['jwks'] _kj = jwks_to_keyjar(_jwks, iss) try: _kid = _jws.jwt.headers['kid'] except KeyError: _keys = _kj.get_signing_key(owner=iss) else: _keys = _kj.get_signing_key(owner=iss, kid=_kid) _ver = _jws.verify_compact(sjwt, _keys) return {'jwks': _ver['jwks'], 'iss': iss}
def verify_self_signed_jwks(sjwt): """ Verify the signature of a signed JWT containing a JWKS. The JWT is signed by one of the keys in the JWKS. In the JWT the JWKS is stored using this format :: 'jwks': { 'keys': [ ] } :param sjwt: Signed Jason Web Token :return: Dictionary containing 'jwks' (the JWKS) and 'iss' (the issuer of the JWT) """ _jws = factory(sjwt) _json = _jws.jwt.part[1] _body = json.loads(as_unicode(_json)) iss = _body['iss'] _jwks = _body['jwks'] _kj = jwks_to_keyjar(_jwks, iss) try: _kid = _jws.jwt.headers['kid'] except KeyError: _keys = _kj.get_signing_key(owner=iss) else: _keys = _kj.get_signing_key(owner=iss, kid=_kid) _ver = _jws.verify_compact(sjwt, _keys) return {'jwks': _ver['jwks'], 'iss': iss}
[ "Verify", "the", "signature", "of", "a", "signed", "JWT", "containing", "a", "JWKS", ".", "The", "JWT", "is", "signed", "by", "one", "of", "the", "keys", "in", "the", "JWKS", ".", "In", "the", "JWT", "the", "JWKS", "is", "stored", "using", "this", "format", "::", "jwks", ":", "{", "keys", ":", "[", "]", "}" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/utils.py#L36-L67
[ "def", "verify_self_signed_jwks", "(", "sjwt", ")", ":", "_jws", "=", "factory", "(", "sjwt", ")", "_json", "=", "_jws", ".", "jwt", ".", "part", "[", "1", "]", "_body", "=", "json", ".", "loads", "(", "as_unicode", "(", "_json", ")", ")", "iss", "=", "_body", "[", "'iss'", "]", "_jwks", "=", "_body", "[", "'jwks'", "]", "_kj", "=", "jwks_to_keyjar", "(", "_jwks", ",", "iss", ")", "try", ":", "_kid", "=", "_jws", ".", "jwt", ".", "headers", "[", "'kid'", "]", "except", "KeyError", ":", "_keys", "=", "_kj", ".", "get_signing_key", "(", "owner", "=", "iss", ")", "else", ":", "_keys", "=", "_kj", ".", "get_signing_key", "(", "owner", "=", "iss", ",", "kid", "=", "_kid", ")", "_ver", "=", "_jws", ".", "verify_compact", "(", "sjwt", ",", "_keys", ")", "return", "{", "'jwks'", ":", "_ver", "[", "'jwks'", "]", ",", "'iss'", ":", "iss", "}" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
request_signed_by_signing_keys
A metadata statement signing request with 'signing_keys' signed by one of the keys in 'signing_keys'. :param keyjar: A KeyJar instance with the private signing key :param msreq: Metadata statement signing request. A MetadataStatement instance. :param iss: Issuer of the signing request also the owner of the signing keys. :return: Signed JWT where the body is the metadata statement
src/fedoidcmsg/utils.py
def request_signed_by_signing_keys(keyjar, msreq, iss, lifetime, kid=''): """ A metadata statement signing request with 'signing_keys' signed by one of the keys in 'signing_keys'. :param keyjar: A KeyJar instance with the private signing key :param msreq: Metadata statement signing request. A MetadataStatement instance. :param iss: Issuer of the signing request also the owner of the signing keys. :return: Signed JWT where the body is the metadata statement """ try: jwks_to_keyjar(msreq['signing_keys'], iss) except KeyError: jwks = keyjar.export_jwks(issuer=iss) msreq['signing_keys'] = jwks _jwt = JWT(keyjar, iss=iss, lifetime=lifetime) return _jwt.pack(owner=iss, kid=kid, payload=msreq.to_dict())
def request_signed_by_signing_keys(keyjar, msreq, iss, lifetime, kid=''): """ A metadata statement signing request with 'signing_keys' signed by one of the keys in 'signing_keys'. :param keyjar: A KeyJar instance with the private signing key :param msreq: Metadata statement signing request. A MetadataStatement instance. :param iss: Issuer of the signing request also the owner of the signing keys. :return: Signed JWT where the body is the metadata statement """ try: jwks_to_keyjar(msreq['signing_keys'], iss) except KeyError: jwks = keyjar.export_jwks(issuer=iss) msreq['signing_keys'] = jwks _jwt = JWT(keyjar, iss=iss, lifetime=lifetime) return _jwt.pack(owner=iss, kid=kid, payload=msreq.to_dict())
[ "A", "metadata", "statement", "signing", "request", "with", "signing_keys", "signed", "by", "one", "of", "the", "keys", "in", "signing_keys", "." ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/utils.py#L70-L91
[ "def", "request_signed_by_signing_keys", "(", "keyjar", ",", "msreq", ",", "iss", ",", "lifetime", ",", "kid", "=", "''", ")", ":", "try", ":", "jwks_to_keyjar", "(", "msreq", "[", "'signing_keys'", "]", ",", "iss", ")", "except", "KeyError", ":", "jwks", "=", "keyjar", ".", "export_jwks", "(", "issuer", "=", "iss", ")", "msreq", "[", "'signing_keys'", "]", "=", "jwks", "_jwt", "=", "JWT", "(", "keyjar", ",", "iss", "=", "iss", ",", "lifetime", "=", "lifetime", ")", "return", "_jwt", ".", "pack", "(", "owner", "=", "iss", ",", "kid", "=", "kid", ",", "payload", "=", "msreq", ".", "to_dict", "(", ")", ")" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
verify_request_signed_by_signing_keys
Verify that a JWT is signed with a key that is inside the JWT. :param smsreq: Signed Metadata Statement signing request :return: Dictionary containing 'ms' (the signed request) and 'iss' (the issuer of the JWT).
src/fedoidcmsg/utils.py
def verify_request_signed_by_signing_keys(smsreq): """ Verify that a JWT is signed with a key that is inside the JWT. :param smsreq: Signed Metadata Statement signing request :return: Dictionary containing 'ms' (the signed request) and 'iss' (the issuer of the JWT). """ _jws = factory(smsreq) _json = _jws.jwt.part[1] _body = json.loads(as_unicode(_json)) iss = _body['iss'] _jwks = _body['signing_keys'] _kj = jwks_to_keyjar(_jwks, iss) try: _kid = _jws.jwt.headers['kid'] except KeyError: _keys = _kj.get_signing_key(owner=iss) else: _keys = _kj.get_signing_key(owner=iss, kid=_kid) _ver = _jws.verify_compact(smsreq, _keys) # remove the JWT specific claims for k in JsonWebToken.c_param.keys(): try: del _ver[k] except KeyError: pass try: del _ver['kid'] except KeyError: pass return {'ms': MetadataStatement(**_ver), 'iss': iss}
def verify_request_signed_by_signing_keys(smsreq): """ Verify that a JWT is signed with a key that is inside the JWT. :param smsreq: Signed Metadata Statement signing request :return: Dictionary containing 'ms' (the signed request) and 'iss' (the issuer of the JWT). """ _jws = factory(smsreq) _json = _jws.jwt.part[1] _body = json.loads(as_unicode(_json)) iss = _body['iss'] _jwks = _body['signing_keys'] _kj = jwks_to_keyjar(_jwks, iss) try: _kid = _jws.jwt.headers['kid'] except KeyError: _keys = _kj.get_signing_key(owner=iss) else: _keys = _kj.get_signing_key(owner=iss, kid=_kid) _ver = _jws.verify_compact(smsreq, _keys) # remove the JWT specific claims for k in JsonWebToken.c_param.keys(): try: del _ver[k] except KeyError: pass try: del _ver['kid'] except KeyError: pass return {'ms': MetadataStatement(**_ver), 'iss': iss}
[ "Verify", "that", "a", "JWT", "is", "signed", "with", "a", "key", "that", "is", "inside", "the", "JWT", ".", ":", "param", "smsreq", ":", "Signed", "Metadata", "Statement", "signing", "request", ":", "return", ":", "Dictionary", "containing", "ms", "(", "the", "signed", "request", ")", "and", "iss", "(", "the", "issuer", "of", "the", "JWT", ")", "." ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/utils.py#L94-L130
[ "def", "verify_request_signed_by_signing_keys", "(", "smsreq", ")", ":", "_jws", "=", "factory", "(", "smsreq", ")", "_json", "=", "_jws", ".", "jwt", ".", "part", "[", "1", "]", "_body", "=", "json", ".", "loads", "(", "as_unicode", "(", "_json", ")", ")", "iss", "=", "_body", "[", "'iss'", "]", "_jwks", "=", "_body", "[", "'signing_keys'", "]", "_kj", "=", "jwks_to_keyjar", "(", "_jwks", ",", "iss", ")", "try", ":", "_kid", "=", "_jws", ".", "jwt", ".", "headers", "[", "'kid'", "]", "except", "KeyError", ":", "_keys", "=", "_kj", ".", "get_signing_key", "(", "owner", "=", "iss", ")", "else", ":", "_keys", "=", "_kj", ".", "get_signing_key", "(", "owner", "=", "iss", ",", "kid", "=", "_kid", ")", "_ver", "=", "_jws", ".", "verify_compact", "(", "smsreq", ",", "_keys", ")", "# remove the JWT specific claims", "for", "k", "in", "JsonWebToken", ".", "c_param", ".", "keys", "(", ")", ":", "try", ":", "del", "_ver", "[", "k", "]", "except", "KeyError", ":", "pass", "try", ":", "del", "_ver", "[", "'kid'", "]", "except", "KeyError", ":", "pass", "return", "{", "'ms'", ":", "MetadataStatement", "(", "*", "*", "_ver", ")", ",", "'iss'", ":", "iss", "}" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
card
A decorator for providing a unittesting function/method with every card in a librarian card library database when it is called.
greencard/greencard.py
def card(func): """ A decorator for providing a unittesting function/method with every card in a librarian card library database when it is called. """ @wraps(func) def wrapped(*args, **kwargs): """Transparent wrapper.""" return func(*args, **kwargs) TESTS.append(wrapped) return wrapped
def card(func): """ A decorator for providing a unittesting function/method with every card in a librarian card library database when it is called. """ @wraps(func) def wrapped(*args, **kwargs): """Transparent wrapper.""" return func(*args, **kwargs) TESTS.append(wrapped) return wrapped
[ "A", "decorator", "for", "providing", "a", "unittesting", "function", "/", "method", "with", "every", "card", "in", "a", "librarian", "card", "library", "database", "when", "it", "is", "called", "." ]
Nekroze/greencard
python
https://github.com/Nekroze/greencard/blob/30fe7eba5742c31b666027e31f33aaa641699857/greencard/greencard.py#L9-L19
[ "def", "card", "(", "func", ")", ":", "@", "wraps", "(", "func", ")", "def", "wrapped", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "\"\"\"Transparent wrapper.\"\"\"", "return", "func", "(", "*", "args", ",", "*", "*", "kwargs", ")", "TESTS", ".", "append", "(", "wrapped", ")", "return", "wrapped" ]
30fe7eba5742c31b666027e31f33aaa641699857
test
library
A decorator for providing a unittest with a library and have it called only once.
greencard/greencard.py
def library(func): """ A decorator for providing a unittest with a library and have it called only once. """ @wraps(func) def wrapped(*args, **kwargs): """Transparent wrapper.""" return func(*args, **kwargs) SINGLES.append(wrapped) return wrapped
def library(func): """ A decorator for providing a unittest with a library and have it called only once. """ @wraps(func) def wrapped(*args, **kwargs): """Transparent wrapper.""" return func(*args, **kwargs) SINGLES.append(wrapped) return wrapped
[ "A", "decorator", "for", "providing", "a", "unittest", "with", "a", "library", "and", "have", "it", "called", "only", "once", "." ]
Nekroze/greencard
python
https://github.com/Nekroze/greencard/blob/30fe7eba5742c31b666027e31f33aaa641699857/greencard/greencard.py#L22-L32
[ "def", "library", "(", "func", ")", ":", "@", "wraps", "(", "func", ")", "def", "wrapped", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "\"\"\"Transparent wrapper.\"\"\"", "return", "func", "(", "*", "args", ",", "*", "*", "kwargs", ")", "SINGLES", ".", "append", "(", "wrapped", ")", "return", "wrapped" ]
30fe7eba5742c31b666027e31f33aaa641699857
test
descovery
Descover and load greencard tests.
greencard/greencard.py
def descovery(testdir): """Descover and load greencard tests.""" from os.path import join, exists, isdir, splitext, basename, sep if not testdir or not exists(testdir) or not isdir(testdir): return None from os import walk import fnmatch import imp for root, _, filenames in walk(testdir): for filename in fnmatch.filter(filenames, '*.py'): path = join(root, filename) modulepath = splitext(root)[0].replace(sep, '.') imp.load_source(modulepath, path)
def descovery(testdir): """Descover and load greencard tests.""" from os.path import join, exists, isdir, splitext, basename, sep if not testdir or not exists(testdir) or not isdir(testdir): return None from os import walk import fnmatch import imp for root, _, filenames in walk(testdir): for filename in fnmatch.filter(filenames, '*.py'): path = join(root, filename) modulepath = splitext(root)[0].replace(sep, '.') imp.load_source(modulepath, path)
[ "Descover", "and", "load", "greencard", "tests", "." ]
Nekroze/greencard
python
https://github.com/Nekroze/greencard/blob/30fe7eba5742c31b666027e31f33aaa641699857/greencard/greencard.py#L35-L49
[ "def", "descovery", "(", "testdir", ")", ":", "from", "os", ".", "path", "import", "join", ",", "exists", ",", "isdir", ",", "splitext", ",", "basename", ",", "sep", "if", "not", "testdir", "or", "not", "exists", "(", "testdir", ")", "or", "not", "isdir", "(", "testdir", ")", ":", "return", "None", "from", "os", "import", "walk", "import", "fnmatch", "import", "imp", "for", "root", ",", "_", ",", "filenames", "in", "walk", "(", "testdir", ")", ":", "for", "filename", "in", "fnmatch", ".", "filter", "(", "filenames", ",", "'*.py'", ")", ":", "path", "=", "join", "(", "root", ",", "filename", ")", "modulepath", "=", "splitext", "(", "root", ")", "[", "0", "]", ".", "replace", "(", "sep", ",", "'.'", ")", "imp", ".", "load_source", "(", "modulepath", ",", "path", ")" ]
30fe7eba5742c31b666027e31f33aaa641699857
test
main
Command line entry point.
greencard/greencard.py
def main(clargs=None): """Command line entry point.""" from argparse import ArgumentParser from librarian.library import Library import sys parser = ArgumentParser( description="A test runner for each card in a librarian library.") parser.add_argument("library", help="Library database") parser.add_argument("-t", "--tests", default="test/", help="Test directory") args = parser.parse_args(clargs) descovery(args.tests) library = Library(args.library) cardcount, passes, failures = execute_tests(library) print(RESULTS.format(len(SINGLES), len(TESTS), cardcount, passes, failures)) sys.exit(failures)
def main(clargs=None): """Command line entry point.""" from argparse import ArgumentParser from librarian.library import Library import sys parser = ArgumentParser( description="A test runner for each card in a librarian library.") parser.add_argument("library", help="Library database") parser.add_argument("-t", "--tests", default="test/", help="Test directory") args = parser.parse_args(clargs) descovery(args.tests) library = Library(args.library) cardcount, passes, failures = execute_tests(library) print(RESULTS.format(len(SINGLES), len(TESTS), cardcount, passes, failures)) sys.exit(failures)
[ "Command", "line", "entry", "point", "." ]
Nekroze/greencard
python
https://github.com/Nekroze/greencard/blob/30fe7eba5742c31b666027e31f33aaa641699857/greencard/greencard.py#L98-L117
[ "def", "main", "(", "clargs", "=", "None", ")", ":", "from", "argparse", "import", "ArgumentParser", "from", "librarian", ".", "library", "import", "Library", "import", "sys", "parser", "=", "ArgumentParser", "(", "description", "=", "\"A test runner for each card in a librarian library.\"", ")", "parser", ".", "add_argument", "(", "\"library\"", ",", "help", "=", "\"Library database\"", ")", "parser", ".", "add_argument", "(", "\"-t\"", ",", "\"--tests\"", ",", "default", "=", "\"test/\"", ",", "help", "=", "\"Test directory\"", ")", "args", "=", "parser", ".", "parse_args", "(", "clargs", ")", "descovery", "(", "args", ".", "tests", ")", "library", "=", "Library", "(", "args", ".", "library", ")", "cardcount", ",", "passes", ",", "failures", "=", "execute_tests", "(", "library", ")", "print", "(", "RESULTS", ".", "format", "(", "len", "(", "SINGLES", ")", ",", "len", "(", "TESTS", ")", ",", "cardcount", ",", "passes", ",", "failures", ")", ")", "sys", ".", "exit", "(", "failures", ")" ]
30fe7eba5742c31b666027e31f33aaa641699857
test
letter_score
Returns the Scrabble score of a letter. Args: letter: a single character string Raises: TypeError if a non-Scrabble character is supplied
nagaram/scrabble.py
def letter_score(letter): """Returns the Scrabble score of a letter. Args: letter: a single character string Raises: TypeError if a non-Scrabble character is supplied """ score_map = { 1: ["a", "e", "i", "o", "u", "l", "n", "r", "s", "t"], 2: ["d", "g"], 3: ["b", "c", "m", "p"], 4: ["f", "h", "v", "w", "y"], 5: ["k"], 8: ["j", "x"], 10: ["q", "z"], } for score, letters in score_map.items(): if letter.lower() in letters: return score else: raise TypeError("Invalid letter: %s", letter)
def letter_score(letter): """Returns the Scrabble score of a letter. Args: letter: a single character string Raises: TypeError if a non-Scrabble character is supplied """ score_map = { 1: ["a", "e", "i", "o", "u", "l", "n", "r", "s", "t"], 2: ["d", "g"], 3: ["b", "c", "m", "p"], 4: ["f", "h", "v", "w", "y"], 5: ["k"], 8: ["j", "x"], 10: ["q", "z"], } for score, letters in score_map.items(): if letter.lower() in letters: return score else: raise TypeError("Invalid letter: %s", letter)
[ "Returns", "the", "Scrabble", "score", "of", "a", "letter", "." ]
a-tal/nagaram
python
https://github.com/a-tal/nagaram/blob/2edcb0ef8cb569ebd1c398be826472b4831d6110/nagaram/scrabble.py#L7-L31
[ "def", "letter_score", "(", "letter", ")", ":", "score_map", "=", "{", "1", ":", "[", "\"a\"", ",", "\"e\"", ",", "\"i\"", ",", "\"o\"", ",", "\"u\"", ",", "\"l\"", ",", "\"n\"", ",", "\"r\"", ",", "\"s\"", ",", "\"t\"", "]", ",", "2", ":", "[", "\"d\"", ",", "\"g\"", "]", ",", "3", ":", "[", "\"b\"", ",", "\"c\"", ",", "\"m\"", ",", "\"p\"", "]", ",", "4", ":", "[", "\"f\"", ",", "\"h\"", ",", "\"v\"", ",", "\"w\"", ",", "\"y\"", "]", ",", "5", ":", "[", "\"k\"", "]", ",", "8", ":", "[", "\"j\"", ",", "\"x\"", "]", ",", "10", ":", "[", "\"q\"", ",", "\"z\"", "]", ",", "}", "for", "score", ",", "letters", "in", "score_map", ".", "items", "(", ")", ":", "if", "letter", ".", "lower", "(", ")", "in", "letters", ":", "return", "score", "else", ":", "raise", "TypeError", "(", "\"Invalid letter: %s\"", ",", "letter", ")" ]
2edcb0ef8cb569ebd1c398be826472b4831d6110
test
word_score
Checks the Scrabble score of a single word. Args: word: a string to check the Scrabble score of input_letters: the letters in our rack questions: integer of the tiles already on the board to build on Returns: an integer Scrabble score amount for the word
nagaram/scrabble.py
def word_score(word, input_letters, questions=0): """Checks the Scrabble score of a single word. Args: word: a string to check the Scrabble score of input_letters: the letters in our rack questions: integer of the tiles already on the board to build on Returns: an integer Scrabble score amount for the word """ score = 0 bingo = 0 filled_by_blanks = [] rack = list(input_letters) # make a copy to speed up find_anagrams() for letter in word: if letter in rack: bingo += 1 score += letter_score(letter) rack.remove(letter) else: filled_by_blanks.append(letter_score(letter)) # we can have both ?'s and _'s in the word. this will apply the ?s to the # highest scrabble score value letters and leave the blanks for low points. for blank_score in sorted(filled_by_blanks, reverse=True): if questions > 0: score += blank_score questions -= 1 # 50 bonus points for using all the tiles in your rack if bingo > 6: score += 50 return score
def word_score(word, input_letters, questions=0): """Checks the Scrabble score of a single word. Args: word: a string to check the Scrabble score of input_letters: the letters in our rack questions: integer of the tiles already on the board to build on Returns: an integer Scrabble score amount for the word """ score = 0 bingo = 0 filled_by_blanks = [] rack = list(input_letters) # make a copy to speed up find_anagrams() for letter in word: if letter in rack: bingo += 1 score += letter_score(letter) rack.remove(letter) else: filled_by_blanks.append(letter_score(letter)) # we can have both ?'s and _'s in the word. this will apply the ?s to the # highest scrabble score value letters and leave the blanks for low points. for blank_score in sorted(filled_by_blanks, reverse=True): if questions > 0: score += blank_score questions -= 1 # 50 bonus points for using all the tiles in your rack if bingo > 6: score += 50 return score
[ "Checks", "the", "Scrabble", "score", "of", "a", "single", "word", "." ]
a-tal/nagaram
python
https://github.com/a-tal/nagaram/blob/2edcb0ef8cb569ebd1c398be826472b4831d6110/nagaram/scrabble.py#L34-L69
[ "def", "word_score", "(", "word", ",", "input_letters", ",", "questions", "=", "0", ")", ":", "score", "=", "0", "bingo", "=", "0", "filled_by_blanks", "=", "[", "]", "rack", "=", "list", "(", "input_letters", ")", "# make a copy to speed up find_anagrams()", "for", "letter", "in", "word", ":", "if", "letter", "in", "rack", ":", "bingo", "+=", "1", "score", "+=", "letter_score", "(", "letter", ")", "rack", ".", "remove", "(", "letter", ")", "else", ":", "filled_by_blanks", ".", "append", "(", "letter_score", "(", "letter", ")", ")", "# we can have both ?'s and _'s in the word. this will apply the ?s to the", "# highest scrabble score value letters and leave the blanks for low points.", "for", "blank_score", "in", "sorted", "(", "filled_by_blanks", ",", "reverse", "=", "True", ")", ":", "if", "questions", ">", "0", ":", "score", "+=", "blank_score", "questions", "-=", "1", "# 50 bonus points for using all the tiles in your rack", "if", "bingo", ">", "6", ":", "score", "+=", "50", "return", "score" ]
2edcb0ef8cb569ebd1c398be826472b4831d6110
test
blank_tiles
Searches a string for blank tile characters ("?" and "_"). Args: input_word: the user supplied string to search through Returns: a tuple of: input_word without blanks integer number of blanks (no points) integer number of questions (points)
nagaram/scrabble.py
def blank_tiles(input_word): """Searches a string for blank tile characters ("?" and "_"). Args: input_word: the user supplied string to search through Returns: a tuple of: input_word without blanks integer number of blanks (no points) integer number of questions (points) """ blanks = 0 questions = 0 input_letters = [] for letter in input_word: if letter == "_": blanks += 1 elif letter == "?": questions += 1 else: input_letters.append(letter) return input_letters, blanks, questions
def blank_tiles(input_word): """Searches a string for blank tile characters ("?" and "_"). Args: input_word: the user supplied string to search through Returns: a tuple of: input_word without blanks integer number of blanks (no points) integer number of questions (points) """ blanks = 0 questions = 0 input_letters = [] for letter in input_word: if letter == "_": blanks += 1 elif letter == "?": questions += 1 else: input_letters.append(letter) return input_letters, blanks, questions
[ "Searches", "a", "string", "for", "blank", "tile", "characters", "(", "?", "and", "_", ")", "." ]
a-tal/nagaram
python
https://github.com/a-tal/nagaram/blob/2edcb0ef8cb569ebd1c398be826472b4831d6110/nagaram/scrabble.py#L72-L95
[ "def", "blank_tiles", "(", "input_word", ")", ":", "blanks", "=", "0", "questions", "=", "0", "input_letters", "=", "[", "]", "for", "letter", "in", "input_word", ":", "if", "letter", "==", "\"_\"", ":", "blanks", "+=", "1", "elif", "letter", "==", "\"?\"", ":", "questions", "+=", "1", "else", ":", "input_letters", ".", "append", "(", "letter", ")", "return", "input_letters", ",", "blanks", ",", "questions" ]
2edcb0ef8cb569ebd1c398be826472b4831d6110
test
word_list
Opens the word list file. Args: sowpods: a boolean to declare using the sowpods list or TWL (default) start: a string of starting characters to find anagrams based on end: a string of ending characters to find anagrams based on Yeilds: a word at a time out of 178691 words for TWL, 267751 for sowpods. Much less if either start or end are used (filtering is applied here)
nagaram/scrabble.py
def word_list(sowpods=False, start="", end=""): """Opens the word list file. Args: sowpods: a boolean to declare using the sowpods list or TWL (default) start: a string of starting characters to find anagrams based on end: a string of ending characters to find anagrams based on Yeilds: a word at a time out of 178691 words for TWL, 267751 for sowpods. Much less if either start or end are used (filtering is applied here) """ location = os.path.join( os.path.dirname(os.path.realpath(__file__)), "wordlists", ) if sowpods: filename = "sowpods.txt" else: filename = "twl.txt" filepath = os.path.join(location, filename) with open(filepath) as wordfile: for word in wordfile.readlines(): word = word.strip() if start and end and word.startswith(start) and word.endswith(end): yield word elif start and word.startswith(start) and not end: yield word elif end and word.endswith(end) and not start: yield word elif not start and not end: yield word
def word_list(sowpods=False, start="", end=""): """Opens the word list file. Args: sowpods: a boolean to declare using the sowpods list or TWL (default) start: a string of starting characters to find anagrams based on end: a string of ending characters to find anagrams based on Yeilds: a word at a time out of 178691 words for TWL, 267751 for sowpods. Much less if either start or end are used (filtering is applied here) """ location = os.path.join( os.path.dirname(os.path.realpath(__file__)), "wordlists", ) if sowpods: filename = "sowpods.txt" else: filename = "twl.txt" filepath = os.path.join(location, filename) with open(filepath) as wordfile: for word in wordfile.readlines(): word = word.strip() if start and end and word.startswith(start) and word.endswith(end): yield word elif start and word.startswith(start) and not end: yield word elif end and word.endswith(end) and not start: yield word elif not start and not end: yield word
[ "Opens", "the", "word", "list", "file", "." ]
a-tal/nagaram
python
https://github.com/a-tal/nagaram/blob/2edcb0ef8cb569ebd1c398be826472b4831d6110/nagaram/scrabble.py#L98-L133
[ "def", "word_list", "(", "sowpods", "=", "False", ",", "start", "=", "\"\"", ",", "end", "=", "\"\"", ")", ":", "location", "=", "os", ".", "path", ".", "join", "(", "os", ".", "path", ".", "dirname", "(", "os", ".", "path", ".", "realpath", "(", "__file__", ")", ")", ",", "\"wordlists\"", ",", ")", "if", "sowpods", ":", "filename", "=", "\"sowpods.txt\"", "else", ":", "filename", "=", "\"twl.txt\"", "filepath", "=", "os", ".", "path", ".", "join", "(", "location", ",", "filename", ")", "with", "open", "(", "filepath", ")", "as", "wordfile", ":", "for", "word", "in", "wordfile", ".", "readlines", "(", ")", ":", "word", "=", "word", ".", "strip", "(", ")", "if", "start", "and", "end", "and", "word", ".", "startswith", "(", "start", ")", "and", "word", ".", "endswith", "(", "end", ")", ":", "yield", "word", "elif", "start", "and", "word", ".", "startswith", "(", "start", ")", "and", "not", "end", ":", "yield", "word", "elif", "end", "and", "word", ".", "endswith", "(", "end", ")", "and", "not", "start", ":", "yield", "word", "elif", "not", "start", "and", "not", "end", ":", "yield", "word" ]
2edcb0ef8cb569ebd1c398be826472b4831d6110
test
valid_scrabble_word
Checks if the input word could be played with a full bag of tiles. Returns: True or false
nagaram/scrabble.py
def valid_scrabble_word(word): """Checks if the input word could be played with a full bag of tiles. Returns: True or false """ letters_in_bag = { "a": 9, "b": 2, "c": 2, "d": 4, "e": 12, "f": 2, "g": 3, "h": 2, "i": 9, "j": 1, "k": 1, "l": 4, "m": 2, "n": 6, "o": 8, "p": 2, "q": 1, "r": 6, "s": 4, "t": 6, "u": 4, "v": 2, "w": 2, "x": 1, "y": 2, "z": 1, "_": 2, } for letter in word: if letter == "?": continue try: letters_in_bag[letter] -= 1 except KeyError: return False if letters_in_bag[letter] < 0: letters_in_bag["_"] -= 1 if letters_in_bag["_"] < 0: return False return True
def valid_scrabble_word(word): """Checks if the input word could be played with a full bag of tiles. Returns: True or false """ letters_in_bag = { "a": 9, "b": 2, "c": 2, "d": 4, "e": 12, "f": 2, "g": 3, "h": 2, "i": 9, "j": 1, "k": 1, "l": 4, "m": 2, "n": 6, "o": 8, "p": 2, "q": 1, "r": 6, "s": 4, "t": 6, "u": 4, "v": 2, "w": 2, "x": 1, "y": 2, "z": 1, "_": 2, } for letter in word: if letter == "?": continue try: letters_in_bag[letter] -= 1 except KeyError: return False if letters_in_bag[letter] < 0: letters_in_bag["_"] -= 1 if letters_in_bag["_"] < 0: return False return True
[ "Checks", "if", "the", "input", "word", "could", "be", "played", "with", "a", "full", "bag", "of", "tiles", "." ]
a-tal/nagaram
python
https://github.com/a-tal/nagaram/blob/2edcb0ef8cb569ebd1c398be826472b4831d6110/nagaram/scrabble.py#L136-L184
[ "def", "valid_scrabble_word", "(", "word", ")", ":", "letters_in_bag", "=", "{", "\"a\"", ":", "9", ",", "\"b\"", ":", "2", ",", "\"c\"", ":", "2", ",", "\"d\"", ":", "4", ",", "\"e\"", ":", "12", ",", "\"f\"", ":", "2", ",", "\"g\"", ":", "3", ",", "\"h\"", ":", "2", ",", "\"i\"", ":", "9", ",", "\"j\"", ":", "1", ",", "\"k\"", ":", "1", ",", "\"l\"", ":", "4", ",", "\"m\"", ":", "2", ",", "\"n\"", ":", "6", ",", "\"o\"", ":", "8", ",", "\"p\"", ":", "2", ",", "\"q\"", ":", "1", ",", "\"r\"", ":", "6", ",", "\"s\"", ":", "4", ",", "\"t\"", ":", "6", ",", "\"u\"", ":", "4", ",", "\"v\"", ":", "2", ",", "\"w\"", ":", "2", ",", "\"x\"", ":", "1", ",", "\"y\"", ":", "2", ",", "\"z\"", ":", "1", ",", "\"_\"", ":", "2", ",", "}", "for", "letter", "in", "word", ":", "if", "letter", "==", "\"?\"", ":", "continue", "try", ":", "letters_in_bag", "[", "letter", "]", "-=", "1", "except", "KeyError", ":", "return", "False", "if", "letters_in_bag", "[", "letter", "]", "<", "0", ":", "letters_in_bag", "[", "\"_\"", "]", "-=", "1", "if", "letters_in_bag", "[", "\"_\"", "]", "<", "0", ":", "return", "False", "return", "True" ]
2edcb0ef8cb569ebd1c398be826472b4831d6110
test
main
docstring for main
howto/howto.py
def main(args): """docstring for main""" try: args.query = ' '.join(args.query).replace('?', '') so = SOSearch(args.query, args.tags) result = so.first_q().best_answer.code if result != None: print result else: print("Sorry I can't find your answer, try adding tags") except NoResult, e: print("Sorry I can't find your answer, try adding tags")
def main(args): """docstring for main""" try: args.query = ' '.join(args.query).replace('?', '') so = SOSearch(args.query, args.tags) result = so.first_q().best_answer.code if result != None: print result else: print("Sorry I can't find your answer, try adding tags") except NoResult, e: print("Sorry I can't find your answer, try adding tags")
[ "docstring", "for", "main" ]
sp4ke/howto
python
https://github.com/sp4ke/howto/blob/2588144a587be5138d45ca9db0ce6ab125fa7d0c/howto/howto.py#L64-L75
[ "def", "main", "(", "args", ")", ":", "try", ":", "args", ".", "query", "=", "' '", ".", "join", "(", "args", ".", "query", ")", ".", "replace", "(", "'?'", ",", "''", ")", "so", "=", "SOSearch", "(", "args", ".", "query", ",", "args", ".", "tags", ")", "result", "=", "so", ".", "first_q", "(", ")", ".", "best_answer", ".", "code", "if", "result", "!=", "None", ":", "print", "result", "else", ":", "print", "(", "\"Sorry I can't find your answer, try adding tags\"", ")", "except", "NoResult", ",", "e", ":", "print", "(", "\"Sorry I can't find your answer, try adding tags\"", ")" ]
2588144a587be5138d45ca9db0ce6ab125fa7d0c
test
cli_run
docstring for argparse
howto/howto.py
def cli_run(): """docstring for argparse""" parser = argparse.ArgumentParser(description='Stupidly simple code answers from StackOverflow') parser.add_argument('query', help="What's the problem ?", type=str, nargs='+') parser.add_argument('-t','--tags', help='semicolon separated tags -> python;lambda') args = parser.parse_args() main(args)
def cli_run(): """docstring for argparse""" parser = argparse.ArgumentParser(description='Stupidly simple code answers from StackOverflow') parser.add_argument('query', help="What's the problem ?", type=str, nargs='+') parser.add_argument('-t','--tags', help='semicolon separated tags -> python;lambda') args = parser.parse_args() main(args)
[ "docstring", "for", "argparse" ]
sp4ke/howto
python
https://github.com/sp4ke/howto/blob/2588144a587be5138d45ca9db0ce6ab125fa7d0c/howto/howto.py#L78-L84
[ "def", "cli_run", "(", ")", ":", "parser", "=", "argparse", ".", "ArgumentParser", "(", "description", "=", "'Stupidly simple code answers from StackOverflow'", ")", "parser", ".", "add_argument", "(", "'query'", ",", "help", "=", "\"What's the problem ?\"", ",", "type", "=", "str", ",", "nargs", "=", "'+'", ")", "parser", ".", "add_argument", "(", "'-t'", ",", "'--tags'", ",", "help", "=", "'semicolon separated tags -> python;lambda'", ")", "args", "=", "parser", ".", "parse_args", "(", ")", "main", "(", "args", ")" ]
2588144a587be5138d45ca9db0ce6ab125fa7d0c
test
JSONAMPDialectReceiver.stringReceived
Handle a JSON AMP dialect request. First, the JSON is parsed. Then, all JSON dialect specific values in the request are turned into the correct objects. Then, finds the correct responder function, calls it, and serializes the result (or error).
txampext/jsondialect.py
def stringReceived(self, string): """Handle a JSON AMP dialect request. First, the JSON is parsed. Then, all JSON dialect specific values in the request are turned into the correct objects. Then, finds the correct responder function, calls it, and serializes the result (or error). """ request = loads(string) identifier = request.pop("_ask") commandName = request.pop("_command") command, responder = self._getCommandAndResponder(commandName) self._parseRequestValues(request, command) d = self._runResponder(responder, request, command, identifier) d.addCallback(self._writeResponse)
def stringReceived(self, string): """Handle a JSON AMP dialect request. First, the JSON is parsed. Then, all JSON dialect specific values in the request are turned into the correct objects. Then, finds the correct responder function, calls it, and serializes the result (or error). """ request = loads(string) identifier = request.pop("_ask") commandName = request.pop("_command") command, responder = self._getCommandAndResponder(commandName) self._parseRequestValues(request, command) d = self._runResponder(responder, request, command, identifier) d.addCallback(self._writeResponse)
[ "Handle", "a", "JSON", "AMP", "dialect", "request", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/txampext/jsondialect.py#L29-L46
[ "def", "stringReceived", "(", "self", ",", "string", ")", ":", "request", "=", "loads", "(", "string", ")", "identifier", "=", "request", ".", "pop", "(", "\"_ask\"", ")", "commandName", "=", "request", ".", "pop", "(", "\"_command\"", ")", "command", ",", "responder", "=", "self", ".", "_getCommandAndResponder", "(", "commandName", ")", "self", ".", "_parseRequestValues", "(", "request", ",", "command", ")", "d", "=", "self", ".", "_runResponder", "(", "responder", ",", "request", ",", "command", ",", "identifier", ")", "d", ".", "addCallback", "(", "self", ".", "_writeResponse", ")" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
JSONAMPDialectReceiver._getCommandAndResponder
Gets the command class and matching responder function for the given command name.
txampext/jsondialect.py
def _getCommandAndResponder(self, commandName): """Gets the command class and matching responder function for the given command name. """ # DISGUSTING IMPLEMENTATION DETAIL EXPLOITING HACK locator = self._remote.boxReceiver.locator responder = locator.locateResponder(commandName) responderFunction = responder.func_closure[1].cell_contents command = responder.func_closure[2].cell_contents return command, responderFunction
def _getCommandAndResponder(self, commandName): """Gets the command class and matching responder function for the given command name. """ # DISGUSTING IMPLEMENTATION DETAIL EXPLOITING HACK locator = self._remote.boxReceiver.locator responder = locator.locateResponder(commandName) responderFunction = responder.func_closure[1].cell_contents command = responder.func_closure[2].cell_contents return command, responderFunction
[ "Gets", "the", "command", "class", "and", "matching", "responder", "function", "for", "the", "given", "command", "name", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/txampext/jsondialect.py#L49-L59
[ "def", "_getCommandAndResponder", "(", "self", ",", "commandName", ")", ":", "# DISGUSTING IMPLEMENTATION DETAIL EXPLOITING HACK", "locator", "=", "self", ".", "_remote", ".", "boxReceiver", ".", "locator", "responder", "=", "locator", ".", "locateResponder", "(", "commandName", ")", "responderFunction", "=", "responder", ".", "func_closure", "[", "1", "]", ".", "cell_contents", "command", "=", "responder", ".", "func_closure", "[", "2", "]", ".", "cell_contents", "return", "command", ",", "responderFunction" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
JSONAMPDialectReceiver._parseRequestValues
Parses all the values in the request that are in a form specific to the JSON AMP dialect.
txampext/jsondialect.py
def _parseRequestValues(self, request, command): """Parses all the values in the request that are in a form specific to the JSON AMP dialect. """ for key, ampType in command.arguments: ampClass = ampType.__class__ if ampClass is exposed.ExposedResponderLocator: request[key] = self._remote continue decoder = _decoders.get(ampClass) if decoder is not None: value = request.get(key) request[key] = decoder(value, self)
def _parseRequestValues(self, request, command): """Parses all the values in the request that are in a form specific to the JSON AMP dialect. """ for key, ampType in command.arguments: ampClass = ampType.__class__ if ampClass is exposed.ExposedResponderLocator: request[key] = self._remote continue decoder = _decoders.get(ampClass) if decoder is not None: value = request.get(key) request[key] = decoder(value, self)
[ "Parses", "all", "the", "values", "in", "the", "request", "that", "are", "in", "a", "form", "specific", "to", "the", "JSON", "AMP", "dialect", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/txampext/jsondialect.py#L62-L77
[ "def", "_parseRequestValues", "(", "self", ",", "request", ",", "command", ")", ":", "for", "key", ",", "ampType", "in", "command", ".", "arguments", ":", "ampClass", "=", "ampType", ".", "__class__", "if", "ampClass", "is", "exposed", ".", "ExposedResponderLocator", ":", "request", "[", "key", "]", "=", "self", ".", "_remote", "continue", "decoder", "=", "_decoders", ".", "get", "(", "ampClass", ")", "if", "decoder", "is", "not", "None", ":", "value", "=", "request", ".", "get", "(", "key", ")", "request", "[", "key", "]", "=", "decoder", "(", "value", ",", "self", ")" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
JSONAMPDialectReceiver._runResponder
Run the responser function. If it succeeds, add the _answer key. If it fails with an error known to the command, serialize the error.
txampext/jsondialect.py
def _runResponder(self, responder, request, command, identifier): """Run the responser function. If it succeeds, add the _answer key. If it fails with an error known to the command, serialize the error. """ d = defer.maybeDeferred(responder, **request) def _addIdentifier(response): """Return the response with an ``_answer`` key. """ response["_answer"] = identifier return response def _serializeFailure(failure): """ If the failure is serializable by this AMP command, serialize it. """ key = failure.trap(*command.allErrors) response = { "_error_code": command.allErrors[key], "_error_description": str(failure.value), "_error": identifier } return response d.addCallbacks(_addIdentifier, _serializeFailure) return d
def _runResponder(self, responder, request, command, identifier): """Run the responser function. If it succeeds, add the _answer key. If it fails with an error known to the command, serialize the error. """ d = defer.maybeDeferred(responder, **request) def _addIdentifier(response): """Return the response with an ``_answer`` key. """ response["_answer"] = identifier return response def _serializeFailure(failure): """ If the failure is serializable by this AMP command, serialize it. """ key = failure.trap(*command.allErrors) response = { "_error_code": command.allErrors[key], "_error_description": str(failure.value), "_error": identifier } return response d.addCallbacks(_addIdentifier, _serializeFailure) return d
[ "Run", "the", "responser", "function", ".", "If", "it", "succeeds", "add", "the", "_answer", "key", ".", "If", "it", "fails", "with", "an", "error", "known", "to", "the", "command", "serialize", "the", "error", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/txampext/jsondialect.py#L80-L108
[ "def", "_runResponder", "(", "self", ",", "responder", ",", "request", ",", "command", ",", "identifier", ")", ":", "d", "=", "defer", ".", "maybeDeferred", "(", "responder", ",", "*", "*", "request", ")", "def", "_addIdentifier", "(", "response", ")", ":", "\"\"\"Return the response with an ``_answer`` key.\n\n \"\"\"", "response", "[", "\"_answer\"", "]", "=", "identifier", "return", "response", "def", "_serializeFailure", "(", "failure", ")", ":", "\"\"\"\n If the failure is serializable by this AMP command, serialize it.\n \"\"\"", "key", "=", "failure", ".", "trap", "(", "*", "command", ".", "allErrors", ")", "response", "=", "{", "\"_error_code\"", ":", "command", ".", "allErrors", "[", "key", "]", ",", "\"_error_description\"", ":", "str", "(", "failure", ".", "value", ")", ",", "\"_error\"", ":", "identifier", "}", "return", "response", "d", ".", "addCallbacks", "(", "_addIdentifier", ",", "_serializeFailure", ")", "return", "d" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
JSONAMPDialectReceiver._writeResponse
Serializes the response to JSON, and writes it to the transport.
txampext/jsondialect.py
def _writeResponse(self, response): """ Serializes the response to JSON, and writes it to the transport. """ encoded = dumps(response, default=_default) self.transport.write(encoded)
def _writeResponse(self, response): """ Serializes the response to JSON, and writes it to the transport. """ encoded = dumps(response, default=_default) self.transport.write(encoded)
[ "Serializes", "the", "response", "to", "JSON", "and", "writes", "it", "to", "the", "transport", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/txampext/jsondialect.py#L111-L116
[ "def", "_writeResponse", "(", "self", ",", "response", ")", ":", "encoded", "=", "dumps", "(", "response", ",", "default", "=", "_default", ")", "self", ".", "transport", ".", "write", "(", "encoded", ")" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
JSONAMPDialectReceiver.connectionLost
Tells the box receiver to stop receiving boxes.
txampext/jsondialect.py
def connectionLost(self, reason): """ Tells the box receiver to stop receiving boxes. """ self._remote.boxReceiver.stopReceivingBoxes(reason) return basic.NetstringReceiver.connectionLost(self, reason)
def connectionLost(self, reason): """ Tells the box receiver to stop receiving boxes. """ self._remote.boxReceiver.stopReceivingBoxes(reason) return basic.NetstringReceiver.connectionLost(self, reason)
[ "Tells", "the", "box", "receiver", "to", "stop", "receiving", "boxes", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/txampext/jsondialect.py#L119-L124
[ "def", "connectionLost", "(", "self", ",", "reason", ")", ":", "self", ".", "_remote", ".", "boxReceiver", ".", "stopReceivingBoxes", "(", "reason", ")", "return", "basic", ".", "NetstringReceiver", ".", "connectionLost", "(", "self", ",", "reason", ")" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
JSONAMPDialectFactory.buildProtocol
Builds a bridge and associates it with an AMP protocol instance.
txampext/jsondialect.py
def buildProtocol(self, addr): """ Builds a bridge and associates it with an AMP protocol instance. """ proto = self._factory.buildProtocol(addr) return JSONAMPDialectReceiver(proto)
def buildProtocol(self, addr): """ Builds a bridge and associates it with an AMP protocol instance. """ proto = self._factory.buildProtocol(addr) return JSONAMPDialectReceiver(proto)
[ "Builds", "a", "bridge", "and", "associates", "it", "with", "an", "AMP", "protocol", "instance", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/txampext/jsondialect.py#L159-L164
[ "def", "buildProtocol", "(", "self", ",", "addr", ")", ":", "proto", "=", "self", ".", "_factory", ".", "buildProtocol", "(", "addr", ")", "return", "JSONAMPDialectReceiver", "(", "proto", ")" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
get_bundle
Read a signed JWKS bundle from disc, verify the signature and instantiate a JWKSBundle instance with the information from the file. :param iss: :param ver_keys: :param bundle_file: :return:
src/fedoidcmsg/bundle.py
def get_bundle(iss, ver_keys, bundle_file): """ Read a signed JWKS bundle from disc, verify the signature and instantiate a JWKSBundle instance with the information from the file. :param iss: :param ver_keys: :param bundle_file: :return: """ fp = open(bundle_file, 'r') signed_bundle = fp.read() fp.close() return JWKSBundle(iss, None).upload_signed_bundle(signed_bundle, ver_keys)
def get_bundle(iss, ver_keys, bundle_file): """ Read a signed JWKS bundle from disc, verify the signature and instantiate a JWKSBundle instance with the information from the file. :param iss: :param ver_keys: :param bundle_file: :return: """ fp = open(bundle_file, 'r') signed_bundle = fp.read() fp.close() return JWKSBundle(iss, None).upload_signed_bundle(signed_bundle, ver_keys)
[ "Read", "a", "signed", "JWKS", "bundle", "from", "disc", "verify", "the", "signature", "and", "instantiate", "a", "JWKSBundle", "instance", "with", "the", "information", "from", "the", "file", ".", ":", "param", "iss", ":", ":", "param", "ver_keys", ":", ":", "param", "bundle_file", ":", ":", "return", ":" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/bundle.py#L212-L225
[ "def", "get_bundle", "(", "iss", ",", "ver_keys", ",", "bundle_file", ")", ":", "fp", "=", "open", "(", "bundle_file", ",", "'r'", ")", "signed_bundle", "=", "fp", ".", "read", "(", ")", "fp", ".", "close", "(", ")", "return", "JWKSBundle", "(", "iss", ",", "None", ")", ".", "upload_signed_bundle", "(", "signed_bundle", ",", "ver_keys", ")" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
get_signing_keys
If the *key_file* file exists then read the keys from there, otherwise create the keys and store them a file with the name *key_file*. :param eid: The ID of the entity that the keys belongs to :param keydef: What keys to create :param key_file: A file name :return: A :py:class:`oidcmsg.key_jar.KeyJar` instance
src/fedoidcmsg/bundle.py
def get_signing_keys(eid, keydef, key_file): """ If the *key_file* file exists then read the keys from there, otherwise create the keys and store them a file with the name *key_file*. :param eid: The ID of the entity that the keys belongs to :param keydef: What keys to create :param key_file: A file name :return: A :py:class:`oidcmsg.key_jar.KeyJar` instance """ if os.path.isfile(key_file): kj = KeyJar() kj.import_jwks(json.loads(open(key_file, 'r').read()), eid) else: kj = build_keyjar(keydef)[1] # make it know under both names fp = open(key_file, 'w') fp.write(json.dumps(kj.export_jwks())) fp.close() kj.issuer_keys[eid] = kj.issuer_keys[''] return kj
def get_signing_keys(eid, keydef, key_file): """ If the *key_file* file exists then read the keys from there, otherwise create the keys and store them a file with the name *key_file*. :param eid: The ID of the entity that the keys belongs to :param keydef: What keys to create :param key_file: A file name :return: A :py:class:`oidcmsg.key_jar.KeyJar` instance """ if os.path.isfile(key_file): kj = KeyJar() kj.import_jwks(json.loads(open(key_file, 'r').read()), eid) else: kj = build_keyjar(keydef)[1] # make it know under both names fp = open(key_file, 'w') fp.write(json.dumps(kj.export_jwks())) fp.close() kj.issuer_keys[eid] = kj.issuer_keys[''] return kj
[ "If", "the", "*", "key_file", "*", "file", "exists", "then", "read", "the", "keys", "from", "there", "otherwise", "create", "the", "keys", "and", "store", "them", "a", "file", "with", "the", "name", "*", "key_file", "*", "." ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/bundle.py#L228-L249
[ "def", "get_signing_keys", "(", "eid", ",", "keydef", ",", "key_file", ")", ":", "if", "os", ".", "path", ".", "isfile", "(", "key_file", ")", ":", "kj", "=", "KeyJar", "(", ")", "kj", ".", "import_jwks", "(", "json", ".", "loads", "(", "open", "(", "key_file", ",", "'r'", ")", ".", "read", "(", ")", ")", ",", "eid", ")", "else", ":", "kj", "=", "build_keyjar", "(", "keydef", ")", "[", "1", "]", "# make it know under both names", "fp", "=", "open", "(", "key_file", ",", "'w'", ")", "fp", ".", "write", "(", "json", ".", "dumps", "(", "kj", ".", "export_jwks", "(", ")", ")", ")", "fp", ".", "close", "(", ")", "kj", ".", "issuer_keys", "[", "eid", "]", "=", "kj", ".", "issuer_keys", "[", "''", "]", "return", "kj" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
jwks_to_keyjar
Convert a JWKS to a KeyJar instance. :param jwks: String representation of a JWKS :return: A :py:class:`oidcmsg.key_jar.KeyJar` instance
src/fedoidcmsg/bundle.py
def jwks_to_keyjar(jwks, iss=''): """ Convert a JWKS to a KeyJar instance. :param jwks: String representation of a JWKS :return: A :py:class:`oidcmsg.key_jar.KeyJar` instance """ if not isinstance(jwks, dict): try: jwks = json.loads(jwks) except json.JSONDecodeError: raise ValueError('No proper JSON') kj = KeyJar() kj.import_jwks(jwks, issuer=iss) return kj
def jwks_to_keyjar(jwks, iss=''): """ Convert a JWKS to a KeyJar instance. :param jwks: String representation of a JWKS :return: A :py:class:`oidcmsg.key_jar.KeyJar` instance """ if not isinstance(jwks, dict): try: jwks = json.loads(jwks) except json.JSONDecodeError: raise ValueError('No proper JSON') kj = KeyJar() kj.import_jwks(jwks, issuer=iss) return kj
[ "Convert", "a", "JWKS", "to", "a", "KeyJar", "instance", "." ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/bundle.py#L252-L267
[ "def", "jwks_to_keyjar", "(", "jwks", ",", "iss", "=", "''", ")", ":", "if", "not", "isinstance", "(", "jwks", ",", "dict", ")", ":", "try", ":", "jwks", "=", "json", ".", "loads", "(", "jwks", ")", "except", "json", ".", "JSONDecodeError", ":", "raise", "ValueError", "(", "'No proper JSON'", ")", "kj", "=", "KeyJar", "(", ")", "kj", ".", "import_jwks", "(", "jwks", ",", "issuer", "=", "iss", ")", "return", "kj" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
JWKSBundle.create_signed_bundle
Create a signed JWT containing a dictionary with Issuer IDs as keys and JWKSs as values. If iss_list is empty then all available issuers are included. :param sign_alg: Which algorithm to use when signing the JWT :param iss_list: A list of issuer IDs who's keys should be included in the signed bundle. :return: A signed JWT
src/fedoidcmsg/bundle.py
def create_signed_bundle(self, sign_alg='RS256', iss_list=None): """ Create a signed JWT containing a dictionary with Issuer IDs as keys and JWKSs as values. If iss_list is empty then all available issuers are included. :param sign_alg: Which algorithm to use when signing the JWT :param iss_list: A list of issuer IDs who's keys should be included in the signed bundle. :return: A signed JWT """ data = self.dict(iss_list) _jwt = JWT(self.sign_keys, iss=self.iss, sign_alg=sign_alg) return _jwt.pack({'bundle':data})
def create_signed_bundle(self, sign_alg='RS256', iss_list=None): """ Create a signed JWT containing a dictionary with Issuer IDs as keys and JWKSs as values. If iss_list is empty then all available issuers are included. :param sign_alg: Which algorithm to use when signing the JWT :param iss_list: A list of issuer IDs who's keys should be included in the signed bundle. :return: A signed JWT """ data = self.dict(iss_list) _jwt = JWT(self.sign_keys, iss=self.iss, sign_alg=sign_alg) return _jwt.pack({'bundle':data})
[ "Create", "a", "signed", "JWT", "containing", "a", "dictionary", "with", "Issuer", "IDs", "as", "keys", "and", "JWKSs", "as", "values", ".", "If", "iss_list", "is", "empty", "then", "all", "available", "issuers", "are", "included", ".", ":", "param", "sign_alg", ":", "Which", "algorithm", "to", "use", "when", "signing", "the", "JWT", ":", "param", "iss_list", ":", "A", "list", "of", "issuer", "IDs", "who", "s", "keys", "should", "be", "included", "in", "the", "signed", "bundle", ".", ":", "return", ":", "A", "signed", "JWT" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/bundle.py#L92-L105
[ "def", "create_signed_bundle", "(", "self", ",", "sign_alg", "=", "'RS256'", ",", "iss_list", "=", "None", ")", ":", "data", "=", "self", ".", "dict", "(", "iss_list", ")", "_jwt", "=", "JWT", "(", "self", ".", "sign_keys", ",", "iss", "=", "self", ".", "iss", ",", "sign_alg", "=", "sign_alg", ")", "return", "_jwt", ".", "pack", "(", "{", "'bundle'", ":", "data", "}", ")" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
JWKSBundle.loads
Upload a bundle from an unsigned JSON document :param jstr: A bundle as a dictionary or a JSON document
src/fedoidcmsg/bundle.py
def loads(self, jstr): """ Upload a bundle from an unsigned JSON document :param jstr: A bundle as a dictionary or a JSON document """ if isinstance(jstr, dict): _info = jstr else: _info = json.loads(jstr) for iss, jwks in _info.items(): kj = KeyJar() if isinstance(jwks, dict): kj.import_jwks(jwks, issuer=iss) else: kj.import_jwks_as_json(jwks, issuer=iss) self.bundle[iss] = kj return self
def loads(self, jstr): """ Upload a bundle from an unsigned JSON document :param jstr: A bundle as a dictionary or a JSON document """ if isinstance(jstr, dict): _info = jstr else: _info = json.loads(jstr) for iss, jwks in _info.items(): kj = KeyJar() if isinstance(jwks, dict): kj.import_jwks(jwks, issuer=iss) else: kj.import_jwks_as_json(jwks, issuer=iss) self.bundle[iss] = kj return self
[ "Upload", "a", "bundle", "from", "an", "unsigned", "JSON", "document" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/bundle.py#L107-L125
[ "def", "loads", "(", "self", ",", "jstr", ")", ":", "if", "isinstance", "(", "jstr", ",", "dict", ")", ":", "_info", "=", "jstr", "else", ":", "_info", "=", "json", ".", "loads", "(", "jstr", ")", "for", "iss", ",", "jwks", "in", "_info", ".", "items", "(", ")", ":", "kj", "=", "KeyJar", "(", ")", "if", "isinstance", "(", "jwks", ",", "dict", ")", ":", "kj", ".", "import_jwks", "(", "jwks", ",", "issuer", "=", "iss", ")", "else", ":", "kj", ".", "import_jwks_as_json", "(", "jwks", ",", "issuer", "=", "iss", ")", "self", ".", "bundle", "[", "iss", "]", "=", "kj", "return", "self" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
JWKSBundle.dict
Return the bundle of keys as a dictionary with the issuer IDs as the keys and the key sets represented as JWKS instances. :param iss_list: List of Issuer IDs that should be part of the output :rtype: Dictionary
src/fedoidcmsg/bundle.py
def dict(self, iss_list=None): """ Return the bundle of keys as a dictionary with the issuer IDs as the keys and the key sets represented as JWKS instances. :param iss_list: List of Issuer IDs that should be part of the output :rtype: Dictionary """ _int = {} for iss, kj in self.bundle.items(): if iss_list is None or iss in iss_list: try: _int[iss] = kj.export_jwks_as_json(issuer=iss) except KeyError: _int[iss] = kj.export_jwks_as_json() return _int
def dict(self, iss_list=None): """ Return the bundle of keys as a dictionary with the issuer IDs as the keys and the key sets represented as JWKS instances. :param iss_list: List of Issuer IDs that should be part of the output :rtype: Dictionary """ _int = {} for iss, kj in self.bundle.items(): if iss_list is None or iss in iss_list: try: _int[iss] = kj.export_jwks_as_json(issuer=iss) except KeyError: _int[iss] = kj.export_jwks_as_json() return _int
[ "Return", "the", "bundle", "of", "keys", "as", "a", "dictionary", "with", "the", "issuer", "IDs", "as", "the", "keys", "and", "the", "key", "sets", "represented", "as", "JWKS", "instances", ".", ":", "param", "iss_list", ":", "List", "of", "Issuer", "IDs", "that", "should", "be", "part", "of", "the", "output", ":", "rtype", ":", "Dictionary" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/bundle.py#L151-L167
[ "def", "dict", "(", "self", ",", "iss_list", "=", "None", ")", ":", "_int", "=", "{", "}", "for", "iss", ",", "kj", "in", "self", ".", "bundle", ".", "items", "(", ")", ":", "if", "iss_list", "is", "None", "or", "iss", "in", "iss_list", ":", "try", ":", "_int", "[", "iss", "]", "=", "kj", ".", "export_jwks_as_json", "(", "issuer", "=", "iss", ")", "except", "KeyError", ":", "_int", "[", "iss", "]", "=", "kj", ".", "export_jwks_as_json", "(", ")", "return", "_int" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
JWKSBundle.upload_signed_bundle
Input is a signed JWT with a JSON document representing the key bundle as body. This method verifies the signature and the updates the instance bundle with whatever was in the received package. Note, that as with dictionary update if an Issuer ID already exists in the instance bundle that will be overwritten with the new information. :param sign_bundle: A signed JWT :param ver_keys: Keys that can be used to verify the JWT signature.
src/fedoidcmsg/bundle.py
def upload_signed_bundle(self, sign_bundle, ver_keys): """ Input is a signed JWT with a JSON document representing the key bundle as body. This method verifies the signature and the updates the instance bundle with whatever was in the received package. Note, that as with dictionary update if an Issuer ID already exists in the instance bundle that will be overwritten with the new information. :param sign_bundle: A signed JWT :param ver_keys: Keys that can be used to verify the JWT signature. """ jwt = verify_signed_bundle(sign_bundle, ver_keys) self.loads(jwt['bundle'])
def upload_signed_bundle(self, sign_bundle, ver_keys): """ Input is a signed JWT with a JSON document representing the key bundle as body. This method verifies the signature and the updates the instance bundle with whatever was in the received package. Note, that as with dictionary update if an Issuer ID already exists in the instance bundle that will be overwritten with the new information. :param sign_bundle: A signed JWT :param ver_keys: Keys that can be used to verify the JWT signature. """ jwt = verify_signed_bundle(sign_bundle, ver_keys) self.loads(jwt['bundle'])
[ "Input", "is", "a", "signed", "JWT", "with", "a", "JSON", "document", "representing", "the", "key", "bundle", "as", "body", ".", "This", "method", "verifies", "the", "signature", "and", "the", "updates", "the", "instance", "bundle", "with", "whatever", "was", "in", "the", "received", "package", ".", "Note", "that", "as", "with", "dictionary", "update", "if", "an", "Issuer", "ID", "already", "exists", "in", "the", "instance", "bundle", "that", "will", "be", "overwritten", "with", "the", "new", "information", ".", ":", "param", "sign_bundle", ":", "A", "signed", "JWT", ":", "param", "ver_keys", ":", "Keys", "that", "can", "be", "used", "to", "verify", "the", "JWT", "signature", "." ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/bundle.py#L169-L181
[ "def", "upload_signed_bundle", "(", "self", ",", "sign_bundle", ",", "ver_keys", ")", ":", "jwt", "=", "verify_signed_bundle", "(", "sign_bundle", ",", "ver_keys", ")", "self", ".", "loads", "(", "jwt", "[", "'bundle'", "]", ")" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
JWKSBundle.as_keyjar
Convert a key bundle into a KeyJar instance. :return: An :py:class:`oidcmsg.key_jar.KeyJar` instance
src/fedoidcmsg/bundle.py
def as_keyjar(self): """ Convert a key bundle into a KeyJar instance. :return: An :py:class:`oidcmsg.key_jar.KeyJar` instance """ kj = KeyJar() for iss, k in self.bundle.items(): try: kj.issuer_keys[iss] = k.issuer_keys[iss] except KeyError: kj.issuer_keys[iss] = k.issuer_keys[''] return kj
def as_keyjar(self): """ Convert a key bundle into a KeyJar instance. :return: An :py:class:`oidcmsg.key_jar.KeyJar` instance """ kj = KeyJar() for iss, k in self.bundle.items(): try: kj.issuer_keys[iss] = k.issuer_keys[iss] except KeyError: kj.issuer_keys[iss] = k.issuer_keys[''] return kj
[ "Convert", "a", "key", "bundle", "into", "a", "KeyJar", "instance", ".", ":", "return", ":", "An", ":", "py", ":", "class", ":", "oidcmsg", ".", "key_jar", ".", "KeyJar", "instance" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/bundle.py#L183-L195
[ "def", "as_keyjar", "(", "self", ")", ":", "kj", "=", "KeyJar", "(", ")", "for", "iss", ",", "k", "in", "self", ".", "bundle", ".", "items", "(", ")", ":", "try", ":", "kj", ".", "issuer_keys", "[", "iss", "]", "=", "k", ".", "issuer_keys", "[", "iss", "]", "except", "KeyError", ":", "kj", ".", "issuer_keys", "[", "iss", "]", "=", "k", ".", "issuer_keys", "[", "''", "]", "return", "kj" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
make_shortcut
return a function which runs the given cmd make_shortcut('ls') returns a function which executes envoy.run('ls ' + arguments)
pub/shortcuts/shortcuts.py
def make_shortcut(cmd): """return a function which runs the given cmd make_shortcut('ls') returns a function which executes envoy.run('ls ' + arguments)""" def _(cmd_arguments, *args, **kwargs): return run("%s %s" % (cmd, cmd_arguments), *args, **kwargs) return _
def make_shortcut(cmd): """return a function which runs the given cmd make_shortcut('ls') returns a function which executes envoy.run('ls ' + arguments)""" def _(cmd_arguments, *args, **kwargs): return run("%s %s" % (cmd, cmd_arguments), *args, **kwargs) return _
[ "return", "a", "function", "which", "runs", "the", "given", "cmd", "make_shortcut", "(", "ls", ")", "returns", "a", "function", "which", "executes", "envoy", ".", "run", "(", "ls", "+", "arguments", ")" ]
llimllib/pub
python
https://github.com/llimllib/pub/blob/bd8472f04800612c50cac0682a4aee0a441b1d56/pub/shortcuts/shortcuts.py#L21-L28
[ "def", "make_shortcut", "(", "cmd", ")", ":", "def", "_", "(", "cmd_arguments", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "return", "run", "(", "\"%s %s\"", "%", "(", "cmd", ",", "cmd_arguments", ")", ",", "*", "args", ",", "*", "*", "kwargs", ")", "return", "_" ]
bd8472f04800612c50cac0682a4aee0a441b1d56
test
nova_process
This function deal with the nova notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return:
ternya/process.py
def nova_process(body, message): """ This function deal with the nova notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = nova_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in nova_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = nova_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
def nova_process(body, message): """ This function deal with the nova notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = nova_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in nova_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = nova_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
[ "This", "function", "deal", "with", "the", "nova", "notification", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/process.py#L27-L54
[ "def", "nova_process", "(", "body", ",", "message", ")", ":", "event_type", "=", "body", "[", "'event_type'", "]", "process", "=", "nova_customer_process", ".", "get", "(", "event_type", ")", "if", "process", "is", "not", "None", ":", "process", "(", "body", ",", "message", ")", "else", ":", "matched", "=", "False", "process_wildcard", "=", "None", "for", "pattern", "in", "nova_customer_process_wildcard", ".", "keys", "(", ")", ":", "if", "pattern", ".", "match", "(", "event_type", ")", ":", "process_wildcard", "=", "nova_customer_process_wildcard", ".", "get", "(", "pattern", ")", "matched", "=", "True", "break", "if", "matched", ":", "process_wildcard", "(", "body", ",", "message", ")", "else", ":", "default_process", "(", "body", ",", "message", ")", "message", ".", "ack", "(", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
cinder_process
This function deal with the cinder notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return:
ternya/process.py
def cinder_process(body, message): """ This function deal with the cinder notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = cinder_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in cinder_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = cinder_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
def cinder_process(body, message): """ This function deal with the cinder notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = cinder_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in cinder_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = cinder_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
[ "This", "function", "deal", "with", "the", "cinder", "notification", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/process.py#L57-L84
[ "def", "cinder_process", "(", "body", ",", "message", ")", ":", "event_type", "=", "body", "[", "'event_type'", "]", "process", "=", "cinder_customer_process", ".", "get", "(", "event_type", ")", "if", "process", "is", "not", "None", ":", "process", "(", "body", ",", "message", ")", "else", ":", "matched", "=", "False", "process_wildcard", "=", "None", "for", "pattern", "in", "cinder_customer_process_wildcard", ".", "keys", "(", ")", ":", "if", "pattern", ".", "match", "(", "event_type", ")", ":", "process_wildcard", "=", "cinder_customer_process_wildcard", ".", "get", "(", "pattern", ")", "matched", "=", "True", "break", "if", "matched", ":", "process_wildcard", "(", "body", ",", "message", ")", "else", ":", "default_process", "(", "body", ",", "message", ")", "message", ".", "ack", "(", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
neutron_process
This function deal with the neutron notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return:
ternya/process.py
def neutron_process(body, message): """ This function deal with the neutron notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = neutron_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in neutron_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = neutron_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
def neutron_process(body, message): """ This function deal with the neutron notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = neutron_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in neutron_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = neutron_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
[ "This", "function", "deal", "with", "the", "neutron", "notification", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/process.py#L87-L114
[ "def", "neutron_process", "(", "body", ",", "message", ")", ":", "event_type", "=", "body", "[", "'event_type'", "]", "process", "=", "neutron_customer_process", ".", "get", "(", "event_type", ")", "if", "process", "is", "not", "None", ":", "process", "(", "body", ",", "message", ")", "else", ":", "matched", "=", "False", "process_wildcard", "=", "None", "for", "pattern", "in", "neutron_customer_process_wildcard", ".", "keys", "(", ")", ":", "if", "pattern", ".", "match", "(", "event_type", ")", ":", "process_wildcard", "=", "neutron_customer_process_wildcard", ".", "get", "(", "pattern", ")", "matched", "=", "True", "break", "if", "matched", ":", "process_wildcard", "(", "body", ",", "message", ")", "else", ":", "default_process", "(", "body", ",", "message", ")", "message", ".", "ack", "(", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
glance_process
This function deal with the glance notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return:
ternya/process.py
def glance_process(body, message): """ This function deal with the glance notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = glance_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in glance_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = glance_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
def glance_process(body, message): """ This function deal with the glance notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = glance_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in glance_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = glance_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
[ "This", "function", "deal", "with", "the", "glance", "notification", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/process.py#L117-L144
[ "def", "glance_process", "(", "body", ",", "message", ")", ":", "event_type", "=", "body", "[", "'event_type'", "]", "process", "=", "glance_customer_process", ".", "get", "(", "event_type", ")", "if", "process", "is", "not", "None", ":", "process", "(", "body", ",", "message", ")", "else", ":", "matched", "=", "False", "process_wildcard", "=", "None", "for", "pattern", "in", "glance_customer_process_wildcard", ".", "keys", "(", ")", ":", "if", "pattern", ".", "match", "(", "event_type", ")", ":", "process_wildcard", "=", "glance_customer_process_wildcard", ".", "get", "(", "pattern", ")", "matched", "=", "True", "break", "if", "matched", ":", "process_wildcard", "(", "body", ",", "message", ")", "else", ":", "default_process", "(", "body", ",", "message", ")", "message", ".", "ack", "(", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
swift_process
This function deal with the swift notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return:
ternya/process.py
def swift_process(body, message): """ This function deal with the swift notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = swift_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in swift_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = swift_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
def swift_process(body, message): """ This function deal with the swift notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = swift_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in swift_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = swift_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
[ "This", "function", "deal", "with", "the", "swift", "notification", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/process.py#L147-L174
[ "def", "swift_process", "(", "body", ",", "message", ")", ":", "event_type", "=", "body", "[", "'event_type'", "]", "process", "=", "swift_customer_process", ".", "get", "(", "event_type", ")", "if", "process", "is", "not", "None", ":", "process", "(", "body", ",", "message", ")", "else", ":", "matched", "=", "False", "process_wildcard", "=", "None", "for", "pattern", "in", "swift_customer_process_wildcard", ".", "keys", "(", ")", ":", "if", "pattern", ".", "match", "(", "event_type", ")", ":", "process_wildcard", "=", "swift_customer_process_wildcard", ".", "get", "(", "pattern", ")", "matched", "=", "True", "break", "if", "matched", ":", "process_wildcard", "(", "body", ",", "message", ")", "else", ":", "default_process", "(", "body", ",", "message", ")", "message", ".", "ack", "(", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
keystone_process
This function deal with the keystone notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return:
ternya/process.py
def keystone_process(body, message): """ This function deal with the keystone notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = keystone_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in keystone_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = keystone_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
def keystone_process(body, message): """ This function deal with the keystone notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = keystone_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in keystone_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = keystone_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
[ "This", "function", "deal", "with", "the", "keystone", "notification", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/process.py#L177-L204
[ "def", "keystone_process", "(", "body", ",", "message", ")", ":", "event_type", "=", "body", "[", "'event_type'", "]", "process", "=", "keystone_customer_process", ".", "get", "(", "event_type", ")", "if", "process", "is", "not", "None", ":", "process", "(", "body", ",", "message", ")", "else", ":", "matched", "=", "False", "process_wildcard", "=", "None", "for", "pattern", "in", "keystone_customer_process_wildcard", ".", "keys", "(", ")", ":", "if", "pattern", ".", "match", "(", "event_type", ")", ":", "process_wildcard", "=", "keystone_customer_process_wildcard", ".", "get", "(", "pattern", ")", "matched", "=", "True", "break", "if", "matched", ":", "process_wildcard", "(", "body", ",", "message", ")", "else", ":", "default_process", "(", "body", ",", "message", ")", "message", ".", "ack", "(", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
heat_process
This function deal with the heat notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return:
ternya/process.py
def heat_process(body, message): """ This function deal with the heat notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = heat_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in heat_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = heat_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
def heat_process(body, message): """ This function deal with the heat notification. First, find process from customer_process that not include wildcard. if not find from customer_process, then find process from customer_process_wildcard. if not find from customer_process_wildcard, then use ternya default process. :param body: dict of openstack notification. :param message: kombu Message class :return: """ event_type = body['event_type'] process = heat_customer_process.get(event_type) if process is not None: process(body, message) else: matched = False process_wildcard = None for pattern in heat_customer_process_wildcard.keys(): if pattern.match(event_type): process_wildcard = heat_customer_process_wildcard.get(pattern) matched = True break if matched: process_wildcard(body, message) else: default_process(body, message) message.ack()
[ "This", "function", "deal", "with", "the", "heat", "notification", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/process.py#L207-L234
[ "def", "heat_process", "(", "body", ",", "message", ")", ":", "event_type", "=", "body", "[", "'event_type'", "]", "process", "=", "heat_customer_process", ".", "get", "(", "event_type", ")", "if", "process", "is", "not", "None", ":", "process", "(", "body", ",", "message", ")", "else", ":", "matched", "=", "False", "process_wildcard", "=", "None", "for", "pattern", "in", "heat_customer_process_wildcard", ".", "keys", "(", ")", ":", "if", "pattern", ".", "match", "(", "event_type", ")", ":", "process_wildcard", "=", "heat_customer_process_wildcard", ".", "get", "(", "pattern", ")", "matched", "=", "True", "break", "if", "matched", ":", "process_wildcard", "(", "body", ",", "message", ")", "else", ":", "default_process", "(", "body", ",", "message", ")", "message", ".", "ack", "(", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
App.serve
Serve app using wsgiref or provided server. Args: - server (callable): An callable
punch/app.py
def serve(self, server=None): """Serve app using wsgiref or provided server. Args: - server (callable): An callable """ if server is None: from wsgiref.simple_server import make_server server = lambda app: make_server('', 8000, app).serve_forever() print('Listening on 0.0.0.0:8000') try: server(self) finally: server.socket.close()
def serve(self, server=None): """Serve app using wsgiref or provided server. Args: - server (callable): An callable """ if server is None: from wsgiref.simple_server import make_server server = lambda app: make_server('', 8000, app).serve_forever() print('Listening on 0.0.0.0:8000') try: server(self) finally: server.socket.close()
[ "Serve", "app", "using", "wsgiref", "or", "provided", "server", "." ]
rochacon/punch
python
https://github.com/rochacon/punch/blob/7f6fb81221049ab74ef561fb40a4174bdb3e77ef/punch/app.py#L70-L83
[ "def", "serve", "(", "self", ",", "server", "=", "None", ")", ":", "if", "server", "is", "None", ":", "from", "wsgiref", ".", "simple_server", "import", "make_server", "server", "=", "lambda", "app", ":", "make_server", "(", "''", ",", "8000", ",", "app", ")", ".", "serve_forever", "(", ")", "print", "(", "'Listening on 0.0.0.0:8000'", ")", "try", ":", "server", "(", "self", ")", "finally", ":", "server", ".", "socket", ".", "close", "(", ")" ]
7f6fb81221049ab74ef561fb40a4174bdb3e77ef
test
pout
Print 'msg' to stdout, and option 'log' at info level.
nicfit/console/_io.py
def pout(msg, log=None): """Print 'msg' to stdout, and option 'log' at info level.""" _print(msg, sys.stdout, log_func=log.info if log else None)
def pout(msg, log=None): """Print 'msg' to stdout, and option 'log' at info level.""" _print(msg, sys.stdout, log_func=log.info if log else None)
[ "Print", "msg", "to", "stdout", "and", "option", "log", "at", "info", "level", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/console/_io.py#L4-L6
[ "def", "pout", "(", "msg", ",", "log", "=", "None", ")", ":", "_print", "(", "msg", ",", "sys", ".", "stdout", ",", "log_func", "=", "log", ".", "info", "if", "log", "else", "None", ")" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
perr
Print 'msg' to stderr, and option 'log' at info level.
nicfit/console/_io.py
def perr(msg, log=None): """Print 'msg' to stderr, and option 'log' at info level.""" _print(msg, sys.stderr, log_func=log.error if log else None)
def perr(msg, log=None): """Print 'msg' to stderr, and option 'log' at info level.""" _print(msg, sys.stderr, log_func=log.error if log else None)
[ "Print", "msg", "to", "stderr", "and", "option", "log", "at", "info", "level", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/console/_io.py#L9-L11
[ "def", "perr", "(", "msg", ",", "log", "=", "None", ")", ":", "_print", "(", "msg", ",", "sys", ".", "stderr", ",", "log_func", "=", "log", ".", "error", "if", "log", "else", "None", ")" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
register
A class decorator for Command classes to register in the default set.
nicfit/command.py
def register(CommandSubClass): """A class decorator for Command classes to register in the default set.""" name = CommandSubClass.name() if name in Command._all_commands: raise ValueError("Command already exists: " + name) Command._all_commands[name] = CommandSubClass return CommandSubClass
def register(CommandSubClass): """A class decorator for Command classes to register in the default set.""" name = CommandSubClass.name() if name in Command._all_commands: raise ValueError("Command already exists: " + name) Command._all_commands[name] = CommandSubClass return CommandSubClass
[ "A", "class", "decorator", "for", "Command", "classes", "to", "register", "in", "the", "default", "set", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/command.py#L11-L17
[ "def", "register", "(", "CommandSubClass", ")", ":", "name", "=", "CommandSubClass", ".", "name", "(", ")", "if", "name", "in", "Command", ".", "_all_commands", ":", "raise", "ValueError", "(", "\"Command already exists: \"", "+", "name", ")", "Command", ".", "_all_commands", "[", "name", "]", "=", "CommandSubClass", "return", "CommandSubClass" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
Command.register
A class decorator for Command classes to register.
nicfit/command.py
def register(Class, CommandSubClass): """A class decorator for Command classes to register.""" for name in [CommandSubClass.name()] + CommandSubClass.aliases(): if name in Class._registered_commands[Class]: raise ValueError("Command already exists: " + name) Class._registered_commands[Class][name] = CommandSubClass return CommandSubClass
def register(Class, CommandSubClass): """A class decorator for Command classes to register.""" for name in [CommandSubClass.name()] + CommandSubClass.aliases(): if name in Class._registered_commands[Class]: raise ValueError("Command already exists: " + name) Class._registered_commands[Class][name] = CommandSubClass return CommandSubClass
[ "A", "class", "decorator", "for", "Command", "classes", "to", "register", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/command.py#L37-L43
[ "def", "register", "(", "Class", ",", "CommandSubClass", ")", ":", "for", "name", "in", "[", "CommandSubClass", ".", "name", "(", ")", "]", "+", "CommandSubClass", ".", "aliases", "(", ")", ":", "if", "name", "in", "Class", ".", "_registered_commands", "[", "Class", "]", ":", "raise", "ValueError", "(", "\"Command already exists: \"", "+", "name", ")", "Class", ".", "_registered_commands", "[", "Class", "]", "[", "name", "]", "=", "CommandSubClass", "return", "CommandSubClass" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
Command.loadCommandMap
Instantiate each registered command to a dict mapping name/alias to instance. Due to aliases, the returned length may be greater there the number of commands, but the unique instance count will match.
nicfit/command.py
def loadCommandMap(Class, subparsers=None, instantiate=True, **cmd_kwargs): """Instantiate each registered command to a dict mapping name/alias to instance. Due to aliases, the returned length may be greater there the number of commands, but the unique instance count will match. """ if not Class._registered_commands: raise ValueError("No commands have been registered with {}" .format(Class)) all = {} for Cmd in set(Class._registered_commands[Class].values()): cmd = Cmd(subparsers=subparsers, **cmd_kwargs) \ if instantiate else Cmd for name in [Cmd.name()] + Cmd.aliases(): all[name] = cmd return all
def loadCommandMap(Class, subparsers=None, instantiate=True, **cmd_kwargs): """Instantiate each registered command to a dict mapping name/alias to instance. Due to aliases, the returned length may be greater there the number of commands, but the unique instance count will match. """ if not Class._registered_commands: raise ValueError("No commands have been registered with {}" .format(Class)) all = {} for Cmd in set(Class._registered_commands[Class].values()): cmd = Cmd(subparsers=subparsers, **cmd_kwargs) \ if instantiate else Cmd for name in [Cmd.name()] + Cmd.aliases(): all[name] = cmd return all
[ "Instantiate", "each", "registered", "command", "to", "a", "dict", "mapping", "name", "/", "alias", "to", "instance", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/command.py#L118-L135
[ "def", "loadCommandMap", "(", "Class", ",", "subparsers", "=", "None", ",", "instantiate", "=", "True", ",", "*", "*", "cmd_kwargs", ")", ":", "if", "not", "Class", ".", "_registered_commands", ":", "raise", "ValueError", "(", "\"No commands have been registered with {}\"", ".", "format", "(", "Class", ")", ")", "all", "=", "{", "}", "for", "Cmd", "in", "set", "(", "Class", ".", "_registered_commands", "[", "Class", "]", ".", "values", "(", ")", ")", ":", "cmd", "=", "Cmd", "(", "subparsers", "=", "subparsers", ",", "*", "*", "cmd_kwargs", ")", "if", "instantiate", "else", "Cmd", "for", "name", "in", "[", "Cmd", ".", "name", "(", ")", "]", "+", "Cmd", ".", "aliases", "(", ")", ":", "all", "[", "name", "]", "=", "cmd", "return", "all" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
ConstrainedArgument.toString
If all of the constraints are satisfied with the given value, defers to the composed AMP argument's ``toString`` method.
txampext/constraints.py
def toString(self, value): """ If all of the constraints are satisfied with the given value, defers to the composed AMP argument's ``toString`` method. """ self._checkConstraints(value) return self.baseArgument.toString(value)
def toString(self, value): """ If all of the constraints are satisfied with the given value, defers to the composed AMP argument's ``toString`` method. """ self._checkConstraints(value) return self.baseArgument.toString(value)
[ "If", "all", "of", "the", "constraints", "are", "satisfied", "with", "the", "given", "value", "defers", "to", "the", "composed", "AMP", "argument", "s", "toString", "method", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/txampext/constraints.py#L20-L26
[ "def", "toString", "(", "self", ",", "value", ")", ":", "self", ".", "_checkConstraints", "(", "value", ")", "return", "self", ".", "baseArgument", ".", "toString", "(", "value", ")" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
ConstrainedArgument.fromString
Converts the string to a value using the composed AMP argument, then checks all the constraints against that value.
txampext/constraints.py
def fromString(self, string): """ Converts the string to a value using the composed AMP argument, then checks all the constraints against that value. """ value = self.baseArgument.fromString(string) self._checkConstraints(value) return value
def fromString(self, string): """ Converts the string to a value using the composed AMP argument, then checks all the constraints against that value. """ value = self.baseArgument.fromString(string) self._checkConstraints(value) return value
[ "Converts", "the", "string", "to", "a", "value", "using", "the", "composed", "AMP", "argument", "then", "checks", "all", "the", "constraints", "against", "that", "value", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/txampext/constraints.py#L29-L36
[ "def", "fromString", "(", "self", ",", "string", ")", ":", "value", "=", "self", ".", "baseArgument", ".", "fromString", "(", "string", ")", "self", ".", "_checkConstraints", "(", "value", ")", "return", "value" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
_updateCompleterDict
Merges ``cdict`` into ``completers``. In the event that a key in cdict already exists in the completers dict a ValueError is raised iff ``regex`` false'y. If a regex str is provided it and the duplicate key are updated to be unique, and the updated regex is returned.
nicfit/shell/completion.py
def _updateCompleterDict(completers, cdict, regex=None): """Merges ``cdict`` into ``completers``. In the event that a key in cdict already exists in the completers dict a ValueError is raised iff ``regex`` false'y. If a regex str is provided it and the duplicate key are updated to be unique, and the updated regex is returned. """ for key in cdict: if key in completers and not regex: raise ValueError(f"Duplicate completion key: {key}") if key in completers: uniq = "_".join([key, str(uuid.uuid4()).replace("-", "")]) regex = regex.replace(f"P<{key}>", f"P<{uniq}>") completers[uniq] = cdict[key] else: completers[key] = cdict[key] return regex
def _updateCompleterDict(completers, cdict, regex=None): """Merges ``cdict`` into ``completers``. In the event that a key in cdict already exists in the completers dict a ValueError is raised iff ``regex`` false'y. If a regex str is provided it and the duplicate key are updated to be unique, and the updated regex is returned. """ for key in cdict: if key in completers and not regex: raise ValueError(f"Duplicate completion key: {key}") if key in completers: uniq = "_".join([key, str(uuid.uuid4()).replace("-", "")]) regex = regex.replace(f"P<{key}>", f"P<{uniq}>") completers[uniq] = cdict[key] else: completers[key] = cdict[key] return regex
[ "Merges", "cdict", "into", "completers", ".", "In", "the", "event", "that", "a", "key", "in", "cdict", "already", "exists", "in", "the", "completers", "dict", "a", "ValueError", "is", "raised", "iff", "regex", "false", "y", ".", "If", "a", "regex", "str", "is", "provided", "it", "and", "the", "duplicate", "key", "are", "updated", "to", "be", "unique", "and", "the", "updated", "regex", "is", "returned", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/shell/completion.py#L13-L30
[ "def", "_updateCompleterDict", "(", "completers", ",", "cdict", ",", "regex", "=", "None", ")", ":", "for", "key", "in", "cdict", ":", "if", "key", "in", "completers", "and", "not", "regex", ":", "raise", "ValueError", "(", "f\"Duplicate completion key: {key}\"", ")", "if", "key", "in", "completers", ":", "uniq", "=", "\"_\"", ".", "join", "(", "[", "key", ",", "str", "(", "uuid", ".", "uuid4", "(", ")", ")", ".", "replace", "(", "\"-\"", ",", "\"\"", ")", "]", ")", "regex", "=", "regex", ".", "replace", "(", "f\"P<{key}>\"", ",", "f\"P<{uniq}>\"", ")", "completers", "[", "uniq", "]", "=", "cdict", "[", "key", "]", "else", ":", "completers", "[", "key", "]", "=", "cdict", "[", "key", "]", "return", "regex" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
WordCompleter.get_completions
log.debug("------------------------------------------------------") log.debug(f"** WORD {self.WORD}") log.debug(f"** words {self.words}") log.debug(f"** word_before_cursor {word_before_cursor}")
nicfit/shell/completion.py
def get_completions(self, document, complete_event): # Get word/text before cursor. if self.sentence: word_before_cursor = document.text_before_cursor else: word_before_cursor = document.get_word_before_cursor(WORD=self.WORD) if self.ignore_case: word_before_cursor = word_before_cursor.lower() def word_matches(word): """ True when the word before the cursor matches. """ if self.ignore_case: word = word.lower() if self.match_middle: return word_before_cursor in word else: return word.startswith(word_before_cursor) ''' log.debug("------------------------------------------------------") log.debug(f"** WORD {self.WORD}") log.debug(f"** words {self.words}") log.debug(f"** word_before_cursor {word_before_cursor}") ''' words = self._words_callable() if self._words_callable else self.words for a in words: if word_matches(a): display_meta = self.meta_dict.get(a, '') log.debug(f"MATCH: {a}, {-len(word_before_cursor)}," f" meta: {display_meta}") yield Completion(self.quote(a), -len(word_before_cursor), display_meta=display_meta)
def get_completions(self, document, complete_event): # Get word/text before cursor. if self.sentence: word_before_cursor = document.text_before_cursor else: word_before_cursor = document.get_word_before_cursor(WORD=self.WORD) if self.ignore_case: word_before_cursor = word_before_cursor.lower() def word_matches(word): """ True when the word before the cursor matches. """ if self.ignore_case: word = word.lower() if self.match_middle: return word_before_cursor in word else: return word.startswith(word_before_cursor) ''' log.debug("------------------------------------------------------") log.debug(f"** WORD {self.WORD}") log.debug(f"** words {self.words}") log.debug(f"** word_before_cursor {word_before_cursor}") ''' words = self._words_callable() if self._words_callable else self.words for a in words: if word_matches(a): display_meta = self.meta_dict.get(a, '') log.debug(f"MATCH: {a}, {-len(word_before_cursor)}," f" meta: {display_meta}") yield Completion(self.quote(a), -len(word_before_cursor), display_meta=display_meta)
[ "log", ".", "debug", "(", "------------------------------------------------------", ")", "log", ".", "debug", "(", "f", "**", "WORD", "{", "self", ".", "WORD", "}", ")", "log", ".", "debug", "(", "f", "**", "words", "{", "self", ".", "words", "}", ")", "log", ".", "debug", "(", "f", "**", "word_before_cursor", "{", "word_before_cursor", "}", ")" ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/shell/completion.py#L76-L110
[ "def", "get_completions", "(", "self", ",", "document", ",", "complete_event", ")", ":", "# Get word/text before cursor.", "if", "self", ".", "sentence", ":", "word_before_cursor", "=", "document", ".", "text_before_cursor", "else", ":", "word_before_cursor", "=", "document", ".", "get_word_before_cursor", "(", "WORD", "=", "self", ".", "WORD", ")", "if", "self", ".", "ignore_case", ":", "word_before_cursor", "=", "word_before_cursor", ".", "lower", "(", ")", "def", "word_matches", "(", "word", ")", ":", "\"\"\" True when the word before the cursor matches. \"\"\"", "if", "self", ".", "ignore_case", ":", "word", "=", "word", ".", "lower", "(", ")", "if", "self", ".", "match_middle", ":", "return", "word_before_cursor", "in", "word", "else", ":", "return", "word", ".", "startswith", "(", "word_before_cursor", ")", "words", "=", "self", ".", "_words_callable", "(", ")", "if", "self", ".", "_words_callable", "else", "self", ".", "words", "for", "a", "in", "words", ":", "if", "word_matches", "(", "a", ")", ":", "display_meta", "=", "self", ".", "meta_dict", ".", "get", "(", "a", ",", "''", ")", "log", ".", "debug", "(", "f\"MATCH: {a}, {-len(word_before_cursor)},\"", "f\" meta: {display_meta}\"", ")", "yield", "Completion", "(", "self", ".", "quote", "(", "a", ")", ",", "-", "len", "(", "word_before_cursor", ")", ",", "display_meta", "=", "display_meta", ")" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
Ternya.work
Start ternya work. First, import customer's service modules. Second, init openstack mq. Third, keep a ternya connection that can auto-reconnect.
ternya/ternya.py
def work(self): """ Start ternya work. First, import customer's service modules. Second, init openstack mq. Third, keep a ternya connection that can auto-reconnect. """ self.init_modules() connection = self.init_mq() TernyaConnection(self, connection).connect()
def work(self): """ Start ternya work. First, import customer's service modules. Second, init openstack mq. Third, keep a ternya connection that can auto-reconnect. """ self.init_modules() connection = self.init_mq() TernyaConnection(self, connection).connect()
[ "Start", "ternya", "work", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L62-L72
[ "def", "work", "(", "self", ")", ":", "self", ".", "init_modules", "(", ")", "connection", "=", "self", ".", "init_mq", "(", ")", "TernyaConnection", "(", "self", ",", "connection", ")", ".", "connect", "(", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.init_mq
Init connection and consumer with openstack mq.
ternya/ternya.py
def init_mq(self): """Init connection and consumer with openstack mq.""" mq = self.init_connection() self.init_consumer(mq) return mq.connection
def init_mq(self): """Init connection and consumer with openstack mq.""" mq = self.init_connection() self.init_consumer(mq) return mq.connection
[ "Init", "connection", "and", "consumer", "with", "openstack", "mq", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L74-L78
[ "def", "init_mq", "(", "self", ")", ":", "mq", "=", "self", ".", "init_connection", "(", ")", "self", ".", "init_consumer", "(", "mq", ")", "return", "mq", ".", "connection" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.init_modules
Import customer's service modules.
ternya/ternya.py
def init_modules(self): """Import customer's service modules.""" if not self.config: raise ValueError("please read your config file.") log.debug("begin to import customer's service modules.") modules = ServiceModules(self.config) modules.import_modules() log.debug("end to import customer's service modules.")
def init_modules(self): """Import customer's service modules.""" if not self.config: raise ValueError("please read your config file.") log.debug("begin to import customer's service modules.") modules = ServiceModules(self.config) modules.import_modules() log.debug("end to import customer's service modules.")
[ "Import", "customer", "s", "service", "modules", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L80-L88
[ "def", "init_modules", "(", "self", ")", ":", "if", "not", "self", ".", "config", ":", "raise", "ValueError", "(", "\"please read your config file.\"", ")", "log", ".", "debug", "(", "\"begin to import customer's service modules.\"", ")", "modules", "=", "ServiceModules", "(", "self", ".", "config", ")", "modules", ".", "import_modules", "(", ")", "log", ".", "debug", "(", "\"end to import customer's service modules.\"", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.init_nova_consumer
Init openstack nova mq 1. Check if enable listening nova notification 2. Create consumer :param mq: class ternya.mq.MQ
ternya/ternya.py
def init_nova_consumer(self, mq): """ Init openstack nova mq 1. Check if enable listening nova notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Nova): log.debug("disable listening nova notification") return for i in range(self.config.nova_mq_consumer_count): mq.create_consumer(self.config.nova_mq_exchange, self.config.nova_mq_queue, ProcessFactory.process(Openstack.Nova)) log.debug("enable listening openstack nova notification.")
def init_nova_consumer(self, mq): """ Init openstack nova mq 1. Check if enable listening nova notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Nova): log.debug("disable listening nova notification") return for i in range(self.config.nova_mq_consumer_count): mq.create_consumer(self.config.nova_mq_exchange, self.config.nova_mq_queue, ProcessFactory.process(Openstack.Nova)) log.debug("enable listening openstack nova notification.")
[ "Init", "openstack", "nova", "mq" ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L106-L123
[ "def", "init_nova_consumer", "(", "self", ",", "mq", ")", ":", "if", "not", "self", ".", "enable_component_notification", "(", "Openstack", ".", "Nova", ")", ":", "log", ".", "debug", "(", "\"disable listening nova notification\"", ")", "return", "for", "i", "in", "range", "(", "self", ".", "config", ".", "nova_mq_consumer_count", ")", ":", "mq", ".", "create_consumer", "(", "self", ".", "config", ".", "nova_mq_exchange", ",", "self", ".", "config", ".", "nova_mq_queue", ",", "ProcessFactory", ".", "process", "(", "Openstack", ".", "Nova", ")", ")", "log", ".", "debug", "(", "\"enable listening openstack nova notification.\"", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.init_cinder_consumer
Init openstack cinder mq 1. Check if enable listening cinder notification 2. Create consumer :param mq: class ternya.mq.MQ
ternya/ternya.py
def init_cinder_consumer(self, mq): """ Init openstack cinder mq 1. Check if enable listening cinder notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Cinder): log.debug("disable listening cinder notification") return for i in range(self.config.cinder_mq_consumer_count): mq.create_consumer(self.config.cinder_mq_exchange, self.config.cinder_mq_queue, ProcessFactory.process(Openstack.Cinder)) log.debug("enable listening openstack cinder notification.")
def init_cinder_consumer(self, mq): """ Init openstack cinder mq 1. Check if enable listening cinder notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Cinder): log.debug("disable listening cinder notification") return for i in range(self.config.cinder_mq_consumer_count): mq.create_consumer(self.config.cinder_mq_exchange, self.config.cinder_mq_queue, ProcessFactory.process(Openstack.Cinder)) log.debug("enable listening openstack cinder notification.")
[ "Init", "openstack", "cinder", "mq" ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L125-L143
[ "def", "init_cinder_consumer", "(", "self", ",", "mq", ")", ":", "if", "not", "self", ".", "enable_component_notification", "(", "Openstack", ".", "Cinder", ")", ":", "log", ".", "debug", "(", "\"disable listening cinder notification\"", ")", "return", "for", "i", "in", "range", "(", "self", ".", "config", ".", "cinder_mq_consumer_count", ")", ":", "mq", ".", "create_consumer", "(", "self", ".", "config", ".", "cinder_mq_exchange", ",", "self", ".", "config", ".", "cinder_mq_queue", ",", "ProcessFactory", ".", "process", "(", "Openstack", ".", "Cinder", ")", ")", "log", ".", "debug", "(", "\"enable listening openstack cinder notification.\"", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.init_neutron_consumer
Init openstack neutron mq 1. Check if enable listening neutron notification 2. Create consumer :param mq: class ternya.mq.MQ
ternya/ternya.py
def init_neutron_consumer(self, mq): """ Init openstack neutron mq 1. Check if enable listening neutron notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Neutron): log.debug("disable listening neutron notification") return for i in range(self.config.neutron_mq_consumer_count): mq.create_consumer(self.config.neutron_mq_exchange, self.config.neutron_mq_queue, ProcessFactory.process(Openstack.Neutron)) log.debug("enable listening openstack neutron notification.")
def init_neutron_consumer(self, mq): """ Init openstack neutron mq 1. Check if enable listening neutron notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Neutron): log.debug("disable listening neutron notification") return for i in range(self.config.neutron_mq_consumer_count): mq.create_consumer(self.config.neutron_mq_exchange, self.config.neutron_mq_queue, ProcessFactory.process(Openstack.Neutron)) log.debug("enable listening openstack neutron notification.")
[ "Init", "openstack", "neutron", "mq" ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L145-L163
[ "def", "init_neutron_consumer", "(", "self", ",", "mq", ")", ":", "if", "not", "self", ".", "enable_component_notification", "(", "Openstack", ".", "Neutron", ")", ":", "log", ".", "debug", "(", "\"disable listening neutron notification\"", ")", "return", "for", "i", "in", "range", "(", "self", ".", "config", ".", "neutron_mq_consumer_count", ")", ":", "mq", ".", "create_consumer", "(", "self", ".", "config", ".", "neutron_mq_exchange", ",", "self", ".", "config", ".", "neutron_mq_queue", ",", "ProcessFactory", ".", "process", "(", "Openstack", ".", "Neutron", ")", ")", "log", ".", "debug", "(", "\"enable listening openstack neutron notification.\"", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.init_glance_consumer
Init openstack glance mq 1. Check if enable listening glance notification 2. Create consumer :param mq: class ternya.mq.MQ
ternya/ternya.py
def init_glance_consumer(self, mq): """ Init openstack glance mq 1. Check if enable listening glance notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Glance): log.debug("disable listening glance notification") return for i in range(self.config.glance_mq_consumer_count): mq.create_consumer(self.config.glance_mq_exchange, self.config.glance_mq_queue, ProcessFactory.process(Openstack.Glance)) log.debug("enable listening openstack glance notification.")
def init_glance_consumer(self, mq): """ Init openstack glance mq 1. Check if enable listening glance notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Glance): log.debug("disable listening glance notification") return for i in range(self.config.glance_mq_consumer_count): mq.create_consumer(self.config.glance_mq_exchange, self.config.glance_mq_queue, ProcessFactory.process(Openstack.Glance)) log.debug("enable listening openstack glance notification.")
[ "Init", "openstack", "glance", "mq" ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L165-L183
[ "def", "init_glance_consumer", "(", "self", ",", "mq", ")", ":", "if", "not", "self", ".", "enable_component_notification", "(", "Openstack", ".", "Glance", ")", ":", "log", ".", "debug", "(", "\"disable listening glance notification\"", ")", "return", "for", "i", "in", "range", "(", "self", ".", "config", ".", "glance_mq_consumer_count", ")", ":", "mq", ".", "create_consumer", "(", "self", ".", "config", ".", "glance_mq_exchange", ",", "self", ".", "config", ".", "glance_mq_queue", ",", "ProcessFactory", ".", "process", "(", "Openstack", ".", "Glance", ")", ")", "log", ".", "debug", "(", "\"enable listening openstack glance notification.\"", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.init_swift_consumer
Init openstack swift mq 1. Check if enable listening swift notification 2. Create consumer :param mq: class ternya.mq.MQ
ternya/ternya.py
def init_swift_consumer(self, mq): """ Init openstack swift mq 1. Check if enable listening swift notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Swift): log.debug("disable listening swift notification") return for i in range(self.config.swift_mq_consumer_count): mq.create_consumer(self.config.swift_mq_exchange, self.config.swift_mq_queue, ProcessFactory.process(Openstack.Swift)) log.debug("enable listening openstack swift notification.")
def init_swift_consumer(self, mq): """ Init openstack swift mq 1. Check if enable listening swift notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Swift): log.debug("disable listening swift notification") return for i in range(self.config.swift_mq_consumer_count): mq.create_consumer(self.config.swift_mq_exchange, self.config.swift_mq_queue, ProcessFactory.process(Openstack.Swift)) log.debug("enable listening openstack swift notification.")
[ "Init", "openstack", "swift", "mq" ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L185-L203
[ "def", "init_swift_consumer", "(", "self", ",", "mq", ")", ":", "if", "not", "self", ".", "enable_component_notification", "(", "Openstack", ".", "Swift", ")", ":", "log", ".", "debug", "(", "\"disable listening swift notification\"", ")", "return", "for", "i", "in", "range", "(", "self", ".", "config", ".", "swift_mq_consumer_count", ")", ":", "mq", ".", "create_consumer", "(", "self", ".", "config", ".", "swift_mq_exchange", ",", "self", ".", "config", ".", "swift_mq_queue", ",", "ProcessFactory", ".", "process", "(", "Openstack", ".", "Swift", ")", ")", "log", ".", "debug", "(", "\"enable listening openstack swift notification.\"", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.init_keystone_consumer
Init openstack swift mq 1. Check if enable listening keystone notification 2. Create consumer :param mq: class ternya.mq.MQ
ternya/ternya.py
def init_keystone_consumer(self, mq): """ Init openstack swift mq 1. Check if enable listening keystone notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Keystone): log.debug("disable listening keystone notification") return for i in range(self.config.keystone_mq_consumer_count): mq.create_consumer(self.config.keystone_mq_exchange, self.config.keystone_mq_queue, ProcessFactory.process(Openstack.Keystone)) log.debug("enable listening openstack keystone notification.")
def init_keystone_consumer(self, mq): """ Init openstack swift mq 1. Check if enable listening keystone notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Keystone): log.debug("disable listening keystone notification") return for i in range(self.config.keystone_mq_consumer_count): mq.create_consumer(self.config.keystone_mq_exchange, self.config.keystone_mq_queue, ProcessFactory.process(Openstack.Keystone)) log.debug("enable listening openstack keystone notification.")
[ "Init", "openstack", "swift", "mq" ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L205-L223
[ "def", "init_keystone_consumer", "(", "self", ",", "mq", ")", ":", "if", "not", "self", ".", "enable_component_notification", "(", "Openstack", ".", "Keystone", ")", ":", "log", ".", "debug", "(", "\"disable listening keystone notification\"", ")", "return", "for", "i", "in", "range", "(", "self", ".", "config", ".", "keystone_mq_consumer_count", ")", ":", "mq", ".", "create_consumer", "(", "self", ".", "config", ".", "keystone_mq_exchange", ",", "self", ".", "config", ".", "keystone_mq_queue", ",", "ProcessFactory", ".", "process", "(", "Openstack", ".", "Keystone", ")", ")", "log", ".", "debug", "(", "\"enable listening openstack keystone notification.\"", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.init_heat_consumer
Init openstack heat mq 1. Check if enable listening heat notification 2. Create consumer :param mq: class ternya.mq.MQ
ternya/ternya.py
def init_heat_consumer(self, mq): """ Init openstack heat mq 1. Check if enable listening heat notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Heat): log.debug("disable listening heat notification") return for i in range(self.config.heat_mq_consumer_count): mq.create_consumer(self.config.heat_mq_exchange, self.config.heat_mq_queue, ProcessFactory.process(Openstack.Heat)) log.debug("enable listening openstack heat notification.")
def init_heat_consumer(self, mq): """ Init openstack heat mq 1. Check if enable listening heat notification 2. Create consumer :param mq: class ternya.mq.MQ """ if not self.enable_component_notification(Openstack.Heat): log.debug("disable listening heat notification") return for i in range(self.config.heat_mq_consumer_count): mq.create_consumer(self.config.heat_mq_exchange, self.config.heat_mq_queue, ProcessFactory.process(Openstack.Heat)) log.debug("enable listening openstack heat notification.")
[ "Init", "openstack", "heat", "mq" ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L225-L243
[ "def", "init_heat_consumer", "(", "self", ",", "mq", ")", ":", "if", "not", "self", ".", "enable_component_notification", "(", "Openstack", ".", "Heat", ")", ":", "log", ".", "debug", "(", "\"disable listening heat notification\"", ")", "return", "for", "i", "in", "range", "(", "self", ".", "config", ".", "heat_mq_consumer_count", ")", ":", "mq", ".", "create_consumer", "(", "self", ".", "config", ".", "heat_mq_exchange", ",", "self", ".", "config", ".", "heat_mq_queue", ",", "ProcessFactory", ".", "process", "(", "Openstack", ".", "Heat", ")", ")", "log", ".", "debug", "(", "\"enable listening openstack heat notification.\"", ")" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
Ternya.enable_component_notification
Check if customer enable openstack component notification. :param openstack_component: Openstack component type.
ternya/ternya.py
def enable_component_notification(self, openstack_component): """ Check if customer enable openstack component notification. :param openstack_component: Openstack component type. """ openstack_component_mapping = { Openstack.Nova: self.config.listen_nova_notification, Openstack.Cinder: self.config.listen_cinder_notification, Openstack.Neutron: self.config.listen_neutron_notification, Openstack.Glance: self.config.listen_glance_notification, Openstack.Swift: self.config.listen_swift_notification, Openstack.Keystone: self.config.listen_keystone_notification, Openstack.Heat: self.config.listen_heat_notification } return openstack_component_mapping[openstack_component]
def enable_component_notification(self, openstack_component): """ Check if customer enable openstack component notification. :param openstack_component: Openstack component type. """ openstack_component_mapping = { Openstack.Nova: self.config.listen_nova_notification, Openstack.Cinder: self.config.listen_cinder_notification, Openstack.Neutron: self.config.listen_neutron_notification, Openstack.Glance: self.config.listen_glance_notification, Openstack.Swift: self.config.listen_swift_notification, Openstack.Keystone: self.config.listen_keystone_notification, Openstack.Heat: self.config.listen_heat_notification } return openstack_component_mapping[openstack_component]
[ "Check", "if", "customer", "enable", "openstack", "component", "notification", "." ]
ndrlslz/ternya
python
https://github.com/ndrlslz/ternya/blob/c05aec10029e645d63ff04313dbcf2644743481f/ternya/ternya.py#L245-L260
[ "def", "enable_component_notification", "(", "self", ",", "openstack_component", ")", ":", "openstack_component_mapping", "=", "{", "Openstack", ".", "Nova", ":", "self", ".", "config", ".", "listen_nova_notification", ",", "Openstack", ".", "Cinder", ":", "self", ".", "config", ".", "listen_cinder_notification", ",", "Openstack", ".", "Neutron", ":", "self", ".", "config", ".", "listen_neutron_notification", ",", "Openstack", ".", "Glance", ":", "self", ".", "config", ".", "listen_glance_notification", ",", "Openstack", ".", "Swift", ":", "self", ".", "config", ".", "listen_swift_notification", ",", "Openstack", ".", "Keystone", ":", "self", ".", "config", ".", "listen_keystone_notification", ",", "Openstack", ".", "Heat", ":", "self", ".", "config", ".", "listen_heat_notification", "}", "return", "openstack_component_mapping", "[", "openstack_component", "]" ]
c05aec10029e645d63ff04313dbcf2644743481f
test
music_info
Get music info from baidu music api
bmd/bmd.py
def music_info(songid): """ Get music info from baidu music api """ if isinstance(songid, list): songid = ','.join(songid) data = { "hq": 1, "songIds": songid } res = requests.post(MUSIC_INFO_URL, data=data) info = res.json() music_data = info["data"] songs = [] for song in music_data["songList"]: song_link, size = _song_link(song, music_data["xcode"]) songs.append({ "name": song["songName"], "singer": song["artistName"], "lrc_link": song["lrcLink"], "song_link": song_link, "size": size }) return songs
def music_info(songid): """ Get music info from baidu music api """ if isinstance(songid, list): songid = ','.join(songid) data = { "hq": 1, "songIds": songid } res = requests.post(MUSIC_INFO_URL, data=data) info = res.json() music_data = info["data"] songs = [] for song in music_data["songList"]: song_link, size = _song_link(song, music_data["xcode"]) songs.append({ "name": song["songName"], "singer": song["artistName"], "lrc_link": song["lrcLink"], "song_link": song_link, "size": size }) return songs
[ "Get", "music", "info", "from", "baidu", "music", "api" ]
maralla/bmd
python
https://github.com/maralla/bmd/blob/bbf87dc01de9a363ae5031e22a5ccc50d506f78a/bmd/bmd.py#L61-L85
[ "def", "music_info", "(", "songid", ")", ":", "if", "isinstance", "(", "songid", ",", "list", ")", ":", "songid", "=", "','", ".", "join", "(", "songid", ")", "data", "=", "{", "\"hq\"", ":", "1", ",", "\"songIds\"", ":", "songid", "}", "res", "=", "requests", ".", "post", "(", "MUSIC_INFO_URL", ",", "data", "=", "data", ")", "info", "=", "res", ".", "json", "(", ")", "music_data", "=", "info", "[", "\"data\"", "]", "songs", "=", "[", "]", "for", "song", "in", "music_data", "[", "\"songList\"", "]", ":", "song_link", ",", "size", "=", "_song_link", "(", "song", ",", "music_data", "[", "\"xcode\"", "]", ")", "songs", ".", "append", "(", "{", "\"name\"", ":", "song", "[", "\"songName\"", "]", ",", "\"singer\"", ":", "song", "[", "\"artistName\"", "]", ",", "\"lrc_link\"", ":", "song", "[", "\"lrcLink\"", "]", ",", "\"song_link\"", ":", "song_link", ",", "\"size\"", ":", "size", "}", ")", "return", "songs" ]
bbf87dc01de9a363ae5031e22a5ccc50d506f78a
test
download_music
process for downing music with multiple threads
bmd/bmd.py
def download_music(song, thread_num=4): """ process for downing music with multiple threads """ filename = "{}.mp3".format(song["name"]) if os.path.exists(filename): os.remove(filename) part = int(song["size"] / thread_num) if part <= 1024: thread_num = 1 _id = uuid.uuid4().hex logger.info("downloading '{}'...".format(song["name"])) threads = [] for i in range(thread_num): if i == thread_num - 1: end = '' else: end = (i + 1) * part - 1 thread = Worker((i * part, end), song, _id) thread.start() threads.append(thread) for t in threads: t.join() fileParts = glob.glob("part-{}-*".format(_id)) fileParts.sort(key=lambda e: e.split('-')[-1]) logger.info("'{}' combine parts...".format(song["name"])) with open(filename, "ab") as f: for part in fileParts: with open(part, "rb") as d: shutil.copyfileobj(d, f) os.remove(part) logger.info("'{}' finished".format(song["name"]))
def download_music(song, thread_num=4): """ process for downing music with multiple threads """ filename = "{}.mp3".format(song["name"]) if os.path.exists(filename): os.remove(filename) part = int(song["size"] / thread_num) if part <= 1024: thread_num = 1 _id = uuid.uuid4().hex logger.info("downloading '{}'...".format(song["name"])) threads = [] for i in range(thread_num): if i == thread_num - 1: end = '' else: end = (i + 1) * part - 1 thread = Worker((i * part, end), song, _id) thread.start() threads.append(thread) for t in threads: t.join() fileParts = glob.glob("part-{}-*".format(_id)) fileParts.sort(key=lambda e: e.split('-')[-1]) logger.info("'{}' combine parts...".format(song["name"])) with open(filename, "ab") as f: for part in fileParts: with open(part, "rb") as d: shutil.copyfileobj(d, f) os.remove(part) logger.info("'{}' finished".format(song["name"]))
[ "process", "for", "downing", "music", "with", "multiple", "threads" ]
maralla/bmd
python
https://github.com/maralla/bmd/blob/bbf87dc01de9a363ae5031e22a5ccc50d506f78a/bmd/bmd.py#L88-L127
[ "def", "download_music", "(", "song", ",", "thread_num", "=", "4", ")", ":", "filename", "=", "\"{}.mp3\"", ".", "format", "(", "song", "[", "\"name\"", "]", ")", "if", "os", ".", "path", ".", "exists", "(", "filename", ")", ":", "os", ".", "remove", "(", "filename", ")", "part", "=", "int", "(", "song", "[", "\"size\"", "]", "/", "thread_num", ")", "if", "part", "<=", "1024", ":", "thread_num", "=", "1", "_id", "=", "uuid", ".", "uuid4", "(", ")", ".", "hex", "logger", ".", "info", "(", "\"downloading '{}'...\"", ".", "format", "(", "song", "[", "\"name\"", "]", ")", ")", "threads", "=", "[", "]", "for", "i", "in", "range", "(", "thread_num", ")", ":", "if", "i", "==", "thread_num", "-", "1", ":", "end", "=", "''", "else", ":", "end", "=", "(", "i", "+", "1", ")", "*", "part", "-", "1", "thread", "=", "Worker", "(", "(", "i", "*", "part", ",", "end", ")", ",", "song", ",", "_id", ")", "thread", ".", "start", "(", ")", "threads", ".", "append", "(", "thread", ")", "for", "t", "in", "threads", ":", "t", ".", "join", "(", ")", "fileParts", "=", "glob", ".", "glob", "(", "\"part-{}-*\"", ".", "format", "(", "_id", ")", ")", "fileParts", ".", "sort", "(", "key", "=", "lambda", "e", ":", "e", ".", "split", "(", "'-'", ")", "[", "-", "1", "]", ")", "logger", ".", "info", "(", "\"'{}' combine parts...\"", ".", "format", "(", "song", "[", "\"name\"", "]", ")", ")", "with", "open", "(", "filename", ",", "\"ab\"", ")", "as", "f", ":", "for", "part", "in", "fileParts", ":", "with", "open", "(", "part", ",", "\"rb\"", ")", "as", "d", ":", "shutil", ".", "copyfileobj", "(", "d", ",", "f", ")", "os", ".", "remove", "(", "part", ")", "logger", ".", "info", "(", "\"'{}' finished\"", ".", "format", "(", "song", "[", "\"name\"", "]", ")", ")" ]
bbf87dc01de9a363ae5031e22a5ccc50d506f78a
test
Machine.execute
Execute a code object The inputs and behavior of this function should match those of eval_ and exec_. .. _eval: https://docs.python.org/3/library/functions.html?highlight=eval#eval .. _exec: https://docs.python.org/3/library/functions.html?highlight=exec#exec .. note:: Need to figure out how the internals of this function must change for ``eval`` or ``exec``. :param code: a python code object :param globals_: optional globals dictionary :param _locals: optional locals dictionary
codemach/machine.py
def execute(self, globals_=None, _locals=None): """ Execute a code object The inputs and behavior of this function should match those of eval_ and exec_. .. _eval: https://docs.python.org/3/library/functions.html?highlight=eval#eval .. _exec: https://docs.python.org/3/library/functions.html?highlight=exec#exec .. note:: Need to figure out how the internals of this function must change for ``eval`` or ``exec``. :param code: a python code object :param globals_: optional globals dictionary :param _locals: optional locals dictionary """ if globals_ is None: globals_ = globals() if _locals is None: self._locals = globals_ else: self._locals = _locals self.globals_ = globals_ if self.contains_op("YIELD_VALUE"): return self.iterate_instructions() else: return self.execute_instructions()
def execute(self, globals_=None, _locals=None): """ Execute a code object The inputs and behavior of this function should match those of eval_ and exec_. .. _eval: https://docs.python.org/3/library/functions.html?highlight=eval#eval .. _exec: https://docs.python.org/3/library/functions.html?highlight=exec#exec .. note:: Need to figure out how the internals of this function must change for ``eval`` or ``exec``. :param code: a python code object :param globals_: optional globals dictionary :param _locals: optional locals dictionary """ if globals_ is None: globals_ = globals() if _locals is None: self._locals = globals_ else: self._locals = _locals self.globals_ = globals_ if self.contains_op("YIELD_VALUE"): return self.iterate_instructions() else: return self.execute_instructions()
[ "Execute", "a", "code", "object", "The", "inputs", "and", "behavior", "of", "this", "function", "should", "match", "those", "of", "eval_", "and", "exec_", "." ]
chuck1/codemach
python
https://github.com/chuck1/codemach/blob/b0e02f363da7aa58de7d6ad6499784282958adeb/codemach/machine.py#L262-L291
[ "def", "execute", "(", "self", ",", "globals_", "=", "None", ",", "_locals", "=", "None", ")", ":", "if", "globals_", "is", "None", ":", "globals_", "=", "globals", "(", ")", "if", "_locals", "is", "None", ":", "self", ".", "_locals", "=", "globals_", "else", ":", "self", ".", "_locals", "=", "_locals", "self", ".", "globals_", "=", "globals_", "if", "self", ".", "contains_op", "(", "\"YIELD_VALUE\"", ")", ":", "return", "self", ".", "iterate_instructions", "(", ")", "else", ":", "return", "self", ".", "execute_instructions", "(", ")" ]
b0e02f363da7aa58de7d6ad6499784282958adeb
test
Machine.load_name
Implementation of the LOAD_NAME operation
codemach/machine.py
def load_name(self, name): """ Implementation of the LOAD_NAME operation """ if name in self.globals_: return self.globals_[name] b = self.globals_['__builtins__'] if isinstance(b, dict): return b[name] else: return getattr(b, name)
def load_name(self, name): """ Implementation of the LOAD_NAME operation """ if name in self.globals_: return self.globals_[name] b = self.globals_['__builtins__'] if isinstance(b, dict): return b[name] else: return getattr(b, name)
[ "Implementation", "of", "the", "LOAD_NAME", "operation" ]
chuck1/codemach
python
https://github.com/chuck1/codemach/blob/b0e02f363da7aa58de7d6ad6499784282958adeb/codemach/machine.py#L353-L364
[ "def", "load_name", "(", "self", ",", "name", ")", ":", "if", "name", "in", "self", ".", "globals_", ":", "return", "self", ".", "globals_", "[", "name", "]", "b", "=", "self", ".", "globals_", "[", "'__builtins__'", "]", "if", "isinstance", "(", "b", ",", "dict", ")", ":", "return", "b", "[", "name", "]", "else", ":", "return", "getattr", "(", "b", ",", "name", ")" ]
b0e02f363da7aa58de7d6ad6499784282958adeb
test
Machine.pop
Pop the **n** topmost items from the stack and return them as a ``list``.
codemach/machine.py
def pop(self, n): """ Pop the **n** topmost items from the stack and return them as a ``list``. """ poped = self.__stack[len(self.__stack) - n:] del self.__stack[len(self.__stack) - n:] return poped
def pop(self, n): """ Pop the **n** topmost items from the stack and return them as a ``list``. """ poped = self.__stack[len(self.__stack) - n:] del self.__stack[len(self.__stack) - n:] return poped
[ "Pop", "the", "**", "n", "**", "topmost", "items", "from", "the", "stack", "and", "return", "them", "as", "a", "list", "." ]
chuck1/codemach
python
https://github.com/chuck1/codemach/blob/b0e02f363da7aa58de7d6ad6499784282958adeb/codemach/machine.py#L374-L380
[ "def", "pop", "(", "self", ",", "n", ")", ":", "poped", "=", "self", ".", "__stack", "[", "len", "(", "self", ".", "__stack", ")", "-", "n", ":", "]", "del", "self", ".", "__stack", "[", "len", "(", "self", ".", "__stack", ")", "-", "n", ":", "]", "return", "poped" ]
b0e02f363da7aa58de7d6ad6499784282958adeb
test
Machine.build_class
Implement ``builtins.__build_class__``. We must wrap all class member functions using :py:func:`function_wrapper`. This requires using a :py:class:`Machine` to execute the class source code and then recreating the class source code using an :py:class:`Assembler`. .. note: We might be able to bypass the call to ``builtins.__build_class__`` entirely and manually construct a class object. https://github.com/python/cpython/blob/master/Python/bltinmodule.c
codemach/machine.py
def build_class(self, callable_, args): """ Implement ``builtins.__build_class__``. We must wrap all class member functions using :py:func:`function_wrapper`. This requires using a :py:class:`Machine` to execute the class source code and then recreating the class source code using an :py:class:`Assembler`. .. note: We might be able to bypass the call to ``builtins.__build_class__`` entirely and manually construct a class object. https://github.com/python/cpython/blob/master/Python/bltinmodule.c """ self._print('build_class') self._print(callable_) self._print('args=',args) if isinstance(args[0], FunctionType): c = args[0].get_code() else: c = args[0].__closure__[0].cell_contents.__code__ # execute the original class source code print('execute original class source code') machine = MachineClassSource(c, self.verbose) l = dict() machine.execute(self.globals_, l) # construct new code for class source a = Assembler() for name, value in l.items(): a.load_const(value) a.store_name(name) a.load_const(None) a.return_value() print('new code for class source') dis.dis(a.code()) #machine = Machine(self.verbose) f = types.FunctionType(a.code(), self.globals_, args[1]) args = (f, *args[1:]) self.call_callbacks('CALL_FUNCTION', callable_, *args) return callable_(*args)
def build_class(self, callable_, args): """ Implement ``builtins.__build_class__``. We must wrap all class member functions using :py:func:`function_wrapper`. This requires using a :py:class:`Machine` to execute the class source code and then recreating the class source code using an :py:class:`Assembler`. .. note: We might be able to bypass the call to ``builtins.__build_class__`` entirely and manually construct a class object. https://github.com/python/cpython/blob/master/Python/bltinmodule.c """ self._print('build_class') self._print(callable_) self._print('args=',args) if isinstance(args[0], FunctionType): c = args[0].get_code() else: c = args[0].__closure__[0].cell_contents.__code__ # execute the original class source code print('execute original class source code') machine = MachineClassSource(c, self.verbose) l = dict() machine.execute(self.globals_, l) # construct new code for class source a = Assembler() for name, value in l.items(): a.load_const(value) a.store_name(name) a.load_const(None) a.return_value() print('new code for class source') dis.dis(a.code()) #machine = Machine(self.verbose) f = types.FunctionType(a.code(), self.globals_, args[1]) args = (f, *args[1:]) self.call_callbacks('CALL_FUNCTION', callable_, *args) return callable_(*args)
[ "Implement", "builtins", ".", "__build_class__", ".", "We", "must", "wrap", "all", "class", "member", "functions", "using", ":", "py", ":", "func", ":", "function_wrapper", ".", "This", "requires", "using", "a", ":", "py", ":", "class", ":", "Machine", "to", "execute", "the", "class", "source", "code", "and", "then", "recreating", "the", "class", "source", "code", "using", "an", ":", "py", ":", "class", ":", "Assembler", "." ]
chuck1/codemach
python
https://github.com/chuck1/codemach/blob/b0e02f363da7aa58de7d6ad6499784282958adeb/codemach/machine.py#L385-L431
[ "def", "build_class", "(", "self", ",", "callable_", ",", "args", ")", ":", "self", ".", "_print", "(", "'build_class'", ")", "self", ".", "_print", "(", "callable_", ")", "self", ".", "_print", "(", "'args='", ",", "args", ")", "if", "isinstance", "(", "args", "[", "0", "]", ",", "FunctionType", ")", ":", "c", "=", "args", "[", "0", "]", ".", "get_code", "(", ")", "else", ":", "c", "=", "args", "[", "0", "]", ".", "__closure__", "[", "0", "]", ".", "cell_contents", ".", "__code__", "# execute the original class source code", "print", "(", "'execute original class source code'", ")", "machine", "=", "MachineClassSource", "(", "c", ",", "self", ".", "verbose", ")", "l", "=", "dict", "(", ")", "machine", ".", "execute", "(", "self", ".", "globals_", ",", "l", ")", "# construct new code for class source", "a", "=", "Assembler", "(", ")", "for", "name", ",", "value", "in", "l", ".", "items", "(", ")", ":", "a", ".", "load_const", "(", "value", ")", "a", ".", "store_name", "(", "name", ")", "a", ".", "load_const", "(", "None", ")", "a", ".", "return_value", "(", ")", "print", "(", "'new code for class source'", ")", "dis", ".", "dis", "(", "a", ".", "code", "(", ")", ")", "#machine = Machine(self.verbose)", "f", "=", "types", ".", "FunctionType", "(", "a", ".", "code", "(", ")", ",", "self", ".", "globals_", ",", "args", "[", "1", "]", ")", "args", "=", "(", "f", ",", "*", "args", "[", "1", ":", "]", ")", "self", ".", "call_callbacks", "(", "'CALL_FUNCTION'", ",", "callable_", ",", "*", "args", ")", "return", "callable_", "(", "*", "args", ")" ]
b0e02f363da7aa58de7d6ad6499784282958adeb
test
Machine.call_function
Implement the CALL_FUNCTION_ operation. .. _CALL_FUNCTION: https://docs.python.org/3/library/dis.html#opcode-CALL_FUNCTION
codemach/machine.py
def call_function(self, c, i): """ Implement the CALL_FUNCTION_ operation. .. _CALL_FUNCTION: https://docs.python.org/3/library/dis.html#opcode-CALL_FUNCTION """ callable_ = self.__stack[-1-i.arg] args = tuple(self.__stack[len(self.__stack) - i.arg:]) self._print('call function') self._print('\tfunction ', callable_) self._print('\ti.arg ', i.arg) self._print('\targs ', args) self.call_callbacks('CALL_FUNCTION', callable_, *args) if isinstance(callable_, FunctionType): ret = callable_(*args) elif callable_ is builtins.__build_class__: ret = self.build_class(callable_, args) elif callable_ is builtins.globals: ret = self.builtins_globals() else: ret = callable_(*args) self.pop(1 + i.arg) self.__stack.append(ret)
def call_function(self, c, i): """ Implement the CALL_FUNCTION_ operation. .. _CALL_FUNCTION: https://docs.python.org/3/library/dis.html#opcode-CALL_FUNCTION """ callable_ = self.__stack[-1-i.arg] args = tuple(self.__stack[len(self.__stack) - i.arg:]) self._print('call function') self._print('\tfunction ', callable_) self._print('\ti.arg ', i.arg) self._print('\targs ', args) self.call_callbacks('CALL_FUNCTION', callable_, *args) if isinstance(callable_, FunctionType): ret = callable_(*args) elif callable_ is builtins.__build_class__: ret = self.build_class(callable_, args) elif callable_ is builtins.globals: ret = self.builtins_globals() else: ret = callable_(*args) self.pop(1 + i.arg) self.__stack.append(ret)
[ "Implement", "the", "CALL_FUNCTION_", "operation", "." ]
chuck1/codemach
python
https://github.com/chuck1/codemach/blob/b0e02f363da7aa58de7d6ad6499784282958adeb/codemach/machine.py#L433-L464
[ "def", "call_function", "(", "self", ",", "c", ",", "i", ")", ":", "callable_", "=", "self", ".", "__stack", "[", "-", "1", "-", "i", ".", "arg", "]", "args", "=", "tuple", "(", "self", ".", "__stack", "[", "len", "(", "self", ".", "__stack", ")", "-", "i", ".", "arg", ":", "]", ")", "self", ".", "_print", "(", "'call function'", ")", "self", ".", "_print", "(", "'\\tfunction '", ",", "callable_", ")", "self", ".", "_print", "(", "'\\ti.arg '", ",", "i", ".", "arg", ")", "self", ".", "_print", "(", "'\\targs '", ",", "args", ")", "self", ".", "call_callbacks", "(", "'CALL_FUNCTION'", ",", "callable_", ",", "*", "args", ")", "if", "isinstance", "(", "callable_", ",", "FunctionType", ")", ":", "ret", "=", "callable_", "(", "*", "args", ")", "elif", "callable_", "is", "builtins", ".", "__build_class__", ":", "ret", "=", "self", ".", "build_class", "(", "callable_", ",", "args", ")", "elif", "callable_", "is", "builtins", ".", "globals", ":", "ret", "=", "self", ".", "builtins_globals", "(", ")", "else", ":", "ret", "=", "callable_", "(", "*", "args", ")", "self", ".", "pop", "(", "1", "+", "i", ".", "arg", ")", "self", ".", "__stack", ".", "append", "(", "ret", ")" ]
b0e02f363da7aa58de7d6ad6499784282958adeb
test
dump
Perfoms a mysqldump backup. Create a database dump for the given database. returns statuscode and shelloutput
pyque/db/mysql.py
def dump(filename, dbname, username=None, password=None, host=None, port=None, tempdir='/tmp', mysqldump_path='mysqldump'): """Perfoms a mysqldump backup. Create a database dump for the given database. returns statuscode and shelloutput """ filepath = os.path.join(tempdir, filename) cmd = mysqldump_path cmd += ' --result-file=' + os.path.join(tempdir, filename) if username: cmd += ' --user=%s' % username if host: cmd += ' --host=%s' % host if port: cmd += ' --port=%s' % port if password: cmd += ' --password=%s' % password cmd += ' ' + dbname ## run mysqldump return sh(cmd)
def dump(filename, dbname, username=None, password=None, host=None, port=None, tempdir='/tmp', mysqldump_path='mysqldump'): """Perfoms a mysqldump backup. Create a database dump for the given database. returns statuscode and shelloutput """ filepath = os.path.join(tempdir, filename) cmd = mysqldump_path cmd += ' --result-file=' + os.path.join(tempdir, filename) if username: cmd += ' --user=%s' % username if host: cmd += ' --host=%s' % host if port: cmd += ' --port=%s' % port if password: cmd += ' --password=%s' % password cmd += ' ' + dbname ## run mysqldump return sh(cmd)
[ "Perfoms", "a", "mysqldump", "backup", ".", "Create", "a", "database", "dump", "for", "the", "given", "database", ".", "returns", "statuscode", "and", "shelloutput" ]
bmaeser/pyque
python
https://github.com/bmaeser/pyque/blob/856dceab8d89cf3771cf21e682466c29a85ae8eb/pyque/db/mysql.py#L11-L35
[ "def", "dump", "(", "filename", ",", "dbname", ",", "username", "=", "None", ",", "password", "=", "None", ",", "host", "=", "None", ",", "port", "=", "None", ",", "tempdir", "=", "'/tmp'", ",", "mysqldump_path", "=", "'mysqldump'", ")", ":", "filepath", "=", "os", ".", "path", ".", "join", "(", "tempdir", ",", "filename", ")", "cmd", "=", "mysqldump_path", "cmd", "+=", "' --result-file='", "+", "os", ".", "path", ".", "join", "(", "tempdir", ",", "filename", ")", "if", "username", ":", "cmd", "+=", "' --user=%s'", "%", "username", "if", "host", ":", "cmd", "+=", "' --host=%s'", "%", "host", "if", "port", ":", "cmd", "+=", "' --port=%s'", "%", "port", "if", "password", ":", "cmd", "+=", "' --password=%s'", "%", "password", "cmd", "+=", "' '", "+", "dbname", "## run mysqldump", "return", "sh", "(", "cmd", ")" ]
856dceab8d89cf3771cf21e682466c29a85ae8eb
test
_connection
returns a connected cursor to the database-server.
pyque/db/mysql.py
def _connection(username=None, password=None, host=None, port=None): "returns a connected cursor to the database-server." c_opts = {} if username: c_opts['user'] = username if password: c_opts['passwd'] = password if host: c_opts['host'] = host if port: c_opts['port'] = port dbc = MySQLdb.connect(**c_opts) dbc.autocommit(True) return dbc
def _connection(username=None, password=None, host=None, port=None): "returns a connected cursor to the database-server." c_opts = {} if username: c_opts['user'] = username if password: c_opts['passwd'] = password if host: c_opts['host'] = host if port: c_opts['port'] = port dbc = MySQLdb.connect(**c_opts) dbc.autocommit(True) return dbc
[ "returns", "a", "connected", "cursor", "to", "the", "database", "-", "server", "." ]
bmaeser/pyque
python
https://github.com/bmaeser/pyque/blob/856dceab8d89cf3771cf21e682466c29a85ae8eb/pyque/db/mysql.py#L37-L49
[ "def", "_connection", "(", "username", "=", "None", ",", "password", "=", "None", ",", "host", "=", "None", ",", "port", "=", "None", ")", ":", "c_opts", "=", "{", "}", "if", "username", ":", "c_opts", "[", "'user'", "]", "=", "username", "if", "password", ":", "c_opts", "[", "'passwd'", "]", "=", "password", "if", "host", ":", "c_opts", "[", "'host'", "]", "=", "host", "if", "port", ":", "c_opts", "[", "'port'", "]", "=", "port", "dbc", "=", "MySQLdb", ".", "connect", "(", "*", "*", "c_opts", ")", "dbc", ".", "autocommit", "(", "True", ")", "return", "dbc" ]
856dceab8d89cf3771cf21e682466c29a85ae8eb
test
render_ditaa
Render ditaa code into a PNG output file.
docs/_exts/ditaa.py
def render_ditaa(self, code, options, prefix='ditaa'): """Render ditaa code into a PNG output file.""" hashkey = code.encode('utf-8') + str(options) + \ str(self.builder.config.ditaa) + \ str(self.builder.config.ditaa_args) infname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), "ditaa") outfname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), "png") inrelfn = posixpath.join(self.builder.imgpath, infname) infullfn = path.join(self.builder.outdir, '_images', infname) outrelfn = posixpath.join(self.builder.imgpath, outfname) outfullfn = path.join(self.builder.outdir, '_images', outfname) if path.isfile(outfullfn): return outrelfn, outfullfn ensuredir(path.dirname(outfullfn)) # ditaa expects UTF-8 by default if isinstance(code, unicode): code = code.encode('utf-8') ditaa_args = [self.builder.config.ditaa] ditaa_args.extend(self.builder.config.ditaa_args) ditaa_args.extend(options) ditaa_args.extend( [infullfn] ) ditaa_args.extend( [outfullfn] ) f = open(infullfn, 'w') f.write(code) f.close() try: self.builder.warn(ditaa_args) p = Popen(ditaa_args, stdout=PIPE, stdin=PIPE, stderr=PIPE) except OSError, err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn('ditaa command %r cannot be run (needed for ditaa ' 'output), check the ditaa setting' % self.builder.config.ditaa) self.builder._ditaa_warned_dot = True return None, None wentWrong = False try: # Ditaa may close standard input when an error occurs, # resulting in a broken pipe on communicate() stdout, stderr = p.communicate(code) except OSError, err: if err.errno != EPIPE: raise wentWrong = True except IOError, err: if err.errno != EINVAL: raise wentWrong = True if wentWrong: # in this case, read the standard output and standard error streams # directly, to get the error message(s) stdout, stderr = p.stdout.read(), p.stderr.read() p.wait() if p.returncode != 0: raise DitaaError('ditaa exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) return outrelfn, outfullfn
def render_ditaa(self, code, options, prefix='ditaa'): """Render ditaa code into a PNG output file.""" hashkey = code.encode('utf-8') + str(options) + \ str(self.builder.config.ditaa) + \ str(self.builder.config.ditaa_args) infname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), "ditaa") outfname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), "png") inrelfn = posixpath.join(self.builder.imgpath, infname) infullfn = path.join(self.builder.outdir, '_images', infname) outrelfn = posixpath.join(self.builder.imgpath, outfname) outfullfn = path.join(self.builder.outdir, '_images', outfname) if path.isfile(outfullfn): return outrelfn, outfullfn ensuredir(path.dirname(outfullfn)) # ditaa expects UTF-8 by default if isinstance(code, unicode): code = code.encode('utf-8') ditaa_args = [self.builder.config.ditaa] ditaa_args.extend(self.builder.config.ditaa_args) ditaa_args.extend(options) ditaa_args.extend( [infullfn] ) ditaa_args.extend( [outfullfn] ) f = open(infullfn, 'w') f.write(code) f.close() try: self.builder.warn(ditaa_args) p = Popen(ditaa_args, stdout=PIPE, stdin=PIPE, stderr=PIPE) except OSError, err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn('ditaa command %r cannot be run (needed for ditaa ' 'output), check the ditaa setting' % self.builder.config.ditaa) self.builder._ditaa_warned_dot = True return None, None wentWrong = False try: # Ditaa may close standard input when an error occurs, # resulting in a broken pipe on communicate() stdout, stderr = p.communicate(code) except OSError, err: if err.errno != EPIPE: raise wentWrong = True except IOError, err: if err.errno != EINVAL: raise wentWrong = True if wentWrong: # in this case, read the standard output and standard error streams # directly, to get the error message(s) stdout, stderr = p.stdout.read(), p.stderr.read() p.wait() if p.returncode != 0: raise DitaaError('ditaa exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) return outrelfn, outfullfn
[ "Render", "ditaa", "code", "into", "a", "PNG", "output", "file", "." ]
lvh/txampext
python
https://github.com/lvh/txampext/blob/a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9/docs/_exts/ditaa.py#L96-L160
[ "def", "render_ditaa", "(", "self", ",", "code", ",", "options", ",", "prefix", "=", "'ditaa'", ")", ":", "hashkey", "=", "code", ".", "encode", "(", "'utf-8'", ")", "+", "str", "(", "options", ")", "+", "str", "(", "self", ".", "builder", ".", "config", ".", "ditaa", ")", "+", "str", "(", "self", ".", "builder", ".", "config", ".", "ditaa_args", ")", "infname", "=", "'%s-%s.%s'", "%", "(", "prefix", ",", "sha", "(", "hashkey", ")", ".", "hexdigest", "(", ")", ",", "\"ditaa\"", ")", "outfname", "=", "'%s-%s.%s'", "%", "(", "prefix", ",", "sha", "(", "hashkey", ")", ".", "hexdigest", "(", ")", ",", "\"png\"", ")", "inrelfn", "=", "posixpath", ".", "join", "(", "self", ".", "builder", ".", "imgpath", ",", "infname", ")", "infullfn", "=", "path", ".", "join", "(", "self", ".", "builder", ".", "outdir", ",", "'_images'", ",", "infname", ")", "outrelfn", "=", "posixpath", ".", "join", "(", "self", ".", "builder", ".", "imgpath", ",", "outfname", ")", "outfullfn", "=", "path", ".", "join", "(", "self", ".", "builder", ".", "outdir", ",", "'_images'", ",", "outfname", ")", "if", "path", ".", "isfile", "(", "outfullfn", ")", ":", "return", "outrelfn", ",", "outfullfn", "ensuredir", "(", "path", ".", "dirname", "(", "outfullfn", ")", ")", "# ditaa expects UTF-8 by default", "if", "isinstance", "(", "code", ",", "unicode", ")", ":", "code", "=", "code", ".", "encode", "(", "'utf-8'", ")", "ditaa_args", "=", "[", "self", ".", "builder", ".", "config", ".", "ditaa", "]", "ditaa_args", ".", "extend", "(", "self", ".", "builder", ".", "config", ".", "ditaa_args", ")", "ditaa_args", ".", "extend", "(", "options", ")", "ditaa_args", ".", "extend", "(", "[", "infullfn", "]", ")", "ditaa_args", ".", "extend", "(", "[", "outfullfn", "]", ")", "f", "=", "open", "(", "infullfn", ",", "'w'", ")", "f", ".", "write", "(", "code", ")", "f", ".", "close", "(", ")", "try", ":", "self", ".", "builder", ".", "warn", "(", "ditaa_args", ")", "p", "=", "Popen", "(", "ditaa_args", ",", "stdout", "=", "PIPE", ",", "stdin", "=", "PIPE", ",", "stderr", "=", "PIPE", ")", "except", "OSError", ",", "err", ":", "if", "err", ".", "errno", "!=", "ENOENT", ":", "# No such file or directory", "raise", "self", ".", "builder", ".", "warn", "(", "'ditaa command %r cannot be run (needed for ditaa '", "'output), check the ditaa setting'", "%", "self", ".", "builder", ".", "config", ".", "ditaa", ")", "self", ".", "builder", ".", "_ditaa_warned_dot", "=", "True", "return", "None", ",", "None", "wentWrong", "=", "False", "try", ":", "# Ditaa may close standard input when an error occurs,", "# resulting in a broken pipe on communicate()", "stdout", ",", "stderr", "=", "p", ".", "communicate", "(", "code", ")", "except", "OSError", ",", "err", ":", "if", "err", ".", "errno", "!=", "EPIPE", ":", "raise", "wentWrong", "=", "True", "except", "IOError", ",", "err", ":", "if", "err", ".", "errno", "!=", "EINVAL", ":", "raise", "wentWrong", "=", "True", "if", "wentWrong", ":", "# in this case, read the standard output and standard error streams", "# directly, to get the error message(s)", "stdout", ",", "stderr", "=", "p", ".", "stdout", ".", "read", "(", ")", ",", "p", ".", "stderr", ".", "read", "(", ")", "p", ".", "wait", "(", ")", "if", "p", ".", "returncode", "!=", "0", ":", "raise", "DitaaError", "(", "'ditaa exited with error:\\n[stderr]\\n%s\\n'", "'[stdout]\\n%s'", "%", "(", "stderr", ",", "stdout", ")", ")", "return", "outrelfn", ",", "outfullfn" ]
a7d6cb9f1e9200dba597378cd40eb6a2096d4fd9
test
Application._atexit
Invoked in the 'finally' block of Application.run.
nicfit/app.py
def _atexit(self): """Invoked in the 'finally' block of Application.run.""" self.log.debug("Application._atexit") if self._atexit_func: self._atexit_func(self)
def _atexit(self): """Invoked in the 'finally' block of Application.run.""" self.log.debug("Application._atexit") if self._atexit_func: self._atexit_func(self)
[ "Invoked", "in", "the", "finally", "block", "of", "Application", ".", "run", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/app.py#L59-L63
[ "def", "_atexit", "(", "self", ")", ":", "self", ".", "log", ".", "debug", "(", "\"Application._atexit\"", ")", "if", "self", ".", "_atexit_func", ":", "self", ".", "_atexit_func", "(", "self", ")" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
Application.run
Run Application.main and exits with the return value.
nicfit/app.py
def run(self, args_list=None): """Run Application.main and exits with the return value.""" self.log.debug("Application.run: {args_list}".format(**locals())) retval = None try: retval = self._run(args_list=args_list) except KeyboardInterrupt: self.log.verbose("Interrupted") # pragma: nocover except SystemExit as exit: self.log.verbose("Exited") retval = exit.code except Exception: print("Uncaught exception", file=sys.stderr) traceback.print_exc() if "debug_pdb" in self.args and self.args.debug_pdb: debugger() retval = Application.UNCAUGHT_EXCEPTION_EXIT raise finally: try: self._atexit() finally: sys.stderr.flush() sys.stdout.flush() sys.exit(retval)
def run(self, args_list=None): """Run Application.main and exits with the return value.""" self.log.debug("Application.run: {args_list}".format(**locals())) retval = None try: retval = self._run(args_list=args_list) except KeyboardInterrupt: self.log.verbose("Interrupted") # pragma: nocover except SystemExit as exit: self.log.verbose("Exited") retval = exit.code except Exception: print("Uncaught exception", file=sys.stderr) traceback.print_exc() if "debug_pdb" in self.args and self.args.debug_pdb: debugger() retval = Application.UNCAUGHT_EXCEPTION_EXIT raise finally: try: self._atexit() finally: sys.stderr.flush() sys.stdout.flush() sys.exit(retval)
[ "Run", "Application", ".", "main", "and", "exits", "with", "the", "return", "value", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/app.py#L74-L98
[ "def", "run", "(", "self", ",", "args_list", "=", "None", ")", ":", "self", ".", "log", ".", "debug", "(", "\"Application.run: {args_list}\"", ".", "format", "(", "*", "*", "locals", "(", ")", ")", ")", "retval", "=", "None", "try", ":", "retval", "=", "self", ".", "_run", "(", "args_list", "=", "args_list", ")", "except", "KeyboardInterrupt", ":", "self", ".", "log", ".", "verbose", "(", "\"Interrupted\"", ")", "# pragma: nocover", "except", "SystemExit", "as", "exit", ":", "self", ".", "log", ".", "verbose", "(", "\"Exited\"", ")", "retval", "=", "exit", ".", "code", "except", "Exception", ":", "print", "(", "\"Uncaught exception\"", ",", "file", "=", "sys", ".", "stderr", ")", "traceback", ".", "print_exc", "(", ")", "if", "\"debug_pdb\"", "in", "self", ".", "args", "and", "self", ".", "args", ".", "debug_pdb", ":", "debugger", "(", ")", "retval", "=", "Application", ".", "UNCAUGHT_EXCEPTION_EXIT", "raise", "finally", ":", "try", ":", "self", ".", "_atexit", "(", ")", "finally", ":", "sys", ".", "stderr", ".", "flush", "(", ")", "sys", ".", "stdout", ".", "flush", "(", ")", "sys", ".", "exit", "(", "retval", ")" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
cd
Context manager that changes to directory `path` and return to CWD when exited.
nicfit/util.py
def cd(path): """Context manager that changes to directory `path` and return to CWD when exited. """ old_path = os.getcwd() os.chdir(path) try: yield finally: os.chdir(old_path)
def cd(path): """Context manager that changes to directory `path` and return to CWD when exited. """ old_path = os.getcwd() os.chdir(path) try: yield finally: os.chdir(old_path)
[ "Context", "manager", "that", "changes", "to", "directory", "path", "and", "return", "to", "CWD", "when", "exited", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/util.py#L16-L25
[ "def", "cd", "(", "path", ")", ":", "old_path", "=", "os", ".", "getcwd", "(", ")", "os", ".", "chdir", "(", "path", ")", "try", ":", "yield", "finally", ":", "os", ".", "chdir", "(", "old_path", ")" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
copytree
Modified from shutil.copytree docs code sample, merges files rather than requiring dst to not exist.
nicfit/util.py
def copytree(src, dst, symlinks=True): """ Modified from shutil.copytree docs code sample, merges files rather than requiring dst to not exist. """ from shutil import copy2, Error, copystat names = os.listdir(src) if not Path(dst).exists(): os.makedirs(dst) errors = [] for name in names: srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if symlinks and os.path.islink(srcname): linkto = os.readlink(srcname) os.symlink(linkto, dstname) elif os.path.isdir(srcname): copytree(srcname, dstname, symlinks) else: copy2(srcname, dstname) # XXX What about devices, sockets etc.? except OSError as why: errors.append((srcname, dstname, str(why))) # catch the Error from the recursive copytree so that we can # continue with other files except Error as err: errors.extend(err.args[0]) try: copystat(src, dst) except OSError as why: # can't copy file access times on Windows if why.winerror is None: errors.extend((src, dst, str(why))) if errors: raise Error(errors)
def copytree(src, dst, symlinks=True): """ Modified from shutil.copytree docs code sample, merges files rather than requiring dst to not exist. """ from shutil import copy2, Error, copystat names = os.listdir(src) if not Path(dst).exists(): os.makedirs(dst) errors = [] for name in names: srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if symlinks and os.path.islink(srcname): linkto = os.readlink(srcname) os.symlink(linkto, dstname) elif os.path.isdir(srcname): copytree(srcname, dstname, symlinks) else: copy2(srcname, dstname) # XXX What about devices, sockets etc.? except OSError as why: errors.append((srcname, dstname, str(why))) # catch the Error from the recursive copytree so that we can # continue with other files except Error as err: errors.extend(err.args[0]) try: copystat(src, dst) except OSError as why: # can't copy file access times on Windows if why.winerror is None: errors.extend((src, dst, str(why))) if errors: raise Error(errors)
[ "Modified", "from", "shutil", ".", "copytree", "docs", "code", "sample", "merges", "files", "rather", "than", "requiring", "dst", "to", "not", "exist", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/util.py#L28-L66
[ "def", "copytree", "(", "src", ",", "dst", ",", "symlinks", "=", "True", ")", ":", "from", "shutil", "import", "copy2", ",", "Error", ",", "copystat", "names", "=", "os", ".", "listdir", "(", "src", ")", "if", "not", "Path", "(", "dst", ")", ".", "exists", "(", ")", ":", "os", ".", "makedirs", "(", "dst", ")", "errors", "=", "[", "]", "for", "name", "in", "names", ":", "srcname", "=", "os", ".", "path", ".", "join", "(", "src", ",", "name", ")", "dstname", "=", "os", ".", "path", ".", "join", "(", "dst", ",", "name", ")", "try", ":", "if", "symlinks", "and", "os", ".", "path", ".", "islink", "(", "srcname", ")", ":", "linkto", "=", "os", ".", "readlink", "(", "srcname", ")", "os", ".", "symlink", "(", "linkto", ",", "dstname", ")", "elif", "os", ".", "path", ".", "isdir", "(", "srcname", ")", ":", "copytree", "(", "srcname", ",", "dstname", ",", "symlinks", ")", "else", ":", "copy2", "(", "srcname", ",", "dstname", ")", "# XXX What about devices, sockets etc.?", "except", "OSError", "as", "why", ":", "errors", ".", "append", "(", "(", "srcname", ",", "dstname", ",", "str", "(", "why", ")", ")", ")", "# catch the Error from the recursive copytree so that we can", "# continue with other files", "except", "Error", "as", "err", ":", "errors", ".", "extend", "(", "err", ".", "args", "[", "0", "]", ")", "try", ":", "copystat", "(", "src", ",", "dst", ")", "except", "OSError", "as", "why", ":", "# can't copy file access times on Windows", "if", "why", ".", "winerror", "is", "None", ":", "errors", ".", "extend", "(", "(", "src", ",", "dst", ",", "str", "(", "why", ")", ")", ")", "if", "errors", ":", "raise", "Error", "(", "errors", ")" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
debugger
If called in the context of an exception, calls post_mortem; otherwise set_trace. ``ipdb`` is preferred over ``pdb`` if installed.
nicfit/util.py
def debugger(): """If called in the context of an exception, calls post_mortem; otherwise set_trace. ``ipdb`` is preferred over ``pdb`` if installed. """ e, m, tb = sys.exc_info() if tb is not None: _debugger.post_mortem(tb) else: _debugger.set_trace()
def debugger(): """If called in the context of an exception, calls post_mortem; otherwise set_trace. ``ipdb`` is preferred over ``pdb`` if installed. """ e, m, tb = sys.exc_info() if tb is not None: _debugger.post_mortem(tb) else: _debugger.set_trace()
[ "If", "called", "in", "the", "context", "of", "an", "exception", "calls", "post_mortem", ";", "otherwise", "set_trace", ".", "ipdb", "is", "preferred", "over", "pdb", "if", "installed", "." ]
nicfit/nicfit.py
python
https://github.com/nicfit/nicfit.py/blob/8313f8edbc5e7361ddad496d6d818324b5236c7a/nicfit/util.py#L95-L104
[ "def", "debugger", "(", ")", ":", "e", ",", "m", ",", "tb", "=", "sys", ".", "exc_info", "(", ")", "if", "tb", "is", "not", "None", ":", "_debugger", ".", "post_mortem", "(", "tb", ")", "else", ":", "_debugger", ".", "set_trace", "(", ")" ]
8313f8edbc5e7361ddad496d6d818324b5236c7a
test
FileSystem.keys
Implements the dict.keys() method
src/fedoidcmsg/file_system.py
def keys(self): """ Implements the dict.keys() method """ self.sync() for k in self.db.keys(): try: yield self.key_conv['from'](k) except KeyError: yield k
def keys(self): """ Implements the dict.keys() method """ self.sync() for k in self.db.keys(): try: yield self.key_conv['from'](k) except KeyError: yield k
[ "Implements", "the", "dict", ".", "keys", "()", "method" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/file_system.py#L99-L108
[ "def", "keys", "(", "self", ")", ":", "self", ".", "sync", "(", ")", "for", "k", "in", "self", ".", "db", ".", "keys", "(", ")", ":", "try", ":", "yield", "self", ".", "key_conv", "[", "'from'", "]", "(", "k", ")", "except", "KeyError", ":", "yield", "k" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
FileSystem.get_mtime
Find the time this file was last modified. :param fname: File name :return: The last time the file was modified.
src/fedoidcmsg/file_system.py
def get_mtime(fname): """ Find the time this file was last modified. :param fname: File name :return: The last time the file was modified. """ try: mtime = os.stat(fname).st_mtime_ns except OSError: # The file might be right in the middle of being written # so sleep time.sleep(1) mtime = os.stat(fname).st_mtime_ns return mtime
def get_mtime(fname): """ Find the time this file was last modified. :param fname: File name :return: The last time the file was modified. """ try: mtime = os.stat(fname).st_mtime_ns except OSError: # The file might be right in the middle of being written # so sleep time.sleep(1) mtime = os.stat(fname).st_mtime_ns return mtime
[ "Find", "the", "time", "this", "file", "was", "last", "modified", "." ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/file_system.py#L111-L126
[ "def", "get_mtime", "(", "fname", ")", ":", "try", ":", "mtime", "=", "os", ".", "stat", "(", "fname", ")", ".", "st_mtime_ns", "except", "OSError", ":", "# The file might be right in the middle of being written", "# so sleep", "time", ".", "sleep", "(", "1", ")", "mtime", "=", "os", ".", "stat", "(", "fname", ")", ".", "st_mtime_ns", "return", "mtime" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
FileSystem.is_changed
Find out if this item has been modified since last :param item: A key :return: True/False
src/fedoidcmsg/file_system.py
def is_changed(self, item): """ Find out if this item has been modified since last :param item: A key :return: True/False """ fname = os.path.join(self.fdir, item) if os.path.isfile(fname): mtime = self.get_mtime(fname) try: _ftime = self.fmtime[item] except KeyError: # Never been seen before self.fmtime[item] = mtime return True if mtime > _ftime: # has changed self.fmtime[item] = mtime return True else: return False else: logger.error('Could not access {}'.format(fname)) raise KeyError(item)
def is_changed(self, item): """ Find out if this item has been modified since last :param item: A key :return: True/False """ fname = os.path.join(self.fdir, item) if os.path.isfile(fname): mtime = self.get_mtime(fname) try: _ftime = self.fmtime[item] except KeyError: # Never been seen before self.fmtime[item] = mtime return True if mtime > _ftime: # has changed self.fmtime[item] = mtime return True else: return False else: logger.error('Could not access {}'.format(fname)) raise KeyError(item)
[ "Find", "out", "if", "this", "item", "has", "been", "modified", "since", "last" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/file_system.py#L128-L152
[ "def", "is_changed", "(", "self", ",", "item", ")", ":", "fname", "=", "os", ".", "path", ".", "join", "(", "self", ".", "fdir", ",", "item", ")", "if", "os", ".", "path", ".", "isfile", "(", "fname", ")", ":", "mtime", "=", "self", ".", "get_mtime", "(", "fname", ")", "try", ":", "_ftime", "=", "self", ".", "fmtime", "[", "item", "]", "except", "KeyError", ":", "# Never been seen before", "self", ".", "fmtime", "[", "item", "]", "=", "mtime", "return", "True", "if", "mtime", ">", "_ftime", ":", "# has changed", "self", ".", "fmtime", "[", "item", "]", "=", "mtime", "return", "True", "else", ":", "return", "False", "else", ":", "logger", ".", "error", "(", "'Could not access {}'", ".", "format", "(", "fname", ")", ")", "raise", "KeyError", "(", "item", ")" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
FileSystem.sync
Goes through the directory and builds a local cache based on the content of the directory.
src/fedoidcmsg/file_system.py
def sync(self): """ Goes through the directory and builds a local cache based on the content of the directory. """ if not os.path.isdir(self.fdir): os.makedirs(self.fdir) for f in os.listdir(self.fdir): fname = os.path.join(self.fdir, f) if not os.path.isfile(fname): continue if f in self.fmtime: if self.is_changed(f): self.db[f] = self._read_info(fname) else: mtime = self.get_mtime(fname) self.db[f] = self._read_info(fname) self.fmtime[f] = mtime
def sync(self): """ Goes through the directory and builds a local cache based on the content of the directory. """ if not os.path.isdir(self.fdir): os.makedirs(self.fdir) for f in os.listdir(self.fdir): fname = os.path.join(self.fdir, f) if not os.path.isfile(fname): continue if f in self.fmtime: if self.is_changed(f): self.db[f] = self._read_info(fname) else: mtime = self.get_mtime(fname) self.db[f] = self._read_info(fname) self.fmtime[f] = mtime
[ "Goes", "through", "the", "directory", "and", "builds", "a", "local", "cache", "based", "on", "the", "content", "of", "the", "directory", "." ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/file_system.py#L170-L188
[ "def", "sync", "(", "self", ")", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "self", ".", "fdir", ")", ":", "os", ".", "makedirs", "(", "self", ".", "fdir", ")", "for", "f", "in", "os", ".", "listdir", "(", "self", ".", "fdir", ")", ":", "fname", "=", "os", ".", "path", ".", "join", "(", "self", ".", "fdir", ",", "f", ")", "if", "not", "os", ".", "path", ".", "isfile", "(", "fname", ")", ":", "continue", "if", "f", "in", "self", ".", "fmtime", ":", "if", "self", ".", "is_changed", "(", "f", ")", ":", "self", ".", "db", "[", "f", "]", "=", "self", ".", "_read_info", "(", "fname", ")", "else", ":", "mtime", "=", "self", ".", "get_mtime", "(", "fname", ")", "self", ".", "db", "[", "f", "]", "=", "self", ".", "_read_info", "(", "fname", ")", "self", ".", "fmtime", "[", "f", "]", "=", "mtime" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
FileSystem.items
Implements the dict.items() method
src/fedoidcmsg/file_system.py
def items(self): """ Implements the dict.items() method """ self.sync() for k, v in self.db.items(): try: yield self.key_conv['from'](k), v except KeyError: yield k, v
def items(self): """ Implements the dict.items() method """ self.sync() for k, v in self.db.items(): try: yield self.key_conv['from'](k), v except KeyError: yield k, v
[ "Implements", "the", "dict", ".", "items", "()", "method" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/file_system.py#L190-L199
[ "def", "items", "(", "self", ")", ":", "self", ".", "sync", "(", ")", "for", "k", ",", "v", "in", "self", ".", "db", ".", "items", "(", ")", ":", "try", ":", "yield", "self", ".", "key_conv", "[", "'from'", "]", "(", "k", ")", ",", "v", "except", "KeyError", ":", "yield", "k", ",", "v" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
FileSystem.clear
Completely resets the database. This means that all information in the local cache and on disc will be erased.
src/fedoidcmsg/file_system.py
def clear(self): """ Completely resets the database. This means that all information in the local cache and on disc will be erased. """ if not os.path.isdir(self.fdir): os.makedirs(self.fdir, exist_ok=True) return for f in os.listdir(self.fdir): del self[f]
def clear(self): """ Completely resets the database. This means that all information in the local cache and on disc will be erased. """ if not os.path.isdir(self.fdir): os.makedirs(self.fdir, exist_ok=True) return for f in os.listdir(self.fdir): del self[f]
[ "Completely", "resets", "the", "database", ".", "This", "means", "that", "all", "information", "in", "the", "local", "cache", "and", "on", "disc", "will", "be", "erased", "." ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/file_system.py#L201-L211
[ "def", "clear", "(", "self", ")", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "self", ".", "fdir", ")", ":", "os", ".", "makedirs", "(", "self", ".", "fdir", ",", "exist_ok", "=", "True", ")", "return", "for", "f", "in", "os", ".", "listdir", "(", "self", ".", "fdir", ")", ":", "del", "self", "[", "f", "]" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
FileSystem.update
Implements the dict.update() method
src/fedoidcmsg/file_system.py
def update(self, ava): """ Implements the dict.update() method """ for key, val in ava.items(): self[key] = val
def update(self, ava): """ Implements the dict.update() method """ for key, val in ava.items(): self[key] = val
[ "Implements", "the", "dict", ".", "update", "()", "method" ]
IdentityPython/fedoidcmsg
python
https://github.com/IdentityPython/fedoidcmsg/blob/d30107be02521fa6cdfe285da3b6b0cdd153c8cc/src/fedoidcmsg/file_system.py#L213-L218
[ "def", "update", "(", "self", ",", "ava", ")", ":", "for", "key", ",", "val", "in", "ava", ".", "items", "(", ")", ":", "self", "[", "key", "]", "=", "val" ]
d30107be02521fa6cdfe285da3b6b0cdd153c8cc
test
chr
x-->int / byte Returns-->BYTE (not str in python3) Behaves like PY2 chr() in PY2 or PY3 if x is str of length > 1 or int > 256 raises ValueError/TypeError is not SUPPRESS_ERRORS
cffi_utils/py2to3.py
def chr(x): ''' x-->int / byte Returns-->BYTE (not str in python3) Behaves like PY2 chr() in PY2 or PY3 if x is str of length > 1 or int > 256 raises ValueError/TypeError is not SUPPRESS_ERRORS ''' global _chr if isinstance(x, int): if x > 256: if SUPPRESS_ERRORS: x = x % 256 return toBytes(_chr(x)) elif isinstance(x, bytes): x = fromBytes(x) if len(x) > 1: if not SUPPRESS_ERRORS: raise TypeError('chr() found string of length > 2') x = x[0] return toBytes(x) elif isinstance(x, str): if len(x) > 1: if not SUPPRESS_ERRORS: raise TypeError('chr() found string of length > 2') x = x[0] return toBytes(x) else: raise TypeError('Unknown type passed to chr: %s', str(type(x)))
def chr(x): ''' x-->int / byte Returns-->BYTE (not str in python3) Behaves like PY2 chr() in PY2 or PY3 if x is str of length > 1 or int > 256 raises ValueError/TypeError is not SUPPRESS_ERRORS ''' global _chr if isinstance(x, int): if x > 256: if SUPPRESS_ERRORS: x = x % 256 return toBytes(_chr(x)) elif isinstance(x, bytes): x = fromBytes(x) if len(x) > 1: if not SUPPRESS_ERRORS: raise TypeError('chr() found string of length > 2') x = x[0] return toBytes(x) elif isinstance(x, str): if len(x) > 1: if not SUPPRESS_ERRORS: raise TypeError('chr() found string of length > 2') x = x[0] return toBytes(x) else: raise TypeError('Unknown type passed to chr: %s', str(type(x)))
[ "x", "--", ">", "int", "/", "byte", "Returns", "--", ">", "BYTE", "(", "not", "str", "in", "python3", ")", "Behaves", "like", "PY2", "chr", "()", "in", "PY2", "or", "PY3", "if", "x", "is", "str", "of", "length", ">", "1", "or", "int", ">", "256", "raises", "ValueError", "/", "TypeError", "is", "not", "SUPPRESS_ERRORS" ]
sundarnagarajan/cffi_utils
python
https://github.com/sundarnagarajan/cffi_utils/blob/1d5ab2d2fcb962372228033106bc23f1d73d31fa/cffi_utils/py2to3.py#L121-L149
[ "def", "chr", "(", "x", ")", ":", "global", "_chr", "if", "isinstance", "(", "x", ",", "int", ")", ":", "if", "x", ">", "256", ":", "if", "SUPPRESS_ERRORS", ":", "x", "=", "x", "%", "256", "return", "toBytes", "(", "_chr", "(", "x", ")", ")", "elif", "isinstance", "(", "x", ",", "bytes", ")", ":", "x", "=", "fromBytes", "(", "x", ")", "if", "len", "(", "x", ")", ">", "1", ":", "if", "not", "SUPPRESS_ERRORS", ":", "raise", "TypeError", "(", "'chr() found string of length > 2'", ")", "x", "=", "x", "[", "0", "]", "return", "toBytes", "(", "x", ")", "elif", "isinstance", "(", "x", ",", "str", ")", ":", "if", "len", "(", "x", ")", ">", "1", ":", "if", "not", "SUPPRESS_ERRORS", ":", "raise", "TypeError", "(", "'chr() found string of length > 2'", ")", "x", "=", "x", "[", "0", "]", "return", "toBytes", "(", "x", ")", "else", ":", "raise", "TypeError", "(", "'Unknown type passed to chr: %s'", ",", "str", "(", "type", "(", "x", ")", ")", ")" ]
1d5ab2d2fcb962372228033106bc23f1d73d31fa
test
ord
x-->char (str of length 1) Returns-->int Behaves like PY2 ord() in PY2 or PY3 if x is str of length > 1 or int > 256 raises ValueError/TypeError is not SUPPRESS_ERRORS
cffi_utils/py2to3.py
def ord(x): ''' x-->char (str of length 1) Returns-->int Behaves like PY2 ord() in PY2 or PY3 if x is str of length > 1 or int > 256 raises ValueError/TypeError is not SUPPRESS_ERRORS ''' global _ord if isinstance(x, int): if x > 256: if not SUPPRESS_ERRORS: raise ValueError('ord() arg not in range(256)') return x % 256 elif isinstance(x, bytes): x = fromBytes(x) if len(x) > 1: if SUPPRESS_ERRORS: x = x[0] return _ord(x) elif isinstance(x, str): if len(x) > 1: if SUPPRESS_ERRORS: x = x[0] return _ord(x) else: raise TypeError('Unknown type passed to ord: %s', str(type(x)))
def ord(x): ''' x-->char (str of length 1) Returns-->int Behaves like PY2 ord() in PY2 or PY3 if x is str of length > 1 or int > 256 raises ValueError/TypeError is not SUPPRESS_ERRORS ''' global _ord if isinstance(x, int): if x > 256: if not SUPPRESS_ERRORS: raise ValueError('ord() arg not in range(256)') return x % 256 elif isinstance(x, bytes): x = fromBytes(x) if len(x) > 1: if SUPPRESS_ERRORS: x = x[0] return _ord(x) elif isinstance(x, str): if len(x) > 1: if SUPPRESS_ERRORS: x = x[0] return _ord(x) else: raise TypeError('Unknown type passed to ord: %s', str(type(x)))
[ "x", "--", ">", "char", "(", "str", "of", "length", "1", ")", "Returns", "--", ">", "int", "Behaves", "like", "PY2", "ord", "()", "in", "PY2", "or", "PY3", "if", "x", "is", "str", "of", "length", ">", "1", "or", "int", ">", "256", "raises", "ValueError", "/", "TypeError", "is", "not", "SUPPRESS_ERRORS" ]
sundarnagarajan/cffi_utils
python
https://github.com/sundarnagarajan/cffi_utils/blob/1d5ab2d2fcb962372228033106bc23f1d73d31fa/cffi_utils/py2to3.py#L152-L178
[ "def", "ord", "(", "x", ")", ":", "global", "_ord", "if", "isinstance", "(", "x", ",", "int", ")", ":", "if", "x", ">", "256", ":", "if", "not", "SUPPRESS_ERRORS", ":", "raise", "ValueError", "(", "'ord() arg not in range(256)'", ")", "return", "x", "%", "256", "elif", "isinstance", "(", "x", ",", "bytes", ")", ":", "x", "=", "fromBytes", "(", "x", ")", "if", "len", "(", "x", ")", ">", "1", ":", "if", "SUPPRESS_ERRORS", ":", "x", "=", "x", "[", "0", "]", "return", "_ord", "(", "x", ")", "elif", "isinstance", "(", "x", ",", "str", ")", ":", "if", "len", "(", "x", ")", ">", "1", ":", "if", "SUPPRESS_ERRORS", ":", "x", "=", "x", "[", "0", "]", "return", "_ord", "(", "x", ")", "else", ":", "raise", "TypeError", "(", "'Unknown type passed to ord: %s'", ",", "str", "(", "type", "(", "x", ")", ")", ")" ]
1d5ab2d2fcb962372228033106bc23f1d73d31fa
test
hex
x-->bytes | bytearray Returns-->bytes: hex-encoded
cffi_utils/py2to3.py
def hex(x): ''' x-->bytes | bytearray Returns-->bytes: hex-encoded ''' if isinstance(x, bytearray): x = bytes(x) return encode(x, 'hex')
def hex(x): ''' x-->bytes | bytearray Returns-->bytes: hex-encoded ''' if isinstance(x, bytearray): x = bytes(x) return encode(x, 'hex')
[ "x", "--", ">", "bytes", "|", "bytearray", "Returns", "--", ">", "bytes", ":", "hex", "-", "encoded" ]
sundarnagarajan/cffi_utils
python
https://github.com/sundarnagarajan/cffi_utils/blob/1d5ab2d2fcb962372228033106bc23f1d73d31fa/cffi_utils/py2to3.py#L191-L198
[ "def", "hex", "(", "x", ")", ":", "if", "isinstance", "(", "x", ",", "bytearray", ")", ":", "x", "=", "bytes", "(", "x", ")", "return", "encode", "(", "x", ",", "'hex'", ")" ]
1d5ab2d2fcb962372228033106bc23f1d73d31fa
test
fromBytes
x-->unicode string | bytearray | bytes Returns-->unicode string, with encoding=latin1
cffi_utils/py2to3.py
def fromBytes(x): ''' x-->unicode string | bytearray | bytes Returns-->unicode string, with encoding=latin1 ''' if isinstance(x, unicode): return x if isinstance(x, bytearray): x = bytes(x) elif isinstance(x, bytes): pass else: return x # unchanged (int etc) return decode(x, DEF_ENCODING)
def fromBytes(x): ''' x-->unicode string | bytearray | bytes Returns-->unicode string, with encoding=latin1 ''' if isinstance(x, unicode): return x if isinstance(x, bytearray): x = bytes(x) elif isinstance(x, bytes): pass else: return x # unchanged (int etc) return decode(x, DEF_ENCODING)
[ "x", "--", ">", "unicode", "string", "|", "bytearray", "|", "bytes", "Returns", "--", ">", "unicode", "string", "with", "encoding", "=", "latin1" ]
sundarnagarajan/cffi_utils
python
https://github.com/sundarnagarajan/cffi_utils/blob/1d5ab2d2fcb962372228033106bc23f1d73d31fa/cffi_utils/py2to3.py#L201-L214
[ "def", "fromBytes", "(", "x", ")", ":", "if", "isinstance", "(", "x", ",", "unicode", ")", ":", "return", "x", "if", "isinstance", "(", "x", ",", "bytearray", ")", ":", "x", "=", "bytes", "(", "x", ")", "elif", "isinstance", "(", "x", ",", "bytes", ")", ":", "pass", "else", ":", "return", "x", "# unchanged (int etc)", "return", "decode", "(", "x", ",", "DEF_ENCODING", ")" ]
1d5ab2d2fcb962372228033106bc23f1d73d31fa
test
toBytes
x-->unicode string | bytearray | bytes Returns-->bytes If x is unicode, MUST have encoding=latin1
cffi_utils/py2to3.py
def toBytes(x): ''' x-->unicode string | bytearray | bytes Returns-->bytes If x is unicode, MUST have encoding=latin1 ''' if isinstance(x, bytes): return x elif isinstance(x, bytearray): return bytes(x) elif isinstance(x, unicode): pass else: return x # unchanged (int etc) # ASSUMES latin1 encoding - Could raise an exception return encode(x, DEF_ENCODING)
def toBytes(x): ''' x-->unicode string | bytearray | bytes Returns-->bytes If x is unicode, MUST have encoding=latin1 ''' if isinstance(x, bytes): return x elif isinstance(x, bytearray): return bytes(x) elif isinstance(x, unicode): pass else: return x # unchanged (int etc) # ASSUMES latin1 encoding - Could raise an exception return encode(x, DEF_ENCODING)
[ "x", "--", ">", "unicode", "string", "|", "bytearray", "|", "bytes", "Returns", "--", ">", "bytes", "If", "x", "is", "unicode", "MUST", "have", "encoding", "=", "latin1" ]
sundarnagarajan/cffi_utils
python
https://github.com/sundarnagarajan/cffi_utils/blob/1d5ab2d2fcb962372228033106bc23f1d73d31fa/cffi_utils/py2to3.py#L217-L232
[ "def", "toBytes", "(", "x", ")", ":", "if", "isinstance", "(", "x", ",", "bytes", ")", ":", "return", "x", "elif", "isinstance", "(", "x", ",", "bytearray", ")", ":", "return", "bytes", "(", "x", ")", "elif", "isinstance", "(", "x", ",", "unicode", ")", ":", "pass", "else", ":", "return", "x", "# unchanged (int etc)", "# ASSUMES latin1 encoding - Could raise an exception", "return", "encode", "(", "x", ",", "DEF_ENCODING", ")" ]
1d5ab2d2fcb962372228033106bc23f1d73d31fa
test
get_rand_int
encoding-->str: one of ENCODINGS avoid-->list of int: to void (unprintable chars etc) Returns-->int that can be converted to requested encoding which is NOT in avoid
cffi_utils/py2to3.py
def get_rand_int(encoding='latin1', avoid=[]): ''' encoding-->str: one of ENCODINGS avoid-->list of int: to void (unprintable chars etc) Returns-->int that can be converted to requested encoding which is NOT in avoid ''' UNICODE_LIMIT = 0x10ffff # See: https://en.wikipedia.org/wiki/UTF-8#Invalid_code_points SURROGATE_RANGE = (0xD800, 0xDFFF) if encoding not in ENCODINGS: raise ValueError('Unsupported encoding: ' + str(encoding)) if encoding == 'ascii': maxord = 2 ** 7 elif encoding == 'latin1': maxord = 2 ** 8 elif encoding == 'utf16': maxord = 2 ** 16 elif encoding == 'utf8': maxord = 2 ** 32 elif encoding == 'utf32': maxord = 2 ** 32 rndint = random.randrange(0, min(maxord, UNICODE_LIMIT)) while ( (rndint in avoid) or (SURROGATE_RANGE[0] <= rndint <= SURROGATE_RANGE[1]) ): rndint = random.randrange(0, min(maxord, UNICODE_LIMIT)) return rndint
def get_rand_int(encoding='latin1', avoid=[]): ''' encoding-->str: one of ENCODINGS avoid-->list of int: to void (unprintable chars etc) Returns-->int that can be converted to requested encoding which is NOT in avoid ''' UNICODE_LIMIT = 0x10ffff # See: https://en.wikipedia.org/wiki/UTF-8#Invalid_code_points SURROGATE_RANGE = (0xD800, 0xDFFF) if encoding not in ENCODINGS: raise ValueError('Unsupported encoding: ' + str(encoding)) if encoding == 'ascii': maxord = 2 ** 7 elif encoding == 'latin1': maxord = 2 ** 8 elif encoding == 'utf16': maxord = 2 ** 16 elif encoding == 'utf8': maxord = 2 ** 32 elif encoding == 'utf32': maxord = 2 ** 32 rndint = random.randrange(0, min(maxord, UNICODE_LIMIT)) while ( (rndint in avoid) or (SURROGATE_RANGE[0] <= rndint <= SURROGATE_RANGE[1]) ): rndint = random.randrange(0, min(maxord, UNICODE_LIMIT)) return rndint
[ "encoding", "--", ">", "str", ":", "one", "of", "ENCODINGS", "avoid", "--", ">", "list", "of", "int", ":", "to", "void", "(", "unprintable", "chars", "etc", ")", "Returns", "--", ">", "int", "that", "can", "be", "converted", "to", "requested", "encoding", "which", "is", "NOT", "in", "avoid" ]
sundarnagarajan/cffi_utils
python
https://github.com/sundarnagarajan/cffi_utils/blob/1d5ab2d2fcb962372228033106bc23f1d73d31fa/cffi_utils/py2to3.py#L306-L336
[ "def", "get_rand_int", "(", "encoding", "=", "'latin1'", ",", "avoid", "=", "[", "]", ")", ":", "UNICODE_LIMIT", "=", "0x10ffff", "# See: https://en.wikipedia.org/wiki/UTF-8#Invalid_code_points", "SURROGATE_RANGE", "=", "(", "0xD800", ",", "0xDFFF", ")", "if", "encoding", "not", "in", "ENCODINGS", ":", "raise", "ValueError", "(", "'Unsupported encoding: '", "+", "str", "(", "encoding", ")", ")", "if", "encoding", "==", "'ascii'", ":", "maxord", "=", "2", "**", "7", "elif", "encoding", "==", "'latin1'", ":", "maxord", "=", "2", "**", "8", "elif", "encoding", "==", "'utf16'", ":", "maxord", "=", "2", "**", "16", "elif", "encoding", "==", "'utf8'", ":", "maxord", "=", "2", "**", "32", "elif", "encoding", "==", "'utf32'", ":", "maxord", "=", "2", "**", "32", "rndint", "=", "random", ".", "randrange", "(", "0", ",", "min", "(", "maxord", ",", "UNICODE_LIMIT", ")", ")", "while", "(", "(", "rndint", "in", "avoid", ")", "or", "(", "SURROGATE_RANGE", "[", "0", "]", "<=", "rndint", "<=", "SURROGATE_RANGE", "[", "1", "]", ")", ")", ":", "rndint", "=", "random", ".", "randrange", "(", "0", ",", "min", "(", "maxord", ",", "UNICODE_LIMIT", ")", ")", "return", "rndint" ]
1d5ab2d2fcb962372228033106bc23f1d73d31fa
test
get_rand_str
encoding-->str: one of ENCODINGS l-->int: length of returned str avoid-->list of int: to void (unprintable chars etc) Returns-->unicode str of the requested encoding
cffi_utils/py2to3.py
def get_rand_str(encoding='latin1', l=64, avoid=[]): ''' encoding-->str: one of ENCODINGS l-->int: length of returned str avoid-->list of int: to void (unprintable chars etc) Returns-->unicode str of the requested encoding ''' ret = unicode('') while len(ret) < l: rndint = get_rand_int(encoding=encoding, avoid=avoid) ret += unichr(rndint) return ret
def get_rand_str(encoding='latin1', l=64, avoid=[]): ''' encoding-->str: one of ENCODINGS l-->int: length of returned str avoid-->list of int: to void (unprintable chars etc) Returns-->unicode str of the requested encoding ''' ret = unicode('') while len(ret) < l: rndint = get_rand_int(encoding=encoding, avoid=avoid) ret += unichr(rndint) return ret
[ "encoding", "--", ">", "str", ":", "one", "of", "ENCODINGS", "l", "--", ">", "int", ":", "length", "of", "returned", "str", "avoid", "--", ">", "list", "of", "int", ":", "to", "void", "(", "unprintable", "chars", "etc", ")", "Returns", "--", ">", "unicode", "str", "of", "the", "requested", "encoding" ]
sundarnagarajan/cffi_utils
python
https://github.com/sundarnagarajan/cffi_utils/blob/1d5ab2d2fcb962372228033106bc23f1d73d31fa/cffi_utils/py2to3.py#L339-L350
[ "def", "get_rand_str", "(", "encoding", "=", "'latin1'", ",", "l", "=", "64", ",", "avoid", "=", "[", "]", ")", ":", "ret", "=", "unicode", "(", "''", ")", "while", "len", "(", "ret", ")", "<", "l", ":", "rndint", "=", "get_rand_int", "(", "encoding", "=", "encoding", ",", "avoid", "=", "avoid", ")", "ret", "+=", "unichr", "(", "rndint", ")", "return", "ret" ]
1d5ab2d2fcb962372228033106bc23f1d73d31fa