Skip to content

Commit

Permalink
Merge pull request #32 from Oxen-AI/feat/config_user
Browse files Browse the repository at this point in the history
Feat/config user
  • Loading branch information
gschoeni authored Oct 9, 2023
2 parents 0f4dd4a + f3c8ab1 commit 4254806
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 45 deletions.
7 changes: 4 additions & 3 deletions oxen/Cargo.lock

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

6 changes: 3 additions & 3 deletions oxen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "oxen"
version = "0.1.28"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -15,8 +15,8 @@ log = "0.4.17"
pyo3-log = "0.8.1"
tokio = { version = "1", features = ["full"] }
pyo3-polars = "0.6.0"
liboxen = "0.8.6"
# liboxen = { path = "../../Oxen/src/lib" }
liboxen = "0.9.1"
# liboxen = { path = "../../rust/Oxen/src/lib" }

[build-dependencies]
cc = { version = "1.0", features = ["parallel"] }
Expand Down
15 changes: 3 additions & 12 deletions oxen/python/oxen/auth.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
from .oxen import auth
from .oxen import auth, util
from typing import Optional


def create_user_config(name: str, email: str, path: Optional[str] = None):
if path is None:
path = f"{auth.get_oxen_home_dir()}/user_config.toml"
if not path.endswith(".toml"):
raise ValueError("Path must end with .toml")
auth.create_user_config(name, email, path)


def add_host_auth(host: str, token: str, path: Optional[str] = None):
def config_auth(host: str, token: str, path: Optional[str] = None):
if path is None:
path = f"{auth.get_oxen_home_dir()}/user_config.toml"
path = f"{util.get_oxen_config_dir()}/user_config.toml"
if not path.endswith(".toml"):
raise ValueError("Path must end with .toml")
auth.add_host_auth(host, token, path)
16 changes: 16 additions & 0 deletions oxen/python/oxen/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .oxen import user, util
from typing import Optional

def config_user(name: str, email: str, path: Optional[str] = None):
if path is None:
path = f"{util.get_oxen_config_dir()}/user_config.toml"
if not path.endswith(".toml"):
raise ValueError(f"Path {path} must end with .toml")
return user.config_user(name, email, path)

def current_user(path: Optional[str] = None):
if path is None:
path = f"{util.get_oxen_config_dir()}/user_config.toml"
if not path.endswith(".toml"):
raise ValueError(f"Path {path} must end with .toml")
return user.current_user(path)
25 changes: 4 additions & 21 deletions oxen/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,15 @@
//!
use crate::error::PyOxenError;
use liboxen::config::user_config::UserConfig;
use liboxen::model::User;
use liboxen::util::fs::oxen_home_dir;
use liboxen::config::auth_config::AuthConfig;
use pyo3::prelude::*;
use std::path::{Path, PathBuf};
use std::path::Path;

