code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=ctx.mets_basename, automatic_backup=ctx.automatic_backup)
if not local_filename.startswith(ctx.directory):
log.debug("File '%s' is not in workspace, copying", local_filename)
local_filename = ctx.resolver.download_t... | def workspace_add_file(ctx, file_grp, file_id, mimetype, page_id, force, local_filename) | Add a file LOCAL_FILENAME to METS in a workspace. | 3.383956 | 3.237118 | 1.045361 |
workspace = Workspace(ctx.resolver, directory=ctx.directory, mets_basename=ctx.mets_basename)
for f in workspace.mets.find_files(
ID=file_id,
fileGrp=file_grp,
mimetype=mimetype,
pageId=page_id,
):
if download:
workspace.download_f... | def workspace_find(ctx, file_grp, mimetype, page_id, file_id, output_field, download) | Find files. | 3.658341 | 3.962117 | 0.92333 |
backup_manager = WorkspaceBackupManager(Workspace(ctx.resolver, directory=ctx.directory, mets_basename=ctx.mets_basename, automatic_backup=ctx.automatic_backup))
backup_manager.add() | def workspace_backup_add(ctx) | Create a new backup | 8.000204 | 8.676848 | 0.922017 |
backup_manager = WorkspaceBackupManager(Workspace(ctx.resolver, directory=ctx.directory, mets_basename=ctx.mets_basename, automatic_backup=ctx.automatic_backup))
for b in backup_manager.list():
print(b) | def workspace_backup_list(ctx) | List backups | 6.98824 | 7.534602 | 0.927486 |
backup_manager = WorkspaceBackupManager(Workspace(ctx.resolver, directory=ctx.directory, mets_basename=ctx.mets_basename, automatic_backup=ctx.automatic_backup))
backup_manager.restore(bak, choose_first) | def workspace_backup_restore(ctx, choose_first, bak) | Restore backup BAK | 6.633183 | 6.818052 | 0.972885 |
backup_manager = WorkspaceBackupManager(Workspace(ctx.resolver, directory=ctx.directory, mets_basename=ctx.mets_basename, automatic_backup=ctx.automatic_backup))
backup_manager.undo() | def workspace_backup_undo(ctx) | Restore the last backup | 7.602122 | 7.948053 | 0.956476 |
validate_properties = validator_class.VALIDATORS["properties"]
def set_defaults(validator, properties, instance, schema):
for prop, subschema in properties.items():
if "default" in subschema:
instance.setdefault(prop, subschema["default"])
for error in... | def extend_with_default(validator_class) | Add a default-setting mechanism to a ``jsonschema`` validation class. | 1.831968 | 1.89877 | 0.964818 |
if isinstance(obj, str):
obj = json.loads(obj)
return JsonValidator(schema)._validate(obj) | def validate(obj, schema) | Validate an object against a schema
Args:
obj (dict):
schema (dict): | 4.937128 | 5.634586 | 0.876218 |
report = ValidationReport()
if not self.validator.is_valid(obj):
for v in self.validator.iter_errors(obj):
report.add_error("[%s] %s" % ('.'.join(str(vv) for vv in v.path), v.message))
return report | def _validate(self, obj) | Do the actual validation
Arguments:
obj (dict): object to validate
Returns: ValidationReport | 3.072313 | 3.028396 | 1.014502 |
if not '://' in parameter:
fname = os.path.abspath(parameter)
else:
fname = workspace.download_url(parameter)
with open(fname, 'r') as param_json_file:
parameter = json.load(param_json_file)
else:
parameter = {}
log.debug("Running processor %s"... | def run_processor(
processorClass,
ocrd_tool=None,
mets_url=None,
resolver=None,
workspace=None,
page_id=None,
log_level=None,
input_file_grp=None,
output_file_grp=None,
parameter=None,
working_dir=None,
): # pylint: disable=too-man... | Create a workspace for mets_url and run processor through it
Args:
parameter (string): URL to the parameter | 3.440113 | 3.538326 | 0.972243 |
workspace = _get_workspace(workspace, resolver, mets_url, working_dir)
args = [executable, '--working-dir', workspace.directory]
args += ['--mets', mets_url]
if log_level:
args += ['--log-level', log_level]
if page_id:
args += ['--page-id', page_id]
if input_file_grp:
... | def run_cli(
executable,
mets_url=None,
resolver=None,
workspace=None,
page_id=None,
log_level=None,
input_file_grp=None,
output_file_grp=None,
parameter=None,
working_dir=None,
) | Create a workspace for mets_url and run MP CLI through it | 1.870114 | 1.789898 | 1.044816 |
return self.workspace.mets.find_files(fileGrp=self.input_file_grp, pageId=self.page_id) | def input_files(self) | List the input files | 11.132594 | 10.635579 | 1.046731 |
if input_file.local_filename is None:
raise Exception("input_file must have 'local_filename' property")
exif = exif_from_filename(input_file.local_filename)
now = datetime.now()
return PcGtsType(
Metadata=MetadataType(
Creator="OCR-D/core %s" % VERSION,
Creat... | def page_from_image(input_file) | Create `OcrdPage </../../ocrd_models/ocrd_models.ocrd_page.html>`_
from an `OcrdFile </../../ocrd_models/ocrd_models.ocrd_file.html>`_
representing an image (i.e. should have ``mimetype`` starting with ``image/``).
Arguments:
* input_file (OcrdFile): | 4.570951 | 4.374844 | 1.044826 |
# print("PARSING PARSING '%s'" % input_file)
if input_file.mimetype.startswith('image'):
return page_from_image(input_file)
if input_file.mimetype == MIMETYPE_PAGE:
return parse(input_file.local_filename, silence=True)
raise Exception("Unsupported mimetype '%s'" % input_file.mimety... | def page_from_file(input_file) | Create a new PAGE-XML from a METS file representing a PAGE-XML or an image.
Arguments:
* input_file (OcrdFile): | 4.269522 | 4.428517 | 0.964097 |
ret = base
for n in args:
if is_string(n):
ret = "%s_%s" % (ret, n)
else:
ret = "%s_%04i" % (ret, n + 1)
return ret | def concat_padded(base, *args) | Concatenate string and zero-padded 4 digit number | 3.403105 | 3.271423 | 1.040252 |
x, y, w, h = box['x'], box['y'], box['w'], box['h']
# tesseract uses a different region representation format
return "%i,%i %i,%i %i,%i %i,%i" % (
x, y,
x + w, y,
x + w, y + h,
x, y + h
) | def points_from_xywh(box) | Constructs a polygon representation from a rectangle described as a dict with keys x, y, w, h. | 3.094465 | 2.793699 | 1.107659 |
y0 = yxyx[0]
x0 = yxyx[1]
y1 = yxyx[2]
x1 = yxyx[3]
return "%s,%s %s,%s %s,%s %s,%s" % (
x0, y0,
x1, y0,
x1, y1,
x0, y1
) | def points_from_y0x0y1x1(yxyx) | Constructs a polygon representation from a rectangle described as a list [y0, x0, y1, x1] | 1.643988 | 1.618785 | 1.015569 |
x0 = xyxy[0]
y0 = xyxy[1]
x1 = xyxy[2]
y1 = xyxy[3]
return "%s,%s %s,%s %s,%s %s,%s" % (
x0, y0,
x1, y0,
x1, y1,
x0, y1
) | def points_from_x0y0x1y1(xyxy) | Constructs a polygon representation from a rectangle described as a list [x0, y0, x1, y1] | 1.673154 | 1.59115 | 1.051538 |
polygon = []
for pair in points.split(" "):
x_y = pair.split(",")
polygon.append([float(x_y[0]), float(x_y[1])])
return polygon | def polygon_from_points(points) | Constructs a numpy-compatible polygon from a page representation. | 2.831852 | 2.470861 | 1.146099 |
z = ZipFile(path_to_zip, 'r')
z.extractall(output_directory)
z.close() | def unzip_file_to_dir(path_to_zip, output_directory) | Extract a ZIP archive to a directory | 2.115422 | 2.166852 | 0.976265 |
xys = [[int(p) for p in pair.split(',')] for pair in points.split(' ')]
minx = sys.maxsize
miny = sys.maxsize
maxx = 0
maxy = 0
for xy in xys:
if xy[0] < minx:
minx = xy[0]
if xy[0] > maxx:
maxx = xy[0]
if xy[1] < miny:
miny = xy[1... | def xywh_from_points(points) | Constructs an dict representing a rectangle with keys x, y, w, h | 1.581226 | 1.552855 | 1.01827 |
if not self.profile_validator.validate(bag):
raise Exception(str(self.profile_validator.report)) | def _validate_profile(self, bag) | Validate against OCRD BagIt profile (bag-info fields, algos etc) | 5.919881 | 4.396905 | 1.346375 |
failed = None
try:
bag.validate(**kwargs)
except BagValidationError as e:
failed = e
# for d in e.details:
# if isinstance(d, ChecksumMismatch):
# log.error("Validation Error: expected %s to have %s checksum of %... | def _validate_bag(self, bag, **kwargs) | Validate BagIt (checksums, payload.oxum etc) | 3.679055 | 3.395609 | 1.083474 |
if skip_unzip:
bagdir = self.path_to_zip
skip_delete = True
else:
# try:
self.profile_validator.validate_serialization(self.path_to_zip)
# except IOError as err:
# raise err
# except ProfileValidationErr... | def validate(self, skip_checksums=False, skip_bag=False, skip_unzip=False, skip_delete=False, processes=2) | Validate an OCRD-ZIP file for profile, bag and workspace conformance
Arguments:
skip_bag (boolean): Whether to skip all checks of manifests and files
skip_checksums (boolean): Whether to omit checksum checks but still check basic BagIt conformance
skip_unzip (boolean): Wheth... | 3.838465 | 3.816116 | 1.005857 |
"Escape markup chars, but do not modify CDATA sections."
if not inStr:
return ''
s1 = (isinstance(inStr, BaseStrType_) and inStr or '%s' % inStr)
s2 = ''
pos = 0
matchobjects = CDATA_pattern_.finditer(s1)
for mo in matchobjects:
s3 = s1[pos:mo.start()]
s2 += quote_xml... | def quote_xml(inStr) | Escape markup chars, but do not modify CDATA sections. | 1.326504 | 1.370138 | 0.968154 |
'''Parse a string, create the object tree, and export it.
Arguments:
- inString -- A string. This XML fragment should not start
with an XML declaration containing an encoding.
- silence -- A boolean. If False, export the object.
Returns -- The root object in the tree.
'''
parser = N... | def parseString(inString, silence=False) | Parse a string, create the object tree, and export it.
Arguments:
- inString -- A string. This XML fragment should not start
with an XML declaration containing an encoding.
- silence -- A boolean. If False, export the object.
Returns -- The root object in the tree. | 1.558154 | 1.34141 | 1.16158 |
if parameters is None or parameters == "":
parameters = {}
else:
with open(parameters, 'r') as f:
parameters = loads(f.read())
parameterValidator = ParameterValidator(ctx.json['tools'][ctx.tool_name])
report = parameterValidator.validate(parameters)
if not report.is_... | def ocrd_tool_tool_parse_params(ctx, parameters, json) | Parse parameters with fallback to defaults and output as shell-eval'able assignments to params var. | 3.165954 | 3.010276 | 1.051716 |
if othertype is not None:
self._el.set('TYPE', 'OTHER')
self._el.set('OTHERTYPE', othertype) | def othertype(self, othertype) | Set the ``OTHERTYPE`` attribute value. | 4.351541 | 3.512406 | 1.238906 |
if otherrole is not None:
self._el.set('ROLE', 'OTHER')
self._el.set('OTHERROLE', otherrole) | def otherrole(self, otherrole) | Get the ``OTHERROLE`` attribute value. | 4.986941 | 4.099153 | 1.216579 |
el_name = self._el.find('mets:name', NS)
if el_name is not None:
return el_name.text | def name(self) | Get the ``mets:name`` element value. | 5.553421 | 2.883925 | 1.925647 |
if name is not None:
el_name = self._el.find('mets:name', NS)
if el_name is None:
el_name = ET.SubElement(self._el, TAG_METS_NAME)
el_name.text = name | def name(self, name) | Get the ``mets:name`` element value. | 3.272446 | 2.561126 | 1.277737 |
def validate(self, *args, **kwargs): # pylint: disable=arguments-differ
return super(ParameterValidator, self)._validate(*args, **kwargs) | Validate a parameter dict against a parameter schema from an ocrd-tool.json
Args:
obj (dict):
schema (dict): | null | null | null | |
if isinstance(node, PcGtsType):
node = node.get_Page()
elif isinstance(node, GlyphType):
return report
_, tag, getter, concatenate_with = [x for x in _HIERARCHY if isinstance(node, x[0])][0]
children_are_consistent = True
children = getattr(node, getter)()
for child in chil... | def handle_inconsistencies(node, strictness, strategy, report) | Check whether the text results on an element is consistent with its child element text results. | 3.748333 | 3.46029 | 1.083242 |
_, _, getter, concatenate_with = [x for x in _HIERARCHY if isinstance(node, x[0])][0]
tokens = [get_text(x, strategy) for x in getattr(node, getter)()]
return concatenate_with.join(tokens).strip() | def concatenate_children(node, concatenate_with, strategy) | Concatenate children of node according to https://ocr-d.github.io/page#consistency-of-text-results-on-different-levels | 5.979923 | 5.654291 | 1.05759 |
textEquivs = node.get_TextEquiv()
if not textEquivs:
log.debug("No text results on %s %s", node, node.id)
return ''
# elif strategy == 'index1':
else:
if len(textEquivs) > 1:
index1 = [x for x in textEquivs if x.index == 1]
if index1:
... | def get_text(node, strategy) | Get the most confident text results, either those with @index = 1 or the first text results or empty string. | 3.994896 | 3.250966 | 1.228833 |
text = text.strip()
textEquivs = node.get_TextEquiv()
if not textEquivs:
node.add_TextEquiv(TextEquivType(Unicode=text))
# elif strategy == 'index1':
else:
if len(textEquivs) > 1:
index1 = [x for x in textEquivs if x.index == 1]
if index1:
... | def set_text(node, text, strategy) | Set the most confident text results, either those with @index = 1, the first text results or add new one. | 3.360749 | 3.049021 | 1.102239 |
if ocrd_page:
validator = PageValidator(ocrd_page, strictness, strategy)
elif ocrd_file:
validator = PageValidator(page_from_file(ocrd_file), strictness, strategy)
elif filename:
validator = PageValidator(parse(filename, silence=True), strictness, str... | def validate(filename=None, ocrd_page=None, ocrd_file=None, strictness='strict', strategy='index1') | Validates a PAGE file for consistency by filename, OcrdFile or passing OcrdPage directly.
Arguments:
filename (string): Path to PAGE
ocrd_page (OcrdPage): OcrdPage instance
ocrd_file (OcrdFile): OcrdFile instance wrapping OcrdPage
strictness (string): 'strict', '... | 2.358401 | 2.782185 | 0.847679 |
if self.strictness == 'off':
return self.report
handle_inconsistencies(self.page, self.strictness, self.strategy, self.report)
return self.report | def _validate(self) | Do the actual validation | 10.26483 | 9.118062 | 1.125769 |
params = [
click.option('-m', '--mets', help="METS URL to validate"),
click.option('-w', '--working-dir', help="Working Directory"),
click.option('-I', '--input-file-grp', help='File group(s) used as input.', default='INPUT'),
click.option('-O', '--output-file-grp', help='File g... | def ocrd_cli_options(f) | Implement MP CLI.
Usage::
import ocrd_click_cli from ocrd.utils
@click.command()
@ocrd_click_cli
def cli(mets_url):
print(mets_url) | 3.215955 | 3.249091 | 0.989802 |
body = ''
for k in ['warning', 'error', 'notice']:
for msg in self.__dict__[k + 's']:
body += '\n <%s>%s</%s>' % (k, msg, k)
return '<report valid="%s">%s\n</report>' % ("true" if self.is_valid else "false", body) | def to_xml(self) | Serialize to XML. | 4.026864 | 3.89965 | 1.032622 |
self.notices += otherself.notices
self.warnings += otherself.warnings
self.errors += otherself.errors | def merge_report(self, otherself) | Merge another report into this one. | 3.244481 | 2.644099 | 1.227065 |
log = getLogger('ocrd.cli.process')
run_tasks(mets, log_level, page_id, tasks)
log.info("Finished") | def process_cli(log_level, mets, page_id, tasks) | Process a series of tasks | 5.858285 | 5.816246 | 1.007228 |
resolver = Resolver()
workspace = Workspace(resolver, directory=directory, mets_basename=mets_basename)
workspace_bagger = WorkspaceBagger(resolver)
workspace_bagger.bag(
workspace,
dest=dest,
ocrd_identifier=identifier,
ocrd_manifestation_depth=manifestation_depth,
... | def bag(directory, mets_basename, dest, identifier, in_place, manifestation_depth, mets, base_version_checksum, tag_file, skip_zip, processes) | Bag workspace as OCRD-ZIP at DEST | 2.573303 | 2.3988 | 1.072746 |
resolver = Resolver()
workspace_bagger = WorkspaceBagger(resolver)
workspace = workspace_bagger.spill(src, directory)
print(workspace) | def spill(directory, src) | Spill/unpack OCRD-ZIP bag at SRC to DEST
SRC must exist an be an OCRD-ZIP
DEST must not exist and be a directory | 7.32547 | 7.28523 | 1.005524 |
resolver = Resolver()
validator = OcrdZipValidator(resolver, src)
report = validator.validate(**kwargs)
print(report)
if not report.is_valid:
sys.exit(1) | def validate(src, **kwargs) | Validate OCRD-ZIP
SRC must exist an be an OCRD-ZIP, either a ZIP file or a directory. | 5.51762 | 3.959104 | 1.393654 |
log = getLogger('ocrd.workspace_backup.restore')
bak = None
candidates = glob(join(self.backup_directory, '%s*' % chksum))
if not candidates:
log.error("No backup found: %s" % chksum)
return
if len(candidates) > 1 and not choose_first:
... | def restore(self, chksum, choose_first=False) | Restore mets.xml to previous state | 3.798871 | 3.658676 | 1.038319 |
log = getLogger('ocrd.workspace_backup.add')
mets_str = self.workspace.mets.to_xml()
chksum = _chksum(mets_str)
backups = self.list()
if backups and backups[0].chksum == chksum:
log.info('No changes since last backup: %s' % backups[0])
else:
... | def add(self) | Create a backup in <self.backup_directory> | 3.530754 | 3.19964 | 1.103485 |
backups = []
for d in glob(join(self.backup_directory, '*')):
backups.append(WorkspaceBackup.from_path(d))
backups.sort(key=lambda b: b.lastmod, reverse=True)
return backups | def list(self) | List all backups as WorkspaceBackup objects, sorted descending by lastmod. | 5.385238 | 2.771341 | 1.943189 |
log = getLogger('ocrd.workspace_backup.undo')
backups = self.list()
if backups:
last_backup = backups[0]
self.restore(last_backup.chksum, choose_first=True)
else:
log.info("No backups, nothing to undo.") | def undo(self) | Restore to last version | 8.4953 | 7.593807 | 1.118714 |
sio = StringIO()
el.export(sio, 0, name_='PcGts', namespacedef_='xmlns:pc="%s"' % NAMESPACES['page'])
return '<?xml version="1.0" encoding="UTF-8"?>\n' + sio.getvalue() | def to_xml(el) | Serialize ``pc:PcGts`` document | 3.732405 | 3.025847 | 1.233508 |
lvl = _ocrdLevel2pythonLevel.get(lvl, lvl)
return logging.getLevelName(lvl) | def getLevelName(lvl) | Get (numerical) python logging level for (string) spec-defined log level name. | 11.52422 | 8.653856 | 1.331686 |
if lvl is None:
return
logging.info('Overriding log level globally to %s', lvl)
lvl = getLevelName(lvl)
global _overrideLogLevel # pylint: disable=global-statement
_overrideLogLevel = lvl
logging.getLogger('').setLevel(lvl)
for loggerName in logging.Logger.manager.loggerDict:
... | def setOverrideLogLevel(lvl) | Override all logger filter levels to include lvl and above.
- Set root logger level
- iterates all existing loggers and sets their log level to ``NOTSET``.
Args:
lvl (string): Log level name. | 2.892003 | 3.164366 | 0.913928 |
logger = logging.getLogger(*args, **kwargs)
if _overrideLogLevel is not None:
logger.setLevel(logging.NOTSET)
return logger | def getLogger(*args, **kwargs) | Wrapper around ``logging.getLogger`` that respects `overrideLogLevel <#setOverrideLogLevel>`_. | 4.183034 | 2.922846 | 1.431151 |
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s.%(msecs)03d %(levelname)s %(name)s - %(message)s',
datefmt='%H:%M:%S')
logging.getLogger('').setLevel(logging.INFO)
# logging.getLogger('ocrd.resolver').setLevel(logging.INFO)
# logging.getLogger('ocrd.resolver.d... | def initLogging() | Sets logging defaults | 2.352602 | 2.339586 | 1.005564 |
if ocrd_manifestation_depth not in ('full', 'partial'):
raise Exception("manifestation_depth must be 'full' or 'partial'")
if in_place and (dest is not None):
raise Exception("Setting 'dest' and 'in_place' is a contradiction")
if in_place and not skip_zip:
... | def bag(self,
workspace,
ocrd_identifier,
dest=None,
ocrd_mets='mets.xml',
ocrd_manifestation_depth='full',
ocrd_base_version_checksum=None,
processes=1,
skip_zip=False,
in_place=False,
tag_files=None... | Bag a workspace
See https://ocr-d.github.com/ocrd_zip#packing-a-workspace-as-ocrd-zip
Arguments:
workspace (ocrd.Workspace): workspace to bag
ord_identifier (string): Ocrd-Identifier in bag-info.txt
dest (string): Path of the generated OCRD-ZIP.
ord_mets... | 2.609237 | 2.6075 | 1.000666 |
# print(dest)
if exists(dest) and not isdir(dest):
raise Exception("Not a directory: %s" % dest)
# If dest is an existing directory, try to derive its name from src
if isdir(dest):
workspace_name = re.sub(r'(\.ocrd)?\.zip$', '', basename(src))
... | def spill(self, src, dest) | Spill a workspace, i.e. unpack it and turn it into a workspace.
See https://ocr-d.github.com/ocrd_zip#unpacking-ocrd-zip-to-a-workspace
Arguments:
src (string): Path to OCRD-ZIP
dest (string): Path to directory to unpack data folder to | 3.350725 | 3.073085 | 1.090345 |
if self.baseurl and '://' not in url:
url = join(self.baseurl, url)
return self.resolver.download_to_directory(self.directory, url, **kwargs) | def download_url(self, url, **kwargs) | Download a URL to the workspace.
Args:
url (string): URL to download to directory
**kwargs : See :py:mod:`ocrd.resolver.Resolver`
Returns:
The local filename of the downloaded file | 5.215705 | 6.141569 | 0.849246 |
# os.chdir(self.directory)
# log.info('f=%s' % f)
oldpwd = os.getcwd()
try:
os.chdir(self.directory)
if is_local_filename(f.url):
f.local_filename = abspath(f.url)
else:
if f.local_filename:
... | def download_file(self, f) | Download a :py:mod:`ocrd.model.ocrd_file.OcrdFile` to the workspace. | 4.018727 | 3.781966 | 1.062603 |
log.debug(
'outputfile file_grp=%s local_filename=%s content=%s',
file_grp,
kwargs.get('local_filename'),
content is not None)
if content is not None and 'local_filename' not in kwargs:
raise Exception("'content' was set but no 'local_... | def add_file(self, file_grp, content=None, **kwargs) | Add an output file. Creates an :class:`OcrdFile` to pass around and adds that to the
OcrdMets OUTPUT section. | 2.565093 | 2.465346 | 1.04046 |
log.info("Saving mets '%s'" % self.mets_target)
if self.automatic_backup:
WorkspaceBackupManager(self).add()
with open(self.mets_target, 'wb') as f:
f.write(self.mets.to_xml(xmllint=True)) | def save_mets(self) | Write out the current state of the METS file. | 5.262517 | 5.138351 | 1.024165 |
files = self.mets.find_files(url=image_url)
if files:
image_filename = self.download_file(files[0]).local_filename
else:
image_filename = self.download_url(image_url)
if image_url not in self.image_cache['exif']:
self.image_cache['exif'][imag... | def resolve_image_exif(self, image_url) | Get the EXIF metadata about an image URL as :class:`OcrdExif`
Args:
image_url (string) : URL of image
Return
:class:`OcrdExif` | 3.122411 | 3.174863 | 0.983479 |
files = self.mets.find_files(url=image_url)
if files:
image_filename = self.download_file(files[0]).local_filename
else:
image_filename = self.download_url(image_url)
if image_url not in self.image_cache['pil']:
self.image_cache['pil'][image_... | def resolve_image_as_pil(self, image_url, coords=None) | Resolve an image URL to a PIL image.
Args:
coords (list) : Coordinates of the bounding box to cut from the image
Returns:
Image or region in image as PIL.Image | 2.222643 | 2.231815 | 0.99589 |
ret = '<exif>'
for k in self.__dict__:
ret += '<%s>%s</%s>' % (k, self.__dict__[k], k)
ret += '</exif>'
return ret | def to_xml(self) | Serialize all properties as XML | 2.675498 | 2.570735 | 1.040752 |
ret = self.basename.rsplit('.', 1)[0]
if ret.endswith('.tar'):
ret = ret[0:len(ret)-4]
return ret | def basename_without_extension(self) | Get the ``os.path.basename`` of the local file, if any, with extension removed. | 3.898163 | 4.044707 | 0.963769 |
if self.mets is None:
raise Exception("OcrdFile %s has no member 'mets' pointing to parent OcrdMets" % self)
return self.mets.get_physical_page_for_file(self) | def pageId(self) | Get the ID of the physical page this file manifests. | 11.056329 | 8.546486 | 1.29367 |
if pageId is None:
return
if self.mets is None:
raise Exception("OcrdFile %s has no member 'mets' pointing to parent OcrdMets" % self)
self.mets.set_physical_page_for_file(pageId, self) | def pageId(self, pageId) | Set the ID of the physical page this file manifests. | 9.231158 | 8.073985 | 1.143321 |
el_FLocat = self._el.find(TAG_METS_FLOCAT)
if el_FLocat is not None:
return el_FLocat.get("{%s}href" % NS["xlink"])
return '' | def url(self) | Get the ``xlink:href`` of this file. | 5.55443 | 4.465397 | 1.243883 |
if url is None:
return
el_FLocat = self._el.find('mets:FLocat', NS)
if el_FLocat is None:
el_FLocat = ET.SubElement(self._el, TAG_METS_FLOCAT)
el_FLocat.set("{%s}href" % NS["xlink"], url) | def url(self, url) | Set the ``xlink:href`` of this file. | 3.50469 | 3.326192 | 1.053664 |
tpl = METS_XML_EMPTY.decode('utf-8')
tpl = tpl.replace('{{ VERSION }}', VERSION)
tpl = tpl.replace('{{ NOW }}', '%s' % datetime.now())
return OcrdMets(content=tpl.encode('utf-8')) | def empty_mets() | Create an empty METS file from bundled template. | 6.569492 | 6.099357 | 1.077079 |
for t in IDENTIFIER_PRIORITY:
found = self._tree.getroot().find('.//mods:identifier[@type="%s"]' % t, NS)
if found is not None:
return found.text | def unique_identifier(self) | Get the unique identifier by looking through ``mods:identifier``
See `specs <https://ocr-d.github.io/mets#unique-id-for-the-document-processed>`_ for details. | 7.981709 | 6.15626 | 1.296519 |
id_el = None
for t in IDENTIFIER_PRIORITY:
id_el = self._tree.getroot().find('.//mods:identifier[@type="%s"]' % t, NS)
if id_el is not None:
break
if id_el is None:
mods = self._tree.getroot().find('.//mods:mods', NS)
id_el... | def unique_identifier(self, purl) | Set the unique identifier by looking through ``mods:identifier``
See `specs <https://ocr-d.github.io/mets#unique-id-for-the-document-processed>`_ for details. | 3.290498 | 3.125207 | 1.052889 |
return [OcrdAgent(el_agent) for el_agent in self._tree.getroot().findall('mets:metsHdr/mets:agent', NS)] | def agents(self) | List all `OcrdAgent </../../ocrd_models/ocrd_models.ocrd_agent.html>`_ | 12.595526 | 7.14414 | 1.763057 |
el_metsHdr = self._tree.getroot().find('.//mets:metsHdr', NS)
if el_metsHdr is None:
el_metsHdr = ET.Element(TAG_METS_METSHDR)
self._tree.getroot().insert(0, el_metsHdr)
# assert(el_metsHdr is not None)
el_agent = ET.SubElement(el_metsHdr, TAG_METS_AGENT... | def add_agent(self, *args, **kwargs) | Add an `OcrdAgent </../../ocrd_models/ocrd_models.ocrd_agent.html>`_ to the list of agents in the metsHdr. | 3.114373 | 2.258498 | 1.378957 |
return [el.get('USE') for el in self._tree.getroot().findall('.//mets:fileGrp', NS)] | def file_groups(self) | List the ``USE`` attributes of all ``mets:fileGrp``. | 12.568583 | 3.925278 | 3.201959 |
ret = []
fileGrp_clause = '' if fileGrp is None else '[@USE="%s"]' % fileGrp
file_clause = ''
if ID is not None:
file_clause += '[@ID="%s"]' % ID
if mimetype is not None:
file_clause += '[@MIMETYPE="%s"]' % mimetype
if url is not None:
... | def find_files(self, ID=None, fileGrp=None, pageId=None, mimetype=None, url=None, local_only=False) | Search ``mets:file`` in this METS document.
Args:
ID (string) : ID of the file
fileGrp (string) : USE of the fileGrp to list files of
pageId (string) : ID of physical page manifested by matching files
url (string) : @xlink:href of mets:Flocat of mets:file
... | 2.997252 | 2.925375 | 1.02457 |
el_fileSec = self._tree.getroot().find('mets:fileSec', NS)
if el_fileSec is None:
el_fileSec = ET.SubElement(self._tree.getroot(), TAG_METS_FILESEC)
el_fileGrp = el_fileSec.find('mets:fileGrp[@USE="%s"]' % fileGrp, NS)
if el_fileGrp is None:
el_fileGrp = ... | def add_file_group(self, fileGrp) | Add a new ``mets:fileGrp``.
Arguments:
fileGrp (string): ``USE`` attribute of the new filegroup. | 1.97692 | 1.975967 | 1.000482 |
if not ID:
raise Exception("Must set ID of the mets:file")
el_fileGrp = self._tree.getroot().find(".//mets:fileGrp[@USE='%s']" % (fileGrp), NS)
if el_fileGrp is None:
el_fileGrp = self.add_file_group(fileGrp)
if ID is not None and self.find_files(ID=ID) !... | def add_file(self, fileGrp, mimetype=None, url=None, ID=None, pageId=None, force=False, local_filename=None, **kwargs) | Add a `OcrdFile </../../ocrd_models/ocrd_models.ocrd_file.html>`_.
Arguments:
fileGrp (string): Add file to ``mets:fileGrp`` with this ``USE`` attribute
mimetype (string):
url (string):
ID (string):
pageId (string):
force (boolean): Whethe... | 2.515907 | 2.27981 | 1.10356 |
# print(pageId, ocrd_file)
# delete any page mapping for this file.ID
for el_fptr in self._tree.getroot().findall(
'mets:structMap[@TYPE="PHYSICAL"]/mets:div[@TYPE="physSequence"]/mets:div[@TYPE="page"]/mets:fptr[@FILEID="%s"]' %
ocrd_file.ID, namespaces... | def set_physical_page_for_file(self, pageId, ocrd_file, order=None, orderlabel=None) | Create a new physical page | 1.980444 | 1.996794 | 0.991812 |
ret = self._tree.getroot().xpath(
'/mets:mets/mets:structMap[@TYPE="PHYSICAL"]/mets:div[@TYPE="physSequence"]/mets:div[@TYPE="page"][./mets:fptr[@FILEID="%s"]]/@ID' %
ocrd_file.ID, namespaces=NS)
if ret:
return ret[0] | def get_physical_page_for_file(self, ocrd_file) | Get the pageId for a ocrd_file | 3.70295 | 3.567458 | 1.03798 |
try:
self._resolve_workspace()
if 'mets_unique_identifier' not in self.skip:
self._validate_mets_unique_identifier()
if 'mets_file_group_names' not in self.skip:
self._validate_mets_file_group_names()
if 'mets_files' not in... | def _validate(self) | Actual validation. | 3.056581 | 2.969996 | 1.029153 |
if self.workspace is None:
self.workspace = self.resolver.workspace_from_url(self.mets_url, baseurl=self.src_dir, download=self.download)
self.mets = self.workspace.mets | def _resolve_workspace(self) | Clone workspace from mets_url unless workspace was provided. | 5.375788 | 3.279457 | 1.639231 |
for f in [f for f in self.mets.find_files() if f.mimetype.startswith('image/')]:
if not f.local_filename and not self.download:
self.report.add_notice("Won't download remote image <%s>" % f.url)
continue
exif = self.workspace.resolve_image_exif(f.... | def _validate_pixel_density(self) | Validate image pixel density
See `spec <https://ocr-d.github.io/mets#pixel-density-of-images-must-be-explicit-and-high-enough>`_. | 5.319778 | 5.014132 | 1.060957 |
for fileGrp in self.mets.file_groups:
if not fileGrp.startswith(FILE_GROUP_PREFIX):
self.report.add_notice("fileGrp USE does not begin with '%s': %s" % (FILE_GROUP_PREFIX, fileGrp))
else:
# OCR-D-FOO-BAR -> ('FOO', 'BAR')
# \____/\... | def _validate_mets_file_group_names(self) | Ensure ``USE`` attributes of ``mets:fileGrp`` conform to OCR-D naming schema..
See `spec <https://ocr-d.github.io/mets#file-group-use-syntax>`_. | 3.701177 | 3.491451 | 1.060069 |
if not self.mets.find_files():
self.report.add_error("No files")
for f in self.mets.find_files():
if f._el.get('GROUPID'): # pylint: disable=protected-access
self.report.add_notice("File '%s' has GROUPID attribute - document might need an update" % f.ID)
... | def _validate_mets_files(self) | Validate ``mets:file`` URLs are sane. | 3.88241 | 3.617185 | 1.073324 |
for ocrd_file in self.mets.find_files(mimetype=MIMETYPE_PAGE, local_only=True):
self.workspace.download_file(ocrd_file)
page_report = PageValidator.validate(ocrd_file=ocrd_file, strictness=self.page_strictness)
self.report.merge_report(page_report) | def _validate_page(self) | Run PageValidator on the PAGE-XML documents referenced in the METS. | 5.12472 | 4.19383 | 1.221967 |
instance = kwargs.get('instance', None)
if instance is not None:
del kwargs['instance']
request = kwargs.get('request', None)
if request is not None:
del kwargs['request']
# Let's grab the current IP of the user.
x_forwarded_for =... | def create_log_action(self, **kwargs) | Helper method to create a new log entry.
This method automatically populates some fields when no explicit value is given.
:param instance: The model instance to log a change for.
:type instance: Model
:param kwargs: Field overrides for the :py:class:`LogAction` object.
:return: T... | 2.328498 | 2.310312 | 1.007872 |
if not isinstance(queryset, QuerySet) or queryset.count() == 0:
return self.none()
content_type = ContentType.objects.get_for_model(queryset.model)
primary_keys = queryset.values_list(queryset.model._meta.pk.name, flat=True)
if isinstance(primary_keys[0], integer_t... | def get_for_objects(self, queryset) | Get log entries for the objects in the specified queryset.
:param queryset: The queryset to get the log entries for.
:type queryset: QuerySet
:return: The LogAction objects for the objects in the given queryset.
:rtype: QuerySet | 2.133799 | 2.179904 | 0.97885 |
if issubclass(model, Model):
self._registry[model] = {
'include_fields': include_fields,
'exclude_fields': exclude_fields,
}
self._connect_signals(model)
else:
raise TypeError("Supplied model is not a valid model.") | def register(self, model, include_fields=[], exclude_fields=[]) | Register a model with actionslog. Actionslog will then track mutations on this model's instances.
:param model: The model to register.
:type model: Model
:param include_fields: The fields to include. Implicitly excludes all other fields.
:type include_fields: list
:param exclude... | 3.206117 | 3.641111 | 0.880533 |
from actionslog.models import LogAction
# Do not track many to many relations
if field.many_to_many:
return False
# Do not track relations to LogAction
if getattr(field, 'rel', None) is not None and field.rel.to == LogAction:
return False
return True | def track_field(field) | Returns whether the given field should be tracked by Actionslog.
Untracked fields are many-to-many relations and relations to the Actionslog LogAction model.
:param field: The field to check.
:type field: Field
:return: Whether the given field should be tracked.
:rtype: bool | 5.27824 | 3.016167 | 1.749982 |
from actionslog.registry import actionslog
if not(old is None or isinstance(old, Model)):
raise TypeError("The supplied old instance is not a valid model instance.")
if not(new is None or isinstance(new, Model)):
raise TypeError("The supplied new instance is not a valid model instance.... | def model_instance_diff(old, new) | Calculates the differences between two model instances. One of the instances may be ``None`` (i.e., a newly
created model or deleted model). This will cause all fields with a value to have changed (from ``None``).
:param old: The old state of the model instance.
:type old: Model
:param new: The new sta... | 1.974023 | 1.971108 | 1.001479 |
# Initialize thread local storage
threadlocal.actionslog = {
'signal_duid': (self.__class__, time.time()),
'remote_ip': request.META.get('REMOTE_ADDR'),
}
# In case of proxy, set 'original' address
if request.META.get('HTTP_X_FORWARDED_FOR'):
... | def process_request(self, request) | Gets the current user from the request and prepares and connects a signal receiver with the user already
attached to it. | 4.095666 | 3.811563 | 1.074537 |
if hasattr(threadlocal, 'actionslog'):
pre_save.disconnect(sender=LogAction, dispatch_uid=threadlocal.actionslog['signal_duid'])
return response | def process_response(self, request, response) | Disconnects the signal receiver to prevent it from staying active. | 15.935753 | 13.521506 | 1.178549 |
if hasattr(threadlocal, 'actionslog'):
pre_save.disconnect(sender=LogAction, dispatch_uid=threadlocal.actionslog['signal_duid'])
return None | def process_exception(self, request, exception) | Disconnects the signal receiver to prevent it from staying active in case of an exception. | 16.622293 | 13.573419 | 1.224621 |
try:
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
auth_user_model = apps.get_model(app_label, model_name)
except ValueError:
auth_user_model = apps.get_model('auth', 'user')
if sender == LogAction and isinstance(user, auth_user_model) a... | def set_user(user, sender, instance, **kwargs) | Signal receiver with an extra, required 'user' kwarg. This method becomes a real (valid) signal receiver when
it is curried with the user. | 3.343606 | 3.246859 | 1.029797 |
if created:
changes = model_instance_diff(None, instance)
log_entry = LogAction.objects.create_log_action(
instance=instance,
action=LogAction.CREATE,
changes=json.dumps(changes),
) | def action_log_create(sender, instance, created, **kwargs) | Signal receiver that creates a log entry when a model instance is first saved to the database.
Direct use is discouraged, connect your model through :py:func:`actionslog.registry.register` instead. | 4.290623 | 4.695362 | 0.9138 |
if instance.pk is not None:
try:
old = sender.objects.get(pk=instance.pk)
except sender.DoesNotExist:
pass
else:
new = instance
changes = model_instance_diff(old, new)
# Log an entry only if there are changes
if c... | def action_log_update(sender, instance, **kwargs) | Signal receiver that creates a log entry when a model instance is changed and saved to the database.
Direct use is discouraged, connect your model through :py:func:`actionslog.registry.register` instead. | 3.080881 | 3.170008 | 0.971884 |
if instance.pk is not None:
changes = model_instance_diff(instance, None)
log_entry = LogAction.objects.create_log_action(
instance=instance,
action=LogAction.DELETE,
changes=json.dumps(changes),
) | def action_log_delete(sender, instance, **kwargs) | Signal receiver that creates a log entry when a model instance is deleted from the database.
Direct use is discouraged, connect your model through :py:func:`actionslog.registry.register` instead. | 4.130332 | 4.176657 | 0.988909 |
LOG.debug("axapi_http: full url = %s", self.url_base + api_url)
LOG.debug("axapi_http: %s url = %s", method, api_url)
LOG.debug("axapi_http: params = %s", json.dumps(logutils.clean(params), indent=4))
# Set "data" variable for the request
if params:
extra_p... | def request(self, method, api_url, params={}, **kwargs) | Generate the API call to the device. | 3.512909 | 3.522795 | 0.997194 |
payload = self._build_payload(host_list=host_list, serial=serial,
instance_name=instance_name,
use_mgmt_port=use_mgmt_port,
interval=interval, bandwidth_base=bandwidth_base,
... | def create(self, host_list=[], serial=None, instance_name=None, use_mgmt_port=False,
interval=None, bandwidth_base=None, bandwidth_unrestricted=None) | Creates a license manager entry
Keyword arguments:
instance_name -- license manager instance name
host_list -- list(dict) a list of dictionaries of the format:
{'ip': '127.0.0.1', 'port': 443}
serial - (str) appliance serial number
use_mgmt_port - (bool) use manageme... | 1.882752 | 2.269678 | 0.829524 |
return self.create(host_list=host_list, serial=serial, instance_name=instance_name,
use_mgmt_port=use_mgmt_port,
interval=interval, bandwidth_base=bandwidth_base,
bandwidth_unrestricted=bandwidth_unrestricted) | def update(self, host_list=[], serial=None, instance_name=None, use_mgmt_port=False,
interval=None, bandwidth_base=None, bandwidth_unrestricted=None) | Update a license manager entry
Keyword arguments:
instance_name -- license manager instance name
host_list -- list(dict) a list of dictionaries of the format:
{'ip': '127.0.0.1', 'port': 443}
serial - (str) appliance serial number
use_mgmt_port - (bool) use managemen... | 1.642781 | 1.967067 | 0.835142 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.