Skip to content

Commit

Permalink
Update docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Apr 16, 2022
1 parent c9c7dc5 commit 2b74cae
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 11 deletions.
21 changes: 11 additions & 10 deletions analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
LOGGER.addHandler(hdlr=handler)


def _custom_time(*args: logging.Formatter or time.time) -> time.struct_time:
def custom_time(*args: logging.Formatter or time.time) -> time.struct_time:
"""Creates custom timezone for ``logging`` which gets used only when invoked by ``Docker``.
This is used only when triggered within a ``docker container`` as it uses UTC timezone.
Expand All @@ -44,7 +44,7 @@ def _custom_time(*args: logging.Formatter or time.time) -> time.struct_time:
return datetime.now().astimezone(tz=local_timezone).timetuple()


def _extract_str(input_: AnyStr) -> str:
def extract_str(input_: AnyStr) -> str:
"""Extracts strings from the received input.
Args:
Expand All @@ -57,15 +57,16 @@ def _extract_str(input_: AnyStr) -> str:
return "".join([i for i in input_ if not i.isdigit() and i not in [",", ".", "?", "-", ";", "!", ":"]]).strip()


def _device_name():
def device_name() -> str:
"""Gets the device name for MacOS and Windows."""
if platform.system() == 'Darwin':
system_kernel = subprocess.check_output("sysctl hw.model", shell=True).decode('utf-8').splitlines()
return _extract_str(system_kernel[0].split(':')[1])
return extract_str(system_kernel[0].split(':')[1])
elif platform.system() == 'Windows':
return subprocess.getoutput("WMIC CSPRODUCT GET VENDOR").replace('Vendor', '').strip()


def _get_ssid() -> Union[str, None]:
def get_ssid() -> Union[str, None]:
"""Checks the current operating system and runs the appropriate command to get the SSID of the access point.
Returns:
Expand All @@ -79,7 +80,7 @@ def _get_ssid() -> Union[str, None]:
)
out, err = process.communicate()
if out.decode(encoding='UTF-8').strip() == "AirPort: Off":
LOGGER.warning(f"{_device_name()} WiFi is turned off.")
LOGGER.warning(f"{device_name()} WiFi is turned off.")
return
if error := process.returncode:
LOGGER.error(f"Failed to fetch SSID with exit code: {error}\n{err}")
Expand All @@ -94,7 +95,7 @@ def _get_ssid() -> Union[str, None]:
return info.strip('SSID').replace('SSID', '').replace(':', '').strip()


def _send_sms(msg: str) -> NoReturn:
def send_sms(msg: str) -> NoReturn:
"""Sens an SMS notification when invoked by the ``run`` method.
Args:
Expand Down Expand Up @@ -130,7 +131,7 @@ def __init__(self, router_pass: str = None):
raise ValueError(
'Router password is required.'
)
self.ssid = _get_ssid() or 'your Network.'
self.ssid = get_ssid() or 'your Network.'
self.snapshot = 'snapshot.json'
self.blocked = 'blocked.yaml'
self.netgear = Netgear(password=router_pass)
Expand Down Expand Up @@ -311,12 +312,12 @@ def run(self, block: bool = False) -> NoReturn:
LOGGER.info(f'{device.name} does not have internet access.')

if threat:
_send_sms(msg=threat)
send_sms(msg=threat)
else:
LOGGER.info(f'NetScan has completed. No threats found on {self.ssid}')


if __name__ == '__main__':
if os.environ.get('DOCKER'):
logging.Formatter.converter = _custom_time
logging.Formatter.converter = custom_time
LocalIPScan().run()
Binary file modified doc_generator/_build/doctrees/README.doctree
Binary file not shown.
Binary file modified doc_generator/_build/doctrees/environment.pickle
Binary file not shown.
Binary file modified doc_generator/_build/doctrees/index.doctree
Binary file not shown.
40 changes: 40 additions & 0 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ <h1 id="index">Index</h1>
<a href="#A"><strong>A</strong></a>
| <a href="#B"><strong>B</strong></a>
| <a href="#C"><strong>C</strong></a>
| <a href="#D"><strong>D</strong></a>
| <a href="#E"><strong>E</strong></a>
| <a href="#G"><strong>G</strong></a>
| <a href="#L"><strong>L</strong></a>
| <a href="#M"><strong>M</strong></a>
| <a href="#R"><strong>R</strong></a>
| <a href="#S"><strong>S</strong></a>

