repo stringlengths 7 55 | path stringlengths 4 127 | func_name stringlengths 1 88 | original_string stringlengths 75 19.8k | language stringclasses 1
value | code stringlengths 75 19.8k | code_tokens listlengths 20 707 | docstring stringlengths 3 17.3k | docstring_tokens listlengths 3 222 | sha stringlengths 40 40 | url stringlengths 87 242 | partition stringclasses 1
value | idx int64 0 252k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
launchdarkly/relayCommander | relay_commander/rc.py | update_redis | def update_redis(project: str, environment: str, feature: str, state: str) \
-> None:
"""
Update redis state for a feature flag.
:param project: LaunchDarkly project key.
:param environment: LaunchDarkly environment key.
:param feature: LaunchDarkly feature key.
:param state: State for a feature flag.
"""
try:
hosts = RedisWrapper.connection_string_parser(
os.environ.get('REDIS_HOSTS'))
except RuntimeError as ex:
LOG.error(ex)
sys.exit(1)
for host in hosts:
LOG.info("connecting to %s:%s", host.host, host.port)
try:
if valid_state(state):
new_state = state.lower()
redis = RedisWrapper(
host.host,
host.port,
project,
environment
)
redis.update_flag_record(new_state, feature)
create_file(project, environment, feature, new_state)
LOG.info("%s was successfully updated.", feature)
else:
raise Exception('Invalid state: {0}, -s needs \
to be either on or off.'.format(state))
except KeyError as ex:
LOG.error("unable to update %s. Exception: %s",
host.host,
ex)
sys.exit(1) | python | def update_redis(project: str, environment: str, feature: str, state: str) \
-> None:
"""
Update redis state for a feature flag.
:param project: LaunchDarkly project key.
:param environment: LaunchDarkly environment key.
:param feature: LaunchDarkly feature key.
:param state: State for a feature flag.
"""
try:
hosts = RedisWrapper.connection_string_parser(
os.environ.get('REDIS_HOSTS'))
except RuntimeError as ex:
LOG.error(ex)
sys.exit(1)
for host in hosts:
LOG.info("connecting to %s:%s", host.host, host.port)
try:
if valid_state(state):
new_state = state.lower()
redis = RedisWrapper(
host.host,
host.port,
project,
environment
)
redis.update_flag_record(new_state, feature)
create_file(project, environment, feature, new_state)
LOG.info("%s was successfully updated.", feature)
else:
raise Exception('Invalid state: {0}, -s needs \
to be either on or off.'.format(state))
except KeyError as ex:
LOG.error("unable to update %s. Exception: %s",
host.host,
ex)
sys.exit(1) | [
"def",
"update_redis",
"(",
"project",
":",
"str",
",",
"environment",
":",
"str",
",",
"feature",
":",
"str",
",",
"state",
":",
"str",
")",
"->",
"None",
":",
"try",
":",
"hosts",
"=",
"RedisWrapper",
".",
"connection_string_parser",
"(",
"os",
".",
... | Update redis state for a feature flag.
:param project: LaunchDarkly project key.
:param environment: LaunchDarkly environment key.
:param feature: LaunchDarkly feature key.
:param state: State for a feature flag. | [
"Update",
"redis",
"state",
"for",
"a",
"feature",
"flag",
"."
] | eee7fa22f04edc3854dd53c3ec2db8c599ad1e89 | https://github.com/launchdarkly/relayCommander/blob/eee7fa22f04edc3854dd53c3ec2db8c599ad1e89/relay_commander/rc.py#L42-L80 | train | 51,300 |
launchdarkly/relayCommander | relay_commander/rc.py | update_ld_api | def update_ld_api(project: str, environment: str, feature: str, state: str):
"""
Execute command against the LaunchDarkly API.
This command is generally not used directly, instead it is called as a
part of running the ``playback()`` function.
:param project: LaunchDarkly project key.
:param environment: LaunchDarkly environment key.
:param feature: LaunchDarkly feature key.
:param state: State for a feature flag.
"""
ld_api = LaunchDarklyApi(
os.environ.get('LD_API_KEY'),
project,
environment
)
if valid_state(state):
if state.lower() == 'off':
new_state = False
else:
new_state = True
ld_api.update_flag(new_state, feature)
else:
raise ValueError('Invalid state: {0}, -s needs to be either \
on or off.'.format(state)) | python | def update_ld_api(project: str, environment: str, feature: str, state: str):
"""
Execute command against the LaunchDarkly API.
This command is generally not used directly, instead it is called as a
part of running the ``playback()`` function.
:param project: LaunchDarkly project key.
:param environment: LaunchDarkly environment key.
:param feature: LaunchDarkly feature key.
:param state: State for a feature flag.
"""
ld_api = LaunchDarklyApi(
os.environ.get('LD_API_KEY'),
project,
environment
)
if valid_state(state):
if state.lower() == 'off':
new_state = False
else:
new_state = True
ld_api.update_flag(new_state, feature)
else:
raise ValueError('Invalid state: {0}, -s needs to be either \
on or off.'.format(state)) | [
"def",
"update_ld_api",
"(",
"project",
":",
"str",
",",
"environment",
":",
"str",
",",
"feature",
":",
"str",
",",
"state",
":",
"str",
")",
":",
"ld_api",
"=",
"LaunchDarklyApi",
"(",
"os",
".",
"environ",
".",
"get",
"(",
"'LD_API_KEY'",
")",
",",
... | Execute command against the LaunchDarkly API.
This command is generally not used directly, instead it is called as a
part of running the ``playback()`` function.
:param project: LaunchDarkly project key.
:param environment: LaunchDarkly environment key.
:param feature: LaunchDarkly feature key.
:param state: State for a feature flag. | [
"Execute",
"command",
"against",
"the",
"LaunchDarkly",
"API",
"."
] | eee7fa22f04edc3854dd53c3ec2db8c599ad1e89 | https://github.com/launchdarkly/relayCommander/blob/eee7fa22f04edc3854dd53c3ec2db8c599ad1e89/relay_commander/rc.py#L100-L126 | train | 51,301 |
launchdarkly/relayCommander | relay_commander/rc.py | generate_relay_config | def generate_relay_config(project):
"""
Generate Relay Proxy Configuration.
Generate a ld-relay.conf file to quickly spin up a relay proxy.
Right now this is mostly used for integration testing.
:param project: LaunchDarkly project key
"""
ld_api = LaunchDarklyApi(
os.environ.get('LD_API_KEY'),
project_key=project
)
config = ConfigGenerator()
envs = ld_api.get_environments(project)
config.generate_relay_config(envs) | python | def generate_relay_config(project):
"""
Generate Relay Proxy Configuration.
Generate a ld-relay.conf file to quickly spin up a relay proxy.
Right now this is mostly used for integration testing.
:param project: LaunchDarkly project key
"""
ld_api = LaunchDarklyApi(
os.environ.get('LD_API_KEY'),
project_key=project
)
config = ConfigGenerator()
envs = ld_api.get_environments(project)
config.generate_relay_config(envs) | [
"def",
"generate_relay_config",
"(",
"project",
")",
":",
"ld_api",
"=",
"LaunchDarklyApi",
"(",
"os",
".",
"environ",
".",
"get",
"(",
"'LD_API_KEY'",
")",
",",
"project_key",
"=",
"project",
")",
"config",
"=",
"ConfigGenerator",
"(",
")",
"envs",
"=",
"... | Generate Relay Proxy Configuration.
Generate a ld-relay.conf file to quickly spin up a relay proxy.
Right now this is mostly used for integration testing.
:param project: LaunchDarkly project key | [
"Generate",
"Relay",
"Proxy",
"Configuration",
"."
] | eee7fa22f04edc3854dd53c3ec2db8c599ad1e89 | https://github.com/launchdarkly/relayCommander/blob/eee7fa22f04edc3854dd53c3ec2db8c599ad1e89/relay_commander/rc.py#L131-L147 | train | 51,302 |
mjirik/io3d | io3d/dcmreaddata.py | dicomdir_info | def dicomdir_info(dirpath, *args, **kwargs):
""" Get information about series in dir"""
dr = DicomReader(dirpath=dirpath, *args, **kwargs)
info = dr.dicomdirectory.get_stats_of_series_in_dir()
return info | python | def dicomdir_info(dirpath, *args, **kwargs):
""" Get information about series in dir"""
dr = DicomReader(dirpath=dirpath, *args, **kwargs)
info = dr.dicomdirectory.get_stats_of_series_in_dir()
return info | [
"def",
"dicomdir_info",
"(",
"dirpath",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"dr",
"=",
"DicomReader",
"(",
"dirpath",
"=",
"dirpath",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"info",
"=",
"dr",
".",
"dicomdirectory",
".",
"g... | Get information about series in dir | [
"Get",
"information",
"about",
"series",
"in",
"dir"
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L45-L49 | train | 51,303 |
mjirik/io3d | io3d/dcmreaddata.py | is_dicom_dir | def is_dicom_dir(datapath):
"""
Check if in dir is one or more dicom file. We use two methods.
First is based on dcm extension detection.
"""
# Second tries open files
# with dicom module.
retval = False
datapath = op.expanduser(datapath)
for f in os.listdir(datapath):
if f.endswith((".dcm", ".DCM")):
retval = True
return True
# @todo not working and I dont know why
try:
pydicom.read_file(os.path.join(datapath, f))
retval = True
# except pydicom.errors.InvalidDicomError:
# logger.debug("Invalid Dicom while reading file " + str(f))
except Exception as e:
logger.warning("Unable to read dicom file " + str(f))
logger.warning(e)
# import traceback
# traceback.print_exc()
if retval:
return True
return False | python | def is_dicom_dir(datapath):
"""
Check if in dir is one or more dicom file. We use two methods.
First is based on dcm extension detection.
"""
# Second tries open files
# with dicom module.
retval = False
datapath = op.expanduser(datapath)
for f in os.listdir(datapath):
if f.endswith((".dcm", ".DCM")):
retval = True
return True
# @todo not working and I dont know why
try:
pydicom.read_file(os.path.join(datapath, f))
retval = True
# except pydicom.errors.InvalidDicomError:
# logger.debug("Invalid Dicom while reading file " + str(f))
except Exception as e:
logger.warning("Unable to read dicom file " + str(f))
logger.warning(e)
# import traceback
# traceback.print_exc()
if retval:
return True
return False | [
"def",
"is_dicom_dir",
"(",
"datapath",
")",
":",
"# Second tries open files",
"# with dicom module.",
"retval",
"=",
"False",
"datapath",
"=",
"op",
".",
"expanduser",
"(",
"datapath",
")",
"for",
"f",
"in",
"os",
".",
"listdir",
"(",
"datapath",
")",
":",
... | Check if in dir is one or more dicom file. We use two methods.
First is based on dcm extension detection. | [
"Check",
"if",
"in",
"dir",
"is",
"one",
"or",
"more",
"dicom",
"file",
".",
"We",
"use",
"two",
"methods",
".",
"First",
"is",
"based",
"on",
"dcm",
"extension",
"detection",
"."
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L52-L81 | train | 51,304 |
mjirik/io3d | io3d/dcmreaddata.py | files_in_dir | def files_in_dir(dirpath, wildcard="*", startpath=None):
"""
Function generates list of files from specific dir
files_in_dir(dirpath, wildcard="*.*", startpath=None)
dirpath: required directory
wilcard: mask for files
startpath: start for relative path
Example
files_in_dir('medical/jatra-kiv','*.dcm', '~/data/')
"""
import glob
filelist = []
if startpath is not None:
completedirpath = os.path.join(startpath, dirpath)
else:
completedirpath = dirpath
if os.path.exists(completedirpath):
logger.info('completedirpath = ' + completedirpath)
else:
logger.error('Wrong path: ' + completedirpath)
raise Exception('Wrong path : ' + completedirpath)
for infile in glob.glob(os.path.join(completedirpath, wildcard)):
filelist.append(infile)
if len(filelist) == 0:
logger.error('No required files in path: ' + completedirpath)
raise Exception('No required file in path: ' + completedirpath)
return filelist | python | def files_in_dir(dirpath, wildcard="*", startpath=None):
"""
Function generates list of files from specific dir
files_in_dir(dirpath, wildcard="*.*", startpath=None)
dirpath: required directory
wilcard: mask for files
startpath: start for relative path
Example
files_in_dir('medical/jatra-kiv','*.dcm', '~/data/')
"""
import glob
filelist = []
if startpath is not None:
completedirpath = os.path.join(startpath, dirpath)
else:
completedirpath = dirpath
if os.path.exists(completedirpath):
logger.info('completedirpath = ' + completedirpath)
else:
logger.error('Wrong path: ' + completedirpath)
raise Exception('Wrong path : ' + completedirpath)
for infile in glob.glob(os.path.join(completedirpath, wildcard)):
filelist.append(infile)
if len(filelist) == 0:
logger.error('No required files in path: ' + completedirpath)
raise Exception('No required file in path: ' + completedirpath)
return filelist | [
"def",
"files_in_dir",
"(",
"dirpath",
",",
"wildcard",
"=",
"\"*\"",
",",
"startpath",
"=",
"None",
")",
":",
"import",
"glob",
"filelist",
"=",
"[",
"]",
"if",
"startpath",
"is",
"not",
"None",
":",
"completedirpath",
"=",
"os",
".",
"path",
".",
"jo... | Function generates list of files from specific dir
files_in_dir(dirpath, wildcard="*.*", startpath=None)
dirpath: required directory
wilcard: mask for files
startpath: start for relative path
Example
files_in_dir('medical/jatra-kiv','*.dcm', '~/data/') | [
"Function",
"generates",
"list",
"of",
"files",
"from",
"specific",
"dir"
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L357-L394 | train | 51,305 |
mjirik/io3d | io3d/dcmreaddata.py | get_slice_location | def get_slice_location(dcmdata, teil=None):
""" get location of the slice
:param dcmdata: dicom data structure
:param teil: filename. Used when slice location doesnt exist
:return:
"""
slice_location = None
if hasattr(dcmdata, 'SliceLocation'):
# print(dcmdata.SliceLocation)
# print(type(dcmdata.SliceLocation))
try:
slice_location = float(dcmdata.SliceLocation)
except Exception as exc:
logger.info("It is not possible to use SliceLocation")
logger.debug(traceback.format_exc())
if slice_location is None and hasattr(dcmdata, "SliceThickness") and teil is not None:
logger.debug(
"Estimating SliceLocation wiht image number and SliceThickness"
)
# from builtins import map
i = list(map(int, re.findall('\d+', teil)))
i = i[-1]
try:
slice_location = float(i * float(dcmdata.SliceThickness))
except ValueError as e:
print(type(dcmdata.SliceThickness))
print(dcmdata.SliceThickness)
logger.debug(traceback.format_exc())
logger.debug("SliceThickness problem")
if slice_location is None and hasattr(dcmdata, "ImagePositionPatient") and hasattr(dcmdata,
"ImageOrientationPatient"):
if dcmdata.ImageOrientationPatient == [1, 0, 0, 0, 1, 0]:
slice_location = dcmdata.ImagePositionPatient[2]
else:
logger.warning("Unknown ImageOrientationPatient")
if slice_location is None:
logger.warning("Problem with slice location")
return slice_location | python | def get_slice_location(dcmdata, teil=None):
""" get location of the slice
:param dcmdata: dicom data structure
:param teil: filename. Used when slice location doesnt exist
:return:
"""
slice_location = None
if hasattr(dcmdata, 'SliceLocation'):
# print(dcmdata.SliceLocation)
# print(type(dcmdata.SliceLocation))
try:
slice_location = float(dcmdata.SliceLocation)
except Exception as exc:
logger.info("It is not possible to use SliceLocation")
logger.debug(traceback.format_exc())
if slice_location is None and hasattr(dcmdata, "SliceThickness") and teil is not None:
logger.debug(
"Estimating SliceLocation wiht image number and SliceThickness"
)
# from builtins import map
i = list(map(int, re.findall('\d+', teil)))
i = i[-1]
try:
slice_location = float(i * float(dcmdata.SliceThickness))
except ValueError as e:
print(type(dcmdata.SliceThickness))
print(dcmdata.SliceThickness)
logger.debug(traceback.format_exc())
logger.debug("SliceThickness problem")
if slice_location is None and hasattr(dcmdata, "ImagePositionPatient") and hasattr(dcmdata,
"ImageOrientationPatient"):
if dcmdata.ImageOrientationPatient == [1, 0, 0, 0, 1, 0]:
slice_location = dcmdata.ImagePositionPatient[2]
else:
logger.warning("Unknown ImageOrientationPatient")
if slice_location is None:
logger.warning("Problem with slice location")
return slice_location | [
"def",
"get_slice_location",
"(",
"dcmdata",
",",
"teil",
"=",
"None",
")",
":",
"slice_location",
"=",
"None",
"if",
"hasattr",
"(",
"dcmdata",
",",
"'SliceLocation'",
")",
":",
"# print(dcmdata.SliceLocation)",
"# print(type(dcmdata.SliceLocation))",
"try",
":",
"... | get location of the slice
:param dcmdata: dicom data structure
:param teil: filename. Used when slice location doesnt exist
:return: | [
"get",
"location",
"of",
"the",
"slice"
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L877-L918 | train | 51,306 |
mjirik/io3d | io3d/dcmreaddata.py | DicomReader.get_overlay | def get_overlay(self):
"""
Function make 3D data from dicom file slices. There are usualy
more overlays in the data.
"""
overlay = {}
dcmlist = self.files_in_serie
for i in range(len(dcmlist)):
onefile = dcmlist[i]
logger.info("reading '%s'" % onefile)
data = self._read_file(onefile)
if len(overlay) == 0:
# first there is created dictionary with
# avalible overlay indexes
for i_overlay in range(0, 50):
try:
# overlay index
data2d = decode_overlay_slice(data, i_overlay)
# mport pdb; pdb.set_trace()
shp2 = data2d.shape
overlay[i_overlay] = np.zeros([len(dcmlist), shp2[0],
shp2[1]], dtype=np.int8)
overlay[i_overlay][-i - 1, :, :] = data2d
except Exception:
# exception is exceptetd. We are trying numbers 0-50
# logger.exception('Problem with overlay image number ' +
# str(i_overlay))
pass
else:
for i_overlay in overlay.keys():
try:
data2d = decode_overlay_slice(data, i_overlay)
overlay[i_overlay][-i - 1, :, :] = data2d
except Exception:
logger.warning('Problem with overlay number ' +
str(i_overlay))
return overlay | python | def get_overlay(self):
"""
Function make 3D data from dicom file slices. There are usualy
more overlays in the data.
"""
overlay = {}
dcmlist = self.files_in_serie
for i in range(len(dcmlist)):
onefile = dcmlist[i]
logger.info("reading '%s'" % onefile)
data = self._read_file(onefile)
if len(overlay) == 0:
# first there is created dictionary with
# avalible overlay indexes
for i_overlay in range(0, 50):
try:
# overlay index
data2d = decode_overlay_slice(data, i_overlay)
# mport pdb; pdb.set_trace()
shp2 = data2d.shape
overlay[i_overlay] = np.zeros([len(dcmlist), shp2[0],
shp2[1]], dtype=np.int8)
overlay[i_overlay][-i - 1, :, :] = data2d
except Exception:
# exception is exceptetd. We are trying numbers 0-50
# logger.exception('Problem with overlay image number ' +
# str(i_overlay))
pass
else:
for i_overlay in overlay.keys():
try:
data2d = decode_overlay_slice(data, i_overlay)
overlay[i_overlay][-i - 1, :, :] = data2d
except Exception:
logger.warning('Problem with overlay number ' +
str(i_overlay))
return overlay | [
"def",
"get_overlay",
"(",
"self",
")",
":",
"overlay",
"=",
"{",
"}",
"dcmlist",
"=",
"self",
".",
"files_in_serie",
"for",
"i",
"in",
"range",
"(",
"len",
"(",
"dcmlist",
")",
")",
":",
"onefile",
"=",
"dcmlist",
"[",
"i",
"]",
"logger",
".",
"in... | Function make 3D data from dicom file slices. There are usualy
more overlays in the data. | [
"Function",
"make",
"3D",
"data",
"from",
"dicom",
"file",
"slices",
".",
"There",
"are",
"usualy",
"more",
"overlays",
"in",
"the",
"data",
"."
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L202-L243 | train | 51,307 |
mjirik/io3d | io3d/dcmreaddata.py | DicomDirectory.get_stats_of_series_in_dir | def get_stats_of_series_in_dir(self, study_id=None):
""" Dicom series staticstics, input is dcmdir, not dirpath
Information is generated from dicomdir.pkl and first files of series
"""
if study_id is not None:
logger.error("study_id tag is not implemented yet")
return
import numpy as np
dcmdir = self.files_with_info
# get series number
# vytvoření slovníku, kde je klíčem číslo série a hodnotou jsou všechny
# informace z dicomdir
series_info = {line['SeriesNumber']: line for line in dcmdir}
# počítání velikosti série
try:
dcmdirseries = [line['SeriesNumber'] for line in dcmdir]
except:
logger.debug('Dicom tag SeriesNumber not found')
series_info = {0: {'Count': 0}}
return series_info
# eturn [0],[0]
bins, counts = np.unique(dcmdirseries, return_counts=True)
# sestavení informace o velikosti série a slovníku
for i in range(0, len(bins)):
series_info[bins[i]]['Count'] = counts[i]
# adding information from files
lst = self.get_sorted_series_files(series_number=bins[i])
metadata = self.get_metaData(dcmlist=lst, series_number=bins[i])
# adding dictionary metadata to series_info dictionary
series_info[bins[i]] = dict(
list(series_info[bins[i]].items()) +
list(metadata.items())
)
return series_info | python | def get_stats_of_series_in_dir(self, study_id=None):
""" Dicom series staticstics, input is dcmdir, not dirpath
Information is generated from dicomdir.pkl and first files of series
"""
if study_id is not None:
logger.error("study_id tag is not implemented yet")
return
import numpy as np
dcmdir = self.files_with_info
# get series number
# vytvoření slovníku, kde je klíčem číslo série a hodnotou jsou všechny
# informace z dicomdir
series_info = {line['SeriesNumber']: line for line in dcmdir}
# počítání velikosti série
try:
dcmdirseries = [line['SeriesNumber'] for line in dcmdir]
except:
logger.debug('Dicom tag SeriesNumber not found')
series_info = {0: {'Count': 0}}
return series_info
# eturn [0],[0]
bins, counts = np.unique(dcmdirseries, return_counts=True)
# sestavení informace o velikosti série a slovníku
for i in range(0, len(bins)):
series_info[bins[i]]['Count'] = counts[i]
# adding information from files
lst = self.get_sorted_series_files(series_number=bins[i])
metadata = self.get_metaData(dcmlist=lst, series_number=bins[i])
# adding dictionary metadata to series_info dictionary
series_info[bins[i]] = dict(
list(series_info[bins[i]].items()) +
list(metadata.items())
)
return series_info | [
"def",
"get_stats_of_series_in_dir",
"(",
"self",
",",
"study_id",
"=",
"None",
")",
":",
"if",
"study_id",
"is",
"not",
"None",
":",
"logger",
".",
"error",
"(",
"\"study_id tag is not implemented yet\"",
")",
"return",
"import",
"numpy",
"as",
"np",
"dcmdir",
... | Dicom series staticstics, input is dcmdir, not dirpath
Information is generated from dicomdir.pkl and first files of series | [
"Dicom",
"series",
"staticstics",
"input",
"is",
"dcmdir",
"not",
"dirpath",
"Information",
"is",
"generated",
"from",
"dicomdir",
".",
"pkl",
"and",
"first",
"files",
"of",
"series"
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L603-L643 | train | 51,308 |
mjirik/io3d | io3d/dcmreaddata.py | DicomDirectory.print_series_info | def print_series_info(self, series_info, minimal_series_number=1):
"""
Print series_info from dcmdirstats
"""
strinfo = ''
if len(series_info) > minimal_series_number:
for serie_number in series_info.keys():
strl = get_one_serie_info(series_info, serie_number)
strinfo = strinfo + strl + '\n'
# rint strl
return strinfo | python | def print_series_info(self, series_info, minimal_series_number=1):
"""
Print series_info from dcmdirstats
"""
strinfo = ''
if len(series_info) > minimal_series_number:
for serie_number in series_info.keys():
strl = get_one_serie_info(series_info, serie_number)
strinfo = strinfo + strl + '\n'
# rint strl
return strinfo | [
"def",
"print_series_info",
"(",
"self",
",",
"series_info",
",",
"minimal_series_number",
"=",
"1",
")",
":",
"strinfo",
"=",
"''",
"if",
"len",
"(",
"series_info",
")",
">",
"minimal_series_number",
":",
"for",
"serie_number",
"in",
"series_info",
".",
"keys... | Print series_info from dcmdirstats | [
"Print",
"series_info",
"from",
"dcmdirstats"
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L645-L656 | train | 51,309 |
mjirik/io3d | io3d/dcmreaddata.py | DicomDirectory.__prepare_info_from_dicomdir_file | def __prepare_info_from_dicomdir_file(self, writedicomdirfile=True):
"""
Check if exists dicomdir file and load it or cerate it
dcmdir = get_dir(dirpath)
dcmdir: list with filenames, SeriesNumber and SliceLocation
"""
createdcmdir = True
dicomdirfile = os.path.join(self.dirpath, self.dicomdir_filename)
ftype = 'pickle'
# if exist dicomdir file and is in correct version, use it
if os.path.exists(dicomdirfile):
try:
dcmdirplus = misc.obj_from_file(dicomdirfile, ftype)
if dcmdirplus['version'] == __version__:
createdcmdir = False
dcmdir = dcmdirplus['filesinfo']
except Exception:
logger.debug('Found dicomdir.pkl with wrong version')
createdcmdir = True
if createdcmdir or self.force_create_dicomdir:
dcmdirplus = self._create_dicomdir_info()
dcmdir = dcmdirplus['filesinfo']
if (writedicomdirfile) and len(dcmdir) > 0:
# obj_to_file(dcmdirplus, dicomdirfile, ftype)
try:
misc.obj_to_file(dcmdirplus, dicomdirfile, ftype)
except:
logger.warning('Cannot write dcmdir file')
traceback.print_exc()
# bj_to_file(dcmdir, dcmdiryamlpath )
dcmdir = dcmdirplus['filesinfo']
self.dcmdirplus = dcmdirplus
self.files_with_info = dcmdir
return dcmdir | python | def __prepare_info_from_dicomdir_file(self, writedicomdirfile=True):
"""
Check if exists dicomdir file and load it or cerate it
dcmdir = get_dir(dirpath)
dcmdir: list with filenames, SeriesNumber and SliceLocation
"""
createdcmdir = True
dicomdirfile = os.path.join(self.dirpath, self.dicomdir_filename)
ftype = 'pickle'
# if exist dicomdir file and is in correct version, use it
if os.path.exists(dicomdirfile):
try:
dcmdirplus = misc.obj_from_file(dicomdirfile, ftype)
if dcmdirplus['version'] == __version__:
createdcmdir = False
dcmdir = dcmdirplus['filesinfo']
except Exception:
logger.debug('Found dicomdir.pkl with wrong version')
createdcmdir = True
if createdcmdir or self.force_create_dicomdir:
dcmdirplus = self._create_dicomdir_info()
dcmdir = dcmdirplus['filesinfo']
if (writedicomdirfile) and len(dcmdir) > 0:
# obj_to_file(dcmdirplus, dicomdirfile, ftype)
try:
misc.obj_to_file(dcmdirplus, dicomdirfile, ftype)
except:
logger.warning('Cannot write dcmdir file')
traceback.print_exc()
# bj_to_file(dcmdir, dcmdiryamlpath )
dcmdir = dcmdirplus['filesinfo']
self.dcmdirplus = dcmdirplus
self.files_with_info = dcmdir
return dcmdir | [
"def",
"__prepare_info_from_dicomdir_file",
"(",
"self",
",",
"writedicomdirfile",
"=",
"True",
")",
":",
"createdcmdir",
"=",
"True",
"dicomdirfile",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"dirpath",
",",
"self",
".",
"dicomdir_filename",
")",... | Check if exists dicomdir file and load it or cerate it
dcmdir = get_dir(dirpath)
dcmdir: list with filenames, SeriesNumber and SliceLocation | [
"Check",
"if",
"exists",
"dicomdir",
"file",
"and",
"load",
"it",
"or",
"cerate",
"it"
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L679-L718 | train | 51,310 |
mjirik/io3d | io3d/dcmreaddata.py | DicomDirectory.series_in_dir | def series_in_dir(self):
"""input is dcmdir, not dirpath """
# none_count = 0
countsd = {}
# dcmdirseries = []
for line in self.files_with_info:
if "SeriesNumber" in line:
sn = line['SeriesNumber']
else:
sn = None
if sn in countsd:
countsd[sn] += 1
else:
countsd[sn] = 1
bins = list(countsd)
counts = list(countsd.values())
# try:
# dcmdirseries = [line['SeriesNumber'] for line in self.files_with_info]
# except:
# return [0], [0]
# bins, counts = np.unique(dcmdirseries, return_counts=True)
# binslist = bins.tolist()
# if None in binslist:
# if len(binslist) == 1:
# return [0], [0]
# else:
# logger.warning
# kvůli správným intervalům mezi biny je nutno jeden přidat na konce
# mxb = np.max(bins)
# if mxb is None:
# mxb = 1
# else:
# mxb = mxb + 1
#
# binslist.append(mxb)
# counts, binsvyhodit = np.histogram(dcmdirseries, bins=binslist)
# return counts.tolist(), bins.tolist()
return counts, bins | python | def series_in_dir(self):
"""input is dcmdir, not dirpath """
# none_count = 0
countsd = {}
# dcmdirseries = []
for line in self.files_with_info:
if "SeriesNumber" in line:
sn = line['SeriesNumber']
else:
sn = None
if sn in countsd:
countsd[sn] += 1
else:
countsd[sn] = 1
bins = list(countsd)
counts = list(countsd.values())
# try:
# dcmdirseries = [line['SeriesNumber'] for line in self.files_with_info]
# except:
# return [0], [0]
# bins, counts = np.unique(dcmdirseries, return_counts=True)
# binslist = bins.tolist()
# if None in binslist:
# if len(binslist) == 1:
# return [0], [0]
# else:
# logger.warning
# kvůli správným intervalům mezi biny je nutno jeden přidat na konce
# mxb = np.max(bins)
# if mxb is None:
# mxb = 1
# else:
# mxb = mxb + 1
#
# binslist.append(mxb)
# counts, binsvyhodit = np.histogram(dcmdirseries, bins=binslist)
# return counts.tolist(), bins.tolist()
return counts, bins | [
"def",
"series_in_dir",
"(",
"self",
")",
":",
"# none_count = 0",
"countsd",
"=",
"{",
"}",
"# dcmdirseries = []",
"for",
"line",
"in",
"self",
".",
"files_with_info",
":",
"if",
"\"SeriesNumber\"",
"in",
"line",
":",
"sn",
"=",
"line",
"[",
"'SeriesNumber'",... | input is dcmdir, not dirpath | [
"input",
"is",
"dcmdir",
"not",
"dirpath"
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L720-L765 | train | 51,311 |
mjirik/io3d | io3d/dcmreaddata.py | DicomDirectory.get_sorted_series_files | def get_sorted_series_files(self, startpath="", series_number=None, return_files_with_info=False,
sort_keys="SliceLocation", return_files=True, remove_doubled_slice_locations=True):
"""
Function returns sorted list of dicom files. File paths are organized
by SeriesUID, StudyUID and FrameUID
:param startpath: path prefix. E.g. "~/data"
:param series_number: ID of series used for filtering the data
:param return_files_with_info: return more complex information about sorted files
:param return_files: return simple list of sorted files
:type sort_keys: One key or list of keys used for sorting method by the order of keys.
"""
dcmdir = self.files_with_info[:]
# select sublist with SeriesNumber
if series_number is not None:
dcmdir = [
line for line in dcmdir if line['SeriesNumber'] == series_number
]
dcmdir = sort_list_of_dicts(dcmdir, keys=sort_keys)
logger.debug('SeriesNumber: ' + str(series_number))
if remove_doubled_slice_locations:
dcmdir = self._remove_doubled_slice_locations(dcmdir)
filelist = []
for onefile in dcmdir:
filelist.append(os.path.join(startpath,
self.dirpath, onefile['filename']))
# head, tail = os.path.split(onefile['filename'])
retval = []
if return_files:
retval.append(filelist)
if return_files_with_info:
retval.append(dcmdir)
if len(retval) == 0:
retval = None
elif len(retval) == 1:
retval = retval[0]
else:
retval = tuple(retval)
return retval | python | def get_sorted_series_files(self, startpath="", series_number=None, return_files_with_info=False,
sort_keys="SliceLocation", return_files=True, remove_doubled_slice_locations=True):
"""
Function returns sorted list of dicom files. File paths are organized
by SeriesUID, StudyUID and FrameUID
:param startpath: path prefix. E.g. "~/data"
:param series_number: ID of series used for filtering the data
:param return_files_with_info: return more complex information about sorted files
:param return_files: return simple list of sorted files
:type sort_keys: One key or list of keys used for sorting method by the order of keys.
"""
dcmdir = self.files_with_info[:]
# select sublist with SeriesNumber
if series_number is not None:
dcmdir = [
line for line in dcmdir if line['SeriesNumber'] == series_number
]
dcmdir = sort_list_of_dicts(dcmdir, keys=sort_keys)
logger.debug('SeriesNumber: ' + str(series_number))
if remove_doubled_slice_locations:
dcmdir = self._remove_doubled_slice_locations(dcmdir)
filelist = []
for onefile in dcmdir:
filelist.append(os.path.join(startpath,
self.dirpath, onefile['filename']))
# head, tail = os.path.split(onefile['filename'])
retval = []
if return_files:
retval.append(filelist)
if return_files_with_info:
retval.append(dcmdir)
if len(retval) == 0:
retval = None
elif len(retval) == 1:
retval = retval[0]
else:
retval = tuple(retval)
return retval | [
"def",
"get_sorted_series_files",
"(",
"self",
",",
"startpath",
"=",
"\"\"",
",",
"series_number",
"=",
"None",
",",
"return_files_with_info",
"=",
"False",
",",
"sort_keys",
"=",
"\"SliceLocation\"",
",",
"return_files",
"=",
"True",
",",
"remove_doubled_slice_loc... | Function returns sorted list of dicom files. File paths are organized
by SeriesUID, StudyUID and FrameUID
:param startpath: path prefix. E.g. "~/data"
:param series_number: ID of series used for filtering the data
:param return_files_with_info: return more complex information about sorted files
:param return_files: return simple list of sorted files
:type sort_keys: One key or list of keys used for sorting method by the order of keys. | [
"Function",
"returns",
"sorted",
"list",
"of",
"dicom",
"files",
".",
"File",
"paths",
"are",
"organized",
"by",
"SeriesUID",
"StudyUID",
"and",
"FrameUID"
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L767-L815 | train | 51,312 |
mjirik/io3d | io3d/dcmreaddata.py | DicomDirectory._create_dicomdir_info | def _create_dicomdir_info(self):
"""
Function crates list of all files in dicom dir with all IDs
"""
filelist = files_in_dir(self.dirpath)
files = []
metadataline = {}
for filepath in filelist:
head, teil = os.path.split(filepath)
dcmdata = None
if os.path.isdir(filepath):
logger.debug("Subdirectory found in series dir is ignored: " + str(filepath))
continue
try:
dcmdata = pydicom.read_file(filepath)
except pydicom.errors.InvalidDicomError as e:
# some files doesnt have DICM marker
try:
dcmdata = pydicom.read_file(filepath, force=self.force_read)
# if e.[0].startswith("File is missing \\'DICM\\' marker. Use force=True to force reading")
except Exception as e:
if teil != self.dicomdir_filename:
# print('Dicom read problem with file ' + filepath)
logger.info('Dicom read problem with file ' + filepath)
import traceback
logger.debug(traceback.format_exc())
if hasattr(dcmdata, "DirectoryRecordSequence"):
# file is DICOMDIR - metainfo about files in directory
# we are not using this info
dcmdata = None
if dcmdata is not None:
metadataline = _prepare_metadata_line(dcmdata, teil)
files.append(metadataline)
# if SliceLocation is None, it is sorted to the end
# this is not necessary it can be deleted
files.sort(key=lambda x: (x['SliceLocation'] is None, x["SliceLocation"]))
dcmdirplus = {'version': __version__, 'filesinfo': files, }
if "StudyDate" in metadataline:
dcmdirplus["StudyDate"] = metadataline["StudyDate"]
return dcmdirplus | python | def _create_dicomdir_info(self):
"""
Function crates list of all files in dicom dir with all IDs
"""
filelist = files_in_dir(self.dirpath)
files = []
metadataline = {}
for filepath in filelist:
head, teil = os.path.split(filepath)
dcmdata = None
if os.path.isdir(filepath):
logger.debug("Subdirectory found in series dir is ignored: " + str(filepath))
continue
try:
dcmdata = pydicom.read_file(filepath)
except pydicom.errors.InvalidDicomError as e:
# some files doesnt have DICM marker
try:
dcmdata = pydicom.read_file(filepath, force=self.force_read)
# if e.[0].startswith("File is missing \\'DICM\\' marker. Use force=True to force reading")
except Exception as e:
if teil != self.dicomdir_filename:
# print('Dicom read problem with file ' + filepath)
logger.info('Dicom read problem with file ' + filepath)
import traceback
logger.debug(traceback.format_exc())
if hasattr(dcmdata, "DirectoryRecordSequence"):
# file is DICOMDIR - metainfo about files in directory
# we are not using this info
dcmdata = None
if dcmdata is not None:
metadataline = _prepare_metadata_line(dcmdata, teil)
files.append(metadataline)
# if SliceLocation is None, it is sorted to the end
# this is not necessary it can be deleted
files.sort(key=lambda x: (x['SliceLocation'] is None, x["SliceLocation"]))
dcmdirplus = {'version': __version__, 'filesinfo': files, }
if "StudyDate" in metadataline:
dcmdirplus["StudyDate"] = metadataline["StudyDate"]
return dcmdirplus | [
"def",
"_create_dicomdir_info",
"(",
"self",
")",
":",
"filelist",
"=",
"files_in_dir",
"(",
"self",
".",
"dirpath",
")",
"files",
"=",
"[",
"]",
"metadataline",
"=",
"{",
"}",
"for",
"filepath",
"in",
"filelist",
":",
"head",
",",
"teil",
"=",
"os",
"... | Function crates list of all files in dicom dir with all IDs | [
"Function",
"crates",
"list",
"of",
"all",
"files",
"in",
"dicom",
"dir",
"with",
"all",
"IDs"
] | ccaf3e378dcc967f2565d477fc27583fd0f61fcc | https://github.com/mjirik/io3d/blob/ccaf3e378dcc967f2565d477fc27583fd0f61fcc/io3d/dcmreaddata.py#L828-L874 | train | 51,313 |
ClericPy/torequests | torequests/parsers.py | SimpleParser.python_parser | def python_parser(self, obj, *args):
"""operate a python obj"""
attr, args = args[0], args[1:]
item = getattr(obj, attr)
if callable(item):
item = item(*args)
return [item] | python | def python_parser(self, obj, *args):
"""operate a python obj"""
attr, args = args[0], args[1:]
item = getattr(obj, attr)
if callable(item):
item = item(*args)
return [item] | [
"def",
"python_parser",
"(",
"self",
",",
"obj",
",",
"*",
"args",
")",
":",
"attr",
",",
"args",
"=",
"args",
"[",
"0",
"]",
",",
"args",
"[",
"1",
":",
"]",
"item",
"=",
"getattr",
"(",
"obj",
",",
"attr",
")",
"if",
"callable",
"(",
"item",
... | operate a python obj | [
"operate",
"a",
"python",
"obj"
] | 1793261688d7a47e1c3a0830d83f8552f5e3e5d9 | https://github.com/ClericPy/torequests/blob/1793261688d7a47e1c3a0830d83f8552f5e3e5d9/torequests/parsers.py#L194-L200 | train | 51,314 |
jurismarches/chopper | chopper/css/extractor.py | CSSExtractor.parse | def parse(self):
"""
Parses the CSS contents and returns the cleaned CSS as a string
:returns: The cleaned CSS
:rtype: str
"""
# Build the HTML tree
self.tree = self._build_tree(self.html_contents)
# Parse the CSS contents
self.stylesheet = self.parser.parse_stylesheet(self.css_contents)
# Get the cleaned CSS contents
self.cleaned_css = self._clean_css() | python | def parse(self):
"""
Parses the CSS contents and returns the cleaned CSS as a string
:returns: The cleaned CSS
:rtype: str
"""
# Build the HTML tree
self.tree = self._build_tree(self.html_contents)
# Parse the CSS contents
self.stylesheet = self.parser.parse_stylesheet(self.css_contents)
# Get the cleaned CSS contents
self.cleaned_css = self._clean_css() | [
"def",
"parse",
"(",
"self",
")",
":",
"# Build the HTML tree",
"self",
".",
"tree",
"=",
"self",
".",
"_build_tree",
"(",
"self",
".",
"html_contents",
")",
"# Parse the CSS contents",
"self",
".",
"stylesheet",
"=",
"self",
".",
"parser",
".",
"parse_stylesh... | Parses the CSS contents and returns the cleaned CSS as a string
:returns: The cleaned CSS
:rtype: str | [
"Parses",
"the",
"CSS",
"contents",
"and",
"returns",
"the",
"cleaned",
"CSS",
"as",
"a",
"string"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/css/extractor.py#L42-L56 | train | 51,315 |
jurismarches/chopper | chopper/css/extractor.py | CSSExtractor.rel_to_abs | def rel_to_abs(self, base_url):
"""
Converts relative links from css contents to absolute links
:param base_url: The base page url to use for building absolute links
:type base_url: str
:param css_contents: The CSS contents to parse
:type css_contents: str
"""
self.cleaned_css = self.rel_to_abs_re.sub(
lambda match: "url('%s')" % urljoin(
base_url, match.group('path').strip('\'"')),
self.cleaned_css) | python | def rel_to_abs(self, base_url):
"""
Converts relative links from css contents to absolute links
:param base_url: The base page url to use for building absolute links
:type base_url: str
:param css_contents: The CSS contents to parse
:type css_contents: str
"""
self.cleaned_css = self.rel_to_abs_re.sub(
lambda match: "url('%s')" % urljoin(
base_url, match.group('path').strip('\'"')),
self.cleaned_css) | [
"def",
"rel_to_abs",
"(",
"self",
",",
"base_url",
")",
":",
"self",
".",
"cleaned_css",
"=",
"self",
".",
"rel_to_abs_re",
".",
"sub",
"(",
"lambda",
"match",
":",
"\"url('%s')\"",
"%",
"urljoin",
"(",
"base_url",
",",
"match",
".",
"group",
"(",
"'path... | Converts relative links from css contents to absolute links
:param base_url: The base page url to use for building absolute links
:type base_url: str
:param css_contents: The CSS contents to parse
:type css_contents: str | [
"Converts",
"relative",
"links",
"from",
"css",
"contents",
"to",
"absolute",
"links"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/css/extractor.py#L58-L70 | train | 51,316 |
jurismarches/chopper | chopper/css/extractor.py | CSSExtractor._clean_css | def _clean_css(self):
"""
Returns the cleaned CSS
:param stylesheet: The Stylesheet object to parse
:type stylesheet: tinycss.css21.Stylesheet
"""
# Init the cleaned CSS rules and contents string
css_rules = []
# For every rule in the CSS
for rule in self.stylesheet.rules:
try:
# Clean the CSS rule
cleaned_rule = self._clean_rule(rule)
# Append the rule to matched CSS rules
if cleaned_rule is not None:
css_rules.append(cleaned_rule)
except:
# On error, assume the rule matched the tree
css_rules.append(rule)
return self._build_css(css_rules) | python | def _clean_css(self):
"""
Returns the cleaned CSS
:param stylesheet: The Stylesheet object to parse
:type stylesheet: tinycss.css21.Stylesheet
"""
# Init the cleaned CSS rules and contents string
css_rules = []
# For every rule in the CSS
for rule in self.stylesheet.rules:
try:
# Clean the CSS rule
cleaned_rule = self._clean_rule(rule)
# Append the rule to matched CSS rules
if cleaned_rule is not None:
css_rules.append(cleaned_rule)
except:
# On error, assume the rule matched the tree
css_rules.append(rule)
return self._build_css(css_rules) | [
"def",
"_clean_css",
"(",
"self",
")",
":",
"# Init the cleaned CSS rules and contents string",
"css_rules",
"=",
"[",
"]",
"# For every rule in the CSS",
"for",
"rule",
"in",
"self",
".",
"stylesheet",
".",
"rules",
":",
"try",
":",
"# Clean the CSS rule",
"cleaned_r... | Returns the cleaned CSS
:param stylesheet: The Stylesheet object to parse
:type stylesheet: tinycss.css21.Stylesheet | [
"Returns",
"the",
"cleaned",
"CSS"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/css/extractor.py#L85-L110 | train | 51,317 |
jurismarches/chopper | chopper/css/extractor.py | CSSExtractor._clean_rule | def _clean_rule(self, rule):
"""
Cleans a css Rule by removing Selectors without matches on the tree
Returns None if the whole rule do not match
:param rule: CSS Rule to check
:type rule: A tinycss Rule object
:returns: A cleaned tinycss Rule with only Selectors matching the tree or None
:rtype: tinycss Rule or None
"""
# Always match @ rules
if rule.at_keyword is not None:
return rule
# Clean selectors
cleaned_token_list = []
for token_list in split_on_comma(rule.selector):
# If the token list matches the tree
if self._token_list_matches_tree(token_list):
# Add a Comma if multiple token lists matched
if len(cleaned_token_list) > 0:
cleaned_token_list.append(
cssselect.parser.Token('DELIM', ',', len(cleaned_token_list) + 1))
# Append it to the list of cleaned token list
cleaned_token_list += token_list
# Return None if selectors list is empty
if not cleaned_token_list:
return None
# Update rule token list
rule.selector = cleaned_token_list
# Return cleaned rule
return rule | python | def _clean_rule(self, rule):
"""
Cleans a css Rule by removing Selectors without matches on the tree
Returns None if the whole rule do not match
:param rule: CSS Rule to check
:type rule: A tinycss Rule object
:returns: A cleaned tinycss Rule with only Selectors matching the tree or None
:rtype: tinycss Rule or None
"""
# Always match @ rules
if rule.at_keyword is not None:
return rule
# Clean selectors
cleaned_token_list = []
for token_list in split_on_comma(rule.selector):
# If the token list matches the tree
if self._token_list_matches_tree(token_list):
# Add a Comma if multiple token lists matched
if len(cleaned_token_list) > 0:
cleaned_token_list.append(
cssselect.parser.Token('DELIM', ',', len(cleaned_token_list) + 1))
# Append it to the list of cleaned token list
cleaned_token_list += token_list
# Return None if selectors list is empty
if not cleaned_token_list:
return None
# Update rule token list
rule.selector = cleaned_token_list
# Return cleaned rule
return rule | [
"def",
"_clean_rule",
"(",
"self",
",",
"rule",
")",
":",
"# Always match @ rules",
"if",
"rule",
".",
"at_keyword",
"is",
"not",
"None",
":",
"return",
"rule",
"# Clean selectors",
"cleaned_token_list",
"=",
"[",
"]",
"for",
"token_list",
"in",
"split_on_comma"... | Cleans a css Rule by removing Selectors without matches on the tree
Returns None if the whole rule do not match
:param rule: CSS Rule to check
:type rule: A tinycss Rule object
:returns: A cleaned tinycss Rule with only Selectors matching the tree or None
:rtype: tinycss Rule or None | [
"Cleans",
"a",
"css",
"Rule",
"by",
"removing",
"Selectors",
"without",
"matches",
"on",
"the",
"tree",
"Returns",
"None",
"if",
"the",
"whole",
"rule",
"do",
"not",
"match"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/css/extractor.py#L112-L150 | train | 51,318 |
jurismarches/chopper | chopper/css/extractor.py | CSSExtractor._token_list_matches_tree | def _token_list_matches_tree(self, token_list):
"""
Returns whether the token list matches the HTML tree
:param selector: A Token list to check
:type selector: list of Token objects
:returns: True if the token list has matches in self.tree
:rtype: bool
"""
try:
parsed_selector = cssselect.parse(
''.join(token.as_css() for token in token_list))[0]
return bool(
self.tree.xpath(
self.xpath_translator.selector_to_xpath(parsed_selector)))
except:
# On error, assume the selector matches the tree
return True | python | def _token_list_matches_tree(self, token_list):
"""
Returns whether the token list matches the HTML tree
:param selector: A Token list to check
:type selector: list of Token objects
:returns: True if the token list has matches in self.tree
:rtype: bool
"""
try:
parsed_selector = cssselect.parse(
''.join(token.as_css() for token in token_list))[0]
return bool(
self.tree.xpath(
self.xpath_translator.selector_to_xpath(parsed_selector)))
except:
# On error, assume the selector matches the tree
return True | [
"def",
"_token_list_matches_tree",
"(",
"self",
",",
"token_list",
")",
":",
"try",
":",
"parsed_selector",
"=",
"cssselect",
".",
"parse",
"(",
"''",
".",
"join",
"(",
"token",
".",
"as_css",
"(",
")",
"for",
"token",
"in",
"token_list",
")",
")",
"[",
... | Returns whether the token list matches the HTML tree
:param selector: A Token list to check
:type selector: list of Token objects
:returns: True if the token list has matches in self.tree
:rtype: bool | [
"Returns",
"whether",
"the",
"token",
"list",
"matches",
"the",
"HTML",
"tree"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/css/extractor.py#L152-L170 | train | 51,319 |
jurismarches/chopper | chopper/css/extractor.py | CSSExtractor._rule_as_string | def _rule_as_string(self, rule):
"""
Converts a tinycss rule to a formatted CSS string
:param rule: The rule to format
:type rule: tinycss Rule object
:returns: The Rule as a CSS string
:rtype: str
"""
if isinstance(rule, RuleSet):
# Simple CSS rule : a { color: red; }
return '%s{%s}' % (
self._selector_as_string(rule.selector),
self._declarations_as_string(rule.declarations))
elif isinstance(rule, ImportRule):
# @import rule
return "@import url('%s') %s;" % (
rule.uri, ','.join(rule.media))
elif isinstance(rule, FontFaceRule):
# @font-face rule
return "@font-face{%s}" % self._declarations_as_string(rule.declarations)
elif isinstance(rule, MediaRule):
# @media rule
return "@media %s{%s}" % (
','.join(rule.media),
''.join(self._rule_as_string(r) for r in rule.rules))
elif isinstance(rule, PageRule):
# @page rule
selector, pseudo = rule.selector
return "@page%s%s{%s}" % (
' %s' % selector if selector else '',
' :%s' % pseudo if pseudo else '',
self._declarations_as_string(rule.declarations))
return '' | python | def _rule_as_string(self, rule):
"""
Converts a tinycss rule to a formatted CSS string
:param rule: The rule to format
:type rule: tinycss Rule object
:returns: The Rule as a CSS string
:rtype: str
"""
if isinstance(rule, RuleSet):
# Simple CSS rule : a { color: red; }
return '%s{%s}' % (
self._selector_as_string(rule.selector),
self._declarations_as_string(rule.declarations))
elif isinstance(rule, ImportRule):
# @import rule
return "@import url('%s') %s;" % (
rule.uri, ','.join(rule.media))
elif isinstance(rule, FontFaceRule):
# @font-face rule
return "@font-face{%s}" % self._declarations_as_string(rule.declarations)
elif isinstance(rule, MediaRule):
# @media rule
return "@media %s{%s}" % (
','.join(rule.media),
''.join(self._rule_as_string(r) for r in rule.rules))
elif isinstance(rule, PageRule):
# @page rule
selector, pseudo = rule.selector
return "@page%s%s{%s}" % (
' %s' % selector if selector else '',
' :%s' % pseudo if pseudo else '',
self._declarations_as_string(rule.declarations))
return '' | [
"def",
"_rule_as_string",
"(",
"self",
",",
"rule",
")",
":",
"if",
"isinstance",
"(",
"rule",
",",
"RuleSet",
")",
":",
"# Simple CSS rule : a { color: red; }",
"return",
"'%s{%s}'",
"%",
"(",
"self",
".",
"_selector_as_string",
"(",
"rule",
".",
"selector",
... | Converts a tinycss rule to a formatted CSS string
:param rule: The rule to format
:type rule: tinycss Rule object
:returns: The Rule as a CSS string
:rtype: str | [
"Converts",
"a",
"tinycss",
"rule",
"to",
"a",
"formatted",
"CSS",
"string"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/css/extractor.py#L184-L223 | train | 51,320 |
jurismarches/chopper | chopper/css/extractor.py | CSSExtractor._selector_as_string | def _selector_as_string(self, selector):
"""
Returns a selector as a CSS string
:param selector: A list of tinycss Tokens
:type selector: list
:returns: The CSS string for the selector
:rtype: str
"""
return ','.join(
''.join(token.as_css() for token in strip_whitespace(token_list))
for token_list in split_on_comma(selector)) | python | def _selector_as_string(self, selector):
"""
Returns a selector as a CSS string
:param selector: A list of tinycss Tokens
:type selector: list
:returns: The CSS string for the selector
:rtype: str
"""
return ','.join(
''.join(token.as_css() for token in strip_whitespace(token_list))
for token_list in split_on_comma(selector)) | [
"def",
"_selector_as_string",
"(",
"self",
",",
"selector",
")",
":",
"return",
"','",
".",
"join",
"(",
"''",
".",
"join",
"(",
"token",
".",
"as_css",
"(",
")",
"for",
"token",
"in",
"strip_whitespace",
"(",
"token_list",
")",
")",
"for",
"token_list",... | Returns a selector as a CSS string
:param selector: A list of tinycss Tokens
:type selector: list
:returns: The CSS string for the selector
:rtype: str | [
"Returns",
"a",
"selector",
"as",
"a",
"CSS",
"string"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/css/extractor.py#L225-L236 | train | 51,321 |
jurismarches/chopper | chopper/css/extractor.py | CSSExtractor._declarations_as_string | def _declarations_as_string(self, declarations):
"""
Returns a list of declarations as a formatted CSS string
:param declarations: The list of tinycss Declarations to format
:type declarations: list of tinycss.css21.Declaration
:returns: The CSS string for the declarations list
:rtype: str
"""
return ''.join('%s:%s%s;' % (
d.name,
d.value.as_css(),
' !' + d.priority if d.priority else '') for d in declarations) | python | def _declarations_as_string(self, declarations):
"""
Returns a list of declarations as a formatted CSS string
:param declarations: The list of tinycss Declarations to format
:type declarations: list of tinycss.css21.Declaration
:returns: The CSS string for the declarations list
:rtype: str
"""
return ''.join('%s:%s%s;' % (
d.name,
d.value.as_css(),
' !' + d.priority if d.priority else '') for d in declarations) | [
"def",
"_declarations_as_string",
"(",
"self",
",",
"declarations",
")",
":",
"return",
"''",
".",
"join",
"(",
"'%s:%s%s;'",
"%",
"(",
"d",
".",
"name",
",",
"d",
".",
"value",
".",
"as_css",
"(",
")",
",",
"' !'",
"+",
"d",
".",
"priority",
"if",
... | Returns a list of declarations as a formatted CSS string
:param declarations: The list of tinycss Declarations to format
:type declarations: list of tinycss.css21.Declaration
:returns: The CSS string for the declarations list
:rtype: str | [
"Returns",
"a",
"list",
"of",
"declarations",
"as",
"a",
"formatted",
"CSS",
"string"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/css/extractor.py#L238-L250 | train | 51,322 |
vsoch/helpme | helpme/utils/format.py | envars_to_markdown | def envars_to_markdown(envars, title = "Environment"):
'''generate a markdown list of a list of environment variable tuples
Parameters
==========
title: A title for the section (defaults to "Environment"
envars: a list of tuples for the environment, e.g.:
[('TERM', 'xterm-256color'),
('SHELL', '/bin/bash'),
('USER', 'vanessa'),
('LD_LIBRARY_PATH', ':/usr/local/pulse')]
'''
markdown = ''
if envars not in [None, '', []]:
markdown += '\n## %s\n' % title
for envar in envars:
markdown += ' - **%s**: %s\n' %(envar[0], envar[1])
return markdown | python | def envars_to_markdown(envars, title = "Environment"):
'''generate a markdown list of a list of environment variable tuples
Parameters
==========
title: A title for the section (defaults to "Environment"
envars: a list of tuples for the environment, e.g.:
[('TERM', 'xterm-256color'),
('SHELL', '/bin/bash'),
('USER', 'vanessa'),
('LD_LIBRARY_PATH', ':/usr/local/pulse')]
'''
markdown = ''
if envars not in [None, '', []]:
markdown += '\n## %s\n' % title
for envar in envars:
markdown += ' - **%s**: %s\n' %(envar[0], envar[1])
return markdown | [
"def",
"envars_to_markdown",
"(",
"envars",
",",
"title",
"=",
"\"Environment\"",
")",
":",
"markdown",
"=",
"''",
"if",
"envars",
"not",
"in",
"[",
"None",
",",
"''",
",",
"[",
"]",
"]",
":",
"markdown",
"+=",
"'\\n## %s\\n'",
"%",
"title",
"for",
"en... | generate a markdown list of a list of environment variable tuples
Parameters
==========
title: A title for the section (defaults to "Environment"
envars: a list of tuples for the environment, e.g.:
[('TERM', 'xterm-256color'),
('SHELL', '/bin/bash'),
('USER', 'vanessa'),
('LD_LIBRARY_PATH', ':/usr/local/pulse')] | [
"generate",
"a",
"markdown",
"list",
"of",
"a",
"list",
"of",
"environment",
"variable",
"tuples"
] | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/utils/format.py#L29-L48 | train | 51,323 |
SetBased/py-stratum | pystratum/application/PyStratumApplication.py | PyStratumApplication.get_default_commands | def get_default_commands(self):
"""
Returns the default commands of this application.
:rtype: list[cleo.Command]
"""
commands = Application.get_default_commands(self)
self.add(ConstantsCommand())
self.add(LoaderCommand())
self.add(PyStratumCommand())
self.add(WrapperCommand())
return commands | python | def get_default_commands(self):
"""
Returns the default commands of this application.
:rtype: list[cleo.Command]
"""
commands = Application.get_default_commands(self)
self.add(ConstantsCommand())
self.add(LoaderCommand())
self.add(PyStratumCommand())
self.add(WrapperCommand())
return commands | [
"def",
"get_default_commands",
"(",
"self",
")",
":",
"commands",
"=",
"Application",
".",
"get_default_commands",
"(",
"self",
")",
"self",
".",
"add",
"(",
"ConstantsCommand",
"(",
")",
")",
"self",
".",
"add",
"(",
"LoaderCommand",
"(",
")",
")",
"self"... | Returns the default commands of this application.
:rtype: list[cleo.Command] | [
"Returns",
"the",
"default",
"commands",
"of",
"this",
"application",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/application/PyStratumApplication.py#L25-L38 | train | 51,324 |
bpannier/simpletr64 | simpletr64/discover.py | Discover.discover | def discover(service="ssdp:all", timeout=1, retries=2, ipAddress="239.255.255.250", port=1900):
"""Discovers UPnP devices in the local network.
Try to discover all devices in the local network which do support UPnP. The discovery process can fail
for various reasons and it is recommended to do at least two discoveries, which you can specify with the
``retries`` parameter.
The default ``service`` parameter tries to address all devices also if you know which kind of service type
you are looking for you should set it as some devices do not respond or respond differently otherwise.
:param service: the service type or list of service types of devices you look for
:type service: str or list[str]
:param float timeout: the socket timeout for each try
:param int retries: how often should be a discovery request send
:param str ipAddress: the multicast ip address to use
:param int port: the port to use
:return: a list of DiscoveryResponse objects or empty if no device was found
:rtype: list[DiscoveryResponse]
Example:
::
results = discover()
for result in results:
print("Host: " + result.locationHost + " Port: " + result.locationPort + " Device definitions: " + \\
result.location)
.. seealso::
:class:`~simpletr64.DiscoveryResponse`, :meth:`~simpletr64.Discover.discoverParticularHost`
"""
socket.setdefaulttimeout(timeout)
messages = []
if isinstance(service, str):
services = [service]
elif isinstance(service, list):
services = service
for service in services:
message = 'M-SEARCH * HTTP/1.1\r\nMX: 5\r\nMAN: "ssdp:discover"\r\nHOST: ' + \
ipAddress + ':' + str(port) + '\r\n'
message += "ST: " + service + "\r\n\r\n"
messages.append(message)
responses = {}
for _ in range(retries):
# setup the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
# noinspection PyAssignmentToLoopOrWithParameter
for _ in range(2):
# send the messages with different service types
for message in messages:
# send message more often to make sure all devices will get it
sock.sendto(message.encode('utf-8'), (ipAddress, port))
while True:
try:
# read the message until timeout
data = sock.recv(1024)
except socket.timeout:
break
else:
# no time out, read the response data and create response object
response = DiscoveryResponse(data)
# filter duplicated responses
responses[response.location] = response
# return a list of all responses
return list(responses.values()) | python | def discover(service="ssdp:all", timeout=1, retries=2, ipAddress="239.255.255.250", port=1900):
"""Discovers UPnP devices in the local network.
Try to discover all devices in the local network which do support UPnP. The discovery process can fail
for various reasons and it is recommended to do at least two discoveries, which you can specify with the
``retries`` parameter.
The default ``service`` parameter tries to address all devices also if you know which kind of service type
you are looking for you should set it as some devices do not respond or respond differently otherwise.
:param service: the service type or list of service types of devices you look for
:type service: str or list[str]
:param float timeout: the socket timeout for each try
:param int retries: how often should be a discovery request send
:param str ipAddress: the multicast ip address to use
:param int port: the port to use
:return: a list of DiscoveryResponse objects or empty if no device was found
:rtype: list[DiscoveryResponse]
Example:
::
results = discover()
for result in results:
print("Host: " + result.locationHost + " Port: " + result.locationPort + " Device definitions: " + \\
result.location)
.. seealso::
:class:`~simpletr64.DiscoveryResponse`, :meth:`~simpletr64.Discover.discoverParticularHost`
"""
socket.setdefaulttimeout(timeout)
messages = []
if isinstance(service, str):
services = [service]
elif isinstance(service, list):
services = service
for service in services:
message = 'M-SEARCH * HTTP/1.1\r\nMX: 5\r\nMAN: "ssdp:discover"\r\nHOST: ' + \
ipAddress + ':' + str(port) + '\r\n'
message += "ST: " + service + "\r\n\r\n"
messages.append(message)
responses = {}
for _ in range(retries):
# setup the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
# noinspection PyAssignmentToLoopOrWithParameter
for _ in range(2):
# send the messages with different service types
for message in messages:
# send message more often to make sure all devices will get it
sock.sendto(message.encode('utf-8'), (ipAddress, port))
while True:
try:
# read the message until timeout
data = sock.recv(1024)
except socket.timeout:
break
else:
# no time out, read the response data and create response object
response = DiscoveryResponse(data)
# filter duplicated responses
responses[response.location] = response
# return a list of all responses
return list(responses.values()) | [
"def",
"discover",
"(",
"service",
"=",
"\"ssdp:all\"",
",",
"timeout",
"=",
"1",
",",
"retries",
"=",
"2",
",",
"ipAddress",
"=",
"\"239.255.255.250\"",
",",
"port",
"=",
"1900",
")",
":",
"socket",
".",
"setdefaulttimeout",
"(",
"timeout",
")",
"messages... | Discovers UPnP devices in the local network.
Try to discover all devices in the local network which do support UPnP. The discovery process can fail
for various reasons and it is recommended to do at least two discoveries, which you can specify with the
``retries`` parameter.
The default ``service`` parameter tries to address all devices also if you know which kind of service type
you are looking for you should set it as some devices do not respond or respond differently otherwise.
:param service: the service type or list of service types of devices you look for
:type service: str or list[str]
:param float timeout: the socket timeout for each try
:param int retries: how often should be a discovery request send
:param str ipAddress: the multicast ip address to use
:param int port: the port to use
:return: a list of DiscoveryResponse objects or empty if no device was found
:rtype: list[DiscoveryResponse]
Example:
::
results = discover()
for result in results:
print("Host: " + result.locationHost + " Port: " + result.locationPort + " Device definitions: " + \\
result.location)
.. seealso::
:class:`~simpletr64.DiscoveryResponse`, :meth:`~simpletr64.Discover.discoverParticularHost` | [
"Discovers",
"UPnP",
"devices",
"in",
"the",
"local",
"network",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/discover.py#L34-L111 | train | 51,325 |
bpannier/simpletr64 | simpletr64/discover.py | Discover.discoverParticularHost | def discoverParticularHost(host, service="ssdp:all", deviceDefinitionURL=None, timeout=1, retries=2,
ipAddress="239.255.255.250", port=1900, proxies=None):
"""Discover a particular host and find the best response.
This tries to find the most specific discovery result for the given host. Only the discovery result contains
the URL to the XML tree which initializes the device definition. If an URL is already known it should be
provided to avoid additional latency for a broader first device discovery.
This method also do some magic to find the best result for the given host as UPnP devices behave sometimes
strangely. This call is costly the result if any should be cached.
:param str host: the host to find
:param service: the service type or list of service types if known to search for
:type service: str or list[str]
:param str deviceDefinitionURL: if provided it is used to skip a first device discovery
:param float timeout: the time to wait for each retry
:param int retries: the amount of times how often the device is tried to discover
:param str ipAddress: the multicast ip address to discover devices
:param int port: the port to discover devices
:param str proxies: proxy definition as defined here:
`Proxy definition <http://docs.python-requests.org/en/latest/user/advanced/#proxies>`_
:return: If the device have been found the response is returned otherwise None
:rtype: DiscoveryResponse
:raises ValueError: if problems with reading or parsing the xml device definition occurs
:raises requests.exceptions.ConnectionError: when the device definitions can not be downloaded
:raises requests.exceptions.ConnectTimeout: when download time out
Example:
::
proxies = {"http": "http://localhost:8888"}
result = discoverParticularHost("192.168.0.1", proxies=proxies)
if result is not None:
print("Host: " + result.locationHost + " Port: " + result.locationPort + " Device definitions: " + \\
result.location)
.. seealso::
:class:`~simpletr64.DiscoveryResponse`, :meth:`~simpletr64.Discover.discover`
"""
# get all IP addresses for the given host
ipResults = socket.getaddrinfo(host, 80)
if len(ipResults) == 0:
return None
ipAddresses = []
# remember all ip addresses for the given host
for ipAdrTupple in ipResults:
ipAddresses.append(ipAdrTupple[4][0])
bestPick = None
services = []
if deviceDefinitionURL is None:
# no xml definition given, so lets search for one
# search for all devices first
discoverResults = Discover.discover(service=service, timeout=timeout, retries=retries,
ipAddress=ipAddress, port=port)
for result in discoverResults:
if result.locationHost in ipAddresses:
# now we found a result for that host, pick the best service type if multiple results for the host
# are found
if Discover.rateServiceTypeInResult(result) > Discover.rateServiceTypeInResult(bestPick):
bestPick = result
# remember all services
if result.service not in services:
services.append(result.service)
if bestPick is None:
return None
else:
# create response with given parameter
bestPick = DiscoveryResponse.create(deviceDefinitionURL, service=service)
# some routers do not advice their TR64 capabilities but their UPnp which is only a subset of actions.
# Try to find out if the given XML definition path will give us a better service type.
# load xml definition
# some devices response differently without a User-Agent
headers = {"User-Agent": "Mozilla/5.0; SimpleTR64-3"}
request = requests.get(bestPick.location, proxies=proxies, headers=headers, timeout=float(timeout))
if request.status_code != 200:
errorStr = DeviceTR64._extractErrorString(request)
raise ValueError('Could not get CPE definitions for "' + bestPick.location + '": ' +
str(request.status_code) + ' - ' + request.reason + " -- " + errorStr)
# parse xml
try:
root = ET.fromstring(request.text.encode('utf-8'))
except Exception as e:
raise ValueError("Could not parse CPE definitions for '" + bestPick.location + "': " + str(e))
# find the first deviceType in the document tree
for element in root.getiterator():
# check if element tag name ends on deviceType, skip XML namespace
if element.tag.lower().endswith("devicetype"):
serviceFound = element.text
# remember the service found if it does not exist yet
if serviceFound not in services:
services.append(serviceFound)
# create a specific service just to check if we found it already
serviceFound = serviceFound.replace("schemas-upnp-org", "dslforum-org")
# test if we already have the best service type then we dont need to do an other discovery request
if serviceFound == bestPick.service:
return bestPick
for service in services:
# we search for the specific device tyoe version as of specified in TR64 protocol.
# some devices returns different results depending on the given service type, so lets be
# very specific
specificService = service.replace("schemas-upnp-org", "dslforum-org")
if specificService not in services:
services.append(specificService)
# we do an other discovery request with more specific service/device type
discoverResultsSpecific = Discover.discover(service=services, timeout=float(timeout), retries=retries,
ipAddress=ipAddress, port=port)
# iterate through all results to find the most specific one
evenBetterPick = None
for specificResult in discoverResultsSpecific:
if specificResult.locationHost in ipAddresses:
if Discover.rateServiceTypeInResult(specificResult) > \
Discover.rateServiceTypeInResult(evenBetterPick):
evenBetterPick = specificResult
if evenBetterPick is not None:
# best we could find
return evenBetterPick
# we found first deviceType tag in the XML structure, no need to go further
break
if deviceDefinitionURL is not None:
# we created our own response, so no result found
return None
# we found only an unspecific result, return it anyway
return bestPick | python | def discoverParticularHost(host, service="ssdp:all", deviceDefinitionURL=None, timeout=1, retries=2,
ipAddress="239.255.255.250", port=1900, proxies=None):
"""Discover a particular host and find the best response.
This tries to find the most specific discovery result for the given host. Only the discovery result contains
the URL to the XML tree which initializes the device definition. If an URL is already known it should be
provided to avoid additional latency for a broader first device discovery.
This method also do some magic to find the best result for the given host as UPnP devices behave sometimes
strangely. This call is costly the result if any should be cached.
:param str host: the host to find
:param service: the service type or list of service types if known to search for
:type service: str or list[str]
:param str deviceDefinitionURL: if provided it is used to skip a first device discovery
:param float timeout: the time to wait for each retry
:param int retries: the amount of times how often the device is tried to discover
:param str ipAddress: the multicast ip address to discover devices
:param int port: the port to discover devices
:param str proxies: proxy definition as defined here:
`Proxy definition <http://docs.python-requests.org/en/latest/user/advanced/#proxies>`_
:return: If the device have been found the response is returned otherwise None
:rtype: DiscoveryResponse
:raises ValueError: if problems with reading or parsing the xml device definition occurs
:raises requests.exceptions.ConnectionError: when the device definitions can not be downloaded
:raises requests.exceptions.ConnectTimeout: when download time out
Example:
::
proxies = {"http": "http://localhost:8888"}
result = discoverParticularHost("192.168.0.1", proxies=proxies)
if result is not None:
print("Host: " + result.locationHost + " Port: " + result.locationPort + " Device definitions: " + \\
result.location)
.. seealso::
:class:`~simpletr64.DiscoveryResponse`, :meth:`~simpletr64.Discover.discover`
"""
# get all IP addresses for the given host
ipResults = socket.getaddrinfo(host, 80)
if len(ipResults) == 0:
return None
ipAddresses = []
# remember all ip addresses for the given host
for ipAdrTupple in ipResults:
ipAddresses.append(ipAdrTupple[4][0])
bestPick = None
services = []
if deviceDefinitionURL is None:
# no xml definition given, so lets search for one
# search for all devices first
discoverResults = Discover.discover(service=service, timeout=timeout, retries=retries,
ipAddress=ipAddress, port=port)
for result in discoverResults:
if result.locationHost in ipAddresses:
# now we found a result for that host, pick the best service type if multiple results for the host
# are found
if Discover.rateServiceTypeInResult(result) > Discover.rateServiceTypeInResult(bestPick):
bestPick = result
# remember all services
if result.service not in services:
services.append(result.service)
if bestPick is None:
return None
else:
# create response with given parameter
bestPick = DiscoveryResponse.create(deviceDefinitionURL, service=service)
# some routers do not advice their TR64 capabilities but their UPnp which is only a subset of actions.
# Try to find out if the given XML definition path will give us a better service type.
# load xml definition
# some devices response differently without a User-Agent
headers = {"User-Agent": "Mozilla/5.0; SimpleTR64-3"}
request = requests.get(bestPick.location, proxies=proxies, headers=headers, timeout=float(timeout))
if request.status_code != 200:
errorStr = DeviceTR64._extractErrorString(request)
raise ValueError('Could not get CPE definitions for "' + bestPick.location + '": ' +
str(request.status_code) + ' - ' + request.reason + " -- " + errorStr)
# parse xml
try:
root = ET.fromstring(request.text.encode('utf-8'))
except Exception as e:
raise ValueError("Could not parse CPE definitions for '" + bestPick.location + "': " + str(e))
# find the first deviceType in the document tree
for element in root.getiterator():
# check if element tag name ends on deviceType, skip XML namespace
if element.tag.lower().endswith("devicetype"):
serviceFound = element.text
# remember the service found if it does not exist yet
if serviceFound not in services:
services.append(serviceFound)
# create a specific service just to check if we found it already
serviceFound = serviceFound.replace("schemas-upnp-org", "dslforum-org")
# test if we already have the best service type then we dont need to do an other discovery request
if serviceFound == bestPick.service:
return bestPick
for service in services:
# we search for the specific device tyoe version as of specified in TR64 protocol.
# some devices returns different results depending on the given service type, so lets be
# very specific
specificService = service.replace("schemas-upnp-org", "dslforum-org")
if specificService not in services:
services.append(specificService)
# we do an other discovery request with more specific service/device type
discoverResultsSpecific = Discover.discover(service=services, timeout=float(timeout), retries=retries,
ipAddress=ipAddress, port=port)
# iterate through all results to find the most specific one
evenBetterPick = None
for specificResult in discoverResultsSpecific:
if specificResult.locationHost in ipAddresses:
if Discover.rateServiceTypeInResult(specificResult) > \
Discover.rateServiceTypeInResult(evenBetterPick):
evenBetterPick = specificResult
if evenBetterPick is not None:
# best we could find
return evenBetterPick
# we found first deviceType tag in the XML structure, no need to go further
break
if deviceDefinitionURL is not None:
# we created our own response, so no result found
return None
# we found only an unspecific result, return it anyway
return bestPick | [
"def",
"discoverParticularHost",
"(",
"host",
",",
"service",
"=",
"\"ssdp:all\"",
",",
"deviceDefinitionURL",
"=",
"None",
",",
"timeout",
"=",
"1",
",",
"retries",
"=",
"2",
",",
"ipAddress",
"=",
"\"239.255.255.250\"",
",",
"port",
"=",
"1900",
",",
"prox... | Discover a particular host and find the best response.
This tries to find the most specific discovery result for the given host. Only the discovery result contains
the URL to the XML tree which initializes the device definition. If an URL is already known it should be
provided to avoid additional latency for a broader first device discovery.
This method also do some magic to find the best result for the given host as UPnP devices behave sometimes
strangely. This call is costly the result if any should be cached.
:param str host: the host to find
:param service: the service type or list of service types if known to search for
:type service: str or list[str]
:param str deviceDefinitionURL: if provided it is used to skip a first device discovery
:param float timeout: the time to wait for each retry
:param int retries: the amount of times how often the device is tried to discover
:param str ipAddress: the multicast ip address to discover devices
:param int port: the port to discover devices
:param str proxies: proxy definition as defined here:
`Proxy definition <http://docs.python-requests.org/en/latest/user/advanced/#proxies>`_
:return: If the device have been found the response is returned otherwise None
:rtype: DiscoveryResponse
:raises ValueError: if problems with reading or parsing the xml device definition occurs
:raises requests.exceptions.ConnectionError: when the device definitions can not be downloaded
:raises requests.exceptions.ConnectTimeout: when download time out
Example:
::
proxies = {"http": "http://localhost:8888"}
result = discoverParticularHost("192.168.0.1", proxies=proxies)
if result is not None:
print("Host: " + result.locationHost + " Port: " + result.locationPort + " Device definitions: " + \\
result.location)
.. seealso::
:class:`~simpletr64.DiscoveryResponse`, :meth:`~simpletr64.Discover.discover` | [
"Discover",
"a",
"particular",
"host",
"and",
"find",
"the",
"best",
"response",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/discover.py#L114-L265 | train | 51,326 |
bpannier/simpletr64 | simpletr64/discover.py | Discover.rateServiceTypeInResult | def rateServiceTypeInResult(discoveryResponse):
"""Gives a quality rating for a given service type in a result, higher is better.
Several UpnP devices reply to a discovery request with multiple responses with different service type
announcements. To find the most specific one we need to be able rate the service types against each other.
Usually this is an internal method and just exported for convenience reasons.
:param DiscoveryResponse discoveryResponse: the response to rate
:return: a rating of the quality of the given service type
:rtype: int
"""
if discoveryResponse is None:
return 0
serviceType = discoveryResponse.service
if serviceType.startswith("urn:dslforum-org:device"):
return 11
if serviceType.startswith("urn:dslforum-org:service"):
return 10
if serviceType.startswith("urn:dslforum-org:"):
return 9
if serviceType.startswith("urn:schemas-upnp-org:device"):
return 8
if serviceType.startswith("urn:schemas-upnp-org:service"):
return 7
if serviceType.startswith("urn:schemas-upnp-org:"):
return 6
if serviceType.startswith("urn:schemas-"): # other schemas, schema-any-com for example
return 5
if serviceType.startswith("urn:"):
return 4
if serviceType.startswith("upnp:rootdevice"):
return 3
if serviceType.startswith("uuid:"): # no service, just the uuid given
return 2
return 1 | python | def rateServiceTypeInResult(discoveryResponse):
"""Gives a quality rating for a given service type in a result, higher is better.
Several UpnP devices reply to a discovery request with multiple responses with different service type
announcements. To find the most specific one we need to be able rate the service types against each other.
Usually this is an internal method and just exported for convenience reasons.
:param DiscoveryResponse discoveryResponse: the response to rate
:return: a rating of the quality of the given service type
:rtype: int
"""
if discoveryResponse is None:
return 0
serviceType = discoveryResponse.service
if serviceType.startswith("urn:dslforum-org:device"):
return 11
if serviceType.startswith("urn:dslforum-org:service"):
return 10
if serviceType.startswith("urn:dslforum-org:"):
return 9
if serviceType.startswith("urn:schemas-upnp-org:device"):
return 8
if serviceType.startswith("urn:schemas-upnp-org:service"):
return 7
if serviceType.startswith("urn:schemas-upnp-org:"):
return 6
if serviceType.startswith("urn:schemas-"): # other schemas, schema-any-com for example
return 5
if serviceType.startswith("urn:"):
return 4
if serviceType.startswith("upnp:rootdevice"):
return 3
if serviceType.startswith("uuid:"): # no service, just the uuid given
return 2
return 1 | [
"def",
"rateServiceTypeInResult",
"(",
"discoveryResponse",
")",
":",
"if",
"discoveryResponse",
"is",
"None",
":",
"return",
"0",
"serviceType",
"=",
"discoveryResponse",
".",
"service",
"if",
"serviceType",
".",
"startswith",
"(",
"\"urn:dslforum-org:device\"",
")",... | Gives a quality rating for a given service type in a result, higher is better.
Several UpnP devices reply to a discovery request with multiple responses with different service type
announcements. To find the most specific one we need to be able rate the service types against each other.
Usually this is an internal method and just exported for convenience reasons.
:param DiscoveryResponse discoveryResponse: the response to rate
:return: a rating of the quality of the given service type
:rtype: int | [
"Gives",
"a",
"quality",
"rating",
"for",
"a",
"given",
"service",
"type",
"in",
"a",
"result",
"higher",
"is",
"better",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/discover.py#L268-L304 | train | 51,327 |
vsoch/helpme | helpme/main/base/http.py | download | def download(self, url,
file_name,
headers=None,
show_progress=True):
'''stream to a temporary file, rename on successful completion
Parameters
==========
file_name: the file name to stream to
url: the url to stream from
headers: additional headers to add
force: If the final image exists, don't overwrite
'''
fd, tmp_file = tempfile.mkstemp(prefix=("%s.tmp." % file_name))
os.close(fd)
# Should we verify the request?
verify = self._verify()
# Check here if exists
if requests.head(url, verify=verify).status_code in [200, 401]:
response = self.stream(url, headers=headers, stream_to=tmp_file)
if isinstance(response, HTTPError):
bot.error("Error downloading %s, exiting." %url)
sys.exit(1)
shutil.move(tmp_file, file_name)
else:
bot.error("Invalid url or permissions %s" %url)
return file_name | python | def download(self, url,
file_name,
headers=None,
show_progress=True):
'''stream to a temporary file, rename on successful completion
Parameters
==========
file_name: the file name to stream to
url: the url to stream from
headers: additional headers to add
force: If the final image exists, don't overwrite
'''
fd, tmp_file = tempfile.mkstemp(prefix=("%s.tmp." % file_name))
os.close(fd)
# Should we verify the request?
verify = self._verify()
# Check here if exists
if requests.head(url, verify=verify).status_code in [200, 401]:
response = self.stream(url, headers=headers, stream_to=tmp_file)
if isinstance(response, HTTPError):
bot.error("Error downloading %s, exiting." %url)
sys.exit(1)
shutil.move(tmp_file, file_name)
else:
bot.error("Invalid url or permissions %s" %url)
return file_name | [
"def",
"download",
"(",
"self",
",",
"url",
",",
"file_name",
",",
"headers",
"=",
"None",
",",
"show_progress",
"=",
"True",
")",
":",
"fd",
",",
"tmp_file",
"=",
"tempfile",
".",
"mkstemp",
"(",
"prefix",
"=",
"(",
"\"%s.tmp.\"",
"%",
"file_name",
")... | stream to a temporary file, rename on successful completion
Parameters
==========
file_name: the file name to stream to
url: the url to stream from
headers: additional headers to add
force: If the final image exists, don't overwrite | [
"stream",
"to",
"a",
"temporary",
"file",
"rename",
"on",
"successful",
"completion"
] | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/main/base/http.py#L172-L204 | train | 51,328 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64.getControlURL | def getControlURL(self, serviceType, default=None):
"""Returns the control URL for a given service type.
When the device definitions have been loaded with :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions` this
method returns for a given service type/namespace the associated control URL. If the device definitions have
not been loaded a default value can be given which gets returned instead. The control URL is used to
execute actions for a dedicated service type/namespace.
:param serviceType: the service type to look up for
:param default: the default return value in case the service type is not found and device definitions are not
loaded
:type default: str or None
:return: the URL/URI
:rtype: str or None
:raises ValueError: if the device did load device definitions and the service type is not known.
.. seealso::
:meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions`
"""
if serviceType in self.__deviceServiceDefinitions.keys():
return self.__deviceServiceDefinitions[serviceType]["controlURL"]
# check if definitions have been loaded, then dont return the default
if self.__deviceXMLInitialized:
raise ValueError("Device do not support given serviceType: " + serviceType)
return default | python | def getControlURL(self, serviceType, default=None):
"""Returns the control URL for a given service type.
When the device definitions have been loaded with :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions` this
method returns for a given service type/namespace the associated control URL. If the device definitions have
not been loaded a default value can be given which gets returned instead. The control URL is used to
execute actions for a dedicated service type/namespace.
:param serviceType: the service type to look up for
:param default: the default return value in case the service type is not found and device definitions are not
loaded
:type default: str or None
:return: the URL/URI
:rtype: str or None
:raises ValueError: if the device did load device definitions and the service type is not known.
.. seealso::
:meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions`
"""
if serviceType in self.__deviceServiceDefinitions.keys():
return self.__deviceServiceDefinitions[serviceType]["controlURL"]
# check if definitions have been loaded, then dont return the default
if self.__deviceXMLInitialized:
raise ValueError("Device do not support given serviceType: " + serviceType)
return default | [
"def",
"getControlURL",
"(",
"self",
",",
"serviceType",
",",
"default",
"=",
"None",
")",
":",
"if",
"serviceType",
"in",
"self",
".",
"__deviceServiceDefinitions",
".",
"keys",
"(",
")",
":",
"return",
"self",
".",
"__deviceServiceDefinitions",
"[",
"service... | Returns the control URL for a given service type.
When the device definitions have been loaded with :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions` this
method returns for a given service type/namespace the associated control URL. If the device definitions have
not been loaded a default value can be given which gets returned instead. The control URL is used to
execute actions for a dedicated service type/namespace.
:param serviceType: the service type to look up for
:param default: the default return value in case the service type is not found and device definitions are not
loaded
:type default: str or None
:return: the URL/URI
:rtype: str or None
:raises ValueError: if the device did load device definitions and the service type is not known.
.. seealso::
:meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions` | [
"Returns",
"the",
"control",
"URL",
"for",
"a",
"given",
"service",
"type",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L299-L326 | train | 51,329 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64.getEventSubURL | def getEventSubURL(self, serviceType, default=None):
"""Returns the event URL for a given service type.
When the device definitions have been loaded with :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions` this
method returns for a given service type/namespace the associated event URL. If the device definitions have
not been loaded a default value can be given which gets returned instead.
:param serviceType: the service type to look up for
:param default: the default return value in case the service type is not found and device definitions are not
loaded
:type default: str or None
:return: the URL/URI
:rtype: str or None
:raises ValueError: if the device did load device definitions and the service type is not known.
.. seealso::
:meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions`
"""
if serviceType in self.__deviceServiceDefinitions.keys():
return self.__deviceServiceDefinitions[serviceType]["eventSubURL"]
# check if definitions have been loaded, then dont return the default
if self.__deviceXMLInitialized:
raise ValueError("Device do not support given serviceType: " + serviceType)
return default | python | def getEventSubURL(self, serviceType, default=None):
"""Returns the event URL for a given service type.
When the device definitions have been loaded with :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions` this
method returns for a given service type/namespace the associated event URL. If the device definitions have
not been loaded a default value can be given which gets returned instead.
:param serviceType: the service type to look up for
:param default: the default return value in case the service type is not found and device definitions are not
loaded
:type default: str or None
:return: the URL/URI
:rtype: str or None
:raises ValueError: if the device did load device definitions and the service type is not known.
.. seealso::
:meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions`
"""
if serviceType in self.__deviceServiceDefinitions.keys():
return self.__deviceServiceDefinitions[serviceType]["eventSubURL"]
# check if definitions have been loaded, then dont return the default
if self.__deviceXMLInitialized:
raise ValueError("Device do not support given serviceType: " + serviceType)
return default | [
"def",
"getEventSubURL",
"(",
"self",
",",
"serviceType",
",",
"default",
"=",
"None",
")",
":",
"if",
"serviceType",
"in",
"self",
".",
"__deviceServiceDefinitions",
".",
"keys",
"(",
")",
":",
"return",
"self",
".",
"__deviceServiceDefinitions",
"[",
"servic... | Returns the event URL for a given service type.
When the device definitions have been loaded with :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions` this
method returns for a given service type/namespace the associated event URL. If the device definitions have
not been loaded a default value can be given which gets returned instead.
:param serviceType: the service type to look up for
:param default: the default return value in case the service type is not found and device definitions are not
loaded
:type default: str or None
:return: the URL/URI
:rtype: str or None
:raises ValueError: if the device did load device definitions and the service type is not known.
.. seealso::
:meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions` | [
"Returns",
"the",
"event",
"URL",
"for",
"a",
"given",
"service",
"type",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L328-L354 | train | 51,330 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64.execute | def execute(self, uri, namespace, action, timeout=2, **kwargs):
"""Executes a given action with optional arguments.
The execution of an action of an UPnP/TR64 device needs more than just the name of an action. It needs the
control URI which is called to place the action and also the namespace aka service type is needed. The
namespace defines the scope or service type of the given action, the same action name can appear in different
namespaces.
The way how to obtain the needed information's is either through the documentation of the vendor of the
device. Or through a discovery requests which return's the URL to the root device description XML.
:param str uri: the control URI, for example ``/upnp/control/hosts``
:param str namespace: the namespace for the given action, for example ``urn:dslforum-org:service:Hosts:1``
:param str action: the name of the action to call, for example ``GetGenericHostEntry``
:param float timeout: the timeout to wait for the action to be executed
:param kwargs: optional arguments for the given action, depends if the action needs parameter. The arguments
are given as dict where the key is the parameter name and the value the value of the parameter.
:type kwargs: dict[str, str]
:return: returns the results of the action, if any. The results are structured as dict where the key is the
name of the result argument and the value is the value of the result.
:rtype: dict[str,str]
:raises ValueError: if parameters are not set correctly
:raises requests.exceptions.ConnectionError: when the action can not be placed on the device
:raises requests.exceptions.ConnectTimeout: when download time out
Example:
::
device = DeviceTR64(...)
device.execute("/upnp/control/hosts", "urn:dslforum-org:service:Hosts:1",
"GetGenericHostEntry", {"NewIndex": 1})
{'NewActive': '0', 'NewIPAddress': '192.168.0.23', 'NewMACAddress': '9C:20:7B:E7:FF:5F',
'NewInterfaceType': 'Ethernet', 'NewHostName': 'Apple-TV', 'NewAddressSource': 'DHCP',
'NewLeaseTimeRemaining': '0'}
.. seealso::
`Additional short explanation of the UPnP protocol <http://www.upnp-hacks.org/upnp.html>`_
:class:`~simpletr64.Discover`, :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions`,
:meth:`~simpletr64.DeviceTR64.loadSCPD`
"""
if not uri:
raise ValueError("No action URI has been defined.")
if not namespace:
raise ValueError("No namespace has been defined.")
if not action:
raise ValueError("No action has been defined.")
# soap headers
header = {'Content-Type': 'text/xml; charset="UTF-8"',
'Soapaction': '"' + namespace + "#" + action + '"'}
# build SOAP body
body = '''<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<s:Header/>
<s:Body>\n'''
body += " <u:" + action + ' xmlns="' + namespace + '">\n'
arguments = {}
for key in kwargs.keys():
body += " <" + key + ">" + str(kwargs[key]) + "</" + key + ">\n"
arguments[key] = str(kwargs[key])
body += " </u:" + action + ">\n"
body += '''</s:Body>
</s:Envelope>'''
# setup proxies
proxies = {}
if self.__httpsProxy:
proxies = {"https": self.__httpsProxy}
if self.__httpProxy:
proxies = {"http": self.__httpProxy}
# setup authentication
auth = None
if self.__password:
auth = HTTPDigestAuth(self.__username, self.__password)
# build the URL
location = self.__protocol + "://" + self.__hostname + ":" + str(self.port) + uri
# Post http request
request = requests.post(location, data=body, headers=header, auth=auth, proxies=proxies, timeout=float(timeout),
verify=self.__verify)
if request.status_code != 200:
errorStr = DeviceTR64._extractErrorString(request)
raise ValueError('Could not execute "' + action + str(arguments) + '": ' + str(request.status_code) +
' - ' + request.reason + " -- " + errorStr)
# parse XML return
try:
root = ET.fromstring(request.text.encode('utf-8'))
except Exception as e:
raise ValueError("Can not parse results for the action: " + str(e))
# iterate in the XML structure to get the action result
actionNode = root[0][0]
# we need to remove XML namespace for the action node
namespaceLength = len(namespace) + 2 # add braces
tag = actionNode.tag[namespaceLength:]
if tag != (action + "Response"):
raise ValueError('Soap result structure is wrong, expected action "' + action +
'Response" got "' + tag + '".')
# pack all the received results
results = {}
for resultNode in actionNode:
results[resultNode.tag] = resultNode.text
return results | python | def execute(self, uri, namespace, action, timeout=2, **kwargs):
"""Executes a given action with optional arguments.
The execution of an action of an UPnP/TR64 device needs more than just the name of an action. It needs the
control URI which is called to place the action and also the namespace aka service type is needed. The
namespace defines the scope or service type of the given action, the same action name can appear in different
namespaces.
The way how to obtain the needed information's is either through the documentation of the vendor of the
device. Or through a discovery requests which return's the URL to the root device description XML.
:param str uri: the control URI, for example ``/upnp/control/hosts``
:param str namespace: the namespace for the given action, for example ``urn:dslforum-org:service:Hosts:1``
:param str action: the name of the action to call, for example ``GetGenericHostEntry``
:param float timeout: the timeout to wait for the action to be executed
:param kwargs: optional arguments for the given action, depends if the action needs parameter. The arguments
are given as dict where the key is the parameter name and the value the value of the parameter.
:type kwargs: dict[str, str]
:return: returns the results of the action, if any. The results are structured as dict where the key is the
name of the result argument and the value is the value of the result.
:rtype: dict[str,str]
:raises ValueError: if parameters are not set correctly
:raises requests.exceptions.ConnectionError: when the action can not be placed on the device
:raises requests.exceptions.ConnectTimeout: when download time out
Example:
::
device = DeviceTR64(...)
device.execute("/upnp/control/hosts", "urn:dslforum-org:service:Hosts:1",
"GetGenericHostEntry", {"NewIndex": 1})
{'NewActive': '0', 'NewIPAddress': '192.168.0.23', 'NewMACAddress': '9C:20:7B:E7:FF:5F',
'NewInterfaceType': 'Ethernet', 'NewHostName': 'Apple-TV', 'NewAddressSource': 'DHCP',
'NewLeaseTimeRemaining': '0'}
.. seealso::
`Additional short explanation of the UPnP protocol <http://www.upnp-hacks.org/upnp.html>`_
:class:`~simpletr64.Discover`, :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions`,
:meth:`~simpletr64.DeviceTR64.loadSCPD`
"""
if not uri:
raise ValueError("No action URI has been defined.")
if not namespace:
raise ValueError("No namespace has been defined.")
if not action:
raise ValueError("No action has been defined.")
# soap headers
header = {'Content-Type': 'text/xml; charset="UTF-8"',
'Soapaction': '"' + namespace + "#" + action + '"'}
# build SOAP body
body = '''<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<s:Header/>
<s:Body>\n'''
body += " <u:" + action + ' xmlns="' + namespace + '">\n'
arguments = {}
for key in kwargs.keys():
body += " <" + key + ">" + str(kwargs[key]) + "</" + key + ">\n"
arguments[key] = str(kwargs[key])
body += " </u:" + action + ">\n"
body += '''</s:Body>
</s:Envelope>'''
# setup proxies
proxies = {}
if self.__httpsProxy:
proxies = {"https": self.__httpsProxy}
if self.__httpProxy:
proxies = {"http": self.__httpProxy}
# setup authentication
auth = None
if self.__password:
auth = HTTPDigestAuth(self.__username, self.__password)
# build the URL
location = self.__protocol + "://" + self.__hostname + ":" + str(self.port) + uri
# Post http request
request = requests.post(location, data=body, headers=header, auth=auth, proxies=proxies, timeout=float(timeout),
verify=self.__verify)
if request.status_code != 200:
errorStr = DeviceTR64._extractErrorString(request)
raise ValueError('Could not execute "' + action + str(arguments) + '": ' + str(request.status_code) +
' - ' + request.reason + " -- " + errorStr)
# parse XML return
try:
root = ET.fromstring(request.text.encode('utf-8'))
except Exception as e:
raise ValueError("Can not parse results for the action: " + str(e))
# iterate in the XML structure to get the action result
actionNode = root[0][0]
# we need to remove XML namespace for the action node
namespaceLength = len(namespace) + 2 # add braces
tag = actionNode.tag[namespaceLength:]
if tag != (action + "Response"):
raise ValueError('Soap result structure is wrong, expected action "' + action +
'Response" got "' + tag + '".')
# pack all the received results
results = {}
for resultNode in actionNode:
results[resultNode.tag] = resultNode.text
return results | [
"def",
"execute",
"(",
"self",
",",
"uri",
",",
"namespace",
",",
"action",
",",
"timeout",
"=",
"2",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"not",
"uri",
":",
"raise",
"ValueError",
"(",
"\"No action URI has been defined.\"",
")",
"if",
"not",
"namesp... | Executes a given action with optional arguments.
The execution of an action of an UPnP/TR64 device needs more than just the name of an action. It needs the
control URI which is called to place the action and also the namespace aka service type is needed. The
namespace defines the scope or service type of the given action, the same action name can appear in different
namespaces.
The way how to obtain the needed information's is either through the documentation of the vendor of the
device. Or through a discovery requests which return's the URL to the root device description XML.
:param str uri: the control URI, for example ``/upnp/control/hosts``
:param str namespace: the namespace for the given action, for example ``urn:dslforum-org:service:Hosts:1``
:param str action: the name of the action to call, for example ``GetGenericHostEntry``
:param float timeout: the timeout to wait for the action to be executed
:param kwargs: optional arguments for the given action, depends if the action needs parameter. The arguments
are given as dict where the key is the parameter name and the value the value of the parameter.
:type kwargs: dict[str, str]
:return: returns the results of the action, if any. The results are structured as dict where the key is the
name of the result argument and the value is the value of the result.
:rtype: dict[str,str]
:raises ValueError: if parameters are not set correctly
:raises requests.exceptions.ConnectionError: when the action can not be placed on the device
:raises requests.exceptions.ConnectTimeout: when download time out
Example:
::
device = DeviceTR64(...)
device.execute("/upnp/control/hosts", "urn:dslforum-org:service:Hosts:1",
"GetGenericHostEntry", {"NewIndex": 1})
{'NewActive': '0', 'NewIPAddress': '192.168.0.23', 'NewMACAddress': '9C:20:7B:E7:FF:5F',
'NewInterfaceType': 'Ethernet', 'NewHostName': 'Apple-TV', 'NewAddressSource': 'DHCP',
'NewLeaseTimeRemaining': '0'}
.. seealso::
`Additional short explanation of the UPnP protocol <http://www.upnp-hacks.org/upnp.html>`_
:class:`~simpletr64.Discover`, :meth:`~simpletr64.DeviceTR64.loadDeviceDefinitions`,
:meth:`~simpletr64.DeviceTR64.loadSCPD` | [
"Executes",
"a",
"given",
"action",
"with",
"optional",
"arguments",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L356-L483 | train | 51,331 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64._extractErrorString | def _extractErrorString(request):
"""Extract error string from a failed UPnP call.
:param request: the failed request result
:type request: requests.Response
:return: an extracted error text or empty str
:rtype: str
"""
errorStr = ""
tag = None
# noinspection PyBroadException
try:
# parse XML return
root = ET.fromstring(request.text.encode('utf-8'))
tag = root[0][0]
except:
# return an empty string as we can not parse the structure
return errorStr
for element in tag.getiterator():
tagName = element.tag.lower()
if tagName.endswith("string"):
errorStr += element.text + " "
elif tagName.endswith("description"):
errorStr += element.text + " "
return errorStr | python | def _extractErrorString(request):
"""Extract error string from a failed UPnP call.
:param request: the failed request result
:type request: requests.Response
:return: an extracted error text or empty str
:rtype: str
"""
errorStr = ""
tag = None
# noinspection PyBroadException
try:
# parse XML return
root = ET.fromstring(request.text.encode('utf-8'))
tag = root[0][0]
except:
# return an empty string as we can not parse the structure
return errorStr
for element in tag.getiterator():
tagName = element.tag.lower()
if tagName.endswith("string"):
errorStr += element.text + " "
elif tagName.endswith("description"):
errorStr += element.text + " "
return errorStr | [
"def",
"_extractErrorString",
"(",
"request",
")",
":",
"errorStr",
"=",
"\"\"",
"tag",
"=",
"None",
"# noinspection PyBroadException",
"try",
":",
"# parse XML return",
"root",
"=",
"ET",
".",
"fromstring",
"(",
"request",
".",
"text",
".",
"encode",
"(",
"'u... | Extract error string from a failed UPnP call.
:param request: the failed request result
:type request: requests.Response
:return: an extracted error text or empty str
:rtype: str | [
"Extract",
"error",
"string",
"from",
"a",
"failed",
"UPnP",
"call",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L486-L515 | train | 51,332 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64.setupTR64Device | def setupTR64Device(self, deviceType):
"""Setup actions for known devices.
For convenience reasons for some devices there is no need to discover/load device definitions before the
pre defined :doc:`tr64` can be used.
The following devices are currently supported (please help to extend):
* fritz.box - Any AVM Fritz Box with the latest software version installed
:param str deviceType: a known device type
:raise ValueError: if the device type is not known.
.. seealso:: :doc:`tr64`
"""
if deviceType.lower() != "fritz.box":
raise ValueError("Unknown device type given.")
self.__deviceServiceDefinitions = {}
self.__deviceXMLInitialized = False
# Fritz.box setup
self.deviceServiceDefinitions["urn:dslforum-org:service:DeviceConfig:1"] = {
"controlURL": "/upnp/control/deviceconfig"}
self.deviceServiceDefinitions["urn:dslforum-org:service:ManagementServer:1"] = {
"controlURL": "/upnp/control/mgmsrv"}
self.deviceServiceDefinitions["urn:dslforum-org:service:LANConfigSecurity:1"] = {
"controlURL": "/upnp/control/lanconfigsecurity"}
self.deviceServiceDefinitions["urn:dslforum-org:service:Time:1"] = {"controlURL": "/upnp/control/time"}
self.deviceServiceDefinitions["urn:dslforum-org:service:LANHostConfigManagement:1"] = {
"controlURL": "/upnp/control/lanhostconfigmgm"}
self.deviceServiceDefinitions["urn:dslforum-org:service:UserInterface:1"] = {
"controlURL": "/upnp/control/userif"}
self.deviceServiceDefinitions["urn:dslforum-org:service:DeviceInfo:1"] = {
"controlURL": "/upnp/control/deviceinfo"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_TAM:1"] = {"controlURL": "/upnp/control/x_tam"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_MyFritz:1"] = {
"controlURL": "/upnp/control/x_myfritz"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_RemoteAccess:1"] = {
"controlURL": "/upnp/control/x_remote"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WLANConfiguration:1"] = {
"controlURL": "/upnp/control/wlanconfig1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WLANConfiguration:3"] = {
"controlURL": "/upnp/control/wlanconfig3"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WLANConfiguration:2"] = {
"controlURL": "/upnp/control/wlanconfig2"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_WebDAVClient:1"] = {
"controlURL": "/upnp/control/x_webdav"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANDSLLinkConfig:1"] = {
"controlURL": "/upnp/control/wandsllinkconfig1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:Hosts:1"] = {"controlURL": "/upnp/control/hosts"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_VoIP:1"] = {"controlURL": "/upnp/control/x_voip"}
self.deviceServiceDefinitions["urn:dslforum-org:service:LANEthernetInterfaceConfig:1"] = {
"controlURL": "/upnp/control/lanethernetifcfg"}
self.deviceServiceDefinitions["urn:dslforum-org:service:Layer3Forwarding:1"] = {
"controlURL": "/upnp/control/layer3forwarding"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANIPConnection:1"] = {
"controlURL": "/upnp/control/wanipconnection1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_OnTel:1"] = {
"controlURL": "/upnp/control/x_contact"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANCommonInterfaceConfig:1"] = {
"controlURL": "/upnp/control/wancommonifconfig1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_UPnP:1"] = {
"controlURL": "/upnp/control/x_upnp"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANDSLInterfaceConfig:1"] = {
"controlURL": "/upnp/control/wandslifconfig1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANPPPConnection:1"] = {
"controlURL": "/upnp/control/wanpppconn1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_Storage:1"] = {
"controlURL": "/upnp/control/x_storage"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANEthernetLinkConfig:1"] = {
"controlURL": "/upnp/control/wanethlinkconfig1"} | python | def setupTR64Device(self, deviceType):
"""Setup actions for known devices.
For convenience reasons for some devices there is no need to discover/load device definitions before the
pre defined :doc:`tr64` can be used.
The following devices are currently supported (please help to extend):
* fritz.box - Any AVM Fritz Box with the latest software version installed
:param str deviceType: a known device type
:raise ValueError: if the device type is not known.
.. seealso:: :doc:`tr64`
"""
if deviceType.lower() != "fritz.box":
raise ValueError("Unknown device type given.")
self.__deviceServiceDefinitions = {}
self.__deviceXMLInitialized = False
# Fritz.box setup
self.deviceServiceDefinitions["urn:dslforum-org:service:DeviceConfig:1"] = {
"controlURL": "/upnp/control/deviceconfig"}
self.deviceServiceDefinitions["urn:dslforum-org:service:ManagementServer:1"] = {
"controlURL": "/upnp/control/mgmsrv"}
self.deviceServiceDefinitions["urn:dslforum-org:service:LANConfigSecurity:1"] = {
"controlURL": "/upnp/control/lanconfigsecurity"}
self.deviceServiceDefinitions["urn:dslforum-org:service:Time:1"] = {"controlURL": "/upnp/control/time"}
self.deviceServiceDefinitions["urn:dslforum-org:service:LANHostConfigManagement:1"] = {
"controlURL": "/upnp/control/lanhostconfigmgm"}
self.deviceServiceDefinitions["urn:dslforum-org:service:UserInterface:1"] = {
"controlURL": "/upnp/control/userif"}
self.deviceServiceDefinitions["urn:dslforum-org:service:DeviceInfo:1"] = {
"controlURL": "/upnp/control/deviceinfo"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_TAM:1"] = {"controlURL": "/upnp/control/x_tam"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_MyFritz:1"] = {
"controlURL": "/upnp/control/x_myfritz"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_RemoteAccess:1"] = {
"controlURL": "/upnp/control/x_remote"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WLANConfiguration:1"] = {
"controlURL": "/upnp/control/wlanconfig1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WLANConfiguration:3"] = {
"controlURL": "/upnp/control/wlanconfig3"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WLANConfiguration:2"] = {
"controlURL": "/upnp/control/wlanconfig2"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_WebDAVClient:1"] = {
"controlURL": "/upnp/control/x_webdav"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANDSLLinkConfig:1"] = {
"controlURL": "/upnp/control/wandsllinkconfig1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:Hosts:1"] = {"controlURL": "/upnp/control/hosts"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_VoIP:1"] = {"controlURL": "/upnp/control/x_voip"}
self.deviceServiceDefinitions["urn:dslforum-org:service:LANEthernetInterfaceConfig:1"] = {
"controlURL": "/upnp/control/lanethernetifcfg"}
self.deviceServiceDefinitions["urn:dslforum-org:service:Layer3Forwarding:1"] = {
"controlURL": "/upnp/control/layer3forwarding"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANIPConnection:1"] = {
"controlURL": "/upnp/control/wanipconnection1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_OnTel:1"] = {
"controlURL": "/upnp/control/x_contact"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANCommonInterfaceConfig:1"] = {
"controlURL": "/upnp/control/wancommonifconfig1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_UPnP:1"] = {
"controlURL": "/upnp/control/x_upnp"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANDSLInterfaceConfig:1"] = {
"controlURL": "/upnp/control/wandslifconfig1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANPPPConnection:1"] = {
"controlURL": "/upnp/control/wanpppconn1"}
self.deviceServiceDefinitions["urn:dslforum-org:service:X_AVM-DE_Storage:1"] = {
"controlURL": "/upnp/control/x_storage"}
self.deviceServiceDefinitions["urn:dslforum-org:service:WANEthernetLinkConfig:1"] = {
"controlURL": "/upnp/control/wanethlinkconfig1"} | [
"def",
"setupTR64Device",
"(",
"self",
",",
"deviceType",
")",
":",
"if",
"deviceType",
".",
"lower",
"(",
")",
"!=",
"\"fritz.box\"",
":",
"raise",
"ValueError",
"(",
"\"Unknown device type given.\"",
")",
"self",
".",
"__deviceServiceDefinitions",
"=",
"{",
"}... | Setup actions for known devices.
For convenience reasons for some devices there is no need to discover/load device definitions before the
pre defined :doc:`tr64` can be used.
The following devices are currently supported (please help to extend):
* fritz.box - Any AVM Fritz Box with the latest software version installed
:param str deviceType: a known device type
:raise ValueError: if the device type is not known.
.. seealso:: :doc:`tr64` | [
"Setup",
"actions",
"for",
"known",
"devices",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L517-L589 | train | 51,333 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64.loadDeviceDefinitions | def loadDeviceDefinitions(self, urlOfXMLDefinition, timeout=3):
"""Loads the device definitions from a given URL which points to the root XML in the device.
This loads the device definitions which is needed in case you like to:
* get additional information's about the device like manufacture, device type, etc
* get all support service types of this device
* use the convenient actions classes part of this library in the actions module
:param str urlOfXMLDefinition: the URL to the root XML which sets the device definitions.
:param float timeout: the timeout for downloading
:raises ValueError: if the XML could not be parsed correctly
:raises requests.exceptions.ConnectionError: when the device definitions can not be downloaded
:raises requests.exceptions.ConnectTimeout: when download time out
.. seealso::
:meth:`~simpletr64.DeviceTR64.loadSCPD`, :meth:`~simpletr64.DeviceTR64.deviceServiceDefinitions`,
:meth:`~simpletr64.DeviceTR64.deviceInformations`, :meth:`~simpletr64.DeviceTR64.deviceSCPD`,
:meth:`~simpletr64.DeviceTR64.getSCDPURL`, :meth:`~simpletr64.DeviceTR64.getControlURL`,
:meth:`~simpletr64.DeviceTR64.getEventSubURL`
"""
# setup proxies
proxies = {}
if self.__httpsProxy:
proxies = {"https": self.__httpsProxy}
if self.__httpProxy:
proxies = {"http": self.__httpProxy}
# some devices response differently without a User-Agent
headers = {"User-Agent": "Mozilla/5.0; SimpleTR64-1"}
# setup authentication
auth = None
if self.__password:
auth = HTTPDigestAuth(self.__username, self.__password)
# get the content
request = requests.get(urlOfXMLDefinition, proxies=proxies, headers=headers, timeout=float(timeout),
auth=auth, verify=self.__verify)
if request.status_code != 200:
errorStr = DeviceTR64._extractErrorString(request)
raise ValueError('Could not get CPE definitions "' + urlOfXMLDefinition + '" : ' +
str(request.status_code) + ' - ' + request.reason + " -- " + errorStr)
# parse XML return
xml = request.text.encode('utf-8')
return self._loadDeviceDefinitions(urlOfXMLDefinition, xml) | python | def loadDeviceDefinitions(self, urlOfXMLDefinition, timeout=3):
"""Loads the device definitions from a given URL which points to the root XML in the device.
This loads the device definitions which is needed in case you like to:
* get additional information's about the device like manufacture, device type, etc
* get all support service types of this device
* use the convenient actions classes part of this library in the actions module
:param str urlOfXMLDefinition: the URL to the root XML which sets the device definitions.
:param float timeout: the timeout for downloading
:raises ValueError: if the XML could not be parsed correctly
:raises requests.exceptions.ConnectionError: when the device definitions can not be downloaded
:raises requests.exceptions.ConnectTimeout: when download time out
.. seealso::
:meth:`~simpletr64.DeviceTR64.loadSCPD`, :meth:`~simpletr64.DeviceTR64.deviceServiceDefinitions`,
:meth:`~simpletr64.DeviceTR64.deviceInformations`, :meth:`~simpletr64.DeviceTR64.deviceSCPD`,
:meth:`~simpletr64.DeviceTR64.getSCDPURL`, :meth:`~simpletr64.DeviceTR64.getControlURL`,
:meth:`~simpletr64.DeviceTR64.getEventSubURL`
"""
# setup proxies
proxies = {}
if self.__httpsProxy:
proxies = {"https": self.__httpsProxy}
if self.__httpProxy:
proxies = {"http": self.__httpProxy}
# some devices response differently without a User-Agent
headers = {"User-Agent": "Mozilla/5.0; SimpleTR64-1"}
# setup authentication
auth = None
if self.__password:
auth = HTTPDigestAuth(self.__username, self.__password)
# get the content
request = requests.get(urlOfXMLDefinition, proxies=proxies, headers=headers, timeout=float(timeout),
auth=auth, verify=self.__verify)
if request.status_code != 200:
errorStr = DeviceTR64._extractErrorString(request)
raise ValueError('Could not get CPE definitions "' + urlOfXMLDefinition + '" : ' +
str(request.status_code) + ' - ' + request.reason + " -- " + errorStr)
# parse XML return
xml = request.text.encode('utf-8')
return self._loadDeviceDefinitions(urlOfXMLDefinition, xml) | [
"def",
"loadDeviceDefinitions",
"(",
"self",
",",
"urlOfXMLDefinition",
",",
"timeout",
"=",
"3",
")",
":",
"# setup proxies",
"proxies",
"=",
"{",
"}",
"if",
"self",
".",
"__httpsProxy",
":",
"proxies",
"=",
"{",
"\"https\"",
":",
"self",
".",
"__httpsProxy... | Loads the device definitions from a given URL which points to the root XML in the device.
This loads the device definitions which is needed in case you like to:
* get additional information's about the device like manufacture, device type, etc
* get all support service types of this device
* use the convenient actions classes part of this library in the actions module
:param str urlOfXMLDefinition: the URL to the root XML which sets the device definitions.
:param float timeout: the timeout for downloading
:raises ValueError: if the XML could not be parsed correctly
:raises requests.exceptions.ConnectionError: when the device definitions can not be downloaded
:raises requests.exceptions.ConnectTimeout: when download time out
.. seealso::
:meth:`~simpletr64.DeviceTR64.loadSCPD`, :meth:`~simpletr64.DeviceTR64.deviceServiceDefinitions`,
:meth:`~simpletr64.DeviceTR64.deviceInformations`, :meth:`~simpletr64.DeviceTR64.deviceSCPD`,
:meth:`~simpletr64.DeviceTR64.getSCDPURL`, :meth:`~simpletr64.DeviceTR64.getControlURL`,
:meth:`~simpletr64.DeviceTR64.getEventSubURL` | [
"Loads",
"the",
"device",
"definitions",
"from",
"a",
"given",
"URL",
"which",
"points",
"to",
"the",
"root",
"XML",
"in",
"the",
"device",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L591-L642 | train | 51,334 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64._loadDeviceDefinitions | def _loadDeviceDefinitions(self, urlOfXMLDefinition, xml):
"""Internal call to parse the XML of the device definition.
:param urlOfXMLDefinition: the URL to the XML device defintions
:param xml: the XML content to parse
"""
# extract the base path of the given XML to make sure any relative URL later will be created correctly
url = urlparse(urlOfXMLDefinition)
baseURIPath = url.path.rpartition('/')[0] + "/"
try:
root = ET.fromstring(xml)
except Exception as e:
raise ValueError("Can not parse CPE definitions '" + urlOfXMLDefinition + "': " + str(e))
self.__deviceServiceDefinitions = {}
self.__deviceSCPD = {}
self.__deviceInformations = {'rootURL': urlOfXMLDefinition}
self.__deviceUnknownKeys = {}
self.__deviceXMLInitialized = False
# iterate through all the informations
self._iterateToFindSCPDElements(root, baseURIPath)
self.__deviceXMLInitialized = True | python | def _loadDeviceDefinitions(self, urlOfXMLDefinition, xml):
"""Internal call to parse the XML of the device definition.
:param urlOfXMLDefinition: the URL to the XML device defintions
:param xml: the XML content to parse
"""
# extract the base path of the given XML to make sure any relative URL later will be created correctly
url = urlparse(urlOfXMLDefinition)
baseURIPath = url.path.rpartition('/')[0] + "/"
try:
root = ET.fromstring(xml)
except Exception as e:
raise ValueError("Can not parse CPE definitions '" + urlOfXMLDefinition + "': " + str(e))
self.__deviceServiceDefinitions = {}
self.__deviceSCPD = {}
self.__deviceInformations = {'rootURL': urlOfXMLDefinition}
self.__deviceUnknownKeys = {}
self.__deviceXMLInitialized = False
# iterate through all the informations
self._iterateToFindSCPDElements(root, baseURIPath)
self.__deviceXMLInitialized = True | [
"def",
"_loadDeviceDefinitions",
"(",
"self",
",",
"urlOfXMLDefinition",
",",
"xml",
")",
":",
"# extract the base path of the given XML to make sure any relative URL later will be created correctly",
"url",
"=",
"urlparse",
"(",
"urlOfXMLDefinition",
")",
"baseURIPath",
"=",
"... | Internal call to parse the XML of the device definition.
:param urlOfXMLDefinition: the URL to the XML device defintions
:param xml: the XML content to parse | [
"Internal",
"call",
"to",
"parse",
"the",
"XML",
"of",
"the",
"device",
"definition",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L644-L668 | train | 51,335 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64._iterateToFindSCPDElements | def _iterateToFindSCPDElements(self, element, baseURIPath):
"""Internal method to iterate through device definition XML tree.
:param element: the XML root node of the device definitions
:type element: xml.etree.ElementTree.Element
:param str baseURIPath: the base URL
"""
for child in element.getchildren():
tagName = child.tag.lower()
if tagName.endswith('servicelist'):
self._processServiceList(child,baseURIPath)
elif tagName.endswith('devicetype'):
if "deviceType" not in self.__deviceInformations.keys():
self.__deviceInformations["deviceType"] = child.text
elif tagName.endswith('friendlyname'):
if "friendlyName" not in self.__deviceInformations.keys():
self.__deviceInformations["friendlyName"] = child.text
elif tagName.endswith('manufacturer'):
if "manufacturer" not in self.__deviceInformations.keys():
self.__deviceInformations["manufacturer"] = child.text
elif tagName.endswith('manufacturerurl'):
if "manufacturerURL" not in self.__deviceInformations.keys():
self.__deviceInformations["manufacturerURL"] = child.text
elif tagName.endswith('modeldescription'):
if "modelDescription" not in self.__deviceInformations.keys():
self.__deviceInformations["modelDescription"] = child.text
elif tagName.endswith('modelname'):
if "modelName" not in self.__deviceInformations.keys():
self.__deviceInformations["modelName"] = child.text
elif tagName.endswith('modelurl'):
if "modelURL" not in self.__deviceInformations.keys():
self.__deviceInformations["modelURL"] = child.text
elif tagName.endswith('modelnumber'):
if "modelNumber" not in self.__deviceInformations.keys():
self.__deviceInformations["modelNumber"] = child.text
elif tagName.endswith('serialnumber'):
if "serialNumber" not in self.__deviceInformations.keys():
self.__deviceInformations["serialNumber"] = child.text
elif tagName.endswith('presentationurl'):
if "presentationURL" not in self.__deviceInformations.keys():
self.__deviceInformations["presentationURL"] = child.text
elif tagName.endswith('udn'):
if "UDN" not in self.__deviceInformations.keys():
self.__deviceInformations["UDN"] = child.text
elif tagName.endswith('upc'):
if "UPC" not in self.__deviceInformations.keys():
self.__deviceInformations["UPC"] = child.text
elif tagName.endswith('iconlist') or tagName.endswith('specversion'):
# skip these items
pass
else:
if not tagName.endswith('device') and not tagName.endswith('devicelist'):
self.__deviceUnknownKeys[child.tag] = child.text
self._iterateToFindSCPDElements(child, baseURIPath) | python | def _iterateToFindSCPDElements(self, element, baseURIPath):
"""Internal method to iterate through device definition XML tree.
:param element: the XML root node of the device definitions
:type element: xml.etree.ElementTree.Element
:param str baseURIPath: the base URL
"""
for child in element.getchildren():
tagName = child.tag.lower()
if tagName.endswith('servicelist'):
self._processServiceList(child,baseURIPath)
elif tagName.endswith('devicetype'):
if "deviceType" not in self.__deviceInformations.keys():
self.__deviceInformations["deviceType"] = child.text
elif tagName.endswith('friendlyname'):
if "friendlyName" not in self.__deviceInformations.keys():
self.__deviceInformations["friendlyName"] = child.text
elif tagName.endswith('manufacturer'):
if "manufacturer" not in self.__deviceInformations.keys():
self.__deviceInformations["manufacturer"] = child.text
elif tagName.endswith('manufacturerurl'):
if "manufacturerURL" not in self.__deviceInformations.keys():
self.__deviceInformations["manufacturerURL"] = child.text
elif tagName.endswith('modeldescription'):
if "modelDescription" not in self.__deviceInformations.keys():
self.__deviceInformations["modelDescription"] = child.text
elif tagName.endswith('modelname'):
if "modelName" not in self.__deviceInformations.keys():
self.__deviceInformations["modelName"] = child.text
elif tagName.endswith('modelurl'):
if "modelURL" not in self.__deviceInformations.keys():
self.__deviceInformations["modelURL"] = child.text
elif tagName.endswith('modelnumber'):
if "modelNumber" not in self.__deviceInformations.keys():
self.__deviceInformations["modelNumber"] = child.text
elif tagName.endswith('serialnumber'):
if "serialNumber" not in self.__deviceInformations.keys():
self.__deviceInformations["serialNumber"] = child.text
elif tagName.endswith('presentationurl'):
if "presentationURL" not in self.__deviceInformations.keys():
self.__deviceInformations["presentationURL"] = child.text
elif tagName.endswith('udn'):
if "UDN" not in self.__deviceInformations.keys():
self.__deviceInformations["UDN"] = child.text
elif tagName.endswith('upc'):
if "UPC" not in self.__deviceInformations.keys():
self.__deviceInformations["UPC"] = child.text
elif tagName.endswith('iconlist') or tagName.endswith('specversion'):
# skip these items
pass
else:
if not tagName.endswith('device') and not tagName.endswith('devicelist'):
self.__deviceUnknownKeys[child.tag] = child.text
self._iterateToFindSCPDElements(child, baseURIPath) | [
"def",
"_iterateToFindSCPDElements",
"(",
"self",
",",
"element",
",",
"baseURIPath",
")",
":",
"for",
"child",
"in",
"element",
".",
"getchildren",
"(",
")",
":",
"tagName",
"=",
"child",
".",
"tag",
".",
"lower",
"(",
")",
"if",
"tagName",
".",
"endswi... | Internal method to iterate through device definition XML tree.
:param element: the XML root node of the device definitions
:type element: xml.etree.ElementTree.Element
:param str baseURIPath: the base URL | [
"Internal",
"method",
"to",
"iterate",
"through",
"device",
"definition",
"XML",
"tree",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L670-L725 | train | 51,336 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64._processServiceList | def _processServiceList(self, serviceList, baseURIPath):
"""Internal method to iterate in the device definition XML tree through the service list.
:param serviceList: the XML root node of a service list
:type serviceList: xml.etree.ElementTree.Element
"""
# iterate through all children in serviceList XML tag
for service in serviceList.getchildren():
# has to be a service
if not service.tag.lower().endswith("service"):
raise ValueError("Non service tag in servicelist: " + service.tag)
serviceType = None
controlURL = None
scpdURL = None
eventURL = None
# go through all the tags of a service and remember the values, ignore unknowns
for child in service:
tag = child.tag.lower()
if tag.endswith("servicetype") or (serviceType is None and tag.endswith("spectype")):
serviceType = child.text
elif tag.endswith("controlurl"):
controlURL = str(child.text)
# if the url does not start with / (relative) or with a protocol add the base path
if not controlURL.startswith("/") and not controlURL.startswith("http"):
controlURL = baseURIPath + controlURL
elif tag.endswith("scpdurl"):
scpdURL = str(child.text)
# if the url does not start with / (relative) or with a protocol add the base path
if not scpdURL.startswith("/") and not scpdURL.startswith("http"):
scpdURL = baseURIPath + scpdURL
elif tag.endswith("eventsuburl"):
eventURL = str(child.text)
# if the url does not start with / (relative) or with a protocol add the base path
if not eventURL.startswith("/") and not eventURL.startswith("http"):
eventURL = baseURIPath + eventURL
# check if serviceType and the URL's have been found
if serviceType is None or controlURL is None:
raise ValueError("Service is not complete: " + str(serviceType) + " - " +
str(controlURL) + " - " + str(scpdURL))
# no service should be defined twice otherwise the old one will be overwritten
if serviceType in self.__deviceServiceDefinitions.keys():
raise ValueError("Service type '" + serviceType + "' is defined twice.")
self.__deviceServiceDefinitions[serviceType] = {"controlURL": controlURL}
# if the scpd url is defined add it
if scpdURL is not None:
self.__deviceServiceDefinitions[serviceType]["scpdURL"] = scpdURL
# if event url is given we add it as well
if eventURL is not None:
self.__deviceServiceDefinitions[serviceType]["eventSubURL"] = eventURL | python | def _processServiceList(self, serviceList, baseURIPath):
"""Internal method to iterate in the device definition XML tree through the service list.
:param serviceList: the XML root node of a service list
:type serviceList: xml.etree.ElementTree.Element
"""
# iterate through all children in serviceList XML tag
for service in serviceList.getchildren():
# has to be a service
if not service.tag.lower().endswith("service"):
raise ValueError("Non service tag in servicelist: " + service.tag)
serviceType = None
controlURL = None
scpdURL = None
eventURL = None
# go through all the tags of a service and remember the values, ignore unknowns
for child in service:
tag = child.tag.lower()
if tag.endswith("servicetype") or (serviceType is None and tag.endswith("spectype")):
serviceType = child.text
elif tag.endswith("controlurl"):
controlURL = str(child.text)
# if the url does not start with / (relative) or with a protocol add the base path
if not controlURL.startswith("/") and not controlURL.startswith("http"):
controlURL = baseURIPath + controlURL
elif tag.endswith("scpdurl"):
scpdURL = str(child.text)
# if the url does not start with / (relative) or with a protocol add the base path
if not scpdURL.startswith("/") and not scpdURL.startswith("http"):
scpdURL = baseURIPath + scpdURL
elif tag.endswith("eventsuburl"):
eventURL = str(child.text)
# if the url does not start with / (relative) or with a protocol add the base path
if not eventURL.startswith("/") and not eventURL.startswith("http"):
eventURL = baseURIPath + eventURL
# check if serviceType and the URL's have been found
if serviceType is None or controlURL is None:
raise ValueError("Service is not complete: " + str(serviceType) + " - " +
str(controlURL) + " - " + str(scpdURL))
# no service should be defined twice otherwise the old one will be overwritten
if serviceType in self.__deviceServiceDefinitions.keys():
raise ValueError("Service type '" + serviceType + "' is defined twice.")
self.__deviceServiceDefinitions[serviceType] = {"controlURL": controlURL}
# if the scpd url is defined add it
if scpdURL is not None:
self.__deviceServiceDefinitions[serviceType]["scpdURL"] = scpdURL
# if event url is given we add it as well
if eventURL is not None:
self.__deviceServiceDefinitions[serviceType]["eventSubURL"] = eventURL | [
"def",
"_processServiceList",
"(",
"self",
",",
"serviceList",
",",
"baseURIPath",
")",
":",
"# iterate through all children in serviceList XML tag",
"for",
"service",
"in",
"serviceList",
".",
"getchildren",
"(",
")",
":",
"# has to be a service",
"if",
"not",
"service... | Internal method to iterate in the device definition XML tree through the service list.
:param serviceList: the XML root node of a service list
:type serviceList: xml.etree.ElementTree.Element | [
"Internal",
"method",
"to",
"iterate",
"in",
"the",
"device",
"definition",
"XML",
"tree",
"through",
"the",
"service",
"list",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L727-L792 | train | 51,337 |
bpannier/simpletr64 | simpletr64/devicetr64.py | DeviceTR64._loadSCPD | def _loadSCPD(self, serviceType, timeout):
"""Internal method to load the action definitions.
:param str serviceType: the service type to load
:param int timeout: the timeout for downloading
"""
if serviceType not in self.__deviceServiceDefinitions.keys():
raise ValueError("Can not load SCPD, no service type defined for: " + serviceType)
if "scpdURL" not in self.__deviceServiceDefinitions[serviceType].keys():
raise ValueError("No SCPD URL defined for: " + serviceType)
# remove actions for given service type
self.__deviceSCPD.pop(serviceType, None)
uri = self.__deviceServiceDefinitions[serviceType]["scpdURL"]
# setup proxies
proxies = {}
if self.__httpsProxy:
proxies = {"https": self.__httpsProxy}
if self.__httpProxy:
proxies = {"http": self.__httpProxy}
# setup authentication
auth = None
if self.__password:
auth = HTTPDigestAuth(self.__username, self.__password)
# build the URL
location = self.__protocol + "://" + self.__hostname + ":" + str(self.port) + uri
# some devices response differently without a User-Agent
headers = {"User-Agent": "Mozilla/5.0; SimpleTR64-2"}
# http request
request = requests.get(location, auth=auth, proxies=proxies, headers=headers, timeout=timeout,
verify=self.__verify)
if request.status_code != 200:
errorStr = DeviceTR64._extractErrorString(request)
raise ValueError('Could not load SCPD for "' + serviceType + '" from ' + location + ': ' +
str(request.status_code) + ' - ' + request.reason + " -- " + errorStr)
data = request.text.encode('utf-8')
if len(data) == 0:
return
# parse XML return
try:
root = ET.fromstring(data)
except Exception as e:
raise ValueError("Can not parse SCPD content for '" + serviceType + "' from '" + location + "': " + str(e))
actions = {}
variableTypes = {}
variableParameterDict = {}
# iterate through the full XML tree
for element in root.getchildren():
tagName = element.tag.lower()
# go deeper for action lists
if tagName.endswith("actionlist"):
# remember the actions and where a specific variable gets referenced
self._parseSCPDActions(element, actions, variableParameterDict)
# go deeper for the variable declarations
elif tagName.endswith("servicestatetable"):
self._parseSCPDVariableTypes(element, variableTypes)
# everything have been parsed now merge the variable declarations into the action parameters
for name in variableParameterDict.keys():
if name not in variableTypes.keys():
raise ValueError("Variable reference in action can not be resolved: " + name)
# iterate through all arguments where this variable have been referenced
for argument in variableParameterDict[name]:
# fill in the type of this variable/argument
argument["dataType"] = variableTypes[name]["dataType"]
# if the variable declaration includes a default value add it to the action parameter as well
if "defaultValue" in variableTypes[name].keys():
argument["defaultValue"] = variableTypes[name]["defaultValue"]
self.__deviceSCPD[serviceType] = actions | python | def _loadSCPD(self, serviceType, timeout):
"""Internal method to load the action definitions.
:param str serviceType: the service type to load
:param int timeout: the timeout for downloading
"""
if serviceType not in self.__deviceServiceDefinitions.keys():
raise ValueError("Can not load SCPD, no service type defined for: " + serviceType)
if "scpdURL" not in self.__deviceServiceDefinitions[serviceType].keys():
raise ValueError("No SCPD URL defined for: " + serviceType)
# remove actions for given service type
self.__deviceSCPD.pop(serviceType, None)
uri = self.__deviceServiceDefinitions[serviceType]["scpdURL"]
# setup proxies
proxies = {}
if self.__httpsProxy:
proxies = {"https": self.__httpsProxy}
if self.__httpProxy:
proxies = {"http": self.__httpProxy}
# setup authentication
auth = None
if self.__password:
auth = HTTPDigestAuth(self.__username, self.__password)
# build the URL
location = self.__protocol + "://" + self.__hostname + ":" + str(self.port) + uri
# some devices response differently without a User-Agent
headers = {"User-Agent": "Mozilla/5.0; SimpleTR64-2"}
# http request
request = requests.get(location, auth=auth, proxies=proxies, headers=headers, timeout=timeout,
verify=self.__verify)
if request.status_code != 200:
errorStr = DeviceTR64._extractErrorString(request)
raise ValueError('Could not load SCPD for "' + serviceType + '" from ' + location + ': ' +
str(request.status_code) + ' - ' + request.reason + " -- " + errorStr)
data = request.text.encode('utf-8')
if len(data) == 0:
return
# parse XML return
try:
root = ET.fromstring(data)
except Exception as e:
raise ValueError("Can not parse SCPD content for '" + serviceType + "' from '" + location + "': " + str(e))
actions = {}
variableTypes = {}
variableParameterDict = {}
# iterate through the full XML tree
for element in root.getchildren():
tagName = element.tag.lower()
# go deeper for action lists
if tagName.endswith("actionlist"):
# remember the actions and where a specific variable gets referenced
self._parseSCPDActions(element, actions, variableParameterDict)
# go deeper for the variable declarations
elif tagName.endswith("servicestatetable"):
self._parseSCPDVariableTypes(element, variableTypes)
# everything have been parsed now merge the variable declarations into the action parameters
for name in variableParameterDict.keys():
if name not in variableTypes.keys():
raise ValueError("Variable reference in action can not be resolved: " + name)
# iterate through all arguments where this variable have been referenced
for argument in variableParameterDict[name]:
# fill in the type of this variable/argument
argument["dataType"] = variableTypes[name]["dataType"]
# if the variable declaration includes a default value add it to the action parameter as well
if "defaultValue" in variableTypes[name].keys():
argument["defaultValue"] = variableTypes[name]["defaultValue"]
self.__deviceSCPD[serviceType] = actions | [
"def",
"_loadSCPD",
"(",
"self",
",",
"serviceType",
",",
"timeout",
")",
":",
"if",
"serviceType",
"not",
"in",
"self",
".",
"__deviceServiceDefinitions",
".",
"keys",
"(",
")",
":",
"raise",
"ValueError",
"(",
"\"Can not load SCPD, no service type defined for: \""... | Internal method to load the action definitions.
:param str serviceType: the service type to load
:param int timeout: the timeout for downloading | [
"Internal",
"method",
"to",
"load",
"the",
"action",
"definitions",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/devicetr64.py#L836-L923 | train | 51,338 |
bpannier/simpletr64 | simpletr64/actions/wifi.py | Wifi.createFromURL | def createFromURL(urlOfXMLDefinition):
"""Factory method to create a DeviceTR64 from an URL to the XML device definitions.
:param str urlOfXMLDefinition:
:return: the new object
:rtype: Wifi
"""
url = urlparse(urlOfXMLDefinition)
if not url.port:
if url.scheme.lower() == "https":
port = 443
else:
port = 80
else:
port = url.port
return Wifi(url.hostname, port, url.scheme) | python | def createFromURL(urlOfXMLDefinition):
"""Factory method to create a DeviceTR64 from an URL to the XML device definitions.
:param str urlOfXMLDefinition:
:return: the new object
:rtype: Wifi
"""
url = urlparse(urlOfXMLDefinition)
if not url.port:
if url.scheme.lower() == "https":
port = 443
else:
port = 80
else:
port = url.port
return Wifi(url.hostname, port, url.scheme) | [
"def",
"createFromURL",
"(",
"urlOfXMLDefinition",
")",
":",
"url",
"=",
"urlparse",
"(",
"urlOfXMLDefinition",
")",
"if",
"not",
"url",
".",
"port",
":",
"if",
"url",
".",
"scheme",
".",
"lower",
"(",
")",
"==",
"\"https\"",
":",
"port",
"=",
"443",
"... | Factory method to create a DeviceTR64 from an URL to the XML device definitions.
:param str urlOfXMLDefinition:
:return: the new object
:rtype: Wifi | [
"Factory",
"method",
"to",
"create",
"a",
"DeviceTR64",
"from",
"an",
"URL",
"to",
"the",
"XML",
"device",
"definitions",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/actions/wifi.py#L62-L79 | train | 51,339 |
bpannier/simpletr64 | simpletr64/actions/wifi.py | Wifi.getWifiInfo | def getWifiInfo(self, wifiInterfaceId=1, timeout=1):
"""Execute GetInfo action to get Wifi basic information's.
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the basic informations
:rtype: WifiBasicInfo
"""
namespace = Wifi.getServiceType("getWifiInfo") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
results = self.execute(uri, namespace, "GetInfo", timeout=timeout)
return WifiBasicInfo(results) | python | def getWifiInfo(self, wifiInterfaceId=1, timeout=1):
"""Execute GetInfo action to get Wifi basic information's.
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the basic informations
:rtype: WifiBasicInfo
"""
namespace = Wifi.getServiceType("getWifiInfo") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
results = self.execute(uri, namespace, "GetInfo", timeout=timeout)
return WifiBasicInfo(results) | [
"def",
"getWifiInfo",
"(",
"self",
",",
"wifiInterfaceId",
"=",
"1",
",",
"timeout",
"=",
"1",
")",
":",
"namespace",
"=",
"Wifi",
".",
"getServiceType",
"(",
"\"getWifiInfo\"",
")",
"+",
"str",
"(",
"wifiInterfaceId",
")",
"uri",
"=",
"self",
".",
"getC... | Execute GetInfo action to get Wifi basic information's.
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the basic informations
:rtype: WifiBasicInfo | [
"Execute",
"GetInfo",
"action",
"to",
"get",
"Wifi",
"basic",
"information",
"s",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/actions/wifi.py#L93-L106 | train | 51,340 |
bpannier/simpletr64 | simpletr64/actions/wifi.py | Wifi.getTotalAssociations | def getTotalAssociations(self, wifiInterfaceId=1, timeout=1):
"""Execute GetTotalAssociations action to get the amount of associated Wifi clients.
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the amount of Wifi clients
:rtype: int
.. seealso:: :meth:`~simpletr64.actions.Wifi.getGenericAssociatedDeviceInfo`
"""
namespace = Wifi.getServiceType("getTotalAssociations") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
results = self.execute(uri, namespace, "GetTotalAssociations", timeout=timeout)
return int(results["NewTotalAssociations"]) | python | def getTotalAssociations(self, wifiInterfaceId=1, timeout=1):
"""Execute GetTotalAssociations action to get the amount of associated Wifi clients.
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the amount of Wifi clients
:rtype: int
.. seealso:: :meth:`~simpletr64.actions.Wifi.getGenericAssociatedDeviceInfo`
"""
namespace = Wifi.getServiceType("getTotalAssociations") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
results = self.execute(uri, namespace, "GetTotalAssociations", timeout=timeout)
return int(results["NewTotalAssociations"]) | [
"def",
"getTotalAssociations",
"(",
"self",
",",
"wifiInterfaceId",
"=",
"1",
",",
"timeout",
"=",
"1",
")",
":",
"namespace",
"=",
"Wifi",
".",
"getServiceType",
"(",
"\"getTotalAssociations\"",
")",
"+",
"str",
"(",
"wifiInterfaceId",
")",
"uri",
"=",
"sel... | Execute GetTotalAssociations action to get the amount of associated Wifi clients.
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the amount of Wifi clients
:rtype: int
.. seealso:: :meth:`~simpletr64.actions.Wifi.getGenericAssociatedDeviceInfo` | [
"Execute",
"GetTotalAssociations",
"action",
"to",
"get",
"the",
"amount",
"of",
"associated",
"Wifi",
"clients",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/actions/wifi.py#L138-L153 | train | 51,341 |
bpannier/simpletr64 | simpletr64/actions/wifi.py | Wifi.getGenericAssociatedDeviceInfo | def getGenericAssociatedDeviceInfo(self, index, wifiInterfaceId=1, timeout=1):
"""Execute GetGenericAssociatedDeviceInfo action to get detailed information about a Wifi client.
:param int index: the number of the client
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the detailed information's about a Wifi client
:rtype: WifiDeviceInfo
.. seealso:: :meth:`~simpletr64.actions.Wifi.getTotalAssociations`
"""
namespace = Wifi.getServiceType("getGenericAssociatedDeviceInfo") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
results = self.execute(uri, namespace, "GetGenericAssociatedDeviceInfo", timeout=timeout,
NewAssociatedDeviceIndex=index)
return WifiDeviceInfo(results) | python | def getGenericAssociatedDeviceInfo(self, index, wifiInterfaceId=1, timeout=1):
"""Execute GetGenericAssociatedDeviceInfo action to get detailed information about a Wifi client.
:param int index: the number of the client
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the detailed information's about a Wifi client
:rtype: WifiDeviceInfo
.. seealso:: :meth:`~simpletr64.actions.Wifi.getTotalAssociations`
"""
namespace = Wifi.getServiceType("getGenericAssociatedDeviceInfo") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
results = self.execute(uri, namespace, "GetGenericAssociatedDeviceInfo", timeout=timeout,
NewAssociatedDeviceIndex=index)
return WifiDeviceInfo(results) | [
"def",
"getGenericAssociatedDeviceInfo",
"(",
"self",
",",
"index",
",",
"wifiInterfaceId",
"=",
"1",
",",
"timeout",
"=",
"1",
")",
":",
"namespace",
"=",
"Wifi",
".",
"getServiceType",
"(",
"\"getGenericAssociatedDeviceInfo\"",
")",
"+",
"str",
"(",
"wifiInter... | Execute GetGenericAssociatedDeviceInfo action to get detailed information about a Wifi client.
:param int index: the number of the client
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the detailed information's about a Wifi client
:rtype: WifiDeviceInfo
.. seealso:: :meth:`~simpletr64.actions.Wifi.getTotalAssociations` | [
"Execute",
"GetGenericAssociatedDeviceInfo",
"action",
"to",
"get",
"detailed",
"information",
"about",
"a",
"Wifi",
"client",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/actions/wifi.py#L155-L172 | train | 51,342 |
bpannier/simpletr64 | simpletr64/actions/wifi.py | Wifi.getSpecificAssociatedDeviceInfo | def getSpecificAssociatedDeviceInfo(self, macAddress, wifiInterfaceId=1, timeout=1):
"""Execute GetSpecificAssociatedDeviceInfo action to get detailed information about a Wifi client.
:param str macAddress: MAC address in the form ``38:C9:86:26:7E:38``; be aware that the MAC address might
be case sensitive, depending on the router
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the detailed information's about a Wifi client
:rtype: WifiDeviceInfo
.. seealso:: :meth:`~simpletr64.actions.Wifi.getGenericAssociatedDeviceInfo`
"""
namespace = Wifi.getServiceType("getSpecificAssociatedDeviceInfo") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
results = self.execute(uri, namespace, "GetSpecificAssociatedDeviceInfo", timeout=timeout,
NewAssociatedDeviceMACAddress=macAddress)
return WifiDeviceInfo(results, macAddress=macAddress) | python | def getSpecificAssociatedDeviceInfo(self, macAddress, wifiInterfaceId=1, timeout=1):
"""Execute GetSpecificAssociatedDeviceInfo action to get detailed information about a Wifi client.
:param str macAddress: MAC address in the form ``38:C9:86:26:7E:38``; be aware that the MAC address might
be case sensitive, depending on the router
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the detailed information's about a Wifi client
:rtype: WifiDeviceInfo
.. seealso:: :meth:`~simpletr64.actions.Wifi.getGenericAssociatedDeviceInfo`
"""
namespace = Wifi.getServiceType("getSpecificAssociatedDeviceInfo") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
results = self.execute(uri, namespace, "GetSpecificAssociatedDeviceInfo", timeout=timeout,
NewAssociatedDeviceMACAddress=macAddress)
return WifiDeviceInfo(results, macAddress=macAddress) | [
"def",
"getSpecificAssociatedDeviceInfo",
"(",
"self",
",",
"macAddress",
",",
"wifiInterfaceId",
"=",
"1",
",",
"timeout",
"=",
"1",
")",
":",
"namespace",
"=",
"Wifi",
".",
"getServiceType",
"(",
"\"getSpecificAssociatedDeviceInfo\"",
")",
"+",
"str",
"(",
"wi... | Execute GetSpecificAssociatedDeviceInfo action to get detailed information about a Wifi client.
:param str macAddress: MAC address in the form ``38:C9:86:26:7E:38``; be aware that the MAC address might
be case sensitive, depending on the router
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
:return: the detailed information's about a Wifi client
:rtype: WifiDeviceInfo
.. seealso:: :meth:`~simpletr64.actions.Wifi.getGenericAssociatedDeviceInfo` | [
"Execute",
"GetSpecificAssociatedDeviceInfo",
"action",
"to",
"get",
"detailed",
"information",
"about",
"a",
"Wifi",
"client",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/actions/wifi.py#L174-L192 | train | 51,343 |
bpannier/simpletr64 | simpletr64/actions/wifi.py | Wifi.setEnable | def setEnable(self, status, wifiInterfaceId=1, timeout=1):
"""Set enable status for a Wifi interface, be careful you don't cut yourself off.
:param bool status: enable or disable the interface
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
"""
namespace = Wifi.getServiceType("setEnable") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
if status:
setStatus = 1
else:
setStatus = 0
self.execute(uri, namespace, "SetEnable", timeout=timeout, NewEnable=setStatus) | python | def setEnable(self, status, wifiInterfaceId=1, timeout=1):
"""Set enable status for a Wifi interface, be careful you don't cut yourself off.
:param bool status: enable or disable the interface
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
"""
namespace = Wifi.getServiceType("setEnable") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
if status:
setStatus = 1
else:
setStatus = 0
self.execute(uri, namespace, "SetEnable", timeout=timeout, NewEnable=setStatus) | [
"def",
"setEnable",
"(",
"self",
",",
"status",
",",
"wifiInterfaceId",
"=",
"1",
",",
"timeout",
"=",
"1",
")",
":",
"namespace",
"=",
"Wifi",
".",
"getServiceType",
"(",
"\"setEnable\"",
")",
"+",
"str",
"(",
"wifiInterfaceId",
")",
"uri",
"=",
"self",... | Set enable status for a Wifi interface, be careful you don't cut yourself off.
:param bool status: enable or disable the interface
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed | [
"Set",
"enable",
"status",
"for",
"a",
"Wifi",
"interface",
"be",
"careful",
"you",
"don",
"t",
"cut",
"yourself",
"off",
"."
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/actions/wifi.py#L194-L209 | train | 51,344 |
bpannier/simpletr64 | simpletr64/actions/wifi.py | Wifi.setChannel | def setChannel(self, channel, wifiInterfaceId=1, timeout=1):
"""Set the channel of this Wifi interface
:param int channel: the channel number
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
"""
namespace = Wifi.getServiceType("setChannel") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
self.execute(uri, namespace, "SetChannel", timeout=timeout, NewChannel=channel) | python | def setChannel(self, channel, wifiInterfaceId=1, timeout=1):
"""Set the channel of this Wifi interface
:param int channel: the channel number
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed
"""
namespace = Wifi.getServiceType("setChannel") + str(wifiInterfaceId)
uri = self.getControlURL(namespace)
self.execute(uri, namespace, "SetChannel", timeout=timeout, NewChannel=channel) | [
"def",
"setChannel",
"(",
"self",
",",
"channel",
",",
"wifiInterfaceId",
"=",
"1",
",",
"timeout",
"=",
"1",
")",
":",
"namespace",
"=",
"Wifi",
".",
"getServiceType",
"(",
"\"setChannel\"",
")",
"+",
"str",
"(",
"wifiInterfaceId",
")",
"uri",
"=",
"sel... | Set the channel of this Wifi interface
:param int channel: the channel number
:param int wifiInterfaceId: the id of the Wifi interface
:param float timeout: the timeout to wait for the action to be executed | [
"Set",
"the",
"channel",
"of",
"this",
"Wifi",
"interface"
] | 31081139f4e6c85084a56de1617df73927135466 | https://github.com/bpannier/simpletr64/blob/31081139f4e6c85084a56de1617df73927135466/simpletr64/actions/wifi.py#L211-L221 | train | 51,345 |
DreamLab/VmShepherd | src/vmshepherd/http/rpc_api.py | RpcApi.enabled_checker | def enabled_checker(func):
""" Access decorator which checks if a RPC method is enabled by our configuration
"""
@wraps(func)
def wrap(self, *args, **kwargs):
if self.allowed_methods and isinstance(self.allowed_methods, list) and func.__name__ not in self.allowed_methods:
raise Exception("Method {} is disabled".format(func.__name__))
return func(self, *args, **kwargs)
return wrap | python | def enabled_checker(func):
""" Access decorator which checks if a RPC method is enabled by our configuration
"""
@wraps(func)
def wrap(self, *args, **kwargs):
if self.allowed_methods and isinstance(self.allowed_methods, list) and func.__name__ not in self.allowed_methods:
raise Exception("Method {} is disabled".format(func.__name__))
return func(self, *args, **kwargs)
return wrap | [
"def",
"enabled_checker",
"(",
"func",
")",
":",
"@",
"wraps",
"(",
"func",
")",
"def",
"wrap",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"self",
".",
"allowed_methods",
"and",
"isinstance",
"(",
"self",
".",
"allowed_me... | Access decorator which checks if a RPC method is enabled by our configuration | [
"Access",
"decorator",
"which",
"checks",
"if",
"a",
"RPC",
"method",
"is",
"enabled",
"by",
"our",
"configuration"
] | 709a412c372b897d53808039c5c64a8b69c12c8d | https://github.com/DreamLab/VmShepherd/blob/709a412c372b897d53808039c5c64a8b69c12c8d/src/vmshepherd/http/rpc_api.py#L18-L26 | train | 51,346 |
DreamLab/VmShepherd | src/vmshepherd/http/rpc_api.py | RpcApi.list_vms | async def list_vms(self, preset):
"""
Listing virtual machines in a given preset
:arg string preset: preset name
:return: (Size of a preset, list of virtual machines)
- first element of a tuple is a size of virtual machines in a preset
- second element is a dict which contains all Virtual Machines, where every element of this dict looks like that:
``{ "VIRTUAL_MACHINE_ID": { "ip": "IP_ADDR", "state": "VM_STATE" }``
:rtype: tuple
Sample response:
``( 1, {'180aa486-ee46-4628-ab1c-f4554b63231': {'ip': '172.1.1.2', 'state': 'running'}} )``
"""
vmshepherd = self.request.app.vmshepherd
preset = vmshepherd.preset_manager.get_preset(preset)
result_vms = {vm.id: {'ip': vm.ip[0], 'state': vm.state.value} for vm in preset.vms}
return preset.count, result_vms | python | async def list_vms(self, preset):
"""
Listing virtual machines in a given preset
:arg string preset: preset name
:return: (Size of a preset, list of virtual machines)
- first element of a tuple is a size of virtual machines in a preset
- second element is a dict which contains all Virtual Machines, where every element of this dict looks like that:
``{ "VIRTUAL_MACHINE_ID": { "ip": "IP_ADDR", "state": "VM_STATE" }``
:rtype: tuple
Sample response:
``( 1, {'180aa486-ee46-4628-ab1c-f4554b63231': {'ip': '172.1.1.2', 'state': 'running'}} )``
"""
vmshepherd = self.request.app.vmshepherd
preset = vmshepherd.preset_manager.get_preset(preset)
result_vms = {vm.id: {'ip': vm.ip[0], 'state': vm.state.value} for vm in preset.vms}
return preset.count, result_vms | [
"async",
"def",
"list_vms",
"(",
"self",
",",
"preset",
")",
":",
"vmshepherd",
"=",
"self",
".",
"request",
".",
"app",
".",
"vmshepherd",
"preset",
"=",
"vmshepherd",
".",
"preset_manager",
".",
"get_preset",
"(",
"preset",
")",
"result_vms",
"=",
"{",
... | Listing virtual machines in a given preset
:arg string preset: preset name
:return: (Size of a preset, list of virtual machines)
- first element of a tuple is a size of virtual machines in a preset
- second element is a dict which contains all Virtual Machines, where every element of this dict looks like that:
``{ "VIRTUAL_MACHINE_ID": { "ip": "IP_ADDR", "state": "VM_STATE" }``
:rtype: tuple
Sample response:
``( 1, {'180aa486-ee46-4628-ab1c-f4554b63231': {'ip': '172.1.1.2', 'state': 'running'}} )`` | [
"Listing",
"virtual",
"machines",
"in",
"a",
"given",
"preset"
] | 709a412c372b897d53808039c5c64a8b69c12c8d | https://github.com/DreamLab/VmShepherd/blob/709a412c372b897d53808039c5c64a8b69c12c8d/src/vmshepherd/http/rpc_api.py#L44-L64 | train | 51,347 |
DreamLab/VmShepherd | src/vmshepherd/http/rpc_api.py | RpcApi.terminate_vm | async def terminate_vm(self, preset, vm_id):
""" Discard vm in specified preset
:arg string preset: preset name
:arg int vm_id: Virtual Machine id
:return: 'OK'
Sample response:
``OK``
"""
vmshepherd = self.request.app.vmshepherd
preset = vmshepherd.preset_manager.get_preset(preset)
await preset.iaas.terminate_vm(vm_id)
return 'OK' | python | async def terminate_vm(self, preset, vm_id):
""" Discard vm in specified preset
:arg string preset: preset name
:arg int vm_id: Virtual Machine id
:return: 'OK'
Sample response:
``OK``
"""
vmshepherd = self.request.app.vmshepherd
preset = vmshepherd.preset_manager.get_preset(preset)
await preset.iaas.terminate_vm(vm_id)
return 'OK' | [
"async",
"def",
"terminate_vm",
"(",
"self",
",",
"preset",
",",
"vm_id",
")",
":",
"vmshepherd",
"=",
"self",
".",
"request",
".",
"app",
".",
"vmshepherd",
"preset",
"=",
"vmshepherd",
".",
"preset_manager",
".",
"get_preset",
"(",
"preset",
")",
"await"... | Discard vm in specified preset
:arg string preset: preset name
:arg int vm_id: Virtual Machine id
:return: 'OK'
Sample response:
``OK`` | [
"Discard",
"vm",
"in",
"specified",
"preset"
] | 709a412c372b897d53808039c5c64a8b69c12c8d | https://github.com/DreamLab/VmShepherd/blob/709a412c372b897d53808039c5c64a8b69c12c8d/src/vmshepherd/http/rpc_api.py#L67-L80 | train | 51,348 |
DreamLab/VmShepherd | src/vmshepherd/http/rpc_api.py | RpcApi.get_vm_metadata | async def get_vm_metadata(self, preset, vm_id):
""" Get vm metadata
:arg string preset: preset name
:arg int vm_id: Virtual Machine id
:return: Metadata for Virtual Machine
:rtype: dict
Sample response:
``{ 'time_shutdown' : "12312312321' }``
"""
vmshepherd = self.request.app.vmshepherd
preset = vmshepherd.preset_manager.get_preset(preset)
vm_info = await preset.iaas.get_vm(vm_id)
ret_info = copy.deepcopy(vm_info.metadata) if vm_info.metadata else {}
ret_info['tags'] = vm_info.tags
ret_info['iaas_shutdown'] = vm_info.timed_shutdown_at
return ret_info | python | async def get_vm_metadata(self, preset, vm_id):
""" Get vm metadata
:arg string preset: preset name
:arg int vm_id: Virtual Machine id
:return: Metadata for Virtual Machine
:rtype: dict
Sample response:
``{ 'time_shutdown' : "12312312321' }``
"""
vmshepherd = self.request.app.vmshepherd
preset = vmshepherd.preset_manager.get_preset(preset)
vm_info = await preset.iaas.get_vm(vm_id)
ret_info = copy.deepcopy(vm_info.metadata) if vm_info.metadata else {}
ret_info['tags'] = vm_info.tags
ret_info['iaas_shutdown'] = vm_info.timed_shutdown_at
return ret_info | [
"async",
"def",
"get_vm_metadata",
"(",
"self",
",",
"preset",
",",
"vm_id",
")",
":",
"vmshepherd",
"=",
"self",
".",
"request",
".",
"app",
".",
"vmshepherd",
"preset",
"=",
"vmshepherd",
".",
"preset_manager",
".",
"get_preset",
"(",
"preset",
")",
"vm_... | Get vm metadata
:arg string preset: preset name
:arg int vm_id: Virtual Machine id
:return: Metadata for Virtual Machine
:rtype: dict
Sample response:
``{ 'time_shutdown' : "12312312321' }`` | [
"Get",
"vm",
"metadata"
] | 709a412c372b897d53808039c5c64a8b69c12c8d | https://github.com/DreamLab/VmShepherd/blob/709a412c372b897d53808039c5c64a8b69c12c8d/src/vmshepherd/http/rpc_api.py#L83-L100 | train | 51,349 |
Suor/autolink | autolink.py | linkify | def linkify(text, attrs={}):
"""
Convert URL-like and email-like strings into links.
"""
def separate_parentheses(s):
start = re_find(r'^\(*', s)
end = re_find(r'\)*$', s)
n = min(len(start), len(end))
if n:
return s[:n], s[n:-n], s[-n:]
else:
return '', s, ''
def link_repl(url, proto='http://'):
opening, url, closing = separate_parentheses(url)
punct = re_find(punct_re, url)
if punct:
url = url[:-len(punct)]
if re.search(proto_re, url):
href = url
else:
href = proto + url
href = escape_url(href)
repl = u'{0!s}<a href="{1!s}"{2!s}>{3!s}</a>{4!s}{5!s}'
return repl.format(opening,
href, attrs_text, url, punct,
closing)
def repl(match):
matches = match.groupdict()
if matches['url']:
return link_repl(matches['url'])
else:
return link_repl(matches['email'], proto='mailto:')
# Prepare attrs
attr = ' {0!s}="{1!s}"'
attrs_text = ''.join(starmap(attr.format, attrs.items()))
# Make replaces
return re.sub(combined_re, repl, force_unicode(text)) | python | def linkify(text, attrs={}):
"""
Convert URL-like and email-like strings into links.
"""
def separate_parentheses(s):
start = re_find(r'^\(*', s)
end = re_find(r'\)*$', s)
n = min(len(start), len(end))
if n:
return s[:n], s[n:-n], s[-n:]
else:
return '', s, ''
def link_repl(url, proto='http://'):
opening, url, closing = separate_parentheses(url)
punct = re_find(punct_re, url)
if punct:
url = url[:-len(punct)]
if re.search(proto_re, url):
href = url
else:
href = proto + url
href = escape_url(href)
repl = u'{0!s}<a href="{1!s}"{2!s}>{3!s}</a>{4!s}{5!s}'
return repl.format(opening,
href, attrs_text, url, punct,
closing)
def repl(match):
matches = match.groupdict()
if matches['url']:
return link_repl(matches['url'])
else:
return link_repl(matches['email'], proto='mailto:')
# Prepare attrs
attr = ' {0!s}="{1!s}"'
attrs_text = ''.join(starmap(attr.format, attrs.items()))
# Make replaces
return re.sub(combined_re, repl, force_unicode(text)) | [
"def",
"linkify",
"(",
"text",
",",
"attrs",
"=",
"{",
"}",
")",
":",
"def",
"separate_parentheses",
"(",
"s",
")",
":",
"start",
"=",
"re_find",
"(",
"r'^\\(*'",
",",
"s",
")",
"end",
"=",
"re_find",
"(",
"r'\\)*$'",
",",
"s",
")",
"n",
"=",
"mi... | Convert URL-like and email-like strings into links. | [
"Convert",
"URL",
"-",
"like",
"and",
"email",
"-",
"like",
"strings",
"into",
"links",
"."
] | 0a101e6fb6359ae18fce1b9f8907ff9113a6086f | https://github.com/Suor/autolink/blob/0a101e6fb6359ae18fce1b9f8907ff9113a6086f/autolink.py#L53-L96 | train | 51,350 |
launchdarkly/relayCommander | relay_commander/ld.py | LaunchDarklyApi.get_environments | def get_environments(self, project_key: str) -> dict:
"""
Retrieve all environments for a given project.
Includes name, key, and mobile key.
:param project_key: Key for project.
:returns: dictionary of environments.
"""
try:
resp = self.client.get_project(project_key)
except launchdarkly_api.rest.ApiException as ex:
msg = "Unable to get environments."
resp = "API response was {0} {1}.".format(ex.status, ex.reason)
LOG.error("%s %s", msg, resp)
sys.exit(1)
envs = []
for env in resp.environments:
env = dict(
key=env.key,
api_key=env.api_key,
client_id=env.id
)
envs.append(env)
return envs | python | def get_environments(self, project_key: str) -> dict:
"""
Retrieve all environments for a given project.
Includes name, key, and mobile key.
:param project_key: Key for project.
:returns: dictionary of environments.
"""
try:
resp = self.client.get_project(project_key)
except launchdarkly_api.rest.ApiException as ex:
msg = "Unable to get environments."
resp = "API response was {0} {1}.".format(ex.status, ex.reason)
LOG.error("%s %s", msg, resp)
sys.exit(1)
envs = []
for env in resp.environments:
env = dict(
key=env.key,
api_key=env.api_key,
client_id=env.id
)
envs.append(env)
return envs | [
"def",
"get_environments",
"(",
"self",
",",
"project_key",
":",
"str",
")",
"->",
"dict",
":",
"try",
":",
"resp",
"=",
"self",
".",
"client",
".",
"get_project",
"(",
"project_key",
")",
"except",
"launchdarkly_api",
".",
"rest",
".",
"ApiException",
"as... | Retrieve all environments for a given project.
Includes name, key, and mobile key.
:param project_key: Key for project.
:returns: dictionary of environments. | [
"Retrieve",
"all",
"environments",
"for",
"a",
"given",
"project",
"."
] | eee7fa22f04edc3854dd53c3ec2db8c599ad1e89 | https://github.com/launchdarkly/relayCommander/blob/eee7fa22f04edc3854dd53c3ec2db8c599ad1e89/relay_commander/ld.py#L50-L78 | train | 51,351 |
launchdarkly/relayCommander | relay_commander/ld.py | LaunchDarklyApi.update_flag | def update_flag(self, state: str, feature_key: str) \
-> launchdarkly_api.FeatureFlag:
"""
Update the flag status for the specified feature flag.
:param state: New feature flag state
:param featureKey: Feature flag key
:returns: FeatureFlag object.
"""
build_env = "/environments/" + self.environment_key + "/on"
patch_comment = [{"op": "replace", "path": build_env, "value": state}]
try:
resp = self.feature.patch_feature_flag(
self.project_key, feature_key, patch_comment)
except launchdarkly_api.rest.ApiException as ex:
msg = "Unable to update flag."
resp = "API response was {0} {1}.".format(ex.status, ex.reason)
LOG.error("%s %s", msg, resp)
sys.exit(1)
return resp | python | def update_flag(self, state: str, feature_key: str) \
-> launchdarkly_api.FeatureFlag:
"""
Update the flag status for the specified feature flag.
:param state: New feature flag state
:param featureKey: Feature flag key
:returns: FeatureFlag object.
"""
build_env = "/environments/" + self.environment_key + "/on"
patch_comment = [{"op": "replace", "path": build_env, "value": state}]
try:
resp = self.feature.patch_feature_flag(
self.project_key, feature_key, patch_comment)
except launchdarkly_api.rest.ApiException as ex:
msg = "Unable to update flag."
resp = "API response was {0} {1}.".format(ex.status, ex.reason)
LOG.error("%s %s", msg, resp)
sys.exit(1)
return resp | [
"def",
"update_flag",
"(",
"self",
",",
"state",
":",
"str",
",",
"feature_key",
":",
"str",
")",
"->",
"launchdarkly_api",
".",
"FeatureFlag",
":",
"build_env",
"=",
"\"/environments/\"",
"+",
"self",
".",
"environment_key",
"+",
"\"/on\"",
"patch_comment",
"... | Update the flag status for the specified feature flag.
:param state: New feature flag state
:param featureKey: Feature flag key
:returns: FeatureFlag object. | [
"Update",
"the",
"flag",
"status",
"for",
"the",
"specified",
"feature",
"flag",
"."
] | eee7fa22f04edc3854dd53c3ec2db8c599ad1e89 | https://github.com/launchdarkly/relayCommander/blob/eee7fa22f04edc3854dd53c3ec2db8c599ad1e89/relay_commander/ld.py#L80-L102 | train | 51,352 |
SetBased/py-stratum | pystratum/wrapper/RowsWithKeyWrapper.py | RowsWithKeyWrapper._write_result_handler | def _write_result_handler(self, routine):
"""
Generates code for calling the stored routine in the wrapper method.
"""
self._write_line('ret = {}')
self._write_execute_rows(routine)
self._write_line('for row in rows:')
num_of_dict = len(routine['columns'])
i = 0
while i < num_of_dict:
value = "row['{0!s}']".format(routine['columns'][i])
stack = ''
j = 0
while j < i:
stack += "[row['{0!s}']]".format(routine['columns'][j])
j += 1
line = 'if {0!s} in ret{1!s}:'.format(value, stack)
self._write_line(line)
i += 1
line = "raise Exception('Duplicate key for %s.' % str(({0!s})))". \
format(", ".join(["row['{0!s}']".format(column_name) for column_name in routine['columns']]))
self._write_line(line)
self._indent_level_down()
i = num_of_dict
while i > 0:
self._write_line('else:')
part1 = ''
j = 0
while j < i - 1:
part1 += "[row['{0!s}']]".format(routine['columns'][j])
j += 1
part1 += "[row['{0!s}']]".format(routine['columns'][j])
part2 = ''
j = i - 1
while j < num_of_dict:
if j + 1 != i:
part2 += "{{row['{0!s}']: ".format(routine['columns'][j])
j += 1
part2 += "row" + ('}' * (num_of_dict - i))
line = "ret{0!s} = {1!s}".format(part1, part2)
self._write_line(line)
self._indent_level_down()
if i > 1:
self._indent_level_down()
i -= 1
self._write_line()
self._write_line('return ret') | python | def _write_result_handler(self, routine):
"""
Generates code for calling the stored routine in the wrapper method.
"""
self._write_line('ret = {}')
self._write_execute_rows(routine)
self._write_line('for row in rows:')
num_of_dict = len(routine['columns'])
i = 0
while i < num_of_dict:
value = "row['{0!s}']".format(routine['columns'][i])
stack = ''
j = 0
while j < i:
stack += "[row['{0!s}']]".format(routine['columns'][j])
j += 1
line = 'if {0!s} in ret{1!s}:'.format(value, stack)
self._write_line(line)
i += 1
line = "raise Exception('Duplicate key for %s.' % str(({0!s})))". \
format(", ".join(["row['{0!s}']".format(column_name) for column_name in routine['columns']]))
self._write_line(line)
self._indent_level_down()
i = num_of_dict
while i > 0:
self._write_line('else:')
part1 = ''
j = 0
while j < i - 1:
part1 += "[row['{0!s}']]".format(routine['columns'][j])
j += 1
part1 += "[row['{0!s}']]".format(routine['columns'][j])
part2 = ''
j = i - 1
while j < num_of_dict:
if j + 1 != i:
part2 += "{{row['{0!s}']: ".format(routine['columns'][j])
j += 1
part2 += "row" + ('}' * (num_of_dict - i))
line = "ret{0!s} = {1!s}".format(part1, part2)
self._write_line(line)
self._indent_level_down()
if i > 1:
self._indent_level_down()
i -= 1
self._write_line()
self._write_line('return ret') | [
"def",
"_write_result_handler",
"(",
"self",
",",
"routine",
")",
":",
"self",
".",
"_write_line",
"(",
"'ret = {}'",
")",
"self",
".",
"_write_execute_rows",
"(",
"routine",
")",
"self",
".",
"_write_line",
"(",
"'for row in rows:'",
")",
"num_of_dict",
"=",
... | Generates code for calling the stored routine in the wrapper method. | [
"Generates",
"code",
"for",
"calling",
"the",
"stored",
"routine",
"in",
"the",
"wrapper",
"method",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/wrapper/RowsWithKeyWrapper.py#L30-L86 | train | 51,353 |
SetBased/py-stratum | pystratum/MetadataDataLayer.py | MetadataDataLayer._log_query | def _log_query(query):
"""
Logs the query on the console.
:param str query: The query.
"""
query = query.strip()
if os.linesep in query:
# Query is a multi line query
MetadataDataLayer.io.log_very_verbose('Executing query:')
MetadataDataLayer.io.log_very_verbose('<sql>{0}</sql>'.format(query))
else:
# Query is a single line query.
MetadataDataLayer.io.log_very_verbose('Executing query: <sql>{0}</sql>'.format(query)) | python | def _log_query(query):
"""
Logs the query on the console.
:param str query: The query.
"""
query = query.strip()
if os.linesep in query:
# Query is a multi line query
MetadataDataLayer.io.log_very_verbose('Executing query:')
MetadataDataLayer.io.log_very_verbose('<sql>{0}</sql>'.format(query))
else:
# Query is a single line query.
MetadataDataLayer.io.log_very_verbose('Executing query: <sql>{0}</sql>'.format(query)) | [
"def",
"_log_query",
"(",
"query",
")",
":",
"query",
"=",
"query",
".",
"strip",
"(",
")",
"if",
"os",
".",
"linesep",
"in",
"query",
":",
"# Query is a multi line query",
"MetadataDataLayer",
".",
"io",
".",
"log_very_verbose",
"(",
"'Executing query:'",
")"... | Logs the query on the console.
:param str query: The query. | [
"Logs",
"the",
"query",
"on",
"the",
"console",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/MetadataDataLayer.py#L20-L34 | train | 51,354 |
vsoch/helpme | helpme/main/discourse/utils.py | request_token | def request_token(self, board):
'''send a public key to request a token. When we call this function,
we already have an RSA key at self.key
board: the discourse board to post to
'''
nonce = str(uuid.uuid4())
data = {'scopes': 'write',
'client_id': self.client_id,
'application_name': 'HelpMe',
'public_key': self.public_key.replace("'",""),
'nonce': nonce }
url = (board + "/user-api-key/new?scopes=write&application_name=HelpMe&public_key=" +
self.public_key.replace("'", "") +
"&client_id=" + self.client_id +
"&nonce=" + nonce )
bot.newline()
bot.info('Open browser to:')
bot.info(url)
bot.newline()
# the user will open browser, get a token, and then have it saved here.
bot.info('Copy paste token, press Ctrl-D to save it:')
lines = []
# The message is multiple lines
while True:
try:
line = enter_input()
except EOFError:
break
if line:
lines.append(line)
message = "\n".join(lines)
# Write to temporary file, we only need to get key
tmpfile = mktemp()
with open(tmpfile, 'w') as filey:
filey.write(message)
# Read in again, and get token **important** is binary
with open(tmpfile, 'rb') as filey:
message = filey.read()
# uses pycryptodome (3.7.2)
cipher = Cipher_PKCS1_v1_5.new(self.key)
decrypted = json.loads(cipher.decrypt(b64decode(message), None).decode())
# Validate nonce is in response
if "nonce" not in decrypted:
bot.exit('Missing nonce field in response for token, invalid.')
# Must return nonce that we sent
if decrypted['nonce'] != nonce:
bot.exit('Invalid nonce, exiting.')
return decrypted['key'] | python | def request_token(self, board):
'''send a public key to request a token. When we call this function,
we already have an RSA key at self.key
board: the discourse board to post to
'''
nonce = str(uuid.uuid4())
data = {'scopes': 'write',
'client_id': self.client_id,
'application_name': 'HelpMe',
'public_key': self.public_key.replace("'",""),
'nonce': nonce }
url = (board + "/user-api-key/new?scopes=write&application_name=HelpMe&public_key=" +
self.public_key.replace("'", "") +
"&client_id=" + self.client_id +
"&nonce=" + nonce )
bot.newline()
bot.info('Open browser to:')
bot.info(url)
bot.newline()
# the user will open browser, get a token, and then have it saved here.
bot.info('Copy paste token, press Ctrl-D to save it:')
lines = []
# The message is multiple lines
while True:
try:
line = enter_input()
except EOFError:
break
if line:
lines.append(line)
message = "\n".join(lines)
# Write to temporary file, we only need to get key
tmpfile = mktemp()
with open(tmpfile, 'w') as filey:
filey.write(message)
# Read in again, and get token **important** is binary
with open(tmpfile, 'rb') as filey:
message = filey.read()
# uses pycryptodome (3.7.2)
cipher = Cipher_PKCS1_v1_5.new(self.key)
decrypted = json.loads(cipher.decrypt(b64decode(message), None).decode())
# Validate nonce is in response
if "nonce" not in decrypted:
bot.exit('Missing nonce field in response for token, invalid.')
# Must return nonce that we sent
if decrypted['nonce'] != nonce:
bot.exit('Invalid nonce, exiting.')
return decrypted['key'] | [
"def",
"request_token",
"(",
"self",
",",
"board",
")",
":",
"nonce",
"=",
"str",
"(",
"uuid",
".",
"uuid4",
"(",
")",
")",
"data",
"=",
"{",
"'scopes'",
":",
"'write'",
",",
"'client_id'",
":",
"self",
".",
"client_id",
",",
"'application_name'",
":",... | send a public key to request a token. When we call this function,
we already have an RSA key at self.key
board: the discourse board to post to | [
"send",
"a",
"public",
"key",
"to",
"request",
"a",
"token",
".",
"When",
"we",
"call",
"this",
"function",
"we",
"already",
"have",
"an",
"RSA",
"key",
"at",
"self",
".",
"key"
] | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/main/discourse/utils.py#L32-L93 | train | 51,355 |
vsoch/helpme | helpme/main/discourse/utils.py | create_post | def create_post(self, title, body, board, category, username):
'''create a Discourse post, given a title, body, board, and token.
Parameters
==========
title: the issue title
body: the issue body
board: the discourse board to post to
'''
category_url = "%s/categories.json" % board
response = requests.get(category_url)
if response.status_code != 200:
print('Error with retrieving %s' % category_url)
sys.exit(1)
# Get a list of all categories
categories = response.json()['category_list']['categories']
categories = {c['name']:c['id'] for c in categories}
# And if not valid, warn the user
if category not in categories:
bot.warning('%s is not valid, will use default' % category)
category_id = categories.get(category, None)
headers = {"Content-Type": "application/json",
"User-Api-Client-Id": self.client_id,
"User-Api-Key": self.token }
# First get the category ids
data = {'title': title,
'raw': body,
'category': category_id}
response = requests.post("%s/posts.json" % board,
headers=headers,
data=json.dumps(data))
if response.status_code in [200, 201, 202]:
topic = response.json()
url = "%s/t/%s/%s" %(board, topic['topic_slug'], topic['topic_id'])
bot.info(url)
return url
elif response.status_code == 404:
bot.error('Cannot post to board, not found. Do you have permission?')
sys.exit(1)
else:
bot.error('Cannot post to board %s' % board)
bot.error(response.content)
sys.exit(1) | python | def create_post(self, title, body, board, category, username):
'''create a Discourse post, given a title, body, board, and token.
Parameters
==========
title: the issue title
body: the issue body
board: the discourse board to post to
'''
category_url = "%s/categories.json" % board
response = requests.get(category_url)
if response.status_code != 200:
print('Error with retrieving %s' % category_url)
sys.exit(1)
# Get a list of all categories
categories = response.json()['category_list']['categories']
categories = {c['name']:c['id'] for c in categories}
# And if not valid, warn the user
if category not in categories:
bot.warning('%s is not valid, will use default' % category)
category_id = categories.get(category, None)
headers = {"Content-Type": "application/json",
"User-Api-Client-Id": self.client_id,
"User-Api-Key": self.token }
# First get the category ids
data = {'title': title,
'raw': body,
'category': category_id}
response = requests.post("%s/posts.json" % board,
headers=headers,
data=json.dumps(data))
if response.status_code in [200, 201, 202]:
topic = response.json()
url = "%s/t/%s/%s" %(board, topic['topic_slug'], topic['topic_id'])
bot.info(url)
return url
elif response.status_code == 404:
bot.error('Cannot post to board, not found. Do you have permission?')
sys.exit(1)
else:
bot.error('Cannot post to board %s' % board)
bot.error(response.content)
sys.exit(1) | [
"def",
"create_post",
"(",
"self",
",",
"title",
",",
"body",
",",
"board",
",",
"category",
",",
"username",
")",
":",
"category_url",
"=",
"\"%s/categories.json\"",
"%",
"board",
"response",
"=",
"requests",
".",
"get",
"(",
"category_url",
")",
"if",
"r... | create a Discourse post, given a title, body, board, and token.
Parameters
==========
title: the issue title
body: the issue body
board: the discourse board to post to | [
"create",
"a",
"Discourse",
"post",
"given",
"a",
"title",
"body",
"board",
"and",
"token",
"."
] | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/main/discourse/utils.py#L97-L152 | train | 51,356 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader.main | def main(self, config_filename, file_names=None):
"""
Loads stored routines into the current schema.
:param str config_filename: The name of the configuration file of the current project
:param list[str] file_names: The sources that must be loaded. If empty all sources (if required) will loaded.
:rtype: int The status of exit.
"""
self._io.title('Loader')
if file_names:
self.__load_list(config_filename, file_names)
else:
self.__load_all(config_filename)
if self.error_file_names:
self.__log_overview_errors()
return 1
else:
return 0 | python | def main(self, config_filename, file_names=None):
"""
Loads stored routines into the current schema.
:param str config_filename: The name of the configuration file of the current project
:param list[str] file_names: The sources that must be loaded. If empty all sources (if required) will loaded.
:rtype: int The status of exit.
"""
self._io.title('Loader')
if file_names:
self.__load_list(config_filename, file_names)
else:
self.__load_all(config_filename)
if self.error_file_names:
self.__log_overview_errors()
return 1
else:
return 0 | [
"def",
"main",
"(",
"self",
",",
"config_filename",
",",
"file_names",
"=",
"None",
")",
":",
"self",
".",
"_io",
".",
"title",
"(",
"'Loader'",
")",
"if",
"file_names",
":",
"self",
".",
"__load_list",
"(",
"config_filename",
",",
"file_names",
")",
"el... | Loads stored routines into the current schema.
:param str config_filename: The name of the configuration file of the current project
:param list[str] file_names: The sources that must be loaded. If empty all sources (if required) will loaded.
:rtype: int The status of exit. | [
"Loads",
"stored",
"routines",
"into",
"the",
"current",
"schema",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L109-L129 | train | 51,357 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader.__log_overview_errors | def __log_overview_errors(self):
"""
Show info about sources files of stored routines that were not loaded successfully.
"""
if self.error_file_names:
self._io.warning('Routines in the files below are not loaded:')
self._io.listing(sorted(self.error_file_names)) | python | def __log_overview_errors(self):
"""
Show info about sources files of stored routines that were not loaded successfully.
"""
if self.error_file_names:
self._io.warning('Routines in the files below are not loaded:')
self._io.listing(sorted(self.error_file_names)) | [
"def",
"__log_overview_errors",
"(",
"self",
")",
":",
"if",
"self",
".",
"error_file_names",
":",
"self",
".",
"_io",
".",
"warning",
"(",
"'Routines in the files below are not loaded:'",
")",
"self",
".",
"_io",
".",
"listing",
"(",
"sorted",
"(",
"self",
".... | Show info about sources files of stored routines that were not loaded successfully. | [
"Show",
"info",
"about",
"sources",
"files",
"of",
"stored",
"routines",
"that",
"were",
"not",
"loaded",
"successfully",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L132-L138 | train | 51,358 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader._add_replace_pair | def _add_replace_pair(self, name, value, quote):
"""
Adds a replace part to the map of replace pairs.
:param name: The name of the replace pair.
:param value: The value of value of the replace pair.
"""
key = '@' + name + '@'
key = key.lower()
class_name = value.__class__.__name__
if class_name in ['int', 'float']:
value = str(value)
elif class_name in ['bool']:
value = '1' if value else '0'
elif class_name in ['str']:
if quote:
value = "'" + value + "'"
else:
self._io.log_verbose("Ignoring constant {} which is an instance of {}".format(name, class_name))
self._replace_pairs[key] = value | python | def _add_replace_pair(self, name, value, quote):
"""
Adds a replace part to the map of replace pairs.
:param name: The name of the replace pair.
:param value: The value of value of the replace pair.
"""
key = '@' + name + '@'
key = key.lower()
class_name = value.__class__.__name__
if class_name in ['int', 'float']:
value = str(value)
elif class_name in ['bool']:
value = '1' if value else '0'
elif class_name in ['str']:
if quote:
value = "'" + value + "'"
else:
self._io.log_verbose("Ignoring constant {} which is an instance of {}".format(name, class_name))
self._replace_pairs[key] = value | [
"def",
"_add_replace_pair",
"(",
"self",
",",
"name",
",",
"value",
",",
"quote",
")",
":",
"key",
"=",
"'@'",
"+",
"name",
"+",
"'@'",
"key",
"=",
"key",
".",
"lower",
"(",
")",
"class_name",
"=",
"value",
".",
"__class__",
".",
"__name__",
"if",
... | Adds a replace part to the map of replace pairs.
:param name: The name of the replace pair.
:param value: The value of value of the replace pair. | [
"Adds",
"a",
"replace",
"part",
"to",
"the",
"map",
"of",
"replace",
"pairs",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L157-L179 | train | 51,359 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader.__load_list | def __load_list(self, config_filename, file_names):
"""
Loads all stored routines in a list into the RDBMS instance.
:param str config_filename: The filename of the configuration file.
:param list[str] file_names: The list of files to be loaded.
"""
self._read_configuration_file(config_filename)
self.connect()
self.find_source_files_from_list(file_names)
self._get_column_type()
self.__read_stored_routine_metadata()
self.__get_constants()
self._get_old_stored_routine_info()
self._get_correct_sql_mode()
self.__load_stored_routines()
self.__write_stored_routine_metadata()
self.disconnect() | python | def __load_list(self, config_filename, file_names):
"""
Loads all stored routines in a list into the RDBMS instance.
:param str config_filename: The filename of the configuration file.
:param list[str] file_names: The list of files to be loaded.
"""
self._read_configuration_file(config_filename)
self.connect()
self.find_source_files_from_list(file_names)
self._get_column_type()
self.__read_stored_routine_metadata()
self.__get_constants()
self._get_old_stored_routine_info()
self._get_correct_sql_mode()
self.__load_stored_routines()
self.__write_stored_routine_metadata()
self.disconnect() | [
"def",
"__load_list",
"(",
"self",
",",
"config_filename",
",",
"file_names",
")",
":",
"self",
".",
"_read_configuration_file",
"(",
"config_filename",
")",
"self",
".",
"connect",
"(",
")",
"self",
".",
"find_source_files_from_list",
"(",
"file_names",
")",
"s... | Loads all stored routines in a list into the RDBMS instance.
:param str config_filename: The filename of the configuration file.
:param list[str] file_names: The list of files to be loaded. | [
"Loads",
"all",
"stored",
"routines",
"in",
"a",
"list",
"into",
"the",
"RDBMS",
"instance",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L182-L199 | train | 51,360 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader.__find_source_files | def __find_source_files(self):
"""
Searches recursively for all source files in a directory.
"""
for dir_path, _, files in os.walk(self._source_directory):
for name in files:
if name.lower().endswith(self._source_file_extension):
basename = os.path.splitext(os.path.basename(name))[0]
relative_path = os.path.relpath(os.path.join(dir_path, name))
if basename in self._source_file_names:
self._io.error("Files '{0}' and '{1}' have the same basename.".
format(self._source_file_names[basename], relative_path))
self.error_file_names.add(relative_path)
else:
self._source_file_names[basename] = relative_path | python | def __find_source_files(self):
"""
Searches recursively for all source files in a directory.
"""
for dir_path, _, files in os.walk(self._source_directory):
for name in files:
if name.lower().endswith(self._source_file_extension):
basename = os.path.splitext(os.path.basename(name))[0]
relative_path = os.path.relpath(os.path.join(dir_path, name))
if basename in self._source_file_names:
self._io.error("Files '{0}' and '{1}' have the same basename.".
format(self._source_file_names[basename], relative_path))
self.error_file_names.add(relative_path)
else:
self._source_file_names[basename] = relative_path | [
"def",
"__find_source_files",
"(",
"self",
")",
":",
"for",
"dir_path",
",",
"_",
",",
"files",
"in",
"os",
".",
"walk",
"(",
"self",
".",
"_source_directory",
")",
":",
"for",
"name",
"in",
"files",
":",
"if",
"name",
".",
"lower",
"(",
")",
".",
... | Searches recursively for all source files in a directory. | [
"Searches",
"recursively",
"for",
"all",
"source",
"files",
"in",
"a",
"directory",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L242-L257 | train | 51,361 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader.__read_stored_routine_metadata | def __read_stored_routine_metadata(self):
"""
Reads the metadata of stored routines from the metadata file.
"""
if os.path.isfile(self._pystratum_metadata_filename):
with open(self._pystratum_metadata_filename, 'r') as file:
self._pystratum_metadata = json.load(file) | python | def __read_stored_routine_metadata(self):
"""
Reads the metadata of stored routines from the metadata file.
"""
if os.path.isfile(self._pystratum_metadata_filename):
with open(self._pystratum_metadata_filename, 'r') as file:
self._pystratum_metadata = json.load(file) | [
"def",
"__read_stored_routine_metadata",
"(",
"self",
")",
":",
"if",
"os",
".",
"path",
".",
"isfile",
"(",
"self",
".",
"_pystratum_metadata_filename",
")",
":",
"with",
"open",
"(",
"self",
".",
"_pystratum_metadata_filename",
",",
"'r'",
")",
"as",
"file",... | Reads the metadata of stored routines from the metadata file. | [
"Reads",
"the",
"metadata",
"of",
"stored",
"routines",
"from",
"the",
"metadata",
"file",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L260-L266 | train | 51,362 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader.__remove_obsolete_metadata | def __remove_obsolete_metadata(self):
"""
Removes obsolete entries from the metadata of all stored routines.
"""
clean = {}
for key, _ in self._source_file_names.items():
if key in self._pystratum_metadata:
clean[key] = self._pystratum_metadata[key]
self._pystratum_metadata = clean | python | def __remove_obsolete_metadata(self):
"""
Removes obsolete entries from the metadata of all stored routines.
"""
clean = {}
for key, _ in self._source_file_names.items():
if key in self._pystratum_metadata:
clean[key] = self._pystratum_metadata[key]
self._pystratum_metadata = clean | [
"def",
"__remove_obsolete_metadata",
"(",
"self",
")",
":",
"clean",
"=",
"{",
"}",
"for",
"key",
",",
"_",
"in",
"self",
".",
"_source_file_names",
".",
"items",
"(",
")",
":",
"if",
"key",
"in",
"self",
".",
"_pystratum_metadata",
":",
"clean",
"[",
... | Removes obsolete entries from the metadata of all stored routines. | [
"Removes",
"obsolete",
"entries",
"from",
"the",
"metadata",
"of",
"all",
"stored",
"routines",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L345-L354 | train | 51,363 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader.__write_stored_routine_metadata | def __write_stored_routine_metadata(self):
"""
Writes the metadata of all stored routines to the metadata file.
"""
with open(self._pystratum_metadata_filename, 'w') as stream:
json.dump(self._pystratum_metadata, stream, indent=4, sort_keys=True) | python | def __write_stored_routine_metadata(self):
"""
Writes the metadata of all stored routines to the metadata file.
"""
with open(self._pystratum_metadata_filename, 'w') as stream:
json.dump(self._pystratum_metadata, stream, indent=4, sort_keys=True) | [
"def",
"__write_stored_routine_metadata",
"(",
"self",
")",
":",
"with",
"open",
"(",
"self",
".",
"_pystratum_metadata_filename",
",",
"'w'",
")",
"as",
"stream",
":",
"json",
".",
"dump",
"(",
"self",
".",
"_pystratum_metadata",
",",
"stream",
",",
"indent",... | Writes the metadata of all stored routines to the metadata file. | [
"Writes",
"the",
"metadata",
"of",
"all",
"stored",
"routines",
"to",
"the",
"metadata",
"file",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L357-L362 | train | 51,364 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader.find_source_files_from_list | def find_source_files_from_list(self, file_names):
"""
Finds all source files that actually exists from a list of file names.
:param list[str] file_names: The list of file names.
"""
for file_name in file_names:
if os.path.exists(file_name):
routine_name = os.path.splitext(os.path.basename(file_name))[0]
if routine_name not in self._source_file_names:
self._source_file_names[routine_name] = file_name
else:
self._io.error("Files '{0}' and '{1}' have the same basename.".
format(self._source_file_names[routine_name], file_name))
self.error_file_names.add(file_name)
else:
self._io.error("File not exists: '{0}'".format(file_name))
self.error_file_names.add(file_name) | python | def find_source_files_from_list(self, file_names):
"""
Finds all source files that actually exists from a list of file names.
:param list[str] file_names: The list of file names.
"""
for file_name in file_names:
if os.path.exists(file_name):
routine_name = os.path.splitext(os.path.basename(file_name))[0]
if routine_name not in self._source_file_names:
self._source_file_names[routine_name] = file_name
else:
self._io.error("Files '{0}' and '{1}' have the same basename.".
format(self._source_file_names[routine_name], file_name))
self.error_file_names.add(file_name)
else:
self._io.error("File not exists: '{0}'".format(file_name))
self.error_file_names.add(file_name) | [
"def",
"find_source_files_from_list",
"(",
"self",
",",
"file_names",
")",
":",
"for",
"file_name",
"in",
"file_names",
":",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"file_name",
")",
":",
"routine_name",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
... | Finds all source files that actually exists from a list of file names.
:param list[str] file_names: The list of file names. | [
"Finds",
"all",
"source",
"files",
"that",
"actually",
"exists",
"from",
"a",
"list",
"of",
"file",
"names",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L365-L382 | train | 51,365 |
SetBased/py-stratum | pystratum/RoutineLoader.py | RoutineLoader.__get_constants | def __get_constants(self):
"""
Gets the constants from the class that acts like a namespace for constants and adds them to the replace pairs.
"""
helper = ConstantClass(self._constants_class_name, self._io)
helper.reload()
constants = helper.constants()
for name, value in constants.items():
self._add_replace_pair(name, value, True)
self._io.text('Read {0} constants for substitution from <fso>{1}</fso>'.
format(len(constants), helper.file_name())) | python | def __get_constants(self):
"""
Gets the constants from the class that acts like a namespace for constants and adds them to the replace pairs.
"""
helper = ConstantClass(self._constants_class_name, self._io)
helper.reload()
constants = helper.constants()
for name, value in constants.items():
self._add_replace_pair(name, value, True)
self._io.text('Read {0} constants for substitution from <fso>{1}</fso>'.
format(len(constants), helper.file_name())) | [
"def",
"__get_constants",
"(",
"self",
")",
":",
"helper",
"=",
"ConstantClass",
"(",
"self",
".",
"_constants_class_name",
",",
"self",
".",
"_io",
")",
"helper",
".",
"reload",
"(",
")",
"constants",
"=",
"helper",
".",
"constants",
"(",
")",
"for",
"n... | Gets the constants from the class that acts like a namespace for constants and adds them to the replace pairs. | [
"Gets",
"the",
"constants",
"from",
"the",
"class",
"that",
"acts",
"like",
"a",
"namespace",
"for",
"constants",
"and",
"adds",
"them",
"to",
"the",
"replace",
"pairs",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/RoutineLoader.py#L385-L397 | train | 51,366 |
DreamLab/VmShepherd | src/vmshepherd/iaas/vm.py | Vm.terminate | async def terminate(self):
""" Terminate vm.
"""
logging.debug('Terminate: %s', self)
self.state = VmState.TERMINATED
return await self.manager.terminate_vm(self.id) | python | async def terminate(self):
""" Terminate vm.
"""
logging.debug('Terminate: %s', self)
self.state = VmState.TERMINATED
return await self.manager.terminate_vm(self.id) | [
"async",
"def",
"terminate",
"(",
"self",
")",
":",
"logging",
".",
"debug",
"(",
"'Terminate: %s'",
",",
"self",
")",
"self",
".",
"state",
"=",
"VmState",
".",
"TERMINATED",
"return",
"await",
"self",
".",
"manager",
".",
"terminate_vm",
"(",
"self",
"... | Terminate vm. | [
"Terminate",
"vm",
"."
] | 709a412c372b897d53808039c5c64a8b69c12c8d | https://github.com/DreamLab/VmShepherd/blob/709a412c372b897d53808039c5c64a8b69c12c8d/src/vmshepherd/iaas/vm.py#L86-L91 | train | 51,367 |
ClericPy/torequests | torequests/logs.py | init_logger | def init_logger(
name="",
handler_path_levels=None,
level=logging.INFO,
formatter=None,
formatter_str=None,
datefmt="%Y-%m-%d %H:%M:%S",
):
"""Add a default handler for logger.
Args:
name = '' or logger obj.
handler_path_levels = [['loggerfile.log',13],['','DEBUG'],['','info'],['','notSet']] # [[path,level]]
level = the least level for the logger.
formatter = logging.Formatter(
'%(levelname)-7s %(asctime)s %(name)s (%(filename)s: %(lineno)s): %(message)s',
"%Y-%m-%d %H:%M:%S")
formatter_str = '%(levelname)-7s %(asctime)s %(name)s (%(funcName)s: %(lineno)s): %(message)s'
custom formatter:
%(asctime)s %(created)f %(filename)s %(funcName)s %(levelname)s %(levelno)s %(lineno)s %(message)s %(module)s %(name)s %(pathname)s %(process)s %(relativeCreated)s %(thread)s %(threadName)s
"""
levels = {
"NOTSET": logging.NOTSET,
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}
if not formatter:
if formatter_str:
formatter_str = formatter_str
else:
formatter_str = "%(asctime)s %(levelname)-5s [%(name)s] %(filename)s(%(lineno)s): %(message)s"
formatter = logging.Formatter(formatter_str, datefmt=datefmt)
logger = name if isinstance(name, logging.Logger) else logging.getLogger(str(name))
logger.setLevel(level)
handler_path_levels = handler_path_levels or [["", "INFO"]]
# ---------------------------------------
for each_handler in handler_path_levels:
path, handler_level = each_handler
handler = logging.FileHandler(path) if path else logging.StreamHandler()
handler.setLevel(
levels.get(handler_level.upper(), 1)
if isinstance(handler_level, str)
else handler_level
)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger | python | def init_logger(
name="",
handler_path_levels=None,
level=logging.INFO,
formatter=None,
formatter_str=None,
datefmt="%Y-%m-%d %H:%M:%S",
):
"""Add a default handler for logger.
Args:
name = '' or logger obj.
handler_path_levels = [['loggerfile.log',13],['','DEBUG'],['','info'],['','notSet']] # [[path,level]]
level = the least level for the logger.
formatter = logging.Formatter(
'%(levelname)-7s %(asctime)s %(name)s (%(filename)s: %(lineno)s): %(message)s',
"%Y-%m-%d %H:%M:%S")
formatter_str = '%(levelname)-7s %(asctime)s %(name)s (%(funcName)s: %(lineno)s): %(message)s'
custom formatter:
%(asctime)s %(created)f %(filename)s %(funcName)s %(levelname)s %(levelno)s %(lineno)s %(message)s %(module)s %(name)s %(pathname)s %(process)s %(relativeCreated)s %(thread)s %(threadName)s
"""
levels = {
"NOTSET": logging.NOTSET,
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}
if not formatter:
if formatter_str:
formatter_str = formatter_str
else:
formatter_str = "%(asctime)s %(levelname)-5s [%(name)s] %(filename)s(%(lineno)s): %(message)s"
formatter = logging.Formatter(formatter_str, datefmt=datefmt)
logger = name if isinstance(name, logging.Logger) else logging.getLogger(str(name))
logger.setLevel(level)
handler_path_levels = handler_path_levels or [["", "INFO"]]
# ---------------------------------------
for each_handler in handler_path_levels:
path, handler_level = each_handler
handler = logging.FileHandler(path) if path else logging.StreamHandler()
handler.setLevel(
levels.get(handler_level.upper(), 1)
if isinstance(handler_level, str)
else handler_level
)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger | [
"def",
"init_logger",
"(",
"name",
"=",
"\"\"",
",",
"handler_path_levels",
"=",
"None",
",",
"level",
"=",
"logging",
".",
"INFO",
",",
"formatter",
"=",
"None",
",",
"formatter_str",
"=",
"None",
",",
"datefmt",
"=",
"\"%Y-%m-%d %H:%M:%S\"",
",",
")",
":... | Add a default handler for logger.
Args:
name = '' or logger obj.
handler_path_levels = [['loggerfile.log',13],['','DEBUG'],['','info'],['','notSet']] # [[path,level]]
level = the least level for the logger.
formatter = logging.Formatter(
'%(levelname)-7s %(asctime)s %(name)s (%(filename)s: %(lineno)s): %(message)s',
"%Y-%m-%d %H:%M:%S")
formatter_str = '%(levelname)-7s %(asctime)s %(name)s (%(funcName)s: %(lineno)s): %(message)s'
custom formatter:
%(asctime)s %(created)f %(filename)s %(funcName)s %(levelname)s %(levelno)s %(lineno)s %(message)s %(module)s %(name)s %(pathname)s %(process)s %(relativeCreated)s %(thread)s %(threadName)s | [
"Add",
"a",
"default",
"handler",
"for",
"logger",
"."
] | 1793261688d7a47e1c3a0830d83f8552f5e3e5d9 | https://github.com/ClericPy/torequests/blob/1793261688d7a47e1c3a0830d83f8552f5e3e5d9/torequests/logs.py#L12-L67 | train | 51,368 |
Miserlou/django-knockout-modeler | knockout_modeler/ko.py | get_fields | def get_fields(model):
"""
Returns a Model's knockout_fields,
or the default set of field names.
"""
try:
if hasattr(model, "knockout_fields"):
fields = model.knockout_fields()
else:
try:
fields = model_to_dict(model).keys()
except Exception as e:
fields = model._meta.get_fields()
return fields
# Crash proofing
except Exception as e:
logger.exception(e)
return [] | python | def get_fields(model):
"""
Returns a Model's knockout_fields,
or the default set of field names.
"""
try:
if hasattr(model, "knockout_fields"):
fields = model.knockout_fields()
else:
try:
fields = model_to_dict(model).keys()
except Exception as e:
fields = model._meta.get_fields()
return fields
# Crash proofing
except Exception as e:
logger.exception(e)
return [] | [
"def",
"get_fields",
"(",
"model",
")",
":",
"try",
":",
"if",
"hasattr",
"(",
"model",
",",
"\"knockout_fields\"",
")",
":",
"fields",
"=",
"model",
".",
"knockout_fields",
"(",
")",
"else",
":",
"try",
":",
"fields",
"=",
"model_to_dict",
"(",
"model",... | Returns a Model's knockout_fields,
or the default set of field names. | [
"Returns",
"a",
"Model",
"s",
"knockout_fields",
"or",
"the",
"default",
"set",
"of",
"field",
"names",
"."
] | 714d21cc5ed008f132cea01dbae9f214c2bf1b76 | https://github.com/Miserlou/django-knockout-modeler/blob/714d21cc5ed008f132cea01dbae9f214c2bf1b76/knockout_modeler/ko.py#L18-L37 | train | 51,369 |
Miserlou/django-knockout-modeler | knockout_modeler/ko.py | get_object_data | def get_object_data(obj, fields, safe):
"""
Given an object and a list of fields, recursively build an object for serialization.
Returns a dictionary.
"""
temp_dict = dict()
for field in fields:
try:
attribute = getattr(obj, str(field))
if isinstance(attribute, list) and all([isinstance(item, models.Model) for item in attribute]):
temp_dict[field] = []
for item in attribute:
temp_dict[field].append(get_object_data(item, get_fields(item), safe)) # Recur
elif isinstance(attribute, models.Model):
attribute_fields = get_fields(attribute)
object_data = get_object_data(attribute, attribute_fields, safe) # Recur
temp_dict[field] = object_data
else:
if not safe:
if isinstance(attribute, basestring):
attribute = cgi.escape(attribute)
temp_dict[field] = attribute
except Exception as e:
logger.info("Unable to get attribute.")
logger.error(e)
continue
return temp_dict | python | def get_object_data(obj, fields, safe):
"""
Given an object and a list of fields, recursively build an object for serialization.
Returns a dictionary.
"""
temp_dict = dict()
for field in fields:
try:
attribute = getattr(obj, str(field))
if isinstance(attribute, list) and all([isinstance(item, models.Model) for item in attribute]):
temp_dict[field] = []
for item in attribute:
temp_dict[field].append(get_object_data(item, get_fields(item), safe)) # Recur
elif isinstance(attribute, models.Model):
attribute_fields = get_fields(attribute)
object_data = get_object_data(attribute, attribute_fields, safe) # Recur
temp_dict[field] = object_data
else:
if not safe:
if isinstance(attribute, basestring):
attribute = cgi.escape(attribute)
temp_dict[field] = attribute
except Exception as e:
logger.info("Unable to get attribute.")
logger.error(e)
continue
return temp_dict | [
"def",
"get_object_data",
"(",
"obj",
",",
"fields",
",",
"safe",
")",
":",
"temp_dict",
"=",
"dict",
"(",
")",
"for",
"field",
"in",
"fields",
":",
"try",
":",
"attribute",
"=",
"getattr",
"(",
"obj",
",",
"str",
"(",
"field",
")",
")",
"if",
"isi... | Given an object and a list of fields, recursively build an object for serialization.
Returns a dictionary. | [
"Given",
"an",
"object",
"and",
"a",
"list",
"of",
"fields",
"recursively",
"build",
"an",
"object",
"for",
"serialization",
"."
] | 714d21cc5ed008f132cea01dbae9f214c2bf1b76 | https://github.com/Miserlou/django-knockout-modeler/blob/714d21cc5ed008f132cea01dbae9f214c2bf1b76/knockout_modeler/ko.py#L40-L71 | train | 51,370 |
Miserlou/django-knockout-modeler | knockout_modeler/ko.py | ko_model | def ko_model(model, field_names=None, data=None):
"""
Given a model, returns the Knockout Model and the Knockout ViewModel.
Takes optional field names and data.
"""
try:
if isinstance(model, str):
modelName = model
else:
modelName = model.__class__.__name__
if field_names:
fields = field_names
else:
fields = get_fields(model)
if hasattr(model, "comparator"):
comparator = str(model.comparator())
else:
comparator = 'id'
modelViewString = render_to_string(
"knockout_modeler/model.js",
{'modelName': modelName, 'fields': fields, 'data': data, 'comparator': comparator}
)
return modelViewString
except Exception as e:
logger.exception(e)
return '' | python | def ko_model(model, field_names=None, data=None):
"""
Given a model, returns the Knockout Model and the Knockout ViewModel.
Takes optional field names and data.
"""
try:
if isinstance(model, str):
modelName = model
else:
modelName = model.__class__.__name__
if field_names:
fields = field_names
else:
fields = get_fields(model)
if hasattr(model, "comparator"):
comparator = str(model.comparator())
else:
comparator = 'id'
modelViewString = render_to_string(
"knockout_modeler/model.js",
{'modelName': modelName, 'fields': fields, 'data': data, 'comparator': comparator}
)
return modelViewString
except Exception as e:
logger.exception(e)
return '' | [
"def",
"ko_model",
"(",
"model",
",",
"field_names",
"=",
"None",
",",
"data",
"=",
"None",
")",
":",
"try",
":",
"if",
"isinstance",
"(",
"model",
",",
"str",
")",
":",
"modelName",
"=",
"model",
"else",
":",
"modelName",
"=",
"model",
".",
"__class... | Given a model, returns the Knockout Model and the Knockout ViewModel.
Takes optional field names and data. | [
"Given",
"a",
"model",
"returns",
"the",
"Knockout",
"Model",
"and",
"the",
"Knockout",
"ViewModel",
".",
"Takes",
"optional",
"field",
"names",
"and",
"data",
"."
] | 714d21cc5ed008f132cea01dbae9f214c2bf1b76 | https://github.com/Miserlou/django-knockout-modeler/blob/714d21cc5ed008f132cea01dbae9f214c2bf1b76/knockout_modeler/ko.py#L74-L104 | train | 51,371 |
Miserlou/django-knockout-modeler | knockout_modeler/ko.py | ko_bindings | def ko_bindings(model):
"""
Given a model, returns the Knockout data bindings.
"""
try:
if isinstance(model, str):
modelName = model
else:
modelName = model.__class__.__name__
modelBindingsString = "ko.applyBindings(new " + modelName + "ViewModel(), $('#" + modelName.lower() + "s')[0]);"
return modelBindingsString
except Exception as e:
logger.error(e)
return '' | python | def ko_bindings(model):
"""
Given a model, returns the Knockout data bindings.
"""
try:
if isinstance(model, str):
modelName = model
else:
modelName = model.__class__.__name__
modelBindingsString = "ko.applyBindings(new " + modelName + "ViewModel(), $('#" + modelName.lower() + "s')[0]);"
return modelBindingsString
except Exception as e:
logger.error(e)
return '' | [
"def",
"ko_bindings",
"(",
"model",
")",
":",
"try",
":",
"if",
"isinstance",
"(",
"model",
",",
"str",
")",
":",
"modelName",
"=",
"model",
"else",
":",
"modelName",
"=",
"model",
".",
"__class__",
".",
"__name__",
"modelBindingsString",
"=",
"\"ko.applyB... | Given a model, returns the Knockout data bindings. | [
"Given",
"a",
"model",
"returns",
"the",
"Knockout",
"data",
"bindings",
"."
] | 714d21cc5ed008f132cea01dbae9f214c2bf1b76 | https://github.com/Miserlou/django-knockout-modeler/blob/714d21cc5ed008f132cea01dbae9f214c2bf1b76/knockout_modeler/ko.py#L107-L123 | train | 51,372 |
Miserlou/django-knockout-modeler | knockout_modeler/ko.py | ko_data | def ko_data(queryset, field_names=None, name=None, safe=False, return_json=False):
"""
Given a QuerySet, return just the serialized representation
based on the knockout_fields as JavaScript.
"""
try:
try:
# Get an inital instance of the QS.
queryset_instance = queryset[0]
except TypeError as e:
# We are being passed an object rather than a QuerySet.
# That's naughty, but we'll survive.
queryset_instance = queryset
queryset = [queryset]
except IndexError as e:
if not isinstance(queryset, list):
# This is an empty QS - get the model directly.
queryset_instance = queryset.model
else:
# We have been given an empty list.
# Return nothing.
return '[]'
modelName = queryset_instance.__class__.__name__
modelNameData = []
if field_names is not None:
fields = field_names
else:
fields = get_fields(queryset_instance)
for obj in queryset:
object_data = get_object_data(obj, fields, safe)
modelNameData.append(object_data)
if name:
modelNameString = name
else:
modelNameString = modelName + "Data"
dthandler = lambda obj: obj.isoformat() if isinstance(obj, (datetime.date, datetime.datetime)) else None
dumped_json = json.dumps(modelNameData, default=dthandler)
if return_json:
return dumped_json
return "var " + modelNameString + " = " + dumped_json + ';'
except Exception as e:
logger.exception(e)
return '[]' | python | def ko_data(queryset, field_names=None, name=None, safe=False, return_json=False):
"""
Given a QuerySet, return just the serialized representation
based on the knockout_fields as JavaScript.
"""
try:
try:
# Get an inital instance of the QS.
queryset_instance = queryset[0]
except TypeError as e:
# We are being passed an object rather than a QuerySet.
# That's naughty, but we'll survive.
queryset_instance = queryset
queryset = [queryset]
except IndexError as e:
if not isinstance(queryset, list):
# This is an empty QS - get the model directly.
queryset_instance = queryset.model
else:
# We have been given an empty list.
# Return nothing.
return '[]'
modelName = queryset_instance.__class__.__name__
modelNameData = []
if field_names is not None:
fields = field_names
else:
fields = get_fields(queryset_instance)
for obj in queryset:
object_data = get_object_data(obj, fields, safe)
modelNameData.append(object_data)
if name:
modelNameString = name
else:
modelNameString = modelName + "Data"
dthandler = lambda obj: obj.isoformat() if isinstance(obj, (datetime.date, datetime.datetime)) else None
dumped_json = json.dumps(modelNameData, default=dthandler)
if return_json:
return dumped_json
return "var " + modelNameString + " = " + dumped_json + ';'
except Exception as e:
logger.exception(e)
return '[]' | [
"def",
"ko_data",
"(",
"queryset",
",",
"field_names",
"=",
"None",
",",
"name",
"=",
"None",
",",
"safe",
"=",
"False",
",",
"return_json",
"=",
"False",
")",
":",
"try",
":",
"try",
":",
"# Get an inital instance of the QS.",
"queryset_instance",
"=",
"que... | Given a QuerySet, return just the serialized representation
based on the knockout_fields as JavaScript. | [
"Given",
"a",
"QuerySet",
"return",
"just",
"the",
"serialized",
"representation",
"based",
"on",
"the",
"knockout_fields",
"as",
"JavaScript",
"."
] | 714d21cc5ed008f132cea01dbae9f214c2bf1b76 | https://github.com/Miserlou/django-knockout-modeler/blob/714d21cc5ed008f132cea01dbae9f214c2bf1b76/knockout_modeler/ko.py#L137-L187 | train | 51,373 |
Miserlou/django-knockout-modeler | knockout_modeler/ko.py | ko | def ko(queryset, field_names=None):
"""
Converts a Django QuerySet into a complete Knockout implementation.
"""
try:
koDataString = ko_data(queryset, field_names)
koModelString = ko_model(queryset[0].__class__.__name__, field_names, data=True)
koBindingsString = ko_bindings(queryset[0])
koString = koDataString + '\n' + koModelString + '\n' + koBindingsString
return koString
except Exception as e:
logger.error(e)
return '' | python | def ko(queryset, field_names=None):
"""
Converts a Django QuerySet into a complete Knockout implementation.
"""
try:
koDataString = ko_data(queryset, field_names)
koModelString = ko_model(queryset[0].__class__.__name__, field_names, data=True)
koBindingsString = ko_bindings(queryset[0])
koString = koDataString + '\n' + koModelString + '\n' + koBindingsString
return koString
except Exception as e:
logger.error(e)
return '' | [
"def",
"ko",
"(",
"queryset",
",",
"field_names",
"=",
"None",
")",
":",
"try",
":",
"koDataString",
"=",
"ko_data",
"(",
"queryset",
",",
"field_names",
")",
"koModelString",
"=",
"ko_model",
"(",
"queryset",
"[",
"0",
"]",
".",
"__class__",
".",
"__nam... | Converts a Django QuerySet into a complete Knockout implementation. | [
"Converts",
"a",
"Django",
"QuerySet",
"into",
"a",
"complete",
"Knockout",
"implementation",
"."
] | 714d21cc5ed008f132cea01dbae9f214c2bf1b76 | https://github.com/Miserlou/django-knockout-modeler/blob/714d21cc5ed008f132cea01dbae9f214c2bf1b76/knockout_modeler/ko.py#L190-L205 | train | 51,374 |
vsoch/helpme | helpme/utils/settings.py | generate_keypair | def generate_keypair(keypair_file):
'''generate_keypair is used by some of the helpers that need a keypair.
The function should be used if the client doesn't have the attribute
self.key. We generate the key and return it.
We use pycryptodome (3.7.2)
Parameters
=========
keypair_file: fullpath to where to save keypair
'''
from Crypto.PublicKey import RSA
key = RSA.generate(2048)
# Ensure helper directory exists
keypair_dir = os.path.dirname(keypair_file)
if not os.path.exists(keypair_dir):
os.makedirs(keypair_dir)
# Save key
with open(keypair_file, 'wb') as filey:
filey.write(key.exportKey('PEM'))
return key | python | def generate_keypair(keypair_file):
'''generate_keypair is used by some of the helpers that need a keypair.
The function should be used if the client doesn't have the attribute
self.key. We generate the key and return it.
We use pycryptodome (3.7.2)
Parameters
=========
keypair_file: fullpath to where to save keypair
'''
from Crypto.PublicKey import RSA
key = RSA.generate(2048)
# Ensure helper directory exists
keypair_dir = os.path.dirname(keypair_file)
if not os.path.exists(keypair_dir):
os.makedirs(keypair_dir)
# Save key
with open(keypair_file, 'wb') as filey:
filey.write(key.exportKey('PEM'))
return key | [
"def",
"generate_keypair",
"(",
"keypair_file",
")",
":",
"from",
"Crypto",
".",
"PublicKey",
"import",
"RSA",
"key",
"=",
"RSA",
".",
"generate",
"(",
"2048",
")",
"# Ensure helper directory exists",
"keypair_dir",
"=",
"os",
".",
"path",
".",
"dirname",
"(",... | generate_keypair is used by some of the helpers that need a keypair.
The function should be used if the client doesn't have the attribute
self.key. We generate the key and return it.
We use pycryptodome (3.7.2)
Parameters
=========
keypair_file: fullpath to where to save keypair | [
"generate_keypair",
"is",
"used",
"by",
"some",
"of",
"the",
"helpers",
"that",
"need",
"a",
"keypair",
".",
"The",
"function",
"should",
"be",
"used",
"if",
"the",
"client",
"doesn",
"t",
"have",
"the",
"attribute",
"self",
".",
"key",
".",
"We",
"gener... | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/utils/settings.py#L56-L80 | train | 51,375 |
SetBased/py-stratum | pystratum/exception/ResultException.py | ResultException.__message | def __message(expected_row_count, actual_row_count, query):
"""
Composes the exception message.
:param str expected_row_count: The expected row count.
:param int actual_row_count: The actual row count.
:param str query: The query.
:rtype: str
"""
query = query.strip()
message = 'Wrong number of rows selected'
message += os.linesep
message += 'Expected number of rows: {}'.format(expected_row_count)
message += os.linesep
message += 'Actual number of rows: {}'.format(actual_row_count)
message += os.linesep
message += 'Query:'
message += os.linesep if os.linesep in query else ' '
message += query
return message | python | def __message(expected_row_count, actual_row_count, query):
"""
Composes the exception message.
:param str expected_row_count: The expected row count.
:param int actual_row_count: The actual row count.
:param str query: The query.
:rtype: str
"""
query = query.strip()
message = 'Wrong number of rows selected'
message += os.linesep
message += 'Expected number of rows: {}'.format(expected_row_count)
message += os.linesep
message += 'Actual number of rows: {}'.format(actual_row_count)
message += os.linesep
message += 'Query:'
message += os.linesep if os.linesep in query else ' '
message += query
return message | [
"def",
"__message",
"(",
"expected_row_count",
",",
"actual_row_count",
",",
"query",
")",
":",
"query",
"=",
"query",
".",
"strip",
"(",
")",
"message",
"=",
"'Wrong number of rows selected'",
"message",
"+=",
"os",
".",
"linesep",
"message",
"+=",
"'Expected n... | Composes the exception message.
:param str expected_row_count: The expected row count.
:param int actual_row_count: The actual row count.
:param str query: The query.
:rtype: str | [
"Composes",
"the",
"exception",
"message",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/exception/ResultException.py#L76-L98 | train | 51,376 |
vsoch/helpme | helpme/client/help.py | main | def main(args, extras):
'''This is the actual driver for the helper.
'''
from helpme.main import get_helper
name = args.command
if name in HELPME_HELPERS:
# Get the helper, do the recording, submit
helper = get_helper(name=name)
if args.asciinema is not None:
if os.path.exists(args.asciinema):
helper.data['record_asciinema'] = args.asciinema
helper.run(positionals=extras) | python | def main(args, extras):
'''This is the actual driver for the helper.
'''
from helpme.main import get_helper
name = args.command
if name in HELPME_HELPERS:
# Get the helper, do the recording, submit
helper = get_helper(name=name)
if args.asciinema is not None:
if os.path.exists(args.asciinema):
helper.data['record_asciinema'] = args.asciinema
helper.run(positionals=extras) | [
"def",
"main",
"(",
"args",
",",
"extras",
")",
":",
"from",
"helpme",
".",
"main",
"import",
"get_helper",
"name",
"=",
"args",
".",
"command",
"if",
"name",
"in",
"HELPME_HELPERS",
":",
"# Get the helper, do the recording, submit",
"helper",
"=",
"get_helper",... | This is the actual driver for the helper. | [
"This",
"is",
"the",
"actual",
"driver",
"for",
"the",
"helper",
"."
] | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/client/help.py#L27-L44 | train | 51,377 |
jurismarches/chopper | chopper/extractor.py | Extractor.extract | def extract(self, html_contents, css_contents=None, base_url=None):
"""
Extracts the cleaned html tree as a string and only
css rules matching the cleaned html tree
:param html_contents: The HTML contents to parse
:type html_contents: str
:param css_contents: The CSS contents to parse
:type css_contents: str
:param base_url: The base page URL to use for relative to absolute links
:type base_url: str
:returns: cleaned HTML contents, cleaned CSS contents
:rtype: str or tuple
"""
# Clean HTML
html_extractor = self.html_extractor(
html_contents, self._xpaths_to_keep, self._xpaths_to_discard)
has_matches = html_extractor.parse()
if has_matches:
# Relative to absolute URLs
if base_url is not None:
html_extractor.rel_to_abs(base_url)
# Convert ElementTree to string
cleaned_html = html_extractor.to_string()
else:
cleaned_html = None
# Clean CSS
if css_contents is not None:
if cleaned_html is not None:
css_extractor = self.css_extractor(css_contents, cleaned_html)
css_extractor.parse()
# Relative to absolute URLs
if base_url is not None:
css_extractor.rel_to_abs(base_url)
cleaned_css = css_extractor.to_string()
else:
cleaned_css = None
else:
return cleaned_html
return (cleaned_html, cleaned_css) | python | def extract(self, html_contents, css_contents=None, base_url=None):
"""
Extracts the cleaned html tree as a string and only
css rules matching the cleaned html tree
:param html_contents: The HTML contents to parse
:type html_contents: str
:param css_contents: The CSS contents to parse
:type css_contents: str
:param base_url: The base page URL to use for relative to absolute links
:type base_url: str
:returns: cleaned HTML contents, cleaned CSS contents
:rtype: str or tuple
"""
# Clean HTML
html_extractor = self.html_extractor(
html_contents, self._xpaths_to_keep, self._xpaths_to_discard)
has_matches = html_extractor.parse()
if has_matches:
# Relative to absolute URLs
if base_url is not None:
html_extractor.rel_to_abs(base_url)
# Convert ElementTree to string
cleaned_html = html_extractor.to_string()
else:
cleaned_html = None
# Clean CSS
if css_contents is not None:
if cleaned_html is not None:
css_extractor = self.css_extractor(css_contents, cleaned_html)
css_extractor.parse()
# Relative to absolute URLs
if base_url is not None:
css_extractor.rel_to_abs(base_url)
cleaned_css = css_extractor.to_string()
else:
cleaned_css = None
else:
return cleaned_html
return (cleaned_html, cleaned_css) | [
"def",
"extract",
"(",
"self",
",",
"html_contents",
",",
"css_contents",
"=",
"None",
",",
"base_url",
"=",
"None",
")",
":",
"# Clean HTML",
"html_extractor",
"=",
"self",
".",
"html_extractor",
"(",
"html_contents",
",",
"self",
".",
"_xpaths_to_keep",
",",... | Extracts the cleaned html tree as a string and only
css rules matching the cleaned html tree
:param html_contents: The HTML contents to parse
:type html_contents: str
:param css_contents: The CSS contents to parse
:type css_contents: str
:param base_url: The base page URL to use for relative to absolute links
:type base_url: str
:returns: cleaned HTML contents, cleaned CSS contents
:rtype: str or tuple | [
"Extracts",
"the",
"cleaned",
"html",
"tree",
"as",
"a",
"string",
"and",
"only",
"css",
"rules",
"matching",
"the",
"cleaned",
"html",
"tree"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/extractor.py#L58-L110 | train | 51,378 |
jurismarches/chopper | chopper/extractor.py | Extractor.__add | def __add(self, dest, xpath):
"""
Adds a Xpath expression to the dest list
:param dest: The destination list to add the Xpath
:type dest: list
:param xpath: The Xpath expression to add
:type xpath: str
"""
assert isinstance(xpath, string_types)
dest.append(xpath) | python | def __add(self, dest, xpath):
"""
Adds a Xpath expression to the dest list
:param dest: The destination list to add the Xpath
:type dest: list
:param xpath: The Xpath expression to add
:type xpath: str
"""
assert isinstance(xpath, string_types)
dest.append(xpath) | [
"def",
"__add",
"(",
"self",
",",
"dest",
",",
"xpath",
")",
":",
"assert",
"isinstance",
"(",
"xpath",
",",
"string_types",
")",
"dest",
".",
"append",
"(",
"xpath",
")"
] | Adds a Xpath expression to the dest list
:param dest: The destination list to add the Xpath
:type dest: list
:param xpath: The Xpath expression to add
:type xpath: str | [
"Adds",
"a",
"Xpath",
"expression",
"to",
"the",
"dest",
"list"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/extractor.py#L142-L152 | train | 51,379 |
SetBased/py-stratum | pystratum/wrapper/Wrapper.py | Wrapper._write_line | def _write_line(self, line=None):
"""
Appends a line of code to the generated code and adjust the indent level of the generated code.
:param line: The line of code (with out LF) that must be appended.
"""
if line is None:
self._write("\n")
if self.__indent_level > 1:
self.__indent_level -= 1
elif line == '':
self._write("\n")
else:
line = (' ' * 4 * self.__indent_level) + line
if line[-1:] == ':':
self.__indent_level += 1
self._write(line + "\n") | python | def _write_line(self, line=None):
"""
Appends a line of code to the generated code and adjust the indent level of the generated code.
:param line: The line of code (with out LF) that must be appended.
"""
if line is None:
self._write("\n")
if self.__indent_level > 1:
self.__indent_level -= 1
elif line == '':
self._write("\n")
else:
line = (' ' * 4 * self.__indent_level) + line
if line[-1:] == ':':
self.__indent_level += 1
self._write(line + "\n") | [
"def",
"_write_line",
"(",
"self",
",",
"line",
"=",
"None",
")",
":",
"if",
"line",
"is",
"None",
":",
"self",
".",
"_write",
"(",
"\"\\n\"",
")",
"if",
"self",
".",
"__indent_level",
">",
"1",
":",
"self",
".",
"__indent_level",
"-=",
"1",
"elif",
... | Appends a line of code to the generated code and adjust the indent level of the generated code.
:param line: The line of code (with out LF) that must be appended. | [
"Appends",
"a",
"line",
"of",
"code",
"to",
"the",
"generated",
"code",
"and",
"adjust",
"the",
"indent",
"level",
"of",
"the",
"generated",
"code",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/wrapper/Wrapper.py#L66-L82 | train | 51,380 |
SetBased/py-stratum | pystratum/wrapper/Wrapper.py | Wrapper.write_routine_method | def write_routine_method(self, routine):
"""
Returns a complete wrapper method.
:param dict[str,*] routine: The routine metadata.
:rtype: str
"""
if self._lob_as_string_flag:
return self._write_routine_method_without_lob(routine)
else:
if self.is_lob_parameter(routine['parameters']):
return self._write_routine_method_with_lob(routine)
else:
return self._write_routine_method_without_lob(routine) | python | def write_routine_method(self, routine):
"""
Returns a complete wrapper method.
:param dict[str,*] routine: The routine metadata.
:rtype: str
"""
if self._lob_as_string_flag:
return self._write_routine_method_without_lob(routine)
else:
if self.is_lob_parameter(routine['parameters']):
return self._write_routine_method_with_lob(routine)
else:
return self._write_routine_method_without_lob(routine) | [
"def",
"write_routine_method",
"(",
"self",
",",
"routine",
")",
":",
"if",
"self",
".",
"_lob_as_string_flag",
":",
"return",
"self",
".",
"_write_routine_method_without_lob",
"(",
"routine",
")",
"else",
":",
"if",
"self",
".",
"is_lob_parameter",
"(",
"routin... | Returns a complete wrapper method.
:param dict[str,*] routine: The routine metadata.
:rtype: str | [
"Returns",
"a",
"complete",
"wrapper",
"method",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/wrapper/Wrapper.py#L113-L127 | train | 51,381 |
SetBased/py-stratum | pystratum/wrapper/Wrapper.py | Wrapper._write_docstring_parameters | def _write_docstring_parameters(self, routine):
"""
Writes the parameters part of the docstring for the wrapper method of a stored routine.
:param dict routine: The metadata of the stored routine.
"""
if routine['pydoc']['parameters']:
self._write_line('')
for param in routine['pydoc']['parameters']:
lines = param['description'].split(os.linesep)
self._write_line(':param {0} {1}: {2}'.format(param['python_type'],
param['parameter_name'],
lines[0]))
del lines[0]
tmp = ':param {0} {1}:'.format(param['python_type'], param['parameter_name'])
indent = ' ' * len(tmp)
for line in lines:
self._write_line('{0} {1}'.format(indent, line))
self._write_line('{0} {1}'.format(indent, param['data_type_descriptor'])) | python | def _write_docstring_parameters(self, routine):
"""
Writes the parameters part of the docstring for the wrapper method of a stored routine.
:param dict routine: The metadata of the stored routine.
"""
if routine['pydoc']['parameters']:
self._write_line('')
for param in routine['pydoc']['parameters']:
lines = param['description'].split(os.linesep)
self._write_line(':param {0} {1}: {2}'.format(param['python_type'],
param['parameter_name'],
lines[0]))
del lines[0]
tmp = ':param {0} {1}:'.format(param['python_type'], param['parameter_name'])
indent = ' ' * len(tmp)
for line in lines:
self._write_line('{0} {1}'.format(indent, line))
self._write_line('{0} {1}'.format(indent, param['data_type_descriptor'])) | [
"def",
"_write_docstring_parameters",
"(",
"self",
",",
"routine",
")",
":",
"if",
"routine",
"[",
"'pydoc'",
"]",
"[",
"'parameters'",
"]",
":",
"self",
".",
"_write_line",
"(",
"''",
")",
"for",
"param",
"in",
"routine",
"[",
"'pydoc'",
"]",
"[",
"'par... | Writes the parameters part of the docstring for the wrapper method of a stored routine.
:param dict routine: The metadata of the stored routine. | [
"Writes",
"the",
"parameters",
"part",
"of",
"the",
"docstring",
"for",
"the",
"wrapper",
"method",
"of",
"a",
"stored",
"routine",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/wrapper/Wrapper.py#L140-L161 | train | 51,382 |
SetBased/py-stratum | pystratum/wrapper/Wrapper.py | Wrapper.__write_docstring_return_type | def __write_docstring_return_type(self):
"""
Writes the return type part of the docstring for the wrapper method of a stored routine.
"""
rtype = self._get_docstring_return_type()
if rtype:
self._write_line('')
self._write_line(':rtype: {0}'.format(rtype)) | python | def __write_docstring_return_type(self):
"""
Writes the return type part of the docstring for the wrapper method of a stored routine.
"""
rtype = self._get_docstring_return_type()
if rtype:
self._write_line('')
self._write_line(':rtype: {0}'.format(rtype)) | [
"def",
"__write_docstring_return_type",
"(",
"self",
")",
":",
"rtype",
"=",
"self",
".",
"_get_docstring_return_type",
"(",
")",
"if",
"rtype",
":",
"self",
".",
"_write_line",
"(",
"''",
")",
"self",
".",
"_write_line",
"(",
"':rtype: {0}'",
".",
"format",
... | Writes the return type part of the docstring for the wrapper method of a stored routine. | [
"Writes",
"the",
"return",
"type",
"part",
"of",
"the",
"docstring",
"for",
"the",
"wrapper",
"method",
"of",
"a",
"stored",
"routine",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/wrapper/Wrapper.py#L164-L171 | train | 51,383 |
SetBased/py-stratum | pystratum/wrapper/Wrapper.py | Wrapper.__write_docstring | def __write_docstring(self, routine):
"""
Writes the docstring for the wrapper method of a stored routine.
:param dict routine: The metadata of the stored routine.
"""
self._write_line('"""')
self.__write_docstring_description(routine)
self._write_docstring_parameters(routine)
self.__write_docstring_return_type()
self._write_line('"""') | python | def __write_docstring(self, routine):
"""
Writes the docstring for the wrapper method of a stored routine.
:param dict routine: The metadata of the stored routine.
"""
self._write_line('"""')
self.__write_docstring_description(routine)
self._write_docstring_parameters(routine)
self.__write_docstring_return_type()
self._write_line('"""') | [
"def",
"__write_docstring",
"(",
"self",
",",
"routine",
")",
":",
"self",
".",
"_write_line",
"(",
"'\"\"\"'",
")",
"self",
".",
"__write_docstring_description",
"(",
"routine",
")",
"self",
".",
"_write_docstring_parameters",
"(",
"routine",
")",
"self",
".",
... | Writes the docstring for the wrapper method of a stored routine.
:param dict routine: The metadata of the stored routine. | [
"Writes",
"the",
"docstring",
"for",
"the",
"wrapper",
"method",
"of",
"a",
"stored",
"routine",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/wrapper/Wrapper.py#L174-L186 | train | 51,384 |
SetBased/py-stratum | pystratum/wrapper/Wrapper.py | Wrapper._get_wrapper_args | def _get_wrapper_args(routine):
"""
Returns code for the parameters of the wrapper method for the stored routine.
:param dict[str,*] routine: The routine metadata.
:rtype: str
"""
ret = ''
for parameter_info in routine['parameters']:
if ret:
ret += ', '
ret += parameter_info['name']
return ret | python | def _get_wrapper_args(routine):
"""
Returns code for the parameters of the wrapper method for the stored routine.
:param dict[str,*] routine: The routine metadata.
:rtype: str
"""
ret = ''
for parameter_info in routine['parameters']:
if ret:
ret += ', '
ret += parameter_info['name']
return ret | [
"def",
"_get_wrapper_args",
"(",
"routine",
")",
":",
"ret",
"=",
"''",
"for",
"parameter_info",
"in",
"routine",
"[",
"'parameters'",
"]",
":",
"if",
"ret",
":",
"ret",
"+=",
"', '",
"ret",
"+=",
"parameter_info",
"[",
"'name'",
"]",
"return",
"ret"
] | Returns code for the parameters of the wrapper method for the stored routine.
:param dict[str,*] routine: The routine metadata.
:rtype: str | [
"Returns",
"code",
"for",
"the",
"parameters",
"of",
"the",
"wrapper",
"method",
"for",
"the",
"stored",
"routine",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/wrapper/Wrapper.py#L224-L240 | train | 51,385 |
launchdarkly/relayCommander | relay_commander/generators.py | ConfigGenerator.generate_relay_config | def generate_relay_config(self, environments: list) -> None:
"""Generate ld-relay.conf file.
Given a list of environments of a project, this will generate a
``ld-relay.conf`` file in the current working directory. The conf file
follows the specification that is documented in the main `ld-relay`_
documentation.
.. _ld-relay: https://github.com/launchdarkly/ld-relay#configuration-file-format
:param environments: list of LaunchDarkly environments.
"""
template = self.env.get_template('ld-relay.conf.jinja')
with open('ld-relay.conf', 'w') as ld_relay_config:
template = template.render(
envs=environments
)
ld_relay_config.write(template) | python | def generate_relay_config(self, environments: list) -> None:
"""Generate ld-relay.conf file.
Given a list of environments of a project, this will generate a
``ld-relay.conf`` file in the current working directory. The conf file
follows the specification that is documented in the main `ld-relay`_
documentation.
.. _ld-relay: https://github.com/launchdarkly/ld-relay#configuration-file-format
:param environments: list of LaunchDarkly environments.
"""
template = self.env.get_template('ld-relay.conf.jinja')
with open('ld-relay.conf', 'w') as ld_relay_config:
template = template.render(
envs=environments
)
ld_relay_config.write(template) | [
"def",
"generate_relay_config",
"(",
"self",
",",
"environments",
":",
"list",
")",
"->",
"None",
":",
"template",
"=",
"self",
".",
"env",
".",
"get_template",
"(",
"'ld-relay.conf.jinja'",
")",
"with",
"open",
"(",
"'ld-relay.conf'",
",",
"'w'",
")",
"as",... | Generate ld-relay.conf file.
Given a list of environments of a project, this will generate a
``ld-relay.conf`` file in the current working directory. The conf file
follows the specification that is documented in the main `ld-relay`_
documentation.
.. _ld-relay: https://github.com/launchdarkly/ld-relay#configuration-file-format
:param environments: list of LaunchDarkly environments. | [
"Generate",
"ld",
"-",
"relay",
".",
"conf",
"file",
"."
] | eee7fa22f04edc3854dd53c3ec2db8c599ad1e89 | https://github.com/launchdarkly/relayCommander/blob/eee7fa22f04edc3854dd53c3ec2db8c599ad1e89/relay_commander/generators.py#L20-L38 | train | 51,386 |
SetBased/py-stratum | pystratum/ConstantClass.py | ConstantClass.__load | def __load(self):
"""
Loads dynamically the class that acts like a namespace for constants.
"""
parts = self.__class_name.split('.')
module_name = ".".join(parts[:-1])
module = __import__(module_name)
modules = []
for comp in parts[1:]:
module = getattr(module, comp)
modules.append(module)
self.__module = modules[-2] | python | def __load(self):
"""
Loads dynamically the class that acts like a namespace for constants.
"""
parts = self.__class_name.split('.')
module_name = ".".join(parts[:-1])
module = __import__(module_name)
modules = []
for comp in parts[1:]:
module = getattr(module, comp)
modules.append(module)
self.__module = modules[-2] | [
"def",
"__load",
"(",
"self",
")",
":",
"parts",
"=",
"self",
".",
"__class_name",
".",
"split",
"(",
"'.'",
")",
"module_name",
"=",
"\".\"",
".",
"join",
"(",
"parts",
"[",
":",
"-",
"1",
"]",
")",
"module",
"=",
"__import__",
"(",
"module_name",
... | Loads dynamically the class that acts like a namespace for constants. | [
"Loads",
"dynamically",
"the",
"class",
"that",
"acts",
"like",
"a",
"namespace",
"for",
"constants",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/ConstantClass.py#L53-L65 | train | 51,387 |
SetBased/py-stratum | pystratum/ConstantClass.py | ConstantClass.constants | def constants(self):
"""
Gets the constants from the class that acts like a namespace for constants.
:rtype: dict<str,*>
"""
ret = {}
name = self.__class_name.split('.')[-1]
constant_class = getattr(self.__module, name)
for name, value in constant_class.__dict__.items():
if re.match(r'^[A-Z][A-Z0-9_]*$', name):
ret[name] = value
return ret | python | def constants(self):
"""
Gets the constants from the class that acts like a namespace for constants.
:rtype: dict<str,*>
"""
ret = {}
name = self.__class_name.split('.')[-1]
constant_class = getattr(self.__module, name)
for name, value in constant_class.__dict__.items():
if re.match(r'^[A-Z][A-Z0-9_]*$', name):
ret[name] = value
return ret | [
"def",
"constants",
"(",
"self",
")",
":",
"ret",
"=",
"{",
"}",
"name",
"=",
"self",
".",
"__class_name",
".",
"split",
"(",
"'.'",
")",
"[",
"-",
"1",
"]",
"constant_class",
"=",
"getattr",
"(",
"self",
".",
"__module",
",",
"name",
")",
"for",
... | Gets the constants from the class that acts like a namespace for constants.
:rtype: dict<str,*> | [
"Gets",
"the",
"constants",
"from",
"the",
"class",
"that",
"acts",
"like",
"a",
"namespace",
"for",
"constants",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/ConstantClass.py#L93-L107 | train | 51,388 |
SetBased/py-stratum | pystratum/ConstantClass.py | ConstantClass.source_with_constants | def source_with_constants(self, constants):
"""
Returns the source of the module with the class that acts like a namespace for constants with new constants.
:param dict[str,int] constants: The new constants.
:rtype: str
"""
old_lines = self.source().split("\n")
info = self.__extract_info(old_lines)
new_lines = old_lines[0:info['start_line']]
for constant, value in sorted(constants.items()):
new_lines.append("{0}{1} = {2}".format(info['indent'], str(constant), str(value)))
new_lines.extend(old_lines[info['last_line']:])
return "\n".join(new_lines) | python | def source_with_constants(self, constants):
"""
Returns the source of the module with the class that acts like a namespace for constants with new constants.
:param dict[str,int] constants: The new constants.
:rtype: str
"""
old_lines = self.source().split("\n")
info = self.__extract_info(old_lines)
new_lines = old_lines[0:info['start_line']]
for constant, value in sorted(constants.items()):
new_lines.append("{0}{1} = {2}".format(info['indent'], str(constant), str(value)))
new_lines.extend(old_lines[info['last_line']:])
return "\n".join(new_lines) | [
"def",
"source_with_constants",
"(",
"self",
",",
"constants",
")",
":",
"old_lines",
"=",
"self",
".",
"source",
"(",
")",
".",
"split",
"(",
"\"\\n\"",
")",
"info",
"=",
"self",
".",
"__extract_info",
"(",
"old_lines",
")",
"new_lines",
"=",
"old_lines",... | Returns the source of the module with the class that acts like a namespace for constants with new constants.
:param dict[str,int] constants: The new constants.
:rtype: str | [
"Returns",
"the",
"source",
"of",
"the",
"module",
"with",
"the",
"class",
"that",
"acts",
"like",
"a",
"namespace",
"for",
"constants",
"with",
"new",
"constants",
"."
] | 7c5ffaa2fdd03f865832a5190b5897ff2c0e3155 | https://github.com/SetBased/py-stratum/blob/7c5ffaa2fdd03f865832a5190b5897ff2c0e3155/pystratum/ConstantClass.py#L154-L172 | train | 51,389 |
vsoch/helpme | helpme/main/base/settings.py | get_configfile_user | def get_configfile_user():
'''return the full path for the user configuration file. If doesn't
exist, create it for the user.
'''
from helpme.defaults import HELPME_CLIENT_SECRETS
# The inital file has a funny username
if not os.path.exists(HELPME_CLIENT_SECRETS):
bot.debug('Generating settings file at %s' % HELPME_CLIENT_SECRETS)
config_dir = os.path.dirname(HELPME_CLIENT_SECRETS)
# The configuration directory might be needed for different clients
if not os.path.exists(config_dir):
mkdir_p(config_dir)
name = RobotNamer().generate()
# Generate the user config
config = configparser.ConfigParser()
config['DEFAULT'] = {'Alias': name }
write_config(HELPME_CLIENT_SECRETS, config)
return HELPME_CLIENT_SECRETS | python | def get_configfile_user():
'''return the full path for the user configuration file. If doesn't
exist, create it for the user.
'''
from helpme.defaults import HELPME_CLIENT_SECRETS
# The inital file has a funny username
if not os.path.exists(HELPME_CLIENT_SECRETS):
bot.debug('Generating settings file at %s' % HELPME_CLIENT_SECRETS)
config_dir = os.path.dirname(HELPME_CLIENT_SECRETS)
# The configuration directory might be needed for different clients
if not os.path.exists(config_dir):
mkdir_p(config_dir)
name = RobotNamer().generate()
# Generate the user config
config = configparser.ConfigParser()
config['DEFAULT'] = {'Alias': name }
write_config(HELPME_CLIENT_SECRETS, config)
return HELPME_CLIENT_SECRETS | [
"def",
"get_configfile_user",
"(",
")",
":",
"from",
"helpme",
".",
"defaults",
"import",
"HELPME_CLIENT_SECRETS",
"# The inital file has a funny username",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"HELPME_CLIENT_SECRETS",
")",
":",
"bot",
".",
"debug",
... | return the full path for the user configuration file. If doesn't
exist, create it for the user. | [
"return",
"the",
"full",
"path",
"for",
"the",
"user",
"configuration",
"file",
".",
"If",
"doesn",
"t",
"exist",
"create",
"it",
"for",
"the",
"user",
"."
] | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/main/base/settings.py#L42-L66 | train | 51,390 |
vsoch/helpme | helpme/main/base/settings.py | remove_user_setting | def remove_user_setting(self, section, name, save=False):
'''remove a setting from the user config
'''
configfile = get_configfile_user()
return _remove_setting(section, name, configfile, save) | python | def remove_user_setting(self, section, name, save=False):
'''remove a setting from the user config
'''
configfile = get_configfile_user()
return _remove_setting(section, name, configfile, save) | [
"def",
"remove_user_setting",
"(",
"self",
",",
"section",
",",
"name",
",",
"save",
"=",
"False",
")",
":",
"configfile",
"=",
"get_configfile_user",
"(",
")",
"return",
"_remove_setting",
"(",
"section",
",",
"name",
",",
"configfile",
",",
"save",
")"
] | remove a setting from the user config | [
"remove",
"a",
"setting",
"from",
"the",
"user",
"config"
] | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/main/base/settings.py#L107-L111 | train | 51,391 |
vsoch/helpme | helpme/main/base/settings.py | _load_config | def _load_config(configfile, section=None):
'''general function to load and return a configuration given a helper
name. This function is used for both the user config and global help me
config files.
'''
if os.path.exists(configfile):
config = read_config(configfile)
if section is not None:
if section in config:
return config._sections[section]
else:
bot.warning('%s not found in %s' %(section, configfile))
return config | python | def _load_config(configfile, section=None):
'''general function to load and return a configuration given a helper
name. This function is used for both the user config and global help me
config files.
'''
if os.path.exists(configfile):
config = read_config(configfile)
if section is not None:
if section in config:
return config._sections[section]
else:
bot.warning('%s not found in %s' %(section, configfile))
return config | [
"def",
"_load_config",
"(",
"configfile",
",",
"section",
"=",
"None",
")",
":",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"configfile",
")",
":",
"config",
"=",
"read_config",
"(",
"configfile",
")",
"if",
"section",
"is",
"not",
"None",
":",
"if",... | general function to load and return a configuration given a helper
name. This function is used for both the user config and global help me
config files. | [
"general",
"function",
"to",
"load",
"and",
"return",
"a",
"configuration",
"given",
"a",
"helper",
"name",
".",
"This",
"function",
"is",
"used",
"for",
"both",
"the",
"user",
"config",
"and",
"global",
"help",
"me",
"config",
"files",
"."
] | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/main/base/settings.py#L114-L126 | train | 51,392 |
vsoch/helpme | helpme/main/base/settings.py | get_settings | def get_settings(self):
'''get all settings for a client, if defined in config.
'''
config = self._load_config_user()
if self.name in config:
return config[self.name] | python | def get_settings(self):
'''get all settings for a client, if defined in config.
'''
config = self._load_config_user()
if self.name in config:
return config[self.name] | [
"def",
"get_settings",
"(",
"self",
")",
":",
"config",
"=",
"self",
".",
"_load_config_user",
"(",
")",
"if",
"self",
".",
"name",
"in",
"config",
":",
"return",
"config",
"[",
"self",
".",
"name",
"]"
] | get all settings for a client, if defined in config. | [
"get",
"all",
"settings",
"for",
"a",
"client",
"if",
"defined",
"in",
"config",
"."
] | e609172260b10cddadb2d2023ab26da8082a9feb | https://github.com/vsoch/helpme/blob/e609172260b10cddadb2d2023ab26da8082a9feb/helpme/main/base/settings.py#L197-L202 | train | 51,393 |
nephila/djangocms-helper | djangocms_helper/runner.py | run | def run(app, argv=sys.argv, extra_args=None):
"""
Run commands in a plain django environment
:param app: application
:param argv: arguments (default to sys.argv)
:param extra_args: list of extra arguments
"""
if app not in argv[:2]:
# app is automatically added if not present
argv.insert(1, app)
if len(argv) < 3 and 'test' not in argv[:2]:
# test argument is given if not argument is passed
argv.insert(2, 'test')
if extra_args:
argv.extend(extra_args)
return runner(argv) | python | def run(app, argv=sys.argv, extra_args=None):
"""
Run commands in a plain django environment
:param app: application
:param argv: arguments (default to sys.argv)
:param extra_args: list of extra arguments
"""
if app not in argv[:2]:
# app is automatically added if not present
argv.insert(1, app)
if len(argv) < 3 and 'test' not in argv[:2]:
# test argument is given if not argument is passed
argv.insert(2, 'test')
if extra_args:
argv.extend(extra_args)
return runner(argv) | [
"def",
"run",
"(",
"app",
",",
"argv",
"=",
"sys",
".",
"argv",
",",
"extra_args",
"=",
"None",
")",
":",
"if",
"app",
"not",
"in",
"argv",
"[",
":",
"2",
"]",
":",
"# app is automatically added if not present",
"argv",
".",
"insert",
"(",
"1",
",",
... | Run commands in a plain django environment
:param app: application
:param argv: arguments (default to sys.argv)
:param extra_args: list of extra arguments | [
"Run",
"commands",
"in",
"a",
"plain",
"django",
"environment"
] | 3fe53aee7b06922112c5e4445b74afeb86f6d836 | https://github.com/nephila/djangocms-helper/blob/3fe53aee7b06922112c5e4445b74afeb86f6d836/djangocms_helper/runner.py#L9-L25 | train | 51,394 |
nephila/djangocms-helper | djangocms_helper/runner.py | cms | def cms(app, argv=sys.argv, extra_args=None):
"""
Run commands in a django cMS environment
:param app: application
:param argv: arguments (default to sys.argv)
:param extra_args: list of extra arguments
"""
try:
import cms # NOQA # nopyflakes
except ImportError:
print('runner.cms is available only if django CMS is installed')
raise
if app not in argv[:2]:
# app is automatically added if not present
argv.insert(1, app)
if len(argv) < 3 and 'test' not in argv[:2]:
# test argument is given if not argument is passed
argv.insert(2, 'test')
if '--cms' not in argv:
# this is the cms runner, just add the cms argument
argv.append('--cms')
if extra_args:
argv.extend(extra_args)
return runner(argv) | python | def cms(app, argv=sys.argv, extra_args=None):
"""
Run commands in a django cMS environment
:param app: application
:param argv: arguments (default to sys.argv)
:param extra_args: list of extra arguments
"""
try:
import cms # NOQA # nopyflakes
except ImportError:
print('runner.cms is available only if django CMS is installed')
raise
if app not in argv[:2]:
# app is automatically added if not present
argv.insert(1, app)
if len(argv) < 3 and 'test' not in argv[:2]:
# test argument is given if not argument is passed
argv.insert(2, 'test')
if '--cms' not in argv:
# this is the cms runner, just add the cms argument
argv.append('--cms')
if extra_args:
argv.extend(extra_args)
return runner(argv) | [
"def",
"cms",
"(",
"app",
",",
"argv",
"=",
"sys",
".",
"argv",
",",
"extra_args",
"=",
"None",
")",
":",
"try",
":",
"import",
"cms",
"# NOQA # nopyflakes",
"except",
"ImportError",
":",
"print",
"(",
"'runner.cms is available only if django CMS is installed'",
... | Run commands in a django cMS environment
:param app: application
:param argv: arguments (default to sys.argv)
:param extra_args: list of extra arguments | [
"Run",
"commands",
"in",
"a",
"django",
"cMS",
"environment"
] | 3fe53aee7b06922112c5e4445b74afeb86f6d836 | https://github.com/nephila/djangocms-helper/blob/3fe53aee7b06922112c5e4445b74afeb86f6d836/djangocms_helper/runner.py#L28-L52 | train | 51,395 |
jurismarches/chopper | chopper/html/extractor.py | HTMLExtractor.parse | def parse(self):
"""
Returns a cleaned lxml ElementTree
:returns: Whether the cleaned HTML has matches or not
:rtype: bool
"""
# Create the element tree
self.tree = self._build_tree(self.html_contents)
# Get explicits elements to keep and discard
self.elts_to_keep = self._get_elements_to_keep()
self.elts_to_discard = self._get_elements_to_discard()
# Init an empty list of Elements to remove
self.elts_to_remove = []
# Check if the root is a match or if there is any matches
is_root = self._is_keep(self.tree)
has_descendant = self._has_keep_elt_in_descendants(self.tree)
if not(is_root or has_descendant):
return False
# Parse and clean the ElementTree
self._parse_element(self.tree, parent_is_keep=is_root)
self._remove_elements(self.elts_to_remove)
return True | python | def parse(self):
"""
Returns a cleaned lxml ElementTree
:returns: Whether the cleaned HTML has matches or not
:rtype: bool
"""
# Create the element tree
self.tree = self._build_tree(self.html_contents)
# Get explicits elements to keep and discard
self.elts_to_keep = self._get_elements_to_keep()
self.elts_to_discard = self._get_elements_to_discard()
# Init an empty list of Elements to remove
self.elts_to_remove = []
# Check if the root is a match or if there is any matches
is_root = self._is_keep(self.tree)
has_descendant = self._has_keep_elt_in_descendants(self.tree)
if not(is_root or has_descendant):
return False
# Parse and clean the ElementTree
self._parse_element(self.tree, parent_is_keep=is_root)
self._remove_elements(self.elts_to_remove)
return True | [
"def",
"parse",
"(",
"self",
")",
":",
"# Create the element tree",
"self",
".",
"tree",
"=",
"self",
".",
"_build_tree",
"(",
"self",
".",
"html_contents",
")",
"# Get explicits elements to keep and discard",
"self",
".",
"elts_to_keep",
"=",
"self",
".",
"_get_e... | Returns a cleaned lxml ElementTree
:returns: Whether the cleaned HTML has matches or not
:rtype: bool | [
"Returns",
"a",
"cleaned",
"lxml",
"ElementTree"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/html/extractor.py#L40-L68 | train | 51,396 |
jurismarches/chopper | chopper/html/extractor.py | HTMLExtractor.rel_to_abs | def rel_to_abs(self, base_url):
"""
Converts relative links from html contents to absolute links
"""
# Delete target attributes
strip_attributes(self.tree, 'target')
# Absolute links
self.tree.rewrite_links(
lambda link: urljoin(base_url, link)
if not link.startswith(self.rel_to_abs_excluded_prefixes) else link)
# Extra attributes
onclick_elements = self.tree.xpath('//*[@onclick]')
for element in onclick_elements:
# Replace attribute with absolute URL
element.set('onclick', self.javascript_open_re.sub(
lambda match: '%s%s%s' % (match.group('opening'),
urljoin(base_url, match.group('url')),
match.group('ending')),
element.get('onclick'))) | python | def rel_to_abs(self, base_url):
"""
Converts relative links from html contents to absolute links
"""
# Delete target attributes
strip_attributes(self.tree, 'target')
# Absolute links
self.tree.rewrite_links(
lambda link: urljoin(base_url, link)
if not link.startswith(self.rel_to_abs_excluded_prefixes) else link)
# Extra attributes
onclick_elements = self.tree.xpath('//*[@onclick]')
for element in onclick_elements:
# Replace attribute with absolute URL
element.set('onclick', self.javascript_open_re.sub(
lambda match: '%s%s%s' % (match.group('opening'),
urljoin(base_url, match.group('url')),
match.group('ending')),
element.get('onclick'))) | [
"def",
"rel_to_abs",
"(",
"self",
",",
"base_url",
")",
":",
"# Delete target attributes",
"strip_attributes",
"(",
"self",
".",
"tree",
",",
"'target'",
")",
"# Absolute links",
"self",
".",
"tree",
".",
"rewrite_links",
"(",
"lambda",
"link",
":",
"urljoin",
... | Converts relative links from html contents to absolute links | [
"Converts",
"relative",
"links",
"from",
"html",
"contents",
"to",
"absolute",
"links"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/html/extractor.py#L70-L91 | train | 51,397 |
jurismarches/chopper | chopper/html/extractor.py | HTMLExtractor._get_elements | def _get_elements(self, source):
"""
Returns the list of HtmlElements for the source
:param source: The source list to parse
:type source: list
:returns: A list of HtmlElements
:rtype: list
"""
return list(chain(*[self.tree.xpath(xpath) for xpath in source])) | python | def _get_elements(self, source):
"""
Returns the list of HtmlElements for the source
:param source: The source list to parse
:type source: list
:returns: A list of HtmlElements
:rtype: list
"""
return list(chain(*[self.tree.xpath(xpath) for xpath in source])) | [
"def",
"_get_elements",
"(",
"self",
",",
"source",
")",
":",
"return",
"list",
"(",
"chain",
"(",
"*",
"[",
"self",
".",
"tree",
".",
"xpath",
"(",
"xpath",
")",
"for",
"xpath",
"in",
"source",
"]",
")",
")"
] | Returns the list of HtmlElements for the source
:param source: The source list to parse
:type source: list
:returns: A list of HtmlElements
:rtype: list | [
"Returns",
"the",
"list",
"of",
"HtmlElements",
"for",
"the",
"source"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/html/extractor.py#L106-L115 | train | 51,398 |
jurismarches/chopper | chopper/html/extractor.py | HTMLExtractor._parse_element | def _parse_element(self, elt, parent_is_keep=False):
"""
Parses an Element recursively
:param elt: HtmlElement to parse
:type elt: lxml.html.HtmlElement
:param parent_is_keep: Whether the element is inside a keep element or not
:type parent_is_keep: bool
"""
for e in elt.iterchildren():
is_discard_element = self._is_discard(e)
is_keep_element = self._is_keep(e)
# Element is an explicit one to discard, flag it and continue
if is_discard_element and not is_keep_element:
self.elts_to_remove.append(e)
continue
if not parent_is_keep:
# Parent element is not an explicit keep, normal process
# Element is an explicit one to keep, inspect it
if is_keep_element:
self._parse_element(e, parent_is_keep=True)
continue
# Has a descendant to keep, inspect it
if self._has_keep_elt_in_descendants(e):
self._parse_element(e)
continue
# Element did not match anything, remove it
self.elts_to_remove.append(e)
else:
# Element is a child of a keep element, only check explicit discards
self._parse_element(e, parent_is_keep=True) | python | def _parse_element(self, elt, parent_is_keep=False):
"""
Parses an Element recursively
:param elt: HtmlElement to parse
:type elt: lxml.html.HtmlElement
:param parent_is_keep: Whether the element is inside a keep element or not
:type parent_is_keep: bool
"""
for e in elt.iterchildren():
is_discard_element = self._is_discard(e)
is_keep_element = self._is_keep(e)
# Element is an explicit one to discard, flag it and continue
if is_discard_element and not is_keep_element:
self.elts_to_remove.append(e)
continue
if not parent_is_keep:
# Parent element is not an explicit keep, normal process
# Element is an explicit one to keep, inspect it
if is_keep_element:
self._parse_element(e, parent_is_keep=True)
continue
# Has a descendant to keep, inspect it
if self._has_keep_elt_in_descendants(e):
self._parse_element(e)
continue
# Element did not match anything, remove it
self.elts_to_remove.append(e)
else:
# Element is a child of a keep element, only check explicit discards
self._parse_element(e, parent_is_keep=True) | [
"def",
"_parse_element",
"(",
"self",
",",
"elt",
",",
"parent_is_keep",
"=",
"False",
")",
":",
"for",
"e",
"in",
"elt",
".",
"iterchildren",
"(",
")",
":",
"is_discard_element",
"=",
"self",
".",
"_is_discard",
"(",
"e",
")",
"is_keep_element",
"=",
"s... | Parses an Element recursively
:param elt: HtmlElement to parse
:type elt: lxml.html.HtmlElement
:param parent_is_keep: Whether the element is inside a keep element or not
:type parent_is_keep: bool | [
"Parses",
"an",
"Element",
"recursively"
] | 53c5489a53e3a5d205a5cb207df751c09633e7ce | https://github.com/jurismarches/chopper/blob/53c5489a53e3a5d205a5cb207df751c09633e7ce/chopper/html/extractor.py#L135-L171 | train | 51,399 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.