String derivative with a special access attributes. These are normal strings, but with the special attributes: .l (or .list) : value as list (split on newlines). .n (or .nlstr): original value (the string itself). .s (or .spstr): value as whitespace-separated string.
| 24 | date_format = "%B %-d, %Y" |
| 25 | |
| 26 | class LSString(str): |
| 27 | """String derivative with a special access attributes. |
| 28 | |
| 29 | These are normal strings, but with the special attributes: |
| 30 | |
| 31 | .l (or .list) : value as list (split on newlines). |
| 32 | .n (or .nlstr): original value (the string itself). |
| 33 | .s (or .spstr): value as whitespace-separated string. |
| 34 | .p (or .paths): list of path objects (requires path.py package) |
| 35 | |
| 36 | Any values which require transformations are computed only once and |
| 37 | cached. |
| 38 | |
| 39 | Such strings are very useful to efficiently interact with the shell, which |
| 40 | typically only understands whitespace-separated options for commands.""" |
| 41 | |
| 42 | def get_list(self): |
| 43 | try: |
| 44 | return self.__list |
| 45 | except AttributeError: |
| 46 | self.__list = self.split('\n') |
| 47 | return self.__list |
| 48 | |
| 49 | l = list = property(get_list) |
| 50 | |
| 51 | def get_spstr(self): |
| 52 | try: |
| 53 | return self.__spstr |
| 54 | except AttributeError: |
| 55 | self.__spstr = self.replace('\n',' ') |
| 56 | return self.__spstr |
| 57 | |
| 58 | s = spstr = property(get_spstr) |
| 59 | |
| 60 | def get_nlstr(self): |
| 61 | return self |
| 62 | |
| 63 | n = nlstr = property(get_nlstr) |
| 64 | |
| 65 | def get_paths(self): |
| 66 | try: |
| 67 | return self.__paths |
| 68 | except AttributeError: |
| 69 | self.__paths = [Path(p) for p in self.split('\n') if os.path.exists(p)] |
| 70 | return self.__paths |
| 71 | |
| 72 | p = paths = property(get_paths) |
| 73 | |
| 74 | # FIXME: We need to reimplement type specific displayhook and then add this |
| 75 | # back as a custom printer. This should also be moved outside utils into the |