</div>
<h2 id="A">A</h2>
Expand Down Expand Up @@ -79,6 +83,34 @@ <h2 id="C">C</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#analyzer.LocalIPScan.create_snapshot">create_snapshot() (analyzer.LocalIPScan method)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#analyzer.custom_time">custom_time() (in module analyzer)</a>
</li>
</ul></td>
</tr></table>

<h2 id="D">D</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#analyzer.device_name">device_name() (in module analyzer)</a>
</li>
</ul></td>
</tr></table>

<h2 id="E">E</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#analyzer.extract_str">extract_str() (in module analyzer)</a>
</li>
</ul></td>
</tr></table>

<h2 id="G">G</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#analyzer.get_ssid">get_ssid() (in module analyzer)</a>
</li>
</ul></td>
</tr></table>
Expand Down Expand Up @@ -112,6 +144,14 @@ <h2 id="R">R</h2>
</ul></td>
</tr></table>

<h2 id="S">S</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#analyzer.send_sms">send_sms() (in module analyzer)</a>
</li>
</ul></td>
</tr></table>



<div class="clearer"></div>
Expand Down
67 changes: 67 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,73 @@ <h1>Welcome to NetScan’s documentation!<a class="headerlink" href="#welcome-to

</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="analyzer.custom_time">
<span class="sig-prename descclassname"><span class="pre">analyzer.</span></span><span class="sig-name descname"><span class="pre">custom_time</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">logging.Formatter</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">time.struct_time</span></span></span><a class="headerlink" href="#analyzer.custom_time" title="Permalink to this definition"></a></dt>
<dd><p>Creates custom timezone for <code class="docutils literal notranslate"><span class="pre">logging</span></code> which gets used only when invoked by <code class="docutils literal notranslate"><span class="pre">Docker</span></code>.</p>
<p>This is used only when triggered within a <code class="docutils literal notranslate"><span class="pre">docker</span> <span class="pre">container</span></code> as it uses UTC timezone.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>*args</strong> – Takes <code class="docutils literal notranslate"><span class="pre">Formatter</span></code> object and current epoch time as arguments passed by <code class="docutils literal notranslate"><span class="pre">formatTime</span></code> from <code class="docutils literal notranslate"><span class="pre">logging</span></code>.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>A struct_time object which is a tuple of:
<strong>current year, month, day, hour, minute, second, weekday, year day and dst</strong> <em>(Daylight Saving Time)</em></p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>struct_time</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="analyzer.device_name">
<span class="sig-prename descclassname"><span class="pre">analyzer.</span></span><span class="sig-name descname"><span class="pre">device_name</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">str</span></span></span><a class="headerlink" href="#analyzer.device_name" title="Permalink to this definition"></a></dt>
<dd><p>Gets the device name for MacOS and Windows.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="analyzer.extract_str">
<span class="sig-prename descclassname"><span class="pre">analyzer.</span></span><span class="sig-name descname"><span class="pre">extract_str</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">input_</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">AnyStr</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">str</span></span></span><a class="headerlink" href="#analyzer.extract_str" title="Permalink to this definition"></a></dt>
<dd><p>Extracts strings from the received input.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>input_</strong> – Takes a string as argument.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>A string after removing special characters.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>str</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="analyzer.get_ssid">
<span class="sig-prename descclassname"><span class="pre">analyzer.</span></span><span class="sig-name descname"><span class="pre">get_ssid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">Optional</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">]</span></span></span></span><a class="headerlink" href="#analyzer.get_ssid" title="Permalink to this definition"></a></dt>
<dd><p>Checks the current operating system and runs the appropriate command to get the SSID of the access point.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>SSID of the access point/router which is being accessed.</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>str</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="analyzer.send_sms">
<span class="sig-prename descclassname"><span class="pre">analyzer.</span></span><span class="sig-name descname"><span class="pre">send_sms</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span><span class="p"><span class="pre">:</span></span> <span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">NoReturn</span></span></span><a class="headerlink" href="#analyzer.send_sms" title="Permalink to this definition"></a></dt>
<dd><p>Sens an SMS notification when invoked by the <code class="docutils literal notranslate"><span class="pre">run</span></code> method.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>msg</strong> – Message that has to be sent.</p>
</dd>
</dl>
</dd></dl>

</section>
<section id="indices-and-tables">
<h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline"></a></h1>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2b74cae

Please sign in to comment.