-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathConfig.fs
212 lines (176 loc) · 7.4 KB
/
Config.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
namespace Plotly.NET.ChartStudio
open DynamicObj
open System
open System.Runtime.InteropServices
open System.IO
open Newtonsoft.Json.Linq
open Newtonsoft.Json
open System.Diagnostics
type Credentials() =
inherit DynamicObj()
static let PLOTLY_DIR =
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
static let CREDENTIALS_FILE = Path.Combine(PLOTLY_DIR, ".credentials")
static member init
(
[<Optional; DefaultParameterValue(null)>] ?Username: string,
[<Optional; DefaultParameterValue(null)>] ?APIKEY: string,
[<Optional; DefaultParameterValue(null)>] ?ProxyUsername: string,
[<Optional; DefaultParameterValue(null)>] ?ProxyPassword: string
) =
Credentials()
|> Credentials.style (
?Username = Username,
?APIKEY = APIKEY,
?ProxyUsername = ProxyUsername,
?ProxyPassword = ProxyPassword
)
static member style
(
[<Optional; DefaultParameterValue(null)>] ?Username: string,
[<Optional; DefaultParameterValue(null)>] ?APIKEY: string,
[<Optional; DefaultParameterValue(null)>] ?ProxyUsername: string,
[<Optional; DefaultParameterValue(null)>] ?ProxyPassword: string
) =
fun (credentials: Credentials) ->
Username
|> DynObj.setValueOpt credentials "username"
APIKEY |> DynObj.setValueOpt credentials "api_key"
ProxyUsername
|> DynObj.setValueOpt credentials "proxy_username"
ProxyPassword
|> DynObj.setValueOpt credentials "proxy_password"
credentials
static member getCredentialsFile() =
try
let json =
JObject.Parse(File.ReadAllText(CREDENTIALS_FILE))
Credentials.init (
string json.["username"],
string json.["api_key"],
string json.["proxy_username"],
string json.["proxy_password"]
)
with
| _ -> Credentials.init ()
static member setCredentialsFile
(
[<Optional; DefaultParameterValue(null)>] ?Username: string,
[<Optional; DefaultParameterValue(null)>] ?APIKEY: string,
[<Optional; DefaultParameterValue(null)>] ?ProxyUsername: string,
[<Optional; DefaultParameterValue(null)>] ?ProxyPassword: string
) =
let text =
Credentials()
|> Credentials.style (
?Username = Username,
?APIKEY = APIKEY,
?ProxyUsername = ProxyUsername,
?ProxyPassword = ProxyPassword
)
|> JsonConvert.SerializeObject
try
File.WriteAllText(CREDENTIALS_FILE, text)
with
| e -> Trace.WriteLine(e.Message)
type Config() =
inherit DynamicObj()
static let PLOTLY_DIR =
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
static let CONFIG_FILE = Path.Combine(PLOTLY_DIR, ".config")
static member init
(
[<Optional; DefaultParameterValue("https://plotly.com")>] ?Domain: string,
[<Optional; DefaultParameterValue("stream.plotly.com")>] ?StreamingDomain: string,
[<Optional; DefaultParameterValue("https://api.plotly.com")>] ?APIDomain: string,
[<Optional; DefaultParameterValue(true)>] ?SSLVerification: bool,
[<Optional; DefaultParameterValue(false)>] ?ProxyAuthorization: bool,
[<Optional; DefaultParameterValue(true)>] ?WorldReadable: bool,
[<Optional; DefaultParameterValue("public")>] ?Sharing: string,
[<Optional; DefaultParameterValue(true)>] ?AutoOpen: bool
) =
Config()
|> Config.style (
?Domain = Domain,
?StreamingDomain = StreamingDomain,
?APIDomain = APIDomain,
?SSLVerification = SSLVerification,
?ProxyAuthorization = ProxyAuthorization,
?WorldReadable = WorldReadable,
?Sharing = Sharing,
?AutoOpen = AutoOpen
)
static member style
(
[<Optional; DefaultParameterValue(null)>] ?Domain: string,
[<Optional; DefaultParameterValue(null)>] ?StreamingDomain: string,
[<Optional; DefaultParameterValue(null)>] ?APIDomain: string,
[<Optional; DefaultParameterValue(null)>] ?SSLVerification: bool,
[<Optional; DefaultParameterValue(null)>] ?ProxyAuthorization: bool,
[<Optional; DefaultParameterValue(null)>] ?WorldReadable: bool,
[<Optional; DefaultParameterValue(null)>] ?Sharing: string,
[<Optional; DefaultParameterValue(null)>] ?AutoOpen: bool
) =
fun (config: Config) ->
Domain
|> DynObj.setValueOpt config "plotly_domain"
StreamingDomain
|> DynObj.setValueOpt config "plotly_streaming_domain"
APIDomain
|> DynObj.setValueOpt config "plotly_api_domain"
SSLVerification
|> DynObj.setValueOpt config "plotly_ssl_verification"
ProxyAuthorization
|> DynObj.setValueOpt config "plotly_proxy_authorization"
WorldReadable
|> DynObj.setValueOpt config "world_readable"
Sharing |> DynObj.setValueOpt config "sharing"
AutoOpen |> DynObj.setValueOpt config "auto_open"
config
static member getConfigFile() =
let getBool (jtoken: JToken) =
let _, result = bool.TryParse(string jtoken)
result
try
let json =
JObject.Parse(File.ReadAllText(CONFIG_FILE))
Config.init (
string json.["plotly_domain"],
string json.["plotly_streaming_domain"],
string json.["plotly_api_domain"],
getBool <| json.["plotly_ssl_verification"],
getBool <| json.["plotly_proxy_authorization"],
getBool <| json.["world_readable"],
string json.["sharing"],
getBool <| json.["auto_open"]
)
with
| _ -> Config.init ()
static member setConfigFile
(
[<Optional; DefaultParameterValue(null)>] ?Domain: string,
[<Optional; DefaultParameterValue(null)>] ?StreamingDomain: string,
[<Optional; DefaultParameterValue(null)>] ?APIDomain: string,
[<Optional; DefaultParameterValue(null)>] ?SSLVerification: bool,
[<Optional; DefaultParameterValue(null)>] ?ProxyAuthorization: bool,
[<Optional; DefaultParameterValue(null)>] ?WorldReadable: bool,
[<Optional; DefaultParameterValue(null)>] ?Sharing: string,
[<Optional; DefaultParameterValue(null)>] ?AutoOpen: bool
) =
let text =
Config()
|> Config.style (
?Domain = Domain,
?StreamingDomain = StreamingDomain,
?APIDomain = APIDomain,
?SSLVerification = SSLVerification,
?ProxyAuthorization = ProxyAuthorization,
?WorldReadable = WorldReadable,
?Sharing = Sharing,
?AutoOpen = AutoOpen
)
|> JsonConvert.SerializeObject
try
File.WriteAllText(CONFIG_FILE, text)
with
| e -> Trace.WriteLine(e.Message)