|
1 | 1 | """Base classes and common methods for the analyzer package.""" |
2 | | - |
| 2 | +import pprint |
3 | 3 |
|
4 | 4 |
|
5 | 5 | from datetime import datetime |
| 6 | +import re |
| 7 | + |
6 | 8 |
|
| 9 | +def is_ip(test): |
| 10 | + """Test to see if a string contains an IPv4 address.""" |
| 11 | + pattern = re.compile(r"(\d{1,3}(?:\.|\]\.\[|\[\.\]|\(\.\)|{\.})\d{1,3}(?:\.|\]\.\[|\[\.\]|\(\.\)|{\.})\d{1,3}(?:\.|\]\.\[|\[\.\]|\(\.\)|{\.})\d{1,3})") |
| 12 | + return len(pattern.findall(test)) > 0 |
7 | 13 |
|
| 14 | +def refang(hostname): |
| 15 | + """Remove square braces around dots in a hostname.""" |
| 16 | + return re.sub(r'[\[\]]','', hostname) |
8 | 17 |
|
9 | 18 | class RecordList: |
10 | 19 |
|
@@ -221,3 +230,40 @@ def has_more_records(self): |
221 | 230 | """ |
222 | 231 | return len(self) < self._totalrecords |
223 | 232 |
|
| 233 | + |
| 234 | +class PrettyRecord: |
| 235 | + """A record that can pretty-print itself. |
| 236 | +
|
| 237 | + For best results, wrap this property in a print() statement. |
| 238 | +
|
| 239 | + Depends on a as_dict property on the base object. |
| 240 | + """ |
| 241 | + |
| 242 | + @property |
| 243 | + def pretty(self): |
| 244 | + """Pretty printed version of this record.""" |
| 245 | + from passivetotal.analyzer import get_config |
| 246 | + config = get_config('pprint') |
| 247 | + return pprint.pformat(self.as_dict, **config) |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | +class PrettyList: |
| 252 | + """A record list that can pretty-print itself. |
| 253 | +
|
| 254 | + Depends on an as_dict property each object in the list. |
| 255 | + """ |
| 256 | + |
| 257 | + @property |
| 258 | + def pretty(self): |
| 259 | + """Pretty printed version of this record list.""" |
| 260 | + from passivetotal.analyzer import get_config |
| 261 | + config = get_config('pprint') |
| 262 | + return pprint.pformat([r.as_dict for r in self], **config) |
| 263 | + |
| 264 | + |
| 265 | + |
| 266 | + |
| 267 | +class AnalyzerError(Exception): |
| 268 | + """Base error class for Analyzer objects.""" |
| 269 | + pass |
0 commit comments