Skip to content

Commit

Permalink
Deploying to gh-pages from main @ c78b89b 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
rugeli committed Mar 21, 2024
1 parent d2763a6 commit c999dca
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 27 deletions.
1 change: 1 addition & 0 deletions _modules/cellpack/autopack.html
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ <h1>Source code for cellpack.autopack</h1><div class="highlight"><pre>
<span class="k">if</span> <span class="n">database_name</span> <span class="o">==</span> <span class="s2">&quot;firebase&quot;</span><span class="p">:</span>
<span class="n">db</span> <span class="o">=</span> <span class="n">DATABASE_IDS</span><span class="o">.</span><span class="n">handlers</span><span class="p">()</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">database_name</span><span class="p">)</span>
<span class="n">db_handler</span> <span class="o">=</span> <span class="n">DBRecipeLoader</span><span class="p">(</span><span class="n">db</span><span class="p">)</span>
<span class="n">db_handler</span><span class="o">.</span><span class="n">validate_input_recipe_path</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
<span class="n">recipe_id</span> <span class="o">=</span> <span class="n">file_path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">db_doc</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">db_handler</span><span class="o">.</span><span class="n">collect_docs_by_id</span><span class="p">(</span>
<span class="n">collection</span><span class="o">=</span><span class="s2">&quot;recipes&quot;</span><span class="p">,</span> <span class="nb">id</span><span class="o">=</span><span class="n">recipe_id</span>
Expand Down
51 changes: 39 additions & 12 deletions _modules/cellpack/autopack/AWSHandler.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ <h3>Navigation</h3>
<h1>Source code for cellpack.autopack.AWSHandler</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
<span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">parse_qs</span><span class="p">,</span> <span class="n">urlparse</span><span class="p">,</span> <span class="n">urlunparse</span>

<span class="kn">import</span> <span class="nn">boto3</span>
<span class="kn">from</span> <span class="nn">botocore.exceptions</span> <span class="kn">import</span> <span class="n">ClientError</span>
Expand Down Expand Up @@ -84,7 +85,7 @@ <h1>Source code for cellpack.autopack.AWSHandler</h1><div class="highlight"><pre
<a class="viewcode-back" href="../../../cellpack.autopack.html#cellpack.autopack.AWSHandler.AWSHandler.get_aws_object_key">[docs]</a>
<span class="k">def</span> <span class="nf">get_aws_object_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">object_name</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">folder_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">object_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">folder_name</span> <span class="o">+</span> <span class="n">object_name</span>
<span class="n">object_name</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">folder_name</span><span class="si">}</span><span class="s2">/</span><span class="si">{</span><span class="n">object_name</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">object_name</span> <span class="o">=</span> <span class="n">object_name</span>
<span class="k">return</span> <span class="n">object_name</span></div>
Expand Down Expand Up @@ -126,29 +127,55 @@ <h1>Source code for cellpack.autopack.AWSHandler</h1><div class="highlight"><pre
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">object_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_aws_object_key</span><span class="p">(</span><span class="n">object_name</span><span class="p">)</span>
<span class="c1"># Generate a presigned URL for the S3 object</span>
<span class="c1"># The response contains the presigned URL</span>
<span class="c1"># https://{self.bucket_name}.s3.{region}.amazonaws.com/{object_key}</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">url</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">s3_client</span><span class="o">.</span><span class="n">generate_presigned_url</span><span class="p">(</span>
<span class="s2">&quot;get_object&quot;</span><span class="p">,</span>
<span class="n">Params</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;Bucket&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">bucket_name</span><span class="p">,</span> <span class="s2">&quot;Key&quot;</span><span class="p">:</span> <span class="n">object_name</span><span class="p">},</span>
<span class="n">ExpiresIn</span><span class="o">=</span><span class="n">expiration</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">base_url</span> <span class="o">=</span> <span class="n">urlunparse</span><span class="p">(</span><span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="o">.</span><span class="n">_replace</span><span class="p">(</span><span class="n">query</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">fragment</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">))</span>
<span class="k">return</span> <span class="n">base_url</span>
<span class="k">except</span> <span class="n">ClientError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="n">logging</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="c1"># The response contains the presigned URL</span>
<span class="c1"># https://{self.bucket_name}.s3.{region}.amazonaws.com/{object_key}</span>
<span class="k">return</span> <span class="n">url</span></div>
<span class="n">logging</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Error generating presigned URL: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">return</span> <span class="kc">None</span></div>


<div class="viewcode-block" id="AWSHandler.save_file">
<a class="viewcode-back" href="../../../cellpack.autopack.html#cellpack.autopack.AWSHandler.AWSHandler.save_file">[docs]</a>
<span class="k">def</span> <span class="nf">save_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_path</span><span class="p">):</span>
<div class="viewcode-block" id="AWSHandler.is_url_valid">
<a class="viewcode-back" href="../../../cellpack.autopack.html#cellpack.autopack.AWSHandler.AWSHandler.is_url_valid">[docs]</a>
<span class="k">def</span> <span class="nf">is_url_valid</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Validate the url&#39;s scheme, bucket name, and query parameters, etc.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">parsed_url</span> <span class="o">=</span> <span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="c1"># Check the scheme</span>
<span class="k">if</span> <span class="n">parsed_url</span><span class="o">.</span><span class="n">scheme</span> <span class="o">!=</span> <span class="s2">&quot;https&quot;</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="c1"># Check the bucket name</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">parsed_url</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;/</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">bucket_name</span><span class="si">}</span><span class="s2">/&quot;</span><span class="p">):</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="c1"># Check unwanted query parameters</span>
<span class="n">unwanted_query_params</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;AWSAccessKeyId&quot;</span><span class="p">,</span> <span class="s2">&quot;Signature&quot;</span><span class="p">,</span> <span class="s2">&quot;Expires&quot;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">parsed_url</span><span class="o">.</span><span class="n">query</span><span class="p">:</span>
<span class="n">query_params</span> <span class="o">=</span> <span class="n">parse_qs</span><span class="p">(</span><span class="n">parsed_url</span><span class="o">.</span><span class="n">query</span><span class="p">)</span>
<span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">unwanted_query_params</span><span class="p">:</span>
<span class="k">if</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">query_params</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="k">return</span> <span class="kc">True</span></div>


