code
stringlengths
52
7.75k
docs
stringlengths
1
5.85k
def check_perm(self, request, resource): perm_name = self.get_perm_name(resource, request.method) if not self._has_perm(request.user, perm_name): raise errors.Forbidden()
Check permission @param request the HTTP request @param resource the requested resource @raise Forbidden if the user doesn't have access to the resource
def _has_perm(self, user, permission): if user.is_superuser: return True if user.is_active: perms = [perm.split('.')[1] for perm in user.get_all_permissions()] return permission in perms return False
Check whether the user has the given permission @return True if user is granted with access, False if not.
def wash_url_argument(var, new_type): out = [] if new_type == 'list': # return lst if isinstance(var, list): out = var else: out = [var] elif new_type == 'str': # return str if isinstance(var, list): try: out = "%s" % var[0] ...
Wash argument into 'new_type', that can be 'list', 'str', 'int', 'tuple' or 'dict'. If needed, the check 'type(var) is not None' should be done before calling this function. @param var: variable value @param new_type: variable type, 'list', 'str', 'int', 't...
def is_local_url(target): ref_url = urlparse(cfg.get('CFG_SITE_SECURE_URL')) test_url = urlparse(urljoin(cfg.get('CFG_SITE_SECURE_URL'), target)) return test_url.scheme in ('http', 'https') and \ ref_url.netloc == test_url.netloc
Determine if URL is a local.
def get_safe_redirect_target(arg='next'): for target in request.args.get(arg), request.referrer: if not target: continue if is_local_url(target): return target return None
Get URL to redirect to and ensure that it is local.
def rewrite_to_secure_url(url, secure_base=None): if secure_base is None: secure_base = cfg.get('CFG_SITE_SECURE_URL') url_parts = list(urlparse(url)) url_secure_parts = urlparse(secure_base) url_parts[0] = url_secure_parts[0] url_parts[1] = url_secure_parts[1] return urlunparse(url...
Rewrite URL to a Secure URL @param url URL to be rewritten to a secure URL. @param secure_base: Base URL of secure site (defaults to CFG_SITE_SECURE_URL).
def get_referer(req, replace_ampersands=False): try: referer = req.headers_in['Referer'] if replace_ampersands == 1: return referer.replace('&', '&') return referer except KeyError: return ''
Return the referring page of a request. Referer (wikipedia): Referer is a common misspelling of the word "referrer"; so common, in fact, that it made it into the official specification of HTTP. When visiting a webpage, the referer or referring page is the URL of the previous webpage from which a link wa...
def make_canonical_urlargd(urlargd, default_urlargd): canonical = drop_default_urlargd(urlargd, default_urlargd) if canonical: return '?' + urlencode(canonical, doseq=True) # FIXME double escaping of '&'? .replace('&', '&') return ''
Build up the query part of an URL from the arguments passed in the 'urlargd' dictionary. 'default_urlargd' is a secondary dictionary which contains tuples of the form (type, default value) for the query arguments (this is the same dictionary as the one you can pass to webinterface_handler.wash_urlargd)....
def create_html_link(urlbase, urlargd, link_label, linkattrd=None, escape_urlargd=True, escape_linkattrd=True, urlhash=None): attributes_separator = ' ' output = '<a href="' + \ create_url(urlbase, urlargd, escape_urlargd, urlhash) + '"' if linkatt...
Creates a W3C compliant link. @param urlbase: base url (e.g. config.CFG_SITE_URL/search) @param urlargd: dictionary of parameters. (e.g. p={'recid':3, 'of'='hb'}) @param link_label: text displayed in a browser (has to be already escaped) @param linkattrd: dictionary of attributes (e.g. a={'class': 'img'...
def string_to_numeric_char_reference(string): out = "" for char in string: out += "&#" + str(ord(char)) + ";" return out
Encode a string to HTML-compatible numeric character reference. Eg: encode_html_entities("abc") == '&#97;&#98;&#99;'
def get_canonical_and_alternates_urls( url, drop_ln=True, washed_argd=None, quote_path=False): dummy_scheme, dummy_netloc, path, dummy_params, query, fragment = urlparse( url) canonical_scheme, canonical_netloc = urlparse(cfg.get('CFG_SITE_URL'))[0:2] parsed_quer...
Given an Invenio URL returns a tuple with two elements. The first is the canonical URL, that is the original URL with CFG_SITE_URL prefix, and where the ln= argument stripped. The second element element is mapping, language code -> alternate URL @param quote_path: if True, the path section of the given...
def create_url(urlbase, urlargd, escape_urlargd=True, urlhash=None): separator = '&amp;' output = urlbase if urlargd: output += '?' if escape_urlargd: arguments = [escape(quote(str(key)), quote=True) + '=' + escape(quote(str(urlargd[key])), quote=Tru...
Creates a W3C compliant URL. Output will look like this: 'urlbase?param1=value1&amp;param2=value2' @param urlbase: base url (e.g. config.CFG_SITE_URL/search) @param urlargd: dictionary of parameters. (e.g. p={'recid':3, 'of'='hb'} @param escape_urlargd: boolean indicating if the function should escape ...
def same_urls_p(a, b): ua = list(urlparse(a)) ub = list(urlparse(b)) ua[4] = parse_qs(ua[4]) ub[4] = parse_qs(ub[4]) return ua == ub
Compare two URLs, ignoring reorganizing of query arguments
def urlargs_replace_text_in_arg(urlargs, regexp_argname, text_old, text_new): out = "" # parse URL arguments into a dictionary: urlargsdict = parse_qs(urlargs) # construct new URL arguments: urlargsdictnew = {} for key in urlargsdict.keys(): if re.match(regexp_argname, key): # repl...
Analyze `urlargs' (URL CGI GET query arguments in string form) and for each occurrence of argument matching `regexp_argname' replace every substring `text_old' by `text_new'. Return the resulting new URL. Used to be used for search engine's create_nearest_terms_box, now it is not us...
def make_user_agent_string(component=None): ret = "Invenio-%s (+%s; \"%s\")" % (cfg.get('CFG_VERSION'), cfg.get('CFG_SITE_URL'), cfg.get('CFG_SITE_NAME')) if component: ret += " %s" % component return ret
Return a nice and uniform user-agent string to be used when Invenio act as a client in HTTP requests.
def make_invenio_opener(component=None): opener = urllib2.build_opener() opener.addheaders = [('User-agent', make_user_agent_string(component))] return opener
Return an urllib2 opener with the useragent already set in the appropriate way.
def auto_version_url(file_path): file_md5 = "" try: file_md5 = md5(open(cfg.get('CFG_WEBDIR') + os.sep + file_path).read()).hexdigest() except IOError: pass return file_path + "?%s" % file_md5
Appends modification time of the file to the request URL in order for the browser to refresh the cache when file changes @param file_path: path to the file, e.g js/foo.js @return: file_path with modification time appended to URL
def get_relative_url(url): # remove any protocol info before stripped_site_url = url.replace("://", "") baseurl = "/" + "/".join(stripped_site_url.split("/")[1:]) # remove any trailing slash ("/") if baseurl[-1] == "/": return baseurl[:-1] else: return baseurl
Returns the relative URL from a URL. For example: 'http://web.net' -> '' 'http://web.net/' -> '' 'http://web.net/1222' -> '/1222' 'http://web.net/wsadas/asd' -> '/wsadas/asd' It will never return a trailing "/". @param url: A url to transform @type url: str @return: relative URL
def function_arg_count(fn): assert callable(fn), 'function_arg_count needed a callable function, not {0}'.format(repr(fn)) if hasattr(fn, '__code__') and hasattr(fn.__code__, 'co_argcount'): return fn.__code__.co_argcount else: return 1
returns how many arguments a funciton has
def map(*args): functions_to_apply = [i for i in args if callable(i)] iterables_to_run = [i for i in args if not callable(i)] #print('functions_to_apply:',functions_to_apply) #print('iterables_to_run:',iterables_to_run) assert len(functions_to_apply)>0, 'at least one function needs to be give...
this map works just like the builtin.map, except, this one you can also: - give it multiple functions to map over an iterable - give it a single function with multiple arguments to run a window based map operation over an iterable
def merge(left, right, how='inner', key=None, left_key=None, right_key=None, left_as='left', right_as='right'): return join(left, right, how, key, left_key, right_key, join_fn=make_union_join(left_as, right_as))
Performs a join using the union join function.
def join(left, right, how='inner', key=None, left_key=None, right_key=None, join_fn=tuple_join): if key is None and (left_key is None or right_key is None): raise ValueError("Must provide either key param or both left_key and right_key") if key is not None: lkey = rkey = key if ca...
:param left: left iterable to be joined :param right: right iterable to be joined :param str | function key: either an attr name, dict key, or function that produces hashable value :param how: 'inner', 'left', 'right', or 'outer' :param join_fn: function called on joined left and right iterable items to...
def _inner_join(left, right, left_key_fn, right_key_fn, join_fn=union_join): joiner = defaultdict(list) for ele in right: joiner[right_key_fn(ele)].append(ele) joined = [] for ele in left: for other in joiner[left_key_fn(ele)]: joined.append(join_fn(ele, other)) retu...
Inner join using left and right key functions :param left: left iterable to be joined :param right: right iterable to be joined :param function left_key_fn: function that produces hashable value from left objects :param function right_key_fn: function that produces hashable value from right objects ...
def _right_join(left, right, left_key_fn, right_key_fn, join_fn=union_join): def reversed_join_fn(left_ele, right_ele): return join_fn(right_ele, left_ele) return _left_join(right, left, right_key_fn, left_key_fn, reversed_join_fn)
:param left: left iterable to be joined :param right: right iterable to be joined :param function left_key_fn: function that produces hashable value from left objects :param function right_key_fn: function that produces hashable value from right objects :param join_fn: function called on joined left and...
def _outer_join(left, right, left_key_fn, right_key_fn, join_fn=union_join): left_joiner = defaultdict(list) for ele in left: left_joiner[left_key_fn(ele)].append(ele) right_joiner = defaultdict(list) for ele in right: right_joiner[right_key_fn(ele)].append(ele) keys = set(left_...
:param left: left iterable to be joined :param right: right iterable to be joined :param function left_key_fn: function that produces hashable value from left objects :param function right_key_fn: function that produces hashable value from right objects :param join_fn: function called on joined left and...
def group(iterable, key=lambda ele: ele): if callable(key): return _group(iterable, key) else: return _group(iterable, make_key_fn(key))
Groups an iterable by a specified attribute, or using a specified key access function. Returns tuples of grouped elements. >>> dogs = [Dog('gatsby', 'Rruff!', 15), Dog('william', 'roof', 12), Dog('edward', 'hi', 15)] >>> groupby(dogs, 'weight') [(Dog('gatsby', 'Rruff!', 15), Dog('edward', 'hi', 15)), (Dog...
def trigger_keyphrases( text = None, # input text to parse keyphrases = None, # keyphrases for parsing input text response = None, # optional text response on trigger function = None, # optional function on trigger...
Parse input text for keyphrases. If any keyphrases are found, respond with text or by seeking confirmation or by engaging a function with optional keyword arguments. Return text or True if triggered and return False if not triggered. If confirmation is required, a confirmation object is returned, encaps...
def parse_networking( text = None ): try: address = _builtins.address port = _builtins.port except: address = None port = None triggers = [] if address and port: triggers.extend([ trigger_keyphrases( text ...
Access address and port parameters via the builtins or __builtin__ module. Relish the nonsense.
def multiparse( text = None, parsers = [parse], help_message = None ): responses = [] for _parser in parsers: response = _parser(text = text) if response is not False: responses.extend(response if response is list else [response]) if not any(resp...
Parse input text by looping over a list of multiple parsers. If one trigger is triggered, return the value returned by that trigger, if multiple triggers are triggered, return a list of the values returned by those triggers. If no triggers are triggered, return False or an optional help message.
def run( self ): if self._function and not self._kwargs: return self._function() if self._function and self._kwargs: return self._function(**self._kwargs)
Engage contained function with optional keyword arguments.
def tax_class_based_on(self, tax_class_based_on): allowed_values = ["shippingAddress", "billingAddress"] # noqa: E501 if tax_class_based_on is not None and tax_class_based_on not in allowed_values: raise ValueError( "Invalid value for `tax_class_based_on` ({0}), mus...
Sets the tax_class_based_on of this TaxSettings. :param tax_class_based_on: The tax_class_based_on of this TaxSettings. :type: str
def create_fixed_rate_shipping(cls, fixed_rate_shipping, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._create_fixed_rate_shipping_with_http_info(fixed_rate_shipping, **kwargs) else: (data) = cls._create_fixed_rate_shipping...
Create FixedRateShipping Create a new FixedRateShipping This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.create_fixed_rate_shipping(fixed_rate_shipping, async=True) >>> result = thread.get() ...
def delete_fixed_rate_shipping_by_id(cls, fixed_rate_shipping_id, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._delete_fixed_rate_shipping_by_id_with_http_info(fixed_rate_shipping_id, **kwargs) else: (data) = cls._delete_f...
Delete FixedRateShipping Delete an instance of FixedRateShipping by its ID. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.delete_fixed_rate_shipping_by_id(fixed_rate_shipping_id, async=True) ...
def get_fixed_rate_shipping_by_id(cls, fixed_rate_shipping_id, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._get_fixed_rate_shipping_by_id_with_http_info(fixed_rate_shipping_id, **kwargs) else: (data) = cls._get_fixed_rate...
Find FixedRateShipping Return single instance of FixedRateShipping by its ID. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.get_fixed_rate_shipping_by_id(fixed_rate_shipping_id, async=True) ...
def list_all_fixed_rate_shippings(cls, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._list_all_fixed_rate_shippings_with_http_info(**kwargs) else: (data) = cls._list_all_fixed_rate_shippings_with_http_info(**kwargs) ...
List FixedRateShippings Return a list of FixedRateShippings This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.list_all_fixed_rate_shippings(async=True) >>> result = thread.get() :param ...
def replace_fixed_rate_shipping_by_id(cls, fixed_rate_shipping_id, fixed_rate_shipping, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._replace_fixed_rate_shipping_by_id_with_http_info(fixed_rate_shipping_id, fixed_rate_shipping, **kwargs) ...
Replace FixedRateShipping Replace all attributes of FixedRateShipping This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.replace_fixed_rate_shipping_by_id(fixed_rate_shipping_id, fixed_rate_shipping, asy...
def update_fixed_rate_shipping_by_id(cls, fixed_rate_shipping_id, fixed_rate_shipping, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._update_fixed_rate_shipping_by_id_with_http_info(fixed_rate_shipping_id, fixed_rate_shipping, **kwargs) ...
Update FixedRateShipping Update attributes of FixedRateShipping This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.update_fixed_rate_shipping_by_id(fixed_rate_shipping_id, fixed_rate_shipping, async=True...
def create_wish_list(cls, wish_list, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._create_wish_list_with_http_info(wish_list, **kwargs) else: (data) = cls._create_wish_list_with_http_info(wish_list, **kwargs) r...
Create WishList Create a new WishList This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.create_wish_list(wish_list, async=True) >>> result = thread.get() :param async bool :para...
def delete_wish_list_by_id(cls, wish_list_id, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._delete_wish_list_by_id_with_http_info(wish_list_id, **kwargs) else: (data) = cls._delete_wish_list_by_id_with_http_info(wish_list_...
Delete WishList Delete an instance of WishList by its ID. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.delete_wish_list_by_id(wish_list_id, async=True) >>> result = thread.get() :p...
def get_wish_list_by_id(cls, wish_list_id, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._get_wish_list_by_id_with_http_info(wish_list_id, **kwargs) else: (data) = cls._get_wish_list_by_id_with_http_info(wish_list_id, **kwa...
Find WishList Return single instance of WishList by its ID. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.get_wish_list_by_id(wish_list_id, async=True) >>> result = thread.get() :pa...
def list_all_wish_lists(cls, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._list_all_wish_lists_with_http_info(**kwargs) else: (data) = cls._list_all_wish_lists_with_http_info(**kwargs) return data
List WishLists Return a list of WishLists This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.list_all_wish_lists(async=True) >>> result = thread.get() :param async bool :param in...
def replace_wish_list_by_id(cls, wish_list_id, wish_list, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._replace_wish_list_by_id_with_http_info(wish_list_id, wish_list, **kwargs) else: (data) = cls._replace_wish_list_by_id_...
Replace WishList Replace all attributes of WishList This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.replace_wish_list_by_id(wish_list_id, wish_list, async=True) >>> result = thread.get() ...
def update_wish_list_by_id(cls, wish_list_id, wish_list, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._update_wish_list_by_id_with_http_info(wish_list_id, wish_list, **kwargs) else: (data) = cls._update_wish_list_by_id_wit...
Update WishList Update attributes of WishList This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.update_wish_list_by_id(wish_list_id, wish_list, async=True) >>> result = thread.get() :pa...
def task(arg = None): # make sure stdout is patched if not hasattr(sys.stdout, 'indent_level'): sys.stdout = IndentedFile(sys.stdout) def decorator(base): info = ': ' + arg if type(arg) is str else '' header = fore.green('** ' + fore.cyan(base.__name__) + info) def func(*args, **kwargs): sys.stdout.i...
Task decorator
def recurse_up(directory, filename): directory = osp.abspath(directory) while True: searchfile = osp.join(directory, filename) if osp.isfile(searchfile): return directory if directory == '/': break else: directory = osp.dirname(directory) return False
Recursive walk a directory up to root until it contains `filename`
def etree_to_dict(tree): d = {tree.tag.split('}')[1]: map( etree_to_dict, tree.iterchildren() ) or tree.text} return d
Translate etree into dictionary. :param tree: etree dictionary object :type tree: <http://lxml.de/api/lxml.etree-module.html>
def csv( self, filepath=None ): self.log.debug('starting the ``csv`` method') renderedData = self._list_of_dictionaries_to_csv("machine") if filepath and renderedData != "NO MATCH": # RECURSIVELY CREATE MISSING DIRECTORIES if not os.path.ex...
*Render the data in CSV format* **Key Arguments:** - ``filepath`` -- path to the file to write the csv content to. Default *None* **Return:** - ``renderedData`` -- the data rendered in csv format **Usage:** To render the data set as csv: .. co...
def json( self, filepath=None ): self.log.debug('starting the ``json`` method') dataCopy = copy.deepcopy(self.listOfDictionaries) for d in dataCopy: for k, v in d.iteritems(): if isinstance(v, datetime): d[k] = v.strf...
*Render the data in json format* **Key Arguments:** - ``filepath`` -- path to the file to write the json content to. Default *None* **Return:** - ``renderedData`` -- the data rendered as json **Usage:** To render the data set as json: .. code-...
def yaml( self, filepath=None ): self.log.debug('starting the ``yaml`` method') dataCopy = [] dataCopy[:] = [dict(l) for l in self.listOfDictionaries] renderedData = yaml.dump(dataCopy, default_flow_style=False) if filepath and len(self.listOfDictio...
*Render the data in yaml format* **Key Arguments:** - ``filepath`` -- path to the file to write the yaml content to. Default *None* **Return:** - ``renderedData`` -- the data rendered as yaml **Usage:** To render the data set as yaml: .. code-...
def _list_of_dictionaries_to_mysql_inserts( self, tableName, createStatement=None): self.log.debug( 'completed the ````_list_of_dictionaries_to_mysql_inserts`` function') if not len(self.listOfDictionaries): return "NO MATCH" ...
Convert a python list of dictionaries to pretty csv output **Key Arguments:** - ``tableName`` -- the name of the table to create the insert statements for - ``createStatement`` -- add this create statement to the top of the file. Will only be executed if no table of that name exists in ...
def create_payment_token(cls, payment_token, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._create_payment_token_with_http_info(payment_token, **kwargs) else: (data) = cls._create_payment_token_with_http_info(payment_token,...
Create PaymentToken Create a new PaymentToken This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.create_payment_token(payment_token, async=True) >>> result = thread.get() :param async bo...
def delete_payment_token_by_id(cls, payment_token_id, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._delete_payment_token_by_id_with_http_info(payment_token_id, **kwargs) else: (data) = cls._delete_payment_token_by_id_with_...
Delete PaymentToken Delete an instance of PaymentToken by its ID. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.delete_payment_token_by_id(payment_token_id, async=True) >>> result = thread.g...
def get_payment_token_by_id(cls, payment_token_id, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._get_payment_token_by_id_with_http_info(payment_token_id, **kwargs) else: (data) = cls._get_payment_token_by_id_with_http_info...
Find PaymentToken Return single instance of PaymentToken by its ID. This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.get_payment_token_by_id(payment_token_id, async=True) >>> result = thread.ge...
def list_all_payment_tokens(cls, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._list_all_payment_tokens_with_http_info(**kwargs) else: (data) = cls._list_all_payment_tokens_with_http_info(**kwargs) return data
List PaymentTokens Return a list of PaymentTokens This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.list_all_payment_tokens(async=True) >>> result = thread.get() :param async bool ...
def replace_payment_token_by_id(cls, payment_token_id, payment_token, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._replace_payment_token_by_id_with_http_info(payment_token_id, payment_token, **kwargs) else: (data) = cls._...
Replace PaymentToken Replace all attributes of PaymentToken This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.replace_payment_token_by_id(payment_token_id, payment_token, async=True) >>> result ...
def update_payment_token_by_id(cls, payment_token_id, payment_token, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._update_payment_token_by_id_with_http_info(payment_token_id, payment_token, **kwargs) else: (data) = cls._up...
Update PaymentToken Update attributes of PaymentToken This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.update_payment_token_by_id(payment_token_id, payment_token, async=True) >>> result = threa...
def split_in_columns(filterform, fields_per_column=None): ''' Return iterator that yields a column (iterator too). By default, flat field list is divided in columns with fields_per_column elements in each (fields_per_column is a class attribute). ''' nfields = len(filterform.fields) if ...
Return iterator that yields a column (iterator too). By default, flat field list is divided in columns with fields_per_column elements in each (fields_per_column is a class attribute).
def dispatch_event(self, event: "Event") -> None: # Set the target of the event if it doesn't have one already. It could happen that # we are simply redispatching an event. if event.target is None: event.set_target(self) listeners: dict[types.MethodType, bool...
Dispatches the given event. It is the duty of this method to set the target of the dispatched event by calling `event.set_target(self)`. Args: event (Event): The event to dispatch. Must not be `None`. Raises: TypeError: If the event is `None` or its ty...
def remove_event_listener(self, event_type: str, event_handler: types.MethodType) -> None: # TODO: we should also accept types.FunctionType, # don't forget the documentation here and in the interface. if not isinstance(event_type, str) or event_type == "" or\ not isinsta...
Removes the given event listener registered on the dispatcher for the given event type. Args: event_type (str): The type of the event to remove the event handler from. Must not be `None` or empty string. event_handler (types.MethodType): The event hand...
def set_target(self, target: EventDispatcherBase) -> None: if self._target is not None: raise PermissionError("The target property already has a valid value.") if not isinstance(target, EventDispatcherBase): raise TypeError("Invalid target type: {}".format(target)...
This method should be called by the event dispatcher that dispatches this event to set its target property. Args: target (EventDispatcherBase): The event dispatcher that will dispatch this event. Raises: PermissionError: If the target property of the event has al...
def download_url(url, content_type=None, download_to_file=None, retry_count=10, timeout=10.0): if not download_to_file: download_to_file = safe_mkstemp(suffix=".tmp", prefix="filedownloadutils_") try: if is_url_a_local_file(url): ...
Will download a file from given URL (either local or external) to the desired path (or generate one if none is given). Local files are copied directly. The function will retry a number of times based on retry_count (default 10) parameter and sleeps a number of seconds based on given timeout (defaul...
def finalize_download(url, download_to_file, content_type, request): # If format is given, a format check is performed. if content_type and content_type not in request.headers['content-type']: msg = 'The downloaded file is not of the desired format' raise InvenioFileDownloadError(msg) ...
Finalizes the download operation by doing various checks, such as format type, size check etc.
def download_local_file(filename, download_to_file): # Try to copy. try: path = urllib2.urlparse.urlsplit(urllib.unquote(filename))[2] if os.path.abspath(path) != path: msg = "%s is not a normalized path (would be %s)." \ % (path, os.path.normpath(path)) ...
Copies a local file to Invenio's temporary directory. @param filename: the name of the file to copy @type filename: string @param download_to_file: the path to save the file to @type download_to_file: string @return: the path of the temporary file created @rtype: string @raise StandardErr...
def safe_mkstemp(suffix, prefix='filedownloadutils_'): tmpfd, tmppath = tempfile.mkstemp( suffix=suffix, prefix=prefix, dir=current_app.config['CFG_TMPSHAREDDIR'] ) # Close the file and leave the responsability to the client code to # correctly open/close it. os.close(tm...
Create a temporary filename that don't have any '.' inside a part from the suffix.
def open_url(url, headers=None): request = urllib2.Request(url) if headers: for key, value in headers.items(): request.add_header(key, value) return URL_OPENER.open(request)
Opens a URL. If headers are passed as argument, no check is performed and the URL will be opened. @param url: the URL to open @type url: string @param headers: the headers to use @type headers: dictionary @return: a file-like object as returned by urllib2.urlopen.
def bulk_log(self, log_message=u"Еще одна пачка обработана", total=None, part_log_time_minutes=5): return BulkLogger(log=self.log, log_message=log_message, total=total, part_log_time_minutes=part_log_time_minutes)
Возвращает инстант логгера для обработки списокв данных :param log_message: То, что будет написано, когда время придет :param total: Общее кол-во объектов, если вы знаете его :param part_log_time_minutes: Раз в какое кол-во минут пытаться писать лог :return: BulkLogger
def db(self, db_alias, shard_key=None): if shard_key is None: shard_key = '' db_key = db_alias + '__' + str(shard_key) if db_key not in self.__db_list: self.__db_list[db_key] = DbQueryService(self, self.__default_headers, {"db_alias": db_alias, "dbAlias": db_ali...
Получить экземпляр работы с БД :type db_alias: basestring Альяс БД из меты :type shard_key: Любой тип. Некоторый идентификатор, который поможет мете найти нужную шарду. Тип зависи от принимающей стороны :rtype: DbQueryService
def __read_developer_settings(self): self.developer_settings = read_developer_settings() if not self.developer_settings: self.log.warning("НЕ УСТАНОВЛЕНЫ настройки разработчика, это может приводить к проблемам в дальнейшей работе!")
Читает конфигурации разработчика с локальной машины или из переменных окружения При этом переменная окружения приоритетнее :return:
def api_call(self, service, method, data, options): if 'self' in data: # может не быть, если вызывается напрямую из кода, # а не из прослоек типа DbQueryService data.pop("self") if options: data.update(options) _headers = dict(self.__def...
:type app: metasdk.MetaApp
async def get_json(self, url, timeout=30, astext=False, exceptions=False): try: with async_timeout.timeout(timeout): res = await self._aio_session.get(url) if res.status != 200: _LOGGER.error("QSUSB returned %s [%s]", res.status, url) ...
Get URL and parse JSON from text.
def stop(self): self._running = False if self._sleep_task: self._sleep_task.cancel() self._sleep_task = None
Stop listening.
def version(self): return self.get_json(URL_VERSION.format(self._url), astext=True)
Get the QS Mobile version.
def listen(self, callback=None): self._running = True self.loop.create_task(self._async_listen(callback))
Start the &listen long poll and return immediately.
async def _async_listen(self, callback=None): while True: if not self._running: return try: packet = await self.get_json( URL_LISTEN.format(self._url), timeout=30, exceptions=True) except asyncio.TimeoutError: ...
Listen loop.
def set_qs_value(self, qsid, val, success_cb): self.loop.create_task(self.async_set_qs_value(qsid, val, success_cb))
Push state to QSUSB, retry with backoff.
async def async_set_qs_value(self, qsid, val, success_cb=None): set_url = URL_SET.format(self._url, qsid, val) for _repeat in range(1, 6): set_result = await self.get_json(set_url, 2) if set_result and set_result.get('data', 'NO REPLY') != 'NO REPLY': if ...
Push state to QSUSB, retry with backoff.
async def update_from_devices(self): res = await self.get_json(URL_DEVICES.format(self._url)) if res: self.devices.update_devices(res) return True return False
Retrieve a list of &devices and values.
def multi_ops(data_stream, *funcs): assert all(callable(func) for func in funcs), 'multi_ops can only apply functions to the first argument' assert len(funcs), 'multi_ops needs at least one function to apply to data_stream' for i in data_stream: if len(funcs) > 1: yield tuple(func...
fork a generator with multiple operations/functions data_stream - an iterable data structure (ie: list/generator/tuple) funcs - every function that will be applied to the data_stream
def attowiki_distro_path(): attowiki_path = os.path.abspath(__file__) if attowiki_path[-1] != '/': attowiki_path = attowiki_path[:attowiki_path.rfind('/')] else: attowiki_path = attowiki_path[:attowiki_path[:-1].rfind('/')] return attowiki_path
return the absolute complete path where attowiki is located .. todo:: use pkg_resources ?
def build_command(self): return cron_utils.cronify("crontab -l | {{ cat; echo \"{} {} {} {} {} CJOBID='{}' MAILTO='' {}\"; }} | crontab - > /dev/null".format(self._minute, self._hour, self._day_of_month, self._month_of_year, self._day_of_week, self._jobid, self._command))
Build out the crontab command
def read_environment_file(envfile=None): if envfile is None: frame = sys._getframe() envfile = os.path.join(os.path.dirname(frame.f_back.f_code.co_filename), '.env') if not os.path.exists(envfile): warnings.warn("not reading %s - it doesn't exist." % envfile) ret...
Read a .env file into os.environ. If not given a path to a envfile path, does filthy magic stack backtracking to find manage.py and then find the envfile.
def infer_format(filename:str) -> str: _, ext = os.path.splitext(filename) return ext
Return extension identifying format of given filename
def reversed_graph(graph:dict) -> dict: ret = defaultdict(set) for node, succs in graph.items(): for succ in succs: ret[succ].add(node) return dict(ret)
Return given graph reversed
def walk(start:list, graphs:iter) -> iter: walked = set([start]) stack = [start] while len(stack) > 0: *stack, curr = stack yield curr succs = it.chain.from_iterable(graph.get(curr, ()) for graph in graphs) for succ in succs: if succ not in walked: ...
walk on given graphs, beginning on start. Yield all found nodes, including start. All graph are understood as a single one, with merged keys and values.
def have_cycle(graph:dict) -> frozenset: # topological sort walked = set() # walked nodes nodes = frozenset(it.chain(it.chain.from_iterable(graph.values()), graph.keys())) # all nodes of the graph preds = reversed_graph(graph) # succ: preds last_walked_len = -1 while last_walked_len != l...
Perform a topologic sort to detect any cycle. Return the set of unsortable nodes. If at least one item, then there is cycle in given graph.
def file_lines(bblfile:str) -> iter: with open(bblfile) as fd: yield from (line.rstrip() for line in fd if line.rstrip())
Yield lines found in given file
def line_type(line:str) -> str: for regex, ltype in LINE_TYPES.items(): if re.fullmatch(regex, line): return ltype raise ValueError("Input line \"{}\" is not bubble formatted".format(line))
Give type of input line, as defined in LINE_TYPES >>> line_type('IN\\ta\\tb') 'IN' >>> line_type('') 'EMPTY'
def line_data(line:str) -> tuple: for regex, _ in LINE_TYPES.items(): match = re.fullmatch(regex, line) if match: return match.groups() raise ValueError("Input line \"{}\" is not bubble formatted".format(line))
Return groups found in given line >>> line_data('IN\\ta\\tb') ('IN', 'a', 'b') >>> line_data('') ()
def _catchCurrentViewContent(self): viewContent = None if self._buffer_color_mode != self._display_color_mode: viewContent = self._buffer.crop( self.View.rectToArray() ) .convert( self._display_color_mode ) else: viewContent = self._buffer.crop( self.View.rectToA...
! \~english Catch the current view content @return: a PIL Image @note Automatically converts the cache color mode and at the same time rotates the captured image data according to the screen angle \~chinese 从缓存中抓取当前视图大小的数据 @...
def _initBuffer(self, bufferColorMode, bufferSize): # super(SSScreenBase)._initBuffer(bufferColorMode, bufferSize) self._buffer_color_mode = bufferColorMode #create screen image buffer and canvas if bufferSize==None: self._buffer = Image.new( bufferColorMode , self....
! \~english Initialize the buffer object instance, use PIL Image as for buffer @param bufferColorMode: "RGB" or "1" @param bufferSize: (width, height) \~chinese 初始化缓冲区对象实例,使用PIL Image作为缓冲区 @param bufferColorMode: 色彩模式, 取值: "RGB" 或 "1" @param bufferSize: 缓存...
def clearCanvas(self, fillColor = 0 ): self.Canvas.rectangle((0, 0, self._display_size[0], self._display_size[1]), outline=0, fill=fillColor)
! \~engliash Clear up canvas and fill color at same time @param fillColor: a color value @note The fillColor value range depends on the setting of _buffer_color_mode. * If it is SS_COLOR_MODE_MONO ("1") monochrome mode, it can only select 0: black and 1: white ...
def clearView(self, fillColor = 0 ): self.Canvas.rectangle(self.View.rectToArray(), outline=0, fill=fillColor)
! \~english Clear up canvas with view size @param fillColor: a color value @note The fillColor value range depends on the setting of _buffer_color_mode. * If it is SS_COLOR_MODE_MONO ("1") monochrome mode, it can only select 0: black and 1: white * If...
def redefineBuffer(self, newBuffer ): # Redefine Frame from an image object if type(self._buffer) == type(newBuffer): self._buffer = newBuffer self.Canvas = ImageDraw.Draw( self._buffer ) # self.View.resize(newBuffer.width, newBuffer.height) retur...
! \~english Redefine frame of Screen @param newFrame: a new fram data @note newFrame can be: * PIL Image * PIL ImageFile * Dictionary, eg. { "size":(width, height), "color_mode":"1" } or { "size":(width, height), "color_mode":"RGB" } ...
def resize(self, newWidth = 0, newHeight = 0): self.height = newHeight self.width = newWidth
! \~english Resize width and height of rectangles @param newWidth: new width value @param newHeight: new height value \~chinese 重新设定矩形高宽 @param newWidth: 新宽度 @param newHeight: 新高度
def adjuestSize(self, offsetWidth = 0, offsetHeight = 0): self.height += offsetHeight self.width += offsetWidth
! \~english Adjuest width and height of rectangles @param offsetWidth: adjust the width. Negative numbers are smaller, Positive number is increased @param offsetHeight: adjust the height. Negative numbers are smaller, Positive number is increased @note The negative numb...
def moveTo(self, newX=0, newY=0): self.x = newX self.y = newY
! \~english Move vertex of rectangles to new point (x,y) @param newX: Coordinated X value @param newY: Coordinated Y value \~chinese 移动矩形到新坐标点 (x,y) @param newX: 坐标 X @param newY: 坐标 Y
def moveOffset(self, offsetX=0, offsetY=0): self.x += offsetX self.y += offsetY
! \~english Offset vertex of rectangles to new point (x,y) @param offsetX: offset X value @param offsetY: offset Y value @note The negative numbers are left or up move , positive number is right or down move,0 remains unchanged. \~chinese 平移矩形指定的距离 (x,y) ...
def swapWH(self): width = self.width self.width = self.height self.height = width
! \~english Swap width and height of rectangles \~chinese 交换矩形高宽边数据
def rectToArray(self, swapWH = False): if swapWH == False: return [self.x, self.y, self.x + self.width, self.y + self.height] else: return [self.x, self.y, self.x + self.height, self.y + self.width]
! \~english Rectangles converted to array of coordinates @return: an array of rect points. eg. (x1,y1,x2,y2) \~chinese 矩形数据转换为矩形坐标数组 @return: 矩形座标数组, 例如: ( x1,y1,x2,y2 )
def _needSwapWH(self, oldDirection, newDirection ): if abs(newDirection - oldDirection) == 0: return False if abs(newDirection - oldDirection) % 180 == 0: return False if abs(newDirection - oldDirection) % 90 == 0: return True return False
! \~english return screen direction status @return Boolean @note No need to rotate if the screen orientation is 0 degrees and 180 degrees \~chinese 返回屏幕方向状态 @return 布尔值 @note 如果屏幕方向是0度和180度就不需要旋转