├── .coveragerc ├── .dockerignore ├── .editorconfig ├── .flake8 ├── .gitignore ├── COPYING ├── Dockerfile ├── README.md ├── client.py ├── connection.py ├── coverage.sh ├── docker_run.sh ├── example_client.py ├── log └── .gitignore ├── monitor.py ├── pyproject.toml ├── request_handler.py ├── requirements.txt ├── saved_spreadsheets └── .gitignore ├── server.py ├── spreadsheets └── .gitignore └── tests ├── __init__.py ├── context.py ├── example.ods ├── test_client.py ├── test_connection.py └── test_monitor.py /.coveragerc: -------------------------------------------------------------------------------- 1 | [report] 2 | omit = 3 | */venv/* 4 | /usr/lib/libreoffice/program/uno.py 5 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | .* 2 | venv 3 | log 4 | saved_spreadsheets 5 | spreadsheets -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | 11 | # Matches multiple files with brace expansion notation 12 | # Set default charset 13 | [*.{js,py}] 14 | charset = utf-8 15 | 16 | # 4 space indentation 17 | [*.py] 18 | indent_style = space 19 | indent_size = 4 20 | 21 | # Tab indentation (no size specified) 22 | [Makefile] 23 | indent_style = tab 24 | 25 | # Indentation override for all JS under lib directory 26 | [*.{js,ts,tsx,scss}] 27 | indent_style = space 28 | indent_size = 2 29 | 30 | # Matches the exact files either package.json or .travis.yml 31 | [{package.json,.travis.yml}] 32 | indent_style = space 33 | indent_size = 2 34 | -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | # This is an example .flake8 config, used when developing *Black* itself. 2 | # Keep in sync with setup.cfg which is used for source packages. 3 | [flake8] 4 | ignore = E203, E266, E501, W503, F403, F401, C901, E711, E712, E402 5 | max-line-length = 79 6 | max-complexity = 18 7 | select = B,C,E,F,W,T4,B9 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .#* 3 | \#* 4 | *~ 5 | __pycache__ 6 | .~lock* 7 | spreadsheets/* 8 | log/*.log 9 | *.pyc 10 | saved_spreadsheets/* 11 | .coverage 12 | htmlcov 13 | .vscode 14 | /todo.org 15 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:16.04 2 | RUN apt-get update && apt-get upgrade -y && apt-get install git python3 python3-dev python-virtualenv libreoffice-calc python3-uno gcc python3-pip -y 3 | COPY * /spreadsheet_server/ 4 | WORKDIR /spreadsheet_server 5 | RUN pip3 install -r requirements.txt 6 | EXPOSE 5555 7 | CMD ["python3", "-c", "from server import SpreadsheetServer; spreadsheet_server = SpreadsheetServer(host='0.0.0.0'); spreadsheet_server.run()"] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spreadsheet_server 2 | 3 | ## Introduction 4 | 5 | spreadsheet_server was built to aid rapid web tool development where the logic 6 | was already implemented in Microsoft Excel/LibreOffice Calc. Instead of 7 | rewriting the logic from scratch, this tool was born. 8 | 9 | The tool has been developed to work on a headless GNU/Linux where the server 10 | and client are on the same machine. 11 | 12 | ## Features 13 | 14 | - 'Instant' access to cells in the spreadsheets as they open in LibreOffice Calc. 15 | - All the function calculation support and power of LibreOffice Calc. 16 | - A given spreadsheet is locked (within python, not on disk) when it is accessed to prevent state irregularities across multiple concurrent connections to the same spreadsheet. 17 | - Monitoring of a directory with automatic loading and unloading of spreadsheets. 18 | - By default, when a spreadsheet file changes on disk, it will be closed and 19 | opened in LibreOffice. 20 | - Spreadsheets can be saved - useful for debugging purposes. 21 | 22 | ## Installation 23 | 24 | ### Ubuntu Server 20.04 25 | 26 | ``` 27 | sudo apt-get update && sudo apt-get upgrade 28 | sudo apt-get install git gcc python3 python3-dev python3-virtualenv libreoffice-calc python3-uno 29 | git clone https://github.com/robsco-git/spreadsheet_server.git 30 | cd spreadsheet_server 31 | mkdir -p ~/.virtualenvs 32 | virtualenv --system-site-packages -p python3 ~/.virtualenvs/spreadsheet_server 33 | source ~/.virtualenvs/spreadsheet_server/bin/activate 34 | pip install -r requirements.txt 35 | python server.py 36 | # Copy spreadsheets (.xlsx, .ods etc) into ./spreadsheets 37 | ``` 38 | 39 | ## Usage 40 | 41 | Place your spreadsheets in './spreadsheets'. 42 | 43 | Make sure you have a correctly set up virtualenv the required packages are installed 44 | (see below), then run the server: 45 | 46 | ``` 47 | python server.py 48 | ``` 49 | 50 | You can also create a SpreadsheetServer object (from server.py) and then call 51 | the method 'run' on the object to start the server. This allows the for 52 | customisation of the default settings. Have a look in server.py to see what can 53 | be set. 54 | 55 | example_client.py is provided for an overview of how to use the functions 56 | exposed by the client. This is a good place to start. 57 | 58 | ## How it works 59 | 60 | - A LibreOffice instance is launched by 'server.py' in a headless state. 61 | - By default, the './spreadsheets' directory is polled every 5 seconds for file 62 | changes. 63 | - New spreadsheets in the directory are opened with LibreOffice and removed 64 | spreadsheets are closed in LibreOffice. 65 | - The 'client.py' connects to the server and can update cells and retrieve 66 | their calculated content. 67 | 68 | ## Questions 69 | 70 | What is UNO? What is Python-UNO? What is PyOO? 71 | The first few paragraphs of the PyOO README should answer most of your questions: 72 | https://github.com/seznam/pyoo/blob/master/README.rst 73 | 74 | ## Notes 75 | 76 | ### Symbolic links and lock files 77 | 78 | If you symbolically link a spreadsheet itself, the lock files that LibreOffice 79 | creates and uses are stored in the directory where the file is located, not in the 80 | directory where the symbolic link is located. It is recommended that you place your 81 | spreadsheet(s) into a directory and symbolically link that directory into the 82 | './spreadsheets' directory. This way, LibreOffice will always be able to locate the 83 | lock files it needs. You can use a directory per project if you like. 84 | 85 | ## Tests 86 | 87 | You can run the all the current tests with 'python -m unittest discover'. 88 | Use './coverage.sh' to run the coverage analysis of the current tests and have a look 89 | in the generated htmlcov directory. You will need the 'coverage' installed to the 90 | virtualenv: 91 | 92 | ``` 93 | pip install coverage 94 | ``` 95 | -------------------------------------------------------------------------------- /client.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Robert Scott 2 | 3 | # This program is free software; you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License 5 | # as published by the Free Software Foundation; either version 2 6 | # of the License, or (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 | 17 | import socket 18 | import json 19 | import traceback 20 | import struct 21 | import select 22 | 23 | IP, PORT = "localhost", 5555 24 | 25 | TIMEOUT = 10 26 | 27 | 28 | class SpreadsheetClient: 29 | def __init__(self, spreadsheet, ip=IP, port=PORT): 30 | try: 31 | self.sock = self.__connect(ip, port) 32 | except socket.error: 33 | raise RuntimeError("Could not connect to the server.") 34 | else: 35 | self.__set_spreadsheet(spreadsheet) 36 | 37 | def __connect(self, ip, port): 38 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 39 | sock.connect((ip, port)) 40 | return sock 41 | 42 | def __set_spreadsheet(self, spreadsheet): 43 | self.__send(["SPREADSHEET", spreadsheet]) 44 | received = self.__receive() 45 | 46 | if received == "NOT FOUND" or received != "OK": 47 | self.disconnect() 48 | raise RuntimeError("The requested spreadsheet was not found.") 49 | 50 | def set_cells(self, sheet, cell_ref, data): 51 | """Set the value(s) for a single cell or a cell range. 52 | 53 | 'sheet' is either a 0-based index or the string name of the sheet. 54 | 'cell_ref' is a LibreOffice style cell reference. eg. "A1" or "D7:G42". 55 | 56 | For a single cell, 'data' is a single string, int or float value. 57 | 58 | For a one dimensional (only horizontal or only vertical) range of 59 | cells, 'data' is a list. For a two dimensional range of cells, 'data' 60 | is a list of lists. For example setting the 'cell_ref' "A1:C3" 61 | requires 'data' of the format: 62 | [[A1, B1, C1], [A2, B2, C2], [A3, B3, C3]]. 63 | """ 64 | 65 | self.__send(["SET", sheet, cell_ref, data]) 66 | 67 | received = self.__receive() 68 | if type(received) == dict: 69 | # The server is retuning an error 70 | raise RuntimeError(received["ERROR"]) 71 | 72 | def get_sheet_names(self): 73 | """Returns a list of all sheet names in the workbook.""" 74 | 75 | self.__send(["GET_SHEETS"]) 76 | sheet_names = self.__receive() 77 | 78 | if sheet_names == "ERROR": 79 | raise Exception("Could not retrieve sheet names.") 80 | 81 | return sheet_names 82 | 83 | def get_cells(self, sheet, cell_ref): 84 | """Get the value of a single cell or a cell range from the server 85 | and return it or them. 86 | 87 | 'sheet' is either a 0-based index or the string name of the sheet. 88 | 'cell_ref' is what one would use in LibreOffice Calc. Eg. "ABC945" or 89 | "A3:F75". 90 | 91 | A single cell is returned for a single value. 92 | A list of cell values is returned for a one dimensional range of cells. 93 | A list of lists is returned for a two dimensional range of cells. 94 | """ 95 | 96 | self.__send(["GET", sheet, cell_ref]) 97 | cells = self.__receive() 98 | 99 | if type(cells) == dict: 100 | # The server is retuning an error 101 | raise RuntimeError(cells["ERROR"]) 102 | 103 | return cells 104 | 105 | def save_spreadsheet(self, filename): 106 | """Save the spreadsheet in its current state on the server. The 107 | server determines where it is saved.""" 108 | 109 | self.__send(["SAVE", filename]) 110 | return self.__receive() 111 | 112 | def __send(self, msg): 113 | """Encode msg into json and then send it over the socket.""" 114 | 115 | json_msg = json.dumps(msg) 116 | json_msg = bytes(json_msg, "utf-8") 117 | 118 | # Prepend the length of the string to the meg 119 | json_msg = struct.pack(">I", len(json_msg)) + json_msg 120 | 121 | try: 122 | self.sock.sendall(json_msg) 123 | except: # noqa 124 | traceback.print_exc() 125 | raise Exception("Could not send message to server") 126 | 127 | def __receive(self): 128 | """Receive a message from the client, convert the received utf-8 129 | bytes into a string then decode if from json.""" 130 | 131 | raw_msg_length = self.__receive_length(4) 132 | if not raw_msg_length: 133 | return False 134 | msg_length = struct.unpack(">I", raw_msg_length)[0] 135 | 136 | recv = self.__receive_length(msg_length) 137 | 138 | if recv == b"": 139 | # The connection has been closed. 140 | raise Exception("Connection to server closed!") 141 | 142 | received = str(recv, encoding="utf-8") 143 | received = json.loads(received) 144 | 145 | return received 146 | 147 | def __receive_length(self, length): 148 | """Receive length number of bytes from the client.""" 149 | 150 | data = b"" 151 | while len(data) < length: 152 | 153 | ready = select.select([self.sock], [], [], TIMEOUT) 154 | 155 | if ready[0]: 156 | 157 | packet = self.sock.recv(length - len(data)) 158 | if not packet: 159 | return b"" 160 | data += packet 161 | 162 | else: 163 | # Did not recieve on the socket within the timeout 164 | return b"" 165 | 166 | return data 167 | 168 | def disconnect(self): 169 | """Disconnect from the server.""" 170 | 171 | try: 172 | self.sock.shutdown(socket.SHUT_RDWR) 173 | except socket.error: 174 | # The client has already disconnected 175 | pass 176 | 177 | self.sock.close() 178 | -------------------------------------------------------------------------------- /connection.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Robert Scott 2 | 3 | # This program is free software; you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License 5 | # as published by the Free Software Foundation; either version 2 6 | # of the License, or (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 16 | # USA. 17 | 18 | import logging 19 | from math import pow 20 | from werkzeug.utils import secure_filename 21 | from threading import ThreadError 22 | import os 23 | 24 | 25 | CELL_REF_ERROR_STR = "Cell range is invalid." 26 | 27 | 28 | class SpreadsheetConnection: 29 | """Handles connections to the spreadsheets opened by soffice (LibreOffice). 30 | """ 31 | 32 | def __init__(self, spreadsheet, lock, save_path): 33 | self.spreadsheet = spreadsheet 34 | self.lock = lock 35 | self.save_path = save_path 36 | 37 | def lock_spreadsheet(self): 38 | """Lock the spreadsheet. 39 | 40 | The getting and setting cell functions rely on a given spreadsheet 41 | being locked. This insures simulations requests to the same spreadsheet 42 | do not interfere with one another. 43 | """ 44 | 45 | self.lock.acquire() 46 | 47 | def unlock_spreadsheet(self): 48 | """ Unlock the spreadsheet and return a 'success' boolean.""" 49 | 50 | try: 51 | self.lock.release() 52 | return True 53 | except (RuntimeError, ThreadError): 54 | return False 55 | 56 | def __get_xy_index(self, cell_ref): 57 | chars = [c for c in cell_ref if c.isalpha()] 58 | nums = [n for n in cell_ref if n.isdigit()] 59 | 60 | alpha_index = 0 61 | for i, c in enumerate(chars): 62 | # The base index for this character is in the range of 0 to 25 63 | c_index = ord(c.upper()) - 65 64 | 65 | if i == len(chars) - 1: 66 | # The simple case of the least significant character. 67 | # Eg. The 'J' in 'AMJ'. 68 | alpha_index += c_index 69 | 70 | else: 71 | # The index for additional characters to the left are 72 | # calculated c_index * 26^(n) where n is the characters 73 | # position to the left. 74 | 75 | # Need to increment c_index for correct multiplication 76 | c_index += 1 77 | alpha_index += c_index * pow(26, len(chars) - i - 1) 78 | 79 | num_index = int("".join(nums)) - 1 # zero-based 80 | alpha_index = int(alpha_index) 81 | 82 | # Check max values 83 | # Column can not be > AMJ == 1023 84 | if alpha_index >= 1024: 85 | raise ValueError(CELL_REF_ERROR_STR) 86 | 87 | # Row can not be > 1048576 88 | if num_index >= 1048576: 89 | raise ValueError(CELL_REF_ERROR_STR) 90 | 91 | return alpha_index, num_index 92 | 93 | def __is_single_cell(self, cell_ref): 94 | if len(cell_ref.split(":")) == 1: 95 | return True 96 | return False 97 | 98 | def __check_single_cell(self, cell_ref): 99 | if not self.__is_single_cell(cell_ref): 100 | raise ValueError( 101 | "Expected a single cell reference. A cell range was given." 102 | ) 103 | 104 | def __cell_to_index(self, cell_ref): 105 | """Convert a spreadsheet style single cell or cell reference, to a zero 106 | based numerical index. 107 | 108 | 'cell_ref' is what one would use in LibreOffice Calc. Eg. "ABC945". 109 | 110 | Returned is: {"row_index": int, "column_index": int}. 111 | """ 112 | 113 | alpha_index, num_index = self.__get_xy_index(cell_ref) 114 | return {"row_index": num_index, "column_index": alpha_index} 115 | 116 | def __cell_range_to_index(self, cell_ref): 117 | """Convert a spreadsheet style range reference to zero-based numerical 118 | indecies that describe the start and end points of the cell range. 119 | 120 | 'cell_ref' is what one would use in LibreOffice Calc. Eg. "A1" or 121 | "A1:D6". 122 | 123 | Returned is: {"row_start": int, "row_end": int, 124 | "column_start": int, "column_end": int} 125 | """ 126 | 127 | left_ref, right_ref = cell_ref.split(":") 128 | 129 | left_alpha_index, left_num_index = self.__get_xy_index(left_ref) 130 | right_alpha_index, right_num_index = self.__get_xy_index(right_ref) 131 | 132 | return { 133 | "row_start": left_num_index, 134 | "row_end": right_num_index, 135 | "column_start": left_alpha_index, 136 | "column_end": right_alpha_index, 137 | } 138 | 139 | def __check_for_lock(self): 140 | if not self.lock.locked(): 141 | raise RuntimeError( 142 | "Lock for this spreadsheet has not been aquired." 143 | ) 144 | 145 | def __convert_to_float_if_numeric(self, value): 146 | """If value is a string representation of a number, convert it to a 147 | float. Otherwise, simply return the string. 148 | """ 149 | 150 | try: 151 | return float(value) 152 | except (ValueError, TypeError): 153 | return value 154 | 155 | def __check_list(self, data): 156 | if not isinstance(data, list): 157 | raise ValueError("Expecting list type.") 158 | 159 | def __check_1D_list(self, data): 160 | self.__check_list(data) 161 | if isinstance(data[0], list): 162 | raise ValueError("Got 2D list when expecting 1D list.") 163 | 164 | for x, cell in enumerate(data): 165 | data[x] = self.__convert_to_float_if_numeric(cell) 166 | 167 | return data 168 | 169 | def set_cells(self, sheet, cell_ref, value): 170 | """Set the value(s) for a single cell or a cell range. This can be used 171 | when it is not known if 'cell_ref' refers to a single cell or a range 172 | 173 | See 'set_cell' and 'set_cell_range' for more information. 174 | """ 175 | 176 | self.__validate_sheet_name(sheet) 177 | self.__validate_cell_ref(cell_ref) 178 | 179 | if self.__is_single_cell(cell_ref): 180 | self.set_cell(sheet, cell_ref, value) 181 | else: 182 | self.set_cell_range(sheet, cell_ref, value) 183 | 184 | def set_cell(self, sheet, cell_ref, value): 185 | """Set the value of a single cell. 186 | 187 | 'sheet' is either a 0-based index or the string name of the sheet. 188 | 'cell_ref' is a LibreOffice style cell reference. eg. "A1". 189 | 'value' is a single string, int or float value. 190 | """ 191 | 192 | self.__check_single_cell(cell_ref) 193 | 194 | self.__check_for_lock() 195 | 196 | r = self.__cell_to_index(cell_ref) 197 | sheet = self.spreadsheet.sheets[sheet] 198 | 199 | if isinstance(value, list): 200 | raise ValueError( 201 | "Expectin a single cell. \ 202 | A list of cells was given." 203 | ) 204 | 205 | value = self.__convert_to_float_if_numeric(value) 206 | sheet[r["row_index"], r["column_index"]].value = value 207 | 208 | def set_cell_range(self, sheet, cell_ref, data): 209 | """Set the values for a cell range. 210 | 211 | 'sheet' is either a 0-based index or the string name of the sheet. 212 | 'cell_ref' is a LibreOffice style cell reference. eg. "D7:G42". 213 | 214 | For a one dimensional (only horizontal or only vertical) range of 215 | cells, 'data' is a list. For a two dimensional range of cells, 'data' 216 | is a list of lists. For example setting the 'cell_ref' "A1:C3" 217 | requires 'data' of the format: 218 | [[A1, B1, C1], [A2, B2, C2], [A3, B3, C3]]. 219 | """ 220 | 221 | self.__check_for_lock() 222 | 223 | r = self.__cell_range_to_index(cell_ref) 224 | sheet = self.spreadsheet.sheets[sheet] 225 | 226 | if r["row_start"] == r["row_end"]: # A row of cells 227 | data = self.__check_1D_list(data) 228 | sheet[ 229 | r["row_start"], r["column_start"] : r["column_end"] + 1 230 | ].values = data 231 | 232 | elif r["column_start"] == r["column_end"]: # A column of cells 233 | data = self.__check_1D_list(data) 234 | sheet[ 235 | r["row_start"] : r["row_end"] + 1, r["column_start"] 236 | ].values = data 237 | 238 | else: # A grid of cells 239 | self.__check_list(data) 240 | for x, row in enumerate(data): 241 | if not isinstance(row, list): 242 | raise ValueError("Expected a list of cells.") 243 | 244 | for y, cell in enumerate(row): 245 | data[x][y] = self.__convert_to_float_if_numeric(cell) 246 | 247 | sheet[ 248 | r["row_start"] : r["row_end"] + 1, 249 | r["column_start"] : r["column_end"] + 1, 250 | ].values = data 251 | 252 | def get_sheet_names(self): 253 | """Returns a list of all sheet names in the workbook.""" 254 | 255 | return [s.name for s in self.spreadsheet.sheets] 256 | 257 | def __validate_cell_ref(self, cell_ref): 258 | """ A cell ref must be of the LibreOffice format 259 | e.g. A1 or A1:ABC123.""" 260 | 261 | if type(cell_ref) is not str: 262 | raise ValueError(CELL_REF_ERROR_STR) 263 | 264 | if not cell_ref[0].isalpha(): 265 | raise ValueError(CELL_REF_ERROR_STR) 266 | 267 | if not cell_ref[-1].isdigit(): 268 | raise ValueError(CELL_REF_ERROR_STR) 269 | 270 | if ":" in cell_ref: 271 | # Check the second alpha if it exists 272 | if not cell_ref[cell_ref.index(":") + 1].isalpha(): 273 | raise ValueError(CELL_REF_ERROR_STR) 274 | 275 | # Check the start of the range has a numeric component 276 | if not cell_ref[cell_ref.index(":") - 1].isdigit(): 277 | raise ValueError(CELL_REF_ERROR_STR) 278 | 279 | # Check for any unallowed characters 280 | for ref in cell_ref: 281 | if not ref.isdigit() and not ref.isalpha() and ref != ":": 282 | raise ValueError(CELL_REF_ERROR_STR) 283 | 284 | # TODO - Check range for sanity 285 | # Reversed ranges should be allowed, they just need to be flipped 286 | # e.g. "A5:A1" must become "A1:A5" 287 | # Also need to convert "A1:A1" to "A1" 288 | 289 | def __validate_sheet_name(self, sheet): 290 | """Don't want to send an invalid sheet to pyoo.""" 291 | 292 | ERROR_STR = "Sheet name is invalid." 293 | 294 | sheet_names = self.get_sheet_names() 295 | if type(sheet) is int: 296 | 297 | if sheet < 0 or sheet > len(sheet_names) - 1: 298 | raise ValueError(ERROR_STR) 299 | 300 | elif type(sheet) is str: 301 | if sheet not in sheet_names: 302 | raise ValueError(ERROR_STR) 303 | 304 | else: 305 | raise ValueError(ERROR_STR) 306 | 307 | def get_cells(self, sheet, cell_ref): 308 | """Gets the value(s) of a single cell or a cell range. This can be used 309 | when it is not known if 'cell_ref' refers to a single cell or a range. 310 | 311 | See 'get_cell' and 'get_cell_range' for more information. 312 | """ 313 | 314 | self.__validate_sheet_name(sheet) 315 | self.__validate_cell_ref(cell_ref) 316 | 317 | if self.__is_single_cell(cell_ref): 318 | return self.get_cell(sheet, cell_ref) 319 | else: 320 | return self.get_cell_range(sheet, cell_ref) 321 | 322 | def get_cell(self, sheet, cell_ref): 323 | """Returns the value of a single cell. 324 | 325 | 'sheet' is either a 0-based index or the string name of the sheet. 326 | 'cell_ref' is what one would use in LibreOffice Calc. Eg. "A3". 327 | 328 | A single cell value is returned. 329 | """ 330 | 331 | self.__check_single_cell(cell_ref) 332 | 333 | r = self.__cell_to_index(cell_ref) 334 | sheet = self.spreadsheet.sheets[sheet] 335 | 336 | return sheet[r["row_index"], r["column_index"]].value 337 | 338 | def get_cell_range(self, sheet, cell_ref): 339 | """Returns the values of a range of cells. 340 | 341 | 'sheet' is either a 0-based index or the string name of the sheet. 342 | 'cell_ref' is what one would use in LibreOffice Calc. Eg. "A3:F75". 343 | 344 | A list of cell values is returned for a one dimensional range of cells. 345 | A list of lists is returned for a two dimensional range of cells. 346 | """ 347 | 348 | r = self.__cell_range_to_index(cell_ref) 349 | sheet = self.spreadsheet.sheets[sheet] 350 | 351 | logging.debug("Requested cell area: " + str(r)) 352 | 353 | # Cell ranges are requested as: [vertical area, horizontal area] 354 | 355 | if r["row_start"] == r["row_end"]: # A row of cells was requested 356 | return sheet[ 357 | r["row_start"], r["column_start"] : r["column_end"] + 1 358 | ].values 359 | 360 | elif r["column_start"] == r["column_end"]: # A column of cells 361 | return sheet[ 362 | r["row_start"] : r["row_end"] + 1, r["column_start"] 363 | ].values 364 | 365 | else: # A grid of cells 366 | return sheet[ 367 | r["row_start"] : r["row_end"] + 1, 368 | r["column_start"] : r["column_end"] + 1, 369 | ].values 370 | 371 | def save_spreadsheet(self, filename): 372 | """Save the spreadsheet in it's current state. 373 | 374 | 'filename' is the name of the file. 375 | """ 376 | 377 | if self.lock.locked(): 378 | filename = secure_filename(filename) 379 | self.spreadsheet.save(os.path.join(self.save_path, filename)) 380 | return True 381 | else: 382 | return False 383 | -------------------------------------------------------------------------------- /coverage.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Run the coverage tests and genereate the html reports 4 | 5 | #coverage run -m tests.test_connection && coverage html 6 | coverage run -m unittest discover && coverage html 7 | -------------------------------------------------------------------------------- /docker_run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sudo docker build . -t spreadsheet_server 3 | sudo docker stop spreadsheet_server 4 | sudo docker rm spreadsheet_server 5 | sudo docker run -d \ 6 | -v /home/robsco/code/spreadsheet_server/spreadsheets:/spreadsheet_server/spreadsheets \ 7 | -v /home/robsco/code/spreadsheet_server/saved_spreadsheets:/spreadsheet_server/saved_spreadsheets \ 8 | --restart always \ 9 | -v /home/robsco/code/spreadsheet_server/log:/spreadsheet_server/log \ 10 | -p 5555:5555 \ 11 | --name spreadsheet_server \ 12 | spreadsheet_server 13 | -------------------------------------------------------------------------------- /example_client.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Robert Scott 2 | 3 | # This program is free software; you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License 5 | # as published by the Free Software Foundation; either version 2 6 | # of the License, or (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 | 17 | import shutil 18 | import os 19 | from client import SpreadsheetClient 20 | 21 | if __name__ == "__main__": 22 | """This script shows how the differnet functions exposed by client.py can be 23 | used.""" 24 | 25 | EXAMPLE_SPREADSHEET = "example.ods" 26 | 27 | # Copy the example spreadsheet from the tests directory into the spreadsheets 28 | # directory 29 | 30 | shutil.copyfile( 31 | os.path.join("tests", EXAMPLE_SPREADSHEET), 32 | os.path.join("spreadsheets", EXAMPLE_SPREADSHEET), 33 | ) 34 | 35 | SHEET_NAME = "Sheet1" 36 | 37 | print( 38 | "Waiting for the example spreadsheet to be scanned and loaded into LibreOffice." 39 | ) 40 | 41 | sc = SpreadsheetClient(EXAMPLE_SPREADSHEET) 42 | 43 | # Get sheet names 44 | sheet_names = sc.get_sheet_names() 45 | print(sheet_names) 46 | 47 | # Set a cell value 48 | sc.set_cells(SHEET_NAME, "A1", 5) 49 | 50 | # Retrieve a cell value. 51 | cell_value = sc.get_cells(SHEET_NAME, "C3") 52 | print(cell_value) 53 | 54 | # Set a one dimensional cell range. 55 | # Cells are set using the format: [A1, A2, A3] 56 | cell_values = [1, 2, 3] 57 | sc.set_cells(SHEET_NAME, "A1:A3", cell_values) 58 | 59 | # Retrieve one dimensional cell range. 60 | cell_values = sc.get_cells(SHEET_NAME, "C1:C3") 61 | print(cell_values) 62 | 63 | # Set a two dimensional cell range. 64 | # Cells are set using the format: [[A1, B1, C1], [A2, B2, C2], [A3, B3, C3]] 65 | cell_values = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 66 | sc.set_cells(SHEET_NAME, "A1:C3", cell_values) 67 | 68 | # Retrieve a two dimensional cell range. 69 | cell_values = sc.get_cells(SHEET_NAME, "A1:C3") 70 | print(cell_values) 71 | 72 | # Save a spreadsheet - it will save into ./saved_spreadsheets 73 | sc.save_spreadsheet(EXAMPLE_SPREADSHEET) 74 | 75 | sc.disconnect() 76 | 77 | os.remove(os.path.join("spreadsheets", EXAMPLE_SPREADSHEET)) 78 | -------------------------------------------------------------------------------- /log/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore 5 | 6 | -------------------------------------------------------------------------------- /monitor.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Robert Scott 2 | 3 | # This program is free software; you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License 5 | # as published by the Free Software Foundation; either version 2 6 | # of the License, or (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 | 17 | import threading 18 | from os import listdir, remove 19 | from os.path import isfile, isdir, join, exists 20 | import logging 21 | from time import sleep 22 | import hashlib 23 | from glob import glob 24 | 25 | 26 | class MonitorThread(threading.Thread): 27 | """Monitors the spreadsheet directory for changes.""" 28 | 29 | def __init__( 30 | self, 31 | spreadsheets, 32 | locks, 33 | hashes, 34 | soffice, 35 | spreadsheets_path, 36 | monitor_frequency, 37 | reload_on_disk_change, 38 | ): 39 | 40 | self._stop_thread = threading.Event() 41 | 42 | self.spreadsheets = spreadsheets 43 | self.locks = locks 44 | self.hashes = hashes 45 | self.soffice = soffice 46 | self.spreadsheets_path = spreadsheets_path 47 | self.monitor_frequency = monitor_frequency 48 | self.reload_on_disk_change = reload_on_disk_change 49 | 50 | self.__delete_lock_files() 51 | 52 | self.done_scan = False # Done an initial scan or not 53 | 54 | super(MonitorThread, self).__init__() 55 | 56 | def stop_thread(self): 57 | self._stop_thread.set() 58 | 59 | def stopped(self): 60 | return self._stop_thread.isSet() 61 | 62 | def initial_scan(self): 63 | return self.done_scan 64 | 65 | def __get_full_path(self, doc): 66 | return join(self.spreadsheets_path, doc) 67 | 68 | def __delete_lock_files(self): 69 | """Lock files can cause issues opening documents.""" 70 | 71 | lock_files = glob( 72 | join(self.spreadsheets_path, ".~lock.*#"), recursive=True 73 | ) 74 | for lock_file in lock_files: 75 | remove(lock_file) 76 | 77 | def __load_spreadsheet(self, doc): 78 | logging.info("Loading " + doc["path"]) 79 | 80 | self.spreadsheets[doc["path"]] = self.soffice.open_spreadsheet( 81 | self.__get_full_path(doc["path"]) 82 | ) 83 | self.locks[doc["path"]] = threading.Lock() 84 | self.hashes[doc["path"]] = doc["hash"] 85 | 86 | def __unload_spreadsheet(self, doc_path): 87 | logging.info("Removing " + doc_path) 88 | self.locks[doc_path].acquire() 89 | self.spreadsheets[doc_path].close() 90 | self.spreadsheets.pop(doc_path, None) 91 | self.locks.pop(doc_path, None) 92 | self.hashes.pop(doc_path, None) 93 | 94 | def __check_added(self): 95 | """Check for new spreadsheets and loads them into LibreOffice.""" 96 | 97 | for doc in self.docs: 98 | if doc["path"][0] != ".": # Ignore hidden files 99 | load = True # Default to loading the spreadsheet 100 | 101 | for key, value in self.spreadsheets.items(): 102 | if doc["path"] == key: 103 | 104 | # Check if the file has been modified 105 | # Does the file now have a differnet hash? 106 | 107 | if ( 108 | self.reload_on_disk_change 109 | and doc["hash"] != self.hashes[doc["path"]] 110 | ): 111 | 112 | self.__unload_spreadsheet(doc["path"]) 113 | else: 114 | load = False 115 | 116 | break 117 | 118 | if load: 119 | self.__load_spreadsheet(doc) 120 | 121 | def __check_removed(self): 122 | """Check for any deleted or removed spreadsheets and remove them from 123 | LibreOffice. 124 | """ 125 | 126 | removed_spreadsheets = [] 127 | for key, value in self.spreadsheets.items(): 128 | removed = True 129 | for doc in self.docs: 130 | if key == doc["path"]: 131 | removed = False 132 | break 133 | if removed: 134 | removed_spreadsheets.append(key) 135 | 136 | for doc_path in removed_spreadsheets: 137 | self.__unload_spreadsheet(doc_path) 138 | 139 | def __scan_directory(self, d): 140 | """Recursively scan a directory for spreadsheets.""" 141 | 142 | dir_contents = listdir(d) 143 | 144 | for f in dir_contents: 145 | 146 | # Ignore particular files 147 | if f[:7] == ".~lock." or f == ".gitignore": 148 | continue 149 | 150 | full_path = join(d, f) 151 | if isfile(full_path): 152 | # Remove self.spreadsheets_path from the path 153 | relative_path = full_path.split(self.spreadsheets_path)[1][1:] 154 | 155 | # Calculate the MD5 hash for the file 156 | hasher = hashlib.md5() 157 | with open(self.__get_full_path(relative_path), "rb") as afile: 158 | buf = afile.read() 159 | hasher.update(buf) 160 | h = hasher.hexdigest() 161 | 162 | self.docs.append({"path": relative_path, "hash": h}) 163 | elif isdir(full_path): 164 | self.__scan_directory(full_path) 165 | 166 | def run(self): 167 | while not self.stopped(): 168 | self.docs = [] 169 | 170 | self.__scan_directory(self.spreadsheets_path) 171 | 172 | self.__check_removed() 173 | self.__check_added() 174 | 175 | self.done_scan = True 176 | 177 | sleep(self.monitor_frequency) 178 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.black] 2 | line-length = 79 3 | include = '\.pyi?$' 4 | exclude = ''' 5 | /( 6 | \.git 7 | | \.hg 8 | | \.mypy_cache 9 | | \.tox 10 | | \.venv 11 | | _build 12 | | buck-out 13 | | build 14 | | dist 15 | )/ 16 | ''' -------------------------------------------------------------------------------- /request_handler.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Robert Scott 2 | 3 | # This program is free software; you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License 5 | # as published by the Free Software Foundation; either version 2 6 | # of the License, or (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 | 17 | import json 18 | import logging 19 | import select 20 | import socketserver 21 | import struct 22 | from socket import SHUT_RDWR 23 | from time import sleep 24 | 25 | from com.sun.star.uno import RuntimeException 26 | from com.sun.star.io import IOException 27 | from connection import SpreadsheetConnection 28 | 29 | TIMEOUT = 10 30 | 31 | 32 | class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): 33 | def __init__(self, save_path, *args, **kwargs): 34 | self.save_path = save_path 35 | socketserver.TCPServer.__init__(self, *args, **kwargs) 36 | 37 | 38 | class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler): 39 | def __send(self, msg): 40 | """Convert a message to JSON and send it to the client. 41 | 42 | The messages are sent as utf-8 encoded bytes 43 | """ 44 | 45 | # What type is msg coming in as? 46 | json_msg = json.dumps(msg) 47 | json_msg = bytes(json_msg, "utf-8") 48 | 49 | # Prepend the length of the string to the meg 50 | json_msg = struct.pack(">I", len(json_msg)) + json_msg 51 | 52 | self.request.send(json_msg) 53 | 54 | logging.info("Sent: " + json.dumps(msg)) 55 | 56 | def __receive(self): 57 | """Receive a message from the client, decode it from JSON and return. 58 | 59 | The received messages are utf-8 encoded bytes. False is returned on 60 | failure to connect to the client, otherwise a string of the message is 61 | returned. 62 | """ 63 | 64 | raw_msg_length = self.__receive_length(4) 65 | if not raw_msg_length: 66 | return False 67 | msg_length = struct.unpack(">I", raw_msg_length)[0] 68 | 69 | recv = self.__receive_length(msg_length) 70 | 71 | if recv == b"": 72 | # The connection is closed. 73 | return False 74 | 75 | recv_json = str(recv, encoding="utf-8") 76 | recv_string = json.loads(recv_json) 77 | 78 | logging.info("Received: " + str(recv_string)) 79 | return recv_string 80 | 81 | def __receive_length(self, length): 82 | """Receive length number of bytes from the client.""" 83 | 84 | data = b"" 85 | while len(data) < length: 86 | 87 | ready = select.select([self.request], [], [], TIMEOUT) 88 | 89 | if ready[0]: 90 | 91 | packet = self.request.recv(length - len(data)) 92 | if not packet: 93 | return b"" 94 | data += packet 95 | 96 | else: 97 | logging.warning("Waited too long to recieve from the client.") 98 | return b"" 99 | 100 | return data 101 | 102 | def __make_connection(self): 103 | """Handle first request to server and check that it adheres to the 104 | protocol. 105 | """ 106 | 107 | def protocol_error(): 108 | # Invalid connection protocol 109 | logging.error( 110 | "Client attempted to connect using and invalid protocol." 111 | ) 112 | self.__send("PROTOCOL ERROR") 113 | self.__close_connection() 114 | return False 115 | 116 | data = self.__receive() 117 | 118 | if type(data) != list: 119 | return protocol_error() 120 | 121 | if len(data) != 2: 122 | return protocol_error() 123 | 124 | if data[0] != "SPREADSHEET": 125 | return protocol_error() 126 | 127 | # If there is a KeyError when looking up the spreadsheets name, wait 128 | # a bit and try again 129 | 130 | max_attempts = self.server.monitor_frequency + 1 131 | attempt = 0 132 | 133 | while 1: 134 | if attempt >= max_attempts: # soffice process isin't coming up 135 | logging.debug("Waited too long for spreadsheet") 136 | # We can assume the spreadsheet does not exist 137 | self.__send("NOT FOUND") 138 | self.__close_connection() 139 | return False 140 | 141 | try: 142 | self.con = SpreadsheetConnection( 143 | self.server.spreadsheets[data[1]], 144 | self.server.locks[data[1]], 145 | self.server.save_path, 146 | ) 147 | break 148 | 149 | except KeyError: 150 | pass 151 | 152 | attempt += 1 153 | logging.debug("Waiting for spreadsheet") 154 | sleep(1) 155 | 156 | # If the spreadsheet was sucessfully connected to 157 | if attempt != max_attempts: 158 | self.__send("OK") 159 | self.con.lock_spreadsheet() 160 | return True 161 | 162 | def __close_connection(self): 163 | """Unlock the spreadsheet and close the connection to the client.""" 164 | 165 | try: 166 | if self.con.lock.locked: 167 | self.con.unlock_spreadsheet() 168 | except (UnboundLocalError, AttributeError): 169 | # con was never created. 170 | pass 171 | 172 | try: 173 | self.request.shutdown(SHUT_RDWR) 174 | except OSError: 175 | # The client has already disconnected. 176 | pass 177 | 178 | logging.debug("Closing socket for ThreadedTCPRequestHandler") 179 | self.request.close() 180 | 181 | def __main_loop(self): 182 | while True: 183 | data = self.__receive() 184 | 185 | if data == False: 186 | # The connection has been lost. 187 | break 188 | 189 | elif data[0] == "SET": 190 | try: 191 | self.con.set_cells(data[1], data[2], data[3]) 192 | except (ValueError, RuntimeException) as e: 193 | self.__send({"ERROR": str(e)}) 194 | else: 195 | self.__send("OK") 196 | 197 | elif data[0] == "GET": 198 | try: 199 | cells = self.con.get_cells(data[1], data[2]) 200 | except (ValueError, RuntimeException) as e: 201 | self.__send({"ERROR": str(e)}) 202 | else: 203 | self.__send(cells) 204 | 205 | elif data[0] == "GET_SHEETS": 206 | sheet_names = self.con.get_sheet_names() 207 | self.__send(sheet_names) 208 | 209 | elif data[0] == "SAVE": 210 | try: 211 | self.con.save_spreadsheet(data[1]) 212 | except (IOException, OSError) as e: 213 | self.__send({"ERROR": str(e)}) 214 | else: 215 | self.__send("OK") 216 | 217 | def handle(self): 218 | """Make a connection to the client, run the main protocol loop and 219 | close the connection. 220 | """ 221 | 222 | if self.__make_connection(): 223 | self.__main_loop() 224 | self.__close_connection() 225 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | psutil==5.7.0 2 | pyoo==1.4 3 | Werkzeug==1.0.1 4 | -------------------------------------------------------------------------------- /saved_spreadsheets/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore 5 | 6 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2016 Robert Scott 2 | 3 | # This program is free software; you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License 5 | # as published by the Free Software Foundation; either version 2 6 | # of the License, or (at your option) any later version. 7 | 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 | 17 | import platform 18 | import os 19 | import sys 20 | import tempfile 21 | 22 | import logging 23 | import pyoo 24 | import subprocess 25 | import threading 26 | from time import sleep 27 | from request_handler import ThreadedTCPRequestHandler, ThreadedTCPServer 28 | from monitor import MonitorThread 29 | from signal import SIGTERM 30 | import fileinput 31 | import psutil 32 | import socket 33 | 34 | this_dir = os.path.dirname(os.path.realpath(__file__)) 35 | SAVE_PATH = os.path.join(this_dir, "saved_spreadsheets") 36 | SPREADSHEETS_PATH = os.path.join(this_dir, "spreadsheets") 37 | SOFFICE_LOG = os.path.join(this_dir, "log", "soffice.log") 38 | LOG_FILE = os.path.join(this_dir, "log", "server.log") 39 | 40 | SOFFICE_PROCNAME = "soffice.bin" 41 | HOST, PORT = "localhost", 5555 42 | SOFFICE_PIPE = "soffice_headless" 43 | MONITOR_FREQ = 5 # In seconds 44 | LOG_LEVEL = logging.DEBUG 45 | 46 | 47 | class SpreadsheetServer: 48 | def __init__( 49 | self, 50 | soffice_log=SOFFICE_LOG, 51 | host=HOST, 52 | port=PORT, 53 | soffice_pipe=SOFFICE_PIPE, 54 | spreadsheets_path=SPREADSHEETS_PATH, 55 | monitor_frequency=MONITOR_FREQ, 56 | reload_on_disk_change=True, 57 | ask_kill=False, 58 | save_path=SAVE_PATH, 59 | log_level=LOG_LEVEL, 60 | log_file=LOG_FILE, 61 | ): 62 | 63 | # Where the output from LibreOffice is logged to 64 | self.soffice_log = soffice_log 65 | 66 | self.host = host # The address on which the server is listening 67 | self.port = port # The port on which the server is listening 68 | 69 | # The name of the pipe set up by LibreOffice that pyoo will connect to. 70 | self.soffice_pipe = soffice_pipe 71 | 72 | # The frequency, in seconds, at which the directory containing the 73 | # spreadsheets is polled. 74 | self.monitor_frequency = monitor_frequency 75 | 76 | # Whether or not to close and open a spreadsheet with the file changes on 77 | # disk 78 | self.reload_on_disk_change = reload_on_disk_change 79 | 80 | # Whether or not to interactively ask the user if they want to kill an 81 | # existing LibreOffice process. 82 | self.ask_kill = ask_kill 83 | 84 | # Where to save the spreadsheets when requested by the client. 85 | self.save_path = save_path 86 | 87 | # Where to look for spreadsheets to load. 88 | self.spreadsheets_path = spreadsheets_path 89 | 90 | self.spreadsheets = {} # Each pyoo spreadsheet object. 91 | self.locks = {} # A lock for each spreadsheet. 92 | self.hashes = {} # A hash of the file contents for each spreadsheet. 93 | 94 | self.log_level = log_level 95 | self.log_file = log_file # Where 'logging' logs to 96 | 97 | self.libreoffice_temp_dir = tempfile.TemporaryDirectory() 98 | 99 | def __logging(self): 100 | """Set up logging.""" 101 | 102 | logging.basicConfig( 103 | format="%(asctime)s:%(levelname)s:%(message)s", 104 | datefmt="%Y%m%d %H:%M:%S", 105 | # filename=self.log_file, 106 | level=self.log_level, 107 | ) 108 | 109 | def __start_soffice(self): 110 | def get_pid(name): 111 | for proc in psutil.process_iter(): 112 | try: 113 | if proc.name() == SOFFICE_PROCNAME: 114 | return proc.pid 115 | except psutil.ZombieProcess: 116 | pass 117 | return False 118 | 119 | def get_soffice_binay_path(): 120 | try: 121 | return str( 122 | subprocess.check_output(["which", "soffice"])[:-1], 123 | encoding="utf-8", 124 | ) 125 | except subprocess.CalledProcessError: 126 | raise RuntimeError( 127 | "The soffice binary was not found. Is LibreOffice installed?" 128 | ) 129 | 130 | def kill_process(pid): 131 | logging.warning("Killing existing LibreOffice process") 132 | process = psutil.Process(pid=pid) 133 | process.terminate() 134 | process.wait() 135 | 136 | soffice_path = get_soffice_binay_path() 137 | 138 | command = ( 139 | soffice_path 140 | + " -env:UserInstallation=file://" 141 | + self.libreoffice_temp_dir.name 142 | + ' --accept="pipe,name=' 143 | + self.soffice_pipe 144 | + ';urp;" --norestore --nologo --nodefault --headless --invisible --nocrashreport --nofirststartwizard' 145 | ) 146 | 147 | self.logfile = open(self.soffice_log, "w") 148 | self.soffice_process = subprocess.Popen( 149 | command, shell=True, stdout=self.logfile, stderr=self.logfile 150 | ) 151 | 152 | def __connect_to_soffice(self): 153 | """Make a connection to soffice and fail if it can not connect.""" 154 | 155 | MAX_ATTEMPTS = 10 156 | 157 | attempt = 0 158 | while 1: 159 | if attempt > MAX_ATTEMPTS: # soffice process isin't coming up 160 | raise RuntimeError( 161 | "Could not connect to the soffice process. Did LibreOffice start?" 162 | ) 163 | 164 | try: 165 | self.soffice = pyoo.Desktop(pipe=SOFFICE_PIPE) 166 | logging.info("Connected to soffice.") 167 | break 168 | 169 | except (OSError, IOError): 170 | attempt += 1 171 | sleep(1) # Wait for the soffice process to start 172 | 173 | except Exception: 174 | exc_type, exc_obj, exc_tb = sys.exc_info() 175 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] 176 | print(exc_type, fname, exc_tb.tb_lineno) 177 | 178 | def __start_threaded_tcp_server(self): 179 | """Set up and start the TCP threaded server to handle incomming 180 | requests. 181 | """ 182 | 183 | logging.info("Starting spreadsheet_server.") 184 | 185 | MAX_ATTEMPTS = 10 186 | attempt = 0 187 | 188 | def start_threaded_tcp_server(attempt): 189 | try: 190 | self.server = ThreadedTCPServer( 191 | self.save_path, 192 | (self.host, self.port), 193 | ThreadedTCPRequestHandler, 194 | ) 195 | 196 | except (OSError, socket.error): 197 | attempt += 1 198 | 199 | if attempt > MAX_ATTEMPTS: 200 | import traceback 201 | 202 | traceback.print_exc() 203 | print( 204 | "Error: The port is in use. Maybe the server is already" 205 | "running?" 206 | ) 207 | exit() 208 | 209 | sleep(1) 210 | start_threaded_tcp_server(attempt) # Try again 211 | 212 | start_threaded_tcp_server(attempt) 213 | 214 | self.server.spreadsheets = self.spreadsheets 215 | self.server.locks = self.locks 216 | self.server.hashes = self.hashes 217 | self.server.monitor_frequency = self.monitor_frequency 218 | 219 | # Start the main server thread. This server thread will start a 220 | # new thread to handle each client connection. 221 | 222 | self.server_thread = threading.Thread(target=self.server.serve_forever) 223 | 224 | self.server_thread.daemon = False # Gracefully stop child threads 225 | self.server_thread.start() 226 | 227 | logging.info("Server thread running. Waiting on connections...") 228 | 229 | def __start_monitor_thread(self): 230 | """This thread monitors the SPREADSHEETS directory to add or remove. 231 | """ 232 | 233 | self.monitor_thread = MonitorThread( 234 | self.spreadsheets, 235 | self.locks, 236 | self.hashes, 237 | self.soffice, 238 | self.spreadsheets_path, 239 | self.monitor_frequency, 240 | self.reload_on_disk_change, 241 | ) 242 | 243 | self.monitor_thread.daemon = True 244 | self.monitor_thread.start() 245 | 246 | def __stop_monitor_thread(self): 247 | """Stop the monitor thread.""" 248 | 249 | self.monitor_thread.stop_thread() 250 | self.monitor_thread.join() 251 | 252 | def __stop_threaded_tcp_server(self): 253 | """Stop the ThreadedTCPServer.""" 254 | 255 | try: 256 | self.server.shutdown() 257 | self.server.server_close() 258 | 259 | except AttributeError: 260 | # The server was never set up 261 | pass 262 | 263 | def __kill_libreoffice(self): 264 | """Terminate the soffice.bin process.""" 265 | 266 | self.soffice_process.terminate() 267 | self.soffice_process.wait() 268 | self.libreoffice_temp_dir.cleanup() 269 | 270 | def __close_logfile(self): 271 | """Close the logfile.""" 272 | 273 | self.logfile.close() 274 | 275 | def stop(self): 276 | """Stop all the threads and shutdown LibreOffice.""" 277 | 278 | self.__stop_monitor_thread() 279 | self.__stop_threaded_tcp_server() 280 | self.__kill_libreoffice() 281 | self.__close_logfile() 282 | 283 | def run(self): 284 | self.__logging() 285 | self.__start_soffice() 286 | self.__connect_to_soffice() 287 | self.__start_monitor_thread() 288 | self.__start_threaded_tcp_server() 289 | 290 | 291 | if __name__ == "__main__": 292 | print("Starting spreadsheet_server...") 293 | 294 | spreadsheet_server = SpreadsheetServer(ask_kill=True) 295 | try: 296 | print("Logging to: " + spreadsheet_server.log_file) 297 | print("Connecting to LibreOffice...") 298 | spreadsheet_server.run() 299 | print("Up and listening for connections!") 300 | while True: 301 | sleep(100) 302 | 303 | except KeyboardInterrupt: 304 | print("Shutting down server. Please wait...") 305 | spreadsheet_server.stop() 306 | -------------------------------------------------------------------------------- /spreadsheets/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore 5 | 6 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robsco-git/spreadsheet_server/29ed8837be2674281c8be74aa466b959db1901fe/tests/__init__.py -------------------------------------------------------------------------------- /tests/context.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | 4 | sys.path.insert(0, os.path.abspath("..")) 5 | 6 | from connection import SpreadsheetConnection 7 | from server import SpreadsheetServer 8 | from monitor import MonitorThread 9 | from request_handler import ThreadedTCPServer, ThreadedTCPRequestHandler 10 | from client import SpreadsheetClient 11 | -------------------------------------------------------------------------------- /tests/example.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robsco-git/spreadsheet_server/29ed8837be2674281c8be74aa466b959db1901fe/tests/example.ods -------------------------------------------------------------------------------- /tests/test_client.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from .context import SpreadsheetServer, SpreadsheetClient 3 | from time import sleep 4 | import os 5 | import shutil 6 | import sys 7 | import logging 8 | 9 | EXAMPLE_SPREADSHEET = "example.ods" 10 | SOFFICE_PIPE = "soffice_headless" 11 | SPREADSHEETS_PATH = "./spreadsheets" 12 | TESTS_PATH = "./tests" 13 | SHEET_NAME = "Sheet1" 14 | 15 | 16 | class TestClient(unittest.TestCase): 17 | @classmethod 18 | def setUpClass(cls): 19 | # Copy the example spreadsheet from the tests directory into the spreadsheets 20 | # directory 21 | 22 | shutil.copyfile( 23 | TESTS_PATH + "/" + EXAMPLE_SPREADSHEET, 24 | SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET, 25 | ) 26 | 27 | cls.server = SpreadsheetServer(log_level=logging.CRITICAL) 28 | cls.server.run() 29 | 30 | @classmethod 31 | def tearDownClass(cls): 32 | cls.server.stop() 33 | os.remove(SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET) 34 | 35 | def setUp(self): 36 | self.sc = SpreadsheetClient(EXAMPLE_SPREADSHEET) 37 | 38 | def tearDown(self): 39 | self.sc.disconnect() 40 | 41 | def test_connect_invalid_spreadsheet(self): 42 | try: 43 | SpreadsheetClient(EXAMPLE_SPREADSHEET + "z") 44 | self.assertTrue(False) 45 | except RuntimeError as e: 46 | self.assertEqual( 47 | str(e), "The requested spreadsheet was not found." 48 | ) 49 | 50 | # Give the ThreadedTCPServer some time to shut down correctly before 51 | # the next test 52 | sleep(1) 53 | 54 | def test_get_sheet_names(self): 55 | sheet_names = self.sc.get_sheet_names() 56 | self.assertEqual(sheet_names, ["Sheet1"]) 57 | 58 | def test_set_cell(self): 59 | self.sc.set_cells(SHEET_NAME, "A1", 5) 60 | a1 = self.sc.get_cells(SHEET_NAME, "A1") 61 | self.assertEqual(a1, 5) 62 | 63 | def test_set_cell_invalid_sheet(self): 64 | try: 65 | self.sc.set_cells(SHEET_NAME + "z", "A1", 5) 66 | self.assertTrue(False) 67 | except RuntimeError as e: 68 | self.assertEqual(str(e), "Sheet name is invalid.") 69 | 70 | def test_get_cell(self): 71 | cell_value = self.sc.get_cells(SHEET_NAME, "C3") 72 | self.assertEqual(cell_value, 6) 73 | 74 | def test_get_cell_invalid_sheet(self): 75 | try: 76 | self.sc.get_cells(SHEET_NAME + "z", "C3") 77 | self.assertTrue(False) 78 | except RuntimeError as e: 79 | self.assertEqual(str(e), "Sheet name is invalid.") 80 | 81 | def test_get_invalid_cell_numeric(self): 82 | try: 83 | self.sc.get_cells(SHEET_NAME, 1) 84 | self.assertTrue(False) 85 | except RuntimeError as e: 86 | self.assertEqual(str(e), "Cell range is invalid.") 87 | 88 | def test_get_invalid_cell_missing_alpha(self): 89 | try: 90 | self.sc.get_cells(SHEET_NAME, "1") 91 | self.assertTrue(False) 92 | except RuntimeError as e: 93 | self.assertEqual(str(e), "Cell range is invalid.") 94 | 95 | def test_get_invalid_cell_missing_numeric(self): 96 | try: 97 | self.sc.get_cells(SHEET_NAME, "A") 98 | self.assertTrue(False) 99 | except RuntimeError as e: 100 | self.assertEqual(str(e), "Cell range is invalid.") 101 | 102 | def test_get_invalid_cell_missing_start_numeric(self): 103 | try: 104 | self.sc.get_cells(SHEET_NAME, "A:B2") 105 | self.assertTrue(False) 106 | except RuntimeError as e: 107 | self.assertEqual(str(e), "Cell range is invalid.") 108 | 109 | def test_get_invalid_cell_missing_end_numeric(self): 110 | try: 111 | self.sc.get_cells(SHEET_NAME, "A1:B") 112 | self.assertTrue(False) 113 | except RuntimeError as e: 114 | self.assertEqual(str(e), "Cell range is invalid.") 115 | 116 | def test_get_invalid_cell_missing_start_alpha(self): 117 | try: 118 | self.sc.get_cells(SHEET_NAME, "1:B2") 119 | self.assertTrue(False) 120 | except RuntimeError as e: 121 | self.assertEqual(str(e), "Cell range is invalid.") 122 | 123 | def test_get_invalid_cell_missing_end_alpha(self): 124 | try: 125 | self.sc.get_cells(SHEET_NAME, "A1:2") 126 | self.assertTrue(False) 127 | except RuntimeError as e: 128 | self.assertEqual(str(e), "Cell range is invalid.") 129 | 130 | def test_get_invalid_cell_negative(self): 131 | try: 132 | self.sc.get_cells(SHEET_NAME, "A-1:B2") 133 | self.assertTrue(False) 134 | except RuntimeError as e: 135 | self.assertEqual(str(e), "Cell range is invalid.") 136 | 137 | def test_get_invalid_cell_numeric_too_large(self): 138 | try: 139 | self.sc.get_cells(SHEET_NAME, "A1048577") 140 | self.assertTrue(False) 141 | except RuntimeError as e: 142 | self.assertEqual(str(e), "Cell range is invalid.") 143 | 144 | def test_get_invalid_cell_alpha_too_large(self): 145 | try: 146 | self.sc.get_cells(SHEET_NAME, "AMK1") 147 | self.assertTrue(False) 148 | except RuntimeError as e: 149 | self.assertEqual(str(e), "Cell range is invalid.") 150 | 151 | def test_set_cell_row(self): 152 | cell_values = [4, 5, 6] 153 | self.sc.set_cells(SHEET_NAME, "A1:A3", cell_values) 154 | 155 | saved_values = self.sc.get_cells(SHEET_NAME, "A1:A3") 156 | self.assertEqual(cell_values, saved_values) 157 | 158 | def test_get_cell_column(self): 159 | cell_values = self.sc.get_cells(SHEET_NAME, "C1:C3") 160 | self.assertEqual(cell_values, [3, 3.5, 6]) 161 | 162 | def test_get_cell_column_large_alpha(self): 163 | cell_values = self.sc.get_cells(SHEET_NAME, "AF5:AF186") 164 | self.assertEqual(cell_values, ["" for x in range(5, 186 + 1)]) 165 | 166 | def test_set_cell_range(self): 167 | cell_values = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 168 | self.sc.set_cells(SHEET_NAME, "A1:C3", cell_values) 169 | 170 | saved_values = self.sc.get_cells(SHEET_NAME, "A1:C3") 171 | self.assertEqual(cell_values, saved_values) 172 | 173 | def test_save_spreadsheet(self): 174 | filename = "test.ods" 175 | self.sc.save_spreadsheet(filename) 176 | 177 | dir_path = os.path.dirname(os.path.realpath(__file__)) 178 | 179 | saved_path = dir_path + "/../saved_spreadsheets/" + filename 180 | self.assertTrue(os.path.exists(saved_path)) 181 | 182 | os.remove(saved_path) 183 | 184 | def test_unicode(self): 185 | for i in range(1000): 186 | self.sc.set_cells(SHEET_NAME, "A1", chr(i)) 187 | cell = self.sc.get_cells(SHEET_NAME, "A1") 188 | try: 189 | int(chr(i)) 190 | continue 191 | except ValueError: 192 | pass # Not a number 193 | 194 | self.assertEqual(chr(i), cell) 195 | 196 | 197 | if __name__ == "__main__": 198 | unittest.main() 199 | -------------------------------------------------------------------------------- /tests/test_connection.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | import threading 4 | import unittest 5 | from signal import SIGTERM 6 | 7 | from .context import SpreadsheetConnection, SpreadsheetServer 8 | 9 | EXAMPLE_SPREADSHEET = "example.ods" 10 | SOFFICE_PIPE = "soffice_headless" 11 | SPREADSHEETS_PATH = "./spreadsheets" 12 | TESTS_PATH = "./tests" 13 | 14 | 15 | class TestConnection(unittest.TestCase): 16 | @classmethod 17 | def setUpClass(cls): 18 | # Copy the example spreadsheet from the tests directory into the spreadsheets 19 | # directory 20 | 21 | shutil.copyfile( 22 | TESTS_PATH + "/" + EXAMPLE_SPREADSHEET, 23 | SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET, 24 | ) 25 | 26 | cls.spreadsheet_server = SpreadsheetServer() 27 | cls.spreadsheet_server._SpreadsheetServer__start_soffice() 28 | cls.spreadsheet_server._SpreadsheetServer__connect_to_soffice() 29 | 30 | @classmethod 31 | def tearDownClass(cls): 32 | cls.spreadsheet_server._SpreadsheetServer__kill_libreoffice() 33 | cls.spreadsheet_server._SpreadsheetServer__close_logfile() 34 | os.remove(SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET) 35 | 36 | def setUp(self): 37 | soffice = self.spreadsheet_server.soffice 38 | self.spreadsheet = soffice.open_spreadsheet( 39 | SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET 40 | ) 41 | 42 | lock = threading.Lock() 43 | self.ss_con = SpreadsheetConnection( 44 | self.spreadsheet, lock, self.spreadsheet_server.save_path 45 | ) 46 | 47 | def tearDown(self): 48 | self.spreadsheet.close() 49 | 50 | def test_lock_spreadsheet(self): 51 | self.ss_con.lock_spreadsheet() 52 | self.assertTrue(self.ss_con.lock.locked()) 53 | self.ss_con.unlock_spreadsheet() 54 | 55 | def test_unlock_spreadsheet(self): 56 | self.ss_con.lock_spreadsheet() 57 | status = self.ss_con.unlock_spreadsheet() 58 | self.assertTrue(status) 59 | self.assertFalse(self.ss_con.lock.locked()) 60 | 61 | def test_unlock_spreadsheet_runtime_error(self): 62 | status = self.ss_con.unlock_spreadsheet() 63 | self.assertFalse(status) 64 | self.assertFalse(self.ss_con.lock.locked()) 65 | 66 | def test_get_xy_index_first_cell(self): 67 | ( 68 | alpha_index, 69 | num_index, 70 | ) = self.ss_con._SpreadsheetConnection__get_xy_index(u"A1") 71 | self.assertEqual(alpha_index, 0) 72 | self.assertEqual(num_index, 0) 73 | 74 | def test_get_xy_index_Z26(self): 75 | ( 76 | alpha_index, 77 | num_index, 78 | ) = self.ss_con._SpreadsheetConnection__get_xy_index(u"Z26") 79 | self.assertEqual(alpha_index, 25) 80 | self.assertEqual(num_index, 25) 81 | 82 | def test_get_xy_index_aa3492(self): 83 | ( 84 | alpha_index, 85 | num_index, 86 | ) = self.ss_con._SpreadsheetConnection__get_xy_index(u"AA3492") 87 | self.assertEqual(alpha_index, 26) 88 | self.assertEqual(num_index, 3491) 89 | 90 | def test_get_xy_index_aaa1024(self): 91 | ( 92 | alpha_index, 93 | num_index, 94 | ) = self.ss_con._SpreadsheetConnection__get_xy_index(u"AAA1024") 95 | self.assertEqual(alpha_index, 702) 96 | self.assertEqual(num_index, 1023) 97 | 98 | def test_get_xy_index_aab739(self): 99 | ( 100 | alpha_index, 101 | num_index, 102 | ) = self.ss_con._SpreadsheetConnection__get_xy_index(u"AAB739") 103 | self.assertEqual(alpha_index, 703) 104 | self.assertEqual(num_index, 738) 105 | 106 | def test_get_xy_index_aba1(self): 107 | ( 108 | alpha_index, 109 | num_index, 110 | ) = self.ss_con._SpreadsheetConnection__get_xy_index(u"ABA1") 111 | self.assertEqual(alpha_index, 728) 112 | self.assertEqual(num_index, 0) 113 | 114 | def test_get_xy_index_abc123(self): 115 | ( 116 | alpha_index, 117 | num_index, 118 | ) = self.ss_con._SpreadsheetConnection__get_xy_index(u"ABC123") 119 | self.assertEqual(alpha_index, 730) 120 | self.assertEqual(num_index, 122) 121 | 122 | def test_get_xy_index_last_cell(self): 123 | ( 124 | alpha_index, 125 | num_index, 126 | ) = self.ss_con._SpreadsheetConnection__get_xy_index(u"AMJ1048576") 127 | self.assertEqual(alpha_index, 1023) 128 | self.assertEqual(num_index, 1048575) 129 | 130 | def test_is_single_cell(self): 131 | status = self.ss_con._SpreadsheetConnection__is_single_cell(u"AMJ1") 132 | self.assertTrue(status) 133 | 134 | def test_is_not_single_cell(self): 135 | status = self.ss_con._SpreadsheetConnection__is_single_cell(u"A1:Z26") 136 | self.assertFalse(status) 137 | 138 | def test_check_not_single_cell(self): 139 | status = True 140 | try: 141 | self.ss_con._SpreadsheetConnection__check_single_cell(u"DD56:Z98") 142 | except ValueError: 143 | status = False 144 | 145 | self.assertFalse(status) 146 | 147 | def test_cell_to_index(self): 148 | d = self.ss_con._SpreadsheetConnection__cell_to_index(u"ABC945") 149 | 150 | self.assertTrue(d["row_index"] == 944) 151 | self.assertTrue(d["column_index"] == 730) 152 | 153 | def test_cell_range_to_index(self): 154 | d = self.ss_con._SpreadsheetConnection__cell_range_to_index(u"C9:Z26") 155 | 156 | self.assertTrue(d["row_start"] == 8) 157 | self.assertTrue(d["row_end"] == 25) 158 | 159 | self.assertTrue(d["column_start"] == 2) 160 | self.assertTrue(d["column_end"] == 25) 161 | 162 | def test_check_for_lock(self): 163 | status = False 164 | try: 165 | self.ss_con._SpreadsheetConnection__check_for_lock() 166 | except RuntimeError: 167 | status = True 168 | 169 | self.assertTrue(status) 170 | 171 | def test_check_numeric(self): 172 | value = self.ss_con._SpreadsheetConnection__convert_to_float_if_numeric( 173 | "1" 174 | ) 175 | self.assertTrue(type(value) is float) 176 | 177 | def test_check_not_numeric(self): 178 | value = self.ss_con._SpreadsheetConnection__convert_to_float_if_numeric( 179 | u"123A" 180 | ) 181 | 182 | self.assertTrue(type(value) is str) 183 | 184 | def test_check_not_list(self): 185 | status = False 186 | try: 187 | self.ss_con._SpreadsheetConnection__check_list(1) 188 | except ValueError: 189 | status = True 190 | 191 | self.assertTrue(status) 192 | 193 | def test_check_1D_list(self): 194 | data = ["1", "2", "3"] 195 | data = self.ss_con._SpreadsheetConnection__check_1D_list(data) 196 | 197 | self.assertEqual(data, [1.0, 2.0, 3.0]) 198 | 199 | def test_check_1D_list_when_2D(self): 200 | status = False 201 | data = [["1", "2", "3"], ["1", "2", "3"]] 202 | try: 203 | data = self.ss_con._SpreadsheetConnection__check_1D_list(data) 204 | except ValueError: 205 | status = True 206 | 207 | self.assertTrue(status) 208 | 209 | def test_invalid_sheet_name(self): 210 | self.ss_con.lock_spreadsheet() 211 | status = False 212 | try: 213 | self.ss_con.set_cells(u"1Sheet1", u"A1", 1) 214 | except ValueError: 215 | status = True 216 | self.ss_con.unlock_spreadsheet() 217 | 218 | self.assertTrue(status) 219 | 220 | def test_set_single_cell(self): 221 | self.ss_con.lock_spreadsheet() 222 | self.ss_con.set_cells(u"Sheet1", u"A1", 1) 223 | self.assertEqual(self.ss_con.get_cells(u"Sheet1", u"A1"), 1) 224 | self.ss_con.unlock_spreadsheet() 225 | 226 | def test_set_single_cell_list_of_data(self): 227 | self.ss_con.lock_spreadsheet() 228 | status = False 229 | try: 230 | self.ss_con.set_cells(u"Sheet1", u"A1", [9, 1]) 231 | except ValueError: 232 | status = True 233 | 234 | self.assertTrue(status) 235 | self.assertNotEqual(self.ss_con.get_cells(u"Sheet1", u"A1"), 9) 236 | self.ss_con.unlock_spreadsheet() 237 | 238 | def test_set_cell_range_columnn(self): 239 | self.ss_con.lock_spreadsheet() 240 | self.ss_con.set_cells(u"Sheet1", u"A1:A5", [1, 2, 3, 4, 5]) 241 | self.assertEqual( 242 | self.ss_con.get_cells(u"Sheet1", u"A1:A5"), 243 | (1.0, 2.0, 3.0, 4.0, 5.0), 244 | ) 245 | self.ss_con.unlock_spreadsheet() 246 | 247 | def test_set_cell_range_row(self): 248 | self.ss_con.lock_spreadsheet() 249 | self.ss_con.set_cells(u"Sheet1", u"A1:E1", [1, 2, 3, 4, 5]) 250 | self.assertEqual( 251 | self.ss_con.get_cells(u"Sheet1", u"A1:E1"), 252 | (1.0, 2.0, 3.0, 4.0, 5.0), 253 | ) 254 | self.ss_con.unlock_spreadsheet() 255 | 256 | def test_set_cell_range_2D(self): 257 | self.ss_con.lock_spreadsheet() 258 | self.ss_con.set_cells(u"Sheet1", u"A1:B2", [[1, 2], [3, 4]]) 259 | self.assertEqual( 260 | self.ss_con.get_cells(u"Sheet1", u"A1:B2"), 261 | ((1.0, 2.0), (3.0, 4.0)), 262 | ) 263 | self.ss_con.unlock_spreadsheet() 264 | 265 | def test_set_cell_range_2D_incorrect_data(self): 266 | self.ss_con.lock_spreadsheet() 267 | status = False 268 | try: 269 | self.ss_con.set_cells(u"Sheet1", u"A1:B2", [9, 9, 9, 9]) 270 | except ValueError: 271 | status = True 272 | 273 | self.assertTrue(status) 274 | 275 | self.assertNotEqual( 276 | self.ss_con.get_cells(u"Sheet1", u"A1:B2"), 277 | ((9.0, 9.0), (9.0, 9.0)), 278 | ) 279 | self.ss_con.unlock_spreadsheet() 280 | 281 | def test_get_sheet_names(self): 282 | sheet_names = self.ss_con.get_sheet_names() 283 | self.assertEqual(sheet_names, [u"Sheet1"]) 284 | 285 | def test_save_spreadsheet(self): 286 | path = "./saved_spreadsheets/" + EXAMPLE_SPREADSHEET + ".new" 287 | 288 | if os.path.exists(path): 289 | os.remove(path) 290 | 291 | self.ss_con.lock_spreadsheet() 292 | status = self.ss_con.save_spreadsheet(EXAMPLE_SPREADSHEET + ".new") 293 | self.assertTrue(status) 294 | self.assertTrue(os.path.exists(path)) 295 | self.ss_con.unlock_spreadsheet() 296 | 297 | def test_save_spreadsheet_no_lock(self): 298 | path = "./saved_spreadsheets/" + EXAMPLE_SPREADSHEET + ".new" 299 | 300 | if os.path.exists(path): 301 | os.remove(path) 302 | 303 | status = self.ss_con.save_spreadsheet(EXAMPLE_SPREADSHEET + ".new") 304 | self.assertFalse(status) 305 | self.assertFalse( 306 | os.path.exists( 307 | "./saved_spreadsheets/" + EXAMPLE_SPREADSHEET + ".new" 308 | ) 309 | ) 310 | 311 | 312 | if __name__ == "__main__": 313 | unittest.main() 314 | -------------------------------------------------------------------------------- /tests/test_monitor.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | import unittest 4 | from time import sleep 5 | 6 | from .context import MonitorThread, SpreadsheetClient, SpreadsheetServer 7 | 8 | this_dir = os.path.dirname(os.path.realpath(__file__)) 9 | parent_dir = os.path.dirname(this_dir) 10 | 11 | SPREADSHEETS_PATH = os.path.join(parent_dir, "spreadsheets") 12 | TESTS_PATH = this_dir 13 | SAVED_SPREADSHEETS_PATH = os.path.join(parent_dir, "saved_spreadsheets") 14 | 15 | EXAMPLE_SPREADSHEET = "example.ods" 16 | EXAMPLE_SPREADSHEET_MOVED = "example_moved.ods" 17 | SOFFICE_PIPE = "soffice_headless" 18 | SHEET_NAME = "Sheet1" 19 | 20 | 21 | class TestMonitor(unittest.TestCase): 22 | @classmethod 23 | def setUpClass(cls): 24 | # Copy the example spreadsheet from the tests directory into the spreadsheets 25 | # directory 26 | 27 | shutil.copyfile( 28 | TESTS_PATH + "/" + EXAMPLE_SPREADSHEET, 29 | SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET, 30 | ) 31 | 32 | cls.spreadsheet_server = SpreadsheetServer() 33 | cls.spreadsheet_server._SpreadsheetServer__start_soffice() 34 | cls.spreadsheet_server._SpreadsheetServer__connect_to_soffice() 35 | 36 | @classmethod 37 | def tearDownClass(cls): 38 | cls.spreadsheet_server._SpreadsheetServer__kill_libreoffice() 39 | cls.spreadsheet_server._SpreadsheetServer__close_logfile() 40 | 41 | os.remove(SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET) 42 | 43 | def setUp(self): 44 | self.spreadsheet_server._SpreadsheetServer__start_monitor_thread() 45 | self.monitor_thread = self.spreadsheet_server.monitor_thread 46 | while not self.monitor_thread.initial_scan(): 47 | sleep(0.5) 48 | 49 | def tearDown(self): 50 | self.spreadsheet_server._SpreadsheetServer__stop_monitor_thread() 51 | 52 | def test_unload_spreadsheet(self): 53 | self.monitor_thread._MonitorThread__unload_spreadsheet( 54 | EXAMPLE_SPREADSHEET 55 | ) 56 | 57 | spreadsheets = [ 58 | key for key, value in self.monitor_thread.spreadsheets.items() 59 | ] 60 | 61 | locks = [key for key, value in self.monitor_thread.locks.items()] 62 | 63 | self.assertTrue(EXAMPLE_SPREADSHEET not in spreadsheets) 64 | self.assertTrue(EXAMPLE_SPREADSHEET not in locks) 65 | 66 | def test_check_added_already_exists(self): 67 | self.monitor_thread._MonitorThread__check_added() 68 | 69 | spreadsheets = [ 70 | key for key, value in self.monitor_thread.spreadsheets.items() 71 | ] 72 | 73 | locks = [key for key, value in self.monitor_thread.locks.items()] 74 | 75 | self.assertTrue(EXAMPLE_SPREADSHEET in spreadsheets) 76 | self.assertTrue(EXAMPLE_SPREADSHEET in locks) 77 | 78 | def test_check_removed_when_renamed(self): 79 | # Rename example.ods to example_moved.ods 80 | 81 | current_loc = SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET 82 | moved_loc = SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET_MOVED 83 | 84 | os.rename(current_loc, moved_loc) 85 | 86 | self.monitor_thread.docs = [] 87 | self.monitor_thread._MonitorThread__scan_directory(SPREADSHEETS_PATH) 88 | self.monitor_thread._MonitorThread__check_removed() 89 | self.monitor_thread._MonitorThread__check_added() 90 | 91 | spreadsheets = [ 92 | key for key, value in self.monitor_thread.spreadsheets.items() 93 | ] 94 | 95 | locks = [key for key, value in self.monitor_thread.locks.items()] 96 | 97 | self.assertTrue(EXAMPLE_SPREADSHEET not in spreadsheets) 98 | self.assertTrue(EXAMPLE_SPREADSHEET not in locks) 99 | self.assertTrue(EXAMPLE_SPREADSHEET_MOVED in spreadsheets) 100 | self.assertTrue(EXAMPLE_SPREADSHEET_MOVED in locks) 101 | 102 | # Move it back to where it was 103 | os.rename(moved_loc, current_loc) 104 | 105 | def test_change_file_hash(self): 106 | # Save the example file with a modification 107 | 108 | current_loc = SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET 109 | moved_loc = SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET_MOVED 110 | shutil.copyfile(current_loc, moved_loc) 111 | 112 | self.spreadsheet_server._SpreadsheetServer__start_threaded_tcp_server() 113 | 114 | self.sc = SpreadsheetClient(EXAMPLE_SPREADSHEET_MOVED) 115 | self.sc.set_cells(SHEET_NAME, "A1", 5) 116 | self.sc.save_spreadsheet(EXAMPLE_SPREADSHEET_MOVED) 117 | self.sc.disconnect() 118 | 119 | current_loc = SAVED_SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET_MOVED 120 | moved_loc = SPREADSHEETS_PATH + "/" + EXAMPLE_SPREADSHEET_MOVED 121 | 122 | hash_before = self.monitor_thread.hashes[EXAMPLE_SPREADSHEET_MOVED] 123 | 124 | os.rename(current_loc, moved_loc) 125 | 126 | # Run a scan manually 127 | self.monitor_thread.docs = [] 128 | self.monitor_thread._MonitorThread__scan_directory(SPREADSHEETS_PATH) 129 | self.monitor_thread._MonitorThread__check_removed() 130 | self.monitor_thread._MonitorThread__check_added() 131 | 132 | hash_after = self.monitor_thread.hashes[EXAMPLE_SPREADSHEET_MOVED] 133 | self.assertNotEqual(hash_before, hash_after) 134 | 135 | self.sc = SpreadsheetClient(EXAMPLE_SPREADSHEET_MOVED) 136 | cell = self.sc.get_cells(SHEET_NAME, "A1") 137 | self.sc.save_spreadsheet(EXAMPLE_SPREADSHEET_MOVED) 138 | self.sc.disconnect() 139 | 140 | self.assertEqual(cell, 5) 141 | 142 | self.spreadsheet_server._SpreadsheetServer__stop_threaded_tcp_server() 143 | os.remove(moved_loc) 144 | 145 | 146 | if __name__ == "__main__": 147 | unittest.main() 148 | --------------------------------------------------------------------------------