<div class="viewcode-block" id="AWSHandler.save_file_and_get_url">
<a class="viewcode-back" href="../../../cellpack.autopack.html#cellpack.autopack.AWSHandler.AWSHandler.save_file_and_get_url">[docs]</a>
<span class="k">def</span> <span class="nf">save_file_and_get_url</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_path</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Uploads a file to S3 and returns the presigned url</span>
<span class="sd"> Uploads a file to S3 and returns the base url</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">file_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">upload_file</span><span class="p">(</span><span class="n">file_path</span><span class="p">)</span>
<span class="k">if</span> <span class="n">file_name</span><span class="p">:</span>
<span class="k">return</span> <span class="n">file_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_presigned_url</span><span class="p">(</span><span class="n">file_name</span><span class="p">)</span></div>
<span class="n">base_url</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_presigned_url</span><span class="p">(</span><span class="n">file_name</span><span class="p">)</span>
<span class="k">if</span> <span class="n">file_name</span> <span class="ow">and</span> <span class="n">base_url</span><span class="p">:</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_url_valid</span><span class="p">(</span><span class="n">base_url</span><span class="p">):</span>
<span class="k">return</span> <span class="n">file_name</span><span class="p">,</span> <span class="n">base_url</span></div>
</div>

</pre></div>
Expand Down
18 changes: 17 additions & 1 deletion _modules/cellpack/autopack/DBRecipeHandler.html
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ <h1>Source code for cellpack.autopack.DBRecipeHandler</h1><div class="highlight"
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">recipe_id</span><span class="si">}</span><span class="s2"> is already in firestore&quot;</span><span class="p">)</span>
<span class="k">return</span>
<span class="n">recipe_to_save</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">upload_collections</span><span class="p">(</span><span class="n">recipe_meta_data</span><span class="p">,</span> <span class="n">recipe_data</span><span class="p">)</span>
<span class="n">recipe_to_save</span><span class="p">[</span><span class="s2">&quot;recipe_path&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">create_path</span><span class="p">(</span><span class="s2">&quot;recipes&quot;</span><span class="p">,</span> <span class="n">recipe_id</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">upload_data</span><span class="p">(</span><span class="s2">&quot;recipes&quot;</span><span class="p">,</span> <span class="n">recipe_to_save</span><span class="p">,</span> <span class="n">recipe_id</span><span class="p">)</span></div>


Expand All @@ -731,7 +732,7 @@ <h1>Source code for cellpack.autopack.DBRecipeHandler</h1><div class="highlight"
<span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">update_or_create</span><span class="p">(</span>
<span class="s2">&quot;results&quot;</span><span class="p">,</span>
<span class="n">file_name</span><span class="p">,</span>
<span class="p">{</span><span class="s2">&quot;user&quot;</span><span class="p">:</span> <span class="n">username</span><span class="p">,</span> <span class="s2">&quot;timestamp&quot;</span><span class="p">:</span> <span class="n">timestamp</span><span class="p">,</span> <span class="s2">&quot;url&quot;</span><span class="p">:</span> <span class="n">url</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;?&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]},</span>
<span class="p">{</span><span class="s2">&quot;user&quot;</span><span class="p">:</span> <span class="n">username</span><span class="p">,</span> <span class="s2">&quot;timestamp&quot;</span><span class="p">:</span> <span class="n">timestamp</span><span class="p">,</span> <span class="s2">&quot;url&quot;</span><span class="p">:</span> <span class="n">url</span><span class="p">},</span>
<span class="p">)</span></div>
</div>

