_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q7100 | create | train | def create(ctx, to, amount, symbol, secret, hash, account, expiration):
""" Create an HTLC contract
"""
ctx.blockchain.blocking = True
tx = ctx.blockchain.htlc_create(
Amount(amount, symbol),
to,
secret,
hash_type=hash,
| python | {
"resource": ""
} |
q7101 | redeem | train | def redeem(ctx, htlc_id, secret, account):
""" Redeem an HTLC contract
"""
| python | {
"resource": ""
} |
q7102 | unique | train | def unique(flags_class):
""" A decorator for flags classes to forbid flag aliases. """
if not is_flags_class_final(flags_class):
raise TypeError('unique check can be applied only to flags classes | python | {
"resource": ""
} |
q7103 | unique_bits | train | def unique_bits(flags_class):
""" A decorator for flags classes to forbid declaring flags with overlapping bits. """
flags_class = unique(flags_class)
other_bits = 0
for name, member in flags_class.__members_without_aliases__.items():
bits = int(member)
if other_bits & bits:
for | python | {
"resource": ""
} |
q7104 | newfeed | train | def newfeed(ctx, symbol, price, market, cer, mssr, mcr, account):
""" Publish a price feed!
Examples:
\b
uptick newfeed USD 0.01 USD/BTS
uptick newfeed USD 100 BTS/USD
Core Exchange Rate (CER)
\b
If no CER is provided, the cer will be the same as the settlement price
with a 5% premium (Only if the 'market' is against the core asset (e.g.
BTS)). The CER is always defined against the core asset (BTS). This
means that if the backing asset is not the core asset (BTS), then you must
specify your own cer as a float. The float `x` will be interpreted as
| python | {
"resource": ""
} |
q7105 | createcommittee | train | def createcommittee(ctx, url, account):
""" Setup a committee account for your account
"""
| python | {
"resource": ""
} |
q7106 | configuration | train | def configuration(ctx):
""" Show configuration variables
"""
t = [["Key", "Value"]] | python | {
"resource": ""
} |
q7107 | sign | train | def sign(ctx, filename):
""" Sign a json-formatted transaction
"""
if filename:
tx = filename.read()
else:
tx = sys.stdin.read()
| python | {
"resource": ""
} |
q7108 | witnesses | train | def witnesses(ctx):
""" List witnesses and relevant information
"""
t = [
[
"weight",
"account",
"signing_key",
"vote_id",
"url",
"total_missed",
"last_confirmed_block_num",
]
]
for witness in sorted(Witnesses(), key=lambda x: x.weight, reverse=True):
witness.refresh()
t.append(
[
"{:.2f}%".format(witness.weight * 100),
witness.account["name"], | python | {
"resource": ""
} |
q7109 | claim | train | def claim(ctx, vestingid, account, amount):
""" Claim funds from the vesting balance
"""
vesting = Vesting(vestingid)
if amount:
amount = | python | {
"resource": ""
} |
q7110 | offline | train | def offline(f):
""" This decorator allows you to access ``ctx.bitshares`` which is
an instance of BitShares with ``offline=True``.
"""
@click.pass_context
@verbose
def new_func(ctx, *args, **kwargs):
ctx.obj["offline"] = True
ctx.bitshares = BitShares(**ctx.obj)
| python | {
"resource": ""
} |
q7111 | verify | train | def verify(ctx, file, account):
""" Verify a signed message
"""
if not file:
print_message("Prompting for message. Terminate with CTRL-D", "info")
file = click.get_text_stream("stdin")
m = Message(file.read(), bitshares_instance=ctx.bitshares)
try:
if m.verify():
print_message("Verified", | python | {
"resource": ""
} |
q7112 | cloneaccount | train | def cloneaccount(ctx, account_name, account):
""" Clone an account
This copies the owner and active permissions as well as the
options (e.g. votes, memo key)
"""
from bitsharesbase import transactions, operations
account = Account(account)
op = {
"fee": {"amount": 0, "asset_id": "1.3.0"},
| python | {
"resource": ""
} |
q7113 | whitelist | train | def whitelist(ctx, whitelist_account, account):
""" Add an account to a whitelist
"""
| python | {
"resource": ""
} |
q7114 | blacklist | train | def blacklist(ctx, blacklist_account, account):
""" Add an account to a blacklist
"""
| python | {
"resource": ""
} |
q7115 | unlist | train | def unlist(ctx, unlist_account, account):
""" Remove an account from any list
"""
| python | {
"resource": ""
} |
q7116 | setproxy | train | def setproxy(ctx, proxy_account, account):
""" Set the proxy account for an account
"""
| python | {
"resource": ""
} |
q7117 | settlements | train | def settlements(ctx, asset, limit):
""" Show pending settlement orders of a bitasset
"""
from bitshares.asset import Asset
asset = Asset(asset, full=True)
if not asset.is_bitasset:
| python | {
"resource": ""
} |
q7118 | addkey | train | def addkey(ctx, key):
""" Add a private key to the wallet
"""
if not key:
while True:
key = click.prompt(
"Private Key (wif) [Enter to quit]",
hide_input=True,
show_default=False,
default="exit",
)
if not key or key == "exit":
break
try:
ctx.bitshares.wallet.addPrivateKey(key)
except Exception as e:
click.echo(str(e))
continue
else:
| python | {
"resource": ""
} |
q7119 | delkey | train | def delkey(ctx, pubkeys):
""" Delete a private key from the wallet
"""
if not pubkeys:
pubkeys = click.prompt("Public Keys").split(" ")
if click.confirm(
"Are you sure you want to delete keys from your wallet?\n"
"This step is IRREVERSIBLE! If you don't have a backup, "
| python | {
"resource": ""
} |
q7120 | create | train | def create(ctx):
""" Create default config file
"""
import shutil
this_dir, this_filename = os.path.split(__file__)
default_config_file = os.path.join(this_dir, "apis/example-config.yaml")
config_file = | python | {
"resource": ""
} |
q7121 | start | train | def start(ctx):
""" Start the API according to the config file
"""
module = ctx.config.get("api", "poloniex")
| python | {
"resource": ""
} |
q7122 | crawl | train | def crawl(url, callback, **kwargs):
"""Crawls an URL with given callback.
Parameters
----------
url : str
An URL to crawl.
callback : callable
A function to be used as spider callback for the given URL.
spider_cls : scrapy.Spider (default: DefaultSpider)
A spider class to be used in the crawler instance.
capture_items : bool (default: True)
If enabled, the scraped items are captured and returned.
return_crawler : bool (default: False)
If enabled, the crawler instance is returned. If ``capture_items`` is
enabled, the scraped items is collected in ``crawler.items``.
settings : dict, optional
Custom crawler settings.
timeout : int, (default: DEFAULT_TIMEOUT) | python | {
"resource": ""
} |
q7123 | _crawl_in_reactor | train | def _crawl_in_reactor(url, callback, spider_cls=DefaultSpider, **kwargs):
"""Crawls given URL with given callback.
Parameters
----------
url : str
The URL to crawl.
callback : callable
Function to be used as callback for the request.
spider_cls : scrapy.Spider (default: DefaultSpider)
A spider class to be used in the crawler instance.
kwargs : dict, optional
| python | {
"resource": ""
} |
q7124 | _run_spider_in_reactor | train | def _run_spider_in_reactor(spider_cls, capture_items=True, return_crawler=False,
settings=None, **kwargs):
"""Runs given spider inside the twisted reactdor.
Parameters
----------
spider_cls : scrapy.Spider
Spider to run.
capture_items : bool (default: True)
If enabled, the scraped items are captured and returned.
return_crawler : bool (default: False)
If enabled, the crawler instance is returned. If ``capture_items`` is
enabled, the scraped items is collected in ``crawler.items``.
settings : dict, optional
Custom crawler settings.
Returns
-------
out : crochet.EventualResult
If ``capture_items`` is ``True``, returns scraped items. If
``return_crawler`` is ``True``, returns the crawler instance.
| python | {
"resource": ""
} |
q7125 | override_start_requests | train | def override_start_requests(spider_cls, start_urls, callback=None, **attrs):
"""Returns a new spider class overriding the ``start_requests``.
This function is useful to replace the start requests of an existing spider
class on runtime.
Parameters
----------
spider_cls : scrapy.Spider
Spider class to be used as base class.
start_urls : iterable
Iterable of URLs or ``Request`` objects.
callback : callable, optional
Callback for the start URLs.
attrs : dict, optional
Additional class attributes.
Returns
-------
out : class
A subclass of ``spider_cls`` with overrided ``start_requests`` | python | {
"resource": ""
} |
q7126 | wait_for | train | def wait_for(timeout, func, *args, **kwargs):
"""Waits for a eventual result.
Parameters
----------
timeout : int
How much time to wait, in seconds.
func : callable
A function that returns ``crochet.EventualResult``.
args : tuple, optional
| python | {
"resource": ""
} |
q7127 | highlight | train | def highlight(code, lexer='html', formatter='html', output_wrapper=None):
"""Highlights given code using pygments."""
if not pygments:
raise TypeError("pygments module required")
if not isinstance(code, six.string_types):
code = pprint.pformat(code)
if isinstance(lexer, six.string_types):
lexer = pygments.lexers.get_lexer_by_name(lexer)
if isinstance(formatter, six.string_types):
formatter = pygments.formatters.get_formatter_by_name(formatter)
| python | {
"resource": ""
} |
q7128 | Netnode._get_next_slot | train | def _get_next_slot(self, tag):
'''
get the first unused supval table key, or 0 if the
table is empty.
useful for filling | python | {
"resource": ""
} |
q7129 | GitRunner.run_git | train | def run_git(self, args, git_env=None):
'''
Runs the git executable with the arguments given and returns a list of
lines produced on its standard output.
'''
popen_kwargs = {
'stdout': subprocess.PIPE,
'stderr': subprocess.PIPE,
}
if git_env:
popen_kwargs['env'] = git_env
if self._git_toplevel:
popen_kwargs['cwd'] = self._git_toplevel
git_process = subprocess.Popen(
[GitRunner._git_executable] + args,
**popen_kwargs
)
try:
out, err = git_process.communicate()
git_process.wait()
except Exception as e:
raise GitError("Couldn't run 'git {args}':{newline}{ex}".format(
args=' '.join(args),
newline=os.linesep,
ex=str(e)
| python | {
"resource": ""
} |
q7130 | add_signature_block | train | def add_signature_block(src_fileobj, dest_fileobj, signing_algorithm, signature=None):
"""Add a signature block to marfile, a MarReader object.
Productversion and channel are preserved, but any existing signatures are overwritten.
Args:
src_fileobj (file object): The input MAR file to add a signature to
dest_fileobj (file object): File object to write new MAR file to. Must be open in w+b mode.
signing_algorithm (str): One of 'sha1', or 'sha384'
signature (bytes): Signature to write, or None to use a dummy signature
"""
algo_id = {'sha1': 1, 'sha384': 2}[signing_algorithm]
if not signature:
signature = make_dummy_signature(algo_id)
src_fileobj.seek(0)
mardata = mar.parse_stream(src_fileobj)
# Header
header = mardata.header
dest_fileobj.write(mar_header.build(header))
# Signature block
sig = dict(algorithm_id=algo_id,
size=len(signature),
signature=signature,
)
# This will be fixed up later
filesize = 0
sigs_offset = dest_fileobj.tell()
sigs = sigs_header.build(dict(
filesize=filesize,
count=1,
sigs=[sig],
))
dest_fileobj.write(sigs)
# Write the additional section
dest_fileobj.write(extras_header.build(mardata.additional))
| python | {
"resource": ""
} |
q7131 | MarWriter.add | train | def add(self, path, compress=None):
"""Add `path` to the MAR file.
If `path` is a file, it will be added directly.
If `path` is a directory, it will be traversed recursively and all
files inside will be added.
| python | {
"resource": ""
} |
q7132 | MarWriter.add_dir | train | def add_dir(self, path, compress):
"""Add all files under directory `path` to the MAR file.
Args:
path (str): path to directory to add to this MAR file
compress (str): One of 'xz', 'bz2', or None. Defaults to None.
"""
if not os.path.isdir(path):
| python | {
"resource": ""
} |
q7133 | MarWriter.add_fileobj | train | def add_fileobj(self, fileobj, path, compress, flags=None):
"""Add the contents of a file object to the MAR file.
Args:
fileobj (file-like object): open file object
path (str): name of this file in the MAR file
compress (str): One of 'xz', 'bz2', or None. Defaults to None.
| python | {
"resource": ""
} |
q7134 | MarWriter.add_stream | train | def add_stream(self, stream, path, compress, flags):
"""Add the contents of an iterable to the MAR file.
Args:
stream (iterable): yields blocks of data
path (str): name of this file in the MAR file
compress (str): One of 'xz', 'bz2', or None. Defaults to None.
flags (int): permission of this file in the MAR file
"""
self.data_fileobj.seek(self.last_offset)
if compress == 'bz2':
stream = bz2_compress_stream(stream)
elif compress == 'xz':
stream = xz_compress_stream(stream)
elif compress is None:
pass
else:
| python | {
"resource": ""
} |
q7135 | MarWriter.add_file | train | def add_file(self, path, compress):
"""Add a single file to the MAR file.
Args:
path (str): path to a file to add to this MAR file.
compress (str): One of 'xz', 'bz2', or None. Defaults to None.
"""
if not os.path.isfile(path):
raise | python | {
"resource": ""
} |
q7136 | MarWriter.write_header | train | def write_header(self):
"""Write the MAR header to the file.
The MAR header includes the MAR magic bytes as well as the offset to
where the index data can be found.
"""
self.fileobj.seek(0)
| python | {
"resource": ""
} |
q7137 | MarWriter.dummy_signatures | train | def dummy_signatures(self):
"""Create a dummy signature.
This is used when initially writing the MAR header and we don't know
what the final signature data will be.
Returns:
Fake signature data suitable for writing to the header with
.write_signatures()
""" | python | {
"resource": ""
} |
q7138 | MarWriter.calculate_signatures | train | def calculate_signatures(self):
"""Calculate the signatures for this MAR file.
Returns:
A list of signature tuples: [(algorithm_id, signature_data), ...]
"""
if not self.signing_algorithm:
return []
algo_id = {'sha1': 1, 'sha384': 2}[self.signing_algorithm]
hashers = [(algo_id, make_hasher(algo_id))]
for block in | python | {
"resource": ""
} |
q7139 | MarWriter.write_signatures | train | def write_signatures(self, signatures):
"""Write signature data to the MAR file.
Args:
signatures (list): list of signature tuples of the form
(algorithm_id, signature_data)
"""
self.fileobj.seek(self.signature_offset)
sig_entries = [dict(algorithm_id=id_,
size=len(sig),
signature=sig)
for (id_, sig) in signatures]
sigs = sigs_header.build(dict(
filesize=self.filesize, | python | {
"resource": ""
} |
q7140 | MarWriter.write_additional | train | def write_additional(self, productversion, channel):
"""Write the additional information to the MAR header.
Args:
productversion (str): product and version string
channel (str): channel string
"""
self.fileobj.seek(self.additional_offset)
| python | {
"resource": ""
} |
q7141 | MarWriter.write_index | train | def write_index(self):
"""Write the index of all our files to the MAR file."""
self.fileobj.seek(self.last_offset)
index | python | {
"resource": ""
} |
q7142 | MarWriter.finish | train | def finish(self):
"""Finalize the MAR file.
The MAR header, index and signatures need to be updated once we've
finished adding all the files.
"""
# Update the last_offset in the mar header
self.write_header()
# Write out the index of contents
| python | {
"resource": ""
} |
q7143 | build_argparser | train | def build_argparser():
"""Build argument parser for the CLI."""
parser = ArgumentParser('Utility for managing MAR files')
create_group = parser.add_argument_group("Create a MAR file")
create_group.add_argument("-c", "--create", metavar="MARFILE", help="create MAR")
create_group.add_argument("-V", "--productversion", dest="productversion",
help="product/version string")
create_group.add_argument("-H", "--channel", dest="channel",
help="channel this MAR file is applicable to")
create_group.add_argument("files", nargs=REMAINDER,
help="files to add to the MAR file")
extract_group = parser.add_argument_group("Extract a MAR file")
extract_group.add_argument("-x", "--extract", help="extract MAR", metavar="MARFILE")
list_group = parser.add_argument_group("Print information on a MAR file")
list_group.add_argument("-t", "--list", help="print out MAR contents",
metavar="MARFILE")
list_group.add_argument("-T", "--list-detailed", metavar="MARFILE",
help="print out MAR contents including signatures")
verify_group = parser.add_argument_group("Verify a MAR file")
verify_group.add_argument("-v", "--verify", metavar="MARFILE",
help="verify the marfile")
parser.add_argument("-j", "--bzip2", action="store_const", dest="compression",
const="bz2", help="compress/decompress members with BZ2")
parser.add_argument("-J", "--xz", action="store_const", dest="compression",
const="xz", help="compress/decompress archive with XZ")
parser.add_argument("--auto", action="store_const", dest="compression",
| python | {
"resource": ""
} |
q7144 | do_extract | train | def do_extract(marfile, destdir, decompress):
"""Extract the MAR file to the destdir."""
with open(marfile, 'rb') as f:
with | python | {
"resource": ""
} |
q7145 | get_keys | train | def get_keys(keyfiles, signature_type):
"""Get public keys for the given keyfiles.
Args:
keyfiles: List of filenames with public keys, or :mozilla- prefixed key
names
signature_type: one of 'sha1' or 'sha384'
Returns:
List of public keys as strings
"""
builtin_keys = {
('release', 'sha1'): [mardor.mozilla.release1_sha1, mardor.mozilla.release2_sha1],
('release', 'sha384'): [mardor.mozilla.release1_sha384, mardor.mozilla.release2_sha384],
('nightly', 'sha1'): [mardor.mozilla.nightly1_sha1, mardor.mozilla.nightly2_sha1],
| python | {
"resource": ""
} |
q7146 | do_verify | train | def do_verify(marfile, keyfiles=None):
"""Verify the MAR file."""
try:
with open(marfile, 'rb') as f:
with MarReader(f) as m:
# Check various parts of the mar file
# e.g. signature algorithms and additional block sections
errors = m.get_errors()
if errors:
print("File is not well formed: {}".format(errors))
sys.exit(1)
if keyfiles:
try:
keys = get_keys(keyfiles, m.signature_type)
except ValueError as e:
print(e)
sys.exit(1)
| python | {
"resource": ""
} |
q7147 | do_list | train | def do_list(marfile, detailed=False):
"""
List the MAR file.
Yields lines of text to output
"""
with open(marfile, 'rb') as f:
with MarReader(f) as m:
if detailed:
if m.compression_type:
yield "Compression type: {}".format(m.compression_type)
if m.signature_type:
yield "Signature type: {}".format(m.signature_type)
if m.mardata.signatures:
plural = "s" if (m.mardata.signatures.count == 0 or m.mardata.signatures.count > 1) else ""
yield "Signature block found with {} signature{}".format(m.mardata.signatures.count, plural)
for s in m.mardata.signatures.sigs:
yield "- Signature {} size {}".format(s.algorithm_id, s.size)
yield ""
if m.mardata.additional:
yield "{} additional block found:".format(len(m.mardata.additional.sections))
for s in m.mardata.additional.sections:
| python | {
"resource": ""
} |
q7148 | do_create | train | def do_create(marfile, files, compress, productversion=None, channel=None,
signing_key=None, signing_algorithm=None):
"""Create a new MAR file."""
with open(marfile, 'w+b') as f:
with MarWriter(f, productversion=productversion, channel=channel,
| python | {
"resource": ""
} |
q7149 | do_hash | train | def do_hash(hash_algo, marfile, asn1=False):
"""Output the hash for this MAR file."""
# Add a dummy signature into a temporary file
dst = tempfile.TemporaryFile()
with open(marfile, 'rb') as f:
add_signature_block(f, dst, hash_algo)
dst.seek(0)
with MarReader(dst) as m:
| python | {
"resource": ""
} |
q7150 | do_add_signature | train | def do_add_signature(input_file, output_file, signature_file):
"""Add a signature to the MAR file."""
signature = open(signature_file, 'rb').read()
if len(signature) == 256:
hash_algo = 'sha1'
elif len(signature) == 512:
hash_algo = 'sha384'
else:
| python | {
"resource": ""
} |
q7151 | check_args | train | def check_args(parser, args):
"""Validate commandline arguments."""
# Make sure only one action has been specified
if len([a for a in [args.create, args.extract, args.verify, args.list,
args.list_detailed, args.hash, args.add_signature] if a
is not None]) != 1:
parser.error("Must specify something to do (one of -c, -x, -t, -T, -v, --hash, --add-signature)")
if args.create and not args.files:
parser.error("Must specify at least one file to add to marfile")
| python | {
"resource": ""
} |
q7152 | get_key_from_cmdline | train | def get_key_from_cmdline(parser, args):
"""Return the signing key and signing algoritm from the commandline."""
if args.keyfiles:
signing_key = open(args.keyfiles[0], 'rb').read()
bits = get_keysize(signing_key)
if bits == 2048:
signing_algorithm = 'sha1'
| python | {
"resource": ""
} |
q7153 | main | train | def main(argv=None):
"""Run the main CLI entry point."""
parser = build_argparser()
args = parser.parse_args(argv)
logging.basicConfig(level=args.loglevel, format="%(message)s")
check_args(parser, args)
if args.extract:
marfile = os.path.abspath(args.extract)
if args.chdir:
os.chdir(args.chdir)
do_extract(marfile, os.getcwd(), args.compression)
elif args.verify:
do_verify(args.verify, args.keyfiles)
elif args.list:
print("\n".join(do_list(args.list)))
elif args.list_detailed:
print("\n".join(do_list(args.list_detailed, detailed=True)))
elif args.create:
marfile = os.path.abspath(args.create)
signing_key, signing_algorithm = get_key_from_cmdline(parser, args)
if args.chdir:
os.chdir(args.chdir)
| python | {
"resource": ""
} |
q7154 | MarReader.compression_type | train | def compression_type(self):
"""Return the latest compresion type used in this MAR.
Returns:
One of None, 'bz2', or 'xz'
"""
best_compression = None
for e in self.mardata.index.entries:
| python | {
"resource": ""
} |
q7155 | MarReader.signature_type | train | def signature_type(self):
"""Return the signature type used in this MAR.
Returns:
One of None, 'unknown', 'sha1', or 'sha384'
"""
if not self.mardata.signatures:
return None
for sig in self.mardata.signatures.sigs:
| python | {
"resource": ""
} |
q7156 | MarReader.extract_entry | train | def extract_entry(self, e, decompress='auto'):
"""Yield blocks of data for this entry from this MAR file.
Args:
e (:obj:`mardor.format.index_entry`): An index_entry object that
refers to this file's size and offset inside the MAR file.
path (str): Where on disk to extract this file to.
decompress (str, optional): Controls whether files are decompressed
when extracted. Must be one of None, 'auto', 'bz2', or 'xz'.
| python | {
"resource": ""
} |
q7157 | MarReader.extract | train | def extract(self, destdir, decompress='auto'):
"""Extract the entire MAR file into a directory.
Args:
destdir (str): A local directory on disk into which the contents of
this MAR file will be extracted. Required parent directories
will be created as necessary.
decompress (obj, optional): Controls whether files are decompressed
when extracted. Must be one of 'auto' or None. Defaults to
'auto'.
"""
for e in self.mardata.index.entries:
| python | {
"resource": ""
} |
q7158 | MarReader.get_errors | train | def get_errors(self):
"""Verify that this MAR file is well formed.
Returns:
A list of strings describing errors in the MAR file
None if this MAR file appears well formed.
"""
errors = []
errors.extend(self._get_signature_errors()) | python | {
"resource": ""
} |
q7159 | MarReader.verify | train | def verify(self, verify_key):
"""Verify that this MAR file has a valid signature.
Args:
verify_key (str): PEM formatted public key
Returns:
True if the MAR file's signature matches its contents
False otherwise; this includes cases where there is no signature.
"""
if not self.mardata.signatures or not self.mardata.signatures.sigs:
# This MAR file can't be verified since it has no signatures
return False
hashers = []
for sig in self.mardata.signatures.sigs:
hashers.append((sig.algorithm_id, sig.signature, make_hasher(sig.algorithm_id)))
assert len(hashers) == len(self.mardata.signatures.sigs)
| python | {
"resource": ""
} |
q7160 | MarReader.productinfo | train | def productinfo(self):
"""Return the productversion and channel of this MAR if present."""
if not self.mardata.additional:
return None
for s in self.mardata.additional.sections:
| python | {
"resource": ""
} |
q7161 | MarReader.calculate_hashes | train | def calculate_hashes(self):
"""Return hashes of the contents of this MAR file.
The hashes depend on the algorithms defined in the MAR file's signature block.
Returns:
A list of (algorithm_id, hash) tuples
"""
hashers = []
| python | {
"resource": ""
} |
q7162 | _has_extras | train | def _has_extras(ctx):
"""Determine if a MAR file has an additional section block or not.
It does this by looking at where file data starts in the file. If this
starts immediately after the signature data, then no additional sections are present.
Args:
ctx (context): construct parsing context
Returns:
| python | {
"resource": ""
} |
q7163 | get_publickey | train | def get_publickey(keydata):
"""Load the public key from a PEM encoded string."""
try:
key = serialization.load_pem_public_key(
keydata,
backend=default_backend(),
)
return key
except ValueError:
key = serialization.load_pem_private_key( | python | {
"resource": ""
} |
q7164 | get_privatekey | train | def get_privatekey(keydata):
"""Load the private key from a PEM encoded string."""
key = serialization.load_pem_private_key(
keydata,
| python | {
"resource": ""
} |
q7165 | get_signature_data | train | def get_signature_data(fileobj, filesize):
"""Read data from MAR file that is required for MAR signatures.
Args:
fileboj (file-like object): file-like object to read the MAR data from
filesize (int): the total size of the file
Yields:
blocks of bytes representing the data required to generate or validate
signatures.
"""
# Read everything except the signature entries
# The first 8 bytes are covered, as is everything from the beginning
# of the additional section to the end of the file. The signature
# algorithm id and size fields are also covered.
fileobj.seek(0)
marfile = mar.parse_stream(fileobj)
if not marfile.signatures:
raise IOError("Can't generate signature data for file without signature blocks")
# MAR header
fileobj.seek(0)
block = fileobj.read(8) | python | {
"resource": ""
} |
q7166 | make_hasher | train | def make_hasher(algorithm_id):
"""Create a hashing object for the given signing algorithm."""
if algorithm_id == 1:
return hashes.Hash(hashes.SHA1(), default_backend())
elif algorithm_id == 2:
return | python | {
"resource": ""
} |
q7167 | sign_hash | train | def sign_hash(private_key, hash, hash_algo):
"""Sign the given hash with the given private key.
Args:
private_key (str): PEM enoded private key
hash (byte str): hash to sign
hash_algo (str): name of hash algorithm used
Returns:
byte string representing the signature
| python | {
"resource": ""
} |
q7168 | verify_signature | train | def verify_signature(public_key, signature, hash, hash_algo):
"""Verify the given signature is correct for the given hash and public key.
Args:
public_key (str): PEM encoded public key
signature (bytes): signature to verify
hash (bytes): hash of data
| python | {
"resource": ""
} |
q7169 | make_rsa_keypair | train | def make_rsa_keypair(bits):
"""Generate an RSA keypair.
Args:
bits (int): number of bits to use for the key.
Returns:
(private_key, public_key) - both as PEM encoded strings
"""
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=bits,
backend=default_backend(),
)
private_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
| python | {
"resource": ""
} |
q7170 | mkdir | train | def mkdir(path):
"""Make a directory and its parents.
Args:
path (str): path to create
Returns:
None
Raises:
OSError if the directory cannot be created.
"""
try:
os.makedirs(path)
# sanity check
| python | {
"resource": ""
} |
q7171 | write_to_file | train | def write_to_file(src, dst):
"""Write data from `src` into `dst`.
Args:
src (iterable): iterable that yields blocks of data to write
dst (file-like object): file-like object that must support
.write(block)
Returns:
number of | python | {
"resource": ""
} |
q7172 | auto_decompress_stream | train | def auto_decompress_stream(src):
"""Decompress data from `src` if required.
If the first block of `src` appears to be compressed, then the entire
stream will be uncompressed. Otherwise the stream will be passed along
as-is.
Args:
src (iterable): iterable that yields blocks of data
Yields:
blocks of uncompressed data
"""
block = next(src) | python | {
"resource": ""
} |
q7173 | path_is_inside | train | def path_is_inside(path, dirname):
"""Return True if path is under dirname."""
path = os.path.abspath(path)
dirname = os.path.abspath(dirname)
while len(path) >= len(dirname):
if path == dirname:
return True
| python | {
"resource": ""
} |
q7174 | safejoin | train | def safejoin(base, *elements):
"""Safely joins paths together.
The result will always be a subdirectory under `base`, otherwise ValueError
is raised.
Args:
base (str): base path
elements (list of strings): path elements to join to base
Returns:
elements joined to base
"""
# TODO: do | python | {
"resource": ""
} |
q7175 | filesize | train | def filesize(fileobj):
"""Return the number of bytes in the fileobj.
This function seeks to the end of the file, and then back to the original position.
"""
| python | {
"resource": ""
} |
q7176 | _sync | train | def _sync(timeout=None):
"""I will wait until all pending handlers cothreads have completed
"""
evt = WeakEvent(auto_reset=False)
# first ensure that any pending callbacks from worker threads have been delivered
# these are calls of _fromMain()
Callback(evt.Signal)
evt.Wait(timeout=timeout)
evt.Reset() # reuse
# grab the current set of inprogress cothreads/events
| python | {
"resource": ""
} |
q7177 | set_debug | train | def set_debug(lvl):
"""Set PVA global debug print level. This prints directly to stdout,
bypassing eg. sys.stdout.
:param lvl: logging.* level or logLevel*
"""
| python | {
"resource": ""
} |
q7178 | cleanup | train | def cleanup():
"""P4P sequenced shutdown. Intended to be atexit. Idenpotent.
"""
_log.debug("P4P atexit begins")
# clean provider registry
from .server import clearProviders, _cleanup_servers
clearProviders()
# close client contexts
from .client.raw import _cleanup_contexts
_cleanup_contexts()
# stop servers
| python | {
"resource": ""
} |
q7179 | Value.changed | train | def changed(self, *fields):
"""Test if one or more fields have changed.
A field is considered to have changed if it has been marked as changed,
or if any of its parent, or child, fields have been marked as changed.
"""
S = super(Value, self).changed
| python | {
"resource": ""
} |
q7180 | NTNDArray.wrap | train | def wrap(self, value):
"""Wrap numpy.ndarray as Value
"""
attrib = getattr(value, 'attrib', {})
S, NS = divmod(time.time(), 1.0)
value = numpy.asarray(value) # loses any special/augmented attributes
dims = list(value.shape)
dims.reverse() # inner-most sent as left
if 'ColorMode' not in attrib:
# attempt to infer color mode from shape
if value.ndim==2:
attrib['ColorMode'] = 0 # gray
elif value.ndim==3:
for idx,dim in enumerate(dims):
if dim==3: # assume it's a color
attrib['ColorMode'] = 2 + idx # 2 - RGB1, 3 - RGB2, 4 - RGB3
break # assume that the first is color, and any subsequent dim=3 is a thin ROI
dataSize = value.nbytes
return Value(self.type, {
'value': (self._code2u[value.dtype.char], value.flatten()),
'compressedSize': dataSize,
| python | {
"resource": ""
} |
q7181 | NTNDArray.unwrap | train | def unwrap(klass, value):
"""Unwrap Value as NTNDArray
"""
V = value.value
if V is None:
# Union empty. treat as zero-length char array
V = | python | {
"resource": ""
} |
q7182 | defaultBuilder | train | def defaultBuilder(value, nt):
"""Reasonably sensible default handling of put builder
"""
if callable(value):
def logbuilder(V):
try:
value(V)
except:
_log.exception("Error in Builder")
raise # will be logged again
return logbuilder
def builder(V):
try:
if isinstance(value, Value):
V[None] = value
elif | python | {
"resource": ""
} |
q7183 | Context.disconnect | train | def disconnect(self, name=None):
"""Clear internal Channel cache, allowing currently unused channels to be implictly closed.
:param str name: None, to clear the entire cache, or a name string to clear only a certain entry.
"""
if name is None:
| python | {
"resource": ""
} |
q7184 | Context._request | train | def _request(self, process=None, wait=None):
"""helper for building pvRequests
:param str process: Control remote processing. May be 'true', 'false', 'passive', or None.
:param bool wait: Wait for all server processing to complete.
| python | {
"resource": ""
} |
q7185 | Context.get | train | def get(self, name, handler, request=None):
"""Begin Fetch of current value of a PV
:param name: A single name string or list of name strings
:param request: A :py:class:`p4p.Value` or string to qualify this request, or None to use a default.
:param callable handler: Completion notification. Called with a Value, RemoteError, or Cancelled
:returns: A object with a method cancel() which may be used to abort | python | {
"resource": ""
} |
q7186 | Context.put | train | def put(self, name, handler, builder=None, request=None, get=True):
"""Write a new value to a PV.
:param name: A single name string or list of name strings
:param callable handler: Completion notification. Called with None (success), RemoteError, or Cancelled
:param callable builder: Called when the PV Put type is known. A builder is responsible
for filling in the Value to be sent. builder(value)
:param request: A :py:class:`p4p.Value` or string to qualify this request, or None to use a default.
:param bool get: Whether to do a Get before the Put. If True then the value passed to the builder callable
will be initialized | python | {
"resource": ""
} |
q7187 | Context.rpc | train | def rpc(self, name, handler, value, request=None):
"""Perform RPC operation on PV
:param name: A single name string or list of name strings
:param callable handler: Completion notification. Called with a Value, RemoteError, or Cancelled
:param request: A :py:class:`p4p.Value` or string to qualify | python | {
"resource": ""
} |
q7188 | Context.monitor | train | def monitor(self, name, handler, request=None, **kws):
"""Begin subscription to named PV
:param str name: PV name string
:param callable handler: Completion notification. Called with None (FIFO not empty), RemoteError, Cancelled, or Disconnected
:param request: A :py:class:`p4p.Value` or string to qualify this | python | {
"resource": ""
} |
q7189 | timesout | train | def timesout(deftimeout=5.0):
"""Decorate a coroutine to implement an overall timeout.
The decorated coroutine will have an additional keyword
argument 'timeout=' which gives a timeout in seconds,
or None to disable timeout.
:param float deftimeout: The default timeout= for the decorated coroutine.
It is suggested perform one overall timeout at a high level
rather than multiple timeouts on low-level operations. ::
@timesout()
@asyncio.coroutine
def dostuff(ctxt):
yield from ctxt.put('msg', 'Working')
| python | {
"resource": ""
} |
q7190 | Subscription.close | train | def close(self):
"""Begin closing subscription.
"""
if self._S is not None:
# after .close() self._event should never be called
| python | {
"resource": ""
} |
q7191 | Context.close | train | def close(self):
"""Force close all Channels and cancel all Operations
"""
if self._Q is not None:
for T in self._T:
self._Q.interrupt()
for n, T in enumerate(self._T):
_log.debug('Join Context worker | python | {
"resource": ""
} |
q7192 | rpc | train | def rpc(rtype=None):
"""Decorator marks a method for export.
:param type: Specifies which :py:class:`Type` this method will return.
The return type (rtype) must be one of:
- An instance of :py:class:`p4p.Type`
- None, in which case the method must return a :py:class:`p4p.Value`
- One of the NT helper classes (eg :py:class:`p4p.nt.NTScalar`).
- A list or tuple used to construct a :py:class:`p4p.Type`.
Exported methods raise an :py:class:`Exception` to indicate an error to the remote caller.
:py:class:`RemoteError` may be raised to send a specific message describing the error condition.
>>> class Example(object):
@rpc(NTScalar.buildType('d'))
def add(self, lhs, rhs):
return {'value':float(lhs)+flost(rhs)}
"""
wrap = None
if rtype | python | {
"resource": ""
} |
q7193 | rpccall | train | def rpccall(pvname, request=None, rtype=None):
"""Decorator marks a client proxy method.
:param str pvname: The PV name, which will be formated using the 'format' argument of the proxy class constructor.
:param request: A pvRequest string or :py:class:`p4p.Value` | python | {
"resource": ""
} |
q7194 | quickRPCServer | train | def quickRPCServer(provider, prefix, target,
maxsize=20,
workers=1,
useenv=True, conf=None, isolate=False):
"""Run an RPC server in the current thread
Calls are handled sequentially, and always in the current thread, if workers=1 (the default).
If workers>1 then calls are handled concurrently by a pool of worker threads.
Requires NTURI style argument encoding.
:param str provider: A provider name. Must be unique in this process.
:param str prefix: PV name prefix. Along with method names, must be globally unique.
:param target: The object which is exporting methods. (use the :func:`rpc` decorator)
:param int maxsize: Number of pending RPC calls to be queued.
:param int workers: Number of worker threads (default 1)
:param useenv: Passed to :class:`~p4p.server.Server`
:param conf: Passed to :class:`~p4p.server.Server`
| python | {
"resource": ""
} |
q7195 | rpcproxy | train | def rpcproxy(spec):
"""Decorator to enable this class to proxy RPC client calls
The decorated class constructor takes two additional arguments,
`context=` is required to be a :class:`~p4p.client.thread.Context`.
`format`= can be a string, tuple, or dictionary and is applied
to PV name strings given to :py:func:`rpcall`.
Other arguments are passed to the user class constructor. ::
@rpcproxy
class MyProxy(object):
@rpccall("%s:add")
def add(lhs='d', rhs='d'):
pass
ctxt = Context('pva')
proxy = MyProxy(context=ctxt, format="tst:") # evaluates "%s:add"%"tst:"
The decorated class will be a sub-class of the provided class and :class:`RPCProxyBase`.
"""
| python | {
"resource": ""
} |
q7196 | ClientUnwrapper.unwrap | train | def unwrap(self, val):
"""Unpack a Value as some other python type
"""
if | python | {
"resource": ""
} |
q7197 | NTTable.buildType | train | def buildType(columns=[], extra=[]):
"""Build a table
:param list columns: List of column names and types. eg [('colA', 'd')]
:param list extra: A list of tuples describing additional non-standard fields
:returns: A :py:class:`Type`
"""
return Type(id="epics:nt/NTTable:1.0",
spec=[
('labels', 'as'),
| python | {
"resource": ""
} |
q7198 | NTTable.wrap | train | def wrap(self, values):
"""Pack an iterable of dict into a Value
>>> T=NTTable([('A', 'ai'), ('B', 'as')])
>>> V = T.wrap([
{'A':42, 'B':'one'},
{'A':43, 'B':'two'},
])
"""
if isinstance(values, Value):
return values
cols = dict([(L, []) for L in self.labels])
try:
# unzip list of dict
for V in values:
for L in self.labels:
try:
cols[L].append(V[L])
except (IndexError, KeyError):
pass
# allow omit empty columns
for L in self.labels:
V = cols[L] | python | {
"resource": ""
} |
q7199 | NTTable.unwrap | train | def unwrap(value):
"""Iterate an NTTable
:returns: An iterator yielding an OrderedDict for each column
"""
ret = []
# build lists of column names, and value
lbl, cols = [], []
for cname, cval in value.value.items():
lbl.append(cname)
| python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.