Search is not available for this dataset
text
stringlengths
75
104k
def main(): '''manual test loop for make_clean_visible_from_raw ''' import argparse import sys parser = argparse.ArgumentParser() parser.add_argument('path') args = parser.parse_args() html = open(args.path).read() html = html.decode('utf8') cursor = 0 for s in non_tag_chars_from_raw(html): for c in s: if c != ' ' and c != html[cursor]: import pdb; pdb.set_trace() sys.stdout.write(c.encode('utf8')) sys.stdout.flush() cursor += 1
def tryload_stage(self, moduleName, functionName, name=None): '''Try to load a stage into self, ignoring errors. If loading a module fails because of some subordinate load failure, just give a warning and move on. On success the stage is added to the stage dictionary. :param str moduleName: name of the Python module :param str functionName: name of the stage constructor :param str name: name of the stage, defaults to `functionName` ''' if name is None: name = functionName try: mod = __import__(moduleName, globals(), locals(), [functionName]) except ImportError, exc: logger.warn('cannot load stage {0}: cannot load module {1}' .format(name, moduleName), exc_info=exc) return if not hasattr(mod, functionName): logger.warn('cannot load stage {0}: module {1} missing {2}' .format(name, moduleName, functionName)) return self[name] = getattr(mod, functionName)
def load_external_stages(self, path): '''Add external stages from the Python module in `path`. `path` must be a path to a Python module source that contains a `Stages` dictionary, which is a map from stage name to callable. :param str path: path to the module file ''' mod = imp.load_source('', path) self.update(mod.Stages)
def load_module_stages(self, mod): '''Add external stages from the Python module `mod`. If `mod` is a string, then it will be interpreted as the name of a module; otherwise it is an actual module object. The module should exist somewhere in :data:`sys.path`. The module must contain a `Stages` dictionary, which is a map from stage name to callable. :param mod: name of the module or the module itself :raise exceptions.ImportError: if `mod` cannot be loaded or does not contain ``Stages`` ''' if isinstance(mod, basestring): mod = __import__(mod, globals=globals(), locals=locals(), fromlist=['Stages'], level=0) if not hasattr(mod, 'Stages'): raise ImportError(mod) self.update(mod.Stages)
def init_stage(self, name, config): '''Construct and configure a stage from known stages. `name` must be the name of one of the stages in this. `config` is the configuration dictionary of the containing object, and its `name` member will be passed into the stage constructor. :param str name: name of the stage :param dict config: parent object configuration :return: callable stage :raise exceptions.KeyError: if `name` is not a known stage ''' subconfig = config.get(name, {}) ctor = self[name] return ctor(subconfig)
def read_to( idx_bytes, stop_bytes=None, run_bytes=None ): ''' iterates through idx_bytes until a byte in stop_bytes or a byte not in run_bytes. :rtype (int, string): idx of last byte and all of bytes including the terminal byte from stop_bytes or not in run_bytes ''' idx = None vals = [] next_b = None while 1: try: idx, next_b = idx_bytes.next() except StopIteration: ## maybe something going wrong? idx = None next_b = None break ## stop when we see any byte in stop_bytes if stop_bytes and next_b in stop_bytes: break ## stop when we see any byte not in run_bytes if run_bytes and next_b not in run_bytes: break ## assemble the ret_val vals.append( next_b ) ## return whatever we have assembled return idx, b''.join(vals), next_b
def iter_attrs( idx_bytes ): ''' called when idx_chars is just past "<a " inside an HTML anchor tag generates tuple(end_idx, attr_name, attr_value) ''' ## read to the end of the "A" tag while 1: idx, attr_name, next_b = read_to(idx_bytes, ['=', '>']) attr_vals = [] ## stop if we hit the end of the tag, or end of idx_bytes if next_b is None or next_b == '>': return idx, space, quote = read_to(idx_bytes, run_bytes = [' ', '\t', '\n', '\r']) if quote not in ['"', "'"]: ## caught start of the property value attr_vals = [quote] quote = ' ' idx, attr_val, next_b = read_to(idx_bytes, [quote, '>']) ## next_b had better either balance the start quote, end the ## tag, or end idx_bytes assert next_b in [quote, '>', None], attr_val attr_vals.append( attr_val ) yield idx, attr_name.strip(), b''.join(attr_vals).strip()
def href_filter(self, href): ''' Test whether an href string meets criteria specified by configuration parameters 'require_abs_url', which means "does it look like it is probably an absolute URL?" and 'domain_substrings'. It searches for each of the domain_substrings in the href individually, and if any match, then returns True. :param: href string :returns bool: ''' if self.config['require_abs_url']: if not href.lower().startswith(('http://', 'https://')): return False if self.config['all_domains']: ## blanket accept all domains as labels return True if self.config['domain_substrings']: parts = href.split('/') if len(parts) < 3: return False domain = parts[2].lower() for substring in self.config['domain_substrings']: try: if substring in domain: return True except Exception, exc: logger.warn('%r in %r raised', substring, domain, exc_info=True) return False
def line_href_anchors(self): ''' simple, regex-based extractor of anchor tags, so we can compute LINE offsets for anchor texts and associate them with their href. Generates tuple(href_string, first_byte, byte_length, anchor_text) Also, this mangles the body.clean_html so that LINE offsets uniquely identify tokens in anchor tags -- quite a kludge. ''' idx = 0 new_clean_html = '' newlines_added = 0 ## split doc up into pieces that end on an anchor tag parts = self.clean_html.split('</a>') assert len('</a>'.join(parts) ) == len(self.clean_html) for i in range(len(parts)): part = parts[i] ## try to get an A tag out: m = anchors_re.match(part) ## if not, then just keep going if not m: new_clean_html += part if i < len(parts) - 1: new_clean_html += '</a>' continue before = m.group('before') ahref = m.group('ahref') ## construct a text containing bytes up to the anchor text pre_anchor_increment = before + ahref ## increment the index to get line number for the anchor idx += len( pre_anchor_increment.splitlines() ) first = idx ## usually this will be one, but it could be more than ## that when an anchor text contains newlines length = len(m.group('anchor').split('\n')) ## construct new clean_html with these newlines inserted new_clean_html += pre_anchor_increment + '\n' + m.group('anchor') + '\n</a>' newlines_added += 2 ## update the index for the next loop idx += length - 1 yield m.group('href'), first, length, m.group('anchor') ## replace clean_html with our new one that has newlines inserted assert len(self.clean_html) == len(new_clean_html) - newlines_added self.clean_html = new_clean_html
def byte_href_anchors(self, chars=False): ''' simple, regex-based extractor of anchor tags, so we can compute BYTE offsets for anchor texts and associate them with their href. Generates tuple(href_string, first_byte, byte_length, anchor_text) ''' input_buffer = self.clean_html if chars: input_buffer = input_buffer.decode('utf8') idx = 0 ## split doc up into pieces that end on an anchor tag parts = input_buffer.split('</a>') assert len('</a>'.join(parts) ) == len(input_buffer) for part in parts: ## try to get an A tag out: m = anchors_re.match(part) if not m: idx += len(part) + 4 continue before = m.group('before') ahref = m.group('ahref') ## increment the index to get line number for the anchor idx += len(before) + len(ahref) first = idx length = len(m.group('anchor')) ## update the index for the next loop # include anchor plus the </a> idx += length + 4 if chars: yield m.group('href').encode('utf8'), first, length, m.group('anchor').encode('utf8') else: yield m.group('href'), first, length, m.group('anchor') assert idx - 4 == len(input_buffer)
def byte_href_anchors_state_machine(self): ''' byte-based state machine extractor of anchor tags, so we can compute byte offsets for anchor texts and associate them with their href. Generates tuple(href_string, first_byte, byte_length, anchor_text) ''' tag_depth = 0 a_tag_depth = 0 vals = [] href = None idx_bytes = enumerate( self.clean_html ) while 1: end_idx, val, next_b = read_to( idx_bytes, '<' ) tag_depth += 1 if href: ## must be inside an anchor tag, so accumulate the ## whole anchor assert a_tag_depth > 0, (href, self.clean_html) vals.append(val) ## figure out if start of an "A" anchor tag or close ## of a previous tag idx, next_b1 = idx_bytes.next() if next_b1.lower() == 'a': ## could be start of "A" tag idx, next_b2 = idx_bytes.next() if next_b2 == ' ': a_tag_depth += 1 href = None for idx, attr_name, attr_val in iter_attrs( idx_bytes ): if attr_name.lower() == 'href': href = attr_val if idx is None: ## doc ended mid tag, so invalid HTML--> just bail return first = idx + 1 ## if we got an href, then we want to keep the ## first byte idx of the anchor: if href: ## Someone could nest an A tag inside another ## A tag, which is invalid (even in HTML5), so ## vals could be nonempty. We only generate ## the leaf-level A tags in these rare cases ## of nested A tags, so reset it: vals = [] elif next_b1 == '/': idx, next_b1 = idx_bytes.next() if next_b1 == 'a': ## could be end of "A" tag idx, next_b2 = idx_bytes.next() if next_b2 == '>': a_tag_depth -= 1 if href: ## join is much faster than using += above anchor = b''.join(vals) length = len(anchor) ## yield the data yield href, first, len(anchor), anchor ## reset, no yield again in a nested A tag href = None else: ## the next_b was not part of </a> or a nested <a tag, ## so keep it in the output vals.append(next_b)
def make_labels(self, clean_html, clean_visible=None): ''' Make a list of Labels for 'author' and the filtered hrefs & anchors ''' if self.offset_type == OffsetType.BYTES: parser = self.byte_href_anchors elif self.offset_type == OffsetType.CHARS: parser = self.char_href_anchors elif self.offset_type == OffsetType.LINES: parser = self.line_href_anchors labels = [] ## make clean_html accessible as a class property so we can self.clean_html = clean_html for href, first, length, value in parser(): if self.href_filter(href): ''' if clean_visible: _check_html = self.clean_html.splitlines()[first-10:10+first+length] _check_visi = clean_visible.splitlines()[first:first+length] if not make_clean_visible(_check_html) == _check_visi: print len(self.clean_html.splitlines()) print len(clean_visible.splitlines()) print href print '\t html: %r' % _check_html print '\t visi: %r' % _check_visi ''' ## add a label for every href label = Label( annotator = Annotator(annotator_id = 'author'), target = Target(target_id = href), ) ## the offset type is specified by the config label.offsets[self.offset_type] = Offset( first=first, length=length, value=value, ## the string name of the content field, not the ## content itself :-) content_form='clean_html') labels.append(label) return labels
def paths(input_dir): 'yield all file paths under input_dir' for root, dirs, fnames in os.walk(input_dir): for i_fname in fnames: i_path = os.path.join(root, i_fname) yield i_path
def _create_column_family(self, family, bytes_columns=[], key_validation_class=TIME_UUID_TYPE): ''' Creates a column family of the name 'family' and sets any of the names in the bytes_column list to have the BYTES_TYPE. key_validation_class defaults to TIME_UUID_TYPE and could also be ASCII_TYPE for md5 hash keys, like we use for 'inbound' ''' sm = SystemManager(random.choice(self.server_list)) # sys.create_column_family(self.namespace, family, super=False) sm.create_column_family(self.namespace, family, super=False, key_validation_class = key_validation_class, default_validation_class = TIME_UUID_TYPE, column_name_class = ASCII_TYPE) for column in bytes_columns: sm.alter_column(self.namespace, family, column, BYTES_TYPE) sm.close()
def _create_counter_column_family(self, family, counter_columns=[], key_validation_class=UTF8Type): ''' Creates a column family of the name 'family' and sets any of the names in the bytes_column list to have the BYTES_TYPE. key_validation_class defaults to TIME_UUID_TYPE and could also be ASCII_TYPE for md5 hash keys, like we use for 'inbound' ''' sm = SystemManager(random.choice(self.server_list)) # sys.create_column_family(self.namespace, family, super=False) sm.create_column_family(self.namespace, family, super=False, key_validation_class = key_validation_class, default_validation_class="CounterColumnType", column_name_class = ASCII_TYPE) for column in counter_columns: sm.alter_column(self.namespace, family, column, COUNTER_COLUMN_TYPE) sm.close()
def tasks(self, key_prefix=''): ''' generate the data objects for every task ''' for row in self._tasks.get_range(): logger.debug(row) if not row[0].startswith(key_prefix): continue data = json.loads(row[1]['task_data']) data['task_key'] = row[0] yield data
def get_random_available(self, max_iter=10000): ''' get a random key out of the first max_iter rows ''' c = 1 keeper = None ## note the ConsistencyLevel here. If we do not do this, and ## get all slick with things like column_count=0 and filter ## empty False, then we can get keys that were recently ## deleted... EVEN if the default consistency would seem to ## rule that out! ## note the random start key, so that we do not always hit the ## same place in the key range with all workers #random_key = hashlib.md5(str(random.random())).hexdigest() #random_key = '0' * 32 #logger.debug('available.get_range(%r)' % random_key) ## scratch that idea: turns out that using a random start key ## OR using row_count=1 can cause get_range to hang for hours ## why we need ConsistencyLevel.ALL on a single node is not ## clear, but experience indicates it is needed. ## note that putting a finite row_count is problematic in two ## ways: # 1) if there are more workers than max_iter, some will not # get tasks # # 2) if there are more than max_iter records, then all workers # have to wade through all of these just to get a task! What # we really want is a "pick random row" function, and that is # probably best implemented using CQL3 token function via the # cql python module instead of pycassa... for row in self._available.get_range(row_count=max_iter, read_consistency_level=pycassa.ConsistencyLevel.ALL): #for row in self._available.get_range(row_count=100): logger.debug('considering %r' % (row,)) if random.random() < 1 / c: keeper = row[0] if c == max_iter: break c += 1 return keeper
def files(text): ''' Iterate over <FILENAME> XML-like tags and tokenize with nltk ''' for f_match in filename_re.finditer(text): yield f_match.group('stream_id'), f_match.group('tagged_doc')
def sentences(self): ''' Iterate over <s> XML-like tags and tokenize with nltk ''' for sentence_id, node in enumerate(self.ner_dom.childNodes): ## increment the char index with any text before the <s> ## tag. Crucial assumption here is that the LingPipe XML ## tags are inserted into the original byte array without ## modifying the portions that are not inside the ## LingPipe-added tags themselves. if node.nodeType == node.TEXT_NODE: ## we expect to only see TEXT_NODE instances with whitespace assert only_whitespace.match(node.data), repr(node.data) ## must convert back to utf-8 to have expected byte offsets self.byte_idx += len(node.data.encode('utf-8')) ## count full lines, i.e. only those that end with a \n # 'True' here means keep the trailing newlines for line in node.data.splitlines(True): if line.endswith('\n'): self.line_idx += 1 else: logger.debug('getting tokens for sentence_id=%d' % sentence_id) more_sentence_remains = True while more_sentence_remains: ## always a sentence sent = Sentence() ## this "node" came from for loop above, and it's ## childNodes list might have been popped by a ## previous pass through this while loop tokens = iter( self.tokens( node ) ) while 1: try: tok = tokens.next() sent.tokens.append(tok) #logger.debug('got token: %r %d %d' % (tok.token, tok.mention_id, tok.sentence_pos)) except StopIteration: yield sent more_sentence_remains = False break
def _make_token(self, start, end): ''' Instantiates a Token from self._input_string[start:end] ''' ## all thfift strings must be encoded first tok_string = self._input_string[start:end].encode('utf-8') if only_whitespace.match(tok_string): ## drop any tokens with only whitespace return None tok = Token() tok.token = tok_string tok.token_num = self.tok_num if 'BYTES' in self.config['offset_types']: tok.offsets[OffsetType.BYTES] = Offset( type = OffsetType.BYTES, first=self.byte_idx + len(self._input_string[:start].encode('utf-8')), length=len(tok_string), value=self.config['offset_debugging'] and tok_string or None, ) if 'LINES' in self.config['offset_types']: tok.offsets[OffsetType.LINES] = Offset( type = OffsetType.LINES, first=self.line_idx, length=1, value=self.config['offset_debugging'] and tok_string or None, ) self.tok_num += 1 ## keep track of position within a sentence tok.sentence_pos = self.sent_pos self.sent_pos += 1 return tok
def tokens(self, sentence_dom): ''' Tokenize all the words and preserve NER labels from ENAMEX tags ''' ## keep track of sentence position, which is reset for each ## sentence, and used above in _make_token self.sent_pos = 0 ## keep track of mention_id, so we can distinguish adjacent ## multi-token mentions within the same coref chain mention_id = 0 while len(sentence_dom.childNodes) > 0: ## shrink the sentence_dom's child nodes. In v0_2_0 this ## was required to cope with HitMaxi16. Now it is just to ## save memory. node = sentence_dom.childNodes.pop(0) if node.nodeType == node.TEXT_NODE: ## process portion before an ENAMEX tag for line in node.data.splitlines(True): self._input_string = line for start, end in self.word_tokenizer.span_tokenize(line): tok = self._make_token(start, end) if tok: yield tok if line.endswith('\n'): ## maintain the index to the current line self.line_idx += 1 ## increment index pasat the 'before' portion self.byte_idx += len(line.encode('utf-8')) else: ## process text inside an ENAMEX tag assert node.nodeName == 'ENAMEX', node.nodeName chain_id = node.attributes.get('ID').value entity_type = node.attributes.get('TYPE').value for node in node.childNodes: assert node.nodeType == node.TEXT_NODE, node.nodeType for line in node.data.splitlines(True): self._input_string = line for start, end in self.word_tokenizer.span_tokenize(line): tok = self._make_token(start, end) if tok: if entity_type in _PRONOUNS: tok.mention_type = MentionType.PRO tok.entity_type = _ENTITY_TYPES[entity_type] ## create an attribute attr = Attribute( attribute_type=AttributeType.PER_GENDER, value=str(_PRONOUNS[entity_type]) ) self.attributes.append(attr) else: ## regular entity_type tok.mention_type = MentionType.NAME tok.entity_type = _ENTITY_TYPES[entity_type] tok.equiv_id = int(chain_id) tok.mention_id = mention_id yield tok if line.endswith('\n'): ## maintain the index to the current line self.line_idx += 1 ## increment index pasat the 'before' portion self.byte_idx += len(line.encode('utf-8')) ## increment mention_id within this sentence mention_id += 1
def get_sentences(self, ner_dom): '''parse the sentences and tokens out of the XML''' lp_parser = LingPipeParser(self.config) lp_parser.set(ner_dom) sentences = list( lp_parser.sentences() ) return sentences, lp_parser.relations, lp_parser.attributes
def recursive_glob_with_tree(new_base, old_base, treeroot, pattern): '''generate a list of tuples(new_base, list(paths to put there) where the files are found inside of old_base/treeroot. ''' results = [] old_cwd = os.getcwd() os.chdir(old_base) for rel_base, dirs, files in os.walk(treeroot): goodfiles = fnmatch.filter(files, pattern) one_dir_results = [] for f in goodfiles: one_dir_results.append(os.path.join(old_base, rel_base, f)) results.append((os.path.join(new_base, rel_base), one_dir_results)) os.chdir(old_cwd) return results
def _retry(func): ''' Decorator for methods that need many retries, because of intermittent failures, such as AWS calls via boto, which has a non-back-off retry. ''' def retry_func(self, *args, **kwargs): tries = 1 while True: # If a handler allows execution to continue, then # fall through and do a back-off retry. try: return func(self, *args, **kwargs) break except OSError as exc: ## OSError: [Errno 24] Too many open files logger.error('assuming OSError unrecoverable') raise except FailedExtraction as exc: ## pass through exc to caller logger.error('FAIL(%d)', tries, exc_info=True) raise except FailedVerification as exc: logger.warn('FAIL(%d)', tries, exc_info=True) if tries >= self.config['tries']: if self.config.get('suppress_failures'): logger.warn('suppressing failure and breaking out of this loop; data may be corrupt, downstream will have to cope') break else: raise except Exception as exc: logger.warn('FAIL(%d): having I/O trouble with S3', tries, exc_info=True) if tries >= self.config['tries']: raise logger.warn('RETRYING (%d left)', self.config['tries'] - tries) time.sleep(3 * tries) tries += 1 return retry_func
def verify_md5(md5_expected, data, other_errors=None): "return True if okay, raise Exception if not" # O_o ? md5_recv = hashlib.md5(data).hexdigest() if md5_expected != md5_recv: if other_errors is not None: logger.critical('\n'.join(other_errors)) raise FailedVerification('original md5 = %r != %r = received md5' \ % (md5_expected, md5_recv)) return True
def get_bucket(config, bucket_name=None): '''This function is mostly about managing configuration, and then finally returns a boto.Bucket object. AWS credentials come first from config keys aws_access_key_id_path, aws_secret_access_key_path (paths to one line files); secondly from environment variables AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY; also from $HOME/.aws/credentials or the magic Amazon http://169.254.169.254/ service. If credentials are not set in the config then behavior is the same as other AWS-based command-line tools. ''' if not bucket_name: if 'bucket' not in config: raise ConfigurationError( 'The "bucket" parameter is required for the s3 stages.') bucket_name = config['bucket'] # get AWS credentials. first, from config; else, from env vars. # (boto will read environment variables and other normal places.) aws_access_key_id_path = config.get('aws_access_key_id_path') aws_secret_access_key_path = config.get('aws_secret_access_key_path') params = () if aws_access_key_id_path and aws_secret_access_key_path: try: access = open(aws_access_key_id_path).read().strip() secret = open(aws_secret_access_key_path).read().strip() params = (access, secret) except: logger.error('failed reading aws credentials from configured file', exc_info=True) raise conn = S3Connection(*params) bucket = conn.get_bucket(bucket_name) return bucket
def _decode(self, data): ''' Given the raw data from s3, return a generator for the items contained in that data. A generator is necessary to support chunk files, but non-chunk files can be provided by a generator that yields exactly one item. Decoding works by case analysis on the config option ``input_format``. If an invalid ``input_format`` is given, then a ``ConfigurationError`` is raised. ''' informat = self.config['input_format'].lower() if informat == 'spinn3r': return _generate_stream_items(data) elif informat == 'streamitem': ver = self.config['streamcorpus_version'] if ver not in _message_versions: raise ConfigurationError( 'Not a valid streamcorpus version: %s ' '(choose from: %s)' % (ver, ', '.join(_message_versions.keys()))) message = _message_versions[ver] return streamcorpus.Chunk(data=data, message=message) elif informat == 'featurecollection' and FCChunk is not None: return FCChunk(data=data) else: raise ConfigurationError( 'from_s3_chunks unknown input_format = %r' % informat)
def get_chunk(self, bucket_name, key_path): '''return Chunk object full of records bucket_name may be None''' bucket = get_bucket(self.config, bucket_name=bucket_name) key = bucket.get_key(key_path) if key is None: raise FailedExtraction('Key "%s" does not exist.' % key_path) fh = StringIO() key.get_contents_to_file(fh) data = fh.getvalue() if not data: raise FailedExtraction('%s: no data (does the key exist?)' % key.key) chunk_type, compression, encryption = parse_file_extensions(key_path) if encryption == 'gpg': if not self.gpg_decryption_key_path: raise FailedExtraction('%s ends with ".gpg" but gpg_decryption_key_path=%s' % (key.key, self.gpg_decryption_key_path)) _errors = [] if compression or encryption: _errors, data = decrypt_and_uncompress( data, self.gpg_decryption_key_path, tmp_dir=self.config.get('tmp_dir_path'), compression=compression, ) if not data: msg = 'decrypt_and_uncompress got no data for {0!r}, from {1} bytes' \ + ' downloaded, errors: {2}' \ .format(key_path, len(data), '\n'.join(_errors)) logger.error(msg) raise FailedExtraction(msg) logger.info( '\n'.join(_errors) ) if not self.config['compare_md5_in_file_name']: logger.warn('not checking md5 in file name, consider setting ' 'from_s3_chunks:compare_md5_in_file_name') else: logger.info('Verifying md5 for "%s"...' % key.key) # The regex hammer. m = re.search('([a-z0-9]{32})(?:\.|$)', key.key) if m is None: raise FailedExtraction( 'Could not extract md5 from key "%s". ' 'Perhaps you should disable compare_md5_in_file_name?' % key.key) i_content_md5 = m.group(1) #import pdb; pdb.set_trace() verify_md5(i_content_md5, data, other_errors=_errors) return self._decode(data)
def stream_id_to_kvlayer_key(stream_id): '''Convert a text stream ID to a kvlayer key. The return tuple can be used directly as a key in the :data:`STREAM_ITEMS_TABLE` table. :param str stream_id: stream ID to convert :return: :mod:`kvlayer` key tuple :raise exceptions.KeyError: if `stream_id` is malformed ''' # Reminder: stream_id is 1234567890-123456789abcdef...0 # where the first part is the (decimal) epoch_ticks and the second # part is the (hex) doc_id parts = stream_id.split('-') if len(parts) != 2: raise KeyError('invalid stream_id ' + stream_id) epoch_ticks_s = parts[0] doc_id_s = parts[1] if not epoch_ticks_s.isdigit(): raise KeyError('invalid stream_id ' + stream_id) if doc_id_s.lstrip(string.hexdigits) != '': raise KeyError('invalid stream_id ' + stream_id) return (base64.b16decode(doc_id_s.upper()), int(epoch_ticks_s))
def kvlayer_key_to_stream_id(k): '''Convert a kvlayer key to a text stream ID. `k` should be of the same form produced by :func:`stream_id_to_kvlayer_key`. :param k: :mod:`kvlayer` key tuple :return: converted stream ID :returntype str: ''' abs_url_hash, epoch_ticks = k return '{0}-{1}'.format(epoch_ticks, base64.b16encode(abs_url_hash).lower())
def key_for_stream_item(si): '''Get a kvlayer key from a stream item. The return tuple can be used directly as a key in the :data:`STREAM_ITEMS_TABLE` table. Note that this recalculates the stream ID, and if the internal data on the stream item is inconsistent then this could return a different result from :func:`stream_id_to_kvlayer_key`. :param si: stream item to get key for :return: :mod:`kvlayer` key tuple ''' # get binary 16 byte digest urlhash = hashlib.md5(si.abs_url).digest() return (urlhash, int(si.stream_time.epoch_ticks))
def main(argv=sys.argv): args = parse(argv) """Serve up some ponies.""" hostname = args.listen port = args.port print( "Making all your dreams for a pony come true on http://{0}:{1}.\n" "Press Ctrl+C to quit.\n".format(hostname, port)) # Hush, werkzeug. logging.getLogger('werkzeug').setLevel(logging.CRITICAL) plugin_manager.load_installed_plugins() app = make_app() run_simple(hostname, port, app)
def build_parser(): """Build the parser that will have all available commands and options.""" description = ( 'HTTPony (pronounced aych-tee-tee-pony) is a simple HTTP ' 'server that pretty prints HTTP requests to a terminal. It ' 'is a useful aide for developing clients that send HTTP ' 'requests. HTTPony acts as a sink for a client so that a ' 'developer can understand what the client is sending.') parser = argparse.ArgumentParser(description=description) parser.add_argument( '-l', '--listen', help='set the IP address or hostname', default='localhost') parser.add_argument( '-p', '--port', help='set the port', default=8000, type=int) return parser
def add_xpaths_to_stream_item(si): '''Mutably tag tokens with xpath offsets. Given some stream item, this will tag all tokens from all taggings in the document that contain character offsets. Note that some tokens may not have computable xpath offsets, so an xpath offset for those tokens will not be set. (See the documentation and comments for ``char_offsets_to_xpaths`` for what it means for a token to have a computable xpath.) If a token can have its xpath offset computed, it is added to its set of offsets with a ``OffsetType.XPATH_CHARS`` key. ''' def sentences_to_xpaths(sentences): tokens = sentences_to_char_tokens(sentences) offsets = char_tokens_to_char_offsets(tokens) return char_offsets_to_xpaths(html, offsets) def xprange_to_offset(xprange): return Offset(type=OffsetType.XPATH_CHARS, first=xprange.start_offset, length=0, xpath=xprange.start_xpath, content_form='clean_html', value=None, xpath_end=xprange.end_xpath, xpath_end_offset=xprange.end_offset) html = unicode(si.body.clean_html, 'utf-8') for sentences in si.body.sentences.itervalues(): tokens = sentences_to_char_tokens(sentences) for token, xprange in izip(tokens, sentences_to_xpaths(sentences)): if xprange is None: continue offset = xprange_to_offset(xprange) token.offsets[OffsetType.XPATH_CHARS] = offset
def sentences_to_char_tokens(si_sentences): '''Convert stream item sentences to character ``Offset``s.''' for sentence in si_sentences: for token in sentence.tokens: if OffsetType.CHARS in token.offsets: yield token
def char_tokens_to_char_offsets(si_tokens): '''Convert character ``Offset``s to character ranges.''' for token in si_tokens: offset = token.offsets[OffsetType.CHARS] yield offset.first, offset.first + offset.length
def char_offsets_to_xpaths(html, char_offsets): '''Converts HTML and a sequence of char offsets to xpath offsets. Returns a generator of :class:`streamcorpus.XpathRange` objects in correspondences with the sequence of ``char_offsets`` given. Namely, each ``XpathRange`` should address precisely the same text as that ``char_offsets`` (sans the HTML). Depending on how ``char_offsets`` was tokenized, it's possible that some tokens cannot have their xpaths generated reliably. In this case, a ``None`` value is yielded instead of a ``XpathRange``. ``char_offsets`` must be a sorted and non-overlapping sequence of character ranges. They do not have to be contiguous. ''' html = uni(html) parser = XpathTextCollector() prev_end = 0 prev_progress = True for start, end in char_offsets: if start == end: # Zero length tokens shall have no quarter! # Note that this is a special case. If we let zero-length tokens # be handled normally, then it will be recorded as if the parser # did not make any progress. But of course, there is no progress # to be had! yield None continue # If we didn't make any progress on the previous token, then we'll # need to try and make progress before we can start tracking offsets # again. Otherwise the parser will report incorrect offset info. # # (The parser can fail to make progress when tokens are split at # weird boundaries, e.g., `&amp` followed by `;`. The parser won't # make progress after `&amp` but will once `;` is given.) # # Here, we feed the parser one character at a time between where the # last token ended and where the next token will start. In most cases, # this will be enough to nudge the parser along. Once done, we can pick # up where we left off and start handing out offsets again. # # If this still doesn't let us make progress, then we'll have to skip # this token too. if not prev_progress: for i in xrange(prev_end, start): parser.feed(html[i]) prev_end += 1 if parser.made_progress: break if not parser.made_progress: yield None continue # Hand the parser everything from the end of the last token to the # start of this one. Then ask for the Xpath, which should be at the # start of `char_offsets`. if prev_end < start: parser.feed(html[prev_end:start]) if not parser.made_progress: parser.feed(html[start:end]) prev_progress = parser.made_progress prev_end = end yield None continue xstart = parser.xpath_offset() # print('START', xstart) # Hand it the actual token and ask for the ending offset. parser.feed(html[start:end]) xend = parser.xpath_offset() # print('END', xend) prev_end = end # If we couldn't make progress then the xpaths generated are probably # incorrect. (If the parser doesn't make progress, then we can't rely # on the callbacks to have been called, which means we may not have # captured all state correctly.) # # Therefore, we simply give up and claim this token is not addressable. if not parser.made_progress: prev_progress = False yield None else: prev_progress = True yield XpathRange(xstart[0], xstart[1], xend[0], xend[1]) parser.feed(html[prev_end:]) parser.close()
def stream_item_roundtrip_xpaths(si, quick=False): '''Roundtrip all Xpath offsets in the given stream item. For every token that has both ``CHARS`` and ``XPATH_CHARS`` offsets, slice the ``clean_html`` with the ``XPATH_CHARS`` offset and check that it matches slicing ``clean_visible`` with the ``CHARS`` offset. If this passes without triggering an assertion, then we're guaranteed that all ``XPATH_CHARS`` offsets in the stream item are correct. (Note that does not check for completeness. On occasion, a token's ``XPATH_CHARS`` offset cannot be computed.) There is copious debugging output to help make potential bugs easier to track down. This is used in tests in addition to the actual transform. It's expensive to run, but not running it means silent and hard to debug bugs. ''' def debug(s): logger.warning(s) def print_window(token, size=200): coffset = token.offsets[OffsetType.CHARS] start = max(0, coffset.first - size) end = min(len(html), coffset.first + coffset.length + size) debug('-' * 49) debug(coffset) debug('window size: %d' % size) debug(html[start:end]) debug('-' * 49) def debug_all(token, xprange, expected, err=None, got=None): debug('-' * 79) if err is not None: debug(err) debug(xprange) debug('expected: "%s"' % expected) if got is not None: debug('got: "%s"' % got) debug('token value: "%s"' % unicode(token.token, 'utf-8')) print_window(token, size=10) print_window(token, size=30) print_window(token, size=100) print_window(token, size=200) debug('-' * 79) def slice_clean_visible(token): coffset = token.offsets[OffsetType.CHARS] return cleanvis[coffset.first:coffset.first + coffset.length] def test_token(token): coffset = token.offsets.get(OffsetType.CHARS) if coffset is None: return False xoffset = token.offsets.get(OffsetType.XPATH_CHARS) if xoffset is None: return False crange = (coffset.first, coffset.first + coffset.length) xprange = XpathRange.from_offset(xoffset) expected = slice_clean_visible(token) if expected != unicode(token.token, 'utf-8'): # Yeah, apparently this can happen. Maybe it's a bug # in Basis? I'm trying to hustle, and this only happens # in two instances for the `random` document, so I'm not # going to try to reproduce a minimal counter-example. # ---AG return False try: got = xprange.slice_node(html_root) except InvalidXpathError as err: debug_all(token, xprange, expected, err=err) raise XpathMismatchError(html, cleanvis, xprange, crange) if expected != got: debug_all(token, xprange, expected, got=got) raise XpathMismatchError(html, cleanvis, xprange, crange) return True cleanvis = unicode(si.body.clean_visible, 'utf-8') html = unicode(si.body.clean_html, 'utf-8') html_root = XpathRange.html_node(html) total, has_valid_xpath = 0, 0 for sentences in si.body.sentences.itervalues(): for sentence in sentences: if quick: for i in xrange(len(sentence.tokens) - 1, -1, -1): if test_token(sentence.tokens[i]): break else: # Exhaustive test. for token in sentence.tokens: total += 1 if test_token(token): has_valid_xpath += 1 if not quick: # This is nonsense if we have quick checking enabled. logger.info('stream item %s: %d/%d tokens with valid xpaths', si.stream_id, has_valid_xpath, total)
def add_element(self, tag): '''Record that `tag` has been seen at this depth. If `tag` is :class:`TextElement`, it records a text node. ''' # Collapse adjacent text nodes if tag is TextElement and self.last_tag is TextElement: return self.last_tag = tag if tag not in self.tags: self.tags[tag] = 1 else: self.tags[tag] += 1
def xpath_piece(self): '''Get an XPath fragment for this location. It is of the form ``tag[n]`` where `tag` is the most recent element added and n is its position. ''' if self.last_tag is TextElement: return 'text()[{count}]'.format(count=self.text_index()) else: return '{tag}[{count}]'.format(tag=self.last_tag, count=self.tags[self.last_tag])
def text_index(self): '''Returns the one-based index of the current text node.''' # This is the number of text nodes we've seen so far. # If we are currently in a text node, great; if not then add # one for the text node that's about to begin. i = self.tags.get(TextElement, 0) if self.last_tag is not TextElement: i += 1 return i
def xpath_offset(self): '''Returns a tuple of ``(xpath, character offset)``. The ``xpath`` returned *uniquely* identifies the end of the text node most recently inserted. The character offsets indicates where the text inside the node ends. (When the text node is empty, the offset returned is `0`.) ''' datai = self.depth_stack[-1].text_index() xpath = (u'/' + u'/'.join(dse.xpath_piece() for dse in self.depth_stack[:-1]) + (u'/text()[%d]' % datai)) return (xpath, self.data_start)
def descendants(elem): ''' Yields all the elements descendant of elem in document order ''' for child in elem.xml_children: if isinstance(child, element): yield child yield from descendants(child)
def select_elements(source): ''' Yields all the elements from the source source - if an element, yields all child elements in order; if any other iterator yields the elements from that iterator ''' if isinstance(source, element): source = source.xml_children return filter(lambda x: isinstance(x, element), source)
def select_name(source, name): ''' Yields all the elements with the given name source - if an element, starts with all child elements in order; can also be any other iterator name - will yield only elements with this name ''' return filter(lambda x: x.xml_name == name, select_elements(source))
def select_name_pattern(source, pat): ''' Yields elements from the source whose name matches the given regular expression pattern source - if an element, starts with all child elements in order; can also be any other iterator pat - re.pattern object ''' return filter(lambda x: pat.match(x.xml_name) is not None, select_elements(source))
def select_value(source, val): ''' Yields elements from the source with the given value (accumulated child text) source - if an element, starts with all child elements in order; can also be any other iterator val - string value to match ''' if isinstance(source, element): source = source.xml_children return filter(lambda x: x.xml_value == val, source)
def select_attribute(source, name, val=None): ''' Yields elements from the source having the given attrivute, optionally with the given attribute value source - if an element, starts with all child elements in order; can also be any other iterator name - attribute name to check val - if None check only for the existence of the attribute, otherwise compare the given value as well ''' def check(x): if val is None: return name in x.xml_attributes else: return name in x.xml_attributes and x.xml_attributes[name] == val return filter(check, select_elements(source))
def following_siblings(elem): ''' Yields elements and text which have the same parent as elem, but come afterward in document order ''' it = itertools.dropwhile(lambda x: x != elem, elem.xml_parent.xml_children) next(it) #Skip the element itself return it
def select_pattern(node, pattern, state=None): ''' Yield descendant nodes matching the given pattern specification pattern - tuple of steps, each of which matches an element by name, with "*" acting like a wildcard, descending the tree in tuple order sort of like a subset of XPath in Python tuple form state - for internal use only pattern examples: ("a", "b", "c") - all c elements whose parent is a b element whose parent is an a element whose parent is node ("*", "*") - any "grandchild" of node ("*", "*", "*") - any "great grandchild" of node ("**", "a") - any a descendant of node >>> from amara3.uxml import tree >>> from amara3.uxml.treeutil import * >>> >>> tb = tree.treebuilder() >>> DOC = '<a xmlns="urn:namespaces:suck"><b><x>1</x></b><c><x>2</x><d><x>3</x></d></c><x>4</x><y>5</y></a>' >>> root = tb.parse(DOC) >>> results = [ e.xml_value for e in select_pattern(root, ('**', 'x')) ] >>> results ['1', '2', '3', '4'] ''' if state is None: state = _prep_pattern(pattern) #for child in select_elements(elem): if isinstance(node, element): for child in node.xml_children: new_state = state(child) if new_state == MATCHED_STATE: yield child elif new_state is not None: yield from select_pattern(child, None, state=new_state) return
def make_pretty(elem, depth=0, indent=' '): ''' Add text nodes as possible to all descendants of an element for spacing & indentation to make the MicroXML as printed easier for people to read. Will not modify the value of any text node which is not already entirely whitespace. Warning: even though this operaton avoids molesting text nodes which already have whitespace, it still makes changes which alter the text. Not all whitespace in XML is ignorable. In XML cues from the DTD indicate which whitespace can be ignored. No such cues are available for MicroXML, so use this function with care. That said, in many real world applications of XML and MicroXML, this function causes no problems. elem - target element whose descendant nodes are to be modified. returns - the same element, which has been updated in place >>> from amara3.uxml import tree >>> from amara3.uxml.treeutil import * >>> DOC = '<a><b><x>1</x></b><c><x>2</x><d><x>3</x></d></c><x>4</x><y>5</y></a>' >>> tb = tree.treebuilder() >>> root = tb.parse(DOC) >>> len(root.xml_children) 4 >>> make_pretty(root) <uxml.element (8763373718343) "a" with 9 children> >>> len(root.xml_children) 9 >>> root.xml_encode() '<a>\n <b>\n <x>1</x>\n </b>\n <c>\n <x>2</x>\n <d>\n <x>3</x>\n </d>\n </c>\n <x>4</x>\n <y>5</y>\n</a>' ''' depth += 1 updated_child_list = [] updated_child_ix = 0 for child in elem.xml_children: if isinstance(child, element): if updated_child_ix % 2: updated_child_list.append(child) updated_child_ix += 1 else: #It's the turn for text, but we have an element new_text = text('\n' + indent*depth, elem) updated_child_list.append(new_text) updated_child_list.append(child) updated_child_ix += 2 make_pretty(child, depth) else: if child.xml_value.strip(): #More to it than whitespace, so leave alone #Note: if only whitespace entities are used, will still be left alone updated_child_list.append(child) updated_child_ix += 1 else: #Only whitespace, so replace with proper indentation new_text = text('\n' + indent*depth, elem) updated_child_list.append(new_text) updated_child_ix += 1 #Trailing indentation might be needed if not(updated_child_ix % 2): new_text = text('\n' + indent*(depth-1), elem) updated_child_list.append(new_text) #updated_child_ix += 1 #About to be done, so not really needed elem.xml_children = updated_child_list return elem
def call_inkscape(args_strings, inkscape_binpath=None): """Call inkscape CLI with arguments and returns its return value. Parameters ---------- args_string: list of str inkscape_binpath: str Returns ------- return_value Inkscape command CLI call return value. """ log.debug('Looking for the binary file for inkscape.') if inkscape_binpath is None: inkscape_binpath = get_inkscape_binpath() if inkscape_binpath is None or not os.path.exists(inkscape_binpath): raise IOError( 'Inkscape binary has not been found. Please check configuration.' ) return call_command(inkscape_binpath, args_strings)
def inkscape_export(input_file, output_file, export_flag="-A", dpi=90, inkscape_binpath=None): """ Call Inkscape to export the input_file to output_file using the specific export argument flag for the output file type. Parameters ---------- input_file: str Path to the input file output_file: str Path to the output file export_flag: str Inkscape CLI flag to indicate the type of the output file Returns ------- return_value Command call return value """ if not os.path.exists(input_file): log.error('File {} not found.'.format(input_file)) raise IOError((0, 'File not found.', input_file)) if '=' not in export_flag: export_flag += ' ' arg_strings = [] arg_strings += ['--without-gui'] arg_strings += ['--export-text-to-path'] arg_strings += ['{}"{}"'.format(export_flag, output_file)] arg_strings += ['--export-dpi={}'.format(dpi)] arg_strings += ['"{}"'.format(input_file)] return call_inkscape(arg_strings, inkscape_binpath=inkscape_binpath)
def svg2pdf(svg_file_path, pdf_file_path, dpi=150, command_binpath=None, support_unicode=False): """ Transform SVG file to PDF file """ if support_unicode: return rsvg_export(svg_file_path, pdf_file_path, dpi=dpi, rsvg_binpath=command_binpath) return inkscape_export(svg_file_path, pdf_file_path, export_flag="-A", dpi=dpi, inkscape_binpath=command_binpath)
def svg2png(svg_file_path, png_file_path, dpi=150, inkscape_binpath=None): """ Transform SVG file to PNG file """ return inkscape_export(svg_file_path, png_file_path, export_flag="-e", dpi=dpi, inkscape_binpath=inkscape_binpath)
def get_environment_for(file_path): """Return a Jinja2 environment for where file_path is. Parameters ---------- file_path: str Returns ------- jinja_env: Jinja2.Environment """ work_dir = os.path.dirname(os.path.abspath(file_path)) if not os.path.exists(work_dir): raise IOError('Could not find folder for dirname of file {}.'.format(file_path)) try: jinja_env = Environment(loader=FileSystemLoader(work_dir)) except: raise else: return jinja_env
def _setup_template_file(self, template_file_path): """ Setup self.template Parameters ---------- template_file_path: str Document template file path. """ try: template_file = template_file_path template_env = get_environment_for(template_file_path) template = template_env.get_template(os.path.basename(template_file)) except: raise else: self._template_file = template_file self._template_env = template_env self.template = template
def fill(self, doc_contents): """ Fill the content of the document with the information in doc_contents. Parameters ---------- doc_contents: dict Set of values to set the template document. Returns ------- filled_doc: str The content of the document with the template information filled. """ try: filled_doc = self.template.render(**doc_contents) except: log.exception('Error rendering Document ' 'for {}.'.format(doc_contents)) raise else: self.file_content_ = filled_doc return filled_doc
def save_content(self, file_path, encoding='utf-8'): """ Save the content of the .txt file in a text file. Parameters ---------- file_path: str Path to the output file. """ if self.file_content_ is None: msg = 'Template content has not been updated. \ Please fill the template before rendering it.' log.exception(msg) raise ValueError(msg) try: write_to_file(file_path, content=self.file_content_, encoding=encoding) except Exception as exc: msg = 'Document of type {} got an error when \ writing content.'.format(self.__class__) log.exception(msg) raise Exception(msg) from exc
def from_template_file(cls, template_file_path, command=None): """ Factory function to create a specific document of the class given by the `command` or the extension of `template_file_path`. See get_doctype_by_command and get_doctype_by_extension. Parameters ---------- template_file_path: str command: str Returns ------- doc """ # get template file extension ext = os.path.basename(template_file_path).split('.')[-1] try: doc_type = get_doctype_by_command(command) except ValueError: doc_type = get_doctype_by_extension(ext) except: raise else: return doc_type(template_file_path)
def fill(self, doc_contents): """ Fill the content of the document with the information in doc_contents. This is different from the TextDocument fill function, because this will check for symbools in the values of `doc_content` and replace them to good XML codes before filling the template. Parameters ---------- doc_contents: dict Set of values to set the template document. Returns ------- filled_doc: str The content of the document with the template information filled. """ for key, content in doc_contents.items(): doc_contents[key] = replace_chars_for_svg_code(content) return super(SVGDocument, self).fill(doc_contents=doc_contents)
def render(self, file_path, **kwargs): """ Save the content of the .svg file in the chosen rendered format. Parameters ---------- file_path: str Path to the output file. Kwargs ------ file_type: str Choices: 'png', 'pdf', 'svg' Default: 'pdf' dpi: int Dots-per-inch for the png and pdf. Default: 150 support_unicode: bool Whether to allow unicode to be encoded in the PDF. Default: False """ temp = get_tempfile(suffix='.svg') self.save_content(temp.name) file_type = kwargs.get('file_type', 'pdf') dpi = kwargs.get('dpi', 150) support_unicode = kwargs.get('support_unicode', False) try: if file_type == 'svg': shutil.copyfile(temp.name, file_path) elif file_type == 'png': svg2png(temp.name, file_path, dpi=dpi) elif file_type == 'pdf': svg2pdf(temp.name, file_path, dpi=dpi, support_unicode=support_unicode) except: log.exception( 'Error exporting file {} to {}'.format(file_path, file_type) ) raise
def render(self, file_path, **kwargs): """ Save the content of the .text file in the PDF. Parameters ---------- file_path: str Path to the output file. """ temp = get_tempfile(suffix='.tex') self.save_content(temp.name) try: self._render_function(temp.name, file_path, output_format='pdf') except: log.exception('Error exporting file {} to PDF.'.format(file_path)) raise
def parse(source, handler): ''' Convert XML 1.0 to MicroXML source - XML 1.0 input handler - MicroXML events handler Returns uxml, extras uxml - MicroXML element extracted from the source extras - information to be preserved but not part of MicroXML, e.g. namespaces ''' h = expat_callbacks(handler) p = xml.parsers.expat.ParserCreate(namespace_separator=' ') p.StartElementHandler = h.start_element p.EndElementHandler = h.end_element p.CharacterDataHandler = h.char_data p.StartNamespaceDeclHandler = h.start_namespace p.EndNamespaceDeclHandler = h.end_namespace p.Parse(source) return p
def search(self): '''Start the optimisation/search using the supplied optimisation method with the supplied inputs for the supplied function''' search = self._method(inputs=self._inputs, function=self._function, state=self._state) search.run()
def parse(source, prefixes=None, model=None, encoding=None, use_xhtml_ns=False): ''' Parse an input source with HTML text into an Amara 3 tree >>> from amara3.uxml import html5 >>> import urllib.request >>> with urllib.request.urlopen('http://uche.ogbuji.net/') as response: ... html5.parse(response) #Warning: if you pass a string, you must make sure it's a byte string, not a Unicode object. You might also want to wrap it with amara.lib.inputsource.text if it's not obviously XML or HTML (for example it could be confused with a file name) ''' def get_tree_instance(namespaceHTMLElements, use_xhtml_ns=use_xhtml_ns): #use_xhtml_ns is a boolean, whether or not to use http://www.w3.org/1999/xhtml return treebuilder(use_xhtml_ns) parser = html5lib.HTMLParser(tree=get_tree_instance) #doc = parser.parse(inputsource(source, None).stream, encoding=encoding) #doc = parser.parse(source, encoding=encoding) doc = parser.parse(source) first_element = next((e for e in doc.root_nodes if isinstance(e, element)), None) return first_element
def markup_fragment(source, encoding=None): ''' Parse a fragment if markup in HTML mode, and return a bindery node Warning: if you pass a string, you must make sure it's a byte string, not a Unicode object. You might also want to wrap it with amara.lib.inputsource.text if it's not obviously XML or HTML (for example it could be confused with a file name) from amara.lib import inputsource from amara.bindery import html doc = html.markup_fragment(inputsource.text('XXX<html><body onload="" color="white"><p>Spam!<p>Eggs!</body></html>YYY')) See also: http://wiki.xml3k.org/Amara2/Tagsoup ''' doc = parse(source, encoding=encoding) frag = doc.html.body return frag
def insertText(self, data, insertBefore=None): """Insert data as text in the current node, positioned before the start of node insertBefore or to the end of the node's text. """ if insertBefore: self.insertBefore(tree.text(data), insertBefore) else: self.xml_append(tree.text(data))
def insertBefore(self, node, refNode): """Insert node as a child of the current node, before refNode in the list of child nodes. Raises ValueError if refNode is not a child of the current node""" offset = self.xml_children.index(refNode) self.xml_insert(node, offset)
def cloneNode(self): """Return a shallow copy of the current node i.e. a node with the same name and attributes but with no parent or child nodes """ attrs = self.xml_attributes.copy() return element(self.xml_name, attrs=attrs)
def execute(option): '''A script that melody calls with each valid set of options. This script runs the required code and returns the results.''' namelist_option = [] makefile_option = [] flags = "" for entry in option: key = entry.keys()[0] if key == "Problem Size": namelist_option.append({"SIZE": entry[key]}) elif key == "F90": makefile_option.append(entry) else: flags += entry[key] + " " makefile_option.append({"F90FLAGS": flags}) namelist = create_input(namelist_option, "namelist", template_location="templates") makefile_include = create_input(makefile_option, "Makefile.include", template_location="templates") benchmark_base = "shallow" # save the input files in the appropriate place location = benchmark_base + "/original/namelist" my_file = open(location, 'w') my_file.write(namelist) my_file.flush() location = benchmark_base + "/common/Makefile.include" my_file = open(location, 'w') my_file.write(makefile_include) my_file.flush() # compile shallow if required base_path = benchmark_base + "/original" import subprocess make_process = subprocess.Popen(["make", "clean"], cwd=base_path, stderr=subprocess.PIPE, stdout=subprocess.PIPE) if make_process.wait() != 0: return False, [] make_process = subprocess.Popen(["make"], cwd=base_path, stderr=subprocess.PIPE, stdout=subprocess.PIPE) if make_process.wait() != 0: return False, [] # run shallow make_process = subprocess.Popen(["./shallow_base"], cwd=base_path, stderr=subprocess.PIPE, stdout=subprocess.PIPE) if make_process.wait() != 0: return False, [] # _ = make_process.stderr.read() stdout = make_process.stdout.read() # determine if the results are correct. We will need to look at # the results from stdout but for the moment we assume they are # correct # extract the required outputs for line in stdout.split("\n"): if "Time-stepping" in line: total_time = line.split()[2] return True, total_time
def create_vcard3_str(name, surname, displayname, email='', org='', title='', url='', note=''): """ Create a vCard3.0 string with the given parameters. Reference: http://www.evenx.com/vcard-3-0-format-specification """ vcard = [] vcard += ['BEGIN:VCARD'] vcard += ['VERSION:3.0'] if name and surname: name = name.strip() vcard += ['N:{};{};;;'.format(name, surname)] if not displayname: displayname = '{} {}'.format(name, surname) vcard += ['FN:{}'.format(displayname)] if email: vcard += ['EMAIL:{}'.format(email)] if org: vcard += ['ORG:{}'.format(org)] if title: vcard += ['TITLE:{}'.format(title)] if url: vcard += ['URL:{}'.format(url)] if note: vcard += ['NOTE:{}'.format(note)] vcard += ['END:VCARD'] return '\n'.join([field.strip() for field in vcard])
def strval(node, outermost=True): ''' XPath-like string value of node ''' if not isinstance(node, element): return node.xml_value if outermost else [node.xml_value] accumulator = [] for child in node.xml_children: if isinstance(child, text): accumulator.append(child.xml_value) elif isinstance(child, element): accumulator.extend(strval(child, outermost=False)) if outermost: accumulator = ''.join(accumulator) return accumulator
def xml_insert(self, child, index=-1): ''' Append a node as the last child child - the child to append. If a string, convert to a text node, for convenience ''' if isinstance(child, str): child = text(child, parent=self) else: child._xml_parent = weakref.ref(self) if index == -1: self.xml_children.append(child) else: self.xml_children.insert(index, child) return
def parse_options(): """ Commandline options arguments parsing. """ # build options and help version = "%%prog {version}".format(version=__version__) parser = OptionParser(version=version) parser.add_option( "-u", "--username", action="store", dest="username", type="string", default="", metavar="RECIPIENT", help="user" ) parser.add_option( "-C", "--calendar", metavar="CALENDAR", action="store", type="string", dest="calendar", default="", help="google calendar ID" ) parser.add_option( "-t", "--timezone", metavar="TIMEZONE", action="store", type="string", dest="timezone", default="", help="user timezone" ) parser.add_option( "-m", "--message", metavar="MESSAGE", action="store", type="string", dest="message", default="", help="message text" ) parser.add_option( "-c", "--config", metavar="CONFIG", action="store", type="string", dest="config", help="path to config file", default="/etc/nagios/notification_google_calendar.ini") parser.add_option( "-q", "--quiet", metavar="QUIET", action="store_true", default=False, dest="quiet", help="be quiet" ) parser.add_option( "-g", "--get-google-credentials", metavar="GET-GOOGLE-CREDENTIALS", action="store_true", default=False, dest="get_google_credentials", help="get google API credentials for user" ) options = parser.parse_args(sys.argv)[0] mandatories = ["username", ] # check mandatory command line options supplied if not options.get_google_credentials: mandatories.append("calendar") # set calendar option required when sending message mandatories.append("message") # set message option required when sending message mandatories.append("timezone") # set timezone option required when sending message if not all(options.__dict__[mandatory] for mandatory in mandatories): parser.error("Required command line option missing\n") return options
def parse_config(options): """ Get settings from config file. """ if os.path.exists(options.config): config = ConfigParser.ConfigParser() try: config.read(options.config) except Exception, err: if not options.quiet: sys.stderr.write("ERROR: Config file read {config} error. {err}".format(config=options.config, err=err)) sys.exit(-1) try: configdata = { "secrets": config.get("GOOGLE", "secrets"), "credentials": config.get("nagios-notification-google-calendar", "credentials"), "start": config.get("nagios-notification-google-calendar", "start"), "end": config.get("nagios-notification-google-calendar", "end"), "message": config.get("nagios-notification-google-calendar", "message"), } except ConfigParser.NoOptionError, err: if not options.quiet: sys.stderr.write("ERROR: Config file missing option error. {err}\n".format(err=err)) sys.exit(-1) # check mandatory config options supplied mandatories = ["secrets", "credentials", "start", "end", "message", ] if not all(configdata[mandatory] for mandatory in mandatories): if not options.quiet: sys.stdout.write("Mandatory config option missing\n") sys.exit(0) return configdata else: if not options.quiet: sys.stderr.write("ERROR: Config file {config} does not exist\n".format(config=options.config)) sys.exit(0)
def get_google_credentials(options, config): """ Get google API credentials for user. """ try: if options.get_google_credentials: flow = flow_from_clientsecrets(config["secrets"], scope=SCOPE, redirect_uri="oob") sys.stdout.write("Follow this URL: {url} and grant access to calendar.\n".format(url=flow.step1_get_authorize_url())) token = raw_input("Enter token:") credentials = flow.step2_exchange(token) storage = Storage(os.path.join(config["credentials"], "{username}.json".format(username=options.username))) storage.put(credentials) credentials.set_store(storage) else: storage = Storage(os.path.join(config["credentials"], "{username}.json".format(username=options.username))) credentials = storage.get() except Exception, err: if not options.quiet: sys.stderr.write("ERROR: Getting google API credentials error. {err}\n".format(err=err)) sys.exit(-1) return credentials
def create_event_datetimes(options, config): """ Create event start and end datetimes. """ now = datetime.datetime.now() return { "start": { "dateTime": (now + datetime.timedelta(minutes=int(config["start"]))).strftime(DT_FORMAT), "timeZone": options.timezone, }, "end": { "dateTime": (now + datetime.timedelta(minutes=int(config["end"]))).strftime(DT_FORMAT), "timeZone": options.timezone, }, }
def create_event(options, config, credentials): """ Create event in calendar with sms reminder. """ try: http = credentials.authorize(httplib2.Http()) service = build("calendar", "v3", http=http) event = { "summary": options.message, "location": "", "reminders": { "useDefault": False, "overrides": [ { "method": "sms", "minutes": config["message"], }, ], } } event.update(create_event_datetimes(options, config)) service.events().insert(calendarId=options.calendar, sendNotifications=True, body=event).execute() except Exception, err: if not options.quiet: sys.stderr.write("ERROR: Creating google calendar event error. {err}\n".format(err=err)) sys.exit(-1)
def main(): """ Processing notification call main function. """ # getting info for creating event options = parse_options() config = parse_config(options) credentials = get_google_credentials(options, config) if not options.get_google_credentials: create_event(options, config, credentials)
def create(input, template, field, outdir, prefix, otype, command, index, dpi, verbose, unicode_support): """Use docstamp to create documents from the content of a CSV file or a Google Spreadsheet. Examples: \n docstamp create -i badge.csv -t badge_template.svg -o badges docstamp create -i badge.csv -t badge_template.svg -o ./badges -d pdf """ logging.basicConfig(level=LOGGING_LVL) log = logging.getLogger(__name__) # setup verbose mode verbose_switch(verbose) input_file = input fields = field # init set of template contents log.debug('Reading CSV elements from {}.'.format(input_file)) items, fieldnames = get_items_from_csv(input_file) # check if got any item if len(items) == 0: click.echo('Quiting because found 0 items.') exit(-1) if not fields: # set the number of zeros that the files will have n_zeros = int(math.floor(math.log10(len(items))) + 1) else: # check that fields has all valid fields for field_name in fields: if field_name not in fieldnames: raise ValueError('Field name {} not found in input file ' ' header.'.format(field_name)) # filter the items if index if index: myitems = {int(idx): items[int(idx)] for idx in index} items = myitems log.debug('Using the elements with index {} of the input ' 'file.'.format(index)) # make output folder if not os.path.exists(outdir): os.mkdir(outdir) # create template document model log.debug('Creating the template object using the file {}.'.format(template)) template_doc = TextDocument.from_template_file(template, command) log.debug('Created an object of type {}.'.format(type(template_doc))) # let's stamp them! for idx in items: item = items[idx] if not len(fields): file_name = str(idx).zfill(n_zeros) else: field_values = [] try: for field_name in fields: field_values.append(item[field_name].replace(' ', '')) except: log.exception('Could not get field {} value from' ' {}'.format(field_name, item)) exit(-1) else: file_name = '_'.join(field_values) log.debug('Filling template {} with values of item {}.'.format(file_name, idx)) try: template_doc.fill(item) except: log.exception('Error filling document for {}th item'.format(idx)) continue # set output file path file_extension = get_extension(template) if prefix is None: basename = os.path.basename(template).replace(file_extension, '') file_name = basename + '_' + file_name file_path = os.path.join(outdir, file_name + '.' + otype) kwargs = {'file_type': otype, 'dpi': dpi, 'support_unicode': unicode_support} log.debug('Rendering file {}.'.format(file_path)) try: template_doc.render(file_path, **kwargs) except: log.exception('Error creating {} for {}.'.format(file_path, item)) exit(-1) else: log.debug('Successfully rendered {}.'.format(file_path))
def get_extension(filepath, check_if_exists=False): """Return the extension of fpath. Parameters ---------- fpath: string File name or path check_if_exists: bool Returns ------- str The extension of the file name or path """ if check_if_exists: if not os.path.exists(filepath): err = 'File not found: ' + filepath log.error(err) raise IOError(err) try: rest, ext = os.path.splitext(filepath) except: raise else: return ext
def add_extension_if_needed(filepath, ext, check_if_exists=False): """Add the extension ext to fpath if it doesn't have it. Parameters ---------- filepath: str File name or path ext: str File extension check_if_exists: bool Returns ------- File name or path with extension added, if needed. """ if not filepath.endswith(ext): filepath += ext if check_if_exists: if not os.path.exists(filepath): err = 'File not found: ' + filepath log.error(err) raise IOError(err) return filepath
def get_tempfile(suffix='.txt', dirpath=None): """ Return a temporary file with the given suffix within dirpath. If dirpath is None, will look for a temporary folder in your system. Parameters ---------- suffix: str Temporary file name suffix dirpath: str Folder path where create the temporary file Returns ------- temp_filepath: str The path to the temporary path """ if dirpath is None: dirpath = get_temp_dir() return tempfile.NamedTemporaryFile(suffix=suffix, dir=dirpath)
def cleanup(workdir, extension): """ Remove the files in workdir that have the given extension. Parameters ---------- workdir: Folder path from where to clean the files. extension: str File extension without the dot, e.g., 'txt' """ [os.remove(f) for f in glob(os.path.join(workdir, '*.' + extension))]
def csv_to_json(csv_filepath, json_filepath, fieldnames, ignore_first_line=True): """ Convert a CSV file in `csv_filepath` into a JSON file in `json_filepath`. Parameters ---------- csv_filepath: str Path to the input CSV file. json_filepath: str Path to the output JSON file. Will be overwritten if exists. fieldnames: List[str] Names of the fields in the CSV file. ignore_first_line: bool """ import csv import json csvfile = open(csv_filepath, 'r') jsonfile = open(json_filepath, 'w') reader = csv.DictReader(csvfile, fieldnames) rows = [] if ignore_first_line: next(reader) for row in reader: rows.append(row) json.dump(rows, jsonfile) jsonfile.close() csvfile.close()
def write_to_file(file_path, content, encoding=None): """ Write `content` inside the file in `file_path` with the given encoding. Parameters ---------- file_path: str Path to the output file. Will be overwritten if exists. content: str The content you want in the file. encoding: str The name of the encoding. """ try: # TODO: check if in Python2 this should be this way # it's possible that we have to make this function more complex # to check type(content) and depending on that set 'w' without enconde # or 'wb' with encode. with open(file_path, "wb") as f: f.write(content.encode(encoding)) except: log.exception('Error writing to file in {}'.format(file_path)) raise
def replace_file_content(filepath, old, new, max=1): """ Modify the content of `filepath`, replacing `old` for `new`. Parameters ---------- filepath: str Path to the file to be modified. It will be overwritten. old: str This is old substring to be replaced. new: str This is new substring, which would replace old substring. max: int If larger than 0, Only the first `max` occurrences are replaced. """ with open(filepath, 'r') as f: content = f.read() content = content.replace(old, new, max) with open(filepath, 'w') as f: f.write(content)
def cleanup_docstamp_output(output_dir=''): """ Remove the 'tmp*.aux', 'tmp*.out' and 'tmp*.log' files in `output_dir`. :param output_dir: """ suffixes = ['aux', 'out', 'log'] files = [f for suf in suffixes for f in glob(os.path.join(output_dir, 'tmp*.{}'.format(suf)))] [os.remove(file) for file in files]
def parse(self): """ Run all parsing functions. """ for tag in self.soup.findAll('span'): self.create_italic(tag) self.create_strong(tag) self.create_underline(tag) self.unwrap_span(tag) for tag in self.soup.findAll('a'): self.remove_comments(tag) self.check_next(tag) if self.soup.body: for tag in self.soup.body.findAll(): self.remove_empty(tag) self.remove_inline_comment(tag) self.parse_attrs(tag) for token, target in self.tokens: self.find_token(tag, token, target) self.remove_blacklisted_tags(tag)
def check_next(self, tag): """ If next tag is link with same href, combine them. """ if (type(tag.next_sibling) == element.Tag and tag.next_sibling.name == 'a'): next_tag = tag.next_sibling if tag.get('href') and next_tag.get('href'): href = self._parse_href(tag.get('href')) next_href = self._parse_href(next_tag.get('href')) if href == next_href: next_text = next_tag.get_text() tag.append(next_text) self.tags_blacklist.append(next_tag)
def create_italic(self, tag): """ See if span tag has italic style and wrap with em tag. """ style = tag.get('style') if style and 'font-style:italic' in style: tag.wrap(self.soup.new_tag('em'))
def create_strong(self, tag): """ See if span tag has bold style and wrap with strong tag. """ style = tag.get('style') if (style and ('font-weight:bold' in style or 'font-weight:700' in style)): tag.wrap(self.soup.new_tag('strong'))
def create_underline(self, tag): """ See if span tag has underline style and wrap with u tag. """ style = tag.get('style') if style and 'text-decoration:underline' in style: tag.wrap(self.soup.new_tag('u'))
def parse_attrs(self, tag): """ Reject attributes not defined in ATTR_WHITELIST. """ if tag.name in ATTR_WHITELIST.keys(): attrs = copy(tag.attrs) for attr, value in attrs.items(): if attr in ATTR_WHITELIST[tag.name]: tag.attrs[attr] = self._parse_attr(tag.name, attr, value) else: del tag.attrs[attr] else: tag.attrs = {}
def remove_empty(self, tag): """ Remove non-self-closing tags with no children *and* no content. """ has_children = len(tag.contents) has_text = len(list(tag.stripped_strings)) if not has_children and not has_text and not tag.is_empty_element: tag.extract()
def clean_linebreaks(self, tag): """ get unicode string without any other content transformation. and clean extra spaces """ stripped = tag.decode(formatter=None) stripped = re.sub('\s+', ' ', stripped) stripped = re.sub('\n', '', stripped) return stripped
def _parse_href(self, href): """ Extract "real" URL from Google redirected url by getting `q` querystring parameter. """ params = parse_qs(urlsplit(href).query) return params.get('q')
def _parse_attr(self, tagname, attr, value): """ Parse attribute. Delegate to href parser for hrefs, otherwise return value. """ if tagname == 'a' and attr == 'href': return self._parse_href(value) else: return value
def translate_key_values(adict, translations, default=''): """Modify the keys in adict to the ones in translations. Be careful, this will modify your input dictionary. The keys not present in translations will be left intact. Parameters ---------- adict: a dictionary translations: iterable of 2-tuples Each 2-tuple must have the following format: (<adict existing key>, <desired key name for the existing key>) Returns ------- Translated adict """ for src_key, dst_key in translations: adict[dst_key] = adict.pop(src_key, default) return adict