Expand Down Expand Up @@ -787,6 +788,21 @@ <h1>Source code for cellpack.autopack.DBRecipeHandler</h1><div class="highlight"
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">get_doc_by_id</span><span class="p">(</span><span class="n">collection</span><span class="p">,</span> <span class="nb">id</span><span class="p">)</span></div>


<div class="viewcode-block" id="DBRecipeLoader.validate_input_recipe_path">
<a class="viewcode-back" href="../../../cellpack.autopack.html#cellpack.autopack.DBRecipeHandler.DBRecipeLoader.validate_input_recipe_path">[docs]</a>
<span class="k">def</span> <span class="nf">validate_input_recipe_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Validates if the input path corresponds to a recipe path in the database.</span>
<span class="sd"> Format of a recipe path: firebase:recipes/[RECIPE-ID]</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">collection</span><span class="p">,</span> <span class="nb">id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">get_collection_id_from_path</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="n">recipe_path</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">get_value</span><span class="p">(</span><span class="n">collection</span><span class="p">,</span> <span class="nb">id</span><span class="p">,</span> <span class="s2">&quot;recipe_path&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">recipe_path</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
<span class="sa">f</span><span class="s2">&quot;No recipe found at the input path: &#39;</span><span class="si">{</span><span class="n">path</span><span class="si">}</span><span class="s2">&#39;. Please ensure the recipe exists in the database and is spelled correctly. Expected path format: &#39;firebase:recipes/[RECIPE-ID]&#39;&quot;</span>
<span class="p">)</span></div>


<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">_get_grad_and_obj</span><span class="p">(</span><span class="n">obj_data</span><span class="p">,</span> <span class="n">obj_dict</span><span class="p">,</span> <span class="n">grad_dict</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
Expand Down
Loading

0 comments on commit c999dca

Please sign in to comment.