def print_terminal_help_topic(topic): helpmodule = importlib.import_module('cqlhelp.cql%s' % "".join(w.capitalize() for w in topic.split("_"))) lines = helpmodule.__doc__.split('\n') try: rows, columns = subprocess.check_output(['stty', 'size']).split() except: rows = 25 columns = 80 wrapper = TextWrapper() wrapper.break_on_hyphens = False try: wrapper.width = int(columns) except ValueError: wrapper.width = 80 for line in lines: if re.match(r'\s', line): line = line.strip() wrapper.initial_indent = ' ' if line.startswith('-'): wrapper.subsequent_indent = ' ' else: wrapper.subsequent_indent = ' ' else: wrapper.initial_indent = '' wrapper.subsequent_indent = '' print(wrapper.fill(line))
def _print_list_helper(cls, list_msg, message_list, helper_info=None): def kwargs_fn(msg): fieldnames = [ fname for _, fname, _, _ in Formatter().parse(list_msg) if fname ] dic = dict() for fieldname in fieldnames: val = getattr(msg, fieldname) if isinstance(val, pathlib.Path): val = val.name dic[fieldname] = val return dic final_msg = "%s:\n" % cls.STATIC_MSG final_msg += "\t%s" % ("\n\t".join( "- " + list_msg.format(**kwargs_fn(msg)) for msg in message_list)) if helper_info: wrapper = TextWrapper() wrapper.initial_indent = "\t\t" wrapper.subsequent_indent = "\t\t" final_msg += "\n\tRemarks:\n%s" % wrapper.fill(helper_info) return final_msg
def CppInitializations(self, Indent=4): """Create initialization list for C++ For example, if the `Variables` object contains atoms m1, m2, t, and x referred to in the `Expressions` object, where m1 and m2 are constant, and t and x are variables, the initialization list should be m1(m1_i), m2(m2_i), t(t_i), x(x_i) The quantities m1_i, etc., appear in the input-argument list output by the method `CppInputArguments`. """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = ' ' * Indent wrapper.subsequent_indent = wrapper.initial_indent def Initialization(atom): if atom.datatype and (atom.datatype == 'std::vector<double>' or atom.datatype == 'std::vector<std::complex<double> >'): return '{0}({1})'.format(self.Variables[atom], len(atom.substitution)) if atom.fundamental: return '{0}({0}_i)'.format(self.Variables[atom]) else: return '{0}({1})'.format(self.Variables[atom], atom.ccode()) Initializations = [Initialization(atom) for atom in self.Atoms] return wrapper.fill(', '.join(Initializations))
def CppInputArguments(self, Indent=12): """Create basic input arguments for C++ The fundamental variables are listed, along with their data types and `const` if the variable is constant. This would be an appropriate string to represent the input arguments for a function or class constructor to calculate the `Expressions` of this CodeConstructor object. For example, if the `Variables` object contains atoms m1, m2, t, and x referred to in the `Expressions` object, where m1 and m2 are constant, and t and x are variables, the input argument list should be const double m1_i, const double m2_i, double t_i, double x_i """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = ' ' * Indent wrapper.subsequent_indent = wrapper.initial_indent InputArguments = [ 'const {0} {1}_i'.format(self.dtype(atom), self.Variables[atom]) for atom in self.Atoms if atom.fundamental ] return wrapper.fill(', '.join(InputArguments)).lstrip()
def CppEvaluateExpressions(self, Indent=4, Expressions=None): """Declare and define the `Expressions` for C++ The output of this function declares are defines the `Expressions` as individual variables. An optional dictionary of expressions allows just a subset of this object's expressions to be output; if this argument is not present, all will be output. """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = ' ' * Indent wrapper.subsequent_indent = wrapper.initial_indent + ' ' Evaluations = [] if not Expressions: Expressions = self.Expressions for Expression in Expressions: try: Evaluations.append( wrapper.fill('{0}{1} {2} = {3};'.format( self.const(Expression), self.dtype(Expression), Expressions[Expression], Expression.ccode()))) except TypeError: pass return '\n'.join(Evaluations)
def refill(msg): """ Refill a changelog message. Normalize the message reducing multiple spaces and newlines to single spaces, recognizing common form of ``bullet lists``, that is paragraphs starting with either a dash "-" or an asterisk "*". """ wrapper = TextWrapper() res = [] items = itemize_re.split(msg.strip()) if len(items) > 1: # Remove possible first empty split, when the message immediately # starts with a bullet if not items[0]: del items[0] if len(items) > 1: wrapper.initial_indent = '- ' wrapper.subsequent_indent = ' ' * 2 for item in items: if item: words = filter(None, item.strip().replace('\n', ' ').split(' ')) normalized = ' '.join(words) res.append(wrapper.fill(normalized)) return '\n\n'.join(res)
def printHeader(s, level=1, length=70, prefix='# <Menotexport>:'): from textwrap import TextWrapper decs = {1: '=', 2: '-', 3: '.'} indents = {1: 0, 2: 4, 3: 8} dec = decs[level] ind = indents[level] indstr = ' ' * int(ind) wrapper = TextWrapper() wrapper.width = length - ind wrapper.initial_indent = indstr wrapper.subsequent_indent = indstr #-------------Get delimiter line------------- hline = '%s%s' % (' ' * int(ind), dec * int(length - ind)) #--------------------Wrap texts-------------------- strings = wrapper.wrap('%s %s' % (prefix, s)) #----------------------Print---------------------- try: print('\n' + hline) except: print('\n' + hline.encode('ascii', 'replace')) for ss in strings: try: print(ss) except: print(ss.encode('ascii', 'replace')) #print(hline) return
def dump_recursive_comments(rpc, post_author, post_permlink, depth=0, format="markdown"): global currentThreadDepth postWrapper = TextWrapper() postWrapper.width = 120 postWrapper.initial_indent = " " * (depth + currentThreadDepth) postWrapper.subsequent_indent = " " * (depth + currentThreadDepth) depth = int(depth) posts = rpc.get_content_replies(post_author, post_permlink) for post in posts: meta = {} for key in ["author", "permlink"]: meta[key] = post[key] meta["reply"] = "@{author}/{permlink}".format(**post) if format == "markdown": body = markdownify(post["body"]) else: body = post["body"] yaml = frontmatter.Post(body, **meta) print(frontmatter.dumps(yaml)) reply = rpc.get_content_replies(post["author"], post["permlink"]) if len(reply): dump_recursive_comments(rpc, post["author"], post["permlink"], depth + 1)
def dump_recursive_comments(rpc, post_author, post_permlink, depth=0, format="markdown"): global currentThreadDepth postWrapper = TextWrapper() postWrapper.width = 120 postWrapper.initial_indent = " " * (depth + currentThreadDepth) postWrapper.subsequent_indent = " " * (depth + currentThreadDepth) depth = int(depth) posts = rpc.get_content_replies(post_author, post_permlink) for post in posts: meta = {} for key in ["author", "permlink"]: meta[key] = post[key] meta["reply"] = "@{author}/{permlink}".format(**post) if format == "markdown": body = markdownify(post["body"]) else: body = post["body"] yaml = frontmatter.Post(body, **meta) print(frontmatter.dumps(yaml)) reply = rpc.get_content_replies(post["author"], post["permlink"]) if len(reply): dump_recursive_comments(rpc, post["author"], post["permlink"], depth + 1)
def _stream_formatter(self, record): """The formatter for standard output.""" if record.levelno < logging.DEBUG: print(record.levelname, end='') elif(record.levelno < logging.INFO): colourPrint(record.levelname, 'green', end='') elif(record.levelno < IMPORTANT): colourPrint(record.levelname, 'magenta', end='') elif(record.levelno < logging.WARNING): colourPrint(record.levelname, 'lightblue', end='') elif(record.levelno < logging.ERROR): colourPrint(record.levelname, 'brown', end='') else: colourPrint(record.levelname, 'red', end='') if record.levelno == logging.WARN: message = '{0}'.format(record.msg[record.msg.find(':')+2:]) else: message = '{0}'.format(record.msg) if len(message) > self.wrapperLength: tw = TextWrapper() tw.width = self.wrapperLength tw.subsequent_indent = ' ' * (len(record.levelname)+2) tw.break_on_hyphens = False message = '\n'.join(tw.wrap(message)) print(': ' + message)
def CppInitializations(self, Indent=4): """Create initialization list for C++ For example, if the `Variables` object contains atoms m1, m2, t, and x referred to in the `Expressions` object, where m1 and m2 are constant, and t and x are variables, the initialization list should be m1(m1_i), m2(m2_i), t(t_i), x(x_i) The quantities m1_i, etc., appear in the input-argument list output by the method `CppInputArguments`. """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = ' '*Indent wrapper.subsequent_indent = wrapper.initial_indent def Initialization(atom): if atom.datatype and (atom.datatype=='std::vector<double>' or atom.datatype=='std::vector<std::complex<double> >'): return '{0}({1})'.format(self.Variables[atom], len(atom.substitution)) if atom.fundamental: return '{0}({0}_i)'.format(self.Variables[atom]) else: return '{0}({1})'.format(self.Variables[atom], atom.ccode()) Initializations = [Initialization(atom) for atom in self.Atoms] return wrapper.fill(', '.join(Initializations))
def CppEvaluations(self, Indent=4): """Evaluate all derived variables in C++ This function uses the `substitution` expressions for the derived variables. This output is appropriate for updating the values of the variables at each step of an integration, for example. """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = ' '*Indent wrapper.subsequent_indent = wrapper.initial_indent + ' ' def Evaluation(atom): def Ccode(a) : try: return a.ccode() except : from sympy.printing import ccode return ccode(a) if atom.datatype and (atom.datatype=='std::vector<double>' or atom.datatype=='std::vector<std::complex<double> >') : return '\n'.join([wrapper.fill('{0}[{1}] = {2};'.format(self.Variables[atom], i, Ccode(atom.substitution[i]))) for i in range(len(atom.substitution))]) else: return wrapper.fill('{0} = {1};'.format(self.Variables[atom], atom.ccode())) return '\n'.join([Evaluation(atom) for atom in self.Atoms if not atom.fundamental and not atom.constant])
def list_posts(discussions): t = PrettyTable([ "identifier", "title", "category", "replies", # "votes", "payouts", ]) t.align = "l" t.align["payouts"] = "r" # t.align["votes"] = "r" t.align["replies"] = "c" for d in discussions: identifier = "@%s/%s" % (d["author"], d["permlink"]) identifier_wrapper = TextWrapper() identifier_wrapper.width = 60 identifier_wrapper.subsequent_indent = " " t.add_row([ identifier_wrapper.fill(identifier), identifier_wrapper.fill(d["title"]), d["category"], d["children"], # d["net_rshares"], d["pending_payout_value"], ]) print(t)
def do_me(self,mdig_model): from textwrap import TextWrapper import re models = mdig.repository.get_models() title_str = "Models in MDiG GRASS db @ " + mdig.repository.db print "-"*len(title_str) print title_str print "model_name [location]" print " description" print "-"*len(title_str) ms=models.keys()[:] ms.sort() for m in ms: try: dm = DispersalModel(models[m],setup=False) tw = TextWrapper(expand_tabs = False, replace_whitespace = True ) tw.initial_indent = " "*4 tw.subsequent_indent = " "*4 desc = dm.get_description() desc = re.sub("[\\s\\t]+"," ",desc) desc = tw.fill(desc) loc = dm.get_location() if not loc: loc = dm.infer_location() if not loc: loc = "unknown" print "%s [%s]:\n%s" % (m,loc,desc) except mdig.model.ValidationError: print "%s [ERROR]" % (m,) sys.exit(0)
def do_me(self, mdig_model): from textwrap import TextWrapper import re models = mdig.repository.get_models() title_str = "Models in MDiG GRASS db @ " + mdig.repository.db print "-" * len(title_str) print title_str print "model_name [location]" print " description" print "-" * len(title_str) ms = models.keys()[:] ms.sort() for m in ms: try: dm = DispersalModel(models[m], setup=False) tw = TextWrapper(expand_tabs=False, replace_whitespace=True) tw.initial_indent = " " * 4 tw.subsequent_indent = " " * 4 desc = dm.get_description() desc = re.sub("[\\s\\t]+", " ", desc) desc = tw.fill(desc) loc = dm.get_location() if not loc: loc = dm.infer_location() if not loc: loc = "unknown" print "%s [%s]:\n%s" % (m, loc, desc) except mdig.model.ValidationError: print "%s [ERROR]" % (m, ) sys.exit(0)
def info(cls, _str=True): if not _str: return PCFGConfig.argNames # Auto text wrapper to output the doc. tw = TextWrapper() tw.initial_indent = " " tw.subsequent_indent = " " retVal = "General Configuration: \n" for argName in PCFGConfig.argNames: arg = str(argName["arg"]) argreq = str(argName["req"]) argtype = str(argName["type"].__name__) argdef = str(argName["def"]) argdoc = str(argName["doc"]) argex = str(argName["ex"]) doclines = tw.wrap(argdoc) aType = "optional" if argreq: aType = "required" retVal += " %s (%s, %s):\n" % (arg, argtype, aType) retVal += " Defaults to %s\n" % (argdef) for docline in doclines: retVal += "%s\n" % docline retVal += " Example: %s\n" % argex retVal += "\n" return retVal
def usage(self): tw = TextWrapper( width=78, drop_whitespace=True, expand_tabs=True, fix_sentence_endings=True, break_long_words=True, break_on_hyphens=True, ) text = tw.fill(self.__doc__.strip()) + "\n\n" try: options = self.pluginOptions except AttributeError: return text + "This plugin does not support any options.\n" tw.subsequent_indent=' ' * 16, text += "Options supported by this plugin:\n" for opt in sorted(options.keys()): text += "--opt={:<12} ".format(opt) text += tw.fill(options[opt].strip()) + "\n" text += "\n" text += "You can also chain options together, e.g.:\n" text += " --opt=systems,stations,csvonly\n" return text
def list_posts(discussions): t = PrettyTable([ "identifier", "title", "category", "replies", # "votes", "payouts", ]) t.align = "l" t.align["payouts"] = "r" # t.align["votes"] = "r" t.align["replies"] = "c" for d in discussions: # Some discussions are dicts or identifiers if isinstance(d, str): d = discussions[d] identifier = "@%s/%s" % (d["author"], d["permlink"]) identifier_wrapper = TextWrapper() identifier_wrapper.width = 60 identifier_wrapper.subsequent_indent = " " t.add_row([ identifier_wrapper.fill(identifier), identifier_wrapper.fill(d["title"]), d["category"], d["children"], # d["net_rshares"], d["pending_payout_value"], ]) print(t)
def printHeader(s, level=1, length=70, prefix='# <Menotexport>:'): from textwrap import TextWrapper decs={1: '=', 2: '-', 3: '.'} indents={1: 0, 2: 4, 3: 8} dec=decs[level] ind=indents[level] indstr=' '*int(ind) wrapper=TextWrapper() wrapper.width=length-ind wrapper.initial_indent=indstr wrapper.subsequent_indent=indstr #-------------Get delimiter line------------- hline='%s%s' %(' '*int(ind),dec*int(length-ind)) #--------------------Wrap texts-------------------- strings=wrapper.wrap('%s %s' %(prefix,s)) #----------------------Print---------------------- try: print('\n'+hline) except: print('\n'+hline.encode('ascii','replace')) for ss in strings: try: print(ss) except: print(ss.encode('ascii','replace')) #print(hline) return
def _stream_formatter(self, record): """The formatter for standard output.""" if record.levelno < logging.DEBUG: print(record.levelname, end='') elif(record.levelno < logging.INFO): colourPrint(record.levelname, 'green', end='') elif(record.levelno < IMPORTANT): colourPrint(record.levelname, 'magenta', end='') elif(record.levelno < logging.WARNING): colourPrint(record.levelname, 'lightblue', end='') elif(record.levelno < logging.ERROR): colourPrint(record.levelname, 'brown', end='') else: colourPrint(record.levelname, 'red', end='') if record.levelno == logging.WARN: message = '{0}'.format(record.msg[record.msg.find(':') + 2:]) else: message = '{0}'.format(record.msg) if len(message) > self.wrapperLength: tw = TextWrapper() tw.width = self.wrapperLength tw.subsequent_indent = ' ' * (len(record.levelname) + 2) tw.break_on_hyphens = False message = '\n'.join(tw.wrap(message)) print(': ' + message)
def compose(self, parser, attr_of=None): # find the original line indentation and its length in characters indentation = self[0].indentation indentation_length = len(indentation.expandtabs(parser.tab_size)) # add the contents of all lines together contents = " ".join([l.contents.strip() for l in self if l.contents]) # wrap the text at the remaining width width = parser.width - indentation_length wrapper = TextWrapper(width=width, initial_indent=self[0].start, break_long_words=False, break_on_hyphens=False) # indent all but the first lines with an additional level wrapper.subsequent_indent = " " * parser.tab_size lines = wrapper.wrap(contents) # prepend the indentation to all lines for i in range(0, len(lines)): # turn the subsequent indentation into a tab character if i > 0: lines[i] = re.sub(r"^" + wrapper.subsequent_indent, "\t", lines[i]) lines[i] = indentation + lines[i] + "\n" return "".join(lines)
def wrap_for_make(items): line = join(sorted(items)) wrapper = TextWrapper() wrapper.width = 60 wrapper.break_on_hyphens = False wrapper.subsequent_indent = '\t' * 2 return ' \\\n'.join(wrapper.wrap(line))
def dump_recursive_parents(rpc, post_author, post_permlink, limit=1, format="markdown"): global currentThreadDepth limit = int(limit) postWrapper = TextWrapper() postWrapper.width = 120 postWrapper.initial_indent = " " * (limit) postWrapper.subsequent_indent = " " * (limit) if limit > currentThreadDepth: currentThreadDepth = limit + 1 post = rpc.get_content(post_author, post_permlink) if limit and post["parent_author"]: parent = rpc.get_content_replies(post["parent_author"], post["parent_permlink"]) if len(parent): dump_recursive_parents(rpc, post["parent_author"], post["parent_permlink"], limit - 1) meta = {} for key in ["author", "permlink"]: meta[key] = post[key] meta["reply"] = "@{author}/{permlink}".format(**post) if format == "markdown": body = markdownify(post["body"]) else: body = post["body"] yaml = frontmatter.Post(body, **meta) print(frontmatter.dumps(yaml))
def dump_recursive_parents(rpc, post_author, post_permlink, limit=1, format="markdown"): global currentThreadDepth limit = int(limit) postWrapper = TextWrapper() postWrapper.width = 120 postWrapper.initial_indent = " " * (limit) postWrapper.subsequent_indent = " " * (limit) if limit > currentThreadDepth: currentThreadDepth = limit + 1 post = rpc.get_content(post_author, post_permlink) if limit and post["parent_author"]: parent = rpc.get_content_replies(post["parent_author"], post["parent_permlink"]) if len(parent): dump_recursive_parents(rpc, post["parent_author"], post["parent_permlink"], limit - 1) meta = {} for key in ["author", "permlink"]: meta[key] = post[key] meta["reply"] = "@{author}/{permlink}".format(**post) if format == "markdown": body = markdownify(post["body"]) else: body = post["body"] yaml = frontmatter.Post(body, **meta) print(frontmatter.dumps(yaml))
def refill(msg): """ Refill a changelog message. Normalize the message reducing multiple spaces and newlines to single spaces, recognizing common form of ``bullet lists``, that is paragraphs starting with either a dash "-" or an asterisk "*". """ wrapper = TextWrapper() res = [] items = itemize_re.split(msg.strip()) if len(items)>1: # Remove possible first empty split, when the message immediately # starts with a bullet if not items[0]: del items[0] if len(items)>1: wrapper.initial_indent = '- ' wrapper.subsequent_indent = ' '*2 for item in items: if item: words = filter(None, item.strip().replace('\n', ' ').split(' ')) normalized = ' '.join(words) res.append(wrapper.fill(normalized)) return '\n\n'.join(res)
def main(): wrapper = TextWrapper(width=80) leading_whitespace_re = re.compile(r"^\s+") for line in fileinput.input(): current_indent = leading_whitespace_re.search(line).end() wrapper.subsequent_indent = " " * (current_indent + 2) print(wrapper.fill(line))
def usage(self): tw = TextWrapper( width=78, drop_whitespace=True, expand_tabs=True, fix_sentence_endings=True, break_long_words=True, break_on_hyphens=True, ) text = tw.fill(self.__doc__.strip()) + "\n\n" try: options = self.pluginOptions except AttributeError: return text + "This plugin does not support any options.\n" tw.subsequent_indent = ' ' * 16, text += "Options supported by this plugin:\n" for opt in sorted(options.keys()): text += "--opt={:<12} ".format(opt) text += tw.fill(options[opt].strip()) + "\n" text += "\n" text += "You can also chain options together, e.g.:\n" text += " --opt={}\n".format(",".join(list(options.keys())[:3])) return text
def default_attribute_formatter(self, key, value): wrapper = TextWrapper() wrapper.initial_indent='FT ' wrapper.subsequent_indent='FT ' wrapper.width=79 attribute_text_template='/{attribute_key}="{attribute_value}"' attribute_text=attribute_text_template.format(attribute_key=key, attribute_value=value) return wrapper.fill(attribute_text)
def number_attribute_formatter(self, key, value): # transl_table attributes do not have their values in quotes wrapper = TextWrapper() wrapper.initial_indent='FT ' wrapper.subsequent_indent='FT ' wrapper.width=79 attribute_text_template='/{attribute_key}={attribute_value}' attribute_text=attribute_text_template.format(attribute_key=key, attribute_value=value) return wrapper.fill(attribute_text)
def default_attribute_formatter(self, key, value): wrapper = TextWrapper() wrapper.initial_indent = 'FT ' wrapper.subsequent_indent = 'FT ' wrapper.width = 80 # can use 80 characters plus the new line attribute_text_template = '/{attribute_key}="{attribute_value}"' attribute_text = attribute_text_template.format(attribute_key=key, attribute_value=value) return wrapper.fill(attribute_text)
def number_attribute_formatter(self, key, value): # transl_table attributes do not have their values in quotes wrapper = TextWrapper() wrapper.initial_indent = 'FT ' wrapper.subsequent_indent = 'FT ' wrapper.width = 80 # can use 80 characters plus the new line attribute_text_template = '/{attribute_key}={attribute_value}' attribute_text = attribute_text_template.format(attribute_key=key, attribute_value=value) return wrapper.fill(attribute_text)
def product_attribute_formatter(self, key, value): # Products can include very long enzyme names which we don't want to break wrapper = TextWrapper() wrapper.initial_indent='FT ' wrapper.subsequent_indent='FT ' wrapper.width=79 wrapper.break_on_hyphens=True attribute_text_template='/{attribute_key}="{attribute_value}"' attribute_text=attribute_text_template.format(attribute_key=key, attribute_value=value) return wrapper.fill(attribute_text)
def header_attribute_formatter(self, key, header_text, quote_character, final_character): wrapper = TextWrapper() wrapper.initial_indent=key + ' ' wrapper.subsequent_indent=key + ' ' wrapper.width=79 attribute_text_template='{attribute_quote_character}{attribute_header_text}{attribute_quote_character}{attribute_final_character}' attribute_text=attribute_text_template.format(attribute_header_text = header_text, attribute_quote_character = quote_character, attribute_final_character = final_character) return wrapper.fill(attribute_text)
def _print_natspec(natspec: Dict) -> None: wrapper = TextWrapper(initial_indent=f" {color('bright magenta')}") for key in [i for i in ("title", "notice", "author", "details") if i in natspec]: wrapper.subsequent_indent = " " * (len(key) + 4) print(wrapper.fill(f"@{key} {color}{natspec[key]}")) for key, value in natspec.get("params", {}).items(): wrapper.subsequent_indent = " " * 9 print(wrapper.fill(f"@param {color('bright blue')}{key}{color} {value}")) if "return" in natspec: wrapper.subsequent_indent = " " * 10 print(wrapper.fill(f"@return {color}{natspec['return']}")) for key in sorted(natspec.get("returns", [])): wrapper.subsequent_indent = " " * 10 print(wrapper.fill(f"@return {color}{natspec['returns'][key]}")) print()
def product_attribute_formatter(self, key, value): # Products can include very long enzyme names which we don't want to break wrapper = TextWrapper() wrapper.initial_indent = 'FT ' wrapper.subsequent_indent = 'FT ' wrapper.width = 80 # can use 80 characters plus the new line wrapper.break_on_hyphens = True attribute_text_template = '/{attribute_key}="{attribute_value}"' attribute_text = attribute_text_template.format(attribute_key=key, attribute_value=value) return wrapper.fill(attribute_text)
def header_attribute_formatter(self, key, header_text, quote_character, final_character): wrapper = TextWrapper() wrapper.initial_indent = key + ' ' wrapper.subsequent_indent = key + ' ' wrapper.width = 80 # can use 80 characters plus the new line attribute_text_template = '{attribute_quote_character}{attribute_header_text}{attribute_quote_character}{attribute_final_character}' attribute_text = attribute_text_template.format( attribute_header_text=header_text, attribute_quote_character=quote_character, attribute_final_character=final_character) return wrapper.fill(attribute_text)
def _pretty_print(key_val, sep=': ', min_col_width=39, text_width=None): """Print a iterable of key/values. Args: key_val (list of (str, str)): the pairs of section names and text. sep (str): separator between section names and text. min_col_width (int): minimal acceptable column width text_width (int): text width to use. If set to None, will try to infer the size of the terminal. """ if text_width is None: text_width = get_terminal_size().columns if text_width < min_col_width: min_col_width = text_width ncols = (text_width + 1) // (min_col_width + 1) colw = (text_width + 1) // ncols - 1 ncols = min(ncols, len(key_val)) wrapper = TextWrapper(width=colw) lines = [] for key, val in key_val: if len(key) + len(sep) >= colw // 2: wrapper.subsequent_indent = ' ' else: wrapper.subsequent_indent = ' ' * (len(key) + len(sep)) lines.extend(wrapper.wrap(f'{key}{sep}{val}')) chunks = [] for rem_col in range(ncols, 1, -1): isep = ceil(len(lines) / rem_col) while isep < len(lines) and lines[isep][0] == ' ': isep += 1 chunks.append(lines[:isep]) lines = lines[isep:] chunks.append(lines) lines = zip_longest(*chunks, fillvalue='') fmt = '|'.join([f'{{:{colw}}}'] * (ncols - 1)) fmt += '|{}' if ncols > 1 else '{}' print(*(fmt.format(*line) for line in lines), sep='\n')
def __get_text_wrapper(width=60): """ Get text wrapper with a fixed with. :param width: width of the wrapper. Default 60. :return: text wrapper :rtype: :py:class:`TextWrapper` """ wrapper = TextWrapper() wrapper.width = width wrapper.subsequent_indent = " " return wrapper
def CppDeclarations(self, Indent=4): """Create declaration statements for C++ For example, if the `Variables` object contains atoms m1, m2, t, and x referred to in the `Expressions` object, where m1 and m2 are constant, and t and x are variables, the declaration list should be const double m1, m2; double t, x; The code knows which atoms need to be declared at the beginning, and which ones should be `const`, for example. For C++, the default datatype is `double`; if the atom was created with a different datatype, that will be used appropriately. """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = '' wrapper.subsequent_indent = '' datatype = '' Declarations = '' Names = [] for atom in self.Atoms: thisdatatype = CodeConstructor.const(atom) + CodeConstructor.dtype( atom) + ' ' if thisdatatype != datatype: if Names: Declarations += wrapper.fill(', '.join(Names)) + ";\n" Names = [] datatype = thisdatatype wrapper.initial_indent = ' ' * Indent + thisdatatype wrapper.subsequent_indent = ' ' * len(wrapper.initial_indent) Names.append(self.Variables[atom]) if Names: Declarations += wrapper.fill(', '.join(Names)) + ";\n" return Declarations.rstrip()
def CppDeclarations(self, Indent=4): """Create declaration statements for C++ For example, if the `Variables` object contains atoms m1, m2, t, and x referred to in the `Expressions` object, where m1 and m2 are constant, and t and x are variables, the declaration list should be const double m1, m2; double t, x; The code knows which atoms need to be declared at the beginning, and which ones should be `const`, for example. For C++, the default datatype is `double`; if the atom was created with a different datatype, that will be used appropriately. """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = '' wrapper.subsequent_indent = '' datatype = '' Declarations = '' Names = [] for atom in self.Atoms: thisdatatype = CodeConstructor.const(atom) + CodeConstructor.dtype(atom) + ' ' if thisdatatype != datatype: if Names: Declarations += wrapper.fill(', '.join(Names)) + ";\n" Names = [] datatype = thisdatatype wrapper.initial_indent = ' '*Indent + thisdatatype wrapper.subsequent_indent = ' '*len(wrapper.initial_indent) Names.append(self.Variables[atom]) if Names: Declarations += wrapper.fill(', '.join(Names)) + ";\n" return Declarations.rstrip()
def CppExpressionsAsFunctions(self, Indent=4, Expressions=None): """Define functions to calculate the `Expressions` in C++ The output of this function gives C++ functions to calculate the `Expressions`, assuming the functions are member methods in a class, and so can access the atoms of the expression without explicit arguments. An optional dictionary of expressions allows just a subset of this object's expressions to be output; if this argument is not present, all will be output. """ def dtype(e): if e.datatype: return e.datatype else: return 'double' from textwrap import TextWrapper from PNObjects import PNCollection wrapper = TextWrapper(width=120) wrapper.initial_indent = ' ' * Indent + ' return ' wrapper.subsequent_indent = ' ' * Indent + ' ' Evaluations = [] if not Expressions: Expressions = self.Expressions for Expression in Expressions: ExprColl = PNCollection() for atom in Expression.substitution_atoms: if atom not in self.Variables: try: ExprColl.AddDerivedVariable( str(atom), atom.substitution, substitution_atoms=atom.substitution_atoms, datatype=atom.datatype) except TypeError: pass MiniConstructor = CodeConstructor(self.Variables, ExprColl) Evaluations.append(' ' * Indent + dtype(Expression) + ' ' + Expressions[Expression] + '() {\n' + MiniConstructor.CppEvaluateExpressions(Indent + 2) + '\n' + wrapper.fill(Expression.ccode()) + ';\n' + ' ' * Indent + '}') return '\n'.join(Evaluations)
def create_docspec(docspec_id, title, tags): title = title.strip() if not title: title = "Docspec %s" % docspec_id title_length = len(title) tags = tags.strip() if not tags: tags = "state:new, priority:normal, untagged" wrapper = TextWrapper(width=80) wrapper.subsequent_indent = ' ' tags = wrapper.fill(tags) return DOCSPEC_TEMPLATE % ('=' * title_length, title, '=' * title_length, NOW.strftime('%Y-%m-%d, %H:%M'), tags)
def dry_run(self): from textwrap import TextWrapper wrapper = TextWrapper() wrapper.subsequent_indent = ' ' wrapper.break_long_words = False wrapper.break_on_hyphens = False wrapper.width = 78 commands = [ '# Create %s' % self._object.full_name(), self.create_cmd(), self._hook.__str__(self._object) ] for command in commands: command.strip() if len(command) > 0: print ' \\\n'.join(wrapper.wrap(command)) print ''
def CppExpressionsAsFunctions(self, Indent=4, Expressions=None): """Define functions to calculate the `Expressions` in C++ The output of this function gives C++ functions to calculate the `Expressions`, assuming the functions are member methods in a class, and so can access the atoms of the expression without explicit arguments. An optional dictionary of expressions allows just a subset of this object's expressions to be output; if this argument is not present, all will be output. """ def dtype(e): if e.datatype: return e.datatype else: return 'double' from textwrap import TextWrapper from PNObjects import PNCollection wrapper = TextWrapper(width=120) wrapper.initial_indent = ' '*Indent + ' return ' wrapper.subsequent_indent = ' '*Indent + ' ' Evaluations = [] if not Expressions: Expressions=self.Expressions for Expression in Expressions: ExprColl = PNCollection() for atom in Expression.substitution_atoms: if atom not in self.Variables: try: ExprColl.AddDerivedVariable(str(atom), atom.substitution, substitution_atoms=atom.substitution_atoms, datatype=atom.datatype) except TypeError: pass MiniConstructor = CodeConstructor(self.Variables, ExprColl) Evaluations.append( ' '*Indent + dtype(Expression) + ' ' + Expressions[Expression] + '() {\n' + MiniConstructor.CppEvaluateExpressions(Indent+2) + '\n' + wrapper.fill(Expression.ccode()) + ';\n' + ' '*Indent + '}' ) return '\n'.join(Evaluations)
def printInd(s, level=1, length=70, prefix=''): from textwrap import TextWrapper indents={1: 0, 2: 4, 3: 8, 4: 12, 5: 16} ind=indents[level] indstr=' '*int(ind) wrapper=TextWrapper() wrapper.width=length wrapper.initial_indent=indstr wrapper.subsequent_indent=indstr string=wrapper.fill('%s %s' %(prefix,s)) try: print('\n'+string) except: print('\n'+string.encode('ascii','replace')) return
def printInd(s, level=1, length=70, prefix=''): from textwrap import TextWrapper indents = {1: 0, 2: 4, 3: 8, 4: 12, 5: 16} ind = indents[level] indstr = ' ' * int(ind) wrapper = TextWrapper() wrapper.width = length wrapper.initial_indent = indstr wrapper.subsequent_indent = indstr string = wrapper.fill('%s %s' % (prefix, s)) try: print('\n' + string) except: print('\n' + string.encode('ascii', 'replace')) return
def format(self, indent=0, width=80, f=sys.stdout, separator=True): """ Formats a block of text and prints it to the output stream. :param int indent: number of spaces to indent the text block :param int width: total text block width :param f: output stream :param dictionary kwargs: supported keyword arguments :param bool separator: if True, there will be a new line appended after the formatted text; default value is True """ if indent > width: return # NOTE: this is wrong! wrapper = TextWrapper() wrapper.width = width - indent wrapper.subsequent_indent = " " * LMITextFormatter.SUB_INDENT for l in wrapper.wrap(self._text): f.write("%s%s\n" % (" " * indent, l)) if separator: f.write("\n")
def printNumHeader(s, idx, num, level=1, length=70, prefix='# <Menotexport>:'): from textwrap import TextWrapper decs = {1: '=', 2: '-', 3: '.'} indents = {1: 0, 2: 4, 3: 8} dec = decs[level] ind = indents[level] indstr = ' ' * int(ind) wrapper = TextWrapper() wrapper.width = length - ind wrapper.initial_indent = indstr wrapper.subsequent_indent = indstr #-------------Get delimiter line------------- decl = int((length - ind - 2 - len(str(idx)) - len(str(num))) / 2.) decl = decl * dec hline1 = '%s%s %d/%d %s' % (' ' * int(ind), decl, idx, num, decl) #hline2='%s%s' %(' '*int(ind),dec*int(length-ind)) #--------------------Wrap texts-------------------- strings = wrapper.wrap('%s %s' % (prefix, s)) #----------------------Print---------------------- try: print('\n' + hline1) except: print('\n' + hline1.encode('ascii', 'replace')) for ss in strings: try: print(ss) except: print(ss.encode('ascii', 'replace')) #print(hline2) return
def CppEvaluations(self, Indent=4): """Evaluate all derived variables in C++ This function uses the `substitution` expressions for the derived variables. This output is appropriate for updating the values of the variables at each step of an integration, for example. """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = ' ' * Indent wrapper.subsequent_indent = wrapper.initial_indent + ' ' def Evaluation(atom): def Ccode(a): try: return a.ccode() except: from sympy.printing import ccode return ccode(a) if atom.datatype and (atom.datatype == 'std::vector<double>' or atom.datatype == 'std::vector<std::complex<double> >'): return '\n'.join([ wrapper.fill('{0}[{1}] = {2};'.format( self.Variables[atom], i, Ccode(atom.substitution[i]))) for i in range(len(atom.substitution)) ]) else: return wrapper.fill('{0} = {1};'.format( self.Variables[atom], atom.ccode())) return '\n'.join([ Evaluation(atom) for atom in self.Atoms if not atom.fundamental and not atom.constant ])
def printNumHeader(s, idx, num, level=1, length=70, prefix='# <Menotexport>:'): from textwrap import TextWrapper decs={1: '=', 2: '-', 3: '.'} indents={1: 0, 2: 4, 3: 8} dec=decs[level] ind=indents[level] indstr=' '*int(ind) wrapper=TextWrapper() wrapper.width=length-ind wrapper.initial_indent=indstr wrapper.subsequent_indent=indstr #-------------Get delimiter line------------- decl=int((length-ind-2-len(str(idx))-len(str(num)))/2.) decl=decl*dec hline1='%s%s %d/%d %s' %(' '*int(ind),decl,idx,num,decl) #hline2='%s%s' %(' '*int(ind),dec*int(length-ind)) #--------------------Wrap texts-------------------- strings=wrapper.wrap('%s %s' %(prefix,s)) #----------------------Print---------------------- try: print('\n'+hline1) except: print('\n'+hline1.encode('ascii','replace')) for ss in strings: try: print(ss) except: print(ss.encode('ascii','replace')) #print(hline2) return
def __init__(self, message, *args): tw = TextWrapper() tw.width = 79 tw.subsequent_indent = '' tw.break_on_hyphens = False # Adds custom error message += '\n\n' message += '*' * 79 + '\n' addenda = ('If you are not sure of how to solve this problem ' 'please copy this error message and email to Jose ' 'Sanchez-Gallego <*****@*****.**> and Drew ' 'Chojnowski <*****@*****.**> and CC Demitri Muna ' '<*****@*****.**> and John Parejko ' '<*****@*****.**>.\n') addenda = '\n'.join(tw.wrap(addenda)) message += addenda + '\n' message += '*' * 79 + '\n' super(PluggingException, self).__init__(message)
def create_docspec(docspec_id, title, tags): title = title.strip() if not title: title = "Docspec %s" % docspec_id title_length = len(title) tags = tags.strip() if not tags: tags = "state:new, priority:normal, untagged" wrapper = TextWrapper(width=80) wrapper.subsequent_indent = ' ' tags = wrapper.fill(tags) return DOCSPEC_TEMPLATE % ( '=' * title_length, title, '=' * title_length, NOW.strftime('%Y-%m-%d, %H:%M'), tags )
def CppEvaluateExpressions(self, Indent=4, Expressions=None): """Declare and define the `Expressions` for C++ The output of this function declares are defines the `Expressions` as individual variables. An optional dictionary of expressions allows just a subset of this object's expressions to be output; if this argument is not present, all will be output. """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = ' '*Indent wrapper.subsequent_indent = wrapper.initial_indent+' ' Evaluations = [] if not Expressions: Expressions=self.Expressions for Expression in Expressions: try: Evaluations.append(wrapper.fill('{0}{1} {2} = {3};'.format(self.const(Expression), self.dtype(Expression), Expressions[Expression], Expression.ccode()))) except TypeError: pass return '\n'.join(Evaluations)
def CppInputArguments(self, Indent=12): """Create basic input arguments for C++ The fundamental variables are listed, along with their data types and `const` if the variable is constant. This would be an appropriate string to represent the input arguments for a function or class constructor to calculate the `Expressions` of this CodeConstructor object. For example, if the `Variables` object contains atoms m1, m2, t, and x referred to in the `Expressions` object, where m1 and m2 are constant, and t and x are variables, the input argument list should be const double m1_i, const double m2_i, double t_i, double x_i """ from textwrap import TextWrapper wrapper = TextWrapper(width=120) wrapper.initial_indent = ' '*Indent wrapper.subsequent_indent = wrapper.initial_indent InputArguments = ['const {0} {1}_i'.format(self.dtype(atom), self.Variables[atom]) for atom in self.Atoms if atom.fundamental] return wrapper.fill(', '.join(InputArguments)).lstrip()
def process(self, molecules, args): """Process the SVD of molecules.""" # Setup the configuration options if 'project_methyls' in args and args.project_methyls: settings.project_methyls = True if 'methyl_scale' in args and args.methyl_scale is not None: settings.methyl_order_parameter = args.methyl_scale if 'fix_sign' in args and args.fix_sign: settings.enable_signfixer = True if 'nofix_sign' in args and args.nofix_sign: settings.enable_signfixer = False if 'fix_nh_scale' in args and args.fix_nh_scale: settings.enable_nhscalefixer = True if 'nofix_nh_scale' in args and args.nofix_nh_scale: settings.enable_nhscalefixer = False if 'fix_outliers' in args and args.fix_outliers: settings.enable_outlierfixer = True if 'nofix_outliers' in args and args.nofix_outliers: settings.enable_outlierfixer = False # If specified, get the identifier for the dataset to use. set_id = args.set if 'set' in args else None # Process the partial alignment calculation if args.command == 'pa': # Get the alignment data data = {} for data_filename in args.data[0]: # verify that the file exists file_path = get_or_fetch(data_filename, extensions='mr.gz', urls=settings.mr_urls, critical=True) # Read the data from the file. data_dict = read_pa_file(file_path, set_id) data.update(data_dict) # If excluded interactions are specified, remove these. if args.exclude: data = {k:v for k, v in data.items() if interaction_type(k) not in args.exclude} # verify that there is data in the data dict msg = "Could not find data in alignment data." check_not_empty(data=data, msg=msg, critical=True) # Prepare the magnetic interactions for the molecules labels = data.keys() process = Process(molecules) magnetic_interactions = process.process(labels=labels) # Apply the fixers to see if the input data can be improved fixer = Fixer(molecules) data_fixed, fixes = fixer.fix(data) data = data_fixed if data_fixed is not None else data # Conduct the SVD on the data (data_pred, Saupe_components, stats) = calc_pa_SVD(magnetic_interactions, data) # Prepare table of stats and fit values table = stats_table(stats) # Prepare a table of the observed and predicted data tables = report_tables(data, data_pred) if len(molecules) > 1: # Make title for stats table title = "Summary SVD Statistics for Molecules " title += word_list([m.fullname for m in molecules]) table.title = title # Make title for the fit data table title = "Observed and Predicted RDCs and RACS for Molecules " title += word_list([m.fullname for m in molecules]) tables['fit'].title = title # Make title for the back-calculated predicted data title = "Back-calculated RDCs and RACS for Molecules " title += word_list([m.fullname for m in molecules]) tables['pred'].title = title else: # Make title for stats table title = "Summary SVD Statistics for Molecule " title += molecules[0].fullname table.title = title # Make title for the fit data table title = "Observed and Predicted RDCs and RACS for Molecule " title += molecules[0].fullname tables['fit'].title = title # Make title for the back-calculated predicted data title = "Back-calculated RDCs and RACS for Molecule " title += molecules[0].fullname tables['pred'].title = title # Prepare the standard output summary = table.content() output = tables['fit'].content() # Prepare and format the fixes listing if fixes: # Setup the text wrapper so that the lines of fixes do not # exceed the set maximum number of columns. wrapper = TextWrapper() wrapper.initial_indent = '* ' wrapper.subsequent_indent = ' ' wrapper.width = utils_settings.default_max_width fixes_wrapped = ['\n'.join(wrapper.wrap(fix)) for fix in fixes] fixes_output = '\n'.join(fixes_wrapped) else: fixes_output = '' # Print or write the report(s) print(summary) if args.out: output += fixes_output write_file('\n'.join((summary, output)), args.out) elif not args.summary: print(output) if fixes: print(fixes_output) # Write the predicted data if args.pred: write_file(tables['pred'].content(), args.pred)
def _exportAnnoFile(abpath_out,anno,verbose=True): '''Export annotations in a single PDF <abpath_out>: str, absolute path to output txt file. <anno>: list, in the form [highlight_list, note_list]. highlight_list and note_list are both lists of Anno objs (see extracthl.py), containing highlights and notes in TEXT format with metadata. To be distinguished with FileAnno objs which contains texts coordinates. if highlight_list or note_list is [], no such info in this PDF. Function takes annotations from <anno> and output to the target txt file in the following format: ----------------------------------------------------- # Title of PDF > Highlighted text line 1 Highlighted text line 2 Highlighted text line 3 ... - @citationkey - Tags: @tag1, @tag2, @tag3... - Ctime: creation time ----------------------------------------------------- # Title of another PDF > Highlighted text line 1 Highlighted text line 2 Highlighted text line 3 ... - @citationkey - Tags: @tag1, @tag2, @tag3... - Ctime: creation time Use tabs in indention, and markup syntax: ">" for highlights, and "-" for notes. Update time: 2016-02-24 13:59:56. ''' conv=lambda x:unicode(x) wrapper=TextWrapper() wrapper.width=80 wrapper.initial_indent='' wrapper.subsequent_indent='\t'+int(len('> '))*' ' wrapper2=TextWrapper() wrapper2.width=80-7 wrapper2.initial_indent='' wrapper2.subsequent_indent='\t\t'+int(len('- Tags: '))*' ' hlii,ntii=anno try: titleii=hlii[0].title except: titleii=ntii[0].title outstr=u'\n\n{0}\n# {1}'.format(int(80)*'-',conv(titleii)) with open(abpath_out, mode='a') as fout: outstr=outstr.encode('ascii','replace') fout.write(outstr) #-----------------Write highlights----------------- if len(hlii)>0: if verbose: print('\n# <mennoteexport>: Exporting highlights in:') print(titleii) #-------------Loop through highlights------------- for hljj in hlii: hlstr=wrapper.fill(hljj.text) tagstr=', '.join(['@'+kk for kk in hljj.tags]) tagstr=wrapper2.fill(tagstr) outstr=u''' \n\t> {0} \t\t- @{1} \t\t- Tags: {2} \t\t- Ctime: {3} '''.format(*map(conv,[hlstr, hljj.citationkey,\ tagstr, hljj.ctime])) outstr=outstr.encode('ascii','replace') fout.write(outstr) #-----------------Write notes----------------- if len(ntii)>0: if verbose: print('\n# <mennoteexport>: Exporting notes in:') print(titleii) #----------------Loop through notes---------------- for ntjj in ntii: ntstr=wrapper.fill(ntjj.text) tagstr=', '.join(['@'+kk for kk in ntjj.tags]) tagstr=wrapper2.fill(tagstr) outstr=u''' \n\t- {0} \t\t- @{1} \t\t\t- Tags: {2} \t\t\t- Ctime: {3} '''.format(*map(conv,[ntstr, ntjj.citationkey,\ tagstr, ntjj.ctime])) outstr=outstr.encode('ascii','replace') fout.write(outstr)
def wrap(text, column=70, honor_leading_ws=True): """Wrap and fill the text to the specified column. The input text is wrapped and filled as done by the standard library textwrap module. The differences here being that this function is capable of filling multiple paragraphs (as defined by text separated by blank lines). Also, when `honor_leading_ws` is True (the default), paragraphs that being with whitespace are not wrapped. This is the algorithm that the Python FAQ wizard used. """ # First, split the original text into paragraph, keeping all blank lines # between them. paragraphs = [] paragraph = [] last_indented = False for line in text.splitlines(True): is_indented = (len(line) > 0 and line[0] in whitespace) if line == NL: if len(paragraph) > 0: paragraphs.append(EMPTYSTRING.join(paragraph)) paragraphs.append(line) last_indented = False paragraph = [] elif last_indented != is_indented: # The indentation level changed. We treat this as a paragraph # break but no blank line will be issued between paragraphs. if len(paragraph) > 0: paragraphs.append(EMPTYSTRING.join(paragraph)) # The next paragraph starts with this line. paragraph = [line] last_indented = is_indented else: # This line does not constitute a paragraph break. paragraph.append(line) # We've consumed all the lines in the original text. Transfer the last # paragraph we were collecting to the full set of paragraphs. paragraphs.append(EMPTYSTRING.join(paragraph)) # Now iterate through all paragraphs, wrapping as necessary. wrapped_paragraphs = [] # The dedented wrapper. wrapper = TextWrapper(width=column, fix_sentence_endings=True) # The indented wrapper. For this one, we'll clobber initial_indent and # subsequent_indent as needed per indented chunk of text. iwrapper = TextWrapper(width=column, fix_sentence_endings=True, ) add_paragraph_break = False for paragraph in paragraphs: if add_paragraph_break: wrapped_paragraphs.append(NL) add_paragraph_break = False paragraph_text = EMPTYSTRING.join(paragraph) # Just copy the blank lines to the final set of paragraphs. if len(paragraph) == 0 or paragraph == NL: wrapped_paragraphs.append(NL) # Choose the wrapper based on whether the paragraph is indented or # not. Also, do not wrap indented paragraphs if honor_leading_ws is # set. elif paragraph[0] in whitespace: if honor_leading_ws: # Leave the indented paragraph verbatim. wrapped_paragraphs.append(paragraph_text) else: # The paragraph should be wrapped, but it must first be # dedented. The leading whitespace on the first line of the # original text will be used as the indentation for all lines # in the wrapped text. for i, ch in enumerate(paragraph_text): if ch not in whitespace: break leading_ws = paragraph[:i] iwrapper.initial_indent=leading_ws iwrapper.subsequent_indent=leading_ws paragraph_text = dedent(paragraph_text) wrapped_paragraphs.append(iwrapper.fill(paragraph_text)) add_paragraph_break = True else: # Fill this paragraph. fill() consumes the trailing newline. wrapped_paragraphs.append(wrapper.fill(paragraph_text)) add_paragraph_break = True return EMPTYSTRING.join(wrapped_paragraphs)
def exportAnno(annodict,outdir,action,verbose=True): '''Export annotations grouped by tags ''' #-----------Export all to a single file----------- if 'm' in action and 'n' not in action: fileout='Mendeley_highlights_by_tags.txt' elif 'n' in action and 'm' not in action: fileout='Mendeley_notes_by_tags.txt' elif 'm' in action and 'n' in action: fileout='Mendeley_annotations_by_tags.txt' abpath_out=os.path.join(outdir,fileout) if os.path.isfile(abpath_out): os.remove(abpath_out) if verbose: printHeader('Exporting all taged annotations to:',3) printInd(abpath_out,4) conv=lambda x:unicode(x) wrapper=TextWrapper() wrapper.width=70 wrapper.initial_indent='' #wrapper.subsequent_indent='\t\t'+int(len('> '))*' ' wrapper.subsequent_indent='\t\t' wrapper2=TextWrapper() wrapper2.width=60 wrapper2.initial_indent='' #wrapper2.subsequent_indent='\t\t\t'+int(len('Title: '))*' ' wrapper2.subsequent_indent='\t\t\t' with open(abpath_out, mode='a') as fout: #----------------Loop through tags---------------- tags=annodict.keys() if len(tags)==0: return tags.sort() #---------------Put @None at the end--------------- if '@None' in tags: tags.remove('@None') tags.append('@None') for tagii in tags: citedictii=annodict[tagii] outstr=u'''\n\n{0}\n# {1}'''.format(int(80)*'-', conv(tagii)) outstr=outstr.encode('ascii','replace') fout.write(outstr) #--------------Loop through cite keys-------------- for citejj, annosjj in citedictii.items(): hljj=annosjj['highlights'] ntjj=annosjj['notes'] outstr=u'''\n\n\t@{0}:'''.format(conv(citejj)) outstr=outstr.encode('ascii','replace') fout.write(outstr) #-----------------Write highlights----------------- if len(hljj)>0: #-------------Loop through highlights------------- for hlkk in hljj: hlstr=wrapper.fill(hlkk.text) title=wrapper2.fill(hlkk.title) outstr=u''' \n\t\t> {0} \t\t\t- Title: {1} \t\t\t- Ctime: {2}'''.format(*map(conv,[hlstr, title,\ hlkk.ctime])) outstr=outstr.encode('ascii','replace') fout.write(outstr) #-----------------Write notes----------------- if len(ntjj)>0: #----------------Loop through notes---------------- for ntkk in ntjj: ntstr=wrapper.fill(ntkk.text) title=wrapper2.fill(ntkk.title) outstr=u''' \n\t\t- {0} \t\t\t- Title: {1} \t\t\t- Ctime: {2}'''.format(*map(conv,[ntstr, title,\ ntkk.ctime])) outstr=outstr.encode('ascii','replace') fout.write(outstr)
def __str__(self): return str(self._config) config = Config(defaults) if len(sys.argv) != 2: textWrapper = TextWrapper(initial_indent = " ", width = 78) print >> sys.stderr, """Usage: %s <config file> The config file is a python script setting some or all of the following variables: """ % sys.argv[0] for k, v in sorted(defaults.items()): print >> sys.stderr, " " + k + ":" o = v[1].find("-") if o < 0: textWrapper.subsequent_indent = " " else: textWrapper.subsequent_indent = " " * (6 + o) print >> sys.stderr, "\n".join(textWrapper.wrap(v[1])) if v[0] is not None: print >> sys.stderr, " Default:", repr(v[0]) sys.exit() else: config(sys.argv[1]) xml, chan = create_rss_channel(config) # walk through all files and subfolders for path, subFolders, files in os.walk(config.source): for f in files: # We use the extension to work out the media type
def help(self,message,*args,**kwargs): """docstring for help""" tw = TextWrapper() tw.subsequent_indent = ' ' self.log._log(logging.HELP,tw.fill(message),args,**kwargs)