_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 75 19.8k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q271600 | parse_agi_result | test | def parse_agi_result(line):
"""Parse AGI results using Regular expression.
AGI Result examples::
100 result=0 Trying...
200 result=0
200 result=-1
200 result=132456
200 result= (timeout)
510 Invalid or unknown command
520-Invalid command syntax. Proper usage follows:
int() argument must be a string, a bytes-like object or a number, not
'NoneType'
HANGUP
"""
# print("--------------\n", line)
if line == 'HANGUP':
return {'error': 'AGIResultHangup',
'msg': 'User hungup during execution'}
kwargs = dict(code=0, response="", line=line)
m = re_code.search(line)
try:
kwargs.update(m.groupdict())
except AttributeError:
# None has no attribute groupdict
pass
return agi_code_check(**kwargs) | python | {
"resource": ""
} |
q271601 | agi_code_check | test | def agi_code_check(code=None, response=None, line=None):
"""
Check the AGI code and return a dict to help on error handling.
"""
code = int(code)
response = response or ""
result = {'status_code': code, 'result': ('', ''), 'msg': ''}
if code == 100:
result['msg'] = line
elif code == 200:
for key, value, data in re_kv.findall(response):
result[key] = (value, data)
# If user hangs up... we get 'hangup' in the data
if data == 'hangup':
return {
'error': 'AGIResultHangup',
'msg': 'User hungup during execution'}
elif key == 'result' and value == '-1':
return {
'error': 'AGIAppError',
'msg': 'Error executing application, or hangup'}
elif code == 510:
result['error'] = 'AGIInvalidCommand'
elif code == 520:
# AGI Usage error
result['error'] = 'AGIUsageError'
result['msg'] = line
else:
# Unhandled code or undefined response
result['error'] = 'AGIUnknownError'
result['msg'] = line
return result | python | {
"resource": ""
} |
q271602 | IdGenerator.reset | test | def reset(cls, uid=None):
"""Mostly used for unit testing. Allow to use a static uuid and reset
all counter"""
for instance in cls.instances:
if uid:
instance.uid = uid
instance.generator = instance.get_generator() | python | {
"resource": ""
} |
q271603 | IdGenerator.get_instances | test | def get_instances(self):
"""Mostly used for debugging"""
return ["<%s prefix:%s (uid:%s)>" % (self.__class__.__name__,
i.prefix, self.uid)
for i in self.instances] | python | {
"resource": ""
} |
q271604 | get_data | test | def get_data(path):
"""
Returns data from a package directory.
'path' should be an absolute path.
"""
# Run the imported setup to get the metadata.
with FakeContext(path):
with SetupMonkey() as sm:
try:
distro = run_setup('setup.py', stop_after='config')
metadata = {'_setuptools': sm.used_setuptools}
for k, v in distro.metadata.__dict__.items():
if k[0] == '_' or not v:
continue
if all(not x for x in v):
continue
metadata[k] = v
if sm.used_setuptools:
for extras in ['cmdclass', 'zip_safe', 'test_suite']:
v = getattr(distro, extras, None)
if v is not None and v not in ([], {}):
metadata[extras] = v
except ImportError as e:
# Either there is no setup py, or it's broken.
logging.exception(e)
metadata = {}
return metadata | python | {
"resource": ""
} |
q271605 | get_primary_keys | test | def get_primary_keys(model):
"""Get primary key properties for a SQLAlchemy model.
:param model: SQLAlchemy model class
"""
mapper = model.__mapper__
return [mapper.get_property_by_column(column) for column in mapper.primary_key] | python | {
"resource": ""
} |
q271606 | Related._deserialize | test | def _deserialize(self, value, *args, **kwargs):
"""Deserialize a serialized value to a model instance.
If the parent schema is transient, create a new (transient) instance.
Otherwise, attempt to find an existing instance in the database.
:param value: The value to deserialize.
"""
if not isinstance(value, dict):
if len(self.related_keys) != 1:
self.fail(
"invalid",
value=value,
keys=[prop.key for prop in self.related_keys],
)
value = {self.related_keys[0].key: value}
if self.transient:
return self.related_model(**value)
try:
result = self._get_existing_instance(
self.session.query(self.related_model), value
)
except NoResultFound:
# The related-object DNE in the DB, but we still want to deserialize it
# ...perhaps we want to add it to the DB later
return self.related_model(**value)
return result | python | {
"resource": ""
} |
q271607 | Related._get_existing_instance | test | def _get_existing_instance(self, query, value):
"""Retrieve the related object from an existing instance in the DB.
:param query: A SQLAlchemy `Query <sqlalchemy.orm.query.Query>` object.
:param value: The serialized value to mapto an existing instance.
:raises NoResultFound: if there is no matching record.
"""
if self.columns:
result = query.filter_by(
**{prop.key: value.get(prop.key) for prop in self.related_keys}
).one()
else:
# Use a faster path if the related key is the primary key.
result = query.get([value.get(prop.key) for prop in self.related_keys])
if result is None:
raise NoResultFound
return result | python | {
"resource": ""
} |
q271608 | SchemaMeta.get_declared_fields | test | def get_declared_fields(mcs, klass, cls_fields, inherited_fields, dict_cls):
"""Updates declared fields with fields converted from the SQLAlchemy model
passed as the `model` class Meta option.
"""
opts = klass.opts
Converter = opts.model_converter
converter = Converter(schema_cls=klass)
declared_fields = super(SchemaMeta, mcs).get_declared_fields(
klass, cls_fields, inherited_fields, dict_cls
)
fields = mcs.get_fields(converter, opts, declared_fields, dict_cls)
fields.update(declared_fields)
return fields | python | {
"resource": ""
} |
q271609 | ModelSchema.load | test | def load(self, data, session=None, instance=None, transient=False, *args, **kwargs):
"""Deserialize data to internal representation.
:param session: Optional SQLAlchemy session.
:param instance: Optional existing instance to modify.
:param transient: Optional switch to allow transient instantiation.
"""
self._session = session or self._session
self._transient = transient or self._transient
if not (self.transient or self.session):
raise ValueError("Deserialization requires a session")
self.instance = instance or self.instance
try:
return super(ModelSchema, self).load(data, *args, **kwargs)
finally:
self.instance = None | python | {
"resource": ""
} |
q271610 | ModelSchema._split_model_kwargs_association | test | def _split_model_kwargs_association(self, data):
"""Split serialized attrs to ensure association proxies are passed separately.
This is necessary for Python < 3.6.0, as the order in which kwargs are passed
is non-deterministic, and associations must be parsed by sqlalchemy after their
intermediate relationship, unless their `creator` has been set.
Ignore invalid keys at this point - behaviour for unknowns should be
handled elsewhere.
:param data: serialized dictionary of attrs to split on association_proxy.
"""
association_attrs = {
key: value
for key, value in iteritems(data)
# association proxy
if hasattr(getattr(self.opts.model, key, None), "remote_attr")
}
kwargs = {
key: value
for key, value in iteritems(data)
if (hasattr(self.opts.model, key) and key not in association_attrs)
}
return kwargs, association_attrs | python | {
"resource": ""
} |
q271611 | gc | test | def gc():
"""Deletes old stellar tables that are not used anymore"""
def after_delete(database):
click.echo("Deleted table %s" % database)
app = get_app()
upgrade_from_old_version(app)
app.delete_orphan_snapshots(after_delete) | python | {
"resource": ""
} |
q271612 | snapshot | test | def snapshot(name):
"""Takes a snapshot of the database"""
app = get_app()
upgrade_from_old_version(app)
name = name or app.default_snapshot_name
if app.get_snapshot(name):
click.echo("Snapshot with name %s already exists" % name)
sys.exit(1)
else:
def before_copy(table_name):
click.echo("Snapshotting database %s" % table_name)
app.create_snapshot(name, before_copy=before_copy) | python | {
"resource": ""
} |
q271613 | list | test | def list():
"""Returns a list of snapshots"""
snapshots = get_app().get_snapshots()
click.echo('\n'.join(
'%s: %s' % (
s.snapshot_name,
humanize.naturaltime(datetime.utcnow() - s.created_at)
)
for s in snapshots
)) | python | {
"resource": ""
} |
q271614 | restore | test | def restore(name):
"""Restores the database from a snapshot"""
app = get_app()
if not name:
snapshot = app.get_latest_snapshot()
if not snapshot:
click.echo(
"Couldn't find any snapshots for project %s" %
load_config()['project_name']
)
sys.exit(1)
else:
snapshot = app.get_snapshot(name)
if not snapshot:
click.echo(
"Couldn't find snapshot with name %s.\n"
"You can list snapshots with 'stellar list'" % name
)
sys.exit(1)
# Check if slaves are ready
if not snapshot.slaves_ready:
if app.is_copy_process_running(snapshot):
sys.stdout.write(
'Waiting for background process(%s) to finish' %
snapshot.worker_pid
)
sys.stdout.flush()
while not snapshot.slaves_ready:
sys.stdout.write('.')
sys.stdout.flush()
sleep(1)
app.db.session.refresh(snapshot)
click.echo('')
else:
click.echo('Background process missing, doing slow restore.')
app.inline_slave_copy(snapshot)
app.restore(snapshot)
click.echo('Restore complete.') | python | {
"resource": ""
} |
q271615 | remove | test | def remove(name):
"""Removes a snapshot"""
app = get_app()
snapshot = app.get_snapshot(name)
if not snapshot:
click.echo("Couldn't find snapshot %s" % name)
sys.exit(1)
click.echo("Deleting snapshot %s" % name)
app.remove_snapshot(snapshot)
click.echo("Deleted") | python | {
"resource": ""
} |
q271616 | rename | test | def rename(old_name, new_name):
"""Renames a snapshot"""
app = get_app()
snapshot = app.get_snapshot(old_name)
if not snapshot:
click.echo("Couldn't find snapshot %s" % old_name)
sys.exit(1)
new_snapshot = app.get_snapshot(new_name)
if new_snapshot:
click.echo("Snapshot with name %s already exists" % new_name)
sys.exit(1)
app.rename_snapshot(snapshot, new_name)
click.echo("Renamed snapshot %s to %s" % (old_name, new_name)) | python | {
"resource": ""
} |
q271617 | replace | test | def replace(name):
"""Replaces a snapshot"""
app = get_app()
snapshot = app.get_snapshot(name)
if not snapshot:
click.echo("Couldn't find snapshot %s" % name)
sys.exit(1)
app.remove_snapshot(snapshot)
app.create_snapshot(name)
click.echo("Replaced snapshot %s" % name) | python | {
"resource": ""
} |
q271618 | Neg_Sampling_Data_Gen.on_epoch_end | test | def on_epoch_end(self) -> None:
'Updates indexes after each epoch for shuffling'
self.indexes = np.arange(self.nrows)
if self.shuffle:
np.random.shuffle(self.indexes) | python | {
"resource": ""
} |
q271619 | textacy_cleaner | test | def textacy_cleaner(text: str) -> str:
"""
Defines the default function for cleaning text.
This function operates over a list.
"""
return preprocess_text(text,
fix_unicode=True,
lowercase=True,
transliterate=True,
no_urls=True,
no_emails=True,
no_phone_numbers=True,
no_numbers=True,
no_currency_symbols=True,
no_punct=True,
no_contractions=False,
no_accents=True) | python | {
"resource": ""
} |
q271620 | apply_parallel | test | def apply_parallel(func: Callable,
data: List[Any],
cpu_cores: int = None) -> List[Any]:
"""
Apply function to list of elements.
Automatically determines the chunk size.
"""
if not cpu_cores:
cpu_cores = cpu_count()
try:
chunk_size = ceil(len(data) / cpu_cores)
pool = Pool(cpu_cores)
transformed_data = pool.map(func, chunked(data, chunk_size), chunksize=1)
finally:
pool.close()
pool.join()
return transformed_data | python | {
"resource": ""
} |
q271621 | process_text_constructor | test | def process_text_constructor(cleaner: Callable,
tokenizer: Callable,
append_indicators: bool,
start_tok: str,
end_tok: str):
"""Generate a function that will clean and tokenize text."""
def process_text(text):
if append_indicators:
return [[start_tok] + tokenizer(cleaner(doc)) + [end_tok] for doc in text]
return [tokenizer(cleaner(doc)) for doc in text]
return process_text | python | {
"resource": ""
} |
q271622 | processor.process_text | test | def process_text(self, text: List[str]) -> List[List[str]]:
"""Combine the cleaner and tokenizer."""
process_text = process_text_constructor(cleaner=self.cleaner,
tokenizer=self.tokenizer,
append_indicators=self.append_indicators,
start_tok=self.start_tok,
end_tok=self.end_tok)
return process_text(text) | python | {
"resource": ""
} |
q271623 | processor.parallel_process_text | test | def parallel_process_text(self, data: List[str]) -> List[List[str]]:
"""Apply cleaner -> tokenizer."""
process_text = process_text_constructor(cleaner=self.cleaner,
tokenizer=self.tokenizer,
append_indicators=self.append_indicators,
start_tok=self.start_tok,
end_tok=self.end_tok)
n_cores = self.num_cores
return flattenlist(apply_parallel(process_text, data, n_cores)) | python | {
"resource": ""
} |
q271624 | processor.generate_doc_length_stats | test | def generate_doc_length_stats(self):
"""Analyze document length statistics for padding strategy"""
heuristic = self.heuristic_pct
histdf = (pd.DataFrame([(a, b) for a, b in self.document_length_histogram.items()],
columns=['bin', 'doc_count'])
.sort_values(by='bin'))
histdf['cumsum_pct'] = histdf.doc_count.cumsum() / histdf.doc_count.sum()
self.document_length_stats = histdf
self.doc_length_huerestic = histdf.query(f'cumsum_pct >= {heuristic}').bin.head(1).values[0]
logging.warning(' '.join(["Setting maximum document length to",
f'{self.doc_length_huerestic} based upon',
f'heuristic of {heuristic} percentile.\n',
'See full histogram by insepecting the',
"`document_length_stats` attribute."]))
self.padding_maxlen = self.doc_length_huerestic | python | {
"resource": ""
} |
q271625 | processor.token_count_pandas | test | def token_count_pandas(self):
""" See token counts as pandas dataframe"""
freq_df = pd.DataFrame.from_dict(self.indexer.word_counts, orient='index')
freq_df.columns = ['count']
return freq_df.sort_values('count', ascending=False) | python | {
"resource": ""
} |
q271626 | map_param_type | test | def map_param_type(param_type):
"""
Perform param type mapping
This requires a bit of logic since this isn't standardized.
If a type doesn't map, assume str
"""
main_type, sub_type = TYPE_INFO_RE.match(param_type).groups()
if main_type in ('list', 'array'):
# Handle no sub-type: "required list"
if sub_type is not None:
sub_type = sub_type.strip()
if not sub_type:
sub_type = 'str'
# Handle list of pairs: "optional list<pair<callsign, path>>"
sub_match = TYPE_INFO_RE.match(sub_type)
if sub_match:
sub_type = sub_match.group(1).lower()
return [PARAM_TYPE_MAP.setdefault(sub_type, string_types)]
return PARAM_TYPE_MAP.setdefault(main_type, string_types) | python | {
"resource": ""
} |
q271627 | parse_interfaces | test | def parse_interfaces(interfaces):
"""
Parse the conduit.query json dict response
This performs the logic of parsing the non-standard params dict
and then returning a dict Resource can understand
"""
parsed_interfaces = collections.defaultdict(dict)
for m, d in iteritems(interfaces):
app, func = m.split('.', 1)
method = parsed_interfaces[app][func] = {}
# Make default assumptions since these aren't provided by Phab
method['formats'] = ['json', 'human']
method['method'] = 'POST'
method['optional'] = {}
method['required'] = {}
for name, type_info in iteritems(dict(d['params'])):
# Set the defaults
optionality = 'required'
param_type = 'string'
# Usually in the format: <optionality> <param_type>
type_info = TYPE_INFO_COMMENT_RE.sub('', type_info)
info_pieces = TYPE_INFO_SPLITTER_RE.findall(type_info)
for info_piece in info_pieces:
if info_piece in ('optional', 'required'):
optionality = info_piece
elif info_piece == 'ignored':
optionality = 'optional'
param_type = 'string'
elif info_piece == 'nonempty':
optionality = 'required'
elif info_piece == 'deprecated':
optionality = 'optional'
else:
param_type = info_piece
method[optionality][name] = map_param_type(param_type)
return dict(parsed_interfaces) | python | {
"resource": ""
} |
q271628 | BidictBase.inverse | test | def inverse(self):
"""The inverse of this bidict.
*See also* :attr:`inv`
"""
# Resolve and return a strong reference to the inverse bidict.
# One may be stored in self._inv already.
if self._inv is not None:
return self._inv
# Otherwise a weakref is stored in self._invweak. Try to get a strong ref from it.
inv = self._invweak()
if inv is not None:
return inv
# Refcount of referent must have dropped to zero, as in `bidict().inv.inv`. Init a new one.
self._init_inv() # Now this bidict will retain a strong ref to its inverse.
return self._inv | python | {
"resource": ""
} |
q271629 | BidictBase._update_with_rollback | test | def _update_with_rollback(self, on_dup, *args, **kw):
"""Update, rolling back on failure."""
writelog = []
appendlog = writelog.append
dedup_item = self._dedup_item
write_item = self._write_item
for (key, val) in _iteritems_args_kw(*args, **kw):
try:
dedup_result = dedup_item(key, val, on_dup)
except DuplicationError:
undo_write = self._undo_write
for dedup_result, write_result in reversed(writelog):
undo_write(dedup_result, write_result)
raise
if dedup_result is not _NOOP:
write_result = write_item(key, val, dedup_result)
appendlog((dedup_result, write_result)) | python | {
"resource": ""
} |
q271630 | BidictBase.copy | test | def copy(self):
"""A shallow copy."""
# Could just ``return self.__class__(self)`` here instead, but the below is faster. It uses
# __new__ to create a copy instance while bypassing its __init__, which would result
# in copying this bidict's items into the copy instance one at a time. Instead, make whole
# copies of each of the backing mappings, and make them the backing mappings of the copy,
# avoiding copying items one at a time.
copy = self.__class__.__new__(self.__class__)
copy._fwdm = self._fwdm.copy() # pylint: disable=protected-access
copy._invm = self._invm.copy() # pylint: disable=protected-access
copy._init_inv() # pylint: disable=protected-access
return copy | python | {
"resource": ""
} |
q271631 | OrderedBidictBase.copy | test | def copy(self):
"""A shallow copy of this ordered bidict."""
# Fast copy implementation bypassing __init__. See comments in :meth:`BidictBase.copy`.
copy = self.__class__.__new__(self.__class__)
sntl = _Sentinel()
fwdm = self._fwdm.copy()
invm = self._invm.copy()
cur = sntl
nxt = sntl.nxt
for (key, val) in iteritems(self):
nxt = _Node(cur, sntl)
cur.nxt = fwdm[key] = invm[val] = nxt
cur = nxt
sntl.prv = nxt
copy._sntl = sntl # pylint: disable=protected-access
copy._fwdm = fwdm # pylint: disable=protected-access
copy._invm = invm # pylint: disable=protected-access
copy._init_inv() # pylint: disable=protected-access
return copy | python | {
"resource": ""
} |
q271632 | OrderedBidictBase.equals_order_sensitive | test | def equals_order_sensitive(self, other):
"""Order-sensitive equality check.
*See also* :ref:`eq-order-insensitive`
"""
# Same short-circuit as BidictBase.__eq__. Factoring out not worth function call overhead.
if not isinstance(other, Mapping) or len(self) != len(other):
return False
return all(i == j for (i, j) in izip(iteritems(self), iteritems(other))) | python | {
"resource": ""
} |
q271633 | inverted | test | def inverted(arg):
"""Yield the inverse items of the provided object.
If *arg* has a :func:`callable` ``__inverted__`` attribute,
return the result of calling it.
Otherwise, return an iterator over the items in `arg`,
inverting each item on the fly.
*See also* :attr:`bidict.BidirectionalMapping.__inverted__`
"""
inv = getattr(arg, '__inverted__', None)
if callable(inv):
return inv()
return ((val, key) for (key, val) in _iteritems_mapping_or_iterable(arg)) | python | {
"resource": ""
} |
q271634 | OrderedBidict.clear | test | def clear(self):
"""Remove all items."""
self._fwdm.clear()
self._invm.clear()
self._sntl.nxt = self._sntl.prv = self._sntl | python | {
"resource": ""
} |
q271635 | OrderedBidict.move_to_end | test | def move_to_end(self, key, last=True):
"""Move an existing key to the beginning or end of this ordered bidict.
The item is moved to the end if *last* is True, else to the beginning.
:raises KeyError: if the key does not exist
"""
node = self._fwdm[key]
node.prv.nxt = node.nxt
node.nxt.prv = node.prv
sntl = self._sntl
if last:
last = sntl.prv
node.prv = last
node.nxt = sntl
sntl.prv = last.nxt = node
else:
first = sntl.nxt
node.prv = sntl
node.nxt = first
sntl.nxt = first.prv = node | python | {
"resource": ""
} |
q271636 | write_temp_file | test | def write_temp_file(text=""):
"""Create a new temporary file and write some initial text to it.
:param text: the text to write to the temp file
:type text: str
:returns: the file name of the newly created temp file
:rtype: str
"""
with NamedTemporaryFile(mode='w+t', suffix='.yml', delete=False) \
as tempfile:
tempfile.write(text)
return tempfile.name | python | {
"resource": ""
} |
q271637 | get_contacts | test | def get_contacts(address_books, query, method="all", reverse=False,
group=False, sort="first_name"):
"""Get a list of contacts from one or more address books.
:param address_books: the address books to search
:type address_books: list(address_book.AddressBook)
:param query: a search query to select contacts
:type quer: str
:param method: the search method, one of "all", "name" or "uid"
:type method: str
:param reverse: reverse the order of the returned contacts
:type reverse: bool
:param group: group results by address book
:type group: bool
:param sort: the field to use for sorting, one of "first_name", "last_name"
:type sort: str
:returns: contacts from the address_books that match the query
:rtype: list(CarddavObject)
"""
# Search for the contacts in all address books.
contacts = []
for address_book in address_books:
contacts.extend(address_book.search(query, method=method))
# Sort the contacts.
if group:
if sort == "first_name":
return sorted(contacts, reverse=reverse, key=lambda x: (
unidecode(x.address_book.name).lower(),
unidecode(x.get_first_name_last_name()).lower()))
elif sort == "last_name":
return sorted(contacts, reverse=reverse, key=lambda x: (
unidecode(x.address_book.name).lower(),
unidecode(x.get_last_name_first_name()).lower()))
else:
raise ValueError('sort must be "first_name" or "last_name" not '
'{}.'.format(sort))
else:
if sort == "first_name":
return sorted(contacts, reverse=reverse, key=lambda x:
unidecode(x.get_first_name_last_name()).lower())
elif sort == "last_name":
return sorted(contacts, reverse=reverse, key=lambda x:
unidecode(x.get_last_name_first_name()).lower())
else:
raise ValueError('sort must be "first_name" or "last_name" not '
'{}.'.format(sort)) | python | {
"resource": ""
} |
q271638 | merge_args_into_config | test | def merge_args_into_config(args, config):
"""Merge the parsed arguments from argparse into the config object.
:param args: the parsed command line arguments
:type args: argparse.Namespace
:param config: the parsed config file
:type config: config.Config
:returns: the merged config object
:rtype: config.Config
"""
# display by name: first or last name
if "display" in args and args.display:
config.set_display_by_name(args.display)
# group by address book
if "group_by_addressbook" in args and args.group_by_addressbook:
config.set_group_by_addressbook(True)
# reverse contact list
if "reverse" in args and args.reverse:
config.set_reverse(True)
# sort criteria: first or last name
if "sort" in args and args.sort:
config.sort = args.sort
# preferred vcard version
if "vcard_version" in args and args.vcard_version:
config.set_preferred_vcard_version(args.vcard_version)
# search in source files
if "search_in_source_files" in args and args.search_in_source_files:
config.set_search_in_source_files(True)
# skip unparsable vcards
if "skip_unparsable" in args and args.skip_unparsable:
config.set_skip_unparsable(True)
# If the user could but did not specify address books on the command line
# it means they want to use all address books in that place.
if "addressbook" in args and not args.addressbook:
args.addressbook = [abook.name for abook in config.abooks]
if "target_addressbook" in args and not args.target_addressbook:
args.target_addressbook = [abook.name for abook in config.abooks] | python | {
"resource": ""
} |
q271639 | load_address_books | test | def load_address_books(names, config, search_queries):
"""Load all address books with the given names from the config.
:param names: the address books to load
:type names: list(str)
:param config: the config instance to use when looking up address books
:type config: config.Config
:param search_queries: a mapping of address book names to search queries
:type search_queries: dict
:yields: the loaded address books
:ytype: addressbook.AddressBook
"""
all_names = {str(book) for book in config.abooks}
if not names:
names = all_names
elif not all_names.issuperset(names):
sys.exit('Error: The entered address books "{}" do not exist.\n'
'Possible values are: {}'.format(
'", "'.join(set(names) - all_names),
', '.join(all_names)))
# load address books which are defined in the configuration file
for name in names:
address_book = config.abook.get_abook(name)
address_book.load(search_queries[address_book.name],
search_in_source_files=config.search_in_source_files())
yield address_book | python | {
"resource": ""
} |
q271640 | prepare_search_queries | test | def prepare_search_queries(args):
"""Prepare the search query string from the given command line args.
Each address book can get a search query string to filter vcards befor
loading them. Depending on the question if the address book is used for
source or target searches different regexes have to be combined into one
search string.
:param args: the parsed command line
:type args: argparse.Namespace
:returns: a dict mapping abook names to their loading queries, if the query
is None it means that all cards should be loaded
:rtype: dict(str:str or None)
"""
# get all possible search queries for address book parsing
source_queries = []
target_queries = []
if "source_search_terms" in args and args.source_search_terms:
escaped_term = ".*".join(re.escape(x)
for x in args.source_search_terms)
source_queries.append(escaped_term)
args.source_search_terms = escaped_term
if "search_terms" in args and args.search_terms:
escaped_term = ".*".join(re.escape(x) for x in args.search_terms)
source_queries.append(escaped_term)
args.search_terms = escaped_term
if "target_contact" in args and args.target_contact:
escaped_term = re.escape(args.target_contact)
target_queries.append(escaped_term)
args.target_contact = escaped_term
if "uid" in args and args.uid:
source_queries.append(args.uid)
if "target_uid" in args and args.target_uid:
target_queries.append(args.target_uid)
# create and return regexp, None means that no query is given and hence all
# contacts should be searched.
source_queries = "^.*(%s).*$" % ')|('.join(source_queries) \
if source_queries else None
target_queries = "^.*(%s).*$" % ')|('.join(target_queries) \
if target_queries else None
logging.debug('Created source query regex: %s', source_queries)
logging.debug('Created target query regex: %s', target_queries)
# Get all possible search queries for address book parsing, always
# depending on the fact if the address book is used to find source or
# target contacts or both.
queries = {abook.name: [] for abook in config.abook._abooks}
for name in queries:
if "addressbook" in args and name in args.addressbook:
queries[name].append(source_queries)
if "target_addressbook" in args and name in args.target_addressbook:
queries[name].append(target_queries)
# If None is included in the search queries of an address book it means
# that either no source or target query was given and this address book
# is affected by this. All contacts should be loaded from that address
# book.
if None in queries[name]:
queries[name] = None
else:
queries[name] = "({})".format(')|('.join(queries[name]))
logging.debug('Created query regex: %s', queries)
return queries | python | {
"resource": ""
} |
q271641 | new_subcommand | test | def new_subcommand(selected_address_books, input_from_stdin_or_file,
open_editor):
"""Create a new contact.
:param selected_address_books: a list of addressbooks that were selected on
the command line
:type selected_address_books: list of address_book.AddressBook
:param input_from_stdin_or_file: the data for the new contact as a yaml
formatted string
:type input_from_stdin_or_file: str
:param open_editor: whether to open the new contact in the edior after
creation
:type open_editor: bool
:returns: None
:rtype: None
"""
# ask for address book, in which to create the new contact
selected_address_book = choose_address_book_from_list(
"Select address book for new contact", selected_address_books)
if selected_address_book is None:
print("Error: address book list is empty")
sys.exit(1)
# if there is some data in stdin
if input_from_stdin_or_file:
# create new contact from stdin
try:
new_contact = CarddavObject.from_user_input(
selected_address_book, input_from_stdin_or_file,
config.get_supported_private_objects(),
config.get_preferred_vcard_version(),
config.localize_dates())
except ValueError as err:
print(err)
sys.exit(1)
else:
new_contact.write_to_file()
if open_editor:
modify_existing_contact(new_contact)
else:
print("Creation successful\n\n%s" % new_contact.print_vcard())
else:
create_new_contact(selected_address_book) | python | {
"resource": ""
} |
q271642 | birthdays_subcommand | test | def birthdays_subcommand(vcard_list, parsable):
"""Print birthday contact table.
:param vcard_list: the vcards to search for matching entries which should
be printed
:type vcard_list: list of carddav_object.CarddavObject
:param parsable: machine readable output: columns devided by tabulator (\t)
:type parsable: bool
:returns: None
:rtype: None
"""
# filter out contacts without a birthday date
vcard_list = [
vcard for vcard in vcard_list if vcard.get_birthday() is not None]
# sort by date (month and day)
# The sort function should work for strings and datetime objects. All
# strings will besorted before any datetime objects.
vcard_list.sort(
key=lambda x: (x.get_birthday().month, x.get_birthday().day)
if isinstance(x.get_birthday(), datetime.datetime)
else (0, 0, x.get_birthday()))
# add to string list
birthday_list = []
for vcard in vcard_list:
date = vcard.get_birthday()
if parsable:
if config.display_by_name() == "first_name":
birthday_list.append("%04d.%02d.%02d\t%s"
% (date.year, date.month, date.day,
vcard.get_first_name_last_name()))
else:
birthday_list.append("%04d.%02d.%02d\t%s"
% (date.year, date.month, date.day,
vcard.get_last_name_first_name()))
else:
if config.display_by_name() == "first_name":
birthday_list.append("%s\t%s"
% (vcard.get_first_name_last_name(),
vcard.get_formatted_birthday()))
else:
birthday_list.append("%s\t%s"
% (vcard.get_last_name_first_name(),
vcard.get_formatted_birthday()))
if birthday_list:
if parsable:
print('\n'.join(birthday_list))
else:
list_birthdays(birthday_list)
else:
if not parsable:
print("Found no birthdays")
sys.exit(1) | python | {
"resource": ""
} |
q271643 | phone_subcommand | test | def phone_subcommand(search_terms, vcard_list, parsable):
"""Print a phone application friendly contact table.
:param search_terms: used as search term to filter the contacts before
printing
:type search_terms: str
:param vcard_list: the vcards to search for matching entries which should
be printed
:type vcard_list: list of carddav_object.CarddavObject
:param parsable: machine readable output: columns devided by tabulator (\t)
:type parsable: bool
:returns: None
:rtype: None
"""
all_phone_numbers_list = []
matching_phone_number_list = []
for vcard in vcard_list:
for type, number_list in sorted(vcard.get_phone_numbers().items(),
key=lambda k: k[0].lower()):
for number in sorted(number_list):
if config.display_by_name() == "first_name":
name = vcard.get_first_name_last_name()
else:
name = vcard.get_last_name_first_name()
# create output lines
line_formatted = "\t".join([name, type, number])
line_parsable = "\t".join([number, name, type])
if parsable:
# parsable option: start with phone number
phone_number_line = line_parsable
else:
# else: start with name
phone_number_line = line_formatted
if re.search(search_terms,
"%s\n%s" % (line_formatted, line_parsable),
re.IGNORECASE | re.DOTALL):
matching_phone_number_list.append(phone_number_line)
elif len(re.sub("\D", "", search_terms)) >= 3:
# The user likely searches for a phone number cause the
# search string contains at least three digits. So we
# remove all non-digit chars from the phone number field
# and match against that.
if re.search(re.sub("\D", "", search_terms),
re.sub("\D", "", number), re.IGNORECASE):
matching_phone_number_list.append(phone_number_line)
# collect all phone numbers in a different list as fallback
all_phone_numbers_list.append(phone_number_line)
if matching_phone_number_list:
if parsable:
print('\n'.join(matching_phone_number_list))
else:
list_phone_numbers(matching_phone_number_list)
elif all_phone_numbers_list:
if parsable:
print('\n'.join(all_phone_numbers_list))
else:
list_phone_numbers(all_phone_numbers_list)
else:
if not parsable:
print("Found no phone numbers")
sys.exit(1) | python | {
"resource": ""
} |
q271644 | list_subcommand | test | def list_subcommand(vcard_list, parsable):
"""Print a user friendly contacts table.
:param vcard_list: the vcards to print
:type vcard_list: list of carddav_object.CarddavObject
:param parsable: machine readable output: columns devided by tabulator (\t)
:type parsable: bool
:returns: None
:rtype: None
"""
if not vcard_list:
if not parsable:
print("Found no contacts")
sys.exit(1)
elif parsable:
contact_line_list = []
for vcard in vcard_list:
if config.display_by_name() == "first_name":
name = vcard.get_first_name_last_name()
else:
name = vcard.get_last_name_first_name()
contact_line_list.append('\t'.join([vcard.get_uid(), name,
vcard.address_book.name]))
print('\n'.join(contact_line_list))
else:
list_contacts(vcard_list) | python | {
"resource": ""
} |
q271645 | modify_subcommand | test | def modify_subcommand(selected_vcard, input_from_stdin_or_file, open_editor):
"""Modify a contact in an external editor.
:param selected_vcard: the contact to modify
:type selected_vcard: carddav_object.CarddavObject
:param input_from_stdin_or_file: new data from stdin (or a file) that
should be incorperated into the contact, this should be a yaml
formatted string
:type input_from_stdin_or_file: str
:param open_editor: whether to open the new contact in the edior after
creation
:type open_editor: bool
:returns: None
:rtype: None
"""
# show warning, if vcard version of selected contact is not 3.0 or 4.0
if selected_vcard.get_version() not in config.supported_vcard_versions:
print("Warning:\nThe selected contact is based on vcard version %s "
"but khard only supports the creation and modification of vcards"
" with version 3.0 and 4.0.\nIf you proceed, the contact will be"
" converted to vcard version %s but beware: This could corrupt "
"the contact file or cause data loss."
% (selected_vcard.get_version(),
config.get_preferred_vcard_version()))
while True:
input_string = input("Do you want to proceed anyway (y/n)? ")
if input_string.lower() in ["", "n", "q"]:
print("Canceled")
sys.exit(0)
if input_string.lower() == "y":
break
# if there is some data in stdin
if input_from_stdin_or_file:
# create new contact from stdin
try:
new_contact = \
CarddavObject.from_existing_contact_with_new_user_input(
selected_vcard, input_from_stdin_or_file,
config.localize_dates())
except ValueError as err:
print(err)
sys.exit(1)
if selected_vcard == new_contact:
print("Nothing changed\n\n%s" % new_contact.print_vcard())
else:
print("Modification\n\n%s\n" % new_contact.print_vcard())
while True:
input_string = input("Do you want to proceed (y/n)? ")
if input_string.lower() in ["", "n", "q"]:
print("Canceled")
break
if input_string.lower() == "y":
new_contact.write_to_file(overwrite=True)
if open_editor:
modify_existing_contact(new_contact)
else:
print("Done")
break
else:
modify_existing_contact(selected_vcard) | python | {
"resource": ""
} |
q271646 | remove_subcommand | test | def remove_subcommand(selected_vcard, force):
"""Remove a contact from the addressbook.
:param selected_vcard: the contact to delete
:type selected_vcard: carddav_object.CarddavObject
:param force: delete without confirmation
:type force: bool
:returns: None
:rtype: None
"""
if not force:
while True:
input_string = input(
"Deleting contact %s from address book %s. Are you sure? "
"(y/n): " % (selected_vcard, selected_vcard.address_book))
if input_string.lower() in ["", "n", "q"]:
print("Canceled")
sys.exit(0)
if input_string.lower() == "y":
break
selected_vcard.delete_vcard_file()
print("Contact %s deleted successfully" % selected_vcard.get_full_name()) | python | {
"resource": ""
} |
q271647 | source_subcommand | test | def source_subcommand(selected_vcard, editor):
"""Open the vcard file for a contact in an external editor.
:param selected_vcard: the contact to edit
:type selected_vcard: carddav_object.CarddavObject
:param editor: the eitor command to use
:type editor: str
:returns: None
:rtype: None
"""
child = subprocess.Popen([editor, selected_vcard.filename])
child.communicate() | python | {
"resource": ""
} |
q271648 | merge_subcommand | test | def merge_subcommand(vcard_list, selected_address_books, search_terms,
target_uid):
"""Merge two contacts into one.
:param vcard_list: the vcards from which to choose contacts for mergeing
:type vcard_list: list of carddav_object.CarddavObject
:param selected_address_books: the addressbooks to use to find the target
contact
:type selected_address_books: list(addressbook.AddressBook)
:param search_terms: the search terms to find the target contact
:type search_terms: str
:param target_uid: the uid of the target contact or empty
:type target_uid: str
:returns: None
:rtype: None
"""
# Check arguments.
if target_uid != "" and search_terms != "":
print("You can not specify a target uid and target search terms for a "
"merge.")
sys.exit(1)
# Find possible target contacts.
if target_uid != "":
target_vcards = get_contacts(selected_address_books, target_uid,
method="uid")
# We require that the uid given can uniquely identify a contact.
if len(target_vcards) != 1:
if not target_vcards:
print("Found no contact for target uid %s" % target_uid)
else:
print("Found multiple contacts for target uid %s" % target_uid)
for vcard in target_vcards:
print(" %s: %s" % (vcard, vcard.get_uid()))
sys.exit(1)
else:
target_vcards = get_contact_list_by_user_selection(
selected_address_books, search_terms, False)
# get the source vcard, from which to merge
source_vcard = choose_vcard_from_list("Select contact from which to merge",
vcard_list)
if source_vcard is None:
print("Found no source contact for merging")
sys.exit(1)
else:
print("Merge from %s from address book %s\n\n"
% (source_vcard, source_vcard.address_book))
# get the target vcard, into which to merge
target_vcard = choose_vcard_from_list("Select contact into which to merge",
target_vcards)
if target_vcard is None:
print("Found no target contact for merging")
sys.exit(1)
else:
print("Merge into %s from address book %s\n\n"
% (target_vcard, target_vcard.address_book))
# merging
if source_vcard == target_vcard:
print("The selected contacts are already identical")
else:
merge_existing_contacts(source_vcard, target_vcard, True) | python | {
"resource": ""
} |
q271649 | copy_or_move_subcommand | test | def copy_or_move_subcommand(action, vcard_list, target_address_book_list):
"""Copy or move a contact to a different address book.
:action: the string "copy" or "move" to indicate what to do
:type action: str
:param vcard_list: the contact list from which to select one for the action
:type vcard_list: list of carddav_object.CarddavObject
:param target_address_book_list: the list of target address books
:type target_address_book_list: list(addressbook.AddressBook)
:returns: None
:rtype: None
"""
# get the source vcard, which to copy or move
source_vcard = choose_vcard_from_list(
"Select contact to %s" % action.title(), vcard_list)
if source_vcard is None:
print("Found no contact")
sys.exit(1)
else:
print("%s contact %s from address book %s"
% (action.title(), source_vcard, source_vcard.address_book))
# get target address book
if len(target_address_book_list) == 1 \
and target_address_book_list[0] == source_vcard.address_book:
print("The address book %s already contains the contact %s"
% (target_address_book_list[0], source_vcard))
sys.exit(1)
else:
available_address_books = [abook for abook in target_address_book_list
if abook != source_vcard.address_book]
selected_target_address_book = choose_address_book_from_list(
"Select target address book", available_address_books)
if selected_target_address_book is None:
print("Error: address book list is empty")
sys.exit(1)
# check if a contact already exists in the target address book
target_vcard = choose_vcard_from_list(
"Select target contact which to overwrite",
get_contact_list_by_user_selection([selected_target_address_book],
source_vcard.get_full_name(), True))
# If the target contact doesn't exist, move or copy the source contact into
# the target address book without further questions.
if target_vcard is None:
copy_contact(source_vcard, selected_target_address_book,
action == "move")
else:
if source_vcard == target_vcard:
# source and target contact are identical
print("Target contact: %s" % target_vcard)
if action == "move":
copy_contact(source_vcard, selected_target_address_book, True)
else:
print("The selected contacts are already identical")
else:
# source and target contacts are different
# either overwrite the target one or merge into target contact
print("The address book %s already contains the contact %s\n\n"
"Source\n\n%s\n\nTarget\n\n%s\n\n"
"Possible actions:\n"
" a: %s anyway\n"
" m: Merge from source into target contact\n"
" o: Overwrite target contact\n"
" q: Quit" % (
target_vcard.address_book, source_vcard,
source_vcard.print_vcard(), target_vcard.print_vcard(),
"Move" if action == "move" else "Copy"))
while True:
input_string = input("Your choice: ")
if input_string.lower() == "a":
copy_contact(source_vcard, selected_target_address_book,
action == "move")
break
if input_string.lower() == "o":
copy_contact(source_vcard, selected_target_address_book,
action == "move")
target_vcard.delete_vcard_file()
break
if input_string.lower() == "m":
merge_existing_contacts(source_vcard, target_vcard,
action == "move")
break
if input_string.lower() in ["", "q"]:
print("Canceled")
break | python | {
"resource": ""
} |
q271650 | Actions.get_action | test | def get_action(cls, alias):
"""Find the name of the action for the supplied alias. If no action is
asociated with the given alias, None is returned.
:param alias: the alias to look up
:type alias: str
:rturns: the name of the corresponding action or None
:rtype: str or NoneType
"""
for action, alias_list in cls.action_map.items():
if alias in alias_list:
return action
return None | python | {
"resource": ""
} |
q271651 | Config._convert_boolean_config_value | test | def _convert_boolean_config_value(config, name, default=True):
"""Convert the named field to bool.
The current value should be one of the strings "yes" or "no". It will
be replaced with its boolean counterpart. If the field is not present
in the config object, the default value is used.
:param config: the config section where to set the option
:type config: configobj.ConfigObj
:param name: the name of the option to convert
:type name: str
:param default: the default value to use if the option was not
previously set
:type default: bool
:returns: None
"""
if name not in config:
config[name] = default
elif config[name] == "yes":
config[name] = True
elif config[name] == "no":
config[name] = False
else:
raise ValueError("Error in config file\nInvalid value for %s "
"parameter\nPossible values: yes, no" % name) | python | {
"resource": ""
} |
q271652 | CarddavObject.new_contact | test | def new_contact(cls, address_book, supported_private_objects, version,
localize_dates):
"""Use this to create a new and empty contact."""
return cls(address_book, None, supported_private_objects, version,
localize_dates) | python | {
"resource": ""
} |
q271653 | CarddavObject.from_file | test | def from_file(cls, address_book, filename, supported_private_objects,
localize_dates):
"""
Use this if you want to create a new contact from an existing .vcf
file.
"""
return cls(address_book, filename, supported_private_objects, None,
localize_dates) | python | {
"resource": ""
} |
q271654 | CarddavObject.from_user_input | test | def from_user_input(cls, address_book, user_input,
supported_private_objects, version, localize_dates):
"""Use this if you want to create a new contact from user input."""
contact = cls(address_book, None, supported_private_objects, version,
localize_dates)
contact._process_user_input(user_input)
return contact | python | {
"resource": ""
} |
q271655 | CarddavObject.from_existing_contact_with_new_user_input | test | def from_existing_contact_with_new_user_input(cls, contact, user_input,
localize_dates):
"""
Use this if you want to clone an existing contact and replace its data
with new user input in one step.
"""
contact = cls(contact.address_book, contact.filename,
contact.supported_private_objects, None, localize_dates)
contact._process_user_input(user_input)
return contact | python | {
"resource": ""
} |
q271656 | CarddavObject._get_names_part | test | def _get_names_part(self, part):
"""Get some part of the "N" entry in the vCard as a list
:param part: the name to get e.g. "prefix" or "given"
:type part: str
:returns: a list of entries for this name part
:rtype: list(str)
"""
try:
the_list = getattr(self.vcard.n.value, part)
except AttributeError:
return []
else:
# check if list only contains empty strings
if not ''.join(the_list):
return []
return the_list if isinstance(the_list, list) else [the_list] | python | {
"resource": ""
} |
q271657 | CarddavObject._add_category | test | def _add_category(self, categories):
""" categories variable must be a list """
categories_obj = self.vcard.add('categories')
categories_obj.value = helpers.convert_to_vcard(
"category", categories, ObjectType.list_with_strings) | python | {
"resource": ""
} |
q271658 | CarddavObject._parse_type_value | test | def _parse_type_value(types, value, supported_types):
"""Parse type value of phone numbers, email and post addresses.
:param types: list of type values
:type types: list(str)
:param value: the corresponding label, required for more verbose
exceptions
:type value: str
:param supported_types: all allowed standard types
:type supported_types: list(str)
:returns: tuple of standard and custom types and pref integer
:rtype: tuple(list(str), list(str), int)
"""
custom_types = []
standard_types = []
pref = 0
for type in types:
type = type.strip()
if type:
if type.lower() in supported_types:
standard_types.append(type)
elif type.lower() == "pref":
pref += 1
elif re.match(r"^pref=\d{1,2}$", type.lower()):
pref += int(type.split("=")[1])
else:
if type.lower().startswith("x-"):
custom_types.append(type[2:])
standard_types.append(type)
else:
custom_types.append(type)
standard_types.append("X-{}".format(type))
return (standard_types, custom_types, pref) | python | {
"resource": ""
} |
q271659 | list_to_string | test | def list_to_string(input, delimiter):
"""converts list to string recursively so that nested lists are supported
:param input: a list of strings and lists of strings (and so on recursive)
:type input: list
:param delimiter: the deimiter to use when joining the items
:type delimiter: str
:returns: the recursively joined list
:rtype: str
"""
if isinstance(input, list):
return delimiter.join(
list_to_string(item, delimiter) for item in input)
return input | python | {
"resource": ""
} |
q271660 | string_to_date | test | def string_to_date(input):
"""Convert string to date object.
:param input: the date string to parse
:type input: str
:returns: the parsed datetime object
:rtype: datetime.datetime
"""
# try date formats --mmdd, --mm-dd, yyyymmdd, yyyy-mm-dd and datetime
# formats yyyymmddThhmmss, yyyy-mm-ddThh:mm:ss, yyyymmddThhmmssZ,
# yyyy-mm-ddThh:mm:ssZ.
for format_string in ("--%m%d", "--%m-%d", "%Y%m%d", "%Y-%m-%d",
"%Y%m%dT%H%M%S", "%Y-%m-%dT%H:%M:%S",
"%Y%m%dT%H%M%SZ", "%Y-%m-%dT%H:%M:%SZ"):
try:
return datetime.strptime(input, format_string)
except ValueError:
pass
# try datetime formats yyyymmddThhmmsstz and yyyy-mm-ddThh:mm:sstz where tz
# may look like -06:00.
for format_string in ("%Y%m%dT%H%M%S%z", "%Y-%m-%dT%H:%M:%S%z"):
try:
return datetime.strptime(''.join(input.rsplit(":", 1)),
format_string)
except ValueError:
pass
raise ValueError | python | {
"resource": ""
} |
q271661 | AddressBook._compare_uids | test | def _compare_uids(uid1, uid2):
"""Calculate the minimum length of initial substrings of uid1 and uid2
for them to be different.
:param uid1: first uid to compare
:type uid1: str
:param uid2: second uid to compare
:type uid2: str
:returns: the length of the shortes unequal initial substrings
:rtype: int
"""
sum = 0
for char1, char2 in zip(uid1, uid2):
if char1 == char2:
sum += 1
else:
break
return sum | python | {
"resource": ""
} |
q271662 | AddressBook._search_all | test | def _search_all(self, query):
"""Search in all fields for contacts matching query.
:param query: the query to search for
:type query: str
:yields: all found contacts
:rtype: generator(carddav_object.CarddavObject)
"""
regexp = re.compile(query, re.IGNORECASE | re.DOTALL)
for contact in self.contacts.values():
# search in all contact fields
contact_details = contact.print_vcard()
if regexp.search(contact_details) is not None:
yield contact
else:
# find phone numbers with special chars like /
clean_contact_details = re.sub("[^a-zA-Z0-9\n]", "",
contact_details)
if regexp.search(clean_contact_details) is not None \
and len(re.sub("\D", "", query)) >= 3:
yield contact | python | {
"resource": ""
} |
q271663 | AddressBook._search_names | test | def _search_names(self, query):
"""Search in the name filed for contacts matching query.
:param query: the query to search for
:type query: str
:yields: all found contacts
:rtype: generator(carddav_object.CarddavObject)
"""
regexp = re.compile(query, re.IGNORECASE | re.DOTALL)
for contact in self.contacts.values():
# only search in contact name
if regexp.search(contact.get_full_name()) is not None:
yield contact | python | {
"resource": ""
} |
q271664 | AddressBook._search_uid | test | def _search_uid(self, query):
"""Search for contacts with a matching uid.
:param query: the query to search for
:type query: str
:yields: all found contacts
:rtype: generator(carddav_object.CarddavObject)
"""
try:
# First we treat the argument as a full UID and try to match it
# exactly.
yield self.contacts[query]
except KeyError:
# If that failed we look for all contacts whos UID start with the
# given query.
for uid in self.contacts:
if uid.startswith(query):
yield self.contacts[uid] | python | {
"resource": ""
} |
q271665 | AddressBook.search | test | def search(self, query, method="all"):
"""Search this address book for contacts matching the query.
The method can be one of "all", "name" and "uid". The backend for this
address book migth be load()ed if needed.
:param query: the query to search for
:type query: str
:param method: the type of fileds to use when seaching
:type method: str
:returns: all found contacts
:rtype: list(carddav_object.CarddavObject)
"""
logging.debug('address book %s, searching with %s', self.name, query)
if not self._loaded:
self.load(query)
if method == "all":
search_function = self._search_all
elif method == "name":
search_function = self._search_names
elif method == "uid":
search_function = self._search_uid
else:
raise ValueError('Only the search methods "all", "name" and "uid" '
'are supported.')
return list(search_function(query)) | python | {
"resource": ""
} |
q271666 | AddressBook.get_short_uid_dict | test | def get_short_uid_dict(self, query=None):
"""Create a dictionary of shortend UIDs for all contacts.
All arguments are only used if the address book is not yet initialized
and will just be handed to self.load().
:param query: see self.load()
:type query: str
:returns: the contacts mapped by the shortes unique prefix of their UID
:rtype: dict(str: CarddavObject)
"""
if self._short_uids is None:
if not self._loaded:
self.load(query)
if not self.contacts:
self._short_uids = {}
elif len(self.contacts) == 1:
self._short_uids = {uid[0:1]: contact
for uid, contact in self.contacts.items()}
else:
self._short_uids = {}
sorted_uids = sorted(self.contacts)
# Prepare for the loop; the first and last items are handled
# seperatly.
item0, item1 = sorted_uids[:2]
same1 = self._compare_uids(item0, item1)
self._short_uids[item0[:same1 + 1]] = self.contacts[item0]
for item_new in sorted_uids[2:]:
# shift the items and the common prefix lenght one further
item0, item1 = item1, item_new
same0, same1 = same1, self._compare_uids(item0, item1)
# compute the final prefix length for item1
same = max(same0, same1)
self._short_uids[item0[:same + 1]] = self.contacts[item0]
# Save the last item.
self._short_uids[item1[:same1 + 1]] = self.contacts[item1]
return self._short_uids | python | {
"resource": ""
} |
q271667 | AddressBook.get_short_uid | test | def get_short_uid(self, uid):
"""Get the shortend UID for the given UID.
:param uid: the full UID to shorten
:type uid: str
:returns: the shortend uid or the empty string
:rtype: str
"""
if uid:
short_uids = self.get_short_uid_dict()
for length_of_uid in range(len(uid), 0, -1):
if short_uids.get(uid[:length_of_uid]) is not None:
return uid[:length_of_uid]
return "" | python | {
"resource": ""
} |
q271668 | VdirAddressBook._find_vcard_files | test | def _find_vcard_files(self, search=None, search_in_source_files=False):
"""Find all vcard files inside this address book.
If a search string is given only files which contents match that will
be returned.
:param search: a regular expression to limit the results
:type search: str
:param search_in_source_files: apply search regexp directly on the .vcf files to speed up parsing (less accurate)
:type search_in_source_files: bool
:returns: the paths of the vcard files
:rtype: generator
"""
files = glob.glob(os.path.join(self.path, "*.vcf"))
if search and search_in_source_files:
for filename in files:
with open(filename, "r") as filehandle:
if re.search(search, filehandle.read(),
re.IGNORECASE | re.DOTALL):
yield filename
else:
yield from files | python | {
"resource": ""
} |
q271669 | VdirAddressBook.load | test | def load(self, query=None, search_in_source_files=False):
"""Load all vcard files in this address book from disk.
If a search string is given only files which contents match that will
be loaded.
:param query: a regular expression to limit the results
:type query: str
:param search_in_source_files: apply search regexp directly on the .vcf files to speed up parsing (less accurate)
:type search_in_source_files: bool
:returns: the number of successfully loaded cards and the number of
errors
:rtype: int, int
:throws: AddressBookParseError
"""
if self._loaded:
return
logging.debug('Loading Vdir %s with query %s', self.name, query)
errors = 0
for filename in self._find_vcard_files(
search=query, search_in_source_files=search_in_source_files):
try:
card = CarddavObject.from_file(self, filename,
self._private_objects,
self._localize_dates)
except (IOError, vobject.base.ParseError) as err:
verb = "open" if isinstance(err, IOError) else "parse"
logging.debug("Error: Could not %s file %s\n%s", verb,
filename, err)
if self._skip:
errors += 1
else:
# FIXME: This should throw an apropriate exception and the
# sys.exit should be called somewhere closer to the command
# line parsing.
logging.error(
"The vcard file %s of address book %s could not be "
"parsed\nUse --debug for more information or "
"--skip-unparsable to proceed", filename, self.name)
sys.exit(2)
else:
uid = card.get_uid()
if not uid:
logging.warning("Card %s from address book %s has no UID "
"and will not be availbale.", card,
self.name)
elif uid in self.contacts:
logging.warning(
"Card %s and %s from address book %s have the same "
"UID. The former will not be availbale.", card,
self.contacts[uid], self.name)
else:
self.contacts[uid] = card
self._loaded = True
if errors:
logging.warning(
"%d of %d vCard files of address book %s could not be parsed.",
errors, len(self.contacts) + errors, self)
logging.debug('Loded %s contacts from address book %s.',
len(self.contacts), self.name) | python | {
"resource": ""
} |
q271670 | AddressBookCollection.get_abook | test | def get_abook(self, name):
"""Get one of the backing abdress books by its name,
:param name: the name of the address book to get
:type name: str
:returns: the matching address book or None
:rtype: AddressBook or NoneType
"""
for abook in self._abooks:
if abook.name == name:
return abook | python | {
"resource": ""
} |
q271671 | Assembler.avail_archs | test | def avail_archs(self):
''' Initialize the dictionary of architectures for assembling via keystone'''
return {
ARM32: (KS_ARCH_ARM, KS_MODE_ARM),
ARM64: (KS_ARCH_ARM64, KS_MODE_LITTLE_ENDIAN),
ARM_TB: (KS_ARCH_ARM, KS_MODE_THUMB),
HEXAGON: (KS_ARCH_HEXAGON, KS_MODE_BIG_ENDIAN),
MIPS32: (KS_ARCH_MIPS, KS_MODE_MIPS32),
MIPS64: (KS_ARCH_MIPS, KS_MODE_MIPS64),
PPC32: (KS_ARCH_PPC, KS_MODE_PPC32),
PPC64: (KS_ARCH_PPC, KS_MODE_PPC64),
SPARC32: (KS_ARCH_SPARC, KS_MODE_SPARC32),
SPARC64: (KS_ARCH_SPARC, KS_MODE_SPARC64),
SYSTEMZ: (KS_ARCH_SYSTEMZ, KS_MODE_BIG_ENDIAN),
X86_16: (KS_ARCH_X86, KS_MODE_16),
X86_32: (KS_ARCH_X86, KS_MODE_32),
X86_64: (KS_ARCH_X86, KS_MODE_64),
} | python | {
"resource": ""
} |
q271672 | Disassembler.avail_archs | test | def avail_archs(self):
''' Initialize the dictionary of architectures for disassembling via capstone'''
return {
ARM32: (CS_ARCH_ARM, CS_MODE_ARM),
ARM64: (CS_ARCH_ARM64, CS_MODE_LITTLE_ENDIAN),
ARM_TB: (CS_ARCH_ARM, CS_MODE_THUMB),
MIPS32: (CS_ARCH_MIPS, CS_MODE_MIPS32),
MIPS64: (CS_ARCH_MIPS, CS_MODE_MIPS64),
SPARC32: (CS_ARCH_SPARC, CS_MODE_BIG_ENDIAN),
SPARC64: (CS_ARCH_SPARC, CS_MODE_V9),
SYSTEMZ: (CS_ARCH_SYSZ, CS_MODE_BIG_ENDIAN),
X86_16: (CS_ARCH_X86, CS_MODE_16),
X86_32: (CS_ARCH_X86, CS_MODE_32),
X86_64: (CS_ARCH_X86, CS_MODE_64),
} | python | {
"resource": ""
} |
q271673 | getargspec_permissive | test | def getargspec_permissive(func):
"""
An `inspect.getargspec` with a relaxed sanity check to support Cython.
Motivation:
A Cython-compiled function is *not* an instance of Python's
types.FunctionType. That is the sanity check the standard Py2
library uses in `inspect.getargspec()`. So, an exception is raised
when calling `argh.dispatch_command(cythonCompiledFunc)`. However,
the CyFunctions do have perfectly usable `.func_code` and
`.func_defaults` which is all `inspect.getargspec` needs.
This function just copies `inspect.getargspec()` from the standard
library but relaxes the test to a more duck-typing one of having
both `.func_code` and `.func_defaults` attributes.
"""
if inspect.ismethod(func):
func = func.im_func
# Py2 Stdlib uses isfunction(func) which is too strict for Cython-compiled
# functions though such have perfectly usable func_code, func_defaults.
if not (hasattr(func, "func_code") and hasattr(func, "func_defaults")):
raise TypeError('{!r} missing func_code or func_defaults'.format(func))
args, varargs, varkw = inspect.getargs(func.func_code)
return inspect.ArgSpec(args, varargs, varkw, func.func_defaults) | python | {
"resource": ""
} |
q271674 | dispatch | test | def dispatch(parser, argv=None, add_help_command=True,
completion=True, pre_call=None,
output_file=sys.stdout, errors_file=sys.stderr,
raw_output=False, namespace=None,
skip_unknown_args=False):
"""
Parses given list of arguments using given parser, calls the relevant
function and prints the result.
The target function should expect one positional argument: the
:class:`argparse.Namespace` object. However, if the function is decorated with
:func:`~argh.decorators.plain_signature`, the positional and named
arguments from the namespace object are passed to the function instead
of the object itself.
:param parser:
the ArgumentParser instance.
:param argv:
a list of strings representing the arguments. If `None`, ``sys.argv``
is used instead. Default is `None`.
:param add_help_command:
if `True`, converts first positional argument "help" to a keyword
argument so that ``help foo`` becomes ``foo --help`` and displays usage
information for "foo". Default is `True`.
:param output_file:
A file-like object for output. If `None`, the resulting lines are
collected and returned as a string. Default is ``sys.stdout``.
:param errors_file:
Same as `output_file` but for ``sys.stderr``.
:param raw_output:
If `True`, results are written to the output file raw, without adding
whitespaces or newlines between yielded strings. Default is `False`.
:param completion:
If `True`, shell tab completion is enabled. Default is `True`. (You
will also need to install it.) See :mod:`argh.completion`.
:param skip_unknown_args:
If `True`, unknown arguments do not cause an error
(`ArgumentParser.parse_known_args` is used).
:param namespace:
An `argparse.Namespace`-like object. By default an
:class:`ArghNamespace` object is used. Please note that support for
combined default and nested functions may be broken if a different
type of object is forced.
By default the exceptions are not wrapped and will propagate. The only
exception that is always wrapped is :class:`~argh.exceptions.CommandError`
which is interpreted as an expected event so the traceback is hidden.
You can also mark arbitrary exceptions as "wrappable" by using the
:func:`~argh.decorators.wrap_errors` decorator.
"""
if completion:
autocomplete(parser)
if argv is None:
argv = sys.argv[1:]
if add_help_command:
if argv and argv[0] == 'help':
argv.pop(0)
argv.append('--help')
if skip_unknown_args:
parse_args = parser.parse_known_args
else:
parse_args = parser.parse_args
if not namespace:
namespace = ArghNamespace()
# this will raise SystemExit if parsing fails
namespace_obj = parse_args(argv, namespace=namespace)
function = _get_function_from_namespace_obj(namespace_obj)
if function:
lines = _execute_command(function, namespace_obj, errors_file,
pre_call=pre_call)
else:
# no commands declared, can't dispatch; display help message
lines = [parser.format_usage()]
if output_file is None:
# user wants a string; we create an internal temporary file-like object
# and will return its contents as a string
if sys.version_info < (3,0):
f = compat.BytesIO()
else:
f = compat.StringIO()
else:
# normally this is stdout; can be any file
f = output_file
for line in lines:
# print the line as soon as it is generated to ensure that it is
# displayed to the user before anything else happens, e.g.
# raw_input() is called
io.dump(line, f)
if not raw_output:
# in most cases user wants one message per line
io.dump('\n', f)
if output_file is None:
# user wanted a string; return contents of our temporary file-like obj
f.seek(0)
return f.read() | python | {
"resource": ""
} |
q271675 | safe_input | test | def safe_input(prompt):
"""
Prompts user for input. Correctly handles prompt message encoding.
"""
if sys.version_info < (3,0):
if isinstance(prompt, compat.text_type):
# Python 2.x: unicode → bytes
encoding = locale.getpreferredencoding() or 'utf-8'
prompt = prompt.encode(encoding)
else:
if not isinstance(prompt, compat.text_type):
# Python 3.x: bytes → unicode
prompt = prompt.decode()
return _input(prompt) | python | {
"resource": ""
} |
q271676 | encode_output | test | def encode_output(value, output_file):
"""
Encodes given value so it can be written to given file object.
Value may be Unicode, binary string or any other data type.
The exact behaviour depends on the Python version:
Python 3.x
`sys.stdout` is a `_io.TextIOWrapper` instance that accepts `str`
(unicode) and breaks on `bytes`.
It is OK to simply assume that everything is Unicode unless special
handling is introduced in the client code.
Thus, no additional processing is performed.
Python 2.x
`sys.stdout` is a file-like object that accepts `str` (bytes)
and breaks when `unicode` is passed to `sys.stdout.write()`.
We can expect both Unicode and bytes. They need to be encoded so as
to match the file object encoding.
The output is binary if the object doesn't explicitly require Unicode.
"""
if sys.version_info > (3,0):
# Python 3: whatever → unicode
return compat.text_type(value)
else:
# Python 2: handle special cases
stream_encoding = getattr(output_file, 'encoding', None)
if stream_encoding:
if stream_encoding.upper() == 'UTF-8':
return compat.text_type(value)
else:
return value.encode(stream_encoding, 'ignore')
else:
# no explicit encoding requirements; force binary
if isinstance(value, compat.text_type):
# unicode → binary
return value.encode('utf-8')
else:
return str(value) | python | {
"resource": ""
} |
q271677 | _guess | test | def _guess(kwargs):
"""
Adds types, actions, etc. to given argument specification.
For example, ``default=3`` implies ``type=int``.
:param arg: a :class:`argh.utils.Arg` instance
"""
guessed = {}
# Parser actions that accept argument 'type'
TYPE_AWARE_ACTIONS = 'store', 'append'
# guess type/action from default value
value = kwargs.get('default')
if value is not None:
if isinstance(value, bool):
if kwargs.get('action') is None:
# infer action from default value
guessed['action'] = 'store_false' if value else 'store_true'
elif kwargs.get('type') is None:
# infer type from default value
# (make sure that action handler supports this keyword)
if kwargs.get('action', 'store') in TYPE_AWARE_ACTIONS:
guessed['type'] = type(value)
# guess type from choices (first item)
if kwargs.get('choices') and 'type' not in list(guessed) + list(kwargs):
guessed['type'] = type(kwargs['choices'][0])
return dict(kwargs, **guessed) | python | {
"resource": ""
} |
q271678 | add_commands | test | def add_commands(parser, functions, namespace=None, namespace_kwargs=None,
func_kwargs=None,
# deprecated args:
title=None, description=None, help=None):
"""
Adds given functions as commands to given parser.
:param parser:
an :class:`argparse.ArgumentParser` instance.
:param functions:
a list of functions. A subparser is created for each of them.
If the function is decorated with :func:`~argh.decorators.arg`, the
arguments are passed to :class:`argparse.ArgumentParser.add_argument`.
See also :func:`~argh.dispatching.dispatch` for requirements
concerning function signatures. The command name is inferred from the
function name. Note that the underscores in the name are replaced with
hyphens, i.e. function name "foo_bar" becomes command name "foo-bar".
:param namespace:
an optional string representing the group of commands. For example, if
a command named "hello" is added without the namespace, it will be
available as "prog.py hello"; if the namespace if specified as "greet",
then the command will be accessible as "prog.py greet hello". The
namespace itself is not callable, so "prog.py greet" will fail and only
display a help message.
:param func_kwargs:
a `dict` of keyword arguments to be passed to each nested ArgumentParser
instance created per command (i.e. per function). Members of this
dictionary have the highest priority, so a function's docstring is
overridden by a `help` in `func_kwargs` (if present).
:param namespace_kwargs:
a `dict` of keyword arguments to be passed to the nested ArgumentParser
instance under given `namespace`.
Deprecated params that should be moved into `namespace_kwargs`:
:param title:
passed to :meth:`argparse.ArgumentParser.add_subparsers` as `title`.
.. deprecated:: 0.26.0
Please use `namespace_kwargs` instead.
:param description:
passed to :meth:`argparse.ArgumentParser.add_subparsers` as
`description`.
.. deprecated:: 0.26.0
Please use `namespace_kwargs` instead.
:param help:
passed to :meth:`argparse.ArgumentParser.add_subparsers` as `help`.
.. deprecated:: 0.26.0
Please use `namespace_kwargs` instead.
.. note::
This function modifies the parser object. Generally side effects are
bad practice but we don't seem to have any choice as ArgumentParser is
pretty opaque.
You may prefer :class:`~argh.helpers.ArghParser.add_commands` for a bit
more predictable API.
.. note::
An attempt to add commands to a parser which already has a default
function (e.g. added with :func:`~argh.assembling.set_default_command`)
results in `AssemblingError`.
"""
# FIXME "namespace" is a correct name but it clashes with the "namespace"
# that represents arguments (argparse.Namespace and our ArghNamespace).
# We should rename the argument here.
if DEST_FUNCTION in parser._defaults:
_require_support_for_default_command_with_subparsers()
namespace_kwargs = namespace_kwargs or {}
# FIXME remove this by 1.0
#
if title:
warnings.warn('argument `title` is deprecated in add_commands(),'
' use `parser_kwargs` instead', DeprecationWarning)
namespace_kwargs['description'] = title
if help:
warnings.warn('argument `help` is deprecated in add_commands(),'
' use `parser_kwargs` instead', DeprecationWarning)
namespace_kwargs['help'] = help
if description:
warnings.warn('argument `description` is deprecated in add_commands(),'
' use `parser_kwargs` instead', DeprecationWarning)
namespace_kwargs['description'] = description
#
# /
subparsers_action = get_subparsers(parser, create=True)
if namespace:
# Make a nested parser and init a deeper _SubParsersAction under it.
# Create a named group of commands. It will be listed along with
# root-level commands in ``app.py --help``; in that context its `title`
# can be used as a short description on the right side of its name.
# Normally `title` is shown above the list of commands
# in ``app.py my-namespace --help``.
subsubparser_kw = {
'help': namespace_kwargs.get('title'),
}
subsubparser = subparsers_action.add_parser(namespace, **subsubparser_kw)
subparsers_action = subsubparser.add_subparsers(**namespace_kwargs)
else:
assert not namespace_kwargs, ('`parser_kwargs` only makes sense '
'with `namespace`.')
for func in functions:
cmd_name, func_parser_kwargs = _extract_command_meta_from_func(func)
# override any computed kwargs by manually supplied ones
if func_kwargs:
func_parser_kwargs.update(func_kwargs)
# create and set up the parser for this command
command_parser = subparsers_action.add_parser(cmd_name, **func_parser_kwargs)
set_default_command(command_parser, func) | python | {
"resource": ""
} |
q271679 | named | test | def named(new_name):
"""
Sets given string as command name instead of the function name.
The string is used verbatim without further processing.
Usage::
@named('load')
def do_load_some_stuff_and_keep_the_original_function_name(args):
...
The resulting command will be available only as ``load``. To add aliases
without renaming the command, check :func:`aliases`.
.. versionadded:: 0.19
"""
def wrapper(func):
setattr(func, ATTR_NAME, new_name)
return func
return wrapper | python | {
"resource": ""
} |
q271680 | arg | test | def arg(*args, **kwargs):
"""
Declares an argument for given function. Does not register the function
anywhere, nor does it modify the function in any way.
The signature of the decorator matches that of
:meth:`argparse.ArgumentParser.add_argument`, only some keywords are not
required if they can be easily guessed (e.g. you don't have to specify type
or action when an `int` or `bool` default value is supplied).
Typical use cases:
- In combination with :func:`expects_obj` (which is not recommended);
- in combination with ordinary function signatures to add details that
cannot be expressed with that syntax (e.g. help message).
Usage::
from argh import arg
@arg('path', help='path to the file to load')
@arg('--format', choices=['yaml','json'])
@arg('-v', '--verbosity', choices=range(0,3), default=2)
def load(path, something=None, format='json', dry_run=False, verbosity=1):
loaders = {'json': json.load, 'yaml': yaml.load}
loader = loaders[args.format]
data = loader(args.path)
if not args.dry_run:
if verbosity < 1:
print('saving to the database')
put_to_database(data)
In this example:
- `path` declaration is extended with `help`;
- `format` declaration is extended with `choices`;
- `dry_run` declaration is not duplicated;
- `verbosity` is extended with `choices` and the default value is
overridden. (If both function signature and `@arg` define a default
value for an argument, `@arg` wins.)
.. note::
It is recommended to avoid using this decorator unless there's no way
to tune the argument's behaviour or presentation using ordinary
function signatures. Readability counts, don't repeat yourself.
"""
def wrapper(func):
declared_args = getattr(func, ATTR_ARGS, [])
# The innermost decorator is called first but appears last in the code.
# We need to preserve the expected order of positional arguments, so
# the outermost decorator inserts its value before the innermost's:
declared_args.insert(0, dict(option_strings=args, **kwargs))
setattr(func, ATTR_ARGS, declared_args)
return func
return wrapper | python | {
"resource": ""
} |
q271681 | confirm | test | def confirm(action, default=None, skip=False):
"""
A shortcut for typical confirmation prompt.
:param action:
a string describing the action, e.g. "Apply changes". A question mark
will be appended.
:param default:
`bool` or `None`. Determines what happens when user hits :kbd:`Enter`
without typing in a choice. If `True`, default choice is "yes". If
`False`, it is "no". If `None` the prompt keeps reappearing until user
types in a choice (not necessarily acceptable) or until the number of
iteration reaches the limit. Default is `None`.
:param skip:
`bool`; if `True`, no interactive prompt is used and default choice is
returned (useful for batch mode). Default is `False`.
Usage::
def delete(key, silent=False):
item = db.get(Item, args.key)
if confirm('Delete '+item.title, default=True, skip=silent):
item.delete()
print('Item deleted.')
else:
print('Operation cancelled.')
Returns `None` on `KeyboardInterrupt` event.
"""
MAX_ITERATIONS = 3
if skip:
return default
else:
defaults = {
None: ('y','n'),
True: ('Y','n'),
False: ('y','N'),
}
y, n = defaults[default]
prompt = text_type('{action}? ({y}/{n})').format(**locals())
choice = None
try:
if default is None:
cnt = 1
while not choice and cnt < MAX_ITERATIONS:
choice = safe_input(prompt)
cnt += 1
else:
choice = safe_input(prompt)
except KeyboardInterrupt:
return None
if choice in ('yes', 'y', 'Y'):
return True
if choice in ('no', 'n', 'N'):
return False
if default is not None:
return default
return None | python | {
"resource": ""
} |
q271682 | Query.replace | test | def replace(self, **kwargs):
'''
Copy the Query object, optionally replacing the filters, order_by, or
limit information on the copy. This is mostly an internal detail that
you can ignore.
'''
data = {
'model': self._model,
'filters': self._filters,
'order_by': self._order_by,
'limit': self._limit,
'select': self._select,
}
data.update(**kwargs)
return Query(**data) | python | {
"resource": ""
} |
q271683 | Query.like | test | def like(self, **kwargs):
'''
When provided with keyword arguments of the form ``col=pattern``, this
will limit the entities returned to those that include the provided
pattern. Note that 'like' queries require that the ``prefix=True``
option must have been provided as part of the column definition.
Patterns allow for 4 wildcard characters, whose semantics are as
follows:
* *?* - will match 0 or 1 of any character
* *\** - will match 0 or more of any character
* *+* - will match 1 or more of any character
* *!* - will match exactly 1 of any character
As an example, imagine that you have enabled the required prefix
matching on your ``User.email`` column. And lets say that you want to
find everyone with an email address that contains the name 'frank'
before the ``@`` sign. You can use either of the following patterns
to discover those users.
* *\*frank\*@*
* *\*frank\*@*
.. note:: Like queries implicitly start at the beginning of strings
checked, so if you want to match a pattern that doesn't start at
the beginning of a string, you should prefix it with one of the
wildcard characters (like ``*`` as we did with the 'frank' pattern).
'''
new = []
for k, v in kwargs.items():
v = self._check(k, v, 'like')
new.append(Pattern(k, v))
return self.replace(filters=self._filters+tuple(new)) | python | {
"resource": ""
} |
q271684 | Query.cached_result | test | def cached_result(self, timeout):
'''
This will execute the query, returning the key where a ZSET of your
results will be stored for pagination, further operations, etc.
The timeout must be a positive integer number of seconds for which to
set the expiration time on the key (this is to ensure that any cached
query results are eventually deleted, unless you make the explicit
step to use the PERSIST command).
.. note:: Limit clauses are ignored and not passed.
Usage::
ukey = User.query.endswith(email='@gmail.com').cached_result(30)
for i in xrange(0, conn.zcard(ukey), 100):
# refresh the expiration
conn.expire(ukey, 30)
users = User.get(conn.zrange(ukey, i, i+99))
...
'''
if not (self._filters or self._order_by):
raise QueryError("You are missing filter or order criteria")
timeout = int(timeout)
if timeout < 1:
raise QueryError("You must specify a timeout >= 1, you gave %r"%timeout)
return self._model._gindex.search(
_connect(self._model), self._filters, self._order_by, timeout=timeout) | python | {
"resource": ""
} |
q271685 | Query.first | test | def first(self):
'''
Returns only the first result from the query, if any.
'''
lim = [0, 1]
if self._limit:
lim[0] = self._limit[0]
if not self._filters and not self._order_by:
for ent in self:
return ent
return None
ids = self.limit(*lim)._search()
if ids:
return self._model.get(ids[0])
return None | python | {
"resource": ""
} |
q271686 | Query.delete | test | def delete(self, blocksize=100):
'''
Will delete the entities that match at the time the query is executed.
Used like::
MyModel.query.filter(email=...).delete()
MyModel.query.endswith(email='@host.com').delete()
.. warning:: can't be used on models on either side of a ``OneToMany``,
``ManyToOne``, or ``OneToOne`` relationship.
'''
from .columns import MODELS_REFERENCED
if not self._model._no_fk or self._model._namespace in MODELS_REFERENCED:
raise QueryError("Can't delete entities of models with foreign key relationships")
de = []
i = 0
for result in self.iter_result(pagesize=blocksize):
de.append(result)
i += 1
if i >= blocksize:
session.delete(de) # one round-trip to delete "chunk" items
del de[:]
i = 0
if de:
session.delete(de) | python | {
"resource": ""
} |
q271687 | _on_delete | test | def _on_delete(ent):
'''
This function handles all on_delete semantics defined on OneToMany columns.
This function only exists because 'cascade' is *very* hard to get right.
'''
seen_d = set([ent._pk])
to_delete = [ent]
seen_s = set()
to_save = []
def _set_default(ent, attr, de=NULL):
pk = ent._pk
if pk in seen_d:
# going to be deleted, don't need to modify
return
col = ent.__class__._columns[attr]
de = de if de is not NULL else col._default
if de in (None, NULL):
setattr(ent, attr, None)
elif callable(col._default):
setattr(ent, attr, col._default())
else:
setattr(ent, attr, col._default)
if pk not in seen_s:
seen_s.add(pk)
to_save.append(ent)
for self in to_delete:
for tbl, attr, action in MODELS_REFERENCED.get(self._namespace, ()):
if action == 'no action':
continue
refs = MODELS[tbl].get_by(**{attr: self.id})
if not refs:
continue
if action == 'restrict':
# raise the exception here for a better traceback
raise _restrict(self, attr, refs)
elif action == 'set null':
for ref in refs:
_set_default(ref, attr, None)
continue
elif action == 'set default':
for ref in refs:
_set_default(ref, attr)
continue
# otherwise col._on_delete == 'cascade'
for ent in (refs if isinstance(refs, list) else [refs]):
if ent._pk not in seen_d:
seen_d.add(ent._pk)
to_delete.append(ent)
# If we got here, then to_delete includes all items to delete. Let's delete
# them!
for self in to_delete:
self.delete(skip_on_delete_i_really_mean_it=SKIP_ON_DELETE)
for self in to_save:
# Careful not to resurrect deleted entities
if self._pk not in seen_d:
self.save() | python | {
"resource": ""
} |
q271688 | redis_prefix_lua | test | def redis_prefix_lua(conn, dest, index, prefix, is_first, pattern=None):
'''
Performs the actual prefix, suffix, and pattern match operations.
'''
tkey = '%s:%s'%(index.partition(':')[0], uuid.uuid4())
start, end = _start_end(prefix)
return _redis_prefix_lua(conn,
[dest, tkey, index],
[start, end, pattern or prefix, int(pattern is not None), int(bool(is_first))]
) | python | {
"resource": ""
} |
q271689 | estimate_work_lua | test | def estimate_work_lua(conn, index, prefix):
'''
Estimates the total work necessary to calculate the prefix match over the
given index with the provided prefix.
'''
if index.endswith(':idx'):
args = [] if not prefix else list(prefix)
if args:
args[0] = '-inf' if args[0] is None else repr(float(args[0]))
args[1] = 'inf' if args[1] is None else repr(float(args[1]))
return _estimate_work_lua(conn, [index], args, force_eval=True)
elif index.endswith(':geo'):
return _estimate_work_lua(conn, [index], filter(None, [prefix]), force_eval=True)
start, end = _start_end(prefix)
return _estimate_work_lua(conn, [index], [start, '(' + end], force_eval=True) | python | {
"resource": ""
} |
q271690 | GeneralIndex.search | test | def search(self, conn, filters, order_by, offset=None, count=None, timeout=None):
'''
Search for model ids that match the provided filters.
Arguments:
* *filters* - A list of filters that apply to the search of one of
the following two forms:
1. ``'column:string'`` - a plain string will match a word in a
text search on the column
.. note:: Read the documentation about the ``Query`` object
for what is actually passed during text search
2. ``('column', min, max)`` - a numeric column range search,
between min and max (inclusive by default)
.. note:: Read the documentation about the ``Query`` object
for information about open-ended ranges
3. ``['column:string1', 'column:string2']`` - will match any
of the provided words in a text search on the column
4. ``Prefix('column', 'prefix')`` - will match prefixes of
words in a text search on the column
5. ``Suffix('column', 'suffix')`` - will match suffixes of
words in a text search on the column
6. ``Pattern('column', 'pattern')`` - will match patterns over
words in a text search on the column
* *order_by* - A string that names the numeric column by which to
sort the results by. Prefixing with '-' will return results in
descending order
.. note:: While you can technically pass a non-numeric index as an
*order_by* clause, the results will basically be to order the
results by string comparison of the ids (10 will come before 2).
.. note:: If you omit the ``order_by`` argument, results will be
ordered by the last filter. If the last filter was a text
filter, see the previous note. If the last filter was numeric,
then results will be ordered by that result.
* *offset* - A numeric starting offset for results
* *count* - The maximum number of results to return from the query
'''
# prepare the filters
pipe, intersect, temp_id = self._prepare(conn, filters)
# handle ordering
if order_by:
reverse = order_by and order_by.startswith('-')
order_clause = '%s:%s:idx'%(self.namespace, order_by.lstrip('-'))
intersect(temp_id, {temp_id:0, order_clause: -1 if reverse else 1})
# handle returning the temporary result key
if timeout is not None:
pipe.expire(temp_id, timeout)
pipe.execute()
return temp_id
offset = offset if offset is not None else 0
end = (offset + count - 1) if count and count > 0 else -1
pipe.zrange(temp_id, offset, end)
pipe.delete(temp_id)
return pipe.execute()[-2] | python | {
"resource": ""
} |
q271691 | GeneralIndex.count | test | def count(self, conn, filters):
'''
Returns the count of the items that match the provided filters.
For the meaning of what the ``filters`` argument means, see the
``.search()`` method docs.
'''
pipe, intersect, temp_id = self._prepare(conn, filters)
pipe.zcard(temp_id)
pipe.delete(temp_id)
return pipe.execute()[-2] | python | {
"resource": ""
} |
q271692 | _connect | test | def _connect(obj):
'''
Tries to get the _conn attribute from a model. Barring that, gets the
global default connection using other methods.
'''
from .columns import MODELS
if isinstance(obj, MODELS['Model']):
obj = obj.__class__
if hasattr(obj, '_conn'):
return obj._conn
if hasattr(obj, 'CONN'):
return obj.CONN
return get_connection() | python | {
"resource": ""
} |
q271693 | FULL_TEXT | test | def FULL_TEXT(val):
'''
This is a basic full-text index keygen function. Words are lowercased, split
by whitespace, and stripped of punctuation from both ends before an inverted
index is created for term searching.
'''
if isinstance(val, float):
val = repr(val)
elif val in (None, ''):
return None
elif not isinstance(val, six.string_types):
if six.PY3 and isinstance(val, bytes):
val = val.decode('latin-1')
else:
val = str(val)
r = sorted(set([x for x in [s.lower().strip(string.punctuation) for s in val.split()] if x]))
if not isinstance(val, str): # unicode on py2k
return [s.encode('utf-8') for s in r]
return r | python | {
"resource": ""
} |
q271694 | refresh_indices | test | def refresh_indices(model, block_size=100):
'''
This utility function will iterate over all entities of a provided model,
refreshing their indices. This is primarily useful after adding an index
on a column.
Arguments:
* *model* - the model whose entities you want to reindex
* *block_size* - the maximum number of entities you want to fetch from
Redis at a time, defaulting to 100
This function will yield its progression through re-indexing all of your
entities.
Example use::
for progress, total in refresh_indices(MyModel, block_size=200):
print "%s of %s"%(progress, total)
.. note:: This uses the session object to handle index refresh via calls to
``.commit()``. If you have any outstanding entities known in the
session, they will be committed.
'''
conn = _connect(model)
max_id = int(conn.get('%s:%s:'%(model._namespace, model._pkey)) or '0')
block_size = max(block_size, 10)
for i in range(1, max_id+1, block_size):
# fetches entities, keeping a record in the session
models = model.get(list(range(i, i+block_size)))
models # for pyflakes
# re-save un-modified data, resulting in index-only updates
session.commit(all=True)
yield min(i+block_size, max_id), max_id | python | {
"resource": ""
} |
q271695 | clean_old_index | test | def clean_old_index(model, block_size=100, **kwargs):
'''
This utility function will clean out old index data that was accidentally
left during item deletion in rom versions <= 0.27.0 . You should run this
after you have upgraded all of your clients to version 0.28.0 or later.
Arguments:
* *model* - the model whose entities you want to reindex
* *block_size* - the maximum number of items to check at a time
defaulting to 100
This function will yield its progression through re-checking all of the
data that could be left over.
Example use::
for progress, total in clean_old_index(MyModel, block_size=200):
print "%s of %s"%(progress, total)
'''
conn = _connect(model)
version = list(map(int, conn.info()['redis_version'].split('.')[:2]))
has_hscan = version >= [2, 8]
pipe = conn.pipeline(True)
prefix = '%s:'%model._namespace
index = prefix + ':'
block_size = max(block_size, 10)
force_hscan = kwargs.get('force_hscan', False)
if (has_hscan or force_hscan) and force_hscan is not None:
max_id = conn.hlen(index)
cursor = None
scanned = 0
while cursor != b'0':
cursor, remove = _scan_index_lua(conn, [index, prefix], [cursor or '0', block_size, 0, 0])
if remove:
_clean_index_lua(conn, [model._namespace], remove)
scanned += block_size
if scanned > max_id:
max_id = scanned + 1
yield scanned, max_id
# need to scan over unique indexes :/
for uniq in chain(model._unique, model._cunique):
name = uniq if isinstance(uniq, six.string_types) else ':'.join(uniq)
idx = prefix + name + ':uidx'
cursor = None
while cursor != b'0':
cursor, remove = _scan_index_lua(conn, [idx, prefix], [cursor or '0', block_size, 1, 0])
if remove:
conn.hdel(idx, *remove)
scanned += block_size
if scanned > max_id:
max_id = scanned + 1
yield scanned, max_id
else:
if model._unique or model._cunique:
if has_hscan:
warnings.warn("You have disabled the use of HSCAN to clean up indexes, this will prevent unique index cleanup", stacklevel=2)
else:
warnings.warn("Unique indexes cannot be cleaned up in Redis versions prior to 2.8", stacklevel=2)
max_id = int(conn.get('%s%s:'%(prefix, model._pkey)) or '0')
for i in range(1, max_id+1, block_size):
ids = list(range(i, min(i+block_size, max_id+1)))
for id in ids:
pipe.exists(prefix + str(id))
pipe.hexists(index, id)
result = iter(pipe.execute())
remove = [id for id, ent, ind in zip(ids, result, result) if ind and not ent]
if remove:
_clean_index_lua(conn, [model._namespace], remove)
yield min(i+block_size, max_id-1), max_id
yield max_id, max_id | python | {
"resource": ""
} |
q271696 | Session.add | test | def add(self, obj):
'''
Adds an entity to the session.
'''
if self.null_session:
return
self._init()
pk = obj._pk
if not pk.endswith(':None'):
self.known[pk] = obj
self.wknown[pk] = obj | python | {
"resource": ""
} |
q271697 | Session.get | test | def get(self, pk):
'''
Fetches an entity from the session based on primary key.
'''
self._init()
return self.known.get(pk) or self.wknown.get(pk) | python | {
"resource": ""
} |
q271698 | redis_writer_lua | test | def redis_writer_lua(conn, pkey, namespace, id, unique, udelete, delete,
data, keys, scored, prefix, suffix, geo, old_data, is_delete):
'''
... Actually write data to Redis. This is an internal detail. Please don't
call me directly.
'''
ldata = []
for pair in data.items():
ldata.extend(pair)
for item in prefix:
item.append(_prefix_score(item[-1]))
for item in suffix:
item.append(_prefix_score(item[-1]))
data = [json.dumps(x, default=_fix_bytes) for x in
(unique, udelete, delete, ldata, keys, scored, prefix, suffix, geo, is_delete, old_data)]
result = _redis_writer_lua(conn, [], [namespace, id] + data)
if isinstance(conn, _Pipeline):
# we're in a pipelined write situation, don't parse the pipeline :P
return
if six.PY3:
result = result.decode()
result = json.loads(result)
if 'unique' in result:
result = result['unique']
raise UniqueKeyViolation(
"Value %r for %s:%s:uidx not distinct (failed for pk=%s)"%(
unique[result], namespace, result, id),
namespace, id)
if 'race' in result:
result = result['race']
if pkey in result:
raise EntityDeletedError(
"Entity %s:%s deleted by another writer; use .save(force=True) to re-save"%(
namespace, id),
namespace, id)
raise DataRaceError(
"%s:%s Column(s) %r updated by another writer, write aborted!"%(
namespace, id, result),
namespace, id) | python | {
"resource": ""
} |
q271699 | Model.save | test | def save(self, full=False, force=False):
'''
Saves the current entity to Redis. Will only save changed data by
default, but you can force a full save by passing ``full=True``.
If the underlying entity was deleted and you want to re-save the entity,
you can pass ``force=True`` to force a full re-save of the entity.
'''
# handle the pre-commit hooks
was_new = self._new
if was_new:
self._before_insert()
else:
self._before_update()
new = self.to_dict()
ret, data = self._apply_changes(
self._last, new, full or self._new or force, is_new=self._new or force)
self._last = data
self._new = False
self._modified = False
self._deleted = False
# handle the post-commit hooks
if was_new:
self._after_insert()
else:
self._after_update()
return ret | python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.