#[pyfunction]
pub fn create_user_config(name: String, email: String, path: String) -> Result<(), PyOxenError> {
pub fn config_auth(host: String, token: String, path: String) -> Result<(), PyOxenError> {
let final_path = Path::new(&path);
let user = User { name, email };
let config = UserConfig::from_user(&user);
config.save(final_path)?;
Ok(())
}

#[pyfunction]
pub fn add_host_auth(host: String, token: String, path: String) -> Result<(), PyOxenError> {
let final_path = Path::new(&path);
let mut config = UserConfig::new(final_path);
let mut config = AuthConfig::new(final_path);
config.add_host_auth_token(host, token);
config.save(final_path)?;
Ok(())
}

#[pyfunction]
pub fn get_oxen_home_dir() -> Result<PathBuf, PyOxenError> {
let path = oxen_home_dir()?;
Ok(path)
}
23 changes: 17 additions & 6 deletions oxen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ pub mod py_branch;
pub mod auth;
pub mod py_commit;
pub mod py_dataset;
pub mod py_diff;
pub mod py_entry;
pub mod py_local_repo;
pub mod py_remote_repo;
pub mod py_paginated_dir_entries;
pub mod py_staged_data;
pub mod py_user;
pub mod user;
pub mod util;

/// A Python module implemented in Rust.
Expand All @@ -40,25 +43,33 @@ fn oxen(py: Python, m: &PyModule) -> PyResult<()> {
// https://docs.rs/pyo3-log/latest/pyo3_log/#interaction-with-python-gil
// pyo3_log::init();

m.add_class::<py_local_repo::PyLocalRepo>()?;
m.add_class::<py_branch::PyBranch>()?;
m.add_class::<py_remote_repo::PyRemoteRepo>()?;
m.add_class::<py_commit::PyCommit>()?;
m.add_class::<py_dataset::PyDataset>()?;
m.add_class::<py_diff::PyDiff>()?;
m.add_class::<py_local_repo::PyLocalRepo>()?;
m.add_class::<py_remote_repo::PyRemoteRepo>()?;
m.add_class::<py_staged_data::PyStagedData>()?;
m.add_class::<py_commit::PyCommit>()?;
m.add_class::<py_user::PyUser>()?;

// Util Module
let util_module = PyModule::new(py, "util")?;
util_module.add_function(wrap_pyfunction!(util::is_tabular, util_module)?)?;
util_module.add_function(wrap_pyfunction!(util::read_df, util_module)?)?;
util_module.add_function(wrap_pyfunction!(util::get_oxen_config_dir, util_module)?)?;
m.add_submodule(util_module)?;

// Auth Module
let auth_module = PyModule::new(py, "auth")?;
auth_module.add_function(wrap_pyfunction!(auth::get_oxen_home_dir, auth_module)?)?;
auth_module.add_function(wrap_pyfunction!(auth::add_host_auth, auth_module)?)?;
auth_module.add_function(wrap_pyfunction!(auth::create_user_config, auth_module)?)?;
auth_module.add_function(wrap_pyfunction!(auth::config_auth, auth_module)?)?;
m.add_submodule(auth_module)?;

// User Module
let user_module = PyModule::new(py, "user")?;
user_module.add_function(wrap_pyfunction!(user::config_user, user_module)?)?;
user_module.add_function(wrap_pyfunction!(user::current_user, user_module)?)?;
m.add_submodule(user_module)?;

Ok(())
}

Expand Down
30 changes: 30 additions & 0 deletions oxen/src/py_diff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use pyo3::prelude::*;

use liboxen::model::diff::generic_diff::GenericDiff;

#[pyclass]
pub struct PyDiff {
pub diff: GenericDiff,
}

#[pymethods]
impl PyDiff {
fn __repr__(&self) -> String {
format!("PyDiff(type={})", self.get_type())
}

#[getter]
pub fn get_type(&self) -> String {
match &self.diff {
GenericDiff::DirDiff(_diff) => {
"dir".to_string()
},
GenericDiff::TabularDiff(_diff) => {
"tabular".to_string()
},
// GenericDiff::TextDiff(_diff) => {
// "text".to_string()
// },
}
}
}
6 changes: 6 additions & 0 deletions oxen/src/py_local_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::path::PathBuf;
use crate::error::PyOxenError;
use crate::py_branch::PyBranch;
use crate::py_commit::PyCommit;
// use crate::py_diff::PyDiff;
use crate::py_staged_data::PyStagedData;

#[pyclass]
Expand Down Expand Up @@ -151,4 +152,9 @@ impl PyLocalRepo {
})?;
Ok(())
}

// pub fn diff(&self, path: &str) -> Result<PyDiff, PyOxenError> {
// let repo = LocalRepository::from_dir(&self.path)?;
// let diff =
// }
}
45 changes: 45 additions & 0 deletions oxen/src/py_user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use liboxen::model::User;
use pyo3::prelude::*;

#[pyclass]
pub struct PyUser {
_user: User,
}

#[pymethods]
impl PyUser {
#[new]
#[pyo3(signature = (name, email))]
pub fn new(name: String, email: String) -> Self {
Self {
_user: User {
name,
email
},
}
}

#[getter]
pub fn name(&self) -> &str {
&self._user.name
}

#[getter]
pub fn email(&self) -> &str {
&self._user.email
}

fn __repr__(&self) -> String {
format!("PyUser(name='{}', email={})", self._user.name, self._user.email)
}

fn __str__(&self) -> String {
format!("{}", self._user.name)
}
}

impl From<User> for PyUser {
fn from(user: User) -> PyUser {
PyUser { _user: user }
}
}
27 changes: 27 additions & 0 deletions oxen/src/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Oxen User Functions
//!
use crate::error::PyOxenError;
use crate::py_user::PyUser;
use liboxen::config::user_config::UserConfig;
use liboxen::model::User;
use pyo3::prelude::*;
use std::path::Path;

#[pyfunction]
pub fn config_user(name: String, email: String, path: String) -> Result<PyUser, PyOxenError> {
let final_path = Path::new(&path);
let user = User { name, email };
let config = UserConfig::from_user(&user);
config.save(final_path)?;
Ok(user.into())
}

#[pyfunction]
pub fn current_user(path: String) -> Result<PyUser, PyOxenError> {
let path = Path::new(&path);
let config = UserConfig::new(&path);
Ok(config.to_user().into())
}


8 changes: 8 additions & 0 deletions oxen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ use std::path::PathBuf;
use crate::error::PyOxenError;

use liboxen::{opts::DFOpts, util};
use liboxen::util::fs::oxen_config_dir;

/// Get the default home directory for exen
#[pyfunction]
pub fn get_oxen_config_dir() -> Result<PathBuf, PyOxenError> {
let path = oxen_config_dir()?;
Ok(path)
}

/// Checks if a path is tabular
#[pyfunction]
Expand Down

0 comments on commit 4254806

Please sign in